What is a computer network

A computer network (network for short) consists of a number of nodes and the links that connect them. The node can be a computer, hub, switch, or router.

The hosts are connected via switches, and routers are responsible for connecting multiple networks

Network classification

  • LAN can realize file management, application software sharing, printer sharing and other functions. It is a computer network that can connect computers in limited areas such as homes, schools, laboratories, university campuses or office buildings. Ethernet and Wi-Fi(wireless network connection) are two of the most common technologies used in local area networks today.
  • Wan, also known as wide area network, external network, public network. A remote network that connects computers in different local area networks or metropolitan area networks to communicate. In an area beyond the distance connected by hubs, connections must be made through routers. This type of network is called a wide area network. The transmission medium of wan is mainly telephone lines or optical fibers, which are connected by ISPs between enterprises. These lines are buried under the road by ISPs beforehand
  • Wireless local area network (WLAN) uses radio frequency (RF) technology, using electromagnetic waves, replacing the old fashioned twisted-pair copper wire constituted by the LOCAL area network, wireless network card, wireless router

In the middle of equipment

switches

Switches work at the data link layer. When a port is successfully connected, the CPU on the switch maps the MAC address to the port to form a MAC address table. In future communications, packets destined for this MAC address will be sent only to the corresponding port, not to all ports. Therefore, switches can be used to divide data link layer broadcasts, namely conflict domains; However, it cannot divide network layer broadcast, that is, broadcast domain.

Briefly summarize the basic functions of the switch:

1. Like hubs, switches provide a large number of ports for cable connections that can be routed in a star topology.

2. Like Repeaters, hubs, and Bridges, when it forwards frames, the switch reproduces a truth-free square electrical signal.

3. Like Bridges, switches use the same forwarding or filtering logic on each port.

4. Like Bridges, switches divide lans into multiple conflict domains, each of which has its own broadband, thus greatly increasing the bandwidth of the LAN.

5. In addition to the functions of Bridges, hubs, and Repeaters, switches offer more advanced features such as virtual local area networks (VLANS) and higher performance.

Older switches only supported two layers (to the MAC data link layer), so routers were required to resolve the IP. Many switches now support three layers (to the IP layer).

The router

A router, usually located at the network layer of the OSI model, is a hardware device that connects two or more networks and acts as a gateway between networks. It understands different protocols, such as Ethernet for a local area network or TCP/IP for the Internet. It can analyze the destination addresses of packets from different types of networks and convert non-TCP /IP network addresses into TCP/IP addresses, or vice versa. Then according to the selected routing algorithm, each packet is transmitted to the specified location according to the best route. So routers can connect non-TCP/IP networks to the Internet.

The principle of

Devices on a network communicate with each other mainly by using their IP addresses, and routers can only forward data based on specific IP addresses. The communication between computers can only be carried out between IP addresses that have the same network address. If you want to communicate with computers on other network segments, the communication must be forwarded through a router. IP addresses of different network addresses cannot communicate directly, even if they are very close to each other. Multiple ports on a router can connect to multiple network segments. The IP address of each port must be the same as that of the connected network segment. Different ports have different network addresses and corresponding network segments. In this way, hosts in each network segment can send data to the router through their OWN IP addresses.

Gateway

Also known as the protocol converter, it realizes network interconnection above the network layer

A gateway is essentially an IP address from a network to other networks (the IP address of a gateway is the IP address of a device with routing function).

address

MAC addresses are required for transmission between devices.

Hardware address

Also called physical address or MAC address, the MAC address is used by the data link layer and the physical layer. This is actually the adapter address or adapter identifier EUI-48. When the adapter is inserted into a computer, the identifier on the adapter becomes the MAC address of the machine.

If a host or router connected to a LAN has multiple adapters installed, such a host or router will have multiple MAC addresses. When a router connects to a LAN through an adapter, the hardware address of the adapter is used to identify an interface on the router. A router needs two adapters and two hardware addresses if it wants to connect to two networks.

The IP address

An IP address is a 32-bit identifier that is unique worldwide to each interface on each host (or router). An IP address is a logical address used by the network layer and other layers.

A/B/C address consists of two fixed fields. The first field is the net-ID, which identifies the network to which the host (or router) is connected. A net-ID is unique across the entire Internet. The second field is host-ID, which identifies the host (router). A host number must be unique within the network range indicated by the network number preceding it. Thus, an IP address is unique across the entire Internet. These three types of addresses are unicast addresses and are the most commonly used.

subnet

IP address = Network ID + Subnet ID + Host ID

A subnet mask is an important attribute of a network or subnet. When a router exchanges information, it must tell its neighbor routers the subnet mask of its own network

The default subnet mask for class A addresses 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

The common IP address writing mode, for example, 192.168.100.1/24, is actually a subnet mask

  • On the Abstract Internet of IP layer, only IP datagrams can be seen. Although many routers forward them in the middle, the source address and destination address of the header remain unchanged.
  • Although the IP datagram header contains the source IP address, the router only selects routes based on the network number of the destination IP address
  • At the link layer of the LAN, only MAC frames are visible. IP datagrams are encapsulated in MAC frames. The source and destination addresses at the head of the MAC frame change when transmitted over different networks: After arriving at the new router, the original source and destination IP addresses are discarded. During forwarding, the network interface software uses ARP to translate the IP address of the next-hop router into a MAC address, which is encapsulated in the data link layer header.

Packet transmission process

What is the process that the network model of the computer goes through when the data is transferred between the protocol layers

  • The originator application layer sends a message, M, called message
  • The application layer sends a message to the transport layer and adds a header to the transport layer to generate a segment
  • Pass the data segment to the network layer and add a network layer header to generate a datagram
  • Transmit the datagram to the data link layer and add the head and tail of the data link layer to generate frame: data frame
  • Transmit each bit of this packet through the physical layer to the next device closely connected to it, namely the switch.
  • Send to the corresponding router through the switch
  • The data link layer of the router receives the data frame first, removes the header of the data link layer, and transmits the data frame to the network layer
  • The network layer of the router analyzes the header of the network layer to know which egress on the router, puts the corresponding datagram on the corresponding egress, and then packages the data link layer at the egress to transmit the newly generated data frame to the next station
  • The data link layer of the receiver removes the header from the data link layer and sends the header to the network layer
  • The network layer removes the header from the network layer and sends it to the transport layer
  • Transport layer Removes the header from the transport layer and sends it to the application layer
  • The application layer can get the original information M

The head of each layer

Application layer – HTTP

Support the operation of network application protocols and programs, similar to wechat/cloud services

Common protocols HTTP(Hypertext Transfer Protocol), FTP (file transfer protocol), SNMP,SMTP (mail),DNS, DHCP

DHCP dynamic host configuration protocol, using UDP protocol to work, automatically assign IP addresses to internal networks. The DHCP server broadcasts once. If the DHCP server broadcasts no response for four times, the DHCP fails

The HTTP protocol mainly consists of three parts:

  • Start line: Describes basic information about a request or response.
  • Header fields: describes packets in more detail in key-value format.
  • Entity Message: Indicates the actual data to be transmitted. It may be binary data, such as pictures and videos, rather than plain text.

The start line and Header fields become the request Header or response Header, collectively called Header. The body of the message is also called the entity, called the body. According to the HTTP protocol, each sent packet must have a Header, but the body information can be omitted. That is, the Header information is required, but the entity information can be omitted. And there has to be a blank line between header and body (CRLF)

Similar to the

GET /home. HTML HTTP/1.1 Host: developer.mozilla.org User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; The rv: 50.0) Gecko / 20100101 Firefox 50.0 / Accept: text/HTML, application/XHTML + XML, application/XML. Q = 0.9 * / *; Q = 0.8 Accept - Language: en - US, en. Q = 0.5 Accept - Encoding: gzip, deflate, br Referer: https://developer.mozilla.org/testpage.html Connection: keep-alive Upgrade-Insecure-Requests: 1 If-Modified-Since: Mon, 18 Jul 2016 02:36:04 GMT If-None-Match: "c561c68d0ba92bbeb8b0fff2a9199f722e3a621a" Cache-Control: max-age=0Copy the code

The header is divided into request header and response header, and part of the header is common

General field role
Cache-Control Controls the behavior of caching
Connection The type of connection the browser wants to use preferentially, for examplekeep-alive
Date Time of packet Creation
Pragma Packet instructions
Via Proxy server information
Transfer-Encoding Transmission coding mode
Upgrade The client is required to upgrade the protocol
Warning There may be errors in the content

The request first

The request first role
Accept The type of media that can be received correctly
Accept-Charset A character set that can be correctly received
Accept-Encoding A list of encoding formats that can be correctly received
Accept-Language List of languages that can be received correctly
Expect Expect the specified behavior of the server
From Email address of the requester
Host Server domain name
If-Match Resource tag comparison at both ends
If-Modified-Since Local resource unmodified returns 304 (comparison time)
If-None-Match Local resource unmodified returns 304 (comparison mark)
User-Agent Client Information
Max-Forwards Limit the number of times that can be forwarded by proxy and gateway
Proxy-Authorization Sends authentication information to the proxy server
Range Request a portion of something
Referer Represents the previous page visited by the browser
TE Transmission coding mode

In response to the first

In response to the first role
Accept-Ranges Whether certain kinds of scopes are supported
Age The amount of time a resource exists in the proxy cache
ETag Resource identifier
Location The client redirects to a URL
Proxy-Authenticate Sends authentication information to the proxy server
Server Server name
WWW-Authenticate Obtain authentication information required for resources

The transport layer – TCP

Manage the protocols for subcontracting data transmission between the sender and the receiver, and detect errors in received packets

The network layer provides logical communication between hosts, while the transport layer provides end-to-end logical communication between application processes

The TCP transmission unit is a packet segment, which consists of the header and data

Mapping between application layer protocols and transport layer protocols:To make computers with different operating systemsThe application processCan communicate with each other, transport layer useProtocol port number, referred to asportAlthough the destination of communication is the application process, the final delivery of the packet is completed by TCP or UDP once the packet is delivered to the destination port of the destination host.

Common port

  • Port 21: FTP file transfer service
  • 22 Port: SSH remote connection service
  • 23 Port: TELNET terminal emulation service
  • 25 Port: SMTP simple mail transfer service
  • 53 Port: DNS domain name resolution service
  • Port 80: HTTP hypertext transfer service
  • Port 443: Hypertext transfer service with HTTPS encryption
  • Port 3306: port of the MYSQL database
  • Port 5432: indicates the PostgreSQL database port
  • Port 6379: Redis database port
  • Port 8080: indicates the default port of the TCP server
  • Port 8888: port of the Nginx server
  • Port 27017: default port of the mongoDB database

The first 20 bytes of the TCP packet header are fixed, and the following 4n bytes are optional.

  • Source port: random port of the local IP address; destination port: port next to the IP address
  • Sequence number Sequence number (4 bytes). This Sequence number ensures that the TCP packets are in order. The peer end can splice the packets in Sequence
  • Acknowledgement Number (4 bytes). This serial Number indicates the Number of the next byte that the data receiver expects to receive, and also indicates that the data of the previous serial Number has been received
  • Data migration
  • Reserved (6 bits) : For future use, should be set to 0 at present
  • identifier
    • URG=1 (critical) : 1 indicates that the data part of the datagram contains emergency information and is a high-priority data packet. The emergency data must be first in the data section of the current packet
    • ACK=1 (acknowledgement) : 1 indicates that the acknowledgement number field is valid and 0 is invalid. TCP stipulates that ACK must be set to 1 for all packet segments sent after a connection is established.
    • PSH=1 (transfer) : 1 means that the receiver should push the data to the application layer immediately, rather than wait until the buffer is full.
    • RST=1 (reset) : 1 indicates that the current TCP connection has a serious problem and may need to be re-established. It can also be used to reject invalid packet segments and connection requests.
    • SYN=1: When SYN=1 and ACK=0, the current packet is a connection request packet. When SYN=1 and ACK=1, the current packet is a reply packet agreeing to establish a connection.
    • FIN=1 (End) : If the field is 1, the packet is a request packet to release the connection.
  • Window Size (2 bytes) : indicates the Size of the Window, indicating how many bytes of data can be received for flow control
  • Check sum (2 bytes) : Checks whether the header and data have changed
  • Emergency pointer (2 bytes) : Valid only if URG = 1, indicates the number of bytes of emergency data. Emergency pointer indicates the end of emergency data. Note: Emergency data can be sent even if the window is 0
  • Options: variable, maximum 40 bytes
    • Maximum Packet Segment Length MSS: indicates the maximum length of a data field
    • Timestamp: Used to calculate the round-trip time RTT

– the IP network layer

When the network data packet is stored and forwarded, the router calculates the egress of the data packet through the routing algorithm, which is the rough storage and forwarding

The network layer only provides simple and flexible, connectionless, datagram services delivered with the greatest effort. The network layer does not provide the promise of quality of service, which may cause errors, loss, repetition, disorder and other problems. If the communication in the host needs to be reliable, the transmission layer is responsible for it (including error handling, flow control, etc.).

The main protocol is IP, which is used with three other protocols:

  • Address Resolution Protocol (ARP)
  • Internet Control Message Protocol (ICMP)
  • Internet Group Management Protocol (IGMP)

  • Version (4 bits) : Protocol version. The IP protocol versions of the communication parties must be the same, including IPv4 and IPv6
  • Header length (4 bits) : The maximum expressible decimal value is 15, one unit for 32-bit word length (4 bytes); The fixed length of the IP header is 20 bytes, so the minimum value is 5
  • Differentiated service (8 bits) : Generally not used
  • Total length (16 bits) : The length of the header and the data, expressed in bytes. The maximum length of a datagram is 2 ^ 16 -1 = 65535 bytes. However, each data link layer protocol at the IP layer defines the maximum length of the data field in a data frame, called the Maximum transmission unit (MTU). When a data packet is encapsulated into a data frame, the length of a data packet cannot exceed the MTU value specified by the link layer protocol. If the MTU value exceeds the MTU value, the data packet must be fragmented.
  • Long datagrams transmit more efficiently (because of the small header ratio), and short routers forward faster
  • Identifier (16 bits) : For each datagram generated, this value is +1, so that the fragmented datagram can be reassembled into the original datagram
  • Flags (3 bits) : Currently only 2 bits are meaningful
    • The minimum value of the flag field is MF. MF = 1 is followed by sharding. MF = 0 indicates the last of several datagrams
    • The middle bit of the flag field is marked as DF, which means sharding is not allowed. Sharding is allowed only when DF = 0
  • Slice offset (13 bits) : The relative position of a slice in the original group
  • TTL (8 bits) : TTL indicates the lifetime of a datagram in the network. If the TTL decreases to 0, the datagram is discarded. The maximum value is 255
  • Protocol (8-bit) : The IP layer of the destination host knows which protocol to assign the data part to

  • Header check sum (16 bits) : only the header of the datagram is checked, but the data part is not included. The header check sum will be recalculated every time the router passes through it. If the value is 0, there is no change
  • Source address (32 bits)
  • Destination address (32 bits)

IP forwarding

The IP header records the source AND destination IP addresses, but not the IP addresses of the routers that pass through it.

So when a router receives a datagram to be forward, from the routing table after the next-hop router’s IP address, sent to the data link layer of the network interface software, network interface software use ARP to convert the next-hop router’s IP address to MAC address, in the link layer MAC frame first, and then find the next-hop router based on the hardware address.

Routing table: Each row corresponds to one network and one routing record (destination network address, subnet mask, 0 next-hop address).Copy the code

Data link layer-MAC frames

It manages the sending and receiving of data packets between two physical machines (terminals, routers, switches, etc.). Wifi is located at the data link layer

Data link layer protocol data unit – frame

  • Destination address and source Address (6 bytes) : Records the MAC address of the router, which is changeable during transmission
  • Type (2 bytes) : What protocol does the layer above flag use
  • Data fields (between 46 and 1500 bytes)
  • Frame check sequence FCS (using CRC check)

APR protocol

MAC addresses can be resolved based on IP addresses at the network layer or data link layer.

There was also an old reverse address resolution protocol, RARP, which used to find IP addresses based on hardware addresses, replaced by DHCP.

Each host has an ARP cache, which contains a mapping table from IP addresses of hosts and routers on the local LAN to hardware addresses. This mapping table is frequently updated (new or timeout deletion: hosts in the network change, and the physical address of the network adapter changes).

The physical layer

How is every bit of data that manages every bit of data transmitted over physical media (cable, fiber optics, radio waves, etc.)

What happens when you enter the domain name to render the page

Connect the Internet

The newly connected laptop requires an IP address, a local router address, and a DNS server address to connect to the Internet. The data is parsed and retrieved using DHCP (application layer), which is supported by UDP (transport layer)

  • DHCP queries are encapsulated in UDP data segments, which are then encapsulated in IP datagrams, which are then encapsulated in 802.1 Ethernet data frames
  • Ethernet data frames are broadcast to the entire LAN to be received by the network device running the DHCP server
  • In the DHCP server device, Ethernet data frames are decapsulated into IP datagrams, decapsulated into UDP network data segments, decapsulated into DHCP information, and then run the ROUTER of DHCP to interpret the information and find that a new device is coming and he needs an IP address.
  • The DHCP server generates DHCP ACK information, which containsIP address of the user.IP address of the local router.The name and IP address of the DNS server
  • After layer by layer encapsulation on the DHCP server, data frames are pushed to the client through the LAN. During this process, the switch records the port number corresponding to the IP address, and then the data frames are layer by layer unsealed on the client
  • After receiving a DHCP ACK packet, a DHCP user obtains his OWN IP address, knows the name and address of the DNS server, and the IP address of the local router. Then, a DHCP user can use the DNS address to find the IP address of the corresponding domain name, and use the IP address of the local router to forward data

Obtain the IP address corresponding to the domain name from DNS

Domain names are converted to IP because computers only know numbers (IP), not domain names. Of course, you don’t need to do this if you enter an IP address directly or if you have DNS cache

DNS cache means that after the DNS returns the correct IP address, the system temporarily stores the result (mapping between the domain name and the IP address). And it will set an expiry time for the cache (such as N hours), in the N hour, when you visit this site again, the system will from your computer directly to the local DNS cache (browser, operating system, routing, etc.) Give the results back to you, and don't have to ask the DNS server, disguised "accelerated" url parsing. Of course, after more than N hours, the system will automatically ask the DNS server again for new results. So, when you change the DNS server and you don't want your computer to use the DNS cache, you need to manually clear the local cacheCopy the code

DNS query information is created and encapsulated into UDP network segments, IP datagrams, and Ethernet data frames. To enable data frames to be sent to the router, you need to use ARP to check the MAC address of the local router. (Although you know the IP address of the router in the previous step, you still need the MAC address of the router to transmit information to the router. Of course, if the DNS server is on the same network segment, you can skip the router.)

  • After receiving ARP packets, the local router sends an ARP reply containing the MAC address of the corresponding port on the router
  • IP datagrams containing DNS queries are sent from the client to the local router over the LAN Switch
  • This IP datagram is sent from the local network to the local ISP network, where it is routed to the corresponding DNS server, where multiple root DNS servers may be involved, so CDN optimization can be used.
  • IP datagrams are decapsulated on the DNS server and the IP address corresponding to the domain name is returned

The IP address is encapsulated in the IP header of the network layer

  • Source ADDRESS: own IP address
  • Destination ADDRESS: IP address of the domain name

HTTP transport

To enable the application processes of computers with different operating systems to communicate with each other, the transport layer uses the protocol port number, referred to as port for short. Although the end of communication is the application process, the final delivery of the destination process is completed by TCP or UDP as long as the packet is delivered to the destination port of the destination host.

The abstract protocol port between the stack layer is a software port (an address at which various protocol processes of the application layer interact with transport entities), and a hardware port on a router or switch (the interface for hardware devices to interact) is a different concept. The port is encapsulated in the TCP header

And then from there it’s old school, setting up a TCP connection

  • The client first creates a TCP socket to communicate with the Web server
  • The TCP SYN segment (the first message of the three-step handshake) is transmitted to the Web Server
  • The Web Server returns a TCP SYNACK message (step 2 of the three-step handshake)
  • The client sends a successful response, and the TCP connection is created successfully
  • The client pushes the HTTP request into the TCP socket and sends the IP datagram to Google’s Web Server. Before the data enters the server, it may also pass through the load-balancing server, which distributes the request to multiple servers
  • The Web Server returns HTTP Reply containing the desired home page content to the client

Browser rendering

  • So the browser gets the data and it decides what the status code is, if it’s 200 it’ll keep parsing, if it’s 400 or 500 it’ll get an error, if it’s 300 it’ll redirect, and there’s a redirection counter here, so if it redirects too many times, it’ll get an error if it redirects too many times. The browser starts parsing the file, unzipping it if it’s in Gzip format, and knowing how to decode the file based on its encoding format.

  • By default, Chrome assigns a render process to each page, meaning that a new render process is created for each new page opened.

  • After decoding the file successfully, the rendering process will officially start. DOM tree will be built according to HTML first, and CSSOM tree will be built if CSS is available. After the CSSOM and DOM trees are built, Render trees are generated, and layouts calculate the Layout, style, and many other aspects of the page elements. Painting starts Painting and then displays the page. This area is currently optimized for reflux repainting as well.