A complete HTTP request response Process (ii)

Source: Internet
Author: User
Tags ack get ip

In the previous article, we described the "Application layer" and "Transport layer" in the computer five-layer model, expounded the relative principle of the more complicated TCP protocol, and I believe we must also have some gains, so this article will continue the five-layer model learning.

Network layer

"Network layer" in fact, is a "forwarding" problem, through the legendary "IP Protocol" divided the network range, that is, I do not directly with the cable and you connected together, I can also through your IP analysis of how to find responsible for your gateway router, and through your gateway routed to you to transmit datagrams.

This is what the "network layer" does, which essentially solves the problem of two hosts that do not exist under the same sub-network to communicate with one another. and the "IP protocol" and "How to resolve the IP algorithm" is the two most core content, we first look at this "IP protocol" related concepts.

Take IPv4 as an example, use 32 bit bit to describe an IP address, so theoretically, the whole IPv4 can provide 40 million IP address, we generally use "dotted decimal" to represent.

For example: 11000001 00100000 11011000 00001001 The IP address is generally recorded as 193.32.216.9.

Thus, we solve the problem of IP addressing, but how to determine the IP address of the sub-network it belongs to?

Introduce a noun "subnet mask", which is expressed in formal and IP addresses, using 32 bit bits. wherein, the bits that describe the network part are all 1, and the number portion of the host in the subnet is all 0.

For example: Subnet mask 11111111.11111111.11111111.00000000, written in decimal is 255.255.255.0. It is clear that the first 24 bits of an IP that uses the subnet mask are its sub-network portion, and the next 8 bits are the number of the host that corresponds to that IP under the sub-network.

As an example:

IP address 172.16.254.1 corresponds to a subnet mask of 255.255.255.0, then we only need to do the "and" Operation both to get the network portion of the IP address.

Therefore, the network number of this IP address is 172.16.254.

Here we discuss a very important protocol, which solves the problem of how a host that has just joined a sub-network obtains the IP address that belongs to it, which is called Dynamic Host Configuration Protocol (DHCP).

Dhcp

In general, we have two ways to configure the host IP address, one is the administrator manually specify an IP address, of course, the cost is very high, you can not configure an already assigned IP address, that is, the administrator needs to record all assigned IP addresses.

Another is our DHCP protocol, which allows newly added hosts to automatically acquire an IP address and associated subnet mask and gateway address.

By default, the router isolates the broadcast package and does not send the received broadcast packets from one subnet to another subnet. When the DHCP server and the client are not on the same subnet, the router acting as the client's default gateway sends the broadcast packet to the subnet where the DHCP server resides, a feature known as DHCP relay (DHCP Relay).

In other words, a subnet should have a DHCP server for the allocation of IP addresses throughout the subnet. But it is also a bit "foolish" to configure a single DHCP server for each subnet.

So another solution is that a gateway in a network knows where the DHCP server is responsible for the network, what the IP address is, and the gateway route is responsible for forwarding the DHCP message request and returning the response message, which is called DHCP relay.

Of course, the current router itself can act as a DHCP server, providing a dynamic address acquisition service for its subnet, so it often doesn't need to be forwarded that much trouble.

The process of complete DHCP requests and responses is this:

The first step:

DHCP Server discovery. the first task at this stage is to find the location of the DHCP server in the current network, and the entire DHCP message exchange is based on the UDP/IP protocol, which is sent to destination port 67.

Because there is no IP address, the "source address" in the IP datagram is "0.0.0.0", and the "Destination address" is "255.255.255.255".

When the datagram is broadcast at the link layer, all hosts under the same subnet will accept the datagram, but only the DHCP server will respond to the request.

So if the router itself is a DHCP server, it will go into the second step, otherwise the router will be grouped forward to the DHCP server in the same network.

Step Two:

provided by the DHCP server. the DHCP server, either on the extranet or gateway route itself, responds to a "deliver message" after a "discovery message" is received.

This message will contain information such as the IP address, subnet mask, IP address lease period, etc. that are recommended by the customer.

Step Three:

DHCP requests . This is actually a selection phase, the client host confirms the server recommended parameters, decided to use, and then still send the request in the form of broadcast to the server to confirm.

Fourth Step:

DHCP ACK. Upon receipt of a confirmation request from the client host, the server will actually allocate an IP address from the IP pool and return the ACK of the client acknowledgment information.

Since then, the host has been given a usable IP address, and finally joined the network.

In addition, there is a detail that we do not know the daily attention, that is, we for the same sub-network, the IP address is always the same, and not because every time after the connection to the network and assigned a different IP.

This is a convention of the DHCP Protocol, when a host joins a subnet for the first time, it obtains a completely new IP address from the DHCP server.

In the future, when the host rejoin the network, it will go directly to the third step of the DHCP request and send the last parameter used by the host to the server to confirm availability. In general, the server will agree and assign an IP address according to your requirements, which is why you use almost the same IP every time.

Finish the DHCP dynamic get IP address, then we simply look at the basic format of IP datagram, and at the end of the discussion of the router selection algorithm, to see how an IP datagram is forwarded by the router.

about how the various fields or options are used, we do not discuss here for the time being, forced interpretation and suitable for everyone to understand, until the specific analysis of the message distribution and interpretation will be easy to understand a lot.

Router

The router is a core device of the network layer, which completes the full path forwarding process from "Destination IP address" to "sub-network where destination IP is located". Its internal structure is as follows:

Each port is connected directly to a single device, and the routing processor in it is responsible for parsing the output port in which the data coming in from an input is to be rolled out.

Therefore, you should also find that the core of the entire router should be this "routing processor", that is, to drive the "routing processor" work of the algorithm, we call "routing algorithm." The algorithm essentially solves the problem of a datagram input into the router's memory, which is forwarded from which output.

A good "routing algorithm" should not only solve the problem of how to get to the destination, but also consider how to get to the destination as quickly as possible, that is, can judge and selectively bypass the congested network path.

The whole routing algorithm is divided into two categories, the Global route selection algorithm and the decentralized routing algorithm. One of the most typical implementations of the former is the "link state routing Algorithm", the latter one of the most typical implementation is "distance vector algorithm".

The theoretical principles of both algorithms are not discussed here, and we focus on how the Internet is based on the routing of these two algorithms.

First, the entire internet is a very large and complex system, so it is divided into an autonomous system (as) as a whole, running the same routing algorithm in each as, and exchanging information between autonomous systems using the BGP protocol.

The entire internet is roughly one of the autonomous systems interconnected, and all the routers inside the autonomous system run the same routing algorithm, the "RIP protocol" based on the distance vector, or the "OSPF protocol" based on the link state.

As for why split the autonomous system, when we introduce this RIP or OSPF, you will understand.

The algorithm for RIP protocol is this :

A simple autonomous system, let's take this as an example to see how the entire RIP protocol works.

First of all, the forwarding of router A must be this:

----------------------------目的子网   下一跳路由   跳数x           B           1q           E           1----------------------------

Other routers are similar, and the first step is to establish a connection to a neighbor directly connected to them.

The second step is an ongoing process in which neighboring routers exchange information with each other every 30 seconds, informing each other of their own forwarding content.

So after an exchange, router A receives forwarding information from B and E, and the routing forwarding is updated as follows:

----------------------------目的子网   下一跳路由   跳数x           B           1q           E           1y           B           2p           E           2----------------------------

But here's a detail, the sub-network Y can be reached by a-B-y, but it can also be reached by A-e-c-y. As you may have guessed, the router will of course choose one of the shortest paths to update its forwarding publication.

Therefore, the algorithm of the distance vector is essentially the exchange of information between each other to ensure that an autonomous system, all routers know the shortest path of a destination subnet.

The implementation of OSPF is this:

We also explain this in the example above:

OSPF is based on link state routing algorithm implementation, so it is also a global routing algorithm, the algorithm can be run once to complete the network routing information update.

OSPF is essentially a Dijkstra shortest path problem, and it updates the entire route forwarding through continuous iteration and computation. Let's say that our router a now runs the OSPF protocol:

When the first iteration is complete, it gets the path calculation for the sub-networks associated with the B and E routers.

The second time to re-run the algorithm with B or E as the starting point, here we assume that the algorithm is run with B as the starting point, then the path to the C-related sub-network is also updated into a routing forwarding.

The third time with C as the starting point of the same running algorithm, and D related to the sub-network path update.

Because D is a terminal route and there are no other routes that are directly connected, the algorithm no longer continues, returning to E.

The fourth time, with E as the origin, runs the algorithm, gets the path of the C-related sub-network, if there is a shorter path, will update A's forwarding to the best path.

Then, until the entire algorithm runs, all the routers in an autonomous system traverse almost all, but unlike RIP,OSPF, which is relatively fast, it can accomplish tasks quickly, and RIP needs to constantly exchange information to meet demand, often falling into a long cycle.

Of course, OSPF requires strong CPU computing power and more memory storage space. So in general, they are widely used throughout the Internet, RIP is applied to the lower-level ISP, and OSPF runs in more advanced ISPs.

At this point, the entire network layer of the basic content is also introduced, summed up, the network layer of the core task is to be responsible for forwarding packets, and how to forward the packet to the destination host network, involving the IP protocol, through the IP address and subnet mask partition sub-network, And the router executes the routing algorithm to know the full route path of the destination subnet and distribute it.

Link Layer

The network layer solves the purpose network of packet forwarding, that is, the gateway route which is forwarded to the destination network, and the link layer solves the packet broadcast to the individual, that is, the destination host.

The IP packet of the network layer is encapsulated as an "Ethernet frame" at the link layer, and its basic structure is this:

The preamble is used to synchronize the clock, as I understand it to distinguish one frame, the source and destination address referring to the "Mac address", also known as the physical address.

A "Mac address" is a hardware-level host unique identifier that is uniquely determined by the manufacturer. Similar to this:

34-e6-ad-17-a5-6b

The MAC address of any host in the world is different, unlike an IP address that can be shared when no one else is using it.

Below we want to talk about a protocol, it completes the host IP address to the MAC address of the conversion, he is the ARP Address Resolution Protocol.

The ARP protocol is actually a bit like the DNS protocol we introduced in the application layer, entering a domain name address, outputting an IP address, and ARP, entering an IP address and outputting a MAC address.

Each host in the network, including the router, has built-in ARP modules and ARP tables. When a datagram arrives at the link layer, the first thing to do is to use the destination IP as input, first query their host ARP module, if you can get the IP destination host Mac address, then the encapsulation of an Ethernet frame to the physical layer sent out just fine.

If the MAC address of the destination IP host is not stored in the ARP table of this machine, then it needs to be queried to other hosts in the same network.

The sender constructs a special ARP packet, the source MAC address is the sender's MAC address, the destination MAC address is the broadcast address: 255.255.255.255, and the source and destination IP addresses are essentially a special Ethernet frame.

So all the hosts under the network will receive this ARP packet, then they have to do is to open the IP address is the same as their own IP address, if it is to respond to an ARP packet, tell the sender's own MAC address.

If not yourself, you'll also check your ARP module to see if you can help.

Eventually, the sender gets the desired destination MAC address and updates its ARP table, then encapsulates a normal Ethernet frame to send out.

Since Ethernet takes a "broadcast" approach, it is important that any host in the same subnet send messages to all the remaining hosts, but they will match whether the destination Mac address is itself or not discarded.

Okay, so that's it. We also briefly introduced the link layer of related content, about the physical layer, in fact, nothing to introduce, is 0, 1 of the transmission of electrical signals.

About the entire OSI five layer model, we have completed the study from the top to the bottom, the next one will be a complete look at "www.baidu.com", the whole computer network happened to the story, in fact, a bit of the title party, the last article to introduce the complete HTTP request process, forgive me!

All the code, pictures, and files in the article are stored on my GitHub:

(Https://github.com/SingleYam/overview_java)

Welcome to the public number: in the code of the high-base, all the articles will be synchronized in the public number.

A complete HTTP request response Process (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.