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