如何从以太网数据包中提取有效载荷(向量 )?

问题描述 投票:0回答:1

我的任务是给我做路由器的任务。以太网数据包的数据类型为vector <unsigned char>,并且以太网标头如下:enter image description here

现在我对矢量没有太多的经验,但是我的问题是如何提取有效载荷部分?例如,如果以太网数据包是ARP数据包,我希望能够使用以下结构来检查操作码,并且本质上能够执行类似于

的操作
arp_hdr *arphdr = (arp_hdr *) packet_arp;
struct arp_hdr
{
  unsigned short  arp_hrd;                 /* format of hardware address   */
  unsigned short  arp_pro;                 /* format of protocol address   */
  unsigned char   arp_hln;                 /* length of hardware address   */
  unsigned char   arp_pln;                 /* length of protocol address   */
  unsigned short  arp_op;                  /* ARP opcode (command)         */
  unsigned char   arp_sha[ETHER_ADDR_LEN]; /* sender hardware address      */
  uint32_t        arp_sip;                 /* sender IP address            */
  unsigned char   arp_tha[ETHER_ADDR_LEN]; /* target hardware address      */
  uint32_t        arp_tip;                 /* target IP address            */
} __attribute__ ((packed)) ;

并且还会提供以太网头:

struct ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
  uint8_t  ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
  uint8_t  ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
  uint16_t ether_type;                  /* packet type ID */
} __attribute__ ((packed)) ;
c++ networking network-programming ethernet arp
1个回答
1
投票

要将一个矢量复制到另一个矢量中,您可以在此处std::vector使用第五个https://en.cppreference.com/w/cpp/container/vector/vector构造函数>

template< class InputIt > vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() );
© www.soinside.com 2019 - 2024. All rights reserved.