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