udp broadcast on more than one nic's

udp broadcast on more than one nic's

Post by Lars Guesma » Sat, 24 Nov 2001 17:13:01



Hello all,

I would like to doing a UDP broadcast via port 3000. For linux boxes with one
network
interface card everything works as expected. But if the box has two or more
cards, these are
ignored and the broadcast is further send only via the first interface. My code
is attached at the end.

Do I need some special to go over all interfaces available on a linux box ?

--
------------------------
Thanks in advance
Lars Gsmar

here is the code:

int    i = 1;
static DWORD   dwLocalAddr = 0;
static u_short PortStatus = 3000;
static int     UpSocket;
char   szBuffer[1024],
struct sockaddr_in addr;

#define StrUp "This is the broadcast message"

 addr.sin_port        = htons(PortStatus);
 addr.sin_family      = AF_INET;
 addr.sin_addr.s_addr = (DWORD) -1;

UpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
setsockopt(UpSocket, SOL_SOCKET, SO_BROADCAST, (LPSTR)&i,sizeof(i));
bind(UpSocket, &addr, sizeof(addr));

if (sendto(UpSocket, StrUp, sizeof(StrUp) + 1, 0, (LPSOCKADDR)&addr,
sizeof(addr)) == -1)
    {
      Message("Broadcast failed!\n");
    }

 
 
 

udp broadcast on more than one nic's

Post by Eugene Kuznetso » Sat, 24 Nov 2001 20:41:19



> Hello all,

> I would like to doing a UDP broadcast via port 3000. For linux boxes with
> one network
> interface card everything works as expected. But if the box has two or
> more cards, these are
> ignored and the broadcast is further send only via the first interface. My
> code is attached at the end.

> Do I need some special to go over all interfaces available on a linux box
> ?

> --
> ------------------------
> Thanks in advance
> Lars Gsmar

> here is the code:

> int    i = 1;
> static DWORD   dwLocalAddr = 0;
> static u_short PortStatus = 3000;
> static int     UpSocket;
> char   szBuffer[1024],
> struct sockaddr_in addr;

> #define StrUp "This is the broadcast message"

>  addr.sin_port        = htons(PortStatus);
>  addr.sin_family      = AF_INET;
>  addr.sin_addr.s_addr = (DWORD) -1;

> UpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
> setsockopt(UpSocket, SOL_SOCKET, SO_BROADCAST, (LPSTR)&i,sizeof(i));
> bind(UpSocket, &addr, sizeof(addr));

> if (sendto(UpSocket, StrUp, sizeof(StrUp) + 1, 0, (LPSOCKADDR)&addr,
> sizeof(addr)) == -1)
>     {
>       Message("Broadcast failed!\n");
>     }

I believe that you must create one socket for each interface and bind it
explicitly to different interfaces. You can either discover addresses of
your interfaces and put them in addr.sin_addr.s_addr or use SO_BINDTODEVICE
option of setsockopt.
By the way, using so Windows'ish syntax in Linux program is not the right
thing.

 
 
 

udp broadcast on more than one nic's

Post by Lars Guesma » Sat, 24 Nov 2001 21:21:15


Thanks to you,  Eugene.

that means there is no generic way for me to say "bind to all_interfaces"
without having to detect all available interfaces? hmm, not very comfortable,
but I guess I have to go the hard way. How do figure out how many nic's are
installed?

Thanks in advance
Lars

note: My programm is a port from windows and at the moment it takes to much work
to remove the "Windows'ish syntax" ;-)

Quote:> I believe that you must create one socket for each interface and bind it
> explicitly to different interfaces. You can either discover addresses of
> your interfaces and put them in addr.sin_addr.s_addr or use SO_BINDTODEVICE
> option of setsockopt.
> By the way, using so Windows'ish syntax in Linux program is not the right
> thing.

 
 
 

udp broadcast on more than one nic's

Post by Eugene Kuznetso » Sat, 24 Nov 2001 22:04:24



> Thanks to you,  Eugene.

> that means there is no generic way for me to say "bind to all_interfaces"
> without having to detect all available interfaces?

I am not aware of one.

Quote:> hmm, not very
> comfortable, but I guess I have to go the hard way. How do figure out how
> many nic's are installed?

You can use function if_nameindex() ( declared in net/if.h,
gives you names of all present network interfaces ).
 
 
 

1. Q: UDP Broadcast, Sockets, Dual NICs, Single Cable

HI, I have a system with dual NICs - eth0 and eth1. eth0 is live (has
an IP address and a cable from it's RJ45 jack into the wall). eth0 is
not 'live' (no IP address, and no cable).

So I have an application that opens 2 UDP sockets, one on eth0 and the
other on eth1. It then sits awaiting for broadcast messages from
clients. When it gets one, we process it. Here's what's strange: When
a broadcast messages comes in, we also get the message on the socket
associated with eth1 (the NIC with no IP address and no cable).

Are UDP broadcast message known to behave in this way? Could this be a
bug in the Linux IP stack?  A but on our application?

Any feedback appreciated.

Thanks.

2. signals to kill all children without zombie?

3. UDP Broadcasts Doesn't ?????

4. PCMCIA setup on Toshiba 2535CDS RH6.0

5. Can't telnet to ONE of TWO NICs on Dual NIC System.

6. Help! Newbie Question about TAR

7. FW & Gw woes with 2 NIC's, only 1 NIC works at one time

8. 2 further serial ports ???

9. 2 NIC'S, one vendor, one tulip driver

10. How to receive UDP and ICMP packet using one UDP socket, (Path MTUD)

11. is it expected behaviour to receive one's own broadcast messages?

12. Getting 'named' to listen to one NIC

13. two IP's on one machine using two differnet NIC's