ip, subnet mask, gateway, default gateway

ip

IP address = network address + host address (also known as: network number and host number), we call hosts with the same network number hosts in the local network, and hosts with different network numbers as hosts in the remote network. The hosts in the local network can communicate with each other directly, and the hosts in the remote network must pass the forwarding data through the local gateway (Gateway) to communicate with each other.

IP addresses mainly include five types of addresses: A, B, C, D, and E:

  • Class A: It consists of a 1-byte network address and a 3-byte host address. The highest bit of the network address must be “0” (the network address with all 0s is reserved for use). The address range is 1.0.0.0 to 127.255.255.255, which is mainly allocated to large networks with a large number of hosts and a small number of LAN networks;
  • Class B: It consists of a 2-byte network address and a 2-byte host address, and the highest two bits of the network address must be “10”. The address range is 128.0.0.0 to 191.255.255.255, generally used for large international companies and government agencies;
  • Class C: It consists of a 3-byte network address and a 1-byte host address, and the highest bit of the network address must be “110”. The address range is 192.0.0.0 to 223.255.255.255, which is used for general small companies, campus network research institutions, etc.;
  • Type D: The first byte starts with “1110”. An address of all zeros (“0.0.0.0”) corresponds to the current host. The IP address of all “1”s (“255.255.255.255”) is the broadcast address of the current subnet. The address range is 224.0.0.0 to 239.255.255.255, also known as broadcast address, so any IP address whose first byte is greater than 223 and less than 240 is a multicast address.
  • Class E: Starting from “11110”, the address range is 240.0.0.0 to 255.255.255.255, temporarily reserved.

Internet address

Calculating the network address is to judge whether the computers in the network are in the same network, if they are in the same network, they can communicate, and if they are not, they cannot, and need the help of the local gateway.

Convert the IP address and subnet mask into binary form, and then the result of the bit-by-bit AND of the two is the network address.

IP decimal: 192.168.1.1
IP binary: 11000000 10101000 00000001 00000001

Subnet mask decimal: 255.255.255.0
Subnet mask binary: 11111111 11111111 11111111 00000000

Network Address = IP & Subnet Mask
Network address binary: 11000000 10101000 00000001 00000000
Network address decimal: 192.168.1.0

Host Address

The result of the inversion of IP and subnet mask is the host address.

IP decimal: 192.168.1.1
IP binary: 11000000 10101000 00000001 00000001

Subnet mask decimal: 255.255.255.0
Subnet mask binary: 11111111 11111111 11111111 00000000

host address = IP & amp; (~subnet mask)
Host address binary: 00000000 00000000 00000000 00000001
Host address decimal: 0.0.0.1

Subnet

In order to determine the network area, each interface of the host and the router is separated, resulting in several separate network islands, which are called subnets.

Subnet mask

Representation method of subnet mask
(1) Dotted decimal notation
Binary conversion to decimal, every 8 digits are separated by dots.

For example: subnet mask binary 11111111.11111111.11111111.00000000, expressed as 255.255.255.0

(2) CIDR slash notation
IP address/n

Example 1: 192.168.1.100/24, its subnet mask is represented as 255.255.255.0, and its binary representation is 11111111.11111111.11111111.00000000

Example 2: 172.16.198.12/20, the subnet mask is 255.255.240.0, and the binary representation is 11111111.11111111.11110000.00000000

It is not difficult to find that there are 24 1s in Example 1 and 20 1s in Example 2, so n comes from this way. Carriers and ISPs often use this method to assign IP addresses to customers.

Note: n is a number from 1 to 32, indicating the length of the network number in the subnet mask. The number of hosts in the subnet is determined by the number of n = 2(32-n)-2 (the reason for -2: When the host bits are all 0, it means the network address of this network, when the host bits are all 1, it means the broadcast address of this network, these are two special addresses).

Why use a subnet mask?

The subnet mask can separate the network address and the host address in the IP address. If two hosts want to communicate, they must first determine whether they are in the same network segment, that is, whether the network addresses are the same. If they are the same, the data packet can be sent directly to the target host, otherwise, the routing gateway is required to forward the data packet to the destination.

It can be understood in such a simple way: host A wants to communicate with host B, and the respective IP addresses of A and B are ANDed with the subnet mask of host A, and the result can be seen:

If the results are the same, it means that the two hosts are in the same network segment, so A can discover B’s MAC address through ARP broadcast, and B can also discover A’s MAC address to achieve normal communication.
If the results are different, the ARP broadcast will end at the local gateway. At this time, A will send the data packet to B to the local gateway first, and the gateway will query the routing table according to the IP address of the B host, and then continue to forward the data packet. Finally delivered to the destination B.

Classification of subnet masks

(1) Default subnet mask
Also called the default subnet mask, that is, if no subnet is divided, the bits of the corresponding network number are all set to 1, and the host number is all set to 0.

IP address without subnetting: network number + host number, then:

Class A network default subnet mask: 255.0.0.0, expressed as /8 in CIDR
Class B network default subnet mask: 255.255.0.0, expressed as /16 in CIDR
Class C network default subnet mask: 255.255.255.0, expressed as /24 in CIDR
(2) Custom subnet mask

After a network is divided into subnets, part of the original host number position is given to the subnet number, and the rest is given to the subnet’s host number. Its form is as follows:

IP address after subnetting: network number + subnet number + subnet host number, for example:

192.168.1.100/25, its subnet mask indicates: 255.255.255.128, which means that the highest bit of the host bit of the network segment 192.168.1.0 is divided into subnets.

Gateway

The gateway of the computer (Gateway) is the exit to other network segments, which is the IP address of the router interface. The IP address used by the router interface can be any address in this network segment, but usually the first or last available address of this network segment is used, which is to avoid as far as possible the host address in this network segment. conflict.

In the following topology diagram example, A and B, C and D can communicate with each other directly (both belong to the same network segment, without going through a router), but A and C, A and D, B and C, B and D They do not belong to the same network segment, so they communicate through the local gateway, and then the router searches the routing table for a direct route that matches the IP address of the other party according to the IP address of the other party, and forwards it through the gateway interface on the other side. Realize the interconnection.

Default Gateway

The default gateway address is obtained after the host bits are all set to 1 except the last bit.

Suppose an existing ip address is 193.6.7.0/24. According to the network prefix 24, the first 24 bits are network bits, and the last 8 bits are host bits. Therefore, the default gateway address of this network is the IP address 193.6.7.11111110 (the bold part is the network bit), converted into a decimal number is 193.6.7.254/24.

A host can have multiple gateways. The default gateway means that if a host cannot find an available gateway, it will send the data packet to the default designated gateway, and the gateway will process the data packet. The gateway currently used by the host generally refers to the default gateway. The default gateway of a computer cannot be specified casually, it must be specified correctly, otherwise a computer will send data packets to a host that is not a gateway, and thus cannot communicate with hosts on other networks. There are two ways to set the default gateway: manual setting and automatic setting.

(1) Manual setting
Manual setting is suitable for the situation where the number of computers is relatively small and the TCP/IP parameters are basically unchanged, for example, there are only a few to a dozen computers. Because this method needs to set the “default gateway” on each computer connected to the network, it is very laborious. Once the IP address of the default gateway must be modified due to migration or other reasons, it will bring a lot of trouble to the network management. Recommended Use.

In Windows 9x, the way to set the default gateway is to right-click on “My Network Places”, click “Properties” in the pop-up menu, select “TCP/IP Protocol” in the network properties dialog box, click “Properties”, and click “Properties”. Just fill in the IP address of the new default gateway in the “Default Gateway” tab.

Special attention should be paid to: the default gateway must be the IP address in the network segment where the computer itself is located, and the IP address in other network segments cannot be filled in.

(2) Automatic settings

Automatic setting is to use DHCP (Dynamic Host Configuration Protocol, Dynamic Host Configuration Protocol) server to automatically assign IP addresses, subnet masks and default gateways to computers in the network. The advantage of this is that once the default gateway of the network changes, as long as the setting of the default gateway in the DHCP server is changed, all computers in the network will obtain the IP address of the new default gateway. This method is suitable for networks with large network scale and possible changes of TCP/IP parameters. Another way to automatically obtain the gateway is to install the client program of the proxy server software (such as MS Proxy) to obtain the gateway automatically. Its principle and method are similar to DHCP. Due to the limited space, it will not be described in detail.