I'm trying to bind my socket to eth0 using SO_BINDTODEVICE
. I am passing eth0 to the variable opt
. My socket name is socketfd
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0) {
perror("socket(): error ");
return 0;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
fprintf(stderr,ifr.ifr_name, sizeof(ifr.ifr_name), opt);
ioctl(sockfd, SIOCGIFINDEX, &ifr);
setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, opt, strlen(opt));
fprintf(stderr, "The bind is done on interface %s \n", opt);
fprintf(stderr, "opt length is %zu \n", strlen(opt));
In order to do some debugging, I'm redirecting the value of opt and strlen(opt) to a log file and they get the values correctly.
cat /home/cayman/log.txt
The bind is done on interface eth0
opt length is 4
I also do a verification by capturing packets on eth0 but I don't see the traffic going through eth0. All what is see is other network related traffic as follows:
tshark -i eth0
1 0.000000 Cisco_51:fd:09 -> Spanning-tree-(for-bridges)_00 STP 62 Conf. Root = 32768/78/00:24:50:51:fd:00 Cost = 0 Port = 0x8009
2 0.326720 192.168.78.1 -> 224.0.0.13 PIMv2 70 Hello
3 1.030538 Cisco_51:fd:09 -> Cisco_51:fd:09 LOOP 62 Reply
4 2.004544 Cisco_51:fd:09 -> Spanning-tree-(for-bridges)_00 STP 62 Conf. Root = 32768/78/00:24:50:51:fd:00 Cost = 0 Port = 0x8009
5 3.254223 192.168.78.1 -> 224.0.0.10 EIGRP 76 Hello
6 4.010168 Cisco_51:fd:09 -> Spanning-tree-(for-bridges)_00 STP 62 Conf. Root = 32768/78/00:24:50:51:fd:00 Cost = 0 Port = 0x8009
7 6.014839 Cisco_51:fd:09 -> Spanning-tree-(for-bridges)_00 STP 62 Conf. Root = 32768/78/00:24:50:51:fd:00 Cost = 0 Port = 0x8009
8 7.896252 192.168.78.1 -> 224.0.0.10 EIGRP 76 Hello
9 8.023003 Cisco_51:fd:09 -> Spanning-tree-(for-bridges)_00 STP 62 Conf. Root = 32768/78/00:24:50:51:fd:00 Cost = 0 Port = 0x8009
I also had a look on this thread and I made sure that I'm running my code as a super-user. I looked also at the man page of SO_BINDTODEVICE but I am not sure what could be missing. Any suggestions please ?