user1511417 user1511417 - 6 months ago 33
C Question

Can I detect whether an UDP-socket or a connected UDP socket is used?

Can I detect whether a client application uses an UDP-socket or a connected UDP-socket?

If yes, how? If no, why?


As I said in my comment above, code call connect on a UDP socket. That enforces only traffic to/from the connection address is allowed (and all other packets get dropped) and allows you to use send instead of sendto, but the traffic is still UDP.

But you can use the netstat command from the command line to see if the datagram socket has a remote address association:

For example, imagine if the code did this:

// create a datagram socket that listens on port 12345
sock = socket(AF_INET, SOCK_DGRAM, 0);
port = 12345;
addrLocal.sin_family = AF_INET;
addrLocal.sin_port = htons(port);
result = bind(sock, (sockaddr*)&addrLocal, sizeof(addrLocal));

// associate the socket only with packets arriving from
addrRemote.sin_family = AF_INET;
addrRemote.sin_port = htons(6666);
addrRemote.sin_addr.s_addr = ipaddress;  // e.g. ""
result = connect(sock, (sockaddr*)&addrRemote, sizeof(addrRemote));

A corresponding netstat -a -u will reveal the following:

ubuntu@ip-10-0-0-15:~$ netstat -u -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 ip-10-0-0-15:12345            ESTABLISHED

The presence of a value that isn't *:* in the Foreign Address column for the UDP socket will reveal if the socket has connection address associated with it.