gpt4 book ai didi

linux - Linux下读取带有特定VLAN标签的802.1Q帧

转载 作者:太空狗 更新时间:2023-10-29 12:16:49 25 4
gpt4 key购买 nike

有什么方法可以接收特定 VLAN 标记帧的数据吗?目前,我通过将 htons(0x22f0) 作为 socket() 函数的输入来接收所有 VLAN 标记的帧,例如:

m_iSocketDesc = socket(AF_PACKET, SOCK_RAW, htons(0x22f0));
numBytes = recvfrom(m_iSocketDesc, message_data, 1522, 0, NULL, 0)

最佳答案

我也有类似的需求。为了接收具有特定 VLAN 的数据包,我创建了一个 VLAN 接口(interface)并将我的原始套接字绑定(bind)到它。现在我可以发送 802.3 帧,内核插入/提取 VLAN 标记。

  1. 创建VLAN接口(interface),需要root权限

    ip link add link <phyInterface> name <phyInterface.VLANID> type vlan id VLANID
    Eg: ip link add link eth0 name eth0.100 type vlan 100
  2. 在您的应用程序中使用正常绑定(bind)功能绑定(bind)到 VLAN 接口(interface)。在上面的例子中eth0.100。

您可以引用以下两个链接获取示例代码(我不是代码的所有者)

发送:https://gist.github.com/austinmarton/1922600

接收:https://gist.github.com/austinmarton/2862515#file-recvraweth-c

唯一需要改变的是绑定(bind)到虚拟 VLAN 接口(interface),而不是绑定(bind)到物理接口(interface)。请注意,接收到的以太网帧是未标记的帧。在此接口(interface)上发送的任何帧都会自动标记 VLANID。

关于linux - Linux下读取带有特定VLAN标签的802.1Q帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21825058/

25 4 0