UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2010-05-18 01:36 PM

medized
Member
Registered: 2010-05-18
Posts: 4

Re: Multicast socket with linux bridge

Hello all,

I am a beginner with the multicast socket, I would really appreciate a little help with the following question :

I have to network interfaces, eth0 and eth1.
I assigned no IP addresses to both of them, by using :

ifconfig eth0 0.0.0.0 up
ifconfig eth0 0.0.0.0 up

I added a bridge to forward the trafic from an interface to an other:

brctl addbr br0
brctl addif bro eth0
brctl addif bro eth1

I also created several IP aliases, to use them with the two interfaces:

ifconfig eth0:0 192.168.0.1 netmask 255.255.255.0 up
ifconfig eth1:0 192.168.1.1 netmask 255.255.255.0 up

I would like to run a sample code for udp multicasting:

Here's the sender code:

int main(int argc, char *argv[]) {

  int sock;                   /* socket descriptor */
  char send_str[MAX_LEN];     /* string to send */
  struct sockaddr_in mc_addr; /* socket address structure */
  unsigned int send_len;      /* length of string to send */
  char* mc_addr_str;          /* multicast IP address */
  unsigned short mc_port;     /* multicast port */
  unsigned char mc_ttl=1;     /* time to live (hop count) */

 
  mc_addr_str = "224.0.0.10";       /* arg 1: multicast IP address */
  mc_port     = 6666; /* arg 2: multicast port number */

 
  /* create a socket for sending to the multicast address */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
  }
  
  /* set the TTL (time to live/hop count) for the send */
  if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, 
       (void*) &mc_ttl, sizeof(mc_ttl))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  } 
 
  
  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
  mc_addr.sin_port        = htons(mc_port);

  printf("Begin typing (return to send, ctrl-C to quit):\n");

  /* clear send buffer */
  memset(send_str, 0, sizeof(send_str));

  while (fgets(send_str, MAX_LEN, stdin)) {
    send_len = strlen(send_str);

    /* send string to multicast address */
    if ((sendto(sock, send_str, send_len, 0, 
         (struct sockaddr *) &mc_addr, 
         sizeof(mc_addr))) != send_len) {
      perror("sendto() sent incorrect number of bytes");
      exit(1);
    }

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));
  }

  close(sock);  

  exit(0);
}

Here's the receiver code:

int main(int argc, char *argv[]) {

  int sock;                     /* socket descriptor */
  int flag_on = 1;              /* socket option flag */
  struct sockaddr_in mc_addr;   /* socket address structure */
  char recv_str[MAX_LEN+1];     /* buffer to receive string */
  int recv_len;                 /* length of string received */
  struct ip_mreq mc_req;        /* multicast request structure */
  char* mc_addr_str;            /* multicast IP address */
  unsigned short mc_port;       /* multicast port */
  struct sockaddr_in from_addr; /* packet source */
  unsigned int from_len;        /* source addr length */

 

  mc_addr_str = "224.0.0.10";      /* arg 1: multicast ip address */
  mc_port = 6666;    /* arg 2: multicast port number */

  
  /* create socket to join multicast group on */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
  }
  
  /* set reuse port to on to allow multiple binds per host */
  if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
       sizeof(flag_on))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  }

  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  mc_addr.sin_port        = htons(mc_port);

  /* bind to multicast address to socket */
  if ((bind(sock, (struct sockaddr *) &mc_addr, 
       sizeof(mc_addr))) < 0) {
    perror("bind() failed");
    exit(1);
  }

  /* construct an IGMP join request structure */
  mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
  mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

  /* send an ADD MEMBERSHIP message via setsockopt */
  if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
       (void*) &mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  }

  for (;;) {          /* loop forever */

    /* clear the receive buffers & structs */
    memset(recv_str, 0, sizeof(recv_str));
    from_len = sizeof(from_addr);
    memset(&from_addr, 0, from_len);

    /* block waiting to receive a packet */
    if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0, 
         (struct sockaddr*)&from_addr, &from_len)) < 0) {
      perror("recvfrom() failed");
      exit(1);
    }

    /* output received string */
    printf("Received %d bytes from %s: ", recv_len, 
           inet_ntoa(from_addr.sin_addr));
    printf("%s", recv_str);
  }

  /* send a DROP MEMBERSHIP message via setsockopt */
  if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
       (void*) &mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  }

  close(sock);  
}

So when I run the receiver program, here's the error that occurs:

setsockopt() failed: No such device

I didn't have any problems before switching to the bridge configuration... :(

Thanks a lot for your help guys !

--
Zed

Offline

#2 2010-05-18 02:18 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Multicast socket with linux bridge

Perhaps you forgot to add/update the multicast routes?

I've no experience with ethernet bridging and multicast, so not much of use.

Offline

#3 2010-05-18 02:24 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Multicast socket with linux bridge

...or it's the typo:

brctl addif bro eth0

should be:

brctl addif br0 eth0

Offline

#4 2010-05-18 02:30 PM

medized
Member
Registered: 2010-05-18
Posts: 4

Re: Multicast socket with linux bridge

Yeep that was the problem !! I didn't specify on which device the multicast packets should go !! I added the following command:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth2

Thanks a lot !

Cheers

--
Zed

Offline

#5 2010-05-18 02:32 PM

medized
Member
Registered: 2010-05-18
Posts: 4

Re: Multicast socket with linux bridge

Sorry the bro / br0 error was just in the message, not on my actual command!

Thx ! :)

Offline

#6 2010-05-18 02:35 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Multicast socket with linux bridge

That's great!

It also seems you can do just one

ifconfig br0 192.168.0.1 netmask 255.255.255.0 up

instead of the aliassing stuff.

(Thought so, because of the other typos. :-)

Offline

#7 2010-05-18 04:59 PM

medized
Member
Registered: 2010-05-18
Posts: 4

Re: Multicast socket with linux bridge

Alright, I have other problems that appeared now...
I don't know how to configure the tables to let go from an interface to another.. Let me be clearer:

I have two Network Interfaces eth0 and eth1. For each one of them, I have two ip aliases:
eth0:0 192.168.0.1 & eth0:1 192.168.0.2

eth1:0 192.168.1.1 & eth1:0 192.168.1.2

Between the two interfaces, there's a bridge.

I built a program which sends packets from 192.168.0.1 to 192.168.1.1 and from 192.168.0.2 to 192.168.1.2, using raw sockets to be able to forge the ip source and destination fields.

The packets can not be delivered ... I guess I have to configure some special routes, but I don't which ones :s I'd like to be able to specify to the kernel to use the eth0 interface when the address is 192.168.0.* , and the eth1 interface for 192.168.1.* addresses, and let the bridge do the rest.

Thanks for your help, this question might sound stupid, but my networking skills are really, I mean really limited !

Thanks again!

Cheers,

--
Zed

Offline

#8 2010-05-18 07:38 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Multicast socket with linux bridge

I don't mess around with bridging too much, so I'm not entirely sure...  But, what does
your routing table look like right now?

Also, why forge packets with raw sockets?  Is it possible you maybe just screwed
something up in your code and crafted invalid packets which are getting dropped?
Why not just bind() to one of your alias IPs, and use normal TCP/UDP sockets to
do the sending?

Offline

#9 2010-05-18 10:24 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Multicast socket with linux bridge

Hardly anyone messes around with ethernet bridges and ip aliases and raw
sockets, so I think you're aquiring more network skills than most very quickly.
I also think you're making stuff much more complicated than necessary.

If I were you I'd just get rid of the aliases and only bring up br0, that makes
things much simpler and the whole point of bridging is to fuse networks
together. I'm not sure if it makes much sense with a bridge anyway. (assuming
you actually need an ethernet bridge.)

Then get rid of the raw sockets and do what Rob said: Just bind to the address
you want to use. If you really want multiple local addresses, then use aliases
on br0, or multiple local addresses (127.0.*).

Offline

#10 2010-09-03 11:20 AM

wmoors
Member
Registered: 2010-09-03
Posts: 13

Re: Multicast socket with linux bridge

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth2

Offline

#11 2010-09-03 01:05 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Multicast socket with linux bridge

Yeah, you probably didn't see a static route for multicast, but I bet it had a dynamic
one setup...  Try "ip mroute show" or "ip route show type multicast"...

Offline

#12 2010-09-03 04:27 PM

wmoors
Member
Registered: 2010-09-03
Posts: 13

Re: Multicast socket with linux bridge

both those commands show up empty... :confused:
I guess there's still something else going on...?

Offline

#13 2010-09-03 07:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Multicast socket with linux bridge

Did you check (soon) after successfully sending some multicast traffic?  Because,
it probably just adds dynamic entries to the cache on the fly as needed, and they age
out of existence after a while...

Offline

#14 2010-09-03 09:58 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Multicast socket with linux bridge

It might be also related to CONFIG_IP_MROUTE.

Offline

Board footer

Powered by FluxBB