IP address query method and device

By constructing the IP set as a prefix tree structure, the problems of IP set storage space and query time are solved, and an efficient IP query method is realized.

CN119322872BActive Publication Date: 2026-02-03BEIJING CHAITIN TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411232139.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-04
Publication Date
2026-02-03
Estimated Expiration
2044-09-04

AI Technical Summary

Technical Problem

Existing technologies require excessive storage space for IP collections and have excessively long query times, which cannot meet general business needs.

Method used

The IP set structure is constructed into a prefix tree structure. The prefix tree results are used to represent IPs with common prefixes, saving storage space. Furthermore, query time is reduced by comparing the byte array with the node range one by one.

Benefits of technology

The prefix tree structure saves storage space, improves query efficiency, reduces comparison time, and enables fast IP lookup.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119322872B_ABST
    Figure CN119322872B_ABST
Patent Text Reader

Abstract

The embodiment of the present application discloses an IP address query method and device, the method comprises the following steps: converting an IP address to be queried into a byte array, judging a node state in a preset IP set structure, if the node state is full inclusion, directly judging that the IP address to be queried belongs to the preset IP set structure, if the node state is no inclusion, directly judging that the IP address to be queried does not belong to the preset IP set structure, if the node state is half inclusion, continuing to judge a child node according to the above mode until a leaf node of the last layer, judging whether all elements in the byte array are in an IP point set represented by a mask of the leaf node, if yes, judging that the IP address to be queried belongs to the preset IP set structure, otherwise, judging that the IP address to be queried does not belong to the preset IP set structure. The IP set structure provided by the present application can save a large amount of storage space, and can return a result in advance according to specific conditions in the query process, without judging a complete IP path, so that the query time can be shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing technology, and in particular to an IP address lookup method and apparatus. Background Technology

[0002] An IP set is a structure that groups together many IP addresses for easy lookup. Currently, there are two versions of the IP protocol: IPv4 and IPv6. IPv4 can represent 2^6 IP addresses. 32 There are over 4 billion IPv6 instances, and IPv6 can represent 2... 128 One IP address, approximately 3.4 x 10 38 IPv6 is compatible with all aspects of IPv4. IP sets, as a commonly used structure, provide IP lookup and are frequently used for functions such as IP access statistics and IP blocking management, making them an important data representation structure.

[0003] Currently, the commonly used methods for representing IP sets are database storage and in-memory dictionaries. Database storage requires querying the database each time a result is used, resulting in low query efficiency; ideally, a single query takes around 1ms, which is not conducive to fast lookups. While in-memory dictionaries offer fast lookups, they consume a lot of memory; theoretically, 1GB of memory can only represent a maximum of 260 million IPs. Therefore, both methods have drawbacks. If a set representing all IPv4 addresses is required, neither of these structures can meet the needs of typical business applications in terms of either storage space or query efficiency. Summary of the Invention

[0004] This invention aims to solve at least one of the technical problems existing in the prior art. To this end, this invention proposes an IP address query method and apparatus to address the technical problems of excessive storage space occupied and excessively long query time in existing IP address sets.

[0005] In a first aspect, the present invention provides an IP address lookup method, the method comprising:

[0006] Convert the IP address to be queried into a byte array;

[0007] Each element in the byte array is compared with a node in a preset IP set structure to determine whether the element is within the range of the node. The preset IP set structure is constructed as a prefix tree structure, and each node in the prefix tree structure shares the prefix of its parent node. The nodes in the prefix tree structure include range nodes.

[0008] If all elements are within the range of the node, then the IP address to be queried is determined to belong to the preset IP set structure.

[0009] In this embodiment, on the one hand, by constructing the IP set structure into a prefix tree structure, IPs with a common prefix can be expressed using the prefix tree result, saving a lot of storage space. On the other hand, in the process of comparing each element in the byte array with the node in the preset IP set structure, since the determination is whether the element is within the range of the node, a lot of comparison time can be saved, thereby greatly reducing query time and improving query efficiency.

[0010] In one technical solution of the above-mentioned IP address lookup method, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node includes:

[0011] Determine whether the node being compared is in a fully inclusive state. If the node being compared is in a fully inclusive state, then directly determine that the IP address to be queried belongs to the preset IP set structure.

[0012] Through the embodiments of this application, since the node in the fully contained state includes all child nodes under the current node's path prefix, it can be directly determined that the IP address to be queried belongs to the preset IP set, without the need to compare the next element or node, thus further reducing query time and improving query efficiency.

[0013] In one technical solution of the above-mentioned IP address query method, the node includes at least leaf nodes and non-leaf nodes.

[0014] In one technical solution of the above IP address lookup method, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node further includes:

[0015] If the node being compared is not in a fully contained state, then determine whether the node being compared is a leaf node.

[0016] If the node being compared is a leaf node, then the mask of the leaf node is used to determine whether the leaf node contains the element being compared.

[0017] If it is determined that the leaf node contains the element currently being compared, then the IP address to be queried belongs to the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure.

[0018] In one technical solution of the above IP address lookup method, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node further includes:

[0019] If the node being compared is a non-leaf node, then determine whether the child nodes of the node being compared contain the element being compared.

[0020] If the child node of the node currently being compared contains the element currently being compared, then the next element in the byte array is compared with the node in the preset IP set structure; otherwise, it is directly determined that the IP address to be queried does not belong to the preset IP set structure.

[0021] In one technical solution of the above IP address lookup method, determining whether the leaf node contains the element currently being compared using the leaf node mask includes:

[0022] Perform an AND operation between the mask of the leaf node and the element at the preset position of the byte array. If the result is greater than 0, then the leaf node is determined to contain the element currently being compared.

[0023] In one technical solution of the above IP address query method, the leaf node is a two-level node. The first level of the leaf node indicates that the current node is a leaf node, and the second level of the leaf node records whether the IP address of the current node is in the set.

[0024] In one technical solution of the above IP address lookup method, the non-leaf node is a single-layer node.

[0025] In one technical solution of the above-mentioned IP address lookup method, the IP address structure in the prefix tree structure is a two-dimensional triangular structure.

[0026] In one technical solution of the above-mentioned IP address lookup method, a single IP address is represented as a line in the two-dimensional triangle structure.

[0027] In one technical solution of the above-mentioned IP address lookup method, the IP range is represented as a triangle in the two-dimensional triangle structure.

[0028] In a second aspect, the present invention provides an IP address lookup device, the device comprising:

[0029] The conversion module is used to convert the IP address to be queried into a byte array;

[0030] The comparison module is used to compare each element in the byte array with the nodes in the preset IP set structure to determine whether the element is within the range of the node. The preset IP set structure is constructed as a prefix tree structure, and each node in the prefix tree structure shares the prefix of the parent node. The nodes in the prefix tree structure include range nodes.

[0031] The judgment module is used to determine that the IP address to be queried belongs to the preset IP set structure if all elements are within the range of the node.

[0032] In a third aspect, the present invention provides an electronic device including a memory and a processor, wherein the memory stores a computer program executable on the processor, and when the computer program is executed by the processor, implements the IP address lookup method as described in any of the first aspects.

[0033] In a fourth aspect, the present invention provides a computer-readable storage medium storing a computer program, characterized in that, when the computer program is executed, it implements the IP address lookup method as described in any of the first aspects.

[0034] The above-described technical solutions of the present invention have at least one or more of the following beneficial effects:

[0035] In implementing the technical solution of the present invention, by constructing the IP set structure into a prefix tree structure, IPs with a common prefix can be expressed using the prefix tree result, saving a lot of storage space. At the same time, during the query process, when comparing each element in the byte array with the node in the preset IP set structure, since the determination is whether the element is within the range of the node, a lot of comparison time can be saved, thereby greatly reducing the query time and improving the query efficiency.

[0036] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0037] The disclosure of this invention will become more readily understood with reference to the accompanying drawings. It will be readily understood by those skilled in the art that these drawings are for illustrative purposes only and are not intended to limit the scope of protection of this invention. Furthermore, similar numbers in the drawings are used to denote similar components, wherein:

[0038] Figure 1 This is a diagram illustrating IPv6 data provided by existing technologies;

[0039] Figure 2This is a schematic diagram of the structure of IPv6 data in the IP set structure provided in Embodiment 1 of this application;

[0040] Figure 3 This is a schematic diagram of a portion of the IPv6 data in the IP set structure provided in Embodiment 1 of this application;

[0041] Figure 4 This is a schematic diagram of another structure of some IPv6 data in the IP set structure provided in Embodiment 1 of this application;

[0042] Figure 5 This is a schematic diagram of another structure of some IPv6 data in the IP set structure provided in Embodiment 1 of this application;

[0043] Figure 6 This is a schematic diagram of the leaf node structure in the IP set structure provided in Embodiment 1 of this application;

[0044] Figure 7 This is a flowchart of the IP address query method provided in Embodiment 2 of this application;

[0045] Figure 8 This is a schematic diagram of the IP address query device provided in Embodiment 3 of this application;

[0046] Figure 9 This is a schematic diagram of the structure of the electronic device provided in Embodiment 4 of this application. Detailed Implementation

[0047] Some embodiments of the present invention will now be described with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are merely illustrative of the technical principles of the present invention and are not intended to limit the scope of protection of the present invention.

[0048] As described in the background section, the existing representation structures of IP sets either occupy a large amount of storage space or have a long query time. If a set representing all IPv4 is required, the existing representation structures of IP sets cannot meet the needs of general business in terms of both storage space and query efficiency.

[0049] Based on this, this application proposes a new IP set structure and a corresponding IP address query method. By constructing the IP set structure into a prefix tree structure, IPs with common prefixes can be expressed using the prefix tree results. This maximizes data sharing, saves a significant amount of storage space, and further reduces query time and improves query efficiency by comparing each element in the byte array with the nodes in the preset IP set structure during subsequent IP address queries, since the determination is based on whether the element is within the range of the node.

[0050] Example 1

[0051] Figure 1 This is a diagram illustrating IPv6 data provided by existing technology. (Refer to...) Figure 1 As shown, IPv6 data can be viewed as one-dimensional data [0, 2^128], with a total storage space of 2. 128 * 128 bits. Since the smallest storage space in a computer is a byte, and one byte represents 8 bits, one IPv6 data requires 16 bytes to represent. Often, many of these IPs represented by bytes share a common prefix. Based on this, this application creatively proposes using a prefix tree structure to express the IP set structure. Each node in the prefix tree structure shares the prefix of its parent node, and all nodes in the prefix tree structure are range nodes, thereby saving storage space.

[0052] It should be noted that in this embodiment, the range node refers to a node representing a range, rather than a single numerical value represented by a conventional node. Therefore, during the query process, the node status of the IP set structure can be directly determined, instead of comparing one byte at a time as in existing technologies.

[0053] Reference Figure 2 As shown, in the IP set structure provided in this application, the IPv6 structure becomes a two-dimensional triangle. Further refer to... Figure 2 As shown, the base of the triangle is 2. 128 The height is 128 bits, and the storage space is 2 according to the triangle area formula. 128 * 64 bits, reducing space by 50% compared to a one-dimensional data structure. In the prefix tree structure, a single IP address is represented as a line in the IP triangle, while an IP range can be represented as a smaller triangle.

[0054] It is understood that, in the embodiments of this application, the prefix tree structure can be divided into three configurations based on its shape:

[0055] 1. Line type, refer to Figure 3 As shown, this represents a unique IP address;

[0056] 2. A triangle with a fixed vertex and the same root node, refer to... Figure 4 As shown, this represents the IP range;

[0057] 3. A triangle whose fixed point is different from the root node, refer to... Figure 5 As shown, this also represents the IP range.

[0058] The following examples illustrate how different IPs are expressed in the IP set structure provided in the embodiments of this application.

[0059] There are generally three ways to express an IP address:

[0060] 1) A single IP address, for example: 192.168.0.1

[0061] 2) CIDR format, for example: 192.168.0.0 / 24

[0062] 3) IP range, for example: 1.1.1.1-2.2.2.2, the range is an open interval including the left and right boundaries.

[0063] The three IP representation methods mentioned above need to be processed separately to map them into the IP collection structure:

[0064] 1) For a single IP address, in an IP set structure (such as...) Figure 2 The triangle shown is represented using a line type;

[0065] 2) For CIDR format, first convert it to IP range representation. For example, 192.168.0.0 / 24 can be converted to 192.168.0.0-192.168.0.255. Then, determine whether the starting and ending IP addresses have the same address prefix. If the starting and ending IP addresses do not have the same address prefix, use the configuration of "a triangle with the same vertices as the root node". If they have the same address prefix, use the configuration of "a triangle with different vertices from the root node".

[0066] 3) For IP ranges, if the starting and ending IP addresses do not have the same address prefix, the configuration of "a triangle with the same vertices as the root node" is used; if they have the same address prefix, the configuration of "a triangle with different vertices from the root node" is used.

[0067] In summary, the above structure allows for easy checking of whether an IP address already exists in the set. A single IP address can be visually represented by a "line" formed by a path. If this "line" coincides with a line in the set, or lies within a triangle, then the current IP address exists in the set; otherwise, the IP address does not exist in the set.

[0068] As a preferred implementation, in this embodiment, the nodes of the prefix tree structure typically contain two attributes: state and children. The state contains a byte-type data, using a mask to express different meanings; the children contain a map-type data, pointing to the child nodes of the current node. The value of the map is also a node type, and the key of the map is a byte type. Therefore, a node can contain a maximum of 256 child nodes.

[0069] In a preferred embodiment, the nodes in this application include at least two types: leaf nodes and non-leaf nodes.

[0070] It is understood that in the embodiments of this application, leaf nodes and non-leaf nodes have the same structure, but the data has different meanings.

[0071] For non-leaf nodes, the 8 bits in the state field represent different meanings, as shown in Table 1 below:

[0072] Table 1

[0073] bit (from left to right) Meaning 0th position Is it a leaf node? If yes, return 1; otherwise, return 0. 1st place Whether to include all child nodes under the current node's path prefix (full inclusion state): 1 for yes, 0 for no. A value of 1 indicates that all child nodes are included, and `children` can be omitted and left empty. 2nd to 4th place Reserved bits 5th to 7th place The prefix of a child node can be represented by n, with a minimum value of 0 and a maximum value of 7, indicating that there are 2n+1 nodes, that is, a minimum of 2 nodes and a maximum of 256 nodes.

[0074] As is understood, in this embodiment, `children` is a dictionary structure representing all child nodes of the current node. If `children` is empty, it means that the current node has no child nodes. The maximum number of `children` is determined by the mask in the `state` field (bits 5 to 7), with a minimum of 2 and a maximum of 256. The dictionary key for `children` is of type byte, with a maximum value of 256.

[0075] For leaf nodes, due to the special structure of the IP set provided in this embodiment, the leaf nodes in this embodiment are composed of two layers of nodes. The first layer of the leaf node indicates that it is currently a leaf node, and the second layer records whether the current node's IP addresses (up to 256) are in the set. The 8 bits in the state field of the first layer of the leaf node represent the following meanings as shown in Table 2:

[0076] Table 2

[0077] bit (from left to right) Meaning 0th position Leaf nodes are fixed at 1. 1st to 4th Reserved bits 5th to 7th place <![CDATA[There are 16 second-layer child nodes, and the state of each node has 8 bits, which can exactly represent 256 IPs. Therefore, the 5th to 7th bits have fixed values of 100, indicating that there are 2 4 = 16 child nodes]]>

[0078] The 8 bits in the `state` field of the second level of the leaf node correspond to the IP address bits contained in the current prefix space, with 1 indicating inclusion and 0 indicating exclusion. The two-level structure of the leaf node is as follows: Figure 6 As shown.

[0079] It should be noted that during the process of checking whether an IP address is in the IP set structure provided in this application embodiment, if a child node does not exist during the segment-by-segment search, it indicates that the current IP address is not in the set. If a non-leaf node is found and is marked as containing all child nodes, it indicates that the IP address exists in the current set. If the search continues until a leaf node is reached, the existence of the IP address in the current set is determined based on the mask of the second-level nodes within the leaf node. This significantly reduces query time and improves query efficiency.

[0080] Example 2

[0081] Corresponding to Embodiment 1 above, this application also provides an IP address query method, which is particularly suitable for querying the existence of an IP address within the IP set structure provided in Embodiment 1. In this embodiment, content that is the same as or similar to that in Embodiment 1 can be referred to the above description and will not be repeated hereafter. (Refer to...) Figure 7 As shown, the method includes the following steps:

[0082] S110: Convert the IP address to be queried into a byte array;

[0083] S120: Compare each element in the byte array with the nodes in the preset IP set structure to determine whether the element is within the range of the node;

[0084] S130: If the element is within the range of the node, then the IP address to be queried is determined to belong to the preset IP set structure;

[0085] The preset IP set structure includes the IP set structure as described in any one of Embodiment 1, that is, the preset IP set structure is constructed as a prefix tree structure, each node in the prefix tree structure shares the prefix of the parent node, and the nodes in the prefix tree structure include range nodes.

[0086] It is understandable that since the default IP set structure uses bytes to represent IPs, when querying whether the IP address to be queried belongs to the default IP set structure, the IP address to be queried must first be converted into a byte array format. The specific conversion process can be found in relevant existing technologies, which will not be elaborated here.

[0087] Understandably, since the nodes in the preset IP set structure are all range nodes, when comparing each element in the byte array with the nodes in the preset IP set structure, it is only necessary to determine whether the element is within the range of the node, rather than comparing each element with a single character as in the traditional IP address lookup method. Therefore, a lot of comparison time can be saved, thereby greatly reducing the query time and improving the query efficiency.

[0088] It is understandable that when all elements in the byte array are within the range of the node, it means that the IP set structure contains all elements in the byte array. In this case, it means that the IP address to be queried belongs to the preset IP set.

[0089] As a preferred implementation, in this embodiment of the application, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node includes:

[0090] Determine whether the node being compared is in a fully inclusive state. If the node being compared is in a fully inclusive state, then directly determine that the IP address to be queried belongs to the preset IP set structure.

[0091] It is understood that in this embodiment, the IP node state has three types: fully inclusive, partially inclusive, and non-inclusive. When a node is in a fully inclusive state, it means that the node contains all child nodes under the current node's path prefix. Therefore, when it is determined that the current node being compared with the current element is in a fully inclusive state, it can be directly determined that the IP address to be queried belongs to the preset IP set structure, without needing to compare the next element or node. This further reduces query time and improves query efficiency. When a node is in a non-inclusive state, it can be directly determined that the IP address to be queried does not belong to the preset IP set structure. When a node is in a partially inclusive state, the child nodes are further evaluated until the end. In summary, the IP address query method provided in this embodiment can return results in advance according to specific circumstances without needing to judge the complete IP path, thus shortening query time and improving query efficiency.

[0092] In a preferred embodiment of this application, the node includes at least leaf nodes and non-leaf nodes.

[0093] It is understood that in the embodiments of this application, leaf nodes and non-leaf nodes have the same structure, but the data has different meanings. The relevant content is described in Embodiment 1 and will not be repeated here.

[0094] As a preferred implementation, in this embodiment of the application, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node further includes:

[0095] If the node being compared is not in a fully contained state, then determine whether the node being compared is a leaf node.

[0096] If the node being compared is a leaf node, then the mask of the leaf node is used to determine whether the leaf node contains the element being compared.

[0097] If it is determined that the leaf node contains the element currently being compared, then the IP address to be queried belongs to the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure.

[0098] Specifically, when it is determined that the node being compared is not in a fully contained state, it is necessary to first determine whether the node is a leaf node or a non-leaf node. For leaf nodes, the leaf node mask can be used to determine whether the leaf node contains the element being compared. It can be understood that if the leaf node contains the element being compared, it means that the IP address to be queried belongs to the preset IP set structure; otherwise, if the leaf node does not contain the element being compared, it means that the IP address to be queried does not belong to the preset IP set structure.

[0099] As a preferred implementation, in this embodiment of the application, determining whether a leaf node contains the element currently being compared using the leaf node's mask includes:

[0100] Perform an AND operation between the mask of the leaf node and the element at the preset position of the byte array. If the result is greater than 0, then the leaf node is determined to contain the element currently being compared.

[0101] It should be noted that the preset position is not specifically limited in the embodiments of this application. Users can set it according to actual needs without violating the inventive concept of this application. In some specific embodiments, the state field (i.e., mask) of the second level of the leaf node can be ANDed with the last byte of the IP to be queried, and the result can be judged to be greater than 0. If the result of the mask calculation is greater than 0, it means that the IP address to be queried belongs to the preset IP set structure.

[0102] As a preferred implementation, in this embodiment of the application, the step of comparing each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node further includes:

[0103] If the node being compared is a non-leaf node, then determine whether the child nodes of the node being compared contain the element being compared.

[0104] If the child node of the node currently being compared contains the element currently being compared, then the next element in the byte array is compared with the node of the preset IP set structure; otherwise, it is directly determined that the IP address to be queried does not belong to the preset IP set structure.

[0105] Specifically, for non-leaf nodes, it can be determined whether the child nodes of the non-leaf node contain the element being compared by comparing the element currently being compared with the child nodes of the non-leaf node.

[0106] Understandably, when it is determined that the child node of the non-leaf node contains the element currently being compared, it means that the element is in the preset IP set structure. At this time, the above comparison process needs to be iterated, that is, the next element in the above byte array is compared with the node of the preset IP set structure to determine whether the next element is in the preset IP set. If all elements in the above byte array are in the preset IP set structure, it means that the IP address to be queried belongs to the preset IP set structure. If, during the comparison process, even one element is found not to be in the preset IP set structure, it means that the IP address to be queried does not belong to the preset IP set structure.

[0107] It is understandable that when it is determined that the child nodes of the non-leaf node do not contain the element currently being compared, it can be directly determined that the IP address to be queried does not belong to the preset IP set structure.

[0108] In a preferred embodiment of this application, the non-leaf nodes are single-layer nodes.

[0109] Specifically, for non-leaf nodes, the 8 bits in the state field represent different meanings, as detailed in the relevant content of Example 1, which will not be repeated here.

[0110] As a preferred implementation, in this embodiment of the application, the leaf node is a two-level node. The first level of the leaf node indicates that the current node is a leaf node, and the second level of the leaf node records whether the IP address of the current node is in the set.

[0111] Specifically, due to the special structure of the IP set provided in this application embodiment, the leaf nodes in this application embodiment are composed of two layers of nodes. The first layer of the leaf node indicates that it is currently a leaf node, and the second layer records whether the IP addresses (up to 256) of the current node are in the set. The 8 bits in the state field of the first layer of the leaf node represent the following from left to right: Bit 0: Leaf node, fixed at 1; Bits 1-4: Reserved bits; Bits 5-7: There are 16 child nodes in the second layer, and each node's state has 8 bits, representing exactly 256 IP addresses. Therefore, bits 5-7 are fixed at 100, indicating that there are 2... 4 = 16 child nodes. The 8 bits in the state field of the second level of the leaf node correspond to the IP points contained in the current prefix space, with 1 indicating inclusion and 0 indicating exclusion.

[0112] As a preferred implementation, in this embodiment of the application, the IP address structure in the prefix tree structure is a two-dimensional triangular structure.

[0113] In some specific embodiments, a single IP is represented as a line in the two-dimensional triangular structure.

[0114] In some specific embodiments, the IP range is represented as a triangle in the two-dimensional triangular structure.

[0115] Specifically, as described in Embodiment 1, since the IP set structure in this application is expressed using a prefix tree, and each node in the prefix tree structure shares the prefix of the parent node, the IPv6 structure (IP address) in the IP set structure provided in this application becomes a two-dimensional triangle.

[0116] The following example illustrates the IP address query method provided in the embodiments of this application.

[0117] Assuming the IP address to be queried is 192.168.0.1, first convert it to IPv6 byte format: [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0xc0a8, 0x0001]

[0118] Secondly, follow the steps in Table 3 below to check whether the IP address is in the preset IP set.

[0119] Table 3

[0120] step Node (byte represented by binary label) Judgment conditions Query results 1 root node {state:[0000 0111]children: {[00000000]: child nodes}} Determine if it is a fully contained state: No. Determine if it is a leaf node: No. Determine if the `children` list contains child nodes whose key is the first byte of the IP address being queried: Yes. The path exists; continue searching. 2 First-level child nodes {state: [00000111]children:{[0000 0000]: child nodes}} Determine if it is a fully contained state: No. Determine if it is a leaf node: No. Determine if the `children` list contains child nodes whose key is the first byte of the IP address being queried: Yes. The path exists; continue searching. 3 ~14 Repeat the above steps until a leaf node is reached. The path exists; continue searching. 15 Leaf nodes, first level {state: [10000100]children:{[0000 0000]: second level}} Determine if it is a leaf node: Yes. Determine if the child node exists at the second level: Yes. The path exists; continue searching. 16 Leaf node, second level {state: [00000001]children: empty} The state (one byte) is ANDed with the last byte of the IP address to be evaluated. The result is then checked to see if it is greater than 0: if yes. If the result of the mask calculation is greater than 0, the IP address exists in the set.

[0121] As shown in Table 3 above, this query function used 16 node checks, and it is independent of the size of the set. That is, regardless of the size of the set, the result can be returned after a maximum of 16 node checks.

[0122] Assuming the IP address to be queried is 36.192.1.23, first convert it to IPv6 byte format: [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x24c0, 0x0117]

[0123] Next, follow the steps in Table 4 below to check whether the IP address is in the preset IP set.

[0124] Table 4

[0125] step Node (byte represented by binary label) Judgment conditions Query results 1~12 Prefix determination for IPv6 with omitted prefixes The path exists; continue searching. 13 {state: [00000111]children:{[0010 0100]: child nodes}} 1. Check if it's a fully contained state: No. 2. Check if it's a leaf node: No. 3. Check if the `children` list contains child nodes whose key is the first byte of the IP address being queried: Yes. The path exists; continue searching. 14 {state: [00000010]children:{[0000 0010]: child nodes,}} Note: The prefix in state is "0010", which is 2. Therefore, the current node can have a maximum of 2² = 4 child nodes, and the key range of the child nodes can only be "00000000", "00000001", "00000010", and "00000011". 1. Check if it's a fully contained state: No. 2. Check if it's a leaf node: No. 3. Check if the `children` list contains child nodes whose key is the first byte of the IP address being queried: Yes. The path exists; continue searching. 15 {state: [01000110]children:empty} Note: The prefix of the parent node is 2. In order to ensure that the number of grandchild nodes is the most efficient 256, the prefix of the child nodes needs to be 8 - 2 = 6. Determine if it is a fully contained state: Yes The current node is in a "fully contained" state, so the result can be "contained".

[0126] During the above query process, since a "fully inclusive" node was encountered, it is not necessary to complete all 16 queries to obtain the query results in advance, which further reduces the query time.

[0127] Example 3

[0128] Corresponding to Embodiment 2 above, this application also provides an IP address query device 200, which is used to implement the IP address query method provided in any of Embodiment 2. In this embodiment, content that is the same as or similar to that in Embodiment 2 above can be referred to the above description and will not be repeated hereafter. (Refer to...) Figure 8 As shown, the device includes:

[0129] The conversion module 210 is used to convert the IP address to be queried into a byte array;

[0130] The comparison module 220 is used to compare each element in the byte array with the nodes in the preset IP set structure to determine whether the element is within the range of the node. The preset IP set structure is constructed as a prefix tree structure, and each node in the prefix tree structure shares the prefix of the parent node. The nodes in the prefix tree structure include range nodes.

[0131] The judgment module 230 is used to determine that the IP address to be queried belongs to the preset IP set structure if all elements are within the range of the node.

[0132] As a preferred implementation, in this embodiment of the application, the comparison module is specifically used for:

[0133] Determine whether the node being compared is in a fully inclusive state. If the node being compared is in a fully inclusive state, then directly determine that the IP address to be queried belongs to the preset IP set structure.

[0134] In a preferred embodiment of this application, the node includes at least leaf nodes and non-leaf nodes.

[0135] As a preferred implementation, in this embodiment of the application, the comparison module is specifically used for:

[0136] If the node being compared is not in a fully contained state, then determine whether the node being compared is a leaf node.

[0137] If the node being compared is a leaf node, then the mask of the leaf node is used to determine whether the leaf node contains the element being compared.

[0138] If it is determined that the leaf node contains the element currently being compared, then the IP address to be queried belongs to the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure.

[0139] As a preferred implementation, in this embodiment of the application, the comparison module is specifically used for:

[0140] If the node being compared is a non-leaf node, then determine whether the child nodes of the node being compared contain the element being compared.

[0141] If the child node of the node currently being compared contains the element currently being compared, then the next element in the byte array is compared with the node in the preset IP set structure; otherwise, it is directly determined that the IP address to be queried does not belong to the preset IP set structure.

[0142] As a preferred implementation, in this embodiment of the application, the comparison module is specifically used for:

[0143] Perform an AND operation between the mask of the leaf node and the element at the preset position of the byte array. If the result is greater than 0, then the leaf node is determined to contain the element currently being compared.

[0144] As a preferred implementation, in this embodiment of the application, the leaf node is a two-level node. The first level of the leaf node indicates that the current node is a leaf node, and the second level of the leaf node records whether the IP address of the current node is in the set.

[0145] In a preferred embodiment of this application, the non-leaf nodes are single-layer nodes.

[0146] As a preferred implementation, in this embodiment of the application, the IP address structure in the prefix tree structure is a two-dimensional triangular structure.

[0147] In a preferred embodiment of this application, a single IP is represented as a line in the two-dimensional triangular structure.

[0148] In a preferred embodiment of this application, the IP range is represented as a triangle in the two-dimensional triangular structure.

[0149] Example 4

[0150] Corresponding to Embodiment 2 above, this application also provides an electronic device, including: a processor and a memory, wherein the memory stores a computer program that can run on the processor, and when the computer program is executed by the processor, it executes the IP address query method provided in any of the above embodiments.

[0151] The electronic device 300 in this embodiment of the invention may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (e.g., vehicle navigation terminals), wearable electronic devices, and fixed terminals such as digital TVs, desktop computers, and laptops. Figure 9 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.

[0152] like Figure 9 As shown, the electronic device 300 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 301, which can perform various appropriate actions and processes to implement the methods of the embodiments described herein, based on a program stored in a read-only memory (ROM) 302 or a program loaded from a storage device 308 into a random access memory (RAM) 303. The RAM 303 also stores various programs and data required for the operation of the electronic device 300. The processing device 301, ROM 302, and RAM 303 are interconnected via a bus 305. An input / output (I / O) interface 305 is also connected to the bus 305.

[0153] Typically, the following devices can be connected to I / O interface 304: input devices 306 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 307 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 308 including, for example, magnetic tapes, hard disks, etc.; and communication devices 309. Communication device 309 allows electronic device 300 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 9 An electronic device 300 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.

[0154] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention also provide a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts, thereby implementing the IP address lookup method as described above. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 309, or installed from a storage device 308, or installed from a ROM 302. When the computer program is executed by the processing device 301, it performs the functions defined in the methods of the embodiments of the present invention.

[0155] Example 5

[0156] Corresponding to Embodiment 2 above, this application also provides a computer-readable storage medium. In this embodiment, the content that is the same as or similar to Embodiment 2 above can be referred to the above description, and will not be repeated hereafter.

[0157] The computer-readable storage medium stores a computer program thereon, which, when executed by a processor, implements the IP address lookup method as described above.

[0158] In some implementations of this application, when the computer program is executed by the processor, it can also implement the steps corresponding to the method described in Embodiment 1. Please refer to the detailed description in Embodiment 1, which will not be repeated here.

[0159] As can be seen from the above description of the embodiments, those skilled in the art can clearly understand that the present invention can be implemented by means of software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in various embodiments or some parts of the embodiments of the present invention.

[0160] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, for system or system embodiments, since they are basically similar to method embodiments, the description is relatively simple, and relevant parts can be referred to the descriptions in the method embodiments. The systems and system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without creative effort.

[0161] It should be understood that various parts of the present invention can be implemented in hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented in software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented using any one or a combination of the following techniques known in the art: discrete logic circuits having logic gates for implementing logical functions on data signals, application-specific integrated circuits (ASICs) having suitable combinational logic gates, programmable gate arrays (PGAs), field-programmable gate arrays (FPGAs), etc.

[0162] In the description of this specification, references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.

[0163] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this invention, "a plurality of" means at least two, such as two, three, etc., unless otherwise explicitly specified.

[0164] Although embodiments of the present invention have been shown and described above, it is understood that the above embodiments are exemplary and should not be construed as limiting the present invention. Those skilled in the art can make changes, modifications, substitutions and variations to the above embodiments within the scope of the present invention.

Claims

1. A method for querying IP addresses, characterized in that, The method includes: Convert the IP address to be queried into a byte array; Each element in the byte array is compared with a node in the preset IP set structure to determine whether the element is within the range of the node. The preset IP set structure is constructed as a prefix tree structure, where each node in the prefix tree structure shares the prefix of its parent node. Each node in the prefix tree structure is a range node, which represents a range rather than a single value. The IPs in the preset IP set structure are represented using bytes. If the node being compared is in a fully inclusive state, then the IP address to be queried is directly determined to belong to the preset IP set structure; the fully inclusive state means that all child nodes under the path prefix of the node being compared are located within the preset IP set; If the node being compared is not in a fully contained state, then determine whether the node being compared is a leaf node. If the node being compared is a leaf node, then the mask of the leaf node is ANDed with the element at the preset position of the byte array. If the result is greater than 0, then the leaf node is determined to contain the element being compared. If it is determined that the leaf node contains the element being compared, then the IP address to be queried belongs to the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure. If the node being compared is a non-leaf node, then determine whether the child nodes of the node being compared contain the element being compared. If the child nodes of the node being compared contain the element being compared, then compare the next element in the byte array with the node in the preset IP set structure. Otherwise, directly determine that the IP address to be queried does not belong to the preset IP set structure.

2. The IP address query method according to claim 1, characterized in that, The leaf node is a two-level node. The first level of the leaf node indicates that the current node is a leaf node, and the second level of the leaf node records whether the IP address of the current node is in the set.

3. In the IP address query method according to claim 1, the non-leaf node is a single-layer node.

4. An IP address lookup device, characterized in that, The device includes: The conversion module is used to convert the IP address to be queried into a byte array; The comparison module is used to compare each element in the byte array with a node in a preset IP set structure to determine whether the element is within the range of the node. The preset IP set structure is constructed as a prefix tree structure, where each node shares the prefix of its parent node. All nodes in the prefix tree structure are range nodes, representing a range rather than a single value. IP addresses in the preset IP set structure are represented using bytes. If the currently compared node is in a fully inclusive state, the IP address to be queried is directly determined to belong to the preset IP set structure. A fully inclusive state means that all child nodes under the path prefix of the currently compared node are within the preset IP set. If the currently compared node is not in a fully inclusive state, it is determined whether the currently compared node is a leaf node. If the node being compared is a leaf node, then the mask of the leaf node is ANDed with the element at the preset position of the byte array. If the result is greater than 0, then the leaf node is determined to contain the element being compared. If the leaf node is determined to contain the element being compared, then the IP address to be queried belongs to the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure. If the node being compared is a non-leaf node, then it is determined whether the child node of the node being compared contains the element being compared. If the child node of the node being compared contains the element being compared, then the next element in the byte array is compared with the node in the preset IP set structure; otherwise, the IP address to be queried does not belong to the preset IP set structure.

Citation Information

Patent Citations

  • IP address matching method, device and equipment and computer readable storage medium

    CN113609125A

  • Data processing method and device based on IP address range

    CN115955341A