gpt4 book ai didi

c - 发送原始数据包时,sendto 函数不使用 struct sockaddr_ll 中提供的 MAC 地址

转载 作者:IT王子 更新时间:2023-10-29 00:56:42 27 4
gpt4 key购买 nike

我正在尝试使用原始套接字发送 OAM 以太网帧。我这样做很成功。

我写的发送函数是:

    int send_frame(sock_info *info,char *buf,int length)
{
struct sockaddr_ll dest_addr;
memset(&dest_addr,0,sizeof(struct sockaddr_ll));
dest_addr.sll_family = PF_PACKET;
dest_addr.sll_protocol = htons(8902);
dest_addr.sll_ifindex = info->if_index;
dest_addr.sll_halen = ETH_MAC_ADDR_LEN;
dest_addr.sll_pkttype = PACKET_OTHERHOST;
dest_addr.sll_hatype = ARPHRD_ETHER;
memset(dest_addr.sll_addr,0,8);

dest_addr.sll_addr[0] = 0x00;
dest_addr.sll_addr[1] = 0xE0;
dest_addr.sll_addr[2] = 0x0C;
dest_addr.sll_addr[3] = 0x00;
dest_addr.sll_addr[4] = 0x95;
dest_addr.sll_addr[5] = 0x02;

return sendto(info->sock_fd, buf, length, 0, (struct sockaddr*) &dest_addr, sizeof(struct sockaddr_ll));
}

我无法使用 wireshark 捕获数据包。在尝试了太多东西之后,我发现用于发送的缓冲区应该包含所有以太网帧字段(从目标地址开始)。当我将目标地址和源地址以及其他以太网字段添加到缓冲区中时,我能够使用 wireshark 捕获数据包。所以发送函数不使用存储在 dest_addr.sll_addr 中的 MAC 地址。

我的问题是,那么struct sockaddr_ll 中的sll_addr 字段有什么用呢?手册上说是目标MAC地址。

最佳答案

对我来说,这听起来就像手册页描述的那样有效(man 7 packet):

SOCK_RAW packets are passed to and from the device driver without any changes in the packet data. When receiving a packet, the address is still parsed and passed in a standard sockaddr_ll address structure. When transmitting a packet, the user supplied buffer should contain the physical layer header. That packet is then queued unmodified to the network driver of the interface defined by the destination address. Some device drivers always add other headers. SOCK_RAW is similar to but not compatible with the obsolete PF_INET/SOCK_PACKET of Linux 2.0.

这里的缓冲区指的是sendto()的第二个参数。所以,stuct sockaddr_ll 只用于返回数据给调用者,而不是格式化RAW数据包。也许您想改用 SOCK_DGRAMlibpcap

关于c - 发送原始数据包时,sendto 函数不使用 struct sockaddr_ll 中提供的 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16235692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com