Search code examples
network-programmingsimulationencapsulationpacketosi

Simulating packet encapsulation


I am developing an application which aims to simulate a real network. In order to do this, I need to have detailed information about how a packet is formed in a system.

Imagine you have an application layer message and you want to encapsulate it in a transport layer payload and add a specific port number for desired process in the header, and then encapsulate it in network layer payload and add IP addresses.

My question is that

  • Where does the encapsulation of upper layer protocols' packets to lower layers happen?
  • Is network card driver responsible for that or some other part in OS? and if so, which part?

I just want to note that I’ve read computer networks: A top down approach and Foruzan's book on the subject but all the information there ,was so theoretical.

Thanks in advance.


Solution

  • If you are asking about a real implementation, usually every message of a layer is conveyed as the whole payload of the lower layer message. Talking about TCP/IP stack in an OS like Windows or Linux, without SSL/TLS, this depends on the types of sockets you use. Supposing you use TCP, STREAM sockets, the application layer message you send with send or write system calls will become the payload of the TCP message. The processing of a TCP segment and an IP datagram happens in the OS Kernel. The processing of a layer 2 frame happens part in the NIC's device driver (in the kernel) and part in the NIC hardware. This depends on the specific NIC.

    Something else to add is that some NIC's are able to calculate the checksum of TCP segments and UDP datagrams. Then the kernel offloads this task to the NIC. Only the checksum.