Method, device and storage medium for automatically adapting DHCP client to DHCP gateway
When obtaining the IP address, the DHCP client checks and calculates the subnet mask and gateway IP information, which solves the problem of service interruption caused by the client IP and gateway IP not being on the same network segment, and achieves the effect of normal forwarding of client messages .
Patent Information
- Application Number
- CN202311648624.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-05
- Publication Date
- 2024-11-29
- Estimated Expiration
- 2043-12-05
AI Technical Summary
When a DHCP client obtains an IP address, if the IP address and gateway IP are not on the same network segment, the client cannot send messages through the gateway, resulting in service interruption.
The DHCP client performs logical calculations to confirm whether the IP address and gateway IP are on the same network segment by checking the subnet mask and gateway IP in the DHCPOffer message. If it is not in the same network segment, the client uses XOR logic calculation to generate the most accurate matching mask, so that the client IP and gateway IP are in the same network segment.
It realizes that DHCP clients can correctly adapt to gateway IP under different network segments to ensure that client packets can be sent normally through the gateway and avoid service interruptions.
Smart Images

Figure CN117527569B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of automatic adaptation of a DHCP client to a DHCP gateway, and in particular to a method, a device and a storage medium for automatic adaptation of a DHCP client to a DHCP gateway. Background Art
[0002] Dynamic Host Configuration Protocol: DHCP (Dynamic Host Configuration Protocol) allows the server to dynamically assign IP addresses and configuration information to clients. If the DHCP client and DHCP server are in the same physical network segment, the client can correctly obtain the dynamically assigned IP address. If they are not in the same physical network segment, a DHCP relay agent is required.
[0003] The role of DHCP Option is to expand the function of DHCP. Because the DHCP protocol not only simply allocates addresses but also implements the function of managing devices, and different application scenarios often give rise to different requirements. In order to meet these customized requirements, this "Option" option that can be customized by users is needed. Because the Option code occupies 1 byte, there are a total of 256 Option values (0 ~ 255, in fact, only 254 values can be used, because 0 and 255 are reserved values). Among them, Option=1 is to set the subnet mask option, and Option=3 is to set the gateway information.
[0004] In the actual use of DHCP clients to obtain dynamic IP from DHCP servers, there has always been a defect: when the IP address obtained by the DHCP client is not in the same network segment as the gateway IP provided by the DHCP server, the DHCP client's message cannot learn the gateway IP's arp, that is, the DHCP client's message cannot be sent out through the gateway. Figure 2 As shown in the figure, the client is assigned an address of 1.1.2.1 / 24, which is not in the same network segment as the relay's 1.1.1.1 / 22. The client cannot ping 1.1.1.1. Summary of the invention
[0005] The present invention provides a method, device and storage medium for automatically adapting a DHCP client to a DHCP gateway, which can at least solve one of the technical problems in the background technology.
[0006] To achieve the above object, the present invention adopts the following technical solutions:
[0007] A method for automatically adapting a DHCP client to a DHCP gateway comprises the following steps:
[0008] S100, the DHCP client checks the DHCPOffer message sent back from the DHCP Server or DHCP Relay;
[0009] S200, the DHCP client checks whether the subnet mask option with Option=1 is carried. If not, the default mask is set to / 32;
[0010] S300, the DHCP client checks whether the gateway IP information of Option=3 is carried. If not, the Server IP or Relay Agent IP in the DHCPOffer message is used as the gateway IP information;
[0011] S400, the DHCP client performs a logical AND calculation on the gateway IP information and the Client_IP based on the subnet mask to confirm whether the gateway IP information and the Client_IP belong to the same network segment;
[0012] If they belong to the same network segment, continue with the subsequent DHCP process;
[0013] When it is confirmed that they do not belong to the same network segment, perform XOR logic calculation on the gateway IP information and Client_IP to calculate the most accurate matching mask and plan them to one network segment; and use the calculated mask information as the final mask information of Client_IP to continue the subsequent DHCP process.
[0014] On the other hand, the present invention further discloses a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor executes the steps of the above method.
[0015] On the other hand, the present invention further discloses a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above method.
[0016] It can be seen from the above technical solution that the method for automatically adapting a DHCP client to a DHCP gateway of the present invention includes the DHCP client checking the Client_IP and the Gateway IP to ensure that the Client_IP and the Gateway IP must be in the same network segment, thereby achieving normal forwarding of the Client_IP through the Gateway IP.
[0017] The beneficial effects achieved by the present invention are:
[0018] Compared with the existing DHCP address allocation process, the present invention can solve the following problems: Figure 2The problem shown is that the Client_IP and the gateway IP are not in the same network segment, resulting in service failure, thereby automatically avoiding possible defects in actual commercial use. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 It is a flowchart of the present invention.
[0020] Figure 2 The defect scene diagram involved in the present invention
[0021] Figure 3 Scene diagram after the present invention solves the defects DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments.
[0023] like Figure 1 As shown, a method for automatically adapting a DHCP client to a DHCP gateway according to an embodiment of the present invention includes the following steps:
[0024] Step 1: The DHCP client checks the DHCPOffer message sent back from the DHCP Server or DHCP Relay;
[0025] Step 2: The DHCP client checks whether it carries the subnet mask option with Option=1. If not, it sets the default mask to / 32.
[0026] Step 3: The DHCP client checks whether the gateway IP information of Option=3 is carried. If not, the Server IP or Relay Agent IP in the DHCPOffer message is used as the gateway IP information;
[0027] Step 4: The DHCP client performs AND logic calculation on the gateway IP information and Client_IP based on the subnet mask to confirm whether the gateway IP information and Client_IP belong to the same network segment. The calculation method is as follows:
[0028] If (Client_IP & Subnet Mask == Gateway IP & Subnet Mask):
[0029] {
[0030] Return True
[0031] }
[0032] Else:
[0033] {
[0034] Return False
[0035] };
[0036] Step 5: If they belong to the same network segment, continue with the subsequent DHCP process;
[0037] Step 6. When it is confirmed that they do not belong to the same network segment, perform XOR logic calculation on the gateway IP information and Client_IP to calculate the most accurate matching mask and plan them to a network segment; and use the calculated mask information as the final mask information of Client_IP to continue the subsequent DHCP process;
[0038] The most exact match mask is calculated as follows:
[0039] Str = Client_IP xor Gateway IP / / Use XOR calculation,
[0040] for i,v in enumerate(Str): / / Poll the XOR result and output the round when the first 1 is found
[0041] if v=1:
[0042] num=i+1
[0043] return num
[0044] Break
[0045] Print 'The current optimal number of mask bits is:', num
[0046] like Figure 3 Example:
[0047] Client_IP=1.1.2.1;Gateway IP=1.1.1.1
[0048] 1.1.2.1 Corresponding binary number: 00000001 00000001 00000010 00000001
[0049] 1.1.1.1 Corresponding binary number: 00000001 00000001 00000001 00000001
[0050] XOR result: 00000000 00000000 000000 11 00000000
[0051] The optimal number of mask bits is: 22 bits;
[0052] The client is eventually assigned an address of 1.1.2.1 / 22, which is in the same network segment as the gateway's 1.1.1.1 / 22. The client can ping 1.1.1.1.
[0053] Client#ping 1.1.1.1
[0054] sending 5,100-byte ICMP echo(es) to 1.1.1.1, timeout is 2 second(s);
[0055] !!!!!
[0056] Success rate is 100 percent(5 / 5), round-trip min / avg / max= 71 / 116 / 293ms;
[0057] [finish].
[0058] In summary, the method for automatically adapting a DHCP client to a DHCP gateway according to an embodiment of the present invention includes the DHCP client checking the Client_IP and the Gateway IP to ensure that the Client_IP and the Gateway IP must be in the same network segment, thereby achieving normal forwarding of the Client_IP through the Gateway IP.
[0059] On the other hand, the present invention further discloses a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor executes the steps of the above method.
[0060] On the other hand, the present invention further discloses a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above method.
[0061] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute any method for automatically adapting a DHCP client to a DHCP gateway in the above embodiments.
[0062] It is understandable that the system provided by the embodiment of the present invention corresponds to the method provided by the embodiment of the present invention, and the explanation, examples and beneficial effects of the relevant contents can refer to the corresponding parts in the above method.
[0063] The embodiment of the present application also provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus.
[0064] Memory, used to store computer programs;
[0065] The processor is used to implement the above-mentioned method of automatically adapting the DHCP client to the DHCP gateway when executing the program stored in the memory.
[0066] The communication bus mentioned in the above electronic device may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus may be divided into an address bus, a data bus, a control bus, etc.
[0067] The communication interface is used for communication between the above electronic device and other devices.
[0068] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0069] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.
[0070] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0071] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0072] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0073] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for automatically adapting a DHCP client to a DHCP gateway, characterized in that: The steps include: S100, the DHCP client checks the DHCP Offer message sent back from the DHCP Server or DHCP Relay; S200, the DHCP client checks whether the subnet mask option with Option=1 is carried. If not, the default mask is set to / 32; S300, the DHCP client checks whether the gateway IP information of Option=3 is carried. If not, the DHCP Server IP or DHCP Relay IP in the DHCP Offer message is used as the gateway IP information; S400, the DHCP client performs a logical AND calculation on the gateway IP information and the Client_IP based on the subnet mask to confirm whether the gateway IP information and the Client_IP belong to the same network segment, specifically including performing a logical AND calculation on the gateway IP information and the subnet mask, performing a logical AND calculation on the Client_IP information and the subnet mask, and calculating whether the two are equal, if they are equal, confirming that the gateway IP information and the Client_IP belong to the same network segment, if they are not equal, confirming that the gateway IP information and the Client_IP do not belong to the same network segment; If they belong to the same network segment, continue with the subsequent DHCP process; When it is confirmed that they do not belong to the same network segment, perform XOR logic calculation on the gateway IP information and Client_IP to calculate the most accurate matching mask and plan them to a network segment; and use the calculated mask information as the final mask information of Client_IP to continue the subsequent DHCP process, including converting the gateway IP information and Client_IP into binary and performing XOR logic calculation, calculating the number of zeros in front of the first 1 when finding the binary result of the XOR calculation, and using the number of bits as the optimal mask, and using the calculated mask information as the final mask information of Client_IP and the gateway.
2. A computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor executes the steps of the method according to claim 1.
3. A computer device comprising a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the method according to claim 1.
Citation Information
Patent Citations
IP address management method under multi-DHCP server network environment and DHCP client
CN105847464A
Network access method, router, electronic equipment and computer readable medium
CN113472907A
