当前位置:网站首页>How two hosts in different network segments directly connected communicate

How two hosts in different network segments directly connected communicate

2022-06-25 06:23:00 Programmer community

Let's take a look today , How to make two directly connected hosts , Configure different network segments ip After the address , just so so ping through . And what can we learn from it ?

Environment configuration

Prepare two computers , System is Ubuntu Of , Connect their network interfaces with a cable

stay Ubuntu Configure their ip Address :

  • A Host's local network card eth0 To configure 192.168.1.100/24
  • B Host's local network card eth0 To configure 192.168.3.100/24

open B The host wireshark Caught tools , On the local network card eth0 Upper grab bag

Execution steps

 I've drawn all the steps and phenomena into the flow chart above , Shouldn't such a close writer be given a third company ?

First of all, use it under the terminal ifconfig Configure two hosts ip After the address , stay A In the terminal window of the host computer ping B The host ip Address 192.168.3.100, Found that there was no ping through ,A The terminal of the host displays :connect:Network is unreachable

We know :

A Want to send icmp request , First icmp The request message must be a complete message

What is missing in the message sent ?

Purpose MAC Address

therefore A You have to broadcast first ARP request , however B Didn't receive ARP request

A and B It's directly connected , And they don't have routers to isolate broadcast packets , Why? B There's no broadcast ARP What about the request ?

AB Direct connection , There should be no possibility of losing it in the middle of the way ,B I didn't get it again , So there's only one possibility ,A Of ARP It didn't go out at all , Why didn't you send it out ?

Because it didn't find the way to go , It doesn't know the purpose ip by 192.168.3.100 Which network interface should the message be sent from

Why don't you know ? Because it doesn't find a matching routing rule in the routing table . That's why A The host will show :connect:Network is unreachable Why

So the second step

stay A Add a routing rule to the routing table of , issue 192.168.3.0/24 All messages of the network segment should be sent from the network card eth0 Send it out , In this way, a complete message can be sent out from the specified network interface

Execute again at this time ping operation ,A Because I don't know B Of MAC Address , It's going to be broadcast first ARP request

radio broadcast ARP Is the request complete ? Yes

Why? ?

Because of the purpose MAC There are addresses , radio broadcast MAC Address

If you want to send a complete message , To find a matching rule in the routing table , Send it to the purpose ip 192.168.3.100 Can I match the message of ?

Tolerable , Because a rule was added in the previous step , All to 192.168.3.0/24 The message of network segment is sent from network card eth0 Send it out , So the radio ARP The message can be sent out

The hair is sent out , That can reach B The mainframe ?

Because it's direct connection , Certainly.

Just imagine , If A and B It's a router connection , Can reach B Do you ? Definitely not , Why? ? Because routers cut off broadcast messages

host B received A Broadcast of ARP Request message , But there was no reply ARP Respond to , Why? ?

And before A The mainframe can't send out ARP Request the same ,B Want to send ARP Respond to , But no routing rules are found in the routing table , Go to 192.168.1.0/24 Routing rules of network segment

So step four

B The host also needs to add a routing rule , issue 192.168.1.0/24 All messages of the network segment should be sent from the network card eth0 Send it out , In this way, a complete message can be sent out from the specified network interface

here A Host again ping B host , Discovery can ping Through the ,B Grab the package on the host and you can see , First A From the mainframe ARP request ,B host ARP The protocol layer automatically replied ARP Respond to , next A Again icmp request ,B The host icmp The protocol layer replies automatically again icmp Respond to

thus ,A and B Of ip Although the address is different, the network segment , But directly connected, they can still communicate

Let's think about another question :

If the two directly connected hosts ip The address is the same as the network segment , Sure ping through , There is no doubt about it , But there is no manual addition of routing rules , Why can ping through ?

That's because when your host network card is configured ip Address time , The system will automatically put “ This ip And this network card ” Added to the routing table as a rule

summary

  • If you want to send a message , It must be ensured that the message is complete , That is to say, its source ip、 Source mac、 Purpose ip、 Purpose mac There has to be , And then according to its purpose ip Match routing table , Determine which network card to send , With these , The message will be sent out from the network card

  • Messages sent out , How to accurately send to the destination host , It's decided by the switches and routers on the network

  • The broadcast domain of a broadcast message , It does not refer to all hosts in the same network segment as the source host , But as long as there's no router , Can be sent to all other hosts connected to the area , Broadcast domain and ip The address is irrelevant


原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201245251133.html