Hello everyone, I’m clickbait, ah no, I’m Xiaoyu Xiaoyu, dedicated to sharing interesting and useful technical articles. Content is divided into translation and original, if you have any questions, feel free to comment or private letter, I hope to progress with you. Sharing is not easy, hope to get your support and attention.

What is the Internet

Internet refers to the network composed of equipment that can communicate with each other is called Internet, refers to the various networks created by using TCP/IP communication agreement, is the largest Internet in the world, also known as “international Internet”.

TCP/IP is the basic communication architecture of the network and provides point-to-point connection mechanism. And the software communication process is abstracted into four layers of abstraction, the lower layer of service layer, which is the familiar seven-layer OSI model. (That is to say, TPC/IP is the basis of networking, from which came OSI)

The OSI model is a standard framework for viewing computers around the world as interconnected networks, that is, the standard for network interconnectivity. In plain English, rules are rules. By following this rule, we can connect to the Internet.)

The OSI model is represented by a diagram:

When discussing TCP/IP, we can divide it into four layers, as shown below:

Just look at it, we need to focus on the application layer HTTP and transport layer TCP, by the way, the relationship between the two models is as follows:

TCP and UDP are located at the transport layer, while HTTP is located at the application layer. In accordance with the mechanism that the lower layer serves the upper layer, we will start with TCP and UDP.

What is the UDP

UDP is a connectionless protocol that sits at the transport layer of the OSI model. The standard’s definition is very specific, that is, data can be sent without thinking. Why is this jerk unreliable?

  • No connection: such as light rain to go to work, no matter his week or weekend, over, naturally there will be some problems
  • Constant speed: This person has no congestion control system, which means that they are inflexible and follow a single frequency, even if the other party can’t stand it, UDP will not change its frequency.
  • Simple: The header overhead is small, only eight bytes, about three times less than TCP, so it’s fast.

However, because UDP is fast, there are some scenarios are not other, such as DNS ah, audio and video ah, real-time games ah, chat tools ah blah blah blah…

Some people may ask, DNS this use of UDP will not have a problem ah, such as UDP packet loss, that is not returned 404 ah.

Yes, but browser response times can be broken down into three categories:

DNS resolution + TCP connection + HTTP request/response

In addition to DNS can use UDP, other two also can not use ah, otherwise the website lost content ah. And there are alternatives, which in some cases fail to use TCP to re-query, not just UDP.

May still have the person to be able to ask, that chat tool this kind uses UDP this problem big, I if tell my goddess to vindicate, this mang fu just send, result all sorts of throw package (send failure), and I still don’t know, that my state of mind collapsed. I thought she knew, but she didn’t, and then she didn’t answer me back.

Can only say, why can’t leave mystery to goddess, let her come to you actively, ha ha ha

In fact, communication tools will generally send two, UDP send a message, if the server received, will use UDP to return you a message, if not returned, or send failure, you will receive a similar message to send failure, to vindicate it, nothing, the message must be sent out, answer do not agree to see yourself.

In general, UDP is suitable for situations where speed is very high but accuracy is low.

What is the TCP

UDP mang f unreliable, TCP guy to make up for.

As for TCP, I think it can be represented by anthropomorphism. Is it known that TCP is full-duplex? In fact, it is two independent people chatting on wechat. Just one person is not good, the other person has to post, or we’ll talk about J.

So how does TCP count?

That’s right, guy looked at the calendar!! And even better, he looks at the calendar when he’s on vacation!!

In fact, TCP creates and terminates connections before and after sending data. Ensure transaction integrity. This is the question we are often asked: three handshakes and four waves.

A three-way handshake creates a connection

To be clear in advance, there is no discussion of the format and content of each logo, but you can do your own research if you are interested. Look up these definitions when you need them.

Before we do that, let’s explain a few nouns.

  • SYN: Establishes a connection. If I send you a message with this symbol, I want to contact you in a negative distance. Of course, this is two-way.
  • FIN: Close the connection, same as above, but I’m ready, please leave
  • SEQ: indicates the initial package number, which varies with time. That is to say, I want you to start counting from this number, in fact, to make sure that both sides are themselves, can not casually bring a serial number to work, that would be messy
  • ACK: Indicates a response, which means I successfully got what you gave me
  • PSH: DATA is being transmitted
  • RST: resets the connection.

Okay, let’s get down to business. Let’s look at a picture

Well, I guess that’s when you look at the picture. But actually, I’m missing a little bit, what are the values of these things? Let’s see a real example of establishing links (visit Baidu’s capture package)!

You can clearly see that after three TCP handshakes, the channel is established and then the HTTP request is sent, which also laterally validates the transport layer service application layer.

You can also see that values like seq ACK change every time you send TCP, and this is the key, they say what they mean, but they don’t say why they’re used.

This is because we want to make sure that both sides of the connection are deterministic so that we can establish a reliable connection.

The client sends a request to Connect to Baidu, and then sends TCP with the SYN flag to the server, telling the server, I want to connect with you.

If the server does not receive it, it will forget it. If it does, it will return an ACK signal indicating that it received it and send the server’s SYN to establish a connection with the client, which is bidirectional and reliable.

What if the server sends it and the client doesn’t get it? The server periodically resends the packets until they succeed or exceed the maximum limit. A simplified version of the heartbeat.

The client receives the SYN and ACK from the server and sends an ACK signal indicating that the server is ok

In the figure above, if the value of the last two ACKS is 1, it means that the response succeeded once.

In more general terms, the next TCP communication should prove that the last TCP communication was successful. Take ACK for example, when I first request connection, the initial ACK must be 0, and then you send me 1, which proves that my first request is successful, and vice versa. You can think for yourself, welcome to communicate with me if you have any questions.

Now, one might say, why does it have to be three? Because the next TCP communication needs to prove that the last TCP communication was successful. Why don’t you try it twice, see if you can prove it. This is the minimum number of three times to ensure efficiency. Three times of communication is theoretically possible, but not necessary.

Four waves to disconnect

The four waves are similar, but the SYN becomes FIN and is used to ‘trigger’ the disconnection. The operation is actually the same.

Why can’t you just create a connection three times?

A probably inappropriate example: creating a connection is adding something to an empty container, and that’s it, because it’s empty to begin with anyway. Disconnecting requires cleaning out the contents of the container after nothing is added.

In one picture:

Don’t understand, private talk about me, I hate you!!

Ok, transport layer task complete, application layer start ~

HTTP and HTTPS

I have a lot to say, but I don’t feel like I have anything to say.

First we know that HTTPS is HTTP, but with an added layer of security: SSL/TLS.

How is it safe?

We know that HTTP is transmitted in clear text, and we cannot verify the integrity of the content. And because it is stateless link, so also do not know who sent this thing, will be changed.

So what exactly does HTTPS’s S do?

Encryption!

Symmetric encryption

It’s our entrance guard. Yeah, like in a hotel. We can protect the content we are about to send, put it in the hotel, and then access the content through access control (key). Looks pretty safe. But if someone sneaks out the door and makes a copy, then our content can be accessed at will. This is ridiculous. We have no privacy. Naked

Asymmetric encryption

Let’s change the plan, we let the hotel provides two entrance guard card, an open, everyone can know, is a private, only for people to know, and then the hotel room into two doors, and outside of the door can be used to open door open (public-key encryption), the inside of the door can be opened by the private key (decrypted), and the inside of the door there is a channel, You can have someone with a public key put the content in the room. Since the door inside can only be opened with a private key, only the owner can view this content.

This is asymmetric encryption, using two keys, a public-public key and a private-public key. And then we just disseminate the public key.

But asymmetric encryption is much slower than symmetric encryption, which is not what we want. And simple asymmetric encryption can only ensure that the private information encrypted with the public key can only be viewed by the person in charge of the private key.

Ps: I still have a problem that I don’t understand. If the public key is distributed freely, then malicious people can communicate freely. Although no problem, it can be controlled by the server. But it feels weird

How do you make it faster?

Symmetric encryption is mixed with asymmetric encryption, with symmetric encryption used for content and asymmetric encryption used for transmission of symmetric encryption keys.

So we only use asymmetric encryption once, and then we can use symmetric encryption to parse the content. Greatly increased speed.

But there’s a serious problem. If a bad guy intercepts the symmetric encryption key sent by the server, he can not only obtain the server’s public key, but also send his public key to the browser, acting as a middleman. Browsers and servers unwittingly engage in dangerous communication activities.

How to prove the correctness of the public key

This takes advantage of another feature of asymmetric encryption, digital certificates. Since the private key is unique, we can encrypt (sign) it with the private key so that only the correct public key can unlock it. The content is guaranteed to be safe.

And you need some secure public key built into the browser to parse the certificate, which is where the Certificate Center (CA) comes in. Certificate centers are an absolutely guaranteed organization that will make arrangements with operating system and browser vendors to plant the public keys of a certificate center (CA) in advance.

Next, the server will ask the certificate center to authenticate the public key in advance and return the digital certificate to the server. The certificate uses the certificate center private key to encrypt server information and the server public key. In this way, the public key cannot be tampered and security is ensured.

The browser then gets the correct public key when it requests it. When the server returns the content, it also performs a step processing to ensure that the content will not be tampered with.

The browser returns two parts, one of which is the hash algorithm result in plain text. One part is the plaintext hash algorithm result encrypted with the server’s private key. The browser then decrypts the result using the hash and the server’s public key. If the two parts are equal, the transmission is ok. You can write to each other happily

Why hash in encryption ah, direct encryption is not good? The simple answer is efficiency.

To optimize the

  • TCP three-way handshake can save a handshake time by using the ‘session cache’, which is actually the cache, and we usually write the same code, but not the same way. Many of the techniques we use are also some basic techniques, so it’s good to draw inferences.
  • It’s pretty much the same. TCP can send data on the second handshake, you can manipulate it
  • Minimize rsa algorithm, it is slow, this way to cache or algorithm optimization, I also do not understand ~
  • Certificate optimization, how cheap how bai, uproot!! But also depends on the enterprise/individual how to choose
  • Wait ~ do not go into depth

conclusion

The content of this article is displayed after understanding after processing. If you don’t understand it, you won’t scrip. If you still have any questions, please point out, I will reply in time.

Knowing these points will give you an overall understanding of the basics of HTTP, both at work and in a job search, and maybe a little FAQ, but it’s so small that you’ll know when you use it.

Tomb-sweeping day soon, I wish you a happy Tomb-sweeping day! Hahaha ~

Some of the pictures are from the Internet

Interview answer (funny ~

Interview answer (Lao Ji ~

Wireshark woes

meaning

How to use Wireshark

TCP&UDP

DNS: Why UDP

You two are so cute (1)

You two are so cute (2)

Feng bao