Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Raw Socket Buffer

When we using a raw socket, we need to handle everything including handling which packet we need to process.

Suppose i receive 2 packet with 10 byte size.
The packet contains message helloworld and helooworld.

Suppose helloworld packet is the first packet I’m receive from the network, and we know that the helloworld packet will be placed into the buffer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But before I’m proccessing the helloworld packet, I’m receive helooworld packet.

Is there any overwritten data in the buffer?

Example code :

struct sockaddr _addr;
socklen_t size = sizeof(_addr);
unsigned char buffer[20];
int fd = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
recvfrom(fd,buffer,sizeof(buffer),0,&_addr,&size);

My question is :
When I’m receive the helloworld packet, the helloworld packet will be placed at the index 0-9, but before I’m proccessing the helloworld packet then i’m also receive the helooworld packet

Will helooworld packet be placed in index 0-9? And that means it will overwrite the previous data or be placed in index 10-19?

>Solution :

In the context of raw sockets and the code example provided, each call to recvfrom will overwrite the contents of the buffer from the start (index 0), assuming the buffer size is sufficient to hold the received data.

To avoid losing data:

  • Process the received data immediately after calling recvfrom.
  • Separate Buffers: If you want to keep multiple packets, you need to copy the received data to a separate buffer before calling recvfrom again or you need to pass different buffers to recvfrom
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading