Hello everyone!
In the previous two sections, we have almost finished learning about the OSI layer 7 model and TCP/IP model. Today, we will learn about network layer data encapsulation and extended IP addresses.
Network Layer Data Encapsulation
The network layer receives data from the transport layer and adds the source and destination addresses to the data.

MAC addresses usually exist in the same plane address space and have no clear address hierarchy. Therefore, MAC addresses are applicable only to the communication of hosts on the same network segment. In addition, MAC addresses are fixed in hardware, which is not flexible. For interconnection communication between different networks, The software-based network layer address - IP address is used for communication, providing greater flexibility.
First, let's use the IP address setting interface in Windows as an example. You may wonder what the IP address, subnet mask, default gateway, and DNS server mean.

When learning IP addresses, you may also encounter concepts such as network addresses, broadcast addresses, and subnets. What do these mean?
IP address
Overview
To enable network communication, a computer must have a network address for quick location. An IP address is the unique ID of a computer on the network. It is the same as the real world that express delivery requires a specific residential address.
The IP address is represented by four decimal numbers separated by dots (.) and each number ranges from 0 to 255. For example, the IP address of a host is 128.20.4.1.
IP Address Composition
IP address = Network address + Host address (also called host number and network number)
Consider why administrative divisions (countries, cities, and villages) are defined to facilitate efficient management and positioning.
Similarly, a network can be divided into many sub-networks. Each sub-network has its own network address. Each sub-network consists of many computers (or another sub-network).
To find the specified IP address, find the specified network address and then find the corresponding host address in the network.
An IP address is a 4 x 8-bit (1-byte) numeric string consisting of 0 and 1 (IP4).
Take the IP address 192.168.1.168 and subnet mask 255.255.255.0 in the screenshot of Windows 7 as an example. This address contains many meanings:
192.168.100.168 (IP address) = 192.168.1.0 (network address) + 0.0.0.168 (host address)
How to calculate the network address and host address? We need to learn the subnet mask first.
Subnet mask
General comments
A subnet mask is also called a network mask, an address mask, and a subnet mask. It is a 4 x 8-bit (1-byte) numeric string consisting of 0 and 1.
It is used to mask (obscure) part of an IP address to divide it into network address and host address, and to indicate whether the IP address is on a local area network (LAN) or a remote network.
Subnet masks can be used to divide a network into subnets, that is, variable-length subnet masks (VLSM), or merge small networks into large networks, that is, supernets.
The subnet mask cannot exist independently. It must be used together with the IP address.
Subnet mask rules
The length is 4 x 8 bits (1 byte), consisting of consecutive 1s and consecutive 0s.
For example, 11111111.11111111.11111111. 00000000, which corresponds to 255.255.255.0 in decimal notation.
Assume that the IP address of computer A on the LAN is 192.168.1.1 and the subnet mask is 255.255.255.0, as shown in the following figure.

Network address: the IP address that is blocked by consecutive 1s, that is, 11000000.10101000.00000001. 00000000. The corresponding network address is 192.168.1.0.
Host address: The IP address is masked by consecutive zeros, that is, 00000000.00000000.00000000.00000001. The corresponding network address is 0.0.0.1.
Exclude two special addresses from this network:
Broadcast address: 192.168.1.255 (all host IDs are 11111111)
Network address: 192.168.1.0 (The host ID is 00000000.)
Maximum number of hosts in the subnet: 2 to the 8th power 256 - 2
Additional information:
For class A addresses, the default subnet mask is 255.0.0.0. The default subnet mask for class B addresses is 255.255.0.0. The default subnet mask for class C addresses is 255.255.255.0.
Calculate the network address based on the subnet mask.
Calculation method
The calculation process is as follows:
1. Convert the IP address and subnet mask to binary.
2. Perform the AND operation on the two to obtain the network address.
Calculation process: Up-to-down alignment, 1 bit and 1 = 1, and other combinations are 0.
Assume that the IP address is 192.168.1.168 and the subnet mask is 255.255.255.0, the network address is converted as follows: 1. Convert the IP address and subnet mask to binary.
192.168.1.168 is converted into 11000000.10101000.00000001.10101000 in binary mode.
255.255.255.0:11111111.11111111.11111111.00000000
Perform an AND operation between the two.
11000000. 10101000. 00000001. 10101000
AND operation 11111111. 11111111. 11111111. 00000000
-----------------------------------------------------------------------------------------------
RESULT 11000000. 10101000. 00000001. 00000000
Convert the result of the operation to decimal: 192.168.1.0
Practice Now
For example, to connect two computers using a network cable, run the following command:

The following lists the IP address settings. Check whether the network is normal under different settings.
Note: In the first case, the two computers are located on the same network 192.168.0.0. In the second or third case, the two computers are located on different networks.
The network address calculation process is the same as that described above.
Conclusion:
The computers connected to the network cable or through a hub or common switch must be on the same network (network address) and have different host addresses.
Note: The same network is not a physical connection, but a network address.
For example, if two computers are connected to the same router (the same link), they cannot communicate with each other if they set different network addresses.
Extended: IP network segment notation
Example: 192.168.0.0/24
192.168.0.0: network address
24: indicates the number of consecutive 1s in the binary representation of the subnet mask. Here, the value is 11111111·11111111·111111110000000, that is, 255.255.255.0.
Default gateway (address)
What is a gateway?
(You can associate customs? What are customs?)
A device connected to two different networks can be called a gateway device. The gateway is used to implement communication and control between two networks.
The gateway can be an exchanger (at Layer 3 or above), a router, a server enabled with a routing protocol, a proxy server, or a firewall.
The gateway address is the IP address of the gateway device.
Suppose we have two networks:
The IP address of network A ranges from 192.168.1.1 to 192.168.1.254 and the subnet mask is 255.255.255.0.
The IP address of network B ranges from 192.168.2.1 to 192.168.2.254 and the subnet mask is 255.255.255.0.
Communication between the two networks must be achieved through a gateway.
If the host in network A finds that the destination host of the packet is not on the local network, it forwards the packet to its own gateway, which then forwards the packet to the gateway of network B. The gateway of network B forwards the packet to a host in network B (as shown in the figure).

The TCP/IP protocol can implement communication between different networks only after the gateway IP address is set.
Default gateway
A host can have multiple gateways. The default gateway means that if a host cannot find an available gateway, it sends a packet to the default gateway, which processes the packet. The gateway used by the host is generally the default gateway.
Extended: Automatically Setting Default Gateway
An automatic setting is a process of automatically allocating an IP address, subnet mask, and default gateway to a computer on the network by using the Dynamic Host Configuration Protocol (DHCP) server.
Once the default gateway of the network changes, all computers on the network obtain the IP address of the new default gateway as long as the default gateway settings on the DHCP server are changed. This method is applicable to large-scale networks where TCP/IP parameters may change.
Another method of automatically obtaining the gateway is to install the proxy server software (such as the MS Proxy) client program. The principle and method of obtaining the gateway are similar to those of DHCP.
Extension Description
Q: Some people on the Internet have asked why computers connected to the same (Layer 2) switch or hub cannot communicate with each other if they have different network addresses.
Answer: In TCP/IP, when the network layer (identifying the communicating party through the IP address) encapsulates packets and sends them to the next data link layer (identifying the communicating party through the MAC address), the MAC address corresponding to the destination IP address needs to be obtained through ARP broadcast.
However, ARP packets can be broadcast only in the same network address. If the destination computer and the source computer are on different networks, they cannot respond. Therefore, the source computer cannot encapsulate data at the link layer.
ARP Address Resolution Protocol
DNS server
Domain name and DNS
When we visit a website, we often use domain names (more semantically clear and easy to remember than IP, for example, www.google.com).
A domain name is a string of dotted names, usually containing the organization name, and always includes a two-to three-letter suffix to indicate the type of organization or the country or region in which the domain is located.
However, communication between computers is implemented through IP addresses. Therefore, domain names need to be resolved to corresponding IP addresses. The DNS is the server that performs domain name resolution.
DNS maintains a mapping table between domain names and IP addresses to resolve domain names of messages.
Enter the domain name www.google.com in the address box of the browser. The operating system checks whether the local hosts' file contains the mapping. If yes, the operating system invokes the mapping to resolve the domain name.
If the domain name mapping does not exist in the hosts' file, the local DNS resolver caches to check whether the domain name mapping exists. If yes, the DNS resolves the domain name.
If no mapping between hosts and the local DNS resolver cache is available, the DNS server that is set in the TCP/IP parameter is searched for. Here, the DNS server is called the local DNS server. When receiving a query request, the DNS server returns the resolution result to the client if the domain name to be queried is contained in the local configuration area resource, completes domain name resolution. This resolution is authoritative.
If the domain name to be queried is not resolved by the local DNS server but the server has cached the mapping, the IP address mapping is invoked to resolve the domain name. This resolution is not authoritative.
If the local domain file and cache resolution of the local DNS server fails, the local DNS server queries the domain name based on the local DNS server settings (whether forwarders are configured). If the forwarding mode is not used, the local DNS server sends the request to the 13 root DNS servers. After receiving the request, the root DNS server determines who authorizes the domain name (.com). and returns an IP address of the top-level domain name server. When the local DNS server receives the IP information, it will contact the server responsible for the .com domain. When the server responsible for the .com domain receives the request, if it cannot resolve it, it will find the address of a lower-level DNS server managing the .com domain (google.com) to the local DNS server. After receiving the address, the local DNS server searches for the Google.com domain server and repeats the preceding operations until the host www.google.com is found.
If the forwarding mode is used, the DNS server forwards the request to the upper-level DNS server for resolution. If the upper-level DNS server fails to resolve the request, the upper-level DNS server searches for the root DNS server or forwards the request to the upper-level DNS server. The local DNS server returns the result to the local DNS server, which then returns the result to the client, regardless of whether the local DNS server is used for forwarding or root prompting.
Thanks for reading!