我有一个我已经绑定到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地址,但没有运气。
答案 0 :(得分:1)
从gabhijit偷偷摸摸:尝试添加
Socket_Addr.sll_pkttype = PACKET_HOST | PACKET_MULTICAST;