1
votes

While doing UDP/TCP raw socket programming in C, why don't we use ethernet header frames in data encapsulation. Why do we only use udp/tcp header and ip header only??

1
What do you mean by encapsulation? Try to clarify :)slezica
Not all links are over ethernet, for one thing.Jonathan Leffler

1 Answers

3
votes

You need to think about the OSI model. We are sending raw data where we specify TCP and UDP headers, which are at the transport layer, or layer 4. This includes portions of layer 3, or IP, in terms of addressing, which is the network layer.

We need not care about layer 2. That is ethernet. We have no guarantee it will even use ethernet. Embedded systems could send through ADSL, DOCSIS, fibre links, wifi, etc.

Another reason is that the hardware of the network card already does headers for layer 2. Due to the circuitry and the inherent complexity of the transmission we don't put our own parity, flow control, and other things on our packet.

You can, if you want, edit the layer 2 frame using certain drivers. It won't be useful for more than one hop, however, due to the fact that the router will retransmit it with its own layer 2 frame. In addition, malformed frames are more likely to be dropped by hardware due to lack of or error in error correction details, and due to the fact it is handled ad a lower level.