I have a UDP socket that is bound to INADDR_ANY to listen to packets on all the IPs my server has. I\'m sending out replies through the same socket.
Right now the se
I encountered the same problem recently.
What I do to solve this problem is
Example:
struct ifreq ifr;
...
recvmsg(fd, &msg...)
...
if (msg.msg_controllen >= sizeof(struct cmsghdr))
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
if (cmptr->cmsg_level == SOL_IP && cmptr->cmsg_type == IP_PKTINFO)
{
iface_index = ((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_ifindex;
}
if_indextoname(iface_index , ifr.ifr_name);
mret=setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
sendmsg(...);
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "");
mret=setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));