Search code examples
c++csocketsnetwork-programmingraw-sockets

Nic Buffer In Raw Socket


I'm new in raw socket

We know that if we using raw socket we need to handle everything including the buffer

if we use raw socket we can use the following call :

recvfrom();
recvmsg();

Suppose I'm using the recvfrom() call

recvfrom(sockfd,buffer,sizeof(buffer),&sock_addr,&size_sock_addr)

When the data comes from the network it will be placed in the Nic buffer.

My question is : (1). Does when we call the recv,recvfrom, recvmsg basically we copying data from the Nic buffer to the buffer in the main memory?

(2). I will use Example :

buffer[3000];
recvfrom(sockfd,buffer,sizeof(buffer),&sock_addr,&size_sock_addr);

Suppose my Nic card using the ring buffer design and my Nic card ring buffer size is 3000 byte and i'm receive a 2 packet and each packet have a size 1500 byte.

I will use array representation to represent the ring buffer:

Ring_Buffer[2] = {packet_1,packet_2};

we called the recvfrom,recvmsg that mean we trying to read from the Nic to the buffer in the ram.

We called recvfrom() and now we will placed the packet_1 into the buffer and now the read pointer Will be move to the next index so whenever we invoke the recvfrom again we basically placed the packet_2 into the buffer

So does when we using recvfrom or recvmsg does it will make read pointer in the ring buffer is moved?


Solution

  • In common operating systems the OS kernel will read from the NIC, process the data and put it into the sockets receive buffer. The application will then read from this receive buffer with recv/recvmsg/recvfrom.