preface

I recently watched the movie “Wreck-it Ralph 2: The Internet” and found some of the animations interesting. I was reminded of the article I read recently about HTTP illustrated and TCP/IP. Well, it’s time to show the real technology.

If you’re still confused about who each protocol is and what it does, it’s worth taking a closer look at TCP/IP.

This article directory

  • TCP/IP protocol family
  • Application Layer
  • Description: SEQ and ACK, serial number and acknowledgment number, SYN synchronization serial number)
  • Network Layer(address and routing knowledge)
  • How to view MAC address
  • The protocols of TCP and UDP that run in the transport layer

1. TCP/IPProtocol family

Internet Protocol Suite (IPS) is a network communication model and an entire family of network transport protocols that form the basic communications architecture of the Internet. It is commonly referred to as the TCP/IP Protocol Suite, or TCP/IP Protocols, or TCP/IP for short. Because the two core protocols of the protocol family, TCP (Transmission Control Protocol) and IP (Internet Protocol), are the earliest standards in the family.

Knock at key points:

  • TCP (Transmission Control Protocol) and IP (Internet Protocol)Are the first two core protocols defined, so they are collectively calledTCP/IP protocol family

1.1 TCP/IPDealer layered

A very important point in the TCP/IP protocol family is layered management, which consists of the following four layers: application layer, transport layer, network layer and data link layer.

TCP/IP

And hierarchical management, the design is relatively simple, in the application layer of the application only need to consider the assignment of their own tasks and do not need to consider the other party’s transmission line is what, whether to ensure transmission service.

2. Application layerApplication Layer

The application layer is the layer used by most common network-related programs to communicate with other programs over the network. The processing of this layer is application-specific; Data is transmitted from network-related programs in the format used within the application and is then encoded into a standard protocol format.

The application layer determines the communication activities of the application services provided to users:

  • HTTP(World Wide Web Services)
  • FTP(File transfer)
  • SMTP(E-mail)
  • SSH(Secure remote login)
  • DNS(Name <-> IP address search, DNS)
  • And many other agreements

Once the data from the application is encoded into a standard application layer protocol, it is passed to the next layer of the IP stack.

3. Transport layer,Transport Layer

The transport layer is located below the application layer and provides data transmission between two computers located in the network connection. There are two different protocols in the transport layer

Important point: Each application layer protocol typically uses one of two transport layer protocols

  • TCP: connection orientedTransmisson Control ProtocolTransmission control protocol
  • UDP: Connectionless packet transmissionUser DataProtocolUser datagram Protocol
UDP TCP
Whether connection There is no connection connection-oriented
reliable Unreliable transmission, no use of flow control and congestion control Reliable transmission, using flow control and congestion control
Number of connected objects Support one-to-one, one-to-many, many-to-one and many-to-many interaction communication Only one to one communication
transport For a message Byte oriented stream
The first overhead Header overhead is small, only 8 bytes The header has a minimum of 20 bytes and a maximum of 60 bytes
scenario Suitable for real-time applications (IP phone, video conference, live broadcast, etc.) Suitable for applications that require reliable transfers, such as file transfers

From: COMPARISON of TCP and UDP

3.1 Meaning of the Transport layer

The capabilities of the network layer allow us to send packets from one machine to another machine on the network, but this is not sufficient to write a network application because:

  • The machine can run multiple applications, and we need to know which application should receive the packets.
  • The network layer can discard or reorder packets. On the other hand, applications usually need to guarantee (that is, lossless) and transfer bytes sequentially.

3.2 What is a “quadruple”?

TCP solves the first problem by defining port numbers:

Port numbers are essentially identifiers that help TCP distinguish between applications running on the machine.

In other words, each port number on a computer is owned by the application on that computer.

The port number is a 2-byte integer. Port 0 is unavailable. As a result, we can have as many as 65,536 ports on one machine.

TCP defines connections by port number.

TCP connections are identified by source and destination IP addresses (from the network layer) and source and destination port numbers. This is also called a quad:

// Source IP address, destination IP address, source port, destination port (SRC IP, DST IP, SRC port, DST port)Copy the code

3.3 SEQandACK, serial number and confirmation number

In TCP networks, to ensure guaranteed and ordered byte delivery for each connection, Sequence numbers and Acknowledgment numbers are used, i.e., Seq and Ack.

The unit of each TCP send and receive is TCP header + data, TCP Segment.

The size of each data segment varies from hundreds to tens of thousands.

SEQ sequence number indicating the offset of bytes in each transmission ACK acknowledgment number indicating the next SEQ expected to receive

Here’s an example:

  1. Serial number for# 2000And the length of100On this connection containing the first2000-2099.Bytes.
  2. Include the first when the receiver receives2099Byte and all other bytes, it sends an acknowledgement# 2100.
  3. That means it’s already at the2100Byte before the byte was received.

3.4 SYN, synchronize serial number

  • To avoid confusion with the data segment of the previous line, the sequence number does not start at 0 when the subline is established.
  • Both ends will useISNGenerator to generate the respective initial serial number (Initial Sequence Number, ISN), usually they are not equal.
  • When a line is established, it is passed through the Control BitsSYNLet the ends of theTCPMust be carried outISNExchange (synchronization).

All right, speak human language. Is the TCP three-way handshake:

This is how TCP connections are set up, and 2 and 3 can be combined into a single message.

Here it is:

In the third handshake (Client – > Server), the SEQ of the third handshake is the first value + 1 (ISN + 1).

4. Network layer,Network Layer

The network layer processes the data packets that flow across the network (packet: the smallest unit of data transmitted over the network).

The network layer specifies the path (transmission line) among many options to the other party’s computer to transmit packets to the other party.

The most prominent protocol in this layer is Internet Protocol (IP), so this layer is also known as the IP layer. At the heart of IP are two main functions: address and routing.

IP
IPV4
IPV6

  • IPv4Specified in theIPThe address length is 32, that is, 2^32-1 nodes (4 billion).
  • We already have more than 4 billion nodes in our network, and this has led to the development of IPV6.
  • IPv6In theIPThe length of the address is 128, that is, 2^128-1 nodes (212.5 billion).
  • ifIPV6When widely used, every grain of sand in the world will have an IP address.

4.1 the address

Today, most machines have both IPv4 and IPv6 addresses. If you run ifconfig, you can see the IPv4 and IPv6 addresses of the computer.

~ ifconfig lo0: Flags = 8049 < UP, LOOPBACK, RUNNING, MULTICAST > mtu 16384 options = 1203 < RXCSUM TXCSUM, TXSTATUS, SW_TIMESTAMP > inet 127.0.0.1 Netmask 0xff000000 Inet6 ::1 Prefixlen 128 Inet6 fe80::1809: 1%lo0 prefixlen 64 scopeid 0x1 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: activeCopy the code

4.2 the routing

IP routing is based on rules that use address prefixes. If you run netstat -rn on your computer, you can see the routing table on your computer.

  • For example, my routing table says any match10.31.10/24IP packets should be sent toLink# 8.
  • If you look closely, you can see the “default” line. This line is a special rule that indicates that any packet that does not match another rule should be addressed using this rule.

It is like default in the switch/case statement.

% netstat -rn
Routing tables
Internet:
Destination        Gateway            Flags        Refs      Use   Netif 
default            10.31.10.222       UGSc           54        0     en0
default            link#17 UCSI 0 0 bridge1 !
10.31.10/24        link#8 UCS 9 0 en8 !
Internet6:
Destination   Gateway       Flags         Netif Expire
fe80::%lo0/64 fe80::1%lo0   UcI           lo0
Copy the code

All nodes on the Internet have these routing tables, and that’s how IP packets are routed to their destination.

If you want to know how to route packets to the nugget juejin.im on your network, run the following command:

traceroute juejin.im
Copy the code

You get the following:

5. Link layer,Link Layer

(also known as data Link layer, network interface layer)

It is used to connect the hardware part of the network. The range on the hardware is in the link layer, including

  • The operating system
  • Hardware device driver
  • NIC (Network Interface Card)
  • Physically visible parts such as optical fibers

5.1 ifconfig: seeMACaddress

In any network, each node has “neighbors.” Link-layer protocols provide the capabilities needed to communicate between “neighbors” directly connected over a link (for example, physical links like CAT5 cables, or radio links in WiFi).

The best known link layer protocol is Ethernet. In Ethernet, each interface has a unique 48-bit (6-byte) address called a Media Access Control (MAC) address.

If you run ifconfig on your computer, you’ll see the name of the network interface and its MAC address.

~ ifconfig ... en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether 88:e9:fe:4c:83:5b inet6 Fe80 ::1809: d41A :a9a:d664% EN0 prefixlen 64 secured scopeid 0x8 inet 192.168.1.8 netmask 0xffFFff00 broadcast 192.168.1.255 ND6 Options =201<PERFORMNUD,DAD> Media: autoselect Status: activeCopy the code

As you can see, each byte in a MAC address is represented by a hexadecimal value, separated by colons.

Network packets sent over Ethernet links have source and destination MAC addresses. To discover its neighbors, Ethernet uses broadcast queries and notifications. Using these broadcast mechanisms, another protocol called ARP can find the mapping between the MAC and IP addresses of neighbors. If YOU run ARP on your computer, you can see this mapping.

~ arp -a-n ? (10.31.xx.xx) at 98:28:xx:2a:cc:xx on en8 ifscope [Ethernet]? (10.31.xx.xx) at f4:8E :xx38: F5 :b5:xx on en8 ifscope [Ethernet]? (10.31.xx.xx) at 54: EE :xx: E1 :33:xx on en8 ifscope [Ethernet]....Copy the code

Now we know the mapping between MAC and IP addresses.

6. TCP/IP communication transmission flow

Then they wrap it up and parse it.

  • The sender will print the header information of the layer after each layer.
  • The receiving end will parse the corresponding header information after each layer.

7. Extensions: Run in the transport layerTCPUDPThe agreement

Each application layer (the highest level of the TCP/IP reference model) protocol typically uses one of two transport layer protocols:

Protocols that run on TOP of TCP:

  • Hypertext Transfer Protocol (HTTP), mainly for general browsing.
  • HTTPS (HTTP over SSL, Secure Hypertext Transfer Protocol).HTTPSecurity version of the protocol.
  • File Transfer Protocol (FTP)For file transfer.
  • POP3 (Post Office Protocol, Version 3), to receive mail.
  • Simple Mail Transfer Protocol (SMTP), used to send E-mail.
  • TELNET (Teletype over the Network), through aTerminal (terminal)Log on to the network.
  • Secure Shell (SSH) is used to replace TELNET., used for encrypted and secure login.

Protocols that run on UDP:

  • Boot Protocol (BOOTP)For non-disk devices.
  • Network Time Protocol (NTP)For network synchronization.
  • Dynamic Host Configuration Protocol (DHCP), dynamically configure IP addresses.

Run over TCP and UDP:

  • Domain Name Service (DNS), for address search, mail forwarding and other work.

disclaimer

Visit foreign community to see this article, think quite concise and clear.

Just for fun on the simple summary, there is a wrong place to blame.

It means it’s a little rough, don’t spray me…

❤️ see three things

If you found this post inspiring, I’d like to invite you to do three small favors for me:

  1. Like, let more people can also see this content (collection does not like, is playing rogue – -)
  2. Pay attention to the public number “front-end dispeller”, irregularly share original knowledge.
  3. Check out other articles as well
  • Design Patterns you Inadvertently Use (part 1) – Creation patterns
  • “King of data visualization library” D3.js fast start to Vue application
  • “True ® Path to Full Stack” a back-end guide to Web front-end development
  • “Vue Practice” 5 minutes for a Vue CLI plug-in
  • “Vue practices” arm your front-end projects
  • “Advanced front end interview” JavaScript handwriting unbeatable secrets
  • “Learn from source code” answers to Vue questions that interviewers don’t know
  • “Learn from the source code” Vue source code in JS SAO operation
  • “Vue Practice” project to upgrade vue-CLI3 correct posture
  • Why do you never understand JavaScript scope chains?