So the problem is that I want to mimic a DHCPDISCOVER packet and when I bind to INADDR_ANY (0.0.0.0) the other interfaces that are up interfere for example:
eth0: 192.168.1.8
eth1: down
and I want to send the packet on eth1, the packet goes out from a source of 192.168.1.8 even though I binded to 0.0.0.0.
The solution I am using (it's not good) is to bring all interfaces down first
Code:
int sockfd = Socket(AF_INET,SOCK_DGRAM,0); //wrapper
struct ifreq ifr;
bzero(&ifr,sizeof(ifr));
/* specify interface to bring down */
strncpy(ifr.ifr_name,"eth1",sizeof(ifr.ifr_name));
ifr.ifr_flags = 0; //forces IFF_UP to be false
if(ioctl(sockfd,SIOCSIFFLAGS,&ifr)<0)
err_quit("ioctl error\n");
I do this for the interfaces that are up then send the packet and it successfully transmits from 0.0.0.0 to 255.255.255.255 in accordance with DHCP RFC.
Then I have to bring up the interfaces and they're settings are messed up so I have to save all the interfaces' settings and it becomes tedious I would have thought there would be a more simple way to bind to 0.0.0.0 and not have it switch to the INADDR_ANY in the syscall to bind.