Introduces the concept of IP protocol and the content of IP header, and related knowledge, ICMP, ARP, port, DNS.
We introduced it briefly beforeTCP and UDP and TCP three-way handshake and four-way waveThen we simply learn IP protocol and related knowledge points, the following content is very simple.
@[toc] The Internet Protocol (IP) Protocol is located at the network layer. The main function is to provide AN IP address, encapsulate the source IP address and destination IP address into the IP header of the packet, and indicate the direction and route of a data transmission for the router. However, it only indicates the addresses of the sender and receiver, and does not maintain any status information about the subsequent datagram, which does not guarantee the arrival of the packet. And whether the data will be discarded and what to do with it afterwards.
Therefore, IP protocol is also a connectionless, unreliable, best-effort packet transfer service. In this case, the upper-layer TCP protocol is needed to ensure reliability.
1 IP and MAC
To ensure that it is actually delivered, various conditions need to be met. Two important conditions are the IP Address and the MAC Address (Media Access Control Address).
1.1 IP
The IP address indicates the address to which the node is assigned. It is the address used at the network layer and above.
IP addresses are classified into public (wan) IP and private (LAN) IP. Public IP is assigned by the service provider. Public IP is unique in the world, while private IP is unique in the same network. Different networks may not be unique and may change.
An IP address is a 32-bit binary number, usually divided into four “8-bit binary numbers” (that is, four bytes). An IP address is usually expressed in dotted decimal notation (A.B.C.D), where a,b,c, and d are all decimal integers ranging from 0 to 255. Example: the dotted decimal IP address (100.4.5.6), is actually a 32 bit binary number (01100100.00000100.00000101.00000110).
Computers can only recognize binary data, so our IP address should be a binary data. However, the IP address we configured is not binary. Why? Such as:
Decimal IP: 192.168.1.100 Converted to binary: 11000000 10101000 00000001 01100100
In fact, it does convert the decimal address to binary data, but if it is displayed using: 11000000 10101000 00000001 01100100. After the establishment of a connection with others to configure such AN IP address, memory is more troublesome.
So, to make it easier for us to represent and remember an IP address, we decimalized every byte of the IP address and used. To indicate: “dotted decimal”. This is why IP addresses are shown in decimal notation.
In Windows, you can run the ipconfig command to view the local IP address.
1.2 MAC
A MAC address is a fixed physical address of a NIC and an address used by the data link layer and physical layer.
IP addresses can be paired with MAC addresses. A MAC address is uniquely assigned by the network device manufacturer to uniquely identify a network adapter on a network. If a device has one or more network adapters, each network adapter must have a unique MAC address.
The MAC address contains 48 bits (6 bytes) and is usually represented as 12 hexadecimal digits, for example: 00-16-EA-AE-3C-40 is a MAC address in which the first three bytes, a hexadecimal number 00-16-EA, represent the number of the network hardware manufacturer, which is assigned by IEEE, and the last three bytes, The hexadecimal number AE-3C-40 represents the series number of a network product (such as a network card) manufactured by the manufacturer. As long as you do not change your MAC address, the MAC address is unique in the world.
2 the IP header
The first length of an ordinary IP address is 20 bytes and the maximum length is 60 bytes. Unless an option field is included, the structure is as follows:
- Four-bit Version: specifies the Version number of the IP protocol. The IPV4 version is still used to identify the IP protocol version. IPV4 is 0100 and IPv6 is 0110, which is the binary 4 and 6.
- Internet Header Length: Indicates the Length of the Header. The unit is 4 bytes (32-bit). Therefore, the maximum Length of the Header is 60 bytes, but the fixed Length of 20 bytes is usually used.
- 8-bit Type Of Service (TOS) : Defines the Service Type required during IP packet transmission. Includes a 3bit priority subfield, a 4bit TOS subfield, and a 1bit that is not used but must be set to 0. TOS of 4 bits represent minimum delay, maximum throughput, highest reliability, and minimum cost respectively. Only one of the 4 bits can be set to 1 at the same time. If all 4 bits are 0, that means it’s a generic service.
- 16-bit Total Length: indicates the Total Length of an IP datagram, in bytes. Using the header length field and total length field, you can know the starting position and length of the data content in the IP datagram. Since this field is 16 bits long, the longest IP packet can reach 2^ 16-1 = 65535 bytes. When the packet is fragmented, the value of this field also changes.
- 16-bit Identification: The unique code used to identify IP datagrams. If an excessively long IP packet needs to be broken up and sent in fragments due to the data link layer frame data segment length limit (MTU, which supports a maximum transmission unit of 1500 bytes), the IP packet identifier of each fragment must be consistent. This identifier is used when a shard is reassembled.
- Three-bit Flag: Currently, there are only two types of flags. That is, only the last two bits are meaningful. The lowest value is More Fragment (MF). The value 1 indicates that there are packets with fragments. The value MF=0 indicates that the current packet is the last packet. If DF=1, the packet cannot be fragmented. If DF=0, the router disdevices the packet. If DF=0, the packet can be fragmented.
- 13-bit Fragment Offset: indicates the relative position of a Fragment in the original IP packet. Through this offset and 16-bit identifier, multiple fragmented packets are restored to the original IP packets.
- 8-bit Time To Live (TTL) : indicates the maximum number of routers that a packet can pass through. When a packet passes through a router in transmission, the TTL decreases by one. If the TTL is 0, the packet is discarded. Generally, the packet does not pass 255 unless there is a cyclic route.
- 8-bit Protocol: indicates the upper-layer transport Protocol type. Generally, 1 indicates ICMP, 6 indicates TCP, and 17 indicates UDP.
- Header Checksum: The 16-bit Header Checksum is used to verify data integrity and ensure that packets (fragments) are received correctly. It is the same as the Checksum used in UDP and TCP packets. First, the 16-bit value is filled in through the verification algorithm. When the target machine receives the value, it performs the verification algorithm again and compares it. If the value is the same, it means there is no problem.
- 16-bit Source IP address and 16 are the destination IP address.
2.1 Related Issues
- Why is IP not the protocol for reliable connections?
- The IP layer has no connection handshake protocol and no timeout or retransmission mechanism. IP protocol only indicates the direction and route of a data transmission for the router, but it only indicates the addresses of the sender and receiver, and does not maintain any information about the status of subsequent datagram. It does not guarantee the arrival of the data packet, whether the data will be discarded, and how to deal with it after discarding.
- What changes have been made to THE IP datagram through the router?
- Each time an IP packet passes through a router, the TTL is reduced by one. If the TTL is 0 but the IP packet still does not reach the destination, the IP packet is discarded.
- This is done to prevent the occurrence of a loop. And the last hop router sends an ICMP unreachable packet to the source IP address.
- IP packet fragmentation
- The length of an IP packet is 16, that is, a maximum of 65535 bytes. According to the link layer, the maximum Transmission unit (MTU) is 1500 bytes.
- When the size of an IP packet exceeds 1500, the IP packet must be fragmented. This is just the initial sending the shard, if a router in a network of MTU is less than 1500, you will need to do in the router port fragmentation, this gives complete receiving IP packet according to a great uncertainty, we know that the packet arrives at the finish line is not necessarily in accordance with the order), because one thousand lost one shard, may cause IP retransmission, Of course, the prerequisite is that the high-level protocol supports retransmission (for example, TCP supports retransmission, but UDP does not).
- For TCP, there is no fragmentation problem because the Maximum Segment Size (MSS) field in the TCP header specifies the Maximum number of bytes that can be transmitted in a TCP packet. Generally, the value is 1500-20-20=1460 bytes. This means that the maximum number of bytes of data that can be carried by an IP packet is 1460, which avoids fragmentation. However, for protocols such as UDP, the sharding operation is still handed over to IP. Although this can increase the efficiency of the payload, the stability will be greatly affected.
3 the ICMP protocol
The Internet Control Message Protocol (ICMP) is an affiliate Protocol of the IP Protocol and belongs to the network layer. It is used to transmit control messages directly between two IP hosts and routers. Control messages refer to the messages about the network itself, such as whether the network is accessible, whether the host is reachable, and whether the route is available. Although these control messages do not transmit user data, they play an important role in the transmission of user data because they verify that the transmission conditions are met before the user transmits the actual message!
The ping command is used to test the connectivity between the host and the specified IP address at the network layer. In fact, the ICMP protocol is used.
4 the ARP protocol
throughTCP/IP protocol layer 7 model and data encapsulation and partitionWe know that the network layer depends on the link layer. The IP datagram must be encapsulated as a frame, and the head of the data frame must be encapsulated with a destination MAC address, so that the data will be sent to the host with the specified MAC address. The link layer does not know IP address, it only knows MAC address. Therefore, the communication between two devices with IP depends on the underlying MAC address. Therefore, information transmission cannot be realized only by knowing each other’s IP address. The communication between IP addresses depends on the underlying MAC address.
In this case, the AddressResolution Protocol (ARP) is used. ARP can find the MAC address of the target device according to the IP address of the target device. ARP is an important Protocol for connecting the network layer and the link layer. Generally, ARP caches the mapping between IP and MAC addresses on the same LAN. However, if the communication parties are not of the same LAN class, the MAC address cannot be directly found through the IP address of the other party. In this case, ARP looks for and returns the MAC address of the gateway (usually the router), and the data is actually forwarded to the gateway. Later, the data will be forwarded to other gateways again through ARP. If the data cannot be found, the MAC address of the corresponding IP address will be found and the data will be transferred to the specified destination!
Here are some examples of different lAns:
A – (B1, B2) – (C1 and C2) — – E
B1 and B2 are two interfaces on the gateway (router) B, C1 and C2 is the two interfaces on the router C, A and E are PC, from host A to host E to send packets, so the destination IP packet is formed in the host A E IP, source IP is the IP address of the host A, B1 the MAC address of the destination MAC address is, The source MAC address is the MAC address of A.
Router B checks that the MAC address of the message is its own, but the destination IP address is not its own. In this case, the real purpose of the message is not its own, but it needs to be forwarded by itself. In this case, after the reencapsulation of B, the source IP address and destination IP address are unchanged, and the source MAC address is changed to the MAC address of B2. After receiving the packet, router C performs the same operation as router B. The source AND destination IP addresses remain the same. The source MAC address changes to THE MAC address of C2, the destination MAC address changes to the MAC address of host E, and sends the packet to host E. Host E checks the IP address and finds that it is the target. Then, host E can parse the data. When sending A reply, host E takes the source IP address (IP address of host A) and source MAC address (MAC address of interface C2) of the received data packet as its destination IP address and MAC address.
The actual ARP query is more complicated!
5 port Indicates the port number
There are physical ports, such as nic ports, and there are logical ports, by which we mean logical ports.
If we only know the MAC and Ip address, then we can only guarantee to transfer the data to the specified host, but there are many applications in the host, how to know which application the data belongs to? This requires ports to support, data to be sent to the other host specified application, only to identify these applications, so the network applications are identified with numbers, these numbers are called port numbers, used to represent an application in the host. Simply put, the port number uniquely identifies an application in a computer, so the port number is unique in a host and cannot conflict. If we directly start two Tomcat without changing the port number, we will report port conflict exception!
Default port number for common applications:
tomcat | 8080 |
Mysql | 3306 |
Oracle | 1521 |
SMTP (Simple Mail Transfer Protocol) | 25 |
POP3 (Post Office Protocol) | 110 |
FTP (File Transfer Protocol) | 21 |
HTTP (Hypertext Transfer Protocol) | 80 |
The port number ranges from 0 to 65535, 65536 digits in total. Do not use ports ranging from 0 to 1024. These ports are reserved for system processes. If these port numbers are used, port conflicts may occur and some functions may become unavailable.
6 DNS service
The Domain Name System (DNS) service is a protocol at the application layer like HTTP. It provides the resolution service between domain names and IP addresses.
Computers can be assigned both IP addresses, host names, and domain names. Users usually use host names or domain names to access each other’s computers, rather than IP addresses. Because a combination of letters and numbers to specify a computer name is more consistent with human memory than a set of pure numbers for an IP address. Without it, if you want to go to Baidu, you need to remember the IP of Baidu, and 163, you need to remember the IP of 163. With DNS processing, you only need to remember the domain name of the corresponding website — that is, the URL can be.
But computers aren’t so good at dealing with names, they’re good at dealing with long lists of numbers. In order to solve the above problems, DNS service came into being. The DNS protocol provides the service of looking up an IP address by a domain name or reversely looking up a domain name from an IP address.
To set up the DNS directory resolution for our Windows computer, you can find the C:\ Windows \system32\drivers\etc\hosts file and add the following:
127.0.0.1 localhost 127.0.0.1 www.baidu.com 127.0.0.1 news.baidu.com
Can be realized, domain name and IP address mapping!
7 Other Knowledge
7.1 Subnet Mask
Subnet maskIs a 32-bit address that masks part of the IP address to distinguish the network id from the host ID and to indicate whether the IP address is on a LAN or a remote network.
Subnet mask – The “all 1” bit mode that masks the network number part of an IP address. 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.
7.2 Default Gateway
A gateway is essentially the IP address of one network to another network. To communicate between the two networks, you must pass through the gateway. The gateway in TCP/IP protocol is the most commonly used.
Default gateway means that if a host cannot find an available gateway, it sends packets to the default gateway, which processes the packets. Now the gateway used by the host is generally referred to as the default gateway. The default gateway is the device connecting the subnet to the external network, usually a router.
The default gateway for a computer should not be specified casually, it must be specified correctly, otherwise a computer will send packets to a computer that is not the gateway and cannot communicate with computers on other networks.
The default gateway can be set manually or automatically.
If you need to communicate, or the article is wrong, please leave a message directly. Also hope to like, collect, follow, I will continue to update a variety of Java learning blog!