Thread: binding to 0.0.0.0

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    9

    binding to 0.0.0.0

    The goal is to send a UDP packet adhering to the bootp and DHCP more specifically.
    now I need to send it from a src ip of 0.0.0.0 to the broadcast address 255.255.255.255
    the issue is that when I bind to 0.0.0.0 it is interpreted as INADDR_ANY
    so what is happening is any interfaces that are 'up' and have an IP address that IP address is used so instead of sending the packet from 0.0.0.0 it's always 192.168.1.8 or whatever the IP address on the other interface is.

    Code:
    	bcast.sin_family = AF_INET;
    	bcast.sin_port = htons(67);
    	bcast.sin_addr.s_addr = INADDR_BROADCAST;
    
    	src.sin_family = AF_INET;
    	src.sin_port = htons(68);
            //src was zero'd earlier in execution so src.sin_addr.s_addr = 0
    	memcpy(&ifr.ifr_addr,&src,sizeof(src));
    
    	snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),"%s",argv[1]);
    	printf("%s\n",ifr.ifr_name);
    
    	/* set IP to INADDR_ANY */
    	if(ioctl(sockfd,SIOCSIFADDR,&ifr)<0) {
    		err_quit("ioctl error\n");
    	}
    
    
    	Bind(sockfd,(SA *) &src,sizeof(src));
    
    	if(setsockopt(sockfd,SOL_SOCKET,SO_BINDTODEVICE,(void *) &ifr,sizeof(ifr)) < 0)
    		err_quit("setsockopt\n");
    	if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,(char *) &flags, sizeof(flags)) < 0)
    		err_quit("setsockopt\n");

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I think other implementations of this protocol use raw sockets, for this, among other, reasons.

    The only way I'm aware of to actually bind to 0.0.0.0 is to ifconfig an interface to that address -- even then, I think the kernel won't like it very much.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Late or early binding?
    By Sharke in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2009, 12:28 PM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. dynamic/static binding
    By faze in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2005, 12:26 PM
  4. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM