0

I need clarification on how to receive a multicast packet on a particular virtual (VLAN) interface. I have send multicast data from one VLAN interface (eth0.10), I need to receive it on other machine which has same interface(eth0.10) , and not in any other virtual interfaces.

Thank you.

here is my receiver socket settings.

const char *interface = NULL;
interface = "eth0.10";
unsigned char multicast_mac[6] = { 0x01, 0x00, 0x5E, 0x40, 0x10, 0x05 }; 

/*open socket*/
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s == -1) {
    perror("socket():");
        exit(1);
}    

memset(&ifr,0, sizeof(struct  ifreq));

strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));

if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
    perror("SIOCGIFINDEX");
    exit(1);
}
ifindex = ifr.ifr_ifindex;

/*retrieve corresponding MAC*/
if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
    perror("SIOCGIFHWADDR");
    exit(1);
}
/*prepare sockaddr_ll*/
socket_address.sll_family   = AF_PACKET;
socket_address.sll_protocol = htons(/*ETH_P_IP*/ ETH_P_8021Q);
socket_address.sll_ifindex  = ifindex;


/*BIND eth0.10 interface to multicast address */
res = bind (s,(struct sockaddr *)&socket_address, sizeof(socket_address));
if (res != 0)
    printf ("BIND >> error \n");

/*SETSOCKOPT for "eth0.10" interface */

if (setsockopt (s, SOL_SOCKET,SO_BINDTODEVICE, interface, 7 < 0 ){
    g_print("SETSOCKOPT FAILED:Client \n");
    return 0;
    close (s);

}

/*FILL packet_mreq structure - to use in setsockopt() */

    pmreq.mr_ifindex                = ifindex;
    pmreq.mr_type                   = PACKET_MR_MULTICAST;
    pmreq.mr_alen                   = ETH_ALEN;
    pmreq.mr_address [0]            = multicast_mac[0];
    pmreq.mr_address [1]            = multicast_mac[1];
    pmreq.mr_address [2]            = multicast_mac[2];
    pmreq.mr_address [3]            = multicast_mac[3];
    pmreq.mr_address [4]            = multicast_mac[4];
    pmreq.mr_address [5]            = multicast_mac[5];

/*JOIN TO MULTICAST GROUP */

    if (setsockopt (s, SOL_PACKET,PACKET_ADD_MEMBERSHIP, &pmreq, sizeof (pmreq)) < 0 ){
        g_print("SETSOCKOPT FAILED:Client \n");
        return 0;
    }
arr
  • 101
  • 2
  • 10

2 Answers2

0

Multicast generally uses IGMP packets to determine who on the network segment should receive the packet: I'm assuming if you bind your multicast socket on the receiving machine to the correct interface, it should be the only one to receive the packet. How are you setting up the multicast receiver socket?

Femi
  • 64,273
  • 8
  • 118
  • 148
  • thank you. I have edited my query with receiver socket settings. Pls tell me about usage of IGMP here. – arr May 23 '13 at 06:17
  • The `PACKET_ADD_MEMBERSHIP` handles sending the IGMP join message. If you're binding to the correct ethernet IP you should be fine (and it looks like you are) plus the `SO_BINDTODEVICE` should help (but I believe that only affects outbound packets): are you receiving the packets on a different interface? – Femi May 23 '13 at 06:21
  • I send thru eth0.10 and try to receive in eth0.10 only, as I have two more virtual interfaces on receiver (eth0.2,eth0.3), data is received in other interfaces also. – arr May 23 '13 at 06:31
  • Are you sending/receiving on the same machine? If so you should try the IP_PROTO level option (`setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))` and use an `ip_mreq` structure instead. – Femi May 23 '13 at 07:14
  • No my sender and receiver are two different machines.Also I use MAC address for multicast instead of IP. – arr May 23 '13 at 08:07
  • Try using the IP_PROTO option, instead of the SOL_PACKET option? – Femi May 23 '13 at 08:25
  • Hi I am using MAC address for multicast. can I use IP_PROTO? or is there any other alternative ? – arr May 24 '13 at 09:56
0

You need to enumerate all your network interfaces, and join the group via each of them in turn. Otherwise the join only takes effect over the single unicast route to the multicast group.

user207421
  • 305,947
  • 44
  • 307
  • 483