LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2011, 09:43 AM   #1
lcamp
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Rep: Reputation: 2
Binding to 0.0.0.0 as src addr for UDP Broadcast Message


How do you bind a UDP socket to 0.0.0.0 not INADDR_ANY
I am trying to send a DHCPDISCOVER packet on udp but the address src is INADDR_ANY and is grabbing the address from another interface.

Currently I am broadcasting on a selected interface but still the SRC Address comes up 192.168.*.* when I need the src to be 0.0.0.0 in accordance with RFC.

Code:
	snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),"tap0");


	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);

	memcpy(&ifr.ifr_addr,&src,sizeof(src));



	sockfd = Socket(AF_INET,SOCK_DGRAM,0);
	Bind(sockfd,(SA *) &src,sizeof(src));
	if(ioctl(sockfd,SIOCSIFADDR,&ifr)<0)
		err_quit("ioctl error\n");
	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");
as a note both bcast and src are both struct sockaddr_in and are initialized to zero.
 
Old 01-28-2011, 03:46 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
This??

http://stackoverflow.com/questions/8...cket-in-python
 
Old 01-29-2011, 07:45 AM   #3
lcamp
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 2
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP Broadcast Issue raviskar Programming 2 02-09-2009 05:41 AM
UDP Broadcast Issue raviskar Linux - Networking 2 02-06-2009 07:48 AM
network, broadcast addr scofiled83 Linux - Networking 5 11-10-2008 07:00 AM
broadcast using udp kingraja84 Programming 1 05-23-2007 05:59 AM
how to broadcast UDP datagrams sokar Programming 2 02-14-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 09:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration