IP address query method and device

By building an IP collection as a prefix tree structure and comparing byte arrays, the problems of IP collection storage space and query time are solved, and efficient IP address query is achieved.

CN119322872AActive Publication Date: 2025-01-17BEIJING CHAITIN TECH CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411232139.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-04
Publication Date
2025-01-17
Estimated Expiration
2044-09-04

AI Technical Summary

Technical Problem

In the prior art, the storage space of the IP set occupies too much and the query time is too long to meet general business needs, especially when processing all IPv4 sets.

Method used

Build the IP collection structure into a prefix tree structure, and convert the IP address to be queried into a byte array, and compare it with the nodes in the preset IP collection structure one by one, and determine whether the elements are within the range of the node to determine whether the IP address is Belongs to the preset IP collection.

Benefits of technology

Save a lot of storage space and comparison time, improve query efficiency, and make IP address query more fast and efficient.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119322872A_ABST
    Figure CN119322872A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses an IP address query method and device, and the method comprises the steps: converting a to-be-queried IP address into a byte array, judging node states in a preset IP set structure, if the node states are all included, directly judging that the to-be-queried IP address belongs to the preset IP set structure, and if the node states are not included, directly judging that the to-be-queried IP address belongs to the preset IP set structure; if the node state is a semi-inclusion state, directly judging that the IP address to be queried does not belong to a preset IP set structure, if the node state is a semi-inclusion state, continuing to judge child nodes according to the mode until leaf nodes of the last layer, judging whether elements in a byte array are all in an IP point position set represented by masks of the leaf nodes, and if so, judging that the elements in the byte array are all in the IP point position set represented by the masks of the leaf nodes; if yes, it is judged that the to-be-queried IP address belongs to the preset IP set combination, and if not, it is judged that the to-be-queried IP address does not belong to the preset IP set. According to the IP set structure provided by the scheme of the invention, a large amount of storage space can be saved, a result can be returned in advance according to specific conditions in the query process, a complete IP path does not need to be judged, and the query time can be shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data processing, and in particular to an IP address query method and device. Background Art

[0002] IP set is a structure that puts many IPs together and can be used for query. Currently, IP protocol has two versions: IP v4 and IPv6. The number of IPs that IPv4 can represent is 2 32 More than 4 billion, IPv6 can represent 2 128 IP, about 3.4x10 38 , IPv6 is compatible with all the contents of IPv4. As a common structure, IP set can provide IP query and is often used for IP access statistics, IP interception management and other functions. It is an important data expression structure.

[0003] Currently, the commonly used methods for expressing IP sets are database storage structure and memory dictionary. The database storage structure needs to query the database to obtain results each time it is used, which has low query efficiency. The ideal single query takes about 1ms, which is not conducive to fast query. Although the memory dictionary method can query quickly, it consumes a lot of memory. 1G of memory can theoretically express up to 260 million IPs. Therefore, both of these storage methods have defects. If a set representing all IPv4 is required, these two structures cannot meet the needs of general business in terms of storage space or query efficiency. Summary of the invention

[0004] The present invention aims to solve at least one of the technical problems existing in the prior art. To this end, the present invention proposes an IP address query method and device to solve the technical problems existing in the prior art such as excessive storage space occupied by IP sets and excessive query time.

[0005] In a first aspect, the present invention provides an IP address query method, the method comprising: Convert the IP address to be queried into a byte array; Comparing the elements in the byte array with the nodes in the preset IP set structure one by one, and determining whether the elements are within the range of the nodes, wherein the preset IP set structure is constructed as a prefix tree structure, each node in the prefix tree structure shares a prefix of a parent node, and the nodes in the prefix tree structure include range nodes; If all elements are within the range of the node, it is determined that the IP address to be queried belongs to the preset IP set structure.

[0006] In the embodiments of the present application, 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 the elements in the byte array with the nodes in the preset IP set structure one by one, since it is judged 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.

[0007] In a technical solution of the above-mentioned IP address query method, the elements in the byte array are compared one by one with the nodes in the preset IP set structure to determine whether the elements are within the range of the nodes: It is determined whether the node currently being compared is in a fully inclusive state. If the node currently being compared is in a fully inclusive state, it is directly determined that the IP address to be queried belongs to the preset IP set structure.

[0008] Through the embodiments of the present application, since the node in the fully inclusive state includes all child nodes under the path prefix of the current node, it is possible to directly determine that the IP address to be queried belongs to the preset IP set without the need to compare the next element or node, thereby further reducing the query time and improving the query efficiency.

[0009] In a technical solution of the above-mentioned IP address query method, the nodes include at least leaf nodes and non-leaf nodes.

[0010] In a technical solution of the above-mentioned IP address query method, comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is not in a fully contained state, determining whether the node currently being compared is a leaf node; If the node currently being compared is a leaf node, the mask of the leaf node is used to determine whether the leaf node contains the element currently being compared; If it is determined that the leaf node contains the element currently being compared, it is determined that the IP address to be queried belongs to the preset IP set structure; otherwise, it is determined that the IP address to be queried does not belong to the preset IP set structure.

[0011] In a technical solution of the above-mentioned IP address query method, comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is a non-leaf node, determining whether the child nodes of the node currently being compared contain the element currently being compared; If the child node of the node currently being compared contains the element currently being compared, 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.

[0012] In a technical solution of the above-mentioned IP address query method, the use of the mask of the leaf node to determine whether the leaf node contains the element currently being compared includes: The mask of the leaf node is ANDed with the element at the preset position of the byte array. If the calculated result is greater than 0, it is determined that the leaf node contains the element currently being compared.

[0013] In a technical solution of the above IP address query method, the leaf node is a double-layer node, the first layer of the leaf node indicates that the current node is a leaf node, and the second layer of the leaf node records whether the IP location of the current node is in the set.

[0014] In a technical solution of the above-mentioned IP address query method, the non-leaf node is a single-layer node.

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

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

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

[0018] In a second aspect, the present invention provides an IP address query device, the device comprising: A conversion module, used for converting the IP address to be queried into a byte array; A comparison module, used for comparing the elements in the byte array with the nodes in the preset IP set structure one by one, and judging whether the elements are within the range of the nodes, wherein 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; 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.

[0019] In a third aspect, the present invention provides an electronic device comprising 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, the IP address query method as described in any one of the first aspects is implemented.

[0020] In a fourth aspect, the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein when the computer program is executed, the IP address query method as described in any one of the first aspects is implemented.

[0021] The above one or more technical solutions of the present invention have at least one or more of the following beneficial effects: In the technical solution for implementing 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 the elements in the byte array with the nodes in the preset IP set structure one by one, since it is judged 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.

[0022] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] The disclosure of the present invention will become more easily understood with reference to the accompanying drawings. It is easy for those skilled in the art to understand that these drawings are only for illustrative purposes and are not intended to limit the scope of protection of the present invention. In addition, similar numbers in the figures are used to represent similar components, among which:

[0024] Figure 1 It is a schematic diagram of IPv6 data provided by the prior art; Figure 2 It is a structural diagram of IPv6 data in the IP set structure provided in Example 1 of the present application; Figure 3 It is a structural diagram of part of IPv6 data in the IP set structure provided in Example 1 of the present application; Figure 4 It is another structural diagram of part of IPv6 data in the IP set structure provided in Example 1 of the present application; Figure 5It is another structural diagram of part of IPv6 data in the IP set structure provided in Example 1 of the present application; Figure 6 It is a structural diagram of a leaf node in the IP set structure provided in Example 1 of the present application; Figure 7 This is a flowchart of the IP address query method provided in Example 2 of the present application; Figure 8 It is a structural diagram of an IP address query device provided in Embodiment 3 of the present application; Fig. 9 It is a schematic diagram of the structure of the electronic device provided in Example 4 of the present application. DETAILED DESCRIPTION

[0025] Some embodiments of the present invention are described below with reference to the accompanying drawings. It should be understood by those skilled in the art that these embodiments are only used to explain the technical principles of the present invention and are not intended to limit the protection scope of the present invention.

[0026] As described in the background art, the existing IP set expression structure either occupies a large storage space or takes a long time to query. If a set representing all IPv4s is required, the existing IP set expression structure cannot meet the general business needs in terms of storage space or query efficiency.

[0027] Based on this, the present 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 a common prefix can be expressed using the prefix tree result, which can maximize data sharing and save a lot of storage space. In subsequent IP address queries, in the process of comparing the elements in the byte array with the nodes in the preset IP set structure one by one, since it is judged 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.

[0028] Embodiment 1 Figure 1 This is a schematic diagram of IPv6 data provided by the prior art. Figure 1 As shown in the figure, IPv6 data can be regarded as one-dimensional data [0, 2128], and its total storage space is 2 128* 128 bits. Since the smallest storage space in a computer is a byte, and one byte represents 8 bits, an IPv6 data needs to be represented by 16 bytes. Usually, many of these IPs represented by bytes have a common prefix. Based on this, the embodiment of the present application creatively proposes to use a prefix tree result to express the IP set structure. Each node in the prefix tree structure shares the prefix of the parent node, and the nodes in the prefix tree structure are all range nodes, thereby saving storage space.

[0029] It should be noted that in the embodiment of the present application, the range node refers to a node expressing a range, rather than a single value represented by a conventional node. Therefore, during the query process, the node state of the IP set structure can be directly determined, rather than comparing one byte at a time as in the prior art.

[0030] Reference Figure 2 As shown, in the IP set structure provided in the implementation of this application, the structure of IPv6 becomes a two-dimensional triangle. Figure 2 As shown, the base of the triangle is 2 128 bits, the height is 128 bits, according to the triangle area formula, the storage space is 2 128 * 64 bits, which is 50% smaller than the one-dimensional data structure space. In the prefix tree structure, a single IP is represented as a line in the IP triangle, while the IP range can be represented as a small triangle.

[0031] It can be understood that in the embodiment of the present application, the prefix tree structure can be divided into three configurations according to the shape: 1. Line type, reference Figure 3 As shown, it indicates an independent IP; 2. A triangle with the same fixed point as the root node, refer to Figure 4 As shown, it indicates the IP range; 3. A triangle with different fixed points and root nodes, refer to Figure 5 As shown, it also indicates the IP range.

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

[0033] There are three common ways to express IP: 1) Single IP, for example: 192.168.0.1 2) CIDR format, for example: 192.168.0.0 / 24 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 The above three IP expressions need to be processed separately to correspond to the IP set structure: 1) For a single IP, in the IP set structure (such as Figure 2 The triangle shown is represented by a line type; 2) For CIDR format, first convert it to IP range expression, for example, 192.168.0.0 / 24 can be converted to 192.168.0.0-192.168.0.255, and 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 "a triangle with the same fixed point as the root node" configuration to represent it. If they have the same address prefix, use "a triangle with different fixed points from the root node" configuration to represent it. 3) For an IP range, if the start and end IP addresses do not have the same address prefix, they are represented using a "triangle with the same vertex as the root node" configuration; if they have the same address prefix, they are represented using a "triangle with a different vertex than the root node" configuration.

[0034] In summary, based on the above structure, it is easy to find whether an IP already exists in the set. A single IP can be intuitively represented by a "line" formed by a path. If this "line" coincides with a line in the set, or is within a certain triangle range, it means that the current IP exists in the set, otherwise the IP does not exist in the set.

[0035] As a preferred implementation, in the embodiment of the present application, the node of the prefix tree structure usually contains two attributes: state and children. Among them, state contains a byte type data, using a mask to express different meanings; children contains a map type data, pointing to the child nodes of the current node, the value of the map is also the node type, and the key of the map is a byte type, so a node can contain up to 256 child nodes.

[0036] As a preferred implementation, in the embodiment of the present application, the nodes include at least two types: leaf nodes and non-leaf nodes.

[0037] It can be understood that in the embodiment of the present application, the leaf nodes and non-leaf nodes have the same structure, but the data have different expression meanings.

[0038] For non-leaf nodes, the 8 bits in the state field represent different meanings, as shown in Table 1 below: Table 1 bit (from left to right) Meaning No. 0 Is it a leaf node? If yes, it is 1; otherwise, it is 0. No. 1 Whether to include all child nodes under the path prefix of the current node (full inclusion state), 1 for yes, 0 for no. When 1, it means 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 the child node can be represented by n, the minimum value is 0, and the maximum value is 7, which means there are 2n+1 nodes, that is, there are at least 2 nodes and at most 256 nodes.

[0039] It is understandable that in the embodiment of the present application, children is a dictionary structure, indicating 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 of children is of byte type, and the maximum value is also 256.

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

[0041] Table 2 bit (from left to right) Meaning No. 0 Leaf node, fixed at 1 1st to 4th place 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]]>

[0042] The 8 bits in the second layer state field of the leaf node correspond to the IP bits contained in the 8-bit space under the current prefix. 1 means included, and 0 means not included. The 2-layer structure of the leaf node is as follows: Figure 6 shown.

[0043] It should be noted that, in the process of searching whether an IP is in the IP set structure provided in the embodiment of the present application, when searching section by section, if the child node does not exist, it means that the current IP is not in the set. If a non-leaf node is found, the node is marked as containing all child nodes, which means that the IP exists in the current set. If a leaf node is found all the way, it is determined whether the IP exists in the current set based on the mask of the second-layer node in the leaf node, which can greatly reduce the query time and improve the query efficiency.

[0044] Embodiment 2 Corresponding to the above-mentioned embodiment 1, the present application also provides an IP address query method, which is particularly suitable for querying whether an IP address exists in the IP set structure provided in the embodiment 1. In this embodiment, the same or similar contents as those in the above-mentioned embodiment 1 can be referred to the above introduction, and will not be repeated in the following. Figure 7 As shown, the method comprises the following steps:

[0045] S110: Convert the IP address to be queried into a byte array; S120: Compare the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes; S130: If the element is within the range of the node, determining that the IP address to be queried belongs to the preset IP set structure; Among them, the preset IP set structure includes the IP set structure as described in any one of the embodiments, 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.

[0046] It is understandable that since byte is used to represent IP in the preset IP set structure, when querying whether the IP address to be queried belongs to the preset IP set structure, it is first necessary to convert the IP address to be queried into a byte array format. The specific conversion process can refer to the relevant existing technology and will not be repeated here.

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

[0048] It can be understood 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. At this time, it means that the IP address to be queried belongs to the preset IP set.

[0049] As a preferred implementation, in the embodiment of the present application, the elements in the byte array are compared one by one with the nodes in the preset IP set structure to determine whether the elements are within the range of the nodes: It is determined whether the node currently being compared is in a fully inclusive state. If the node currently being compared is in a fully inclusive state, it is directly determined that the IP address to be queried belongs to the preset IP set structure.

[0050] It is understandable that, in the embodiment of the present application, there are three types of IP node states: full inclusion, semi-inclusion, and non-inclusion. Since when a node is in a full inclusion state, it means that the node contains all child nodes under the path prefix of the current node, so when it is determined that the current node compared with the current element is in a full inclusion state, it can be directly determined that the IP address to be queried belongs to the preset IP set structure, and there is no need to compare the next element or node, thereby further reducing the query time and improving the query efficiency. When the node is in a non-inclusion state, it can be directly determined that the IP address to be queried does not belong to the preset IP set structure; when the node is in a semi-inclusion state, the child nodes are continued to be judged until the end. In summary, the IP address query method provided in the embodiment of the present application can return the result in advance according to the specific situation, without judging the complete IP path, so the query time can be shortened and the query efficiency can be improved.

[0051] As a preferred implementation, in the embodiment of the present application, the nodes include at least leaf nodes and non-leaf nodes.

[0052] It can be understood that in the embodiment of the present application, the leaf nodes and non-leaf nodes have the same structure, but the data have different expression meanings. The relevant content is described in Example 1 and will not be repeated here.

[0053] As a preferred implementation, in the embodiment of the present application, comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is not in a fully contained state, determining whether the node currently being compared is a leaf node; If the node currently being compared is a leaf node, the mask of the leaf node is used to determine whether the leaf node contains the element currently being compared; If it is determined that the leaf node contains the element currently being compared, it is determined that the IP address to be queried belongs to the preset IP set structure; otherwise, it is determined that the IP address to be queried does not belong to the preset IP set structure.

[0054] Specifically, when it is determined that the node currently 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 a leaf node, the mask of the leaf node can be used to determine whether the leaf node contains the element currently being compared. It is understandable that when it is determined that the leaf node contains the element currently being compared, it means that the IP address to be queried belongs to the preset IP set structure, otherwise, when it is determined that the leaf node does not contain the element currently being compared, it means that the IP address to be queried does not belong to the preset IP set structure.

[0055] As a preferred implementation, in the embodiment of the present application, the use of the mask of the leaf node to determine whether the leaf node contains the element currently being compared includes: The mask of the leaf node is ANDed with the element at the preset position of the byte array. If the calculated result is greater than 0, it is determined that the leaf node contains the element currently being compared.

[0056] It should be noted that in the embodiments of the present application, the preset position is not specifically limited. Without violating the inventive concept of the present application, the user can set it according to actual needs. In some specific embodiments, the state field (i.e., mask) of the second layer of the leaf node can be "and-operated" with the last byte of the IP to be queried to determine whether the result is greater than 0. If the result after the mask calculation is greater than 0, it means that the IP address to be queried belongs to the preset IP set structure.

[0057] As a preferred implementation, in the embodiment of the present application, comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is a non-leaf node, determining whether the child nodes of the node currently being compared contain the element currently being compared; If the child node of the node currently being compared contains the element currently being compared, 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.

[0058] Specifically, for a non-leaf node, the element currently being compared may be compared with the child nodes of the non-leaf node to determine whether the child nodes of the non-leaf node include the element currently being compared.

[0059] It can be understood that 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-mentioned comparison process needs to be iterated, that is, the next element in the above-mentioned 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-mentioned 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, as long as one element is found not in the preset IP set structure, it means that the IP address to be queried does not belong to the preset IP set structure.

[0060] It is understandable that when it is determined that the child node of the non-leaf node does 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.

[0061] As a preferred implementation, in the embodiment of the present application, the non-leaf node is a single-layer node.

[0062] Specifically, for a non-leaf node, the 8 bits in the state field represent different meanings respectively. Please refer to the relevant contents of the first embodiment for details, which will not be described here one by one.

[0063] As a preferred implementation, in the embodiment of the present application, the leaf node is a double-layer node, the first layer of the leaf node indicates that the current node is a leaf node, and the second layer of the leaf node records whether the IP location of the current node is in the set.

[0064] Specifically, due to the special structure of the IP set provided by the embodiment of the present application, the leaf node in the embodiment of the present application is composed of a two-layer node. Among them, the first layer of the leaf node indicates that it is currently a leaf node, and the second layer records whether the IP point of the current node (up to 256) is in the set. The 8 bits in the state field of the first layer of the leaf node represent the following meanings from left to right: bit 0: leaf node, fixed to 1; bits 1 to 4: reserved bits; bits 5 to 7: There are 16 second-layer child nodes, and the state of each node has 8 bits, which just expresses 256 IPs. Therefore, the fixed value of bits 5 to 7 is 100, indicating that there are 2 4 = 16 child nodes. The 8 bits in the second-level state field of the leaf node correspond to the IP bits contained in the 8-bit space under the current prefix, 1 means included, and 0 means not included.

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

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

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

[0068] Specifically, as described in Example 1, since the IP set structure in the present application is expressed using a prefix tree result, each node in the prefix tree structure shares the prefix of the parent node. Therefore, in the IP set structure provided in the implementation of the present application, the structure of IPv6 (IP address) becomes a two-dimensional triangle.

[0069] The following example illustrates the IP address query method provided in the embodiment of the present application.

[0070] Assume that the IP address to be queried is 192.168.0.1. First convert it to the IPv6 byte format: [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0xc0a8, 0x0001] Secondly, query according to the method steps in Table 3 below to determine whether the IP address is in the preset IP set.

[0071] Table 3 step Node (byte is represented by binary notation) Judgment conditions Query results 1 root node{state: [0000 0111]children: {[0000 0000]: child node}} Check if it is in full inclusion state: No Is it a leaf node: No Does children contain a child node with the first byte of the IP to be queried as the key: Yes The path exists, continue searching 2 First level child nodes {state: [0000 0111] children: {[0000 0000]: child nodes}} Check if it is in full inclusion state: No Is it a leaf node: No Does children contain a child node with the first byte of the IP to be queried as the key: Yes The path exists, continue searching 3 ~ 14 Repeat the above steps until the leaf node The path exists, continue searching 15 Leaf node, first layer {state: [10000100] children: {[0000 0000]: second layer}} Determine whether it is a leaf node: Yes Whether the second level of the child node exists: Yes The path exists, continue searching 16 Leaf node, second layer {state: [00000001] children: empty} The state (a byte) is ANDed with the last byte of the IP to be checked to see if the result is greater than 0: Yes The result of the mask calculation is greater than 0, and the IP exists in the set As shown in Table 3 above, this query function uses 16 node judgments and is independent of the size of the collection, that is, regardless of the size of the collection, the result can be returned after a maximum of 16 node judgments.

[0072] Assume that the IP address to be queried is 36.192.1.23. First, convert it to the IPv6 byte format: [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x24c0, 0x0117] Secondly, query according to the method steps in Table 4 below to determine whether the IP address is in the preset IP set.

[0073] Table 4 step Node (byte is represented by binary notation) Judgment conditions Query results 1~12 Omit the prefix IPv6 prefix judgment The path exists, continue searching 13 {state: [00000111]children:{[0010 0100]: child node}} 1. Check if it is in full inclusion state: No 2. Check if it is a leaf node: No 3. Check if children contains a child node with the first byte of the IP to be queried as the key: Yes The path exists, continue searching 14 {state: [00000010]children:{[0000 0010]: child node,}}Note: At this time, the prefix in the state is "0010", which is 2. Therefore, the current node has at most 22 = 4 child nodes, and the key range of the child nodes can only be in "00000000", "00000001", "00000010", "00000011". 1. Check if it is in full inclusion state: No 2. Check if it is a leaf node: No 3. Check if children contains a child node with the first byte of the IP to be queried as the key: Yes The path exists, continue searching 15 {state: [01000110]children:NULL} Note: The prefix of the parent node is 2. In order to ensure that the number of grandchild nodes is 256, the most efficient, the prefix of the child node needs to be 8-2 = 6. Determine whether it is a fully inclusive state: Yes The current node is in the "all included" state, and the result can be returned as included

[0074] In the above query process, since the "all-inclusive" node is encountered, it is not necessary to complete all 16 queries and the query results can be completed in advance, which further reduces the query time.

[0075] Embodiment 3 Corresponding to the above-mentioned embodiment 2, the present application further provides an IP address query device 200, which is used to implement the IP address query method provided in any one of the embodiments 2. In this embodiment, the same or similar contents as those in the above-mentioned embodiment 2 can be referred to the above introduction, and will not be described in detail later. Figure 8 As shown, the device comprises:

[0076] The conversion module 210 is used to convert the IP address to be queried into a byte array; A comparison module 220, used for comparing the elements in the byte array with the nodes in the preset IP set structure one by one, and determining whether the elements are within the range of the nodes, wherein 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; The judgment module 230 is used to determine whether the IP address to be queried belongs to the preset IP set structure if all elements are within the range of the node.

[0077] As a preferred implementation, in the embodiment of the present application, the comparison module is specifically used for: It is determined whether the node currently being compared is in a fully inclusive state. If the node currently being compared is in a fully inclusive state, it is directly determined that the IP address to be queried belongs to the preset IP set structure.

[0078] As a preferred implementation, in the embodiment of the present application, the nodes include at least leaf nodes and non-leaf nodes.

[0079] As a preferred implementation, in the embodiment of the present application, the comparison module is specifically used for: If the node currently being compared is not in a fully contained state, determining whether the node currently being compared is a leaf node; If the node currently being compared is a leaf node, the mask of the leaf node is used to determine whether the leaf node contains the element currently being compared; If it is determined that the leaf node contains the element currently being compared, it is determined that the IP address to be queried belongs to the preset IP set structure; otherwise, it is determined that the IP address to be queried does not belong to the preset IP set structure.

[0080] As a preferred implementation, in the embodiment of the present application, the comparison module is specifically used for: If the node currently being compared is a non-leaf node, determining whether the child nodes of the node currently being compared contain the element currently being compared; If the child node of the node currently being compared contains the element currently being compared, 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.

[0081] As a preferred implementation, in the embodiment of the present application, the comparison module is specifically used for: The mask of the leaf node is ANDed with the element at the preset position of the byte array. If the calculated result is greater than 0, it is determined that the leaf node contains the element currently being compared.

[0082] As a preferred implementation, in the embodiment of the present application, the leaf node is a double-layer node, the first layer of the leaf node indicates that the current node is a leaf node, and the second layer of the leaf node records whether the IP location of the current node is in the set.

[0083] As a preferred implementation, in the embodiment of the present application, the non-leaf node is a single-layer node.

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

[0085] As a preferred implementation, in the embodiment of the present application, a single IP is represented as a line in the two-dimensional triangular structure.

[0086] As a preferred implementation, in the embodiment of the present application, the IP range is represented as a triangle in the two-dimensional triangle structure.

[0087] Embodiment 4 Corresponding to the above-mentioned embodiment 2, the present application also provides an electronic device, including: a processor and a memory, the memory stores a computer program that can be run on the processor, and when the computer program is executed by the processor, the IP address query method provided in any one of the above-mentioned embodiments is executed.

[0088] The electronic device 300 in the embodiment of the present invention may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), wearable electronic devices, etc., as well as fixed terminals such as digital TVs, desktop computers, laptop computers, etc. Fig. 9 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0089] like Fig. 9As 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 according to a program stored in a read-only memory (ROM) 302 or a program loaded from a storage device 308 to a random access memory (RAM) 303 to implement the method of the embodiment of the present invention. In the RAM 303, various programs and data required for the operation of the electronic device 300 are also stored. The processing device 301, the ROM 302, and the RAM 303 are connected to each other via a bus 305. An input / output (I / O) interface 305 is also connected to the bus 305.

[0090] Typically, the following devices may be connected to the I / O interface 304: an input device 306 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 307 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 308 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 309. The communication device 309 may allow the electronic device 300 to communicate with other devices wirelessly or by wire to exchange data. Although Fig. 9 The electronic device 300 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0091] In particular, according to an embodiment of the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present invention also provides a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart, thereby implementing the IP address query method as described above. In such an embodiment, the computer program can be downloaded and installed from the network through the communication device 309, or installed from the storage device 308, or installed from the ROM 302. When the computer program is executed by the processing device 301, the above-mentioned functions defined in the method of the embodiment of the present invention are executed.

[0092] Embodiment 5 Corresponding to the above-mentioned embodiment 2, the embodiment of the present application further provides a computer-readable storage medium, wherein, in this embodiment, the same or similar contents as those of the above-mentioned embodiment 2 can be referred to the above introduction and will not be repeated later.

[0093] The computer-readable storage medium stores a computer program thereon, and when the computer program is executed by the processor, the IP address query method described above is implemented.

[0094] In some implementations, in the embodiments of the present application, when the computer program is executed by the processor, the steps corresponding to the method described in Embodiment 1 may also be implemented. Please refer to the detailed description in Embodiment 1 and no further details will be given here.

[0095] It can be known from the above description of the implementation mode that those skilled in the art can clearly understand that the present invention can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present invention can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present invention or certain parts of the embodiments.

[0096] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can refer to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system or system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can refer to the partial description of the method embodiment. The system and system embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without creative work.

[0097] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0098] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.

[0099] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of the present invention, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.

[0100] Although the embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are exemplary and are not to be construed as limitations of the present invention. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present invention.

Claims

1. An IP address query method, characterized in that: The method comprises: Convert the IP address to be queried into a byte array; Comparing the elements in the byte array with the nodes in the preset IP set structure one by one, and determining whether the elements are within the range of the nodes, wherein the preset IP set structure is constructed as a prefix tree structure, each node in the prefix tree structure shares a prefix of a parent node, and the nodes in the prefix tree structure include range nodes; If all elements are within the range of the node, it is determined that the IP address to be queried belongs to the preset IP set structure.

2. The IP address query method according to claim 1, characterized in that: The elements in the byte array are compared one by one with the nodes in the preset IP set structure to determine whether the elements are included in the range of the nodes: It is determined whether the node currently being compared is in a fully inclusive state. If the node currently being compared is in a fully inclusive state, it is directly determined that the IP address to be queried belongs to the preset IP set structure.

3. The IP address query method according to claim 2, characterized in that: The nodes include at least leaf nodes and non-leaf nodes.

4. The IP address query method according to claim 3, characterized in that: The step of comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is not in a fully contained state, determining whether the node currently being compared is a leaf node; If the node currently being compared is a leaf node, the mask of the leaf node is used to determine whether the leaf node contains the element currently being compared; If it is determined that the leaf node contains the element currently being compared, it is determined that the IP address to be queried belongs to the preset IP set structure; otherwise, it is determined that the IP address to be queried does not belong to the preset IP set structure.

5. The IP address query method according to claim 4, characterized in that: The step of comparing the elements in the byte array with the nodes in the preset IP set structure one by one to determine whether the elements are within the range of the nodes also includes: If the node currently being compared is a non-leaf node, determining whether the child nodes of the node currently being compared contain the element currently being compared; If the child node of the node currently being compared contains the element currently being compared, 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.

6. The IP address query method according to claim 4, characterized in that: The using the mask of the leaf node to determine whether the leaf node contains the element currently being compared includes: The mask of the leaf node is ANDed with the element at the preset position of the byte array. If the calculated result is greater than 0, it is determined that the leaf node contains the element currently being compared.

7. The IP address query method according to any one of claims 3 to 6, characterized in that: The leaf node is a double-layer node. The first layer of the leaf node indicates that the current node is a leaf node, and the second layer of the leaf node records whether the IP location of the current node is in the set.

8. According to the IP address query method according to any one of claims 3 to 6, the non-leaf node is a single-layer node.

9. The IP address query method according to any one of claims 1 to 6, characterized in that: In the prefix tree structure, the structure of the IP address is a two-dimensional triangle structure.

10. An IP address query device, characterized in that: The device comprises: A conversion module, used for converting the IP address to be queried into a byte array; A comparison module, used for comparing the elements in the byte array with the nodes in the preset IP set structure one by one, and judging whether the elements are within the range of the nodes, wherein 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; 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.

Citation Information

Patent Citations

  • Automatic address range detection for IP networks

    CN102474532A

  • IP attribution query method and apparatus

    CN107807976A

  • Range query method and device, computer readable storage medium and electronic equipment

    CN112800067A

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

    CN113609125A

  • Data processing method and device based on IP address range

    CN115955341A