1 /*
2 * Copyright (C) 2024 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 #include <net/if.h>
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <unistd.h>
22 #include <hdf_log.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <regex.h>
26 #include "securec.h"
27 #include "wpa_hdi_util.h"
28
29 #define MAC_UINT_SIZE 6
30 #define MAC_STRING_SIZE 17
31
ConvertMacToStr(const unsigned char * mac,int macSize,char * macStr,int strLen)32 int ConvertMacToStr(const unsigned char *mac, int macSize, char *macStr, int strLen)
33 {
34 if (mac == NULL || macStr == NULL || macSize != MAC_UINT_SIZE || strLen <= MAC_STRING_SIZE) {
35 return -1;
36 }
37 const int posZero = 0;
38 const int posOne = 1;
39 const int posTwo = 2;
40 const int posThree = 3;
41 const int posFour = 4;
42 const int posFive = 5;
43 if (snprintf_s(macStr, strLen, strLen - 1, "%02x:%02x:%02x:%02x:%02x:%02x", mac[posZero], mac[posOne], mac[posTwo],
44 mac[posThree], mac[posFour], mac[posFive]) < 0) {
45 return -1;
46 }
47 return 0;
48 }
49
IsValidHexCharAndConvert(char c)50 char IsValidHexCharAndConvert(char c)
51 {
52 if (c >= '0' && c <= '9') {
53 return c - '0';
54 }
55 if (c >= 'a' && c <= 'f') {
56 return c - 'a' + ('9' - '0' + 1);
57 }
58 if (c >= 'A' && c <= 'F') {
59 return c - 'A' + ('9' - '0' + 1);
60 }
61 return -1;
62 }
63
ConvertMacToArray(const char * macStr,unsigned char * mac,int macSize)64 int ConvertMacToArray(const char *macStr, unsigned char *mac, int macSize)
65 {
66 if (macStr == NULL || mac == NULL || macSize != MAC_UINT_SIZE || strlen(macStr) != MAC_STRING_SIZE) {
67 return -1;
68 }
69 const int shiftNum = 4;
70 const int macSpaceNum = 3;
71 unsigned char tmp = 0;
72 for (int i = 0, j = 0; i < MAC_STRING_SIZE; ++i) {
73 if (j == 0 || j == 1) {
74 int8_t v = IsValidHexCharAndConvert(macStr[i]);
75 if (v < 0) {
76 return -1;
77 }
78 tmp <<= shiftNum;
79 tmp |= v;
80 ++j;
81 } else {
82 if (macStr[i] != ':') {
83 return -1;
84 }
85 mac[i / macSpaceNum] = tmp;
86 j = 0;
87 tmp = 0;
88 }
89 }
90 mac[MAC_STRING_SIZE / macSpaceNum] = tmp;
91 return 0;
92 }
93
CheckMacIsValid(const char * macStr)94 int CheckMacIsValid(const char *macStr)
95 {
96 if (macStr == NULL || strlen(macStr) != MAC_STRING_SIZE) {
97 return -1;
98 }
99 for (int i = 0, j = 0; i < MAC_STRING_SIZE; ++i) {
100 if (j == 0 || j == 1) {
101 int v = IsValidHexCharAndConvert(macStr[i]);
102 if (v < 0) {
103 return -1;
104 }
105 ++j;
106 } else {
107 if (macStr[i] != ':') {
108 return -1;
109 }
110 j = 0;
111 }
112 }
113 return 0;
114 }
115
GetIfaceState(const char * ifaceName)116 int GetIfaceState(const char *ifaceName)
117 {
118 int state = 0;
119 int sock = socket(AF_INET, SOCK_DGRAM, 0);
120 if (sock < 0) {
121 HDF_LOGE("GetIfaceState: create socket fail");
122 return state;
123 }
124
125 struct ifreq ifr;
126 (void)memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr));
127 if (strcpy_s(ifr.ifr_name, IFNAMSIZ, ifaceName) != EOK) {
128 HDF_LOGE("GetIfaceState: strcpy_s fail");
129 close(sock);
130 return state;
131 }
132 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
133 HDF_LOGE("GetIfaceState: can not get interface state: %{public}s", ifaceName);
134 close(sock);
135 return state;
136 }
137 state = ((ifr.ifr_flags & IFF_UP) > 0 ? 1 : 0);
138 HDF_LOGD("GetIfaceState: current interface state: %{public}d", state);
139 close(sock);
140 return state;
141 }
142
CharReplace(char * data,int start,int end,const char hiddenChar)143 static int CharReplace(char* data, int start, int end, const char hiddenChar)
144 {
145 if (!data) {
146 HDF_LOGE("CharReplace: data invalid.");
147 return 1;
148 }
149 for (int i = start; i < end; i++) {
150 data[i] = hiddenChar;
151 }
152
153 return 0;
154 }
155
DataAnonymize(const char * input,int inputLen,char * output,int outputSize)156 int DataAnonymize(const char *input, int inputLen, char* output, int outputSize)
157 {
158 if (!input || !output || inputLen > outputSize) {
159 HDF_LOGE("DataAnonymize: arg invalid.");
160 return 1;
161 }
162
163 if (memcpy_s(output, outputSize, input, inputLen) != EOK) {
164 HDF_LOGE("DataAnonymize: memcpy_s fail");
165 return 1;
166 }
167
168 const char hiddenChar = '*';
169 const int minHiddenSize = 3;
170 const int headKeepSize = 3;
171 const int tailKeepSize = 3;
172
173 if (inputLen < minHiddenSize) {
174 return CharReplace(output, 0, inputLen, hiddenChar);
175 }
176
177 if (inputLen < (minHiddenSize + headKeepSize + tailKeepSize)) {
178 int beginIndex = 1;
179 int hiddenSize = inputLen - minHiddenSize + 1;
180 hiddenSize = hiddenSize > minHiddenSize ? minHiddenSize : hiddenSize;
181 return CharReplace(output, beginIndex, beginIndex + hiddenSize, hiddenChar);
182 }
183 return CharReplace(output, headKeepSize, inputLen - tailKeepSize, hiddenChar);
184 }
185
StrtoUint(const char * input)186 unsigned int StrtoUint(const char *input)
187 {
188 if (input == NULL || input[0] == '\0' || strlen(input) > MAX_UINT32_LENGTH) {
189 HDF_LOGE("StrtoUint: invalid data!");
190 return 0;
191 }
192 char *endPtr = NULL;
193 unsigned long result = 0;
194 result = (unsigned long)strtol(input, &endPtr, NUMBER_BASE);
195
196 if (endPtr == input || *endPtr != '\0') {
197 HDF_LOGE("StrtoUint: invalid data!");
198 return 0;
199 } else if (errno == ERANGE) {
200 HDF_LOGE("StrtoUint: failed!");
201 return 0;
202 } else {
203 return (unsigned int)result;
204 }
205 }
206
StrtoInt(const char * input)207 int StrtoInt(const char *input)
208 {
209 if (input == NULL || input[0] == '\0' || strlen(input) > MAX_INT32_LENGTH) {
210 HDF_LOGE("StrtoInt: invalid data!");
211 return 0;
212 }
213 char *endPtr = NULL;
214 long result = 0;
215 result = strtol(input, &endPtr, NUMBER_BASE);
216
217 if (endPtr == input || *endPtr != '\0') {
218 HDF_LOGE("StrtoInt: invalid data!");
219 return 0;
220 } else if (errno == ERANGE) {
221 HDF_LOGE("StrtoInt: failed!");
222 return 0;
223 } else {
224 return (int)result;
225 }
226 }
227
228