• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "common_util.h"
17 #include <ctype.h>
18 #include <securec.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include "dhcp_define.h"
27 #include "dhcp_logger.h"
28 
29 #define NEW_FILEPATH_MODE 0755
30 #define TIME_BASE_YEAR 1900
31 #define TIME_SEC_TO_USEC (1000 * 1000)
32 
33 #undef LOG_TAG
34 #define LOG_TAG "DhcpServerCommon"
35 
36 typedef struct tm *ptm;
LogTime(void)37 void LogTime(void)
38 {
39     time_t curr;
40     time(&curr);
41     struct tm nowTime;
42     localtime_r(&curr, &nowTime);
43     ptm tt = &nowTime;
44     if (tt) {
45         tt->tm_year += TIME_BASE_YEAR;
46         printf("[%04d-%02d-%02d %02d:%02d:%02d", tt->tm_year, tt->tm_mon + 1,
47             tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);
48     } else {
49         printf("[1970-01-01 08:00:00");
50     }
51 }
52 
Tmspusec(void)53 uint64_t Tmspusec(void)
54 {
55     struct timeval t;
56     gettimeofday(&t, 0);
57     return (uint64_t)((long long)t.tv_sec * TIME_SEC_TO_USEC + t.tv_usec);
58 }
59 
Tmspsec(void)60 uint64_t Tmspsec(void)
61 {
62     struct timeval t;
63     gettimeofday(&t, 0);
64     return (uint64_t)((long long)t.tv_sec);
65 }
66 
LeftTrim(char * buf)67 void LeftTrim(char *buf)
68 {
69     if (buf == NULL || (buf[0] != ' ' && buf[0] != '\t')) {
70         return;
71     }
72     int i = 0;
73     while (buf[i] == ' ' || buf[i] == '\t') {
74         ++i;
75     }
76     int j = 0;
77     while (buf[i] != '\0') {
78         buf[j++] = buf[i++];
79     }
80     buf[j] = '\0';
81     return;
82 }
83 
RightTrim(char * buf)84 void RightTrim(char *buf)
85 {
86     if (buf == NULL || buf[0] == '\0') {
87         return;
88     }
89     int i = strlen(buf) - 1;
90     while (i >= 0 && (buf[i] == ' ' || buf[i] == '\t')) {
91         buf[i] = '\0';
92         --i;
93     }
94     return;
95 }
96 
TrimString(char * buf)97 void TrimString(char *buf)
98 {
99     RightTrim(buf);
100     LeftTrim(buf);
101     return;
102 }
103 
GetFilePath(const char * fileName)104 const char *GetFilePath(const char *fileName)
105 {
106     static char currFilePath[DHCP_MAX_PATH_LENGTH];
107     if (!fileName) {
108         return 0;
109     }
110     int flen = strlen(fileName);
111     if (flen == 0) {
112         return 0;
113     }
114     char currName[DHCP_MAX_PATH_LENGTH] = {'\0'};
115     if (memcpy_s(currName, sizeof(currName), fileName, strlen(fileName)) != EOK) {
116         return 0;
117     }
118     char *last = strrchr(currName, '/');
119     if (last) {
120         *last = '\0';
121     }
122     if (memset_s(currFilePath, sizeof(currFilePath), '\0', sizeof(currFilePath)) != EOK) {
123         return 0;
124     }
125     if (memcpy_s(currFilePath, sizeof(currFilePath), currName, strlen(currName)) != EOK) {
126         return 0;
127     }
128     return currFilePath;
129 }
130 
GetLeaseFile(const char * fileName,const char * ifname)131 const char *GetLeaseFile(const char *fileName, const char *ifname)
132 {
133     static char leaseFileName[DHCP_MAX_PATH_LENGTH];
134     if (!fileName || !ifname) {
135         return 0;
136     }
137     if (strlen(fileName) == 0 || strlen(ifname) == 0) {
138         return 0;
139     }
140     if (snprintf_s(leaseFileName, sizeof(leaseFileName), sizeof(leaseFileName) - 1, "%s.%s", fileName, ifname) < 0) {
141         LOGE("Failed to get dhcp lease file path!");
142         return 0;
143     }
144     return leaseFileName;
145 }
146 
CreatePath(const char * fileName)147 int CreatePath(const char *fileName)
148 {
149     if (!fileName) {
150         return RET_FAILED;
151     }
152     int len = strlen(fileName);
153     if (!len) {
154         return RET_FAILED;
155     }
156     char filePath[DHCP_MAX_PATH_LENGTH] = {'\0'};
157     if (strncpy_s(filePath, sizeof(filePath), fileName, len) != EOK) {
158         return RET_FAILED;
159     }
160     for (int i = 0; i < len; i++) {
161         if (filePath[i] == '/') {
162             filePath[i] = '\0';
163             if (access(filePath, 0) != 0) {
164                 mkdir(filePath, NEW_FILEPATH_MODE);
165             }
166             filePath[i] = '/';
167         }
168     }
169     if (len > 0 && access(filePath, 0) != 0) {
170         mkdir(filePath, NEW_FILEPATH_MODE);
171     }
172     return RET_SUCCESS;
173 }
174 
RemoveSpaceCharacters(char * buf,size_t bufSize)175 int RemoveSpaceCharacters(char *buf, size_t bufSize)
176 {
177     if ((buf == NULL) || (strlen(buf) == 0) || (bufSize == 0)) {
178         LOGE("RemoveSpaceCharacters() buf == NULL or len == 0!");
179         return DHCP_FALSE;
180     }
181 
182     /* Handle rightmost spaces. */
183     int nEnd = strlen(buf) - 1;
184     while ((nEnd >= 0) && isspace(buf[nEnd])) {
185         buf[nEnd--] = '\0';
186     }
187 
188     /* Handle leftmost spaces. */
189     int i = 0;
190     while (isspace(buf[i])) {
191         buf[i++] = '\0';
192     }
193 
194     int j = 0;
195     while (buf[i] != '\0') {
196         buf[j++] = buf[i++];
197     }
198     buf[j] = '\0';
199 
200     return DHCP_TRUE;
201 }
202