原始套接字多播

Raw socket multicasting

我有一个绑定到 eth2 的原始套接字。

#define DEVICE_NAME "eth2"

// open a socket
int Socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

if (Socket < 0)
{
    perror("socket() error");
    return -1;
}

// create a interface request structure
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));     

// set the interface name
strncpy(ifr.ifr_name, DEVICE_NAME, IFNAMSIZ); 

// get interface index
ioctl(Socket, SIOCGIFINDEX, &ifr);
int Socket_Index = ifr.ifr_ifindex;

// bind the socket to the interface
struct sockaddr_ll Socket_Addr;
Socket_Addr.sll_family = AF_PACKET;
Socket_Addr.sll_protocol = htons(ETH_P_ALL);
Socket_Addr.sll_ifindex = Socket_Index;
bind(Socket, (struct sockaddr *)&Socket_Addr, sizeof(Socket_Addr)); 

// add multicast addresses to the socket, based on Unit Number

struct packet_mreq mreq;
mreq.mr_ifindex = Socket_Index;
mreq.mr_type = PACKET_MR_MULTICAST;
mreq.mr_alen = ETH_ALEN; 

memcpy(mreq.mr_address, Addresses[UNITS_1_2], ETH_ALEN);
setsockopt(Socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

memcpy(mreq.mr_address, Addresses[UNIT_3], ETH_ALEN);
setsockopt(Socket, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

地址[UNITS_1_2]解析为03:00:00:01:04:00,地址[UNIT_3]解析为02:00:00:01:04:01。

套接字只接收多播数据包,不接收单播数据包。在调试时,我启动了 tcpdump,低调地进入混杂模式就成功了。

我的问题是:在没有混杂模式的情况下,我可以在同一个原始套接字上同时接收多播和单播数据包吗?我尝试使用 maddr 添加 02:00:00:01:04:01 到 eth0s mac 地址,但没有成功。

从 gabhijit 偷偷摸摸:尝试添加

Socket_Addr.sll_pkttype = PACKET_HOST | PACKET_MULTICAST;