如何使我的组播数据包到达特定的虚拟接口?

时间:2013-05-23 04:07:35

标签: sockets interface multicast

我需要澄清如何在特定虚拟(VLAN)接口上接收多播数据包。 我从一个VLAN接口(eth0.10)发送组播数据,我需要在其他具有相同接口(eth0.10)的机器上接收它,而不是任何其他虚拟接口。

谢谢。

这是我的接收器套接字设置。

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

2 个答案:

答案 0 :(得分:0)

组播通常使用IGMP数据包来确定网段上的哪些人应该接收数据包:我假设您将接收计算机上的组播套接字绑定到正确的接口,它应该是唯一接收数据包的接口。你是如何设置多播接收器套接字的?

答案 1 :(得分:0)

您需要枚举所有网络接口,并依次通过每个接口加入该组。否则,加入仅对通向组播组的单个单播路由生效。