Encapsulation is the process of adding headers to data at each layer of a stack. Data that is to be sent over a network starts at the application layer and moves down a protocol stack until it leaves a host at the physical layer. At each layer of a hostâ€™s stack, a header is placed in front of the data. Your data combined with a header or headers is a datagram. A datagram at layer 3 is called a packet; a packet starts with a layer-3 header. A datagram at layer 2 is a frame; a frame starts with a layer-2 header (frame header).
A datagram header provides a path up a stack toward an application. A header must contain a data field that indicates the type of data encapsulated at the layer immediately above the layer that added the header. For example, when your computer receives web page from a web server, the header that immediately precedes the web-page data must contain a field that indicates to your computer that the data being received is meant for your web browser. Another example occurs when a host is encapsulating a packet with a frame header at layer 2, the frame header must contain a value that indicates what protocol is being spoken; this value could indicate what type of layer-3 header immediately follows the layer-2 header in the frame.
Letâ€™s revisit the postal service analogy. Suppose you are mailing a gift to your Uncle Charlie in Seattle. You put the gift in a box. You then wrap shipping paper around the box. Finally, you write Uncle Charlieâ€™s address on the shipping paper so the postal service will know how to route the package. You have just encapsulated the gift twice and put an address on the outer wrapping.
Your process of preparing the gift for shipment is analogous to the process a host goes through when it sends data. The data starts off in an application and must move down the stack. As the host is preparing the data for transmission, it encapsulates the data with a header at each layer as the data moves down the stack. The last header contains an address that allows the data to reach its destination.
The header at layer 2 always contains an address; since this is a data link, or frame, header, the address type changes based on the medium on which the frame is to be transmitted. For some protocols, like IP and AppleTalk, the layer-3 header also has an address. If an internetworking host, like a router, examines a frameâ€™s layer-2 address to determine where to send a network message, the internetworking device is bridging frames. If an internetworking device uses a layer-3 address to determine where to send a network message, the internetworking device is routing packets.
When Uncle Charlie receives the package you sent, he checks the destination address on the outside to make sure itâ€™s his. He removes the shipping paper and, then, opens the box. He now finds out what the gift is.
A host, that receives a network message, reads the destination address to determine if the address is its own. If the address matches, the host moves the data up the stack, de-encapsulating at each layer. Each layer removes a header that was added by the corresponding layer on the transmitting host until all that is left is the original data that was transmitted.