我必须发送和接收具有自定义ethertype的原始以太网帧。没有使用IP协议。
发送和接收单播数据包有效,但是多播数据包存在问题。没有收到组播数据包。我试图在我的多播MAC中使用PACKET_ADD_MEMBERSHIP
套接字选项来接收那些数据包,但没有成功。
我不想使用混杂模式,因为我只需要单播和多播消息。
这是我的初始化代码:
static uint8_t macAddress[] = { 0x00, 0x00, 0xC4, 0x00, 0x00, 0x01 };
static uint8_t mcastAddress[] = { 0x00, 0x00, 0xC4, 0x00, 0xFF, 0xFF };
int sockID;
struct ifreq ifr;
struct sockaddr_ll sll;
struct packet_mreq mreq;
int ethDevNr;
// Create socket
sockID = socket(AF_PACKET, SOCK_RAW, htons(0xAABB));
if (sockID < 0)
syslog(LOG_ERR, "Socket creation failed!\n");
// Change MAC address
memset(&ifr, 0, sizeof(ifr));
memcpy(ifr.ifr_hwaddr.sa_data, macAddress, 6);
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
if (ioctl(sockID, SIOCSIFHWADDR, &ifr) < 0)
syslog(LOG_ERR, "Setting MAC failed!\n");
// Get device index
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
if (ioctl(sockID, SIOCGIFINDEX, &ifr) < 0)
syslog(LOG_ERR, "SIOCGIFINDEX failed!\n");
ethDevNr = ifr.ifr_ifindex;
// Add multicast mac
memset(&mreq, 0, sizeof(mreq));
mreq.mr_ifindex = ethDevNr;
mreq.mr_type = PACKET_MR_MULTICAST;
mreq.mr_alen = ETH_ALEN;
memcpy(mreq.mr_address, mcastAddress, ETH_ALEN);
if (setsockopt(sockID, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
syslog(LOG_ERR, "Adding MAC failed!\n");
// Open socket
sll.sll_family = PF_PACKET;
sll.sll_ifindex = ethDevNr;
sll.sll_protocol = htons(0xAABB);
sll.sll_pkttype = PACKET_HOST | PACKET_MULTICAST | PACKET_BROADCAST;
bind(sockID, (struct sockaddr*)&sll, sizeof(sll));
// Wait for packets
while (1) {
static uint8_t buf[1000];
int recvBytes;
recvBytes = recvfrom(sockID, buf, 1000, 0, NULL, NULL);
syslog(LOG_NOTICE, "Received %4d bytes\n", recvBytes);
}
如何不使用接收所有数据包的混杂模式来仅接收两个MAC地址(我的自定义单播和我的自定义多播)的数据包?