A method, device, electronic device and storage medium for address segment retrieval
By converting the IP address into a binary number and using a hash algorithm or matching relationship, the address segment where the IP address is located is quickly determined, which solves the problem of low IP address segment retrieval efficiency in the existing technology and achieves a more efficient address segment retrieval.
Patent Information
- Application Number
- CN202111563191.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-20
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2041-12-20
AI Technical Summary
In the existing technology, the retrieval efficiency of IP address segments is low, especially when the number of IP address segments is small, the time complexity of binary search results in low retrieval efficiency.
By converting the IP address into a binary number, and using a hash algorithm or binary number matching based on the pre-stored correspondence, the address segment where the IP address is located is quickly determined and the time complexity is reduced.
The efficiency of address segment retrieval is improved, especially when the number of address segments is small, the retrieval time complexity is significantly reduced and the retrieval speed is improved.
Smart Images

Figure CN114268608B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer application technology, and in particular to an address segment retrieval method, device, electronic device and storage medium. Background Art
[0002] The Internet Protocol (IP) address is a unified address format provided by the IP protocol. To facilitate the management and use of IP addresses, multiple IP addresses are divided into the same address segment, and each address segment is allocated. For example, the address segment can be allocated to a certain operator in a certain area. Therefore, after obtaining an IP address, the address segment where the IP address is located can be retrieved based on the IP address, so as to determine the ownership information of the IP address.
[0003] In the related art, when searching for an address segment where a certain IP address is located, the address segment where the IP address is located can be determined by binary search based on the value range of the IP address.
[0004] In the above retrieval process, the time complexity corresponding to the binary search method is N = log 2 2 N , of which 2 N is the number of bits of the IP mask. This will result in N searches being required even when the number of IP address segments is small, resulting in low efficiency in address segment searches. Summary of the invention
[0005] The purpose of the embodiments of the present application is to provide an address segment search method, device, electronic device and storage medium to improve the efficiency of address segment search. The specific technical solution is as follows:
[0006] The present application provides an address segment search method, the method comprising:
[0007] Get the first IP address;
[0008] Convert the first IP address into a binary number to obtain a first data string;
[0009] According to a pre-stored first correspondence, the address segment matching the first data string is retrieved to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0010] Optionally, the method further includes:
[0011] Obtain multiple second address segments;
[0012] For each second address segment, convert the IP mask corresponding to the second address segment into a binary number to obtain a third data string;
[0013] For each second address segment, generating a second data string corresponding to the second address segment according to a third data string of the IP mask corresponding to the second address segment;
[0014] A first corresponding relationship is generated based on each second address segment and a second data string corresponding to each second address segment.
[0015] Optionally, the step of generating, for each second address segment, a second data string corresponding to the second address segment according to a third data string of the IP mask corresponding to the second address segment includes:
[0016] For each second address segment, the third data string of the IP mask corresponding to the second address segment is determined as the second data string corresponding to the second address segment.
[0017] Optionally, the step of generating, for each second address segment, a second data string corresponding to the second address segment according to a third data string of the IP mask corresponding to the second address segment includes:
[0018] For each second address segment, a preset number of preset binary numbers are added after the third data string corresponding to the IP mask of the second address segment to obtain a second data string corresponding to the second address segment.
[0019] Optionally, the step of generating a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment includes:
[0020] According to each second address segment and the second data string corresponding to the second address segment, a correspondence relationship between the second address segment and the second data string is generated to obtain a first correspondence relationship.
[0021] Optionally, the step of retrieving an address segment matching the first data string according to a pre-stored first correspondence to obtain a first address segment where the first IP address is located includes:
[0022] Taking the number of binary numbers included in the first data string as a target number, obtaining the first target number of binary numbers in the first data string as a fourth data string;
[0023] If the fourth data string is not included in the first corresponding relationship, the target number is reduced by one, and the step of obtaining the first target number of binary numbers in the first data string as the fourth data string is returned to be executed;
[0024] If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0025] Optionally, the step of generating a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment includes:
[0026] For each second address segment, using a preset hash algorithm, calculate a hash value of a second data string corresponding to the second address segment to obtain a first hash value corresponding to the second address segment;
[0027] According to each second address segment and the first hash value corresponding to the second address segment, a correspondence relationship between the second address segment and the first hash value is generated to obtain a first correspondence relationship.
[0028] Optionally, the step of retrieving an address segment matching the first data string according to a pre-stored first correspondence to obtain a first address segment where the first IP address is located includes:
[0029] Taking the number of binary numbers included in the first data string as the target number, and using a preset hash algorithm, calculating the second hash values of the first target number of binary numbers in the first data string;
[0030] If the second hash value is not included in the first corresponding relationship, the target number is reduced by one, and the step of calculating the second hash values of the first target number of binary numbers in the first data string by using the preset hash algorithm is returned to execution;
[0031] If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0032] Optionally, the method further includes:
[0033] The attribution information of the first IP address is determined based on a pre-stored second correspondence between the second address segment and the attribution information, and the first address segment where the first IP address is located, where the attribution information includes one or more of a country, a city, and an operator.
[0034] The present application provides an address segment search device, the device comprising:
[0035] A first acquisition module, used to acquire a first IP address;
[0036] A first conversion module, used for converting the first IP address into a binary number to obtain a first data string;
[0037] A retrieval module is used to retrieve the address segment matching the first data string according to a pre-stored first correspondence relationship, and obtain the first address segment where the first IP address is located, wherein the first correspondence relationship is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0038] Optionally, the device further comprises:
[0039] A second acquisition module, used to acquire multiple second address segments;
[0040] A second conversion module is used for converting the IP mask corresponding to each second address segment into a binary number to obtain a third data string;
[0041] A first generating module is used to generate, for each second address segment, a second data string corresponding to the second address segment according to a third data string of the IP mask corresponding to the second address segment;
[0042] The second generating module is used to generate a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment.
[0043] Optionally, the first generating module is specifically used to determine, for each second address segment, a third data string of the IP mask corresponding to the second address segment as the second data string corresponding to the second address segment.
[0044] Optionally, the first generating module is specifically used to supplement a preset number of preset binary numbers after a third data string of the IP mask corresponding to each second address segment, so as to obtain a second data string corresponding to the second address segment.
[0045] Optionally, the second generating module is specifically used to generate a correspondence between the second address segment and the second data string according to each second address segment and the second data string corresponding to the second address segment, so as to obtain a first correspondence.
[0046] Optionally, the retrieval module is specifically configured to take the number of binary numbers included in the first data string as a target number, and obtain the first target number of binary numbers in the first data string as a fourth data string;
[0047] If the fourth data string is not included in the first corresponding relationship, the target number is reduced by one, and the step of obtaining the first target number of binary numbers in the first data string as the fourth data string is returned to be executed;
[0048] If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0049] Optionally, the second generating module is specifically configured to calculate, for each second address segment, a hash value of a second data string corresponding to the second address segment by using a preset hash algorithm to obtain a first hash value corresponding to the second address segment;
[0050] According to each second address segment and the first hash value corresponding to the second address segment, a correspondence relationship between the second address segment and the first hash value is generated to obtain a first correspondence relationship.
[0051] Optionally, the retrieval module is specifically configured to take the number of binary numbers included in the first data string as the target number, and calculate the second hash value of the first target number of binary numbers in the first data string using a preset hash algorithm;
[0052] If the second hash value is not included in the first corresponding relationship, the target number is reduced by one, and the step of calculating the second hash values of the first target number of binary numbers in the first data string by using the preset hash algorithm is returned to execution;
[0053] If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0054] Optionally, the device further comprises:
[0055] A determination module is used to determine the attribution information of the first IP address based on a second correspondence between the second address segment and the attribution information pre-stored, and the first address segment where the first IP address is located, wherein the attribution information includes one or more of a country, a city, and an operator.
[0056] The embodiment of the present application also provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0057] Memory, used to store computer programs;
[0058] The processor is used to implement any of the above-mentioned address segment retrieval method steps when executing the program stored in the memory.
[0059] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, any of the above-mentioned address segment retrieval method steps is implemented.
[0060] An embodiment of the present application also provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any of the above-mentioned address segment retrieval methods.
[0061] Beneficial effects of the embodiments of the present application:
[0062] The technical solution provided by the embodiment of the present application can convert the first IP address to be retrieved into a binary number to obtain a first data string, so as to retrieve the first address segment where the first IP address is located according to the pre-stored first corresponding relationship. Compared with the related art, since the first corresponding relationship is generated according to each second address segment and the second data string corresponding to each second address segment, that is, it is generated according to the binary number corresponding to each second address segment and the IP mask of the second address segment, considering that the IP mask of the same address segment is the same, that is, the binary number corresponding to the IP mask of the same address segment is the same, therefore, the binary number corresponding to the IP mask in the first data string of the first IP address is the same as the binary number corresponding to the IP mask in the second address segment where the first IP address is located. Based on this, when retrieving the first address segment where the first IP address is located according to the first data string and the first corresponding relationship, the maximum value of the time complexity is the number of bits of the binary number of the first IP address, and when the number of address segments is less, the time complexity of the retrieval is smaller, which effectively reduces the time complexity of the address segment retrieval, thereby improving the efficiency of the address segment retrieval.
[0063] Of course, implementing any product or method of the present application does not necessarily require achieving all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application, and for ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0065] Figure 1 A schematic diagram of a first flow chart of an address segment search method provided in an embodiment of the present application;
[0066] Figure 2 A second flow chart of the address segment search method provided in the embodiment of the present application;
[0067] Figure 3A third flow chart of the address segment search method provided in the embodiment of the present application;
[0068] Figure 4 A fourth flow chart of the address segment search method provided in the embodiment of the present application;
[0069] Figure 5 A fifth flow chart of the address segment retrieval method provided in the embodiment of the present application;
[0070] Figure 6 A schematic diagram of the structure of an address segment retrieval device provided in an embodiment of the present application;
[0071] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0072] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field based on the present application belong to the scope of protection of the present application.
[0073] In order to solve the problems in the related art, the embodiment of the present application also provides an address segment retrieval method, such as Figure 1 As shown, Figure 1 The first flow chart of the address segment search method provided in the embodiment of the present application is shown in FIG. The method can be applied to any electronic device, and the electronic device is not specifically limited here. Figure 1 The method shown includes the following steps.
[0074] Step S101, obtaining a first IP address.
[0075] Step S102, converting the first IP address into a binary number to obtain a first data string.
[0076] Step S103, according to the pre-stored first correspondence, retrieve the address segment matching the first data string to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0077] use Figure 1The method shown can convert the first IP address to be retrieved into a binary number to obtain a first data string, so as to retrieve the first address segment where the first IP address is located according to the pre-stored first corresponding relationship. Compared with the related art, since the first corresponding relationship is generated according to each second address segment and the second data string corresponding to each second address segment, that is, it is generated according to each second address segment and the binary number corresponding to the IP mask of the second address segment, considering that the IP mask of the same address segment is the same, that is, the binary number corresponding to the IP mask of the same address segment is the same, therefore, the binary number corresponding to the IP mask in the first data string of the first IP address is the same as the binary number corresponding to the IP mask in the second address segment where the first IP address is located. Based on this, when retrieving the first address segment where the first IP address is located according to the first data string and the first corresponding relationship, the maximum value of the time complexity is the number of bits of the binary number of the first IP address, and when the number of address segments is less, the time complexity of the retrieval is smaller, which effectively reduces the time complexity of the address segment retrieval, thereby improving the efficiency of the address segment retrieval.
[0078] The following specific examples are used to illustrate the embodiments of the present application.
[0079] With respect to the above step S101, the first IP address is obtained.
[0080] In an optional embodiment, the user can trigger a search operation for the address segment where a certain IP address is located. For example, a search bar is included in the display interface of the electronic device. When the user enters a certain IP address in the search bar, a search operation for the IP address will be triggered. At this time, the electronic device will obtain the IP address entered by the user (recorded as the first IP address). The electronic device can determine that the first IP address is the IP address to be searched.
[0081] In the embodiment of the present application, the first IP address may be an Internet Protocol version 4 (IPv4) address or an Internet Protocol version 6 (IPv6) address. The first IP address is not specifically limited. For ease of understanding, the following description is based on the example that the first IP address is an IPv4 address, which does not serve any limiting role.
[0082] With respect to the above step S102, the first IP address is converted into a binary number to obtain a first data string.
[0083] In this step, after acquiring the first IP address, the electronic device may convert the IP address from a decimal number to a binary number to obtain a data string of the first IP address (referred to as a first data string).
[0084] In an optional embodiment, when the first IP address is the IPv4 address, the electronic device may convert the first IP address into a 32-bit binary number consisting of 0 or 1 to obtain a first data string.
[0085] For example, when the first IP address is 1.2.2.0, the first data string of the first IP address is expressed as: 0000000100000010 00000010 00000000.
[0086] In an optional embodiment, when the first IP address is the IPv6 address, the electronic device may convert the first IP address into a 128-bit binary number consisting of 0 or 1 to obtain a first data string.
[0087] In the embodiment of the present application, the conversion process of the above-mentioned first IP address is not described in detail.
[0088] For the above step S103, that is, according to the pre-stored first correspondence, the address segment matching the first data string is retrieved to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0089] In this step, the electronic device can generate a first correspondence relationship based on each pre-divided address segment (recorded as the second address segment) and the data string corresponding to the IP mask of the second address segment (recorded as the second data string). Based on the above-mentioned first data string, the electronic device can retrieve the address segment (recorded as the first address segment) where the first data string is located in the first correspondence relationship. For the generation method of the above-mentioned first correspondence relationship and the retrieval method of the first address segment, please refer to the description below, which will not be repeated here.
[0090] In an optional embodiment, according to the above Figure 1 The method shown in the embodiment of the present application also provides an address segment retrieval method. Figure 2 As shown, Figure 2 A second flow chart of the address segment search method provided in the embodiment of the present application. The method includes the following steps.
[0091] Step S201, obtaining multiple second address segments.
[0092] In this step, according to the value range of the IP address, a plurality of second address segments can be obtained, and the electronic device can obtain each second address segment.
[0093] For ease of understanding, the above IP address is taken as an IPv4 address as an example for explanation.
[0094] The value range of the above IPv4 address is 0.0.0.0 to 255.255.255.255. This value range can be divided into multiple address segments. For example, the value range of address segment 1 is: 0.0.0.0 to 0.0.0.255, and the value range of address segment 2 can be: 2.2.2.8 to 2.2.3.100, etc.
[0095] For each of the above second address segments, the IP mask corresponding to each IP address in the second address segment is the same. For ease of understanding, the above address segment 1 and address segment 2 are used as examples for description.
[0096] In address segment 1, the first 24 bits of the binary number corresponding to each IP address are the same, that is, 000000000000000 00000000, which can be expressed as: 0.0.0.0 / 24.
[0097] In address segment 2, the first 16 bits of the binary number corresponding to each IP address are the same, that is, 0000001000000010, which can be expressed as: 2.2.2.8 / 16.
[0098] The value ranges corresponding to the above-mentioned multiple second address segments are all different, and the number of IP addresses included in each second address segment can be the same or different. Here, the number of the above-mentioned second address segments and the value range of each second address segment are not specifically limited.
[0099] Step S202: for each second address segment, convert the IP mask corresponding to the second address segment into a binary number to obtain a third data string.
[0100] In this step, for each second address segment, the electronic device can convert another IP address in the second address segment into a binary number, obtain a data string corresponding to the IP address (recorded as the fifth data string), and determine the first preset number of binary numbers in the fifth data string as the binary numbers corresponding to the IP mask of the second data segment according to the number of bits of the IP mask corresponding to the second address segment, and obtain a data string (recorded as the third data string). The preset number of bits is the number of bits of the IP mask corresponding to the second address segment.
[0101] For ease of understanding, the above-mentioned address segment 1 is still used as an example for explanation. The electronic device can convert the first IP address, that is, 0.0.0.0 / 24, into a binary number to obtain a fifth data string, which is represented by: 00000000 0000000000000000 00000000. According to the number of bits of the IP mask of address segment 1, that is, 24, the electronic device can obtain the first 24 bits of binary data in the fifth data string to obtain a third data string, that is, 00000000 000000000 000000000.
[0102] Step S203: for each second address segment, generate a second data string corresponding to the second address segment according to the third data string of the IP mask corresponding to the second address segment.
[0103] In an optional embodiment, the above step S203, for each second address segment, generates a second data string corresponding to the second address segment according to the third data string of the IP mask corresponding to the second address segment, which can be specifically expressed as:
[0104] For each second address segment, the third data string corresponding to the IP mask of the second address segment is determined as the second data string corresponding to the second address segment. At this time, the second data string corresponding to each second address segment is the third data string corresponding to the IP mask of the second address segment.
[0105] For ease of understanding, the third data string corresponding to the IP mask of a second address segment is 0000001000000010 00000010 for example, and the electronic device can directly use the third data string as the second data string corresponding to the second address segment. That is, the second data string corresponding to the second address segment is: 00000010 00000010 00000010.
[0106] In another optional embodiment, the above step S203, for each second address segment, generates a second data string corresponding to the second address segment according to the third data string of the IP mask corresponding to the second address segment, which can be specifically expressed as:
[0107] For each second address segment, a preset number of preset binary numbers are added after the third data string corresponding to the IP mask of the second address segment to obtain a second data string corresponding to the second address segment.
[0108] For each second address segment, since the third data string corresponding to the second address segment must be the first preset number of binary numbers of the fifth data string corresponding to any IP address in the second address segment, the electronic device can add a preset number of preset base numbers after the third data string corresponding to the IP mask of the second address segment to obtain the second data string corresponding to the second address segment. At this time, the second data string corresponding to each second address segment is a combination of the third data string corresponding to the IP mask of the second address segment and a preset number of preset binary numbers.
[0109] In the embodiment of the present application, the above-mentioned preset binary number can be 0 or 1. Here, the above-mentioned preset binary number is not specifically limited.
[0110] In an optional embodiment, since the number of bits of any IP address in the second address segment after conversion to a binary number is fixed, such as the number of bits corresponding to the IPv4 address is 32 bits, and the number of bits corresponding to the IPv6 address is 128 bits, the preset number can be the difference between the number of bits of the binary number corresponding to any IP address in the second address segment and the preset number of bits (i.e., the number of bits corresponding to the IP mask of the second address segment).
[0111] For ease of understanding, the third data string is still taken as an example where the second address segment is 00000010 00000010 00000010 and the preset binary number is 0.
[0112] Since the number of bits corresponding to an IPv4 address is 32 bits, the preset number is 8=32-24, that is, the electronic device can add 8 zeros after the third data string to obtain a second data string corresponding to the second address segment. The second data string is represented as: 00000010 00000010 00000010 00000000.
[0113] In the embodiment of the present application, the generation method of the second data string is not specifically limited. For ease of understanding, the following description is only based on the second data string being the third data string, which does not serve any limiting role.
[0114] Through the third data string corresponding to each second address segment, the electronic device can generate a second data string corresponding to each second address segment, that is, a second data string related to the IP mask of each second address segment, thereby ensuring the correlation between the first corresponding relationship generated based on the second data string and the IP mask of the second address segment, thereby facilitating the subsequent retrieval of the address segment where the IP address is located and improving retrieval efficiency.
[0115] Step S204: generating a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment.
[0116] The method for generating the above-mentioned first relationship can be referred to the description below and will not be elaborated here.
[0117] Through the above steps S201 to S204, the electronic device can generate a first corresponding relationship in advance according to each second address segment and the second data string corresponding to each second address segment.
[0118] Step S205, obtaining a first IP address.
[0119] Step S206, converting the first IP address into a binary number to obtain a first data string.
[0120] Step S207, according to the pre-stored first correspondence, retrieve the address segment matching the first data string to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0121] The above steps S205 to S207 are the same as the above steps S101 to S103.
[0122] In an optional embodiment, according to Figure 2 The method shown in the embodiment of the present application also provides an address segment retrieval method. Figure 3 As shown, Figure 3 A third flow chart of the address segment search method provided in the embodiment of the present application. The method comprises the following steps.
[0123] Step S301, obtaining multiple second address segments.
[0124] Step S302: for each second address segment, convert the IP mask corresponding to the second address segment into a binary number to obtain a third data string.
[0125] Step S303: for each second address segment, generate a second data string corresponding to the second address segment according to the third data string of the IP mask corresponding to the second address segment.
[0126] The above steps S301 to S303 are the same as the above steps S201 to S203.
[0127] Step S304, generating a correspondence between the second address segment and the second data string according to each second address segment and the second data string corresponding to the second address segment, and obtaining a first correspondence.
[0128] In this step, the electronic device can generate a correspondence between the second address segment and the second data string according to the correspondence between each second address segment and each second data string, and obtain a first correspondence, as shown in Table 1.
[0129] Table 1
[0130] Address segment Data string Address segment 1 Data string 1 Address segment 2 Data string 2 Address segment 3 Data string 3
[0131] In the corresponding relationship shown in Table 1, the data string corresponding to address segment 1 is data string 1, the data string corresponding to address segment 2 is data string 2, and the data string corresponding to address segment 3 is data string 3.
[0132] Through the above step S204, the electronic device can generate a first corresponding relationship according to each second address segment and each second address segment, so that the generated first corresponding relationship retains the association between the second address segment and the IP mask of the second address segment, thereby facilitating the subsequent retrieval process of the address segment where the IP address is located.
[0133] Step S305, obtaining a first IP address.
[0134] Step S306, converting the first IP address into a binary number to obtain a first data string.
[0135] The above steps S305 to S306 are the same as the above steps S205 to S206.
[0136] Step S307, taking the number of binary numbers included in the first data string as the target number, obtaining the first target number of binary numbers in the first data string as the fourth data string.
[0137] For ease of understanding, the first data string of 00000010 00000010 00000010 00001100 is taken as an example for explanation.
[0138] The number of binary numbers included in the first data string (i.e., the number of bits of binary data) is 32. The electronic device can obtain the first 32 bits of the binary numbers as the fourth data string. At this time, the fourth data string is: 00000010 00000010 0000001000001100.
[0139] Step S308, if the first corresponding relationship does not include the fourth data string, the target number is reduced by one, and the process returns to execute the step of obtaining the first target number of binary numbers in the first data string as the fourth data string.
[0140] In this step, after obtaining the fourth data string, the electronic device can search whether the first correspondence relationship includes the fourth data string. If the first correspondence relationship does not include the fourth data string, the electronic device can determine that the fourth data string is not the binary number corresponding to the IP mask of each second address segment included in the first correspondence relationship, that is, the fourth data string is not the binary number corresponding to the IP mask of the first IP address. At this time, the electronic device can reduce the target number by one, and return to execute the step S307 of obtaining the first target number of binary numbers in the first data string as the fourth data string, that is, re-obtain the fourth data string according to the target number after reduction by one.
[0141] Still taking the above target number of 32 and the first data string of 00000010 00000010 00000010 00001100 as an example, when the electronic device determines that the fourth data string is not included in the first corresponding relationship, the target number will be updated to 31. At this time, the re-acquired fourth data string is 00000010 00000010 00000010 0000110.
[0142] Step S309: If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0143] In this step, if the first corresponding relationship includes the fourth data string, the electronic device can determine that the fourth data string is the binary number corresponding to the IP mask of a second address segment, that is, the IP mask of the first IP address is the same as the IP mask of the second address segment corresponding to the fourth data string included in the first corresponding relationship. At this time, the electronic device can determine the second address segment corresponding to the fourth data string in the first corresponding relationship as the first address segment where the first IP address is located.
[0144] For ease of understanding, the above Table 1 is used as an example for explanation. Now assuming that the data string 2 shown in Table 1 is the same as the fourth data string, the electronic device can determine the address segment 2 shown in Table 1 as the first address segment where the above first IP address is located.
[0145] In an embodiment of the present application, the above-mentioned step S308 is executed when the first corresponding relationship does not include the fourth data string, and step S309 is executed when the first corresponding relationship includes the fourth data string. Here, the execution of the above-mentioned step S308 and step S309 is not specifically limited.
[0146] The above steps S307 to S309 are refinements of the above step S207.
[0147] In the above steps S307 to S309, the target quantity is transformed in the order from large to small. In addition, the target quantity can also be transformed in the order from small to large. When the target quantity is transformed in the order from small to large, the subtraction operation in the above step S308 will be changed to the addition operation. Here, the retrieval process of transforming the target quantity in the order from small to large is not specifically described.
[0148] In the above steps S307 to S309, the target number has a value range of 1 to the number of binary numbers included in the first data string. For example, when the first data string is 32 bits, the target data string has a value range of [1,32]. At this time, the maximum time complexity of the above retrieval process is 32. When the number of bits of the binary number corresponding to the IP mask of each first address segment is higher, the time complexity corresponding to the retrieval process is smaller, and the address segment retrieval efficiency is higher.
[0149] In addition, considering that in the actual address segment division process, a certain IP address will not be used as a first segment, the value range of the above target number can be: [1,X], where X is the number of binary numbers included in the first data string -1.
[0150] Through the above steps S307-S309, the electronic device can accurately determine the first address segment where the first IP address is located based on the first corresponding relationship and the first data string. While ensuring the accuracy of the determined first address segment, the time complexity of the address segment retrieval process is reduced, thereby improving the efficiency of the address segment retrieval.
[0151] In an optional embodiment, according to the above Figure 2 The method shown in the embodiment of the present application also provides an address segment retrieval method. Figure 4 As shown, Figure 4 A fourth flow chart of the address segment search method provided in the embodiment of the present application. The method comprises the following steps.
[0152] Step S401, obtaining multiple second address segments.
[0153] Step S402: for each second address segment, convert the IP mask corresponding to the second address segment into a binary number to obtain a third data string.
[0154] Step S403: for each second address segment, generate a second data string corresponding to the second address segment according to the third data string of the IP mask corresponding to the second address segment.
[0155] The above steps S401 to S403 are the same as the above steps S201 to S203.
[0156] Step S404: for each second address segment, using a preset hash algorithm, calculate a hash value of a second data string corresponding to the second address segment to obtain a first hash value corresponding to the second address segment.
[0157] In the embodiment of the present application, the above-mentioned preset hash algorithm includes but is not limited to the Secure Hash Algorithm 1 (SHA-1) algorithm and the Message-Digest Algorithm (MD5) algorithm. Here, the above-mentioned preset hash algorithm is not specifically limited. The calculation method of the above-mentioned first hash value can refer to the hash value calculation method in the related art, and will not be specifically described here.
[0158] Step S405 , generating a correspondence relationship between the second address segment and the first hash value corresponding to each second address segment, to obtain a first correspondence relationship.
[0159] In this step, after calculating and obtaining the first hash value corresponding to each second address segment, the electronic device can generate a correspondence relationship between each second address segment and each first hash value to obtain a first correspondence relationship, as shown in Table 2.
[0160] Table 2
[0161] Address segment Hash value Address segment 1 Hash value 1 Address segment 2 Hash value 2 Address segment 3 Hash value 3
[0162] In the corresponding relationship shown in Table 2, the hash value corresponding to address segment 1 is hash value 1, the hash value corresponding to address segment 2 is hash value 2, and the hash value corresponding to address segment 3 is hash value 3.
[0163] Through the above steps S404-S405, the electronic device can generate a first corresponding relationship based on each second address segment and each second address segment, so that the generated first corresponding relationship retains the association between the second address segment and the IP mask of the second address segment, thereby facilitating the subsequent retrieval process of the address segment where the IP address is located.
[0164] In addition, when the second data string is different, the calculated first hash value is also different. Compared with the second data string composed of multiple binary numbers, the representation of the first hash value is simpler, and the second hash value can be retrieved in the first corresponding relationship later, thereby shortening the retrieval time of the hash value and improving the efficiency of the address segment retrieval.
[0165] The above steps S404 to S405 are refinements of the above step S204.
[0166] Step S406, obtaining a first IP address.
[0167] Step S407, convert the first IP address into a binary number to obtain a first data string.
[0168] The above steps S406 to S407 are the same as the above steps S205 to S206.
[0169] Step S408, taking the number of binary numbers included in the first data string as the target number, and using a preset hash algorithm, calculate the second hash value of the first target number of binary numbers in the first data string.
[0170] The calculation method of the second Hash value of the first target number of binary numbers in the first data string may refer to the calculation method of the first Hash value, which will not be described in detail here.
[0171] Step S409, if the first corresponding relationship does not include the second hash value, the target number is reduced by one, and the process returns to the step of calculating the second hash value of the first target number of binary numbers in the first data string using a preset hash algorithm.
[0172] In this step, after obtaining the above-mentioned second hash value, the electronic device can search whether the above-mentioned first correspondence includes the second hash value. If the second hash value is not included in the first correspondence, the electronic device can determine that the second hash value is not the hash value of the binary number corresponding to the IP mask of each second address segment included in the first correspondence, that is, the second hash value is not the hash value of the binary number corresponding to the IP mask of the first IP address. At this time, the electronic device can reduce the above-mentioned target number by one, and return to the step of calculating the second hash value of the first target number of binary numbers in the first data string using the preset hash algorithm in the above-mentioned step S408. That is, the second hash value is re-obtained according to the target number after reduction by one.
[0173] Step S410: If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0174] In this step, if the first corresponding relationship includes the second hash value, the electronic device can determine that the second hash value is the hash value of the binary number corresponding to the IP mask of a second address segment, that is, the IP mask of the first IP address is the same as the IP mask of the second address segment corresponding to the second hash value included in the first corresponding relationship. At this time, the electronic device can determine the second address segment corresponding to the second hash value in the first corresponding relationship as the first address segment where the first IP address is located.
[0175] The above steps S408 to S410 are refinements of the above step S207.
[0176] In the above steps S408 to S410, the above target quantity is transformed in the order from large to small. In addition, the above target quantity can also be transformed in the order from small to large. When the above target quantity is transformed in the order from small to large, the subtraction operation in the above step S409 will be changed to the addition operation. Here, the retrieval process of transforming the target quantity in the order from small to large is not specifically described.
[0177] In the above steps S408 to S410, the target number has a value range of 1 to the number of binary numbers included in the first data string. For example, when the first data string is 32 bits, the target data string has a value range of [1,32]. At this time, the maximum time complexity of the above retrieval process is 32. When the number of bits of the binary number corresponding to the IP mask of each first address segment is higher, the time complexity corresponding to the retrieval process is smaller, and the address segment retrieval efficiency is higher.
[0178] In addition, considering that in the actual address segment division process, a certain IP address will not be used as a first segment, the value range of the above target number can be: [1,X], where X is the number of binary numbers included in the first data string -1.
[0179] Through the above steps S408-S410, the electronic device can accurately determine the first address segment where the first IP address is located based on the first corresponding relationship and the second hash value. While ensuring the accuracy of the determined first address segment, the time complexity of the address segment retrieval process is reduced, thereby improving the efficiency of the address segment retrieval.
[0180] In the above Figure 3 and Figure 4 In the illustrated embodiment, only the second data string is the third data string as an example for explanation. When the second data string is a combination of the third data string and a preset number of preset binary numbers, the electronic device needs to supplement the preset binary number after the first preset number of binary numbers of the first data string when executing the above step S307 or step S408, and the obtained data string is recorded as the sixth data string. That is, when executing the above step S307, the electronic device obtains the first target number of binary numbers from the first data string, and supplements the preset binary number after the binary number, or when executing the above step S408, the electronic device obtains the first target number of binary numbers from the first data string, supplements the preset binary number after the binary number, and uses the preset hash algorithm to calculate the second hash value of the data string after the preset binary number is supplemented. The number of binary numbers included in the sixth data string is the same as the number of binary numbers included in the second data string. Here, when the second data string is a combination of the third data string and a preset number of preset binary numbers, the retrieval process of the IP address is not specifically described.
[0181] In an optional embodiment, the first corresponding relationship may include, in addition to the second address segment and the second data string / first hash value, the number of bits of the binary number corresponding to the IP mask of each address segment.
[0182] For ease of understanding, still taking the correspondence shown in Table 1 as an example, the first correspondence including the binary number corresponding to the IP mask may be as shown in Table 3.
[0183] Table 3
[0184] Address segment Data string Number of digits Address segment 1 Data string 1 30 Address segment 2 Data string 2 25 Address segment 3 Data string 3 30
[0185] In the corresponding relationship in Table 2, the number of bits of the binary number corresponding to the IP mask of address segment 1 is 30, the number of bits of the binary number corresponding to the IP mask of address segment 2 is 25, the number of bits of the binary number corresponding to the IP mask of address segment 1 is 25, and the number of bits of the binary number corresponding to the IP mask of address segment 3 is 30.
[0186] When retrieving the first address segment where the first IP address is located, the electronic device can determine each bit number as the above-mentioned target number according to the number of bits corresponding to each address segment in the first corresponding relationship, in order from large to small or from small to large, thereby performing address segment retrieval based on the target number.
[0187] By determining each digit in the above-mentioned first correspondence as the above-mentioned target number, while ensuring that the determined target number is the same as the number of bits of the binary number corresponding to the IP mask of each address segment, it is avoided to determine the number of bits of the binary number corresponding to the IP mask not included in the first correspondence as the target number, thereby reducing the time complexity of address segment retrieval and improving the efficiency of address segment retrieval.
[0188] In an optional embodiment, according to the above Figure 1 The method shown in the embodiment of the present application also provides an address segment retrieval method. Figure 5 As shown, Figure 5 A fifth flow chart of the address segment search method provided in the embodiment of the present application. The method comprises the following steps.
[0189] Step S501, obtaining a first IP address.
[0190] Step S502: Convert the first IP address into a binary number to obtain a first data string.
[0191] Step S503, according to the pre-stored first correspondence, retrieve the address segment matching the first data string to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0192] The above steps S501 to S503 are the same as the above steps S101 to S103.
[0193] Step S504, determining the attribution information of the first IP address based on the pre-stored second correspondence between the second address segment and the attribution information and the first address segment where the first IP address is located, the attribution information including one or more of country, city, and operator.
[0194] In the embodiment of the present application, when generating the above-mentioned first corresponding relationship, the electronic device may also obtain the attribution information corresponding to each second address segment, thereby generating a second corresponding relationship between each second address segment and the attribution information.
[0195] For ease of understanding, the above Table 1 is still used as an example for explanation. The second corresponding relationship including the attribution information may be as shown in Table 4.
[0196] Table 4
[0197] Address segment Data string Attribution Information Address segment 1 Data string 1 Information 1 Address segment 2 Data string 2 Information 2 Address segment 3 Data string 3 Information 3
[0198] In the corresponding relationship shown in Table 4, the attribution information corresponding to address segment 1 is information 1, the attribution information corresponding to address segment 2 is information 2, and the attribution information corresponding to address segment 3 is information 3.
[0199] After determining the first address segment where the first IP address is located, the electronic device can determine the attribution information corresponding to the first address segment according to the second corresponding relationship to obtain the attribution information of the first IP address.
[0200] The first address segment where the first IP address is located is an address segment 2 shown in Table 4. According to the corresponding relationship shown in Table 4, the electronic device can determine that the attribution information of the first IP address is information 2.
[0201] The above-mentioned attribution information may include one or more of a country, a city, and an operator. Here, the above-mentioned attribution information is not limited.
[0202] In an embodiment of the present application, through the above-mentioned second corresponding relationship, the electronic device can accurately determine the attribution information of the first IP address, while ensuring the accuracy of the determined attribution information, and facilitating corresponding operations based on the attribution information, such as server allocation.
[0203] Based on the same inventive concept, according to the address segment retrieval method provided in the above embodiment of the present application, the embodiment of the present application also provides an address segment retrieval device. Figure 6 As shown, Figure 6 A schematic diagram of the structure of an address segment search device provided in an embodiment of the present application. The device includes the following modules.
[0204] A first acquisition module 601, used to acquire a first IP address;
[0205] A first conversion module 602, configured to convert the first IP address into a binary number to obtain a first data string;
[0206] The retrieval module 603 is used to retrieve the address segment matching the first data string according to a pre-stored first correspondence relationship, and obtain the first address segment where the first IP address is located, wherein the first correspondence relationship is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0207] Optionally, the above-mentioned address segment retrieval device may further include:
[0208] A second acquisition module, used to acquire multiple second address segments;
[0209] A second conversion module is used for converting the IP mask corresponding to each second address segment into a binary number to obtain a third data string;
[0210] A first generating module is used to generate, for each second address segment, a second data string corresponding to the second address segment according to a third data string of the IP mask corresponding to the second address segment;
[0211] The second generating module is used to generate a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment.
[0212] Optionally, the first generating module may be specifically configured to determine, for each second address segment, a third data string of the IP mask corresponding to the second address segment as the second data string corresponding to the second address segment.
[0213] Optionally, the above-mentioned first generating module can be specifically used to supplement a preset number of preset binary numbers after the third data string of the IP mask corresponding to each second address segment, so as to obtain a second data string corresponding to the second address segment.
[0214] Optionally, the second generating module may be specifically used to generate a correspondence between the second address segment and the second data string according to each second address segment and the second data string corresponding to the second address segment, so as to obtain a first correspondence.
[0215] Optionally, the retrieval module 603 may be specifically configured to take the number of binary numbers included in the first data string as a target number, and obtain the first target number of binary numbers in the first data string as a fourth data string;
[0216] If the fourth data string is not included in the first corresponding relationship, the target number is reduced by one, and the process returns to the step of obtaining the first target number of binary numbers in the first data string as the fourth data string;
[0217] If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0218] Optionally, the second generating module may be specifically configured to calculate, for each second address segment, a hash value of a second data string corresponding to the second address segment using a preset hash algorithm to obtain a first hash value corresponding to the second address segment;
[0219] According to each second address segment and the first hash value corresponding to the second address segment, a correspondence relationship between the second address segment and the first hash value is generated to obtain a first correspondence relationship.
[0220] Optionally, the retrieval module 603 may be specifically configured to take the number of binary numbers included in the first data string as the target number, and calculate the second hash value of the first target number of binary numbers in the first data string using a preset hash algorithm;
[0221] If the second hash value is not included in the first corresponding relationship, the target number is reduced by one, and the process returns to the step of calculating the second hash value of the first target number of binary numbers in the first data string by using a preset hash algorithm;
[0222] If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
[0223] Optionally, the above-mentioned address segment retrieval device may further include:
[0224] The determination module is used to determine the attribution information of the first IP address based on the second correspondence between the second address segment and the attribution information stored in advance, and the first address segment where the first IP address is located, and the attribution information includes one or more of the country, city, and operator.
[0225] Through the device provided by the embodiment of the present application, the first IP address to be retrieved can be converted into a binary number to obtain a first data string, so as to retrieve the first address segment where the first IP address is located according to the pre-stored first corresponding relationship. Compared with the related art, since the first corresponding relationship is generated according to each second address segment and the second data string corresponding to each second address segment, that is, it is generated according to the binary number corresponding to each second address segment and the IP mask of the second address segment, considering that the IP mask of the same address segment is the same, that is, the binary number corresponding to the IP mask of the same address segment is the same, therefore, the binary number corresponding to the IP mask in the first data string of the first IP address is the same as the binary number corresponding to the IP mask in the second address segment where the first IP address is located. Based on this, when retrieving the first address segment where the first IP address is located according to the first data string and the first corresponding relationship, the maximum value of the time complexity is the number of bits of the binary number of the first IP address, and when the number of address segments is smaller, the time complexity of the retrieval is smaller, which effectively reduces the time complexity of the address segment retrieval, thereby improving the efficiency of the address segment retrieval.
[0226] Based on the same inventive concept, according to the address segment retrieval method provided in the above embodiment of the present application, the embodiment of the present application also provides an electronic device, such as Figure 7 As shown, it includes a processor 701, a communication interface 702, a memory 703 and a communication bus 704, wherein the processor 701, the communication interface 702, and the memory 703 communicate with each other through the communication bus 704.
[0227] Memory 703, used for storing computer programs;
[0228] The processor 701 is used to execute the program stored in the memory 703 to implement the following steps:
[0229] Get the first IP address;
[0230] Convert the first IP address into a binary number to obtain a first data string;
[0231] According to the pre-stored first correspondence, the address segment matching the first data string is retrieved to obtain the first address segment where the first IP address is located, wherein the first correspondence is generated based on each second address segment and the second data string corresponding to each second address segment, and the second data string corresponding to each second address segment is generated based on the binary number corresponding to the IP mask of the second address segment.
[0232] By means of the electronic device provided by the embodiment of the present application, the first IP address to be retrieved can be converted into a binary number to obtain a first data string, so as to retrieve the first address segment where the first IP address is located according to the first correspondence relationship pre-stored. Compared with the related art, since the first correspondence relationship is generated according to each second address segment and the second data string corresponding to each second address segment, that is, it is generated according to the binary number corresponding to each second address segment and the IP mask of the second address segment, considering that the IP mask of the same address segment is the same, that is, the binary number corresponding to the IP mask of the same address segment is the same, therefore, the binary number corresponding to the IP mask in the first data string of the first IP address is the same as the binary number corresponding to the IP mask in the second address segment where the first IP address is located. Based on this, when retrieving the first address segment where the first IP address is located according to the first data string and the first correspondence relationship, the maximum value of the time complexity is the number of bits of the binary number of the first IP address, and when the number of address segments is less, the time complexity of the retrieval is smaller, which effectively reduces the time complexity of the address segment retrieval, thereby improving the efficiency of the address segment retrieval.
[0233] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0234] The communication interface is used for communication between the above electronic device and other devices.
[0235] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0236] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0237] Based on the same inventive concept, according to the address segment retrieval method provided in the above-mentioned embodiments of the present application, the embodiments of the present application also provide a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps of any of the above-mentioned address segment retrieval methods are implemented.
[0238] Based on the same inventive concept, according to the address segment retrieval method provided in the above-mentioned embodiments of the present application, the embodiments of the present application also provide a computer program product containing instructions, which, when running on a computer, enables the computer to execute any address segment retrieval method in the above-mentioned embodiments.
[0239] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0240] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0241] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for embodiments such as devices, electronic devices, computer-readable storage media, and computer program products, since they are basically similar to method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.
[0242] The above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application are included in the protection scope of the present application.
Claims
1. An address segment retrieval method, characterized in that: The method comprises: Obtaining a first Internet Protocol IP address; Convert the first IP address into a binary number to obtain a first data string; According to a pre-stored first correspondence relationship, an address segment matching the first data string is retrieved to obtain a first address segment where the first IP address is located, wherein the first correspondence relationship is a correspondence relationship between the second data string and the second address segment; The first corresponding relationship is generated in a manner including: For each second address segment, convert any IP address in the second address segment into a binary number to obtain a data string corresponding to the IP address, and according to the number of bits of the IP mask corresponding to the second address segment, determine the first preset number of binary numbers in the data string corresponding to the IP address as the binary numbers corresponding to the IP mask of the second address segment to obtain a second data string; wherein the preset number of bits is the number of bits of the IP mask corresponding to the second address segment; Generate a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment; The step of retrieving the address segment matching the first data string according to the pre-stored first corresponding relationship to obtain the first address segment where the first IP address is located includes: Taking the number of binary numbers included in the first data string as a target number, obtaining the first target number of binary numbers in the first data string as a fourth data string; If the fourth data string is not included in the first corresponding relationship, the target number is reduced by one, and the step of obtaining the first target number of binary numbers in the first data string as the fourth data string is returned to be executed; If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
2. The method according to claim 1, characterized in that The step of generating a first corresponding relationship based on each second address segment and the second data string corresponding to each second address segment includes: For each second address segment, using a preset hash algorithm, calculate a hash value of a second data string corresponding to the second address segment to obtain a first hash value corresponding to the second address segment; According to each second address segment and the first hash value corresponding to the second address segment, a correspondence relationship between the second address segment and the first hash value is generated to obtain a first correspondence relationship.
3. The method according to claim 2, characterized in that The step of retrieving the address segment matching the first data string according to the pre-stored first corresponding relationship to obtain the first address segment where the first IP address is located includes: Taking the number of binary numbers included in the first data string as the target number, and using a preset hash algorithm, calculating the second hash values of the first target number of binary numbers in the first data string; If the second hash value is not included in the first corresponding relationship, the target number is reduced by one, and the step of calculating the second hash values of the first target number of binary numbers in the first data string by using the preset hash algorithm is returned to execution; If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
4. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: The attribution information of the first IP address is determined based on a pre-stored second correspondence between the second address segment and the attribution information, and the first address segment where the first IP address is located, where the attribution information includes one or more of a country, a city, and an operator.
5. An address segment search device, characterized in that: The device comprises: A first acquisition module, used to acquire a first Internet Protocol IP address; A first conversion module, used for converting the first IP address into a binary number to obtain a first data string; A retrieval module, configured to retrieve an address segment matching the first data string according to a pre-stored first correspondence relationship, and obtain a first address segment where the first IP address is located, wherein the first correspondence relationship is a correspondence relationship between a second data string and a second address segment; The first corresponding relationship is generated in a manner including: For each second address segment, convert any IP address in the second address segment into a binary number to obtain a data string corresponding to the IP address, and according to the number of bits of the IP mask corresponding to the second address segment, determine the first preset number of binary numbers in the data string corresponding to the IP address as the binary numbers corresponding to the IP mask of the second address segment to obtain a second data string; wherein the preset number of bits is the number of bits of the IP mask corresponding to the second address segment; Generate a first corresponding relationship based on each second address segment and a second data string corresponding to each second address segment; The retrieval module is specifically configured to take the number of binary numbers included in the first data string as a target number, and obtain the first target number of binary numbers in the first data string as a fourth data string; If the fourth data string is not included in the first corresponding relationship, the target number is reduced by one, and the step of obtaining the first target number of binary numbers in the first data string as the fourth data string is returned to be executed; If the first corresponding relationship includes the fourth data string, the second address segment corresponding to the fourth data string in the first corresponding relationship is determined as the first address segment where the first IP address is located.
6. The device according to claim 5, characterized in that The generating the first corresponding relationship based on each second address segment and the second data string corresponding to each second address segment includes: for each second address segment, using a preset hash algorithm, calculating a hash value of the second data string corresponding to the second address segment to obtain a first hash value corresponding to the second address segment; According to each second address segment and the first hash value corresponding to the second address segment, a correspondence relationship between the second address segment and the first hash value is generated to obtain a first correspondence relationship.
7. The device according to claim 6, characterized in that The retrieval module is specifically configured to calculate the second hash values of the first target number of binary numbers in the first data string by using a preset hash algorithm, taking the number of binary numbers included in the first data string as the target number; If the second hash value is not included in the first corresponding relationship, the target number is reduced by one, and the step of calculating the second hash values of the first target number of binary numbers in the first data string by using the preset hash algorithm is returned to execution; If the first corresponding relationship includes the second hash value, the second address segment corresponding to the first hash value in the first corresponding relationship is determined as the first address segment where the first IP address is located.
8. The device according to any one of claims 5 to 7, characterized in that: The device also includes: A determination module is used to determine the attribution information of the first IP address based on a second correspondence between the second address segment and the attribution information pre-stored, and the first address segment where the first IP address is located, wherein the attribution information includes one or more of a country, a city, and an operator.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing any of the methods described in claims 1-4 when executing a program stored in a memory.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
