I have a UDP socket which I bind to 0.0.0.0. After doing that, how can I know the exact network interface on which I am receiving the packets? I tried getsockname, but it returns 0.0.0.0.
Asked
Active
Viewed 354 times
0
-
2If the socket is bound to 0.0.0.0 it is not bound to a specific network interface. This means you cannot get the interface of the socket. You can only get the interface a specific UDP packet came in, but the next packet on the socket can already arrive at a different interface. To find out which interface a packet came in see [How to tell which interface the socket received the message from?](https://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from) – Steffen Ullrich Jul 16 '20 at 19:38
-
Yes, this works. I wanted the solution on Windows & Linux. On Windows also there is IP_PKTINFO available through WSARecvMsg API. Your pointer helps. Thanks. – ARaj Jul 20 '20 at 09:32
-
Steffen, is it also possible to fetch the interface type ie. eth0 or wifi etc? – ARaj Jul 21 '20 at 06:31
-
Not directly. But going from interface id to interface properties is a different question and should be asked as a new one. – Steffen Ullrich Jul 21 '20 at 07:08
-
Sure, if there is no direct way, it is a separate context. Agreed. – ARaj Jul 22 '20 at 09:25
1 Answers
0
There is a good code example at How can I get to know the IP address for interfaces in C?. Shortened to your case it's
#define MAX_INTERFACE_COUNT 10
struct ifconf ifc;
struct ifreq ifr[MAX_INTERFACE_COUNT];
ifc.ifc_len = sizeof(ifr);
ifc.ifc_ifcu.ifcu_buf = (caddr_t) ifr;
if (ioctl(sock, SIOCGIFCONF, &ifc)) {
printf("Error getting ifconf structure");
exit(-1);
}
int ifc_num = ifc.ifc_len / sizeof(struct ifreq);
printf("Bound to %d interfaces:\n", ifc_num);
for (int i = 0; i < ifc_num; ++i)
printf(" %d is '%s'\n", i+1, ifr[i].ifr_name);
I'm assuming here sock
is your socket handle and there are not more than MAX_INTERFACE_COUNT
network interfaces (hardware and virtual) at your computer.

fcdt
- 2,371
- 5
- 14
- 26
-
I have 2 interfaces on my system, one is a loopback and other is ethernet. Above code is listing both. I was hoping it would give me ethernet only, from where I am receiving the packet. – ARaj Jul 17 '20 at 14:41
-
Cause you bound your socket to `0.0.0.0`, it receives packages from all interfaces, even the loopback interface (see [here](https://stackoverflow.com/questions/55984824/what-is-the-meaning-of-0-0-0-0-address-while-receiving-broadcast-or-other-packe)). The listing is therefore correct. If you don't want the loopback in your output, filter it e.g. with `if (strcmp(ifr[i].ifr_name," lo "))` in the line before the `printf`. – fcdt Jul 17 '20 at 16:05
-
So, if I have an lo, wifi and eth0 on my system, and I receive a packet on the wifi interface, will above snippet give me 'lo & wifi' OR 'lo & wifi & eth0' ? I don't have a similar setup, hence my ask. – ARaj Jul 17 '20 at 16:13
-
Only the active interfaces (in an indefinite order) should be output, since only they can receive packets. if your wifi is not connected, it could be that this interface is not output. Try `ifconfig` to find out. – fcdt Jul 17 '20 at 16:18
-
Ok, in this case, I am not looking for all the active interfaces, but just the interface on which the packet was received. The solution suggested by Steffen Ullrich is working in this case. – ARaj Jul 20 '20 at 09:30