If your goddess loves you, you ask her, she probably won’t tell you.

But if it works, you can ping it.

You may know the answer by reading the headline, but do you understand the reason behind it? What happens if you replace 127.0.0.1 with 0.0.0.0 or localhost? Do you know the difference between these ips?

I have encountered this problem in the interview before, we look at a GIF to understand the scene of the interviewer and me at that time, and ask for the shadow area of the little white’s heart at that time.

Without further ado, we’ll just drive.

Unplug the network cable and disconnect the network.

Then on the console, type ping 127.0.0.1.

$Ping 127.0.0.1PING 127.0.0.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: Icmp_seq =0 TTL =64 time=0.080 ms 64 bytes from 127.0.0.1: ICmp_seq =1 TTL =64 time=0.093 ms 64 bytes from 127.0.0.1: Icmp_seq =2 TTL =64 time=0.074 ms 64 bytes from 127.0.0.1: ICmp_seq =3 TTL =64 time=0.079 ms 64 bytes from 127.0.0.1: Icmp_seq =4 TTL =64 time= 0.075ms ^C -- 730.0.1 ping statistics -- 730.4packets transmitted, 730.4packets received 0.0% packet loss round-trip min/avg/ Max /stddev = 0.074/0.081/0.093/0.006msCopy the code

After you remove the network cable, ping 127.0.0.1 can be pinged.

In fact, the first half of the title of this article has been answered here. But we can think a little deeper.

How can you ping 127.0.0.1 even if you are disconnected?

Does that mean you don’t have to pay for Internet access?

Can’t.

First we need to enter the basic science link.

Do not understand the students read to understand, understand to see when the leak to fill a vacancy.

What is a 127.0.0.1

First of all, this is an IPV4 address.

An IPV4 address is a 32-bit address, consisting of eight bits per byte.

The ones starting with 127 are loopback addresses, which are special IPV4 addresses. They are artificially specified.

127.0.0.1 is one of many loopback addresses. Instead of 127.0.0.2, it’s 127.0.0.1 because that’s what the source code defines, and it doesn’t make any sense.

/* Address to loopback in software to local host. */
#define	INADDR_LOOPBACK		0x7f000001	/* 127.0.0.1   */
Copy the code

IPv4 addresses are 32 bits, 2 to the power of 32, which is about 4 billion. There are 7.6 billion people on earth alone, and 4 billion IP is not enough, and in fact it is running out.

So there’s IPV6, which is a 128-bit address, which is about 2 to the 128 ≈10 to the 38. It is said that the amount of sand on earth is about 10 to the 23rd power, so IPV6 IP can be considered inexhaustible.

IPV4 consists of 8-bit groups separated by periods (.).

IPV6 consists of 16-bit groups separated by colons (:). If it’s all zeros, you can omit it.

In IPV4, the loopback address is 127.0.0.1. In IPV6, it is ::1. The reason why there are two colons in an IPV6 address is that only two consecutive colons are allowed in an IPV6 address.

One more thing: ping 127.0.0.1 is used under IPV4. Under IPV6, the ping6 ::1 command is used.

What is the ping

Ping is an application-level command, which can be interpreted as belonging to the same level as a game or chat program. However, chat software can send and receive messages, like and so on, a lot of complicated functions. The function of Ping, as a small software, is relatively simple: it tries to send a small message to the target machine to determine whether the destination machine is reachable. In fact, it is to determine whether the network of the target machine can be connected.

The ping application uses ICMP at the network layer.

Although BOTH ICMP and IP protocols are network layer protocols, ICMP uses IP to transmit messages.

To ping an IP address is to send a message to an IP address.

TCP data sending and ping

Generally, we use TCP for network data transmission, so let’s take a look at the difference between it and Ping.

Ping and other application-layer software belong to the application layer.

So let’s take a horizontal comparison, for example, chat software, if it uses TCP to send messages.

In order to send a message, you have to know where to send it. In Linux, everything is a file, so the destination you want to send a message to is also a file, which leads to the concept of sockets.

To use the socket, you first need to create it.

Socket (AF_INET, SOCK_STREAM, 0) is created in TCP transport. Where AF_INET means that IPV4 host:port will be used to resolve the network address you will enter later. SOCK_STREAM is a TCP protocol that uses byte stream oriented streams and works at the transport layer.

Once the socket is created, you can happily write data to this file. Calling the socket’s sendto interface will move the process from user state to kernel state, and finally call the sock_sendmsg method.

Then it goes to the transport layer with the TCP header. After a series of operations, such as IP header on the network layer and MAC on the data link layer, etc. Enter the ring buffer queue of the network card and send it along the network card.

The socket is created using a socket(AF_INET,SOCK_RAW,IPPROTO_ICMP). SOCK_RAW is a raw socket that works at the network layer. So building ICMP data, which is the Network layer protocol, is perfect. Ping is also called sock_sendmsg method after entering the kernel state. After entering the network layer, ICMP and IP headers are added, and MAC headers are added at the data link layer, which is also sent along the network adapter. So ping is essentially the same as sending a message in a normal application.

This also explains why when you suspect that there is a problem with the network, the first question people ask you is can you ping it? ** Because it can be simply understood as ping is their own group of data packets, so that the system according to the path of other software to send data again, if it can pass, it means that other software sent data can also pass.

How can you ping 127.0.0.1 even if you are disconnected

As mentioned earlier, with a network, ping ends up sending data out over a network card.

So in the case of disconnection, the network card has not worked, but the ping loopback address is normal, we can take a look at the working principle in this case.

From the application layer to the transport layer to the network layer. This section of the path is almost the same as when ping the Internet. At the network layer, the system will obtain the routing information in the routing table according to the destination IP address, which includes the choice of network adapter to send the message.

If the destination IP address is an external IP address, the IP address is sent from the real NETWORK adapter.

If the destination IP address is a loopback address, the system selects the local NIC.

A local nic is actually a “dummy nic”. It doesn’t have a ring buffer like a “real NIC”. The dummy nic pushes data to a linked list called input_pkt_queue. This linked list, which is actually shared by all network cards, holds messages destined for the local machine. When a message is sent to the list, another soft interrupt is triggered **.

A tool for handling soft interrupts is called “ksoftirqd” (this is a kernel thread), which immediately pulls the message out of the linked list as soon as it receives a soft interrupt and passes it up the data link layer, network layer, and so on to the application.

This path is used to ping the loopback address and send data to the loopback address through various protocols, such as TCP. The entire path never goes through the “real card”. ** 127.0.0.1 is called a local loopback address. If a message is sent to this address, it will not go out of the network. ** Therefore, if the network is disconnected, 127.0.0.1 can still be pinged.

What is the difference between ping a loopback address and ping a local address

We execute ifconfig on the MAC.

$ ifconfigLo0: flags = 8049 < UP, LOOPBACK, RUNNING, MULTICAST > mtu 16384 inet 127.0.0.1 netmask 0 xff000000... en0: Flags = 8863 < UP, BROADCAST, SMART, RUNNING, SIMPLEX, MULTICAST > mtu 1500 inet 192.168.31.6 netmask 0 xffffff00 BROADCAST 192.168.31.255...Copy the code

You can see lo0, which represents the local loopback interface, and the corresponding address is 127.0.0.1, also known as the loopback address.

And eth0, the IP address of the first nic on the machine is 192.168.31.6, which is called the local IP.

I used to think that ping the local IP would go out through the “real network card,” meet the first router, and send back to the local.

To verify this statement, you can capture packets, but the results are not the same as the above statement.


You can see that ping the local IP address is the same as ping the loopback address, the relevant network data, all go to lo0, the local loopback interface, also mentioned above **” false network card “**.

As long as the local loopback interface, the data is not sent to the network, the local network protocol stack circle, sent back. Therefore, ping a loopback address is the same as ping a local address.

Is 127.0.0.1 different from localhost and 0.0.0.0

Back to the question in the GIF at the beginning of the article, I am a regular visitor to the interview.

When using nginx for the first time, I found that with these IP addresses, I could normally access the welcome page of nginx. It was once thought that all these IPS were the same.

But there are some fundamental differences.

Localhost is not called an IP address. It is a formal domain name like baidu.com. By default, it will be interpreted as 127.0.0.1, which can be changed in /etc/hosts.

So by default, using localhost is really the same as using 127.0.0.1.

Ping 0.0.0.0 will fail because it represents an invalid destination address in IPV4.

$Ping 0.0.0.0
PING 0.0.0.0 (0.0.0.0): 56 data bytes
ping: sendto: No route to host
ping: sendto: No route to host
Copy the code

But it’s useful, because recall that when we start a server, we listen to an IP and port and wait for the client to connect.

If listen is 0.0.0.0 on the machine, it represents all IPV4 addresses on the machine.

/* Address to accept any incoming messages. */
#Define INADDR_ANY ((unsigned long int) 0x00000000 /* 0.0.0.0 */
Copy the code

Let me give you an example. 127.0.0.1 and 192.168.31.6 are both IPV4 addresses of the local server. If you listen for 0.0.0.0, you can use both addresses to access the server.

Of course, 0.0.0.0 cannot be used when the client connects. You must specify which server IP to connect to.

conclusion

  • 127.0.0.1 is the loopback address. Localhost is the domain name, but the default is 127.0.0.1.

  • Ping loopback address and ping the local address, is the same, go through the network layer and data link layer logic, and finally before the network card is about to make a hard turn, insert data into a linked list on the soft interrupt notification KsoftirQD to receive data logic, the root does not go out of the network. So you can ping the loopback address even if you’re off the network.

  • If the server listen is 0.0.0.0, then the service can be accessed using both 127.0.0.1 and the native address.

The last

Recently, I was too busy with my work. I had black circles under my eyes, and I was even darker. I felt sorry for everyone for so long.

In this article, several large pictures were originally giFs, but the characters were found to be too small when they were moved, and the pictures would not move when they were enlarged. Some of them affect the experience, so I’ll just change it to a static image.

Welcome everyone to add my wechat (the lower right corner of the public number “contact me”), watching each other circle of friends chop a knife what haha.

If the article helps you, look at the bottom right corner of the article and do something positive (click twice) to support it. (Crazy hint, come on, this is really important to me!)

I’m Xiao Bai, and I’ll see you next time.

The resources

127.0.0.1 how much do you know about native Network Communication?! — Recommended to pay attention to Fei Elder brother’s “development of internal strength cultivation”

Article recommendation:

  • Gifs! If the IP layer is fragmented, why is the TCP layer fragmented?

  • Gifs! Why is there a P in the GMP model? The reason behind this is heartwarming

  • I/O timeout, hope you don’t step on the net/ HTTP packet pit

  • Wonderful! The programmer ape’s first guide to Internet slang

  • Programmer’s Guide to Preventing sudden death

  • I feel like I might win the Turing Award…

  • Shame on everyone. After three years of golang, I still can’t get this memory leak question right

  • Hardcore! HTTP interview questions

  • Stick the TCP packet packets: I just made mistakes every packet | hardcore diagram

  • Hardcore illustration! 30 pictures take you to understand! What’s the difference between a router, a hub, a switch, a bridge, and an optical modem?

Stop it. Let’s drown in the ocean of knowledge

[Golang Xiao Bai Growth Story]