Back to series
· 15 min read

IP Address: How Computers Are Located on a Network


1. What Is an IP Address?

When we use a browser to visit a website, call a remote API, or connect to a database, data needs to be transmitted from one computer to another.

But the number of devices on a network is enormous. How does a computer know who the data should be sent to?

This requires IP addresses.

IP address stands for Internet Protocol Address. It is the logical address used by the IP protocol to identify network interfaces and address packets.

For example:

192.168.1.100

This is a common IPv4 address.

When one computer needs to send data to another computer, the IP protocol carries the source IP address and destination IP address in the packet.

Network devices use the destination IP address to look up routing information and decide which network interface the data should be forwarded through.

1. What Problems Do IP Addresses Solve?

IP addresses mainly solve three problems:

Problem Role of the IP address
How to identify a network interface Configure a logical address for the network interface
How to determine the network location Identify the network it belongs to via the network prefix
How to find the destination network Perform routing and forwarding based on the destination IP address

For example:

Client:
IP address: 192.168.1.100

Server:
IP address: 203.0.113.10

When the client needs to access the server, it sets the destination IP address to 203.0.113.10.

The router uses this destination address to look up the routing table, select the next hop, and forward the packet step by step to the destination network.

2. Is an IP Address a Device's Permanent ID?

No.

An IP address is a logical address at the network layer, not a permanent, unchanging hardware identity for a device.

The same computer can:

  • Use different IP addresses on different networks.
  • Have multiple IP addresses at the same time.
  • Obtain an IP address automatically via DHCP.
  • Use a statically configured IP address.
  • Obtain an additional IP address through a virtual network interface.
  • Have independent network addresses in containers or virtual machines.

For example, a laptop may be connected to both Wi-Fi and a wired network at the same time, and each network interface can have its own IP address.

Therefore, a more accurate statement is:

An IP address is used to identify a logical communication endpoint in the network, rather than to permanently identify a physical device.

3. What Is the Difference Between an IP Address and a MAC Address?

Both IP addresses and MAC addresses participate in network communication, but they solve different problems.

Aspect IP address MAC address
Layer Network layer Data link layer
Primary purpose Network-layer logical addressing Link-layer addressing
Typical format IPv4, IPv6 48-bit Ethernet MAC address
Example 192.168.1.100 00:1A:2B:3C:4D:5E
Primary devices Router Switch
Can it change? Yes Can be modified or randomized
Scope Can be routed across multiple networks Usually used on the current link

For example, when a computer accesses a remote server:

  • The destination IP address is usually the remote server's IP address.
  • The destination MAC address in the Ethernet frame is usually the MAC address of the next hop on the current link.

If the destination server is not on the local subnet, the destination MAC address is usually the MAC address of the default gateway, not the remote server's MAC address.

This is also the key to understanding IP addressing and routing and forwarding.


2. IPv4 Addresses: The Most Common IP Address Format

IPv4 is an Internet protocol version that is still widely used today.

An IPv4 address consists of a 32-bit binary number.

1. How IPv4 Addresses Are Represented

A 32-bit IPv4 address is usually divided into four groups of 8 bits each.

Each group is called a byte, also known as an octet.

For example:

192.168.1.100

The corresponding binary form:

11000000.10101000.00000001.01100100

Converting each group of binary digits to decimal:

Binary Decimal
11000000 192
10101000 168
00000001 1
01100100 100

Therefore:

11000000.10101000.00000001.01100100
=
192.168.1.100

2. Why is the range of each group 0 to 255?

Each group contains 8 binary bits.

The number of combinations that 8 binary bits can represent is:

2^8=256

Therefore, the value range is:

0 \sim 255

The entire IPv4 address consists of 32 bits.

The theoretical number of addresses is:

2^{32}=4,294,967,296

That is, about 4.29 billion addresses.

However, not all of these addresses can be assigned to ordinary hosts, because they include private addresses, multicast addresses, special-purpose addresses, reserved addresses, and so on.

3. Binary Conversion of IPv4 Addresses

For example, convert 192.168.1.10 into binary.

192 = 11000000
168 = 10101000
1   = 00000001
10  = 00001010

The final result:

11000000.10101000.00000001.00001010

Conversely, to convert binary to decimal, you need to calculate according to the weight of each bit.

For example:

11000000_2

The calculation process:

1\times128+1\times64=192

Therefore:

11000000_2=192_{10}

4. Composition of an IPv4 Address

An IPv4 address does not simply represent a single computer.

It can usually be divided into:

  • Network prefix: identifies the network to which the device belongs.
  • Host part: identifies the specific interface within that network.

For example:

IP address: 192.168.1.100
Subnet mask: 255.255.255.0

The corresponding CIDR notation:

192.168.1.100/24

Here, /24 means that the first 24 bits belong to the network prefix.

The remaining 8 bits belong to the host part.

Therefore:

Network address: 192.168.1.0
Host address: 100

Note: The division between the network address and the host part is determined by the subnet mask or the CIDR prefix length, not by the dots in the IPv4 address.


III. Network Address, Host Address, and Broadcast Address

To understand IPv4 addresses, you must understand three concepts:

  • Network address.
  • Host address.
  • Broadcast address.

Take 192.168.1.100/24 as an example.

1. Network Address

A network address is used to identify an IP subnet.

For /24:

IP address: 192.168.1.100
Subnet mask: 255.255.255.0

The first 24 bits belong to the network part.

Therefore, the network address is:

192.168.1.0

In the network address, the host part is all zeros.

An ordinary host cannot use this address as a regular unicast host address within that subnet.

2. Host Address

A host address is used to identify a specific interface within a subnet.

In 192.168.1.0/24, the range of usable host addresses in a traditional IPv4 subnet is:

192.168.1.1
to
192.168.1.254

Among them:

  • 192.168.1.1 can be assigned to the gateway.
  • 192.168.1.100 can be assigned to a computer.
  • 192.168.1.200 can be assigned to a server.

These are only examples; the actual allocation depends on network planning.

3. Broadcast Address

A broadcast address is used to send broadcast data to all hosts within an IPv4 subnet.

For:

192.168.1.0/24

the broadcast address is:

192.168.1.255

Its host part is all ones.

4. Relationship Among the Three Types of Addresses

Address type Example Purpose
Network address 192.168.1.0 Identifies the subnet
Host address 192.168.1.100 Identifies an interface within the subnet
Broadcast address 192.168.1.255 Broadcasts to all hosts in the subnet

Note the following:

  • In point-to-point links, a /31 subnet can use both addresses as endpoints, without following the traditional allocation of network and broadcast addresses.
  • /32 represents a single IPv4 address.
  • Not all IPv4 subnets follow the traditional rule of reserving two addresses.

IV. Subnet Mask: How to Distinguish the Network Part from the Host Part?

A subnet mask is a 32-bit value used in IPv4 networks to divide the network prefix from the host part.

Like an IP address, it consists of four groups of 8 binary bits.

For example:

255.255.255.0

Binary form:

11111111.11111111.11111111.00000000

Where:

  • Consecutive 1s represent the network prefix.
  • The trailing 0s represent the host portion.

1. How does a subnet mask work?

Assume:

IP address: 192.168.1.100
Subnet mask: 255.255.255.0

Perform a bitwise AND operation on the IP address and the subnet mask.

Rules:

IP bit Mask bit Result
0 0 0
0 1 0
1 0 0
1 1 1

Calculation process:

IP address:
11000000.10101000.00000001.01100100

Subnet mask:
11111111.11111111.11111111.00000000

Bitwise AND:
11000000.10101000.00000001.00000000

Convert to decimal:

192.168.1.0

Therefore, the network address is:

192.168.1.0

2. Common subnet masks

CIDR Subnet mask Network bits Host bits
/8 255.0.0.0 8 24
/16 255.255.0.0 16 16
/20 255.255.240.0 20 12
/24 255.255.255.0 24 8
/25 255.255.255.128 25 7
/26 255.255.255.192 26 6
/27 255.255.255.224 27 5
/28 255.255.255.240 28 4
/30 255.255.255.252 30 2
/31 255.255.255.254 31 1
/32 255.255.255.255 32 0

3. How do you calculate the number of addresses in a subnet?

For a standard IPv4 subnet, when the number of host bits is h:

Total addresses=2^h

Number of assignable hosts in a traditional broadcast subnet:

Usable hosts=2^h-2

For example:

192.168.1.0/24:

h=32-24=8

Total number of addresses:

2^8=256

Number of traditionally usable hosts:

256-2=254

The two subtracted addresses are the network address and the broadcast address.

For /31 point-to-point links and /32 host routes, this traditional formula cannot be applied directly.


V. CIDR: Classless Inter-Domain Routing

CIDR is short for Classless Inter-Domain Routing, known in Chinese as “classless inter-domain routing.”

CIDR uses the prefix length to describe the network portion and no longer relies on the fixed network boundaries of traditional Class A, B, and C addresses.

1. What is CIDR?

For example:

192.168.1.100/24

The 24 after the slash indicates that the network prefix length is 24 bits.

Therefore:

Network prefix: 192.168.1
Host part: last 8 bits

CIDR allows networks to allocate address blocks of different sizes according to actual needs.

2. Why is CIDR needed?

Traditional IPv4 classful addressing divides addresses into classes such as A, B, and C.

Class Default prefix Default mask
Class A /8 255.0.0.0
Class B /16 255.255.0.0
Class C /24 255.255.255.0

This approach has obvious problems:

Suppose an organization only needs 500 IPv4 addresses.

If only traditional classful addresses can be used, it may need to request an address block much larger than its actual needs.

CIDR can allocate addresses flexibly according to actual needs.

For example:

192.168.0.0/23

Contains:

2^{32-23}=512

IPv4 addresses.

In a traditional broadcast subnet, 510 host addresses can be allocated.

3. How is a CIDR address block calculated?

For example:

192.168.10.0/26

Number of host bits:

32-26=6

Total number of addresses:

2^6=64

Subnet mask:

255.255.255.192

The address block range is:

192.168.10.0
to
192.168.10.63

Where:

  • Network address: 192.168.10.0
  • Usable host addresses: 192.168.10.1 to 192.168.10.62
  • Broadcast address: 192.168.10.63

4. CIDR and route aggregation

CIDR is not only used for subnetting; it can also reduce the number of route entries in a routing table.

For example, the following four contiguous networks:

192.168.0.0/24
192.168.1.0/24
192.168.2.0/24
192.168.3.0/24

can be aggregated into:

192.168.0.0/22

The prerequisite is that these networks are contiguous in address space and meet the aggregation boundary requirements.

In this way, a router can use one larger route prefix to represent multiple contiguous networks.

The core value of CIDR:

  • Flexible address allocation.
  • Reduced IPv4 address waste.
  • Support for route aggregation.
  • Reduced routing table size.

VI. Subnetting: How do you split one network into multiple networks?

Subnetting is the process of dividing a larger IP network into multiple smaller subnets.

This can improve address utilization and also enable network isolation and management.

1. Why is subnetting needed?

Suppose a company has three departments:

Department Number of devices
Development Department 100 devices
Testing Department 50 devices
Operations Department 20 devices

If all devices are on the same subnet, it may lead to:

  • An excessively large broadcast domain.
  • Complex network management.
  • Lack of isolation between departments.
  • Insufficiently flexible address allocation.
  • Difficulty in implementing security policies.

Different departments can be divided into different subnets.

For example:

Development Department: 192.168.10.0/25
Testing Department: 192.168.10.128/26
Operations Department: 192.168.10.192/27

2. Basic principles of subnetting

Suppose the original network is:

192.168.1.0/24

Originally, there are 8 host bits.

If 2 of the bits are allocated to subnet numbering:

Original network prefix: /24
New network prefix: /26

Number of subnets:

2^2=4

Number of addresses per subnet:

2^{32-26}=64

Number of traditionally usable hosts:

64-2=62

Subnetting result:

Subnet Address range Network address Broadcast address
Subnet 1 192.168.1.0 - 192.168.1.63 192.168.1.0 192.168.1.63
Subnet 2 192.168.1.64 - 192.168.1.127 192.168.1.64 192.168.1.127
Subnet 3 192.168.1.128 - 192.168.1.191 192.168.1.128 192.168.1.191
Subnet 4 192.168.1.192 - 192.168.1.255 192.168.1.192 192.168.1.255

3. Subnetting Diagram

flowchart TD
    A["192.168.1.0/24 Original network"] --> B["Divide into four /26 subnets"]
    B --> C["Subnet 1: 192.168.1.0/26"]
    B --> D["Subnet 2: 192.168.1.64/26"]
    B --> E["Subnet 3: 192.168.1.128/26"]
    B --> F["Subnet 4: 192.168.1.192/26"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style C fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style D fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#F1F3F5,stroke:#C7CDD4,color:#334155

4. How to Determine Whether Two IPs Belong to the Same Subnet

Assume:

Host A: 192.168.1.100/24
Host B: 192.168.1.200/24

The network prefixes of both hosts are:

192.168.1.0/24

Therefore, they belong to the same subnet.

Now consider:

Host A: 192.168.1.100/24
Host B: 192.168.2.100/24

Their network addresses are, respectively:

192.168.1.0
192.168.2.0

Therefore, they belong to different subnets.

Decision logic:

flowchart TD
    A["Obtain the local IP and subnet mask"] --> B["Calculate the local network address"]
    B --> C["Obtain the target IP"]
    C --> D["Use the local subnet mask to calculate the target network address"]
    D --> E{"Are the two network addresses the same?"}
    E -->|Yes| F["The target belongs to the local subnet"]
    E -->|No| G["The target belongs to another network"]
    F --> H["Send over the local link"]
    G --> I["Forward through the default gateway"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style E fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style F fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style G fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style H fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976

Note: this describes the common determination method used in ordinary IPv4 unicast communication. Actual systems may also be affected by policy routing, VPNs, special routes, and proxy mechanisms.


VII. IPv4 Address Classification and Special Addresses

IPv4 addresses can be divided by purpose into public addresses, private addresses, loopback addresses, link-local addresses, multicast addresses, and so on.

1. Public IP Addresses

A public IP address can be routed within the global Internet routing system.

For example:

8.8.8.8

This is a common public DNS service address.

Public IP addresses are usually managed and allocated by the Internet address allocation system.

Note:

  • Having a public IP does not mean the service is necessarily accessible from outside.
  • Firewalls, security groups, and routing policies may still block connections.
  • A public IP is not the same as a static IP; the address may also change dynamically.

2. Private IP Addresses

Private IPv4 addresses are mainly used for internal networks.

RFC 1918 defines three private address ranges:

Address Range CIDR Number of Addresses
10.0.0.0 - 10.255.255.255 10.0.0.0/8 16,777,216
172.16.0.0 - 172.31.255.255 172.16.0.0/12 1,048,576
192.168.0.0 - 192.168.255.255 192.168.0.0/16 65,536

Common home network:

192.168.1.100

Common enterprise internal network:

10.0.0.10

Private addresses cannot be routed directly on the global Internet as ordinary public destination addresses.

Communication with the public Internet usually requires NAT or another network boundary mechanism.

3. Loopback Address

IPv4 loopback address range:

127.0.0.0/8

The most common is:

127.0.0.1

It usually refers to the local host.

For example:

ping 127.0.0.1

This operation mainly tests the loopback capability of the local TCP/IP network protocol stack.

Accessing:

http://127.0.0.1:8080

means accessing port 8080 on the current computer, rather than another computer on the local network.

4. Link-Local Addresses

IPv4 link-local address range:

169.254.0.0/16

When a device cannot obtain an IPv4 address via DHCP, some operating systems may automatically configure a link-local address.

It is mainly used for local communication on the same link.

Link-local addresses are usually not forwarded by routers to other networks.

5. Multicast Addresses

IPv4 multicast address range:

224.0.0.0/4

Multicast is used to send data to multiple receivers in a specific multicast group.

Common scenarios:

  • Multimedia distribution.
  • Network discovery.
  • Routing protocol communication.
  • LAN service discovery.

Multicast differs from broadcast:

  • Broadcast targets all relevant devices in the broadcast domain.
  • Multicast targets receivers that have joined a specific multicast group.

6. Quick Reference for Special Addresses

Address or Range Purpose
0.0.0.0 Unspecified address; in certain contexts, indicates the default route
127.0.0.0/8 Loopback address
169.254.0.0/16 IPv4 link-local address
10.0.0.0/8 Private address
172.16.0.0/12 Private address
192.168.0.0/16 Private address
224.0.0.0/4 IPv4 multicast address
255.255.255.255 Limited broadcast address
192.0.2.0/24 Documentation example address
198.51.100.0/24 Documentation example address
203.0.113.0/24 Documentation example address

Among them, 192.0.2.0/24, 198.51.100.0/24, and 203.0.113.0/24 are TEST-NET address blocks for documentation and examples and should not be used as ordinary public service addresses.


8. Default Gateway: How to Leave the Local Network?

The default gateway is the next-hop router that a host typically uses when no more specific route matches.

The problem it solves is:

When the destination IP does not belong to the local subnet, who should the data be handed to for further forwarding?

1. Why Is a Default Gateway Needed?

Suppose there is a computer on the local network:

IP address: 192.168.1.100
Subnet mask: 255.255.255.0
Default gateway: 192.168.1.1

It needs to access:

203.0.113.20

The destination IP does not belong to:

192.168.1.0/24

Therefore, the client needs to send the data to the default gateway.

2. How Does a Default Gateway Work?

The client first determines whether the destination address belongs to the local subnet.

If it belongs to the local subnet, send it directly over the local link.

If it does not belong to the local subnet, look up the routing table.

When no more specific route matches, use the default route.

For example:

default via 192.168.1.1 dev eth0

This route means:

By default, send the data to 192.168.1.1 through the eth0 interface.

3. Default Gateway Forwarding Process

flowchart TD
    A["Host prepares to send an IP packet"] --> B["Check destination IP address"]
    B --> C{"Does the destination belong to the local subnet?"}
    C -->|Yes| D["Resolve destination host MAC address"]
    C -->|No| E["Query local routing table"]
    E --> F{"Is there a matching route?"}
    F -->|No| G["Check whether a default route exists"]
    F -->|Yes| H["Select next hop of matching route"]
    G --> I{"Does a default route exist?"}
    I -->|No| J["Report no available route"]
    I -->|Yes| K["Select default gateway"]
    H --> L["Resolve next-hop MAC address"]
    K --> L
    D --> M["Encapsulate data link frame"]
    L --> M
    M --> N["Send data frame"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style C fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style D fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style G fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style H fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style I fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style J fill:#FCE8E8,stroke:#DCA6A6,color:#843F3F
    style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style L fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style M fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style N fill:#DCEFE5,stroke:#A3CBB4,color:#315C43

4. What Is the Difference Between the Default Gateway and the Destination IP?

This is a very important concept.

Suppose:

Client IP: 192.168.1.100
Default gateway: 192.168.1.1
Destination server: 203.0.113.20

When the client sends data:

Field Address
IP source address 192.168.1.100
IP destination address 203.0.113.20
Ethernet source MAC Client NIC MAC
Ethernet destination MAC Default gateway MAC

Therefore:

  • The IP destination address usually points to the final destination server.
  • The MAC destination address points to the next hop on the current link.

After receiving the data, the router continues forwarding it according to the routing table.


9. Routing: How Does Data Cross Multiple Networks?

IP addresses solve the problem of logical addressing, but IP addresses alone are not enough.

Data also needs to know how to traverse different networks to reach its destination.

This is what routing does.

1. What Is Routing?

Routing is the process by which network devices select a forwarding path based on the destination IP address.

Routers maintain routing tables to determine:

  • Where the destination network is.
  • Which interface the data should be sent from.
  • Which router is the next hop.
  • What routing rules to use.

2. What Is a Routing Table?

In Linux, you can use:

ip route

View the routing table.

Example:

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
10.0.0.0/8 via 192.168.1.254 dev eth0

Meaning:

Route entry Description
default via 192.168.1.1 Default traffic is sent to 192.168.1.1
192.168.1.0/24 dev eth0 The local subnet is sent directly from eth0
10.0.0.0/8 via 192.168.1.254 When accessing 10.0.0.0/8, traffic is sent to the specified next hop

3. Longest Prefix Match

A router may have multiple routes that match the destination IP at the same time.

It usually prefers the route with the longest prefix length, that is, the most specific route.

For example:

10.0.0.0/8 via 192.168.1.1
10.1.0.0/16 via 192.168.1.2
10.1.2.0/24 via 192.168.1.3

If the destination IP is:

10.1.2.100

All three routes may match.

But:

10.1.2.0/24

has the longest prefix, so it is matched first.

This rule is called longest prefix matching, Longest Prefix Match.

4. How Does Data Pass Through Multiple Routers?

Suppose data is sent from a client to a remote server:

Client
  |
Router A
  |
Router B
  |
Router C
  |
Server

Actual forwarding process:

flowchart TD
    A["Client sends IP packet"] --> B["Router A receives packet"]
    B --> C["Look up routing table"]
    C --> D["Select next-hop Router B"]
    D --> E["Re-encapsulate link-layer frame"]
    E --> F["Router B receives packet"]
    F --> G["Look up routing table"]
    G --> H["Select next-hop Router C"]
    H --> I["Re-encapsulate link-layer frame"]
    I --> J["Router C receives packet"]
    J --> K["Look up route to destination network"]
    K --> L["Forward to the network where the server resides"]
    L --> M["Server receives packet"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style C fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style D fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style E fill:#F1F3F5,stroke:#C7CDD4,color:#334155
    style F fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style G fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style H fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style I fill:#F1F3F5,stroke:#C7CDD4,color:#334155
    style J fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style K fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style L fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style M fill:#DCEFE5,stroke:#A3CBB4,color:#315C43

Note:

In normal IP forwarding:

  • The IP source and destination addresses usually remain unchanged.
  • Each time it passes through a Layer 3 routing device, the IP TTL is usually decremented by 1.
  • The router re-encapsulates the link-layer frame required by the next hop.
  • If NAT is performed, the IP address or port may be modified.
  • If it passes through a tunnel, a new outer IP header may also be added.

5. What Is TTL?

TTL is the abbreviation of Time To Live.

IPv4 packets contain a TTL field, which limits the number of routing hops a packet can traverse in the network.

Each time a packet passes through a router performing IP forwarding, the TTL is usually decremented by 1.

When the TTL reaches 0, the router discards the packet and usually notifies the sender with an ICMP Time Exceeded message.

This prevents packets from being forwarded indefinitely in a misconfigured routing loop.

IPv6 uses the Hop Limit field to implement a similar function.


10. NAT: How Do Private IPs Access the Internet?

NAT is the abbreviation of Network Address Translation, known in Chinese as 网络地址转换.

It allows network devices to modify the address information in IP packets, thereby enabling communication between different address spaces.

The most common scenario is:

Multiple devices using private IPs access the Internet through a single public IP.

1. Why Is NAT Needed?

Suppose there are three devices in a home network:

Device Private IP
Laptop 192.168.1.100
Smartphone 192.168.1.101
Tablet 192.168.1.102

They all need to access the Internet.

However, these private IP addresses cannot be directly routed on the global Internet as ordinary public addresses.

A home router can use NAT to translate internal private addresses into a public address.

For example:

Home public IP: 203.0.113.5

2. How NAT Works

The most common type is NAPT, also known as port address translation.

It not only modifies IP addresses but also uses port numbers to distinguish different connections.

For example:

Internal address Translated public mapping
192.168.1.100:50001 203.0.113.5:40001
192.168.1.101:50002 203.0.113.5:40002
192.168.1.102:50003 203.0.113.5:40003

When data is sent from the internal network to the Internet, the router creates a NAT mapping.

When the server returns data, the router uses the mapping table to find the corresponding internal device.

3. NAT Communication Flow

flowchart TD
    A["Internal device initiates connection"] --> B["Data reaches NAT router"]
    B --> C["Create or look up NAT mapping"]
    C --> D["Translate source IP and source port"]
    D --> E["Send data using public address"]
    E --> F["Remote server returns response"]
    F --> G["NAT router receives response"]
    G --> H["Look up NAT mapping table"]
    H --> I{"Does a corresponding mapping exist?"}
    I -->|No| J["Discard or handle according to rules"]
    I -->|Yes| K["Restore internal destination IP and port"]
    K --> L["Forward to corresponding internal device"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style C fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style D fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style G fill:#E8E0F7,stroke:#B6A5D9,color:#4F3B75
    style H fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style I fill:#FFF1D6,stroke:#E8C98B,color:#73551D
    style J fill:#FCE8E8,stroke:#DCA6A6,color:#843F3F
    style K fill:#DCEFE5,stroke:#A3CBB4,color:#315C43
    style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976

4. Common Types of NAT

Type Function Common Scenario
Static NAT Fixed mapping between private and public addresses Internal service mapping
Dynamic NAT Dynamically allocate addresses from a public address pool Enterprise networks
NAPT/PAT Multiple internal addresses share a public address and are distinguished by port Home routers
DNAT Modify destination address Port forwarding
SNAT Modify source address Internal network access to the public Internet

5. NAT Is Not a Firewall

This is a common misconception.

NAT is mainly responsible for address translation.

A firewall is mainly responsible for access control and traffic filtering.

Although home routers usually provide both NAT and firewall functions, they are not the same mechanism.

For example:

  • NAT allows multiple devices to share a public address.
  • A firewall can block connections from certain IPs or ports.
  • Port forwarding can map external access to internal services.
  • Firewall rules can still block such access.

11. IPv6: Solving the IPv4 Address Shortage

IPv4 has only a 32-bit address space.

As the number of Internet devices continues to increase, IPv4 addresses have gradually become scarce.

IPv6 is the next-generation Internet protocol and uses 128-bit addresses.

1. IPv6 Address Format

IPv6 addresses are usually represented in hexadecimal, divided into groups of 16 bits each, for a total of eight groups.

For example:

2001:0db8:0000:0000:0000:ff00:0042:8329

Each group consists of four hexadecimal digits.

2. IPv6 Address Compression Rules

IPv6 addresses can be abbreviated.

Main rules:

  1. Leading consecutive zeros in each group can be omitted.
  2. A consecutive sequence of all-zero groups can be compressed using ::.
  3. Within the same address, :: can be used at most once.

For example:

2001:0db8:0000:0000:0000:ff00:0042:8329

Abbreviated as:

2001:db8::ff00:42:8329

Another example:

2001:0db8:0000:0000:0000:0000:0000:0001

Abbreviated as:

2001:db8::1

3. How Large Is the IPv6 Address Space?

IPv6 addresses are 128 bits long.

Total number of addresses:

2^{128}

This is an extremely vast address space.

IPv6 no longer faces the same scale of public address scarcity as IPv4.

4. Differences Between IPv6 and IPv4

Comparison Dimension IPv4 IPv6
Address length 32 bits 128 bits
Address representation Dotted decimal Colon-separated hexadecimal
Number of addresses Approximately 4.3 billion 2^{128}
Broadcast Supports broadcast Does not use broadcast
Multicast Supported Supported
Address resolution ARP Neighbor Discovery Protocol
Autoconfiguration DHCP, manual configuration SLAAC, DHCPv6, manual configuration
NAT Widely used No longer needs to rely on NAT to alleviate public address shortages
Header Variable length Fixed 40-byte basic header

Note:

IPv6 does not mean that NAT will never be used in networks. In actual deployments, translation mechanisms such as NAT66 and NAT64 still exist, but IPv6's address design no longer requires relying on NAT for large-scale public address sharing as IPv4 does.

5. Common IPv6 Address Types

Type Address Range or Example Purpose
Global unicast address 2000::/3 Globally routable communication
Link-local address fe80::/10 Communication on the same link
Unique local address fc00::/7 Similar to internal network addresses
Loopback address ::1 Local host communication
Unspecified address :: Address not yet specified
Multicast address ff00::/8 Multicast communication

IPv6 does not have the traditional broadcast addresses that IPv4 has; the related functions are usually implemented through multicast.