Network Model and Network Programming

Glossary

  • OSI (Open System Interconnect), that is, open system interconnection. Generally called the OSI reference model, it is a network interconnection model studied by the ISO (International Organization for Standardization) organization in 1985.
  • TCP (Transmission Control Protocol), transmission control protocol
  • IP (Internet Protocol), the full name of the Internet protocol address, refers to the IP address, which means the Internet protocol assigned to the user to use the Internet
  • TCP/IP is a protocol group that can be divided into four layers: network interface layer, network layer, transport layer and application layer.
  • To sum up, OSI is a reference model, and the actual application is basically the TCP/IP four-layer protocol

*Note: The difference between TCP/IP4 and 5 layers is actually whether the underlying physical layer and data link layer are defined together.

1. OSI seven-layer protocol model

1. Division of the OSI seven-layer model

The OSI seven-layer protocol model is mainly: application layer (Application), presentation layer (Presentation), session layer (Session), transport layer (Transport), network layer (Network), data link layer (Data Link), physical layer ( Physical). As shown below.
Each layer implements its own functions and protocols, and completes the interface communication with adjacent layers. The service definition of OSI specifies the services provided by each layer. The service of a certain layer is a capability of this layer and the layers below it, which is provided to a higher layer through an interface. The services provided by each layer have nothing to do with how these services are implemented.

2. Function definition of each layer

Here we only give a general description of the functions of each OSI layer, and do not delve into it in detail, because each layer is actually a complex layer. Later, I will also carry out in-depth study of some layers according to my personal direction. Here we have a general understanding. We start from the top layer - the application layer. The whole process is explained by taking the sending of a commercial quotation between Company A and Company B as an example.

<1> The layer closest to the user in the application layer
OSI reference model is to provide application interfaces for computer users and directly provide users with various network services. Our common application layer network service protocols are: HTTP FTP TFTP SMTP SNMP DNS TELNET HTTPS POP3 DHCP etc.

In fact, the boss of company A is the user we mentioned, and the commercial quotation he wants to send is a network service provided by the application layer. Of course, the boss can also choose other services, for example, sending a business contract, sending An inquiry form, etc.

<2> Presentation layer
The presentation layer provides various encoding and conversion functions for application layer data, ensuring that the data sent by the application layer of one system can be recognized by the application layer of another system. If necessary, this layer provides a standard representation for converting various data formats inside the computer into a standard representation used in communication. Data compression and encryption are also among the transformation functions that the presentation layer can provide. (It has been merged into the application layer in the five-layer model) The formats include JPEG, ASCll, DECOIC, encrypted formats, etc.

Since company A and company B are companies in different countries, they agree to use English as the communication language, so at this time, the presentation layer (the company's secretary) is to translate the transmission information of the application layer into English. At the same time, in order to prevent other companies from seeing it, the people of company A will also encrypt the quotation. This is the role of representation, converting and translating data at the application layer.

<3> Session layer
The session layer is responsible for establishing, managing and terminating communication sessions between presentation layer entities. Communication at this layer consists of service requests and responses between applications in different devices. In the five-layer model, it has been merged into the application layer) corresponding to the host process, which refers to the ongoing session between the local host and the remote host

Colleagues at the conversation layer get the converted data from colleagues at the presentation layer (colleagues at the conversation layer are similar to the company’s external relations department), and colleagues at the conversation layer may have the contact information between the company and many other companies. Here, the company is the actual transmission entities in the process. They manage the company's contact sessions with many outside companies. After receiving the data from the presentation layer, the session layer will establish and record the session. He must first find the address information of company B, then put the entire information into an envelope, and write the address and contact information. Ready to send the data. After confirming that company B has received the quotation, the conversation is over, and the colleagues from the external relations department will terminate the conversation.

<4> Transport layer
The transport layer establishes the end-to-end link of the host. The role of the transport layer is to provide end-to-end reliable and transparent data transmission services for the upper layer protocol, including dealing with issues such as error control and flow control. This layer shields the details of the data communication of the lower layer from the upper layer, so that the upper layer users only see a reliable data path from host to host between two transmission entities, which can be controlled and set by the user. We usually say that TCP UDP is at this layer. The port number is both the "end" here. The protocols are: TCP UDP, once the data packet leaves the network card, it enters the network transport layer

The transport layer is equivalent to the person in the company who is responsible for sending and receiving express mail, the company's own courier, who is responsible for delivering the materials to be sent from the upper layer to the courier company or post office.

<5> The network layer
establishes the connection between two nodes through IP addressing, selects the appropriate routing and switching nodes for the packets sent by the transport layer at the source, and sends them to the transport layer at the destination without error . It is commonly referred to as the IP layer. This layer is what we often call the IP protocol layer. The IP protocol is the foundation of the Internet. (for logical address addressing, to achieve path selection between different networks) protocols include: ICMP IGMP IP (IPV4 IPV6) ARP RARP

The network layer is equivalent to the huge courier network of the courier company. Different distribution centers across the country, for example, SF Express from Shenzhen to Beijing (land transportation as an example, air transportation seems to fly directly to Beijing), first of all, go to SF Express Shenzhen distribution center, from Shenzhen distribution center to Wuhan distribution center, from Wuhan distribution center to Beijing Shunyi distribution center. Each distribution center is equivalent to an IP node in the network.

<6> Data link layer
Combine bits into bytes, then combine bytes into frames, use link layer addresses (Ethernet uses MAC addresses) to access media, and perform error detection.

The data link layer is further divided into 2 sublayers: the logical link control sublayer (LLC) and the media access control sublayer (MAC).

The MAC sublayer handles the CSMA/CD algorithm, data error check, framing, etc.; the LLC sublayer defines some fields so that the last protocol can share the data link layer. In actual use, the LLC sublayer is not necessary.

This did not find a suitable example

<7> Physical layer
The transmission of the actual final signal is realized through the physical layer. Transmission of bitstreams over physical media. Specifies level, speed and cable pinout. Commonly used devices are (various physical devices) hubs, repeaters, modems, network cables, twisted pair cables, and coaxial cables. These are the transmission media of the physical layer.

The means of transportation in the express delivery process is equivalent to our physical layer, such as cars, trains, planes, and ships.

3. Communication characteristics: peer-to-peer communication

Peer-to-peer communication, in order to transmit data packets from the source to the destination, each layer of the OSI model at the source must communicate with the peer layer at the destination, this communication method is called peer-to-peer communication. In the communication process of each layer, it uses its own protocol for communication.

Two, TCP/IP protocol

  • The four-layer architecture of TCP/IP mainly includes: application layer, transport layer, network layer and network interface layer. In essence, there are only the upper three layers, and the network interface layer has no specific content.
  • The TCP/IP five-layer architecture includes: application layer, transport layer, network layer, data link layer and physical layer.
  • The five-layer protocol is just a synthesis of OSI and TCP/IP, and the actual application is still the four-layer structure of TCP/IP. For convenience, the next two layers can be called the network interface layer.

     

    The following figure involves the socket socket

     


    Socket is just a connection mode, not a protocol. Socket is an encapsulation of TCP/IP protocol. Socket itself is not a protocol, but a call interface (API)

Three, socket socket

This is a communication channel established to realize the above communication process. Its real representative is a communication process between the client and the server. The two processes communicate through sockets, and the communication rules adopt the specified protocol.

Through Socket, we can use the TCP/IP protocol. tcp, udp, simply put (although not accurate) are the two most basic protocols,

Many other protocols are based on these two protocols. For example, http is based on tcp. You can create tcp connections or udp connections with sockets.

This means that sockets can be used to create connections of any protocol, because other protocols are based on this.

TCP and UDP at the transport layer.
TCP: Transmission Control Protocol
UDP: User Datagram Protocol (UDP: User Datagram Protocol)

The specific connection between the application layer and the transport layer can be found in another article:

Protocol details:

1. Network layer


1.1 The role of the network layer

Network layer : Responsible for address management and routing selection. For example, in the IP protocol, a host is identified by an IP address, and the data transmission line (route) between two hosts is planned through a routing table. Router (Router ) works at the network layer. Typical protocol: IP. Typical equipment: Router: Realize data routing and forwarding.

  • Host : a device with an IP address but no routing control;
  • Router : not only equipped with IP address, but also capable of routing control;
  • Node : Collective term for hosts and routers

1.2 IP protocol*

insert image description here

1.2.1 Protocol header format

insert image description here

  • 4-digit version number (version): specifies the version of the IP protocol, for IPv4, it is 4.
  • 4-bit header length (header length): How many 32bits is the length of the IP header, that is, the number of bytes of length * 4. 4bit means that the maximum number is 15, so the maximum length of the IP header is 60 bytes.
  • 8-bit service type (Type Of Service): 3-bit priority field (deprecated), 4-bit TOS field, and 1-bit reserved field (must be set to 0). 4-bit TOS respectively represent: minimum delay, maximum throughput Volume, highest reliability, and minimum cost. These four conflict with each other, and only one can be selected. For applications such as ssh/telnet, the minimum delay is more important; for programs such as ftp, the maximum throughput is more important.
  • 16-bit total length (total length): How many bytes the IP datagram occupies as a whole.
  • 16-bit identifier (id): uniquely identifies the packet sent by the host. If the IP packet is fragmented at the data link layer, the id in each fragment is the same.
  • 3-bit flag field: The first bit is reserved (reserved means it is not used now, but it may be used in the future if it has not been decided yet). The second bit is 1, which means that fragmentation is prohibited. At this time, if the packet length exceeds the MTU, the IP module The message will be discarded. The third bit means "more fragments". If it is fragmented, the last fragment is set to 1, and the others are 0. It is similar to an end mark.
  • 13-bit fragment offset (framegament offset): It is the offset of the fragment relative to the beginning of the original IP message. In fact, it indicates where the current fragment is in the original message. The actual number of bytes offset is This value * 8 is obtained. Therefore, except for the last message, the length of other messages must be an integer multiple of 8 (otherwise the messages are not continuous).
  • 8-bit time to live (Time To Live, TTL): The maximum number of hops for a datagram to reach its destination. Generally, it is 64. Every time it passes through a route, TTL-= 1, until it reaches 0, it will be discarded Yes. This field is mainly used to prevent routing loops
  • 8-bit protocol: Indicates the type of the upper layer protocol
  • 16-bit header checksum: use CRC to verify whether the header is damaged.
  • 32-bit source address and 32-bit destination address: Indicates the sender and receiver.
  • Option field (variable length, up to 40 bytes): slightly

1.2.2 Network segment division

The IP address is divided into two parts, the network number and the host number

  • Network number : ensure that the two network segments connected to each other have different identifiers;
  • Host number : In the same network segment, hosts have the same network number, but must have different host numbers;
    insert image description here
    insert image description here
  • Automatic management of subnet internal ip—DHCP technology
    can automatically assign IP addresses to new host nodes in the subnet, avoiding the inconvenience of manual IP management.
    General routers have DHCP functions. Therefore, routers can also be regarded as a DHCP server .

1.2.3 Subnet mask

  • Introduce an additional subnet mask (subnet mask) to distinguish the network number from the host number;
  • The subnet mask is also a 32-bit positive integer. It usually ends with a string of "0";
  • Perform a "bitwise AND" operation on the IP address and the subnet mask, and the result is the network number;
  • The division of network number and host number has nothing to do with whether the IP address is class A, class B or class C

The subnet mask is to distinguish the network bit from the host bit. As we mentioned above, an ip address is composed of a network part and a host part. Just as a person's name is made up of a first name and a last name.

Then we can compare the IP address to a person's name, then the subnet mask is like a list, we can quickly know those people with the same surname and those with different surnames, group people with the same surname into a group, and let them can communicate with each other.

for example:

There is a network segment of 192.168.1.0-192.1.254, this network segment is like a village, we call it Security Village, this network segment has an IP address of 192.168.1.1, we call it Security One, and A person is called Anfang 2, and its IP address is 192.168.1.2. When we saw them, we knew they were from the same village.

There is another network segment, which is 192.168.0.0——192.168.255.254. We call it Ancun. There is a village with two IP addresses, 192.168.1.1 and 192.168.1.2, also called Security 1 and Security 2. Then the problem comes. ? At this time, how to distinguish which village they belong to?

At this time, the subnet mask is needed to determine which network segment they belong to. You need to take Security 1 and Security 2 to the village to identify them, and you will know that they belong to that village. The network segment of Security Village is 255.255. 255.0, the network segment of Ancun is 255.255.0.0.

There will also be ip addresses similar to "same name" and "same surname" in the network. How to distinguish which network segment they belong to depends on the subnet mask

1.2.4 Special IP address

  • Set all the host addresses in the IP address to 0, which becomes the network number, representing this LAN;
  • Set all the host addresses in the IP address to 1, which becomes a broadcast address, which is used to send data packets to all hosts connected to each other in the same link;
  • The IP address of 127.* is used for the local loopback (loop back) test, usually 127.0.0.1

1.2.5 Limitation on the number of IP addresses

  • We know that an IP address (IPv4) is a 4-byte 32-bit positive integer. Then there are only 2 to the 32nd power of IP addresses, which is about 4.3 billion. The TCP/IP protocol stipulates that each host needs to have an IP address.
  • Does this mean that only 4.3 billion hosts can access the network?
  • In fact, due to the existence of some special IP addresses, the number is far less than 4.3 billion; in addition, IP addresses are not configured according to the number of hosts, but each network card needs to be configured with one or more IP addresses.
  • CIDR (masking scheme) has alleviated the problem of insufficient IP addresses to a certain extent (increased utilization and reduced waste, but the absolute upper limit of IP addresses has not increased), but it is still not enough. There are three ways at this time to solve
    • Dynamic allocation of IP addresses: only assign IP addresses to devices connected to the network. Therefore, devices with the same MAC address will not necessarily get the same IP address every time they connect to the Internet;
    • NAT technology (will be introduced later);
    • IPv6: IPv6 is not a simple upgraded version of IPv4. These are two irrelevant protocols and are not compatible with each other; IPv6 uses 16 bytes and 128 bits to represent an IP address; but IPv6 is not popular yet;

1.2.6 Routing

In a complex network structure, find a route to the end

insert image description here
The transmission process of IP packets is also the same as asking for directions.

  • When the IP data packet arrives at the router, the router will check the destination IP first;
  • The router decides whether the data packet can be sent directly to the target host or needs to be sent to the next router;
  • Repeat in turn until reaching the target IP address

In order to send data packets to the target host, all hosts maintain a routing table (Routing Table), which records which router the IP data should be sent to in the next step. IP packets will be transmitted on each data link according to this routing table.
insert image description here

  • Routing table—The routing table can be viewed by using the route command.
    If the destination IP hits the routing table, it can be forwarded directly;
    the last line in the routing table is mainly composed of the next hop address and the sending interface. When the destination address matches the routing table When no other lines in the .

2. Link layer


2.1 Link layer role

Data link layer : Responsible for the transmission and identification of data frames between devices. For example, the driver of the network card device, frame synchronization (that is, what signal is detected from the network line is counted as the beginning of a new frame), conflict detection (if a conflict is detected Automatic retransmission), data error checking, etc. There are standards such as Ethernet, token ring network, and wireless LAN. The switch (Switch) works at the data link layer. Typical protocol: Ethernet protocol. Typical equipment: switch : Realize data exchange and forwarding.

2.2 Ethernet

"Ethernet" is not a specific network, but a technical standard; it includes not only the content of the data link layer, but also some content of the physical layer. For example: specifies the network topology, access control method, transmission rate wait;

For example, the network cable in Ethernet must use twisted pair; the transmission rate is 10M, 100M, 1000M, etc.;

Ethernet is currently the most widely used LAN technology; alongside Ethernet are Token Ring, Wireless LAN, etc.
insert image description here

  • The source address and destination address refer to the hardware address of the network card (also called MAC address), the length is 48 bits, and it is solidified when the network card leaves the factory;
  • The frame protocol type field has three values, corresponding to IP, ARP, and RARP;
  • At the end of the frame is the CRC check code

2.3 MAC address

IP and MAC

Although ipv6 is now available, most of the protocols we use are still ipv4. The so-called ip is the number of the entire network of your computer. Other computers need this number to access the computer. But this number is constantly changing in many cases. The only constant is your MAC address: the physical address.

MAC is the unique network address used to identify the network card device in the network. Uniformly assigned by the relevant hardware manufacturer, the MAC address of each computer is unique.

To make a metaphor, you often move, and you have an address every time you move, XX unit XX in XX community, this is the IP. But your name remains the same, this is MAC, the difference is that our MAC does not allow duplicate names

Our IP is divided into two parts: as shown in the figure above, it is divided into the network part and the host part. The network part is like you are in XX Town, XX City, XX Province, which is fixed by the country. But the number XX of unit XX in XX community is determined by the developer himself. The two numbers add up to your ip. The difference is that in reality the length of the two numbers is fixed, but the ip addresses of A, B, C, and D on the network change

  • MAC format:
    • The MAC address is used to identify the connected nodes in the data link layer;
    • The length is 48 bits, and 6 bytes. It is generally represented by a hexadecimal number plus a colon (for example: 08:00:27:03:fb:19)
    • It is determined when the network card leaves the factory and cannot be modified. The mac address is usually unique (the mac address in the virtual machine is not the real mac address, which may conflict; some network cards also support user configuration of the mac address)

2.4 PERSON*

MTU is equivalent to the limit on the package size when sending express. This limit is the limit of the physical layer corresponding to different data links.

  • The data length in the Ethernet frame stipulates a minimum of 46 bytes and a maximum of 1500 bytes. The length of the ARP data packet is not enough for 46 bytes, and padding bits must be added at the back;
  • The maximum value of 1500 is called the maximum transmission unit (MTU) of Ethernet, and different network types have different MTUs
  • If a data packet is routed from the Ethernet to the dial-up link, and the length of the data packet is greater than the MTU of the dial-up link, the data packet needs to be fragmented (fragmentation);
  • The MTU of different data link layer standards is different;

MTU is the maximum transmission unit. If a datagram is larger than MTU but smaller than 64K, data fragmentation will be performed at the network layer, but tcp will not perform data fragmentation, because the MSS negotiated by tcp at the transport layer is calculated through MTU or MTU Therefore,  the data fragmentation of the network layer is mainly aimed at UDP datagrams.

  • Divide larger IP packets into multiple small packets, and label each small packet;
  • The 16-bit identification (id) of each small packet IP protocol header is the same;
  • In the 3-bit flag field of the IP protocol header of each small packet, the second bit is 0, indicating that fragmentation is allowed, and the third bit is used to indicate the end mark (whether the current is the last small packet, if yes, set it to 1, otherwise set it to 0 );
  • When arriving at the opposite end, these small packets will be reassembled in order, assembled together and returned to the transport layer;
  • Once any of these small packets are lost, the reassembly at the receiving end will fail. But the IP layer will not be responsible for retransmitting the data

2.5 ARP protocol

ARP is a protocol to solve the address problem. It uses the target address as a clue to locate the MAC address corresponding to the next network device that should receive the data packet. However, ARP is only applicable to IPv4, not to IPv6

In simple terms, the ARP protocol is to obtain the MAC through IP.
insert image description here
RARP is a protocol that reverses ARP and locates the IP address from the MAC address.

2.6 DNS

DNS is a domain name resolution server (Domain Name System), which is a server that converts a website into an IP address.

Effectively manage the correspondence between hostnames and IP addresses -> DNS system
insert image description here

2.7 NAT technology

NAT (Network Address Translator) is a technology used to use private addresses in the local network and switch to global IP addresses when connecting to the Internet

insert image description here
NAPT
Then the problem comes, if there are multiple hosts in the LAN accessing the same external network server, then the destination IP in the data returned by the server is the same. So how does the NAT router determine which LAN to forward this data packet to? the host?

At this time, NAPT will solve this problem.  Use IP+port to establish this relationship

3. What happens when the browser enters the url (classic problem)


1. DNS resolution: resolve the domain name to an IP address; must-see—DNS reference
2. TCP connection: TCP three-way handshake;
3. An HTTP request occurs ;
4. The server processes the request and returns an HTTP message;
5. The browser parses and renders the page ;
6. Disconnect: TCP waves four times;

Guess you like

Origin blog.csdn.net/a1058926697/article/details/130739743