无法接收自定义以太网帧

时间:2018-09-01 04:33:51

标签: c linux sockets

我创建了一个socket_raw,自定义协议类型为0x2328,然后在另一台计算机上发送了0x2328类型的数据。捕获数据包可以被捕获,但是recvfrom无法接收它。我该怎么办?这是收到的代码。

struct sockaddr_ll sll;
struct ifreq ifr;

if ((sd = socket (PF_PACKET, SOCK_RAW, htons (0x2328))) < 0) {
    perror ("socket() failed to get socket descriptor for using ioctl() ");
    exit (EXIT_FAILURE);
}

// Use ioctl() to look up interface name and get its MAC address.
memset (&ifr, 0, sizeof (ifr));
sprintf (ifr.ifr_name, "eth1");

if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
    perror ("ioctl() failed");
    return (EXIT_FAILURE);
}

memset (&sll, 0, sizeof (sll));
int sll_len;
sll.sll_family  = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll_len         = sizeof(sll);

char buffer[1024];
recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sll, &sll_len);

0 个答案:

没有答案