Project Home
Project Home
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - broadcast UDP with multiple interfaces: (10 Items)
   
broadcast UDP with multiple interfaces  
I want to broadcast on one specific network.  I have two nics, each on a different network.  I assume that I bind the 
socket to the IP address of the interface I want the broadcast to go out on.

So to test my understanding I use inet_aton() and bind the socket.  Unfortunately it still broadcasts on the "other" 
network.

Can someone explain this to me?  Thanx

Tim
Re: broadcast UDP with multiple interfaces  
On Mon, Sep 22, 2008 at 04:47:06PM -0400, Tim Gessner wrote:
> I want to broadcast on one specific network.  I have two nics, each on a different network.  I assume that I bind the 
socket to the IP address of the interface I want the broadcast to go out on.
> 
> So to test my understanding I use inet_aton() and bind the socket.  Unfortunately it still broadcasts on the "other" 
network.
> 
> Can someone explain this to me?  Thanx
> 
> Tim

It's probably just following the routing table.
Here's the broadcast technote which should help.

-seanb
Attachment: Text bcast_technote 2.48 KB
Re: broadcast UDP with multiple interfaces  
Thanx.

I tried using IP_MULTICAST_IF but that didn't seem to work.  What does work is to query the interface for its broadcast 
address using SIOCGIFBRDADDR and then using that address rather than INADDR_BROADCAST.

Thanx again
Tim
Re: broadcast UDP with multiple interfaces  
Ok, I really need help on this one.  I have two nics and I want to broadcast to "255.255.255.255".  What happens is that
 when I use INADDR_BROADCAST I get 192.168.0.255, which is en0.  The source address is 192.168.0.29.

I can deliberately broadcast using en1 in sendto with the broadcast address of en1, 192.168.75.255.  But that is not 
what I want to do.

What I want is to have a source IP of 192.168.75.80 (en1) and dest IP of 255.255.255.255.  At least that is what I think
 I want.

I have tried binding and IP_MULTICAST_IF.  Nothing seems to change the source to en1 and nothing seems to make any 
difference to the dest IP with the exception of changing sendto to the specific subnet.  I have tried to sendto "255.255
.255.255" and it still sends to 192.168.0.255.  This does not seem correct to me at all.

Can anyone help?  Thanx
Tim
Re: broadcast UDP with multiple interfaces  
Hi Tim,

I do have  a problem using UDP multicast on en1 (one special interface of 2).
I bound to a specific ip (en1) I can send multicast
messages on each nic but I'm not able to receive multicast
frames with this configuration anymore.

With wireshark I can see the IGMP multicast messages  (join and leave)
and I can see the udp multicast frames on the net which should be received
by en1 but the stack doesn't receive them.

I am going to post more details tomorrow including some sample
code but I am still not sure if this is a problem

1.in my code
2.within multicast managing of the nic driver
3.within the tcp/ip stack.

Could you please post  a sample code as well and describe
the ip configuration you are using and what happens on the net

post the output of ifconfig and netstat -nr as well. I am going to 
debug deeper into my problem it might be I find something 
helpful for you as well.

regards,
  Clemens
Re: broadcast UDP with multiple interfaces  
> Hi Tim,
> 
> I do have  a problem using UDP multicast on en1 (one special interface of 2).
> I bound to a specific ip (en1) I can send multicast
> messages on each nic but I'm not able to receive multicast
> frames with this configuration anymore.
> 
> With wireshark I can see the IGMP multicast messages  (join and leave)
> and I can see the udp multicast frames on the net which should be received
> by en1 but the stack doesn't receive them.
> 
> I am going to post more details tomorrow including some sample
> code but I am still not sure if this is a problem
> 
> 1.in my code
> 2.within multicast managing of the nic driver
> 3.within the tcp/ip stack.
> 
> Could you please post  a sample code as well and describe
> the ip configuration you are using and what happens on the net
> 
> post the output of ifconfig and netstat -nr as well. I am going to 
> debug deeper into my problem it might be I find something 
> helpful for you as well.
> 
> regards,
>   Clemens

Hi all,
now I have written a simple test program which can be 
used to test receive and send of multicast packets

I am able to send in all 3 possible modes
(socket bound to ip of en0,en1 or to INADDR_ANY).

Tim, if I bind to different sockets the src address changes 
as well.

I am just able to receive multicast messages with 
INADDR_ANY configuration.

Looking into the documentation tells me it is not forbidden
and I should be able to do that but it doesn't work.

see the attached sample file

Does somebody have an idea, did I something wrong within
my code ?

regards,
   Clemens


Attachment: Text udpMulticastMultiInterface.c 8.18 KB
Re: broadcast UDP with multiple interfaces  
Here is how I create the socket

	m_fdDMCPSock = ::socket(PF_INET, SOCK_DGRAM, 0);
	if ( m_fdDMCPSock == -1 )
	{
		// <<ERROR>>
		LOG_STD_ERROR("Failed to create a socket for DMCP processing.");
	}
	
	if ( m_fdDMCPSock != -1 )
	{
		int nRet = -1;

		int nOptVal = 1;
		nRet = ::setsockopt(m_fdDMCPSock,SOL_SOCKET,SO_BROADCAST,&nOptVal,sizeof(nOptVal));
		if ( nRet < 0 )
		{
			// <<ERROR>>
			LOG_STD_ERROR("Failed to set options for DMCP processing socket.");
			bRet = false; 
		}

		struct ifreq ifr;
		::strcpy(ifr.ifr_name,"en1");
		::ioctl(m_fdDMCPSock,SIOCGIFADDR,reinterpret_cast<int>(&ifr));
	
		nRet = ::setsockopt(m_fdDMCPSock,SOL_SOCKET,SO_BINDTODEVICE,&ifr,sizeof(ifr));
		if ( nRet < 0 )
		{
			// <<ERROR>>
			LOG_STD_ERROR("Failed to set options for DMCP processing socket.");
			bRet = false; 
		}		
	}


From that point on - I operate pretty normally.  Things are working fine for me now.

Tim
Re: broadcast UDP with multiple interfaces  
Hi Tim,

I tried the SO_BINDTODEVICE option already (combined to the INADDR_ANY)
but it does not solve my problem.

Attached the updated test program.

What I found is that if I am doing SO_BINDTODEVICE and try to send
with the same socket over en0 is fine but doing SO_BINDTODEVICE
with en1 the system tells me No route to host if the default gateway
is set  to the IP of en0.
If I change the default gateway to the ip of en1 it works for en1,
but then it does not work for en0 any more.

Does someone have an idea how it is possible to receive
and send multicast messages for each interface ?


regards,
  Clemens

Attachment: Text udpMulticastMultiInterface.c 12.61 KB
RE: broadcast UDP with multiple interfaces  
Try using either the socket option SO_DONTROUTE, or even better as you
have more control is to use sendmsg() with the flag MSG_DONTROUTE when
sending your broadcast packets. In this case you can use the same socket
for your broadcast packets and your routed unicast packets. DONTROUTE is
bypassing the routing table. 

Dave

> -----Original Message-----
> From: clemens gmeiner [mailto:community-noreply@qnx.com]
> Sent: Wednesday, November 05, 2008 9:37 AM
> To: general-networking
> Subject: Re: broadcast UDP with multiple interfaces
> 
> Hi Tim,
> 
> I tried the SO_BINDTODEVICE option already (combined to the
INADDR_ANY)
> but it does not solve my problem.
> 
> Attached the updated test program.
> 
> What I found is that if I am doing SO_BINDTODEVICE and try to send
> with the same socket over en0 is fine but doing SO_BINDTODEVICE
> with en1 the system tells me No route to host if the default gateway
> is set  to the IP of en0.
> If I change the default gateway to the ip of en1 it works for en1,
> but then it does not work for en0 any more.
> 
> Does someone have an idea how it is possible to receive
> and send multicast messages for each interface ?
> 
> 
> regards,
>   Clemens
> 
> 
> 
> _______________________________________________
> General
> http://community.qnx.com/sf/go/post15976
Re: RE: broadcast UDP with multiple interfaces  
Hi Dave,
I might be wrong within that topic but I try to multicast.
I tried the MSG_DONTROUTE option within the sendto then it tells me
Network is unreachable.
I also tried SO_DONTROUTE same result Network is unreachable.

regards,
  Clemens

> Try using either the socket option SO_DONTROUTE, or even better as you
> have more control is to use sendmsg() with the flag MSG_DONTROUTE when
> sending your broadcast packets. In this case you can use the same socket
> for your broadcast packets and your routed unicast packets. DONTROUTE is
> bypassing the routing table. 
> 
> Dave
> 
> > -----Original Message-----
> > From: clemens gmeiner [mailto:community-noreply@qnx.com]
> > Sent: Wednesday, November 05, 2008 9:37 AM
> > To: general-networking
> > Subject: Re: broadcast UDP with multiple interfaces
> > 
> > Hi Tim,
> > 
> > I tried the SO_BINDTODEVICE option already (combined to the
> INADDR_ANY)
> > but it does not solve my problem.
> > 
> > Attached the updated test program.
> > 
> > What I found is that if I am doing SO_BINDTODEVICE and try to send
> > with the same socket over en0 is fine but doing SO_BINDTODEVICE
> > with en1 the system tells me No route to host if the default gateway
> > is set  to the IP of en0.
> > If I change the default gateway to the ip of en1 it works for en1,
> > but then it does not work for en0 any more.
> > 
> > Does someone have an idea how it is possible to receive
> > and send multicast messages for each interface ?
> > 
> > 
> > regards,
> >   Clemens
> > 
> > 
> > 
> > _______________________________________________
> > General
> > http://community.qnx.com/sf/go/post15976