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:
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_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
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());