Implementation method for allocating fixed IP address based on physical port, router, server and storage medium
By recording the packet source port number in the router and inserting the DHCP Option82 field, the problem of dynamic changes in DHCP allocation IP address is solved, and the device automatically obtains fixed IP, reducing operation and maintenance costs, and is suitable for small and medium-sized enterprises and home networks.
Patent Information
- Application Number
- CN202510730313.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-03
- Publication Date
- 2025-07-01
- Estimated Expiration
- 2045-06-03
AI Technical Summary
The dynamic changes of the IP address assigned by traditional routers through DHCP lead to difficulties in positioning equipment and increasing operation and maintenance costs. Existing solutions such as router stacking methods are cumbersome and costly.
By modifying the network driver to record the packet source port number and inserting the port information field into the DHCP request packet, the binding of the port and the IP address is achieved using DHCP Option82, and the fixed IP address is dynamically allocated.
It realizes that the device automatically obtains a unique fixed IP address, reduces operation and maintenance complexity, supports plug and play, is suitable for small and medium-sized enterprises and home networks, and has low hardware costs.
Smart Images

Figure CN120238520A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer networks, and in particular, to a method for implementing the allocation of a fixed IP address based on a physical port, a router, a server, and a storage medium. Background Art
[0002] Modern network devices (such as cameras and AI terminals) generally have a built-in DHCP Client function and will automatically apply for an IP address from a router after power-on. During production testing, the upper computer needs to establish communication with the devices at specific workstations. However, the IP addresses allocated by the traditional router through DHCP are dynamically changed (for example, 192.168.1.101 may be allocated to workstation A today and to workstation B tomorrow), resulting in the upper computer being unable to locate the device through the IP address.
[0003] Due to the dynamic allocation mechanism of DHCP (four-step interaction: discovery → offer → request → acknowledgment), although it is efficient, it cannot ensure that the device obtains the same IP each time. In large-scale testing, if relying on DHCP, it is necessary to frequently manually check the IP correspondence relationship, increasing the operation and maintenance costs.
[0004] The current main solution is the multi-router stacking scheme: each router sets the DHCP address pool to a single address, that is, only one device can be connected under each router, and then another router is connected on the upper layer and the subordinate router is set for static allocation. This method is cumbersome to set up, difficult to maintain, and costly.
[0005] In view of this, the present application is proposed. Summary of the Invention
[0006] The present invention provides a method for implementing the allocation of a fixed IP address based on a physical port, a router, a server, and a storage medium to solve at least one of the above technical problems.
[0007] A method for implementing the allocation of a fixed IP address based on a physical port includes: A recording step: recording the source port number of a data packet by modifying the function for receiving data packets in a network driver; A detection step: parsing the data packet to determine whether it is a DHCP request packet; A modification step: if it is a DHCP request packet, inserting a custom port information field into the data packet, where the port information field includes the source port number of the data packet; An allocation step: sending the modified DHCP request packet to a DHCP server, so that the DHCP server allocates an IP address according to the source port number in the port information field according to a preset mapping rule between the port number and the IP address.
[0008] Preferably, the port information field is a DHCP Option82 field, and the DHCP Option82 field includes: Port identification sub-field: used to store the source port number of the data packet; Device identification sub-field: used to store the device identifier that sends the data packet.
[0009] Preferably, the function for modifying the function of receiving data packets in the network driver includes: During the process of the network driver receiving the data packet, record the source port number of the data packet by expanding the fields of the data packet buffer.
[0010] Preferably, the data packet comes from a device externally connected to the LAN port, and the external device has a DHCP client.
[0011] Preferably, the preset port number and IP address mapping rule is: The IP address corresponding to port number N is 192.168.1.N + X or 192.168.1.N * Y, where N, X, and Y are natural numbers, and the values of N + X and N * Y are not greater than 255.
[0012] Preferably, the method for modifying the network driver includes: intercepting the data packet receiving function at the operating system kernel layer and injecting port recording logic through a hook function.
[0013] Preferably, it further includes an interface configuration display step: the port and the IP address to be fixed are made into configurable items and presented in the configuration items, and the binding relationship between the port number and the IP address can be manually configured.
[0014] The present invention also provides a router, including: the router includes a plurality of LAN ports, and each LAN port can be externally connected to a device with a DHCP client; And the router can execute the implementation method for allocating a fixed IP address based on a physical port as described above.
[0015] The present invention also provides a server, including: Modification module: insert a custom port information field into the DHCP request packet, and the port information field includes the source port number of the DHCP request packet; Parsing module: parse the port information field in the received DHCP request packet and extract the source port number; Rule library module: store the mapping relationship table between the port number and the IP address; Allocation module: allocate an IP address bound to the port number to the client according to the mapping relationship table; and includes a dynamic update module, and the dynamic update module is used to support the dynamic update of the mapping relationship table, including: Manually configure the binding relationship between the port number and the IP address through the management interface; Or update the binding relationship according to the network topology change through an automated script.
[0016] The present invention can also provide a storage medium storing computer-readable instructions, which, when executed by one or more processors, cause the one or more processors to execute the implementation method of allocating a fixed IP address based on a physical port as described above.
[0017] The implementation method of the present invention for allocating a fixed IP address based on a physical port automatically records the source port number of the data packet through the driver layer and dynamically injects the DHCP Option82 field, enabling the DHCP server to directly associate the physical port with the IP address; users do not need to manually configure network parameters, and the device automatically obtains a unique fixed IP after accessing any LAN physical port; the deployment efficiency is improved, and the operation and maintenance complexity is significantly reduced.
[0018] Based on the mapping and binding rules between the port number and the IP address, the uniqueness of the IP address is guaranteed when multiple devices access in parallel, completely eliminating the risk of IP address conflicts; through the integrated design of driver-level data packet modification and built-in switching functions, the hardware dependence on an independent VLAN switch or an external DHCP server is eliminated.
[0019] Through the innovation of the implementation method, the hardware cost can be controlled within a few hundred yuan, and it supports plug and play, which is especially suitable for low-cost deployment scenarios such as small and medium-sized enterprises and home networks. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 is a schematic diagram of the implementation method of the present invention for allocating a fixed IP address based on a physical port in an embodiment; Figure 2 is a schematic diagram of the scenario when the embodiment of the present invention is used; Figure 3 is Figure 2 the schematic diagram of the structure of the router in Figure 4 is Figure 1 the schematic diagram of some steps of Figure 5 is the schematic diagram of the interface configuration display.
[0021] ICON: 1 - Host computer; 2 - Router; 21 - Chip; 22 - WAN port; 23 - LAN port; 24 - Power supply interface; 3 - Device under test. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0022] To make the objectives, 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 with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are some but not all of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts fall within the scope of protection of the present invention. Therefore, the detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the claimed present invention, but merely represents selected embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts fall within the scope of protection of the present invention.
[0023] The following makes a detailed description of specific embodiments of the present invention with reference to the accompanying drawings. Embodiment 1
[0024] Please refer to Figures 1 to 5 ; The embodiment of the present invention provides a method for implementing the assignment of a fixed IP address based on a physical port. Its application scenario is as follows: A networking mode of a router 2 + a DHCP server is adopted. The router 2 supports multiple LAN ports 23 (such as LAN1 to LAN4), and each port is connected to a DHCP-enabled client device (such as a computer, an IP phone, a camera). The router 2 is connected to the host computer 1 (server) through the WAN port 22. The system realizes the binding of the port number and the IP address by modifying the network driver of the router 2 and the DHCP server logic.
[0025] S1, recording step: By modifying the function for receiving data packets in the network driver, record the source port number of the data packet. The technical implementation can be to mount a hook function (NF_INET_PRE_ROUTING) through the Netfilter framework at the Linux kernel layer of the router 2 to intercept all DHCP data packets passing through the LAN port 23, parse the Ethernet frame header, and extract the source MAC address and the source port number (such as the physical port number of LAN1).
[0026] In this embodiment, the modification of the function for receiving data packets in the network driver includes: during the process of the network driver receiving data packets, record the source port number of the data packet by expanding the fields of the data packet buffer (such as SKB).
[0027] Specifically, when expanding the data packet buffer field, the most important thing is #define SKB_EXT_PORT(skb)(*((u8 *)skb->cb)) / / Define the SKB extension field, and SKB_EXT_PORT(skb) = rx_port; thus writing the port number into the extension field. The rx_port is obtained by reading the network device hardware register, and the port number is stored in the skb data packet received this time. Among them, the port number needs to be obtained by reading the corresponding register according to the data manual of chip 21 of router 2.
[0028] Among them, the data packet comes from an external device connected to LAN port 23. The external device under test 3 has a DHCP client, and the DHCP request packet is sent by the DHCP client of the device under test 3.
[0029] S2, Detection step: Analyze the data packet to determine whether it is a DHCP request packet.
[0030] Please refer to Figure 4 , when it is determined that it is a DHCP request packet, enter the S3 modification step and the S4 allocation step. When it is determined that it is not a DHCP request packet, directly continue to forward it to the upper layer.
[0031] S3 Modification step: Insert a custom port information field into the data packet. The port information field includes the source port number of the data packet; among them, the port information field can adopt different field forms as long as it can carry the source port number of the data packet. It is preferably the DHCP Option82 field. The DHCP Option82 field is an extension option in the DHCP protocol, full name Relay Agent Information Option. Its core function is to enhance the DHCP server's control ability over the client's physical location and security policy.
[0032] The DHCP Option82 field includes: Port identification sub-field: Identify the LAN port where the client is located, and used to store the source port number of the data packet; Device identification sub-field: Used to store the device identifier that sends the data packet, and can be used to uniquely identify the physical device accessed by the client.
[0033] S4, Allocation step: Send the modified DHCP request packet to the DHCP server, so that the DHCP server allocates an IP address according to the source port number in the port information field according to the preset port number and IP address mapping rule.
[0034] Mapping rule: The DHCP server (such as ISC DHCPd) reads the port number N in Option82. Please refer to Figure 5 , in this embodiment, the address is allocated according to the formula IP = 192.168.1.N * Y, where Y is a natural number other than 0, and the value of N * Y is not greater than 255. In this example, Y = 10. For example, when N = 1, that is, the DHCP request packet comes from Ferry 1, the IP address is 192.168.1.10; when N = 2, the IP address is 192.168.1.20.
[0035] In other preferred embodiments, the preset port number and IP address mapping rule may be: the IP address corresponding to the port number N is 192.168.1.N + X, where N and X are natural numbers, and the value of N + X is not greater than 255.
[0036] In this embodiment, dnsmasq is used as the DHCP server, which is convenient for configuring the rule of configuring the IP based on the information in Option82. dnsmasq will respond to the DHCP request packet according to the rule configuration file. Since the router itself has the port MAC binding function, it will automatically send the replied DHCP packet to the source port, so that the device can obtain the expected IP allocation.
[0037] Further, it further includes S5, the interface configuration display step: Please refer to Figure 5 , the port and the IP that needs to be fixed are made into configurable items and displayed in the configuration items, and the binding relationship between the port number and the IP address can be manually configured.
[0038] This embodiment can solve the problem in Figure 2 where A, B, C, D,... refer to the network devices to be tested that need to be fixed with IPs connected under the LAN port 23 of Router 2. Here, after the device to be tested 3 is tested, it will be replaced by the next batch of devices to be tested 3. Therefore, the MAC addresses of the devices connected under the LAN port 23 are not fixed, and the purpose of fixing the IP of the device to be tested 3 cannot be achieved by setting the static binding of IP and MAC in Router 2.
[0039] The implementation method of allocating fixed IP addresses based on physical ports automatically records the source port number of the data packet through the driver layer and dynamically injects the DHCP Option82 field, enabling the DHCP server to directly associate the physical port with the IP address; users do not need to manually configure network parameters, and the device automatically obtains a unique fixed IP after accessing any LAN port 23; the deployment efficiency is improved, and the operation and maintenance complexity is significantly reduced.
[0040] Based on the mapping and binding rules of port numbers and IP addresses, it ensures the uniqueness of IP addresses when multiple devices are connected in parallel, completely eliminating the risk of IP address conflicts; through the integrated design of driver-level data packet modification and built-in switching functions, it eliminates the hardware dependence on independent VLAN switches or external DHCP servers.
[0041] Through the innovation of the implementation method, the hardware cost can be controlled within the hundreds of yuan level, and it supports plug-and-play, which is especially suitable for low-cost deployment scenarios such as small and medium-sized enterprises and home networks. Embodiment 2
[0042] Please refer to Figure 2 and Figure 3 ; This embodiment provides a router 2, which includes 1 WAN port 22 and multiple LAN ports 23. Each LAN port 23 can be externally connected to a DUT 3 with a DHCP client, and the WAN port 22 can be externally connected to a host computer 1; in addition, it also includes a power supply interface 24.
[0043] And the router 2 can execute the implementation method of allocating fixed IP addresses based on physical ports as described in Embodiment 1.
[0044] In this embodiment, a low-cost MT7628 module chip + open-source system OpenWRT is used to develop a router 2 with 4 LAN ports 23 + 1 WAN port 22. If more LAN ports 23 are needed, an 8-port switch chip or other multi-port chips can be added for expansion. Embodiment 3
[0045] The third embodiment of the present invention also provides a server, mainly referring to a DHCP server, which includes: Modifying module: Insert a custom port information field into the DHCP request packet, and the port information field includes the source port number of the DHCP request packet; in this embodiment, the source port number of the DHCP request packet can be recorded by modifying the extended data packet buffer SKB.
[0046] In other preferred embodiments, the modifying module can be integrated into the network driver layer, intercept the DHCP request packet through a hook function (Hook), insert a custom DHCP Option82 field at the data link layer, and write the data packet source port number (such as port 1) into this field.
[0047] Parsing module: Deployed on the DHCP server side, parse the port information field in the received DHCP request packet, and extract the source port number; for example, extract the port number in the Option82 field, such as port 1, port 2, etc.
[0048] Rule library module: Stores the mapping relationship table between port numbers and IP addresses. The pre-stored binding relationship between port numbers and IP addresses in the mapping relationship table can be, for example, port 1 → 192.168.1.10; Allocation module: Queries the rule library according to the parsing result, and allocates the IP address bound to the port number to the client according to the mapping relationship table. For example, it allocates 192.168.1.10 to the client of port 1.
[0049] And includes a dynamic update module, which is used to support the dynamic update of the mapping relationship table, including: Manually configure the binding relationship between port numbers and IP addresses through the management interface; for example, the administrator inputs the binding relationship between port numbers and IP addresses (such as port 2 → 192.168.1.20) through the Web management interface, and the rule library module updates the mapping table in real time.
[0050] Or update the binding relationship according to the network topology change through an automated script. For example, deploy a Python script to monitor the network topology (such as detecting the change of the switch port status through the SNMP protocol). When a new device is detected to be connected to port 3, generate a binding rule (port 3 → 192.168.1.30) automatically and synchronize it to the rule library.
[0051] This server can achieve a strong association between the LAN physical port and the IP through the Option82 field, avoid human errors in traditional dynamic IP configuration, and achieve precise binding. It supports dual-mode updates of manual / auto, adapts to small and medium-sized networks (manual) and the dynamic environment of the Internet of Things (auto), and adopts a lightweight server architecture (such as a Raspberry Pi cluster), with a hardware cost lower than similar solutions in the market. Embodiment 4
[0052] The present invention also provides a storage medium storing computer-readable instructions. When these computer-readable instructions are executed by one or more processors, they can enable one or more processors to execute the implementation method of allocating a fixed IP address based on the physical port described in Embodiment 1. Such a storage medium can be any medium capable of storing computer-readable instructions, such as a hard disk, a solid-state drive, a USB flash drive, an optical disc, etc.
[0053] In several embodiments provided by the embodiments of the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device and method embodiments described above are merely illustrative. For example, the flowcharts in the accompanying drawings show the possible architectures, functions, and operations of devices, methods, and computer program products according to multiple embodiments of the present invention. In this regard, each block in the flowchart or block diagram can represent a module, a program segment, or a part of code, and the module, program segment, or part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks can actually be executed substantially in parallel, and they can sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, as well as the combination of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or actions, or can be implemented by a combination of dedicated hardware and computer instructions.
[0054] In addition, each functional module in various embodiments of the present invention can be integrated together to form an independent part, or each module can exist alone, or two or more modules can be integrated to form an independent part.
[0055] If the described function is implemented in the form of a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, an electronic device, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program code. It should be noted that in this article, the term "including", "comprising", or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article, or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or also includes elements inherent to this process, method, article, or device. Without further limitations, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article, or device including the element.
[0056] The terms used in the embodiments of the present invention are for the purpose of describing specific embodiments only and are not intended to limit the present invention. The singular forms "a", "the" and "said" used in the embodiments of the present invention and the appended claims are also intended to include the plural forms unless the context clearly dictates otherwise.
[0057] It should be understood that the term " / and" used herein is merely a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B may represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " herein generally represents an "or" relationship between the associated objects before and after.
[0058] Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to determining" or "in response to detecting". Similarly, depending on the context, the phrase "if determined" or "if detected (stated condition or event)" may be interpreted as "when determined" or "in response to determining" or "when detected (stated condition or event)" or "in response to detecting (stated condition or event)".
[0059] The "first / second" mentioned in the embodiments is only to distinguish similar objects and does not represent a specific order for the objects. It can be understood that the "first / second" can be interchanged in a specific order or sequence when permitted. It should be understood that the objects distinguished by the "first / second" can be interchanged under appropriate circumstances so that the embodiments described herein can be implemented in an order other than those illustrated or described herein.
[0060] The above are only the preferred embodiments of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions falling within the idea of the present invention belong to the protection scope of the present invention.
Claims
1. An implementation method for allocating a fixed IP address based on a physical port, characterized in that Including: Recording step: By modifying the function for receiving data packets in the network driver, record the source port number of the data packet; Detecting step: Parse the data packet to determine whether it is a DHCP request packet; Modifying step: If it is a DHCP request packet, insert a custom port information field into the data packet, and the port information field includes the source port number of the data packet; Allocating step: Send the modified DHCP request packet to the DHCP server, so that the DHCP server allocates an IP address according to the source port number in the port information field according to the preset mapping rule between the port number and the IP address; 2. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, wherein The port information field is a DHCP Option82 field, and the DHCP Option82 field includes: Port identification sub-field: Used to store the source port number of the data packet; Device identification sub-field: Used to store the device identifier that sends the data packet; 3. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, wherein The function of modifying the function for receiving data packets in the network driver includes: During the process of the network driver receiving the data packet, record the source port number of the data packet by expanding the field of the data packet buffer; 4. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, wherein The data packet comes from a device externally connected to the LAN port, and the external device has a DHCP client; 5. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, characterized in that, The preset mapping rule between the port number and the IP address is: The IP address corresponding to the port number N is 192.168.1.N+X or 192.168.1.N*Y, where N, X, and Y are natural numbers, and the values of N+X and N*Y are not greater than 255; 6. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, characterized in that, The method of modifying the network driver includes: intercepting the data packet receiving function at the operating system kernel layer and injecting port recording logic through a hook function; 7. The implementation method for allocating a fixed IP address based on a physical port according to claim 1, wherein, It also includes an interface configuration display step: The port and the IP address to be fixed are made into configurable items, presented in the configuration items, and the binding relationship between the port number and the IP address can be manually configured; 8. A router, characterized in that, The router includes 1 WAN port and multiple LAN ports, and each LAN port can be externally connected to a device with a DHCP client; And the router can execute the implementation method for allocating a fixed IP address based on the physical port as described in any one of claims 1-7; 9. A server, characterized in that, Including: Modifying module: Insert a custom port information field into the DHCP request packet, and the port information field includes the source port number of the DHCP request packet; Parsing module: Parse the port information field in the received DHCP request packet and extract the source port number; Rule library module: Store the mapping relationship table between the port number and the IP address; Allocating module: According to the mapping relationship table, allocate an IP address bound to the port number to the client; and includes a dynamic update module, and the dynamic update module is used to support the dynamic update of the mapping relationship table, including: Manually configure the binding relationship between the port number and the IP address through the management interface; Or update the binding relationship according to the network topology change through an automated script; 10. A storage medium storing computer-readable instructions, characterized in that, When the computer-readable instructions are executed by one or more processors, the one or more processors execute the implementation method for allocating a fixed IP address based on the physical port as described in any one of claims 1-8.
Citation Information
Patent Citations
IP address allocation method and device
CN106453681A
An MHAL routing method of a multi-bus processor
CN109558367A
An NAT-based load balancing method in a DPDK environment
CN109787912A
Satellite channel high-speed reliable data transmission method based on DPDK
CN110190893A