Understanding How Home Networks Access External Servers

As someone who frequently deals with electronics, I have always had a question.

Is there no trick behind the ability to casually interact with external servers just by tapping on a phone or typing on a computer at home?

How do home devices access external servers?

Before this, I only knew part of the story.

Therefore, I have summarized some common concepts and the process of network devices accessing external servers.

First, let’s look at a few concepts.

1. The “Identity” of Home Network Devices: Public IP vs. Private IP

(1) Home IP (Private IP) IP (Private IP)

Definition and Function: The mobile phones, computers, smart TVs, and other network devices in our homes generally use private IP addresses. These addresses are used within the local area network (LAN), and common private IP address ranges include:

·10.0.0.0 – 10.255.255.255

·172.16.0.0 – 172.31.255.255

·192.168.0.0 – 192.168.255.255

Private IP addresses can be reused in different local area networks and are mainly used for communication between devices within the LAN. It’s like setting up a LAN at home through a router, where devices transfer data relying on private IP addresses to identify each other.

Acquisition Method: Typically, the home router automatically assigns private IP addresses to connected devices via DHCP (Dynamic Host Configuration Protocol). When a device powers on and connects to the network, the router selects an address from the private IP address pool to assign to it.

(2) Public IP IP

Definition and Function: A public IP is a globally unique IP address, like a “global passport” for devices in the network world. Devices with a public IP can be directly accessed by other devices on the internet. For example, various website servers that we access daily must have a public IP so that users worldwide can access these sites by entering the URL in their browsers.

Acquisition Method: Generally assigned to users by Internet Service Providers (ISPs). Business users or some individuals with special needs may apply for a public IP from their ISP, which allocates it from its IPv4 address pool. However, most home users typically do not receive a public IP directly from their ISP but are provided with other means to access the internet.

2. The “Translator” of the Network World: Network Address Translation (NAT)

(1) Background and Purpose

·Alleviating IP Shortages: With limited IPv4 addresses and an increasing number of network devices, public IPs have become scarce. NAT allows multiple internal devices to share a limited number of public IPs, solving the address shortage problem.

·Enhancing Security: NAT hides internal private IPs, making only the public IP of the NAT device visible externally, reducing the risk of attacks on the internal network.

(2) Working Principle

·Address Translation: In the LAN, a device with IP 192.168.1.10 sends a packet to the NAT device (router), which replaces the source IP with the public IP (for example, 202.100.1.1), and records the mapping relationship. When the server responds,NAT uses the mapping to convert the destination IP back to the private IP of device A.

·Port Mapping: When multiple devices access the external network simultaneously, IP alone cannot distinguish responses. For example, device A uses source port 1000, and device B uses 2000, after NAT conversion, the source IP is the NAT device’s public IP, but the ports become 3000 and 4000. When the server responds, NAT forwards the data based on the port mapping.

(3) Types

·Static NAT: A static NAT maps one internal private IP to a fixed public IP, suitable for internal devices that require a fixed public IP for access, such as enterprise web servers.

·Dynamic NAT: Dynamically assigns public IPs from a public IP address pool to internal devices, returning the address to the pool after use.

·Network Address Port Translation (NAPT) : The most commonly used method, which translates both IP and port numbers, allowing multiple devices to share a single public IP, commonly used in home networks.

(4) Impact and Limitations

·Advantages: Alleviates IPv4 address shortages, enhances network security, and facilitates internet access for businesses and homes.

·Limitations: Disrupts end-to-end IP communication, and some applications based on this may encounter compatibility issues, such as P2P applications requiring special techniques to establish connections.

3. The “Phone Book” of the Network World: DNS (Domain Name System)

DNS, short for Domain Name System, is like the “phone book” of the network world. Devices in the network identify and communicate with each other through IP addresses, and combinations of numbers like192.168.1.10 are hard to remember, so people invented easy-to-remember domain names likebaidu.com andtaobao.com.

The role of DNS is to resolve the input domain name, such asbaidu.com, into the corresponding IP address, which is the IP address corresponding to the Baidu server. Thus, when entering a domain name in the browser to access a website, the DNS system helps us find the actual server IP address where the website is located, establishing a connection to retrieve web content. In simple terms, DNS resolves the conflict between human memory convenience and computer recognition convenience, allowing us to easily access various network resources. If there is a problem with DNS, such as a DNS server failure, it may become impossible to access websites via domain names, and one can only access them through IP addresses or may not be able to access the internet at all.

With the above concepts in mind, the next part is crucial!!

4. The Complete Process of Home Devices Accessing External Servers

When devices in a home use private IPs to access external servers through routers and NAT conversion, the entire process is as follows:

(1) Device Initiates Request

Devices such as mobile phones and computers in the home are assigned private IP addresses (for example,192.168.1.100). When they want to access an external server (like Baidu’s server), they first construct a data packet containing the target server’s IP address and port number. If a domain name is entered, the device will first query the DNS server for the IP address corresponding to that domain name. For example, when enteringbaidu.com, the device asks the DNS server for the IP address corresponding tobaidu.com, and after the DNS server returns the corresponding IP address, the device constructs the data packet. The source IP address of the packet is the device’s own private IP, and the source port number is a randomly generated available port (such as5000), and then sends the data packet to the home router.

(2) Router Receives and Processes

The home router, as the hub connecting the LAN to the external network, receives the data packet sent by the device. The router first checks the destination IP address of the packet to confirm it is an external network address, determining that NAT conversion is needed.

(3) NAT Conversion – Address and Port Mapping

The router selects a public IP address from the available public IP address pool (usually assigned by the ISP, which may be dynamic or static) (let’s assume it is202.100.1.1), while also assigning a new port number for that connection (such as6000). The router creates a NAT translation table, recording the mapping relationship between the private IP address (192.168.1.100:5000) and the public IP address and port (202.100.1.1:6000). Then, the router modifies the source IP address of the packet to the selected public IP address, the source port number to the newly assigned port number, and sends the modified packet to the external network.

(4) Packet Transmission in the External Network

The modified packet is transmitted through the ISP’s network infrastructure on the internet, being forwarded by a series of routing devices based on the destination IP address in the packet, ultimately reaching the target external server (such as Baidu’s server). The server receives the packet showing the source IP address as the router’s public IP address (202.100.1.1) and port number (6000).

(5) External Server Responds

After processing the request, the external server constructs a response packet, using the router’s public IP address (202.100.1.1) and port number (6000) as the destination IP address and port, sending back the response data.

(6) Router Receives Response and Converts Back to Private IP

The router receives the response data packet from the external server, checking the destination IP address and port number (202.100.1.1:6000), querying the NAT translation table to find the corresponding private IP address and port (192.168.1.100:5000). Then, the router modifies the destination IP address of the packet to the original device’s private IP address, changes the destination port number to the original device’s port number, and forwards the packet to the corresponding device in the home LAN.

(7) Device Receives Response

The home device receives the response data packet forwarded by the router, identifying it as a response to its previous request based on the port number in the packet, thus completing a full communication process with the external server.

Through the detailed introduction of various network concepts and processes above, I believe everyone now has a clearer understanding of how their home network devices access external servers.

Leave a Comment