我需要澄清如何在特定虚拟 (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;
}