【问题标题】:Read data-link (MAC) layer packets in Linux在 Linux 中读取数据链路 (MAC) 层数据包
【发布时间】:2014-04-05 18:58:13
【问题描述】:

在 Linux 上从数据链路 (MAC) 层读取数据包的最简单/最短/最简单的方法是什么?

谁能给我们一个代码 sn-p 来告诉我们如何做到这一点?

我们为什么需要它? 我们正在开发一种网络摄像机,其中千兆芯片仅实现数据链路层。由于我们没有资源来实现 IP 栈,我们只需要使用 MAC 地址来交换数据包。

【问题讨论】:

    标签: linux networking packet-sniffers data-link-layer


    【解决方案1】:

    这是我要找的代码 sn-p:

    #include <stdio.h>
    #include <stdlib.h>
    
    #include <sys/socket.h>
    #include <linux/if_packet.h>
    #include <linux/if_ether.h>
    #include <linux/if_arp.h>
    
    
    int main()
    {
            int s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
            if (s == -1)
            {
                printf("Error while creating socket. Aborting...\n");
                return 1;
            }
    
            void* buffer = (void*)malloc(ETH_FRAME_LEN);
            while(1)
            {
                int receivedBytes = recvfrom(s, buffer, ETH_FRAME_LEN, 0, NULL, NULL);
                printf("%d bytes received\n", receivedBytes);
                int i;
                for (i = 0; i < receivedBytes; i++)
                {
                   printf("%X ", ((unsigned char*)buffer)[i]);
                }
                printf("\n");
            }
            return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2016-06-09
      • 2020-08-15
      相关资源
      最近更新 更多