Skip to content

How to reconstruct an ImmediateBuffer from an IP packet? #31

Answered by wiresock
cavivie asked this question in Q&A
Discussion options

You must be logged in to vote

Assuming that packet is a pointer to a raw IPv4 packet and length represents its size, the following code snippet accomplishes the task:

memset(buffer.get(), 0, offsetof(INTERMEDIATE_BUFFER, m_IBuffer));  
memmove(&buffer->m_IBuffer[ETHER_HEADER_LENGTH], packet, length);  
auto* const eth_header = reinterpret_cast<ether_header_ptr>(buffer->m_IBuffer);  
memmove(eth_header->h_source, source_mac, ETH_ALEN);  
memmove(eth_header->h_dest, dest_mac, ETH_ALEN);  
eth_header->h_proto = ETH_P_IP_NET;

buffer->m_Length = gsl::narrow_cast<uint32_t>(length) + ETHER_HEADER_LENGTH;

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@cavivie
Comment options

Answer selected by cavivie
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #30 on August 15, 2024 14:44.