简体   繁体   English

如何从以太网数据包中提取有效载荷(矢量<unsigned char> )?

[英]How to Extract the Payload from Ethernet Packet (vector<unsigned char>)?

For my assignment I have been given the task of making a router.对于我的任务,我被赋予了制作路由器的任务。 The ethernet packet datatype is vector <unsigned char> and the ethernet header is as follows:以太网数据包数据类型为vector <unsigned char> ,以太网标头如下: 在此处输入图片说明

Now I do not have much experience working with vectors, but my question is how can I extract the payload part?现在我没有太多使用向量的经验,但我的问题是如何提取有效载荷部分? For example if the ethernet packet is an ARP packet I would like to be able to use the following structure to check the opcode and essentially be able to do something like例如,如果以太网数据包是一个 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)) ;

And the ethernet header is also given:并且还给出了以太网标头:

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)) ;

To copy a vector into another vector, you can use the 5th std::vector constructor here https://en.cppreference.com/w/cpp/container/vector/vector要将向量复制到另一个向量中,您可以在此处使用第 5 个std::vector构造函数https://en.cppreference.com/w/cpp/container/vector/vector

template< class InputIt > vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() );

So to extract the payload (assuming the payload is everything in the dataframe that is not in the header) you'd do:因此,要提取有效载荷(假设有效载荷是数据帧中不在标头中的所有内容),您需要执行以下操作:

...assuming the existence of a std::vector<unsigned char> dataframe;
std::vector<unsigned char> payload(dataframe.begin() + sizeof(ethernet_hdr), dataframe.end());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM