1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_IP_TOOLS_H
17 #define OHOS_IP_TOOLS_H
18 
19 #include <algorithm>
20 #include <arpa/inet.h>
21 #include <cstring>
22 #include <iomanip>
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
28 namespace OHOS {
29 namespace Wifi {
30 static const int MIN_PREFIX_LEN = 0;
31 static const int MAX_PREFIX_LEN = 32;
32 static const int MAX_IPV6_PREFIX_LEN = 128;
33 static const int BIT_NUM_PER_BYTE = 8;
34 static const int IPV4_BYTE_NUM = 4;
35 static const unsigned int IPV4_DOT_NUM = 3;
36 static const int IPV6_BYTE_NUM = 16;
37 static const int IPV6_DIGIT_NUM_PER_SEG = 4;
38 static const int IPV6_COLON_NUM = 7;
39 static const int MAX_IPV4_MASK_BYTE = 255;
40 static const int POS_0 = 0;
41 static const int POS_1 = 1;
42 static const int POS_2 = 2;
43 static const int POS_3 = 3;
44 static const int HEX_BYTE_DIGIT_NUM = 2;
45 static const int HEX_FORM = 16;
46 static const int MIN_BYTE = 0;
47 static const int MAX_BYTE = 255;
48 static const unsigned int BIT_NUM_BYTE = 8;
49 static const int BITS_24 = 24;
50 static const int BITS_16 = 16;
51 static const int BITS_8 = 8;
52 
53 class IpTools {
54 public:
55     /**
56      * @Description : Converts an IPv4 address in the int format to a string format.
57      *
58      * @param addressIpv4 - IPv4 address in the int format.[in]
59      * @return std::string : IPv4 address in string format.
60      */
61     static std::string ConvertIpv4Address(unsigned int addressIpv4);
62 
63     /**
64      * @Description : Converts an IPv4 address in string format to an int format.
65      *
66      * @param address - IPv4 address in string format.[in]
67      * @return unsigned int : Specifies the IPv4 address in the int format. 0 is failure.
68      */
69     static unsigned int ConvertIpv4Address(const std::string &address);
70 
71     /**
72      * @Description : Converts a 16-byte IPv6 address to a string.
73      *
74      * @param addressIpv6 - 16-byte IPv6 address.[in]
75      * @return std::string : IPv6 address in string format
76      */
77     static std::string ConvertIpv6Address(const std::vector<unsigned char> &addressIpv6);
78 
79     /**
80      * @Description : Converts the IPv6 address of a string into a 16-byte format.
81      *
82      * @param address - IPv6 address in string format.[in]
83      * @param addressIpv6 - 16-byte IPv6 address.[in]
84      */
85     static void ConvertIpv6Address(const std::string &address, std::vector<unsigned char> &addressIpv6);
86 
87     /**
88      * @Description : Converts the prefix length to an IPv4 mask.
89      *
90      * @param prefixLength - Prefix Length.[in]
91      * @return std::string : IPv4 mask in string format.
92      */
93     static std::string ConvertIpv4Mask(int prefixLength);
94 
95     /**
96      * @Description : Converts the prefix length in the int format to the string format.
97      *
98      * @param prefixLength - Prefix Length.[in]
99      * @return std::string : Prefix length in string format.
100      */
101     static std::string ConvertIpv6Mask(int prefixLength);
102 
103     /**
104      * @Description : Obtains the length based on the subnet mask.
105      *
106      * @param mask - The mask.[in]
107      * @return int
108      */
109     static int GetMaskLength(std::string mask);
110 
111     /**
112      * @Description : Obtains the length based on the subnet mask.
113      *
114      * @param mask - The mask.[in]
115      * @return int
116      */
117     static int GetIPV6MaskLength(std::string mask);
118 
119     /**
120      * @Description : Resolving Web Sites Bypassing Agents.
121      *
122      * @param exclusionObjectList - Website list, separated by commas(,).[in]
123      * @param exclusionList - Website List.[in]
124      */
125     static void GetExclusionObjectList(const std::string &exclusionObjectList, std::vector<std::string> &exclusionList);
126 };
127 }  // namespace Wifi
128 }  // namespace OHOS
129 
130 #endif