Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display socket options

Tags:

linux

shell

How I can see from shell what socket options are set? In particular I'm interesting to know if SO_BROADCAST is set?

like image 644
dimba Avatar asked Dec 01 '09 05:12

dimba


People also ask

How do I show sockets in Linux?

ss command is a tool that is used for displaying network socket related information on a Linux system. The tool displays more detailed information that the netstat command which is used for displaying active socket connections.

What is So_linger socket option?

The SO_LINGER option expects option_value to point to a linger structure, as defined in socket. h. This structure is defined in the following example: struct linger { int l_onoff; ⁄* option on⁄off *⁄ int l_linger; ⁄* linger time *⁄ }; The l_onoff field is set to 0 , if the SO_LINGER option is disabled.

How do I list sockets in Unix?

Examining Unix Domain Sockets. To list all listening Unix Domain Sockets, run the ss -xln command. The x flag ensures that only domain sockets are displayed. Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process . . . u_str LISTEN 0 5 /tmp/stream.


1 Answers

You can use lsof(8). If PID is the process ID and FD is the file descriptor number of the socket you're interested in, you can do this:

lsof -a -p PID -d FD -T f

To list all IPv4 sockets of a process:

lsof -a -p PID -i 4 -T f

This will print out the socket options with a SO=, among other information. Note that if no options are set, you'll get the empty string, so you'll see something like SO=PQLEN=0 etc. To test for SO_BROADCAST, just grep for the string SO_BROADCAST after the SO=, e.g.

if lsof -a -p PID -d FD -T f | grep -q 'SO=[^=]*SO_BROADCAST'; then
    # socket has SO_BROADCAST
else
    # it doesn't
fi
like image 109
Adam Rosenfield Avatar answered Oct 18 '22 09:10

Adam Rosenfield