1518678f8Sopenharmony_ci/* 2518678f8Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License. 5518678f8Sopenharmony_ci * You may obtain a copy of the License at 6518678f8Sopenharmony_ci * 7518678f8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8518678f8Sopenharmony_ci * 9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and 13518678f8Sopenharmony_ci * limitations under the License. 14518678f8Sopenharmony_ci */ 15518678f8Sopenharmony_ci 16518678f8Sopenharmony_ci#include "common_util.h" 17518678f8Sopenharmony_ci#include <ctype.h> 18518678f8Sopenharmony_ci#include <securec.h> 19518678f8Sopenharmony_ci#include <stdint.h> 20518678f8Sopenharmony_ci#include <stdio.h> 21518678f8Sopenharmony_ci#include <string.h> 22518678f8Sopenharmony_ci#include <sys/stat.h> 23518678f8Sopenharmony_ci#include <sys/time.h> 24518678f8Sopenharmony_ci#include <time.h> 25518678f8Sopenharmony_ci#include <unistd.h> 26518678f8Sopenharmony_ci#include "dhcp_s_define.h" 27518678f8Sopenharmony_ci#include "dhcp_logger.h" 28518678f8Sopenharmony_ci 29518678f8Sopenharmony_ci#define NEW_FILEPATH_MODE 0755 30518678f8Sopenharmony_ci#define TIME_BASE_YEAR 1900 31518678f8Sopenharmony_ci#define TIME_SEC_TO_USEC (1000 * 1000) 32518678f8Sopenharmony_ci 33518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpServerCommon"); 34518678f8Sopenharmony_ci 35518678f8Sopenharmony_citypedef struct tm *ptm; 36518678f8Sopenharmony_civoid DHCP_LOGTime(void) 37518678f8Sopenharmony_ci{ 38518678f8Sopenharmony_ci time_t curr; 39518678f8Sopenharmony_ci (void)time(&curr); 40518678f8Sopenharmony_ci struct tm nowTime; 41518678f8Sopenharmony_ci localtime_r(&curr, &nowTime); 42518678f8Sopenharmony_ci ptm tt = &nowTime; 43518678f8Sopenharmony_ci if (tt) { 44518678f8Sopenharmony_ci tt->tm_year += TIME_BASE_YEAR; 45518678f8Sopenharmony_ci printf("[%04d-%02d-%02d %02d:%02d:%02d", tt->tm_year, tt->tm_mon + 1, 46518678f8Sopenharmony_ci tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec); 47518678f8Sopenharmony_ci } else { 48518678f8Sopenharmony_ci printf("[1970-01-01 08:00:00"); 49518678f8Sopenharmony_ci } 50518678f8Sopenharmony_ci} 51518678f8Sopenharmony_ci 52518678f8Sopenharmony_ciuint64_t Tmspusec(void) 53518678f8Sopenharmony_ci{ 54518678f8Sopenharmony_ci struct timeval t; 55518678f8Sopenharmony_ci gettimeofday(&t, 0); 56518678f8Sopenharmony_ci return (uint64_t)((long long)t.tv_sec * TIME_SEC_TO_USEC + t.tv_usec); 57518678f8Sopenharmony_ci} 58518678f8Sopenharmony_ci 59518678f8Sopenharmony_ciuint64_t Tmspsec(void) 60518678f8Sopenharmony_ci{ 61518678f8Sopenharmony_ci struct timeval t; 62518678f8Sopenharmony_ci gettimeofday(&t, 0); 63518678f8Sopenharmony_ci return (uint64_t)((long long)t.tv_sec); 64518678f8Sopenharmony_ci} 65518678f8Sopenharmony_ci 66518678f8Sopenharmony_civoid LeftTrim(char *buf) 67518678f8Sopenharmony_ci{ 68518678f8Sopenharmony_ci if (buf == nullptr || (buf[0] != ' ' && buf[0] != '\t')) { 69518678f8Sopenharmony_ci return; 70518678f8Sopenharmony_ci } 71518678f8Sopenharmony_ci int i = 0; 72518678f8Sopenharmony_ci while (buf[i] == ' ' || buf[i] == '\t') { 73518678f8Sopenharmony_ci ++i; 74518678f8Sopenharmony_ci } 75518678f8Sopenharmony_ci int j = 0; 76518678f8Sopenharmony_ci while (buf[i] != '\0') { 77518678f8Sopenharmony_ci buf[j++] = buf[i++]; 78518678f8Sopenharmony_ci } 79518678f8Sopenharmony_ci buf[j] = '\0'; 80518678f8Sopenharmony_ci return; 81518678f8Sopenharmony_ci} 82518678f8Sopenharmony_ci 83518678f8Sopenharmony_civoid RightTrim(char *buf) 84518678f8Sopenharmony_ci{ 85518678f8Sopenharmony_ci if (buf == nullptr || buf[0] == '\0') { 86518678f8Sopenharmony_ci return; 87518678f8Sopenharmony_ci } 88518678f8Sopenharmony_ci int i = strlen(buf) - 1; 89518678f8Sopenharmony_ci while (i >= 0 && (buf[i] == ' ' || buf[i] == '\t')) { 90518678f8Sopenharmony_ci buf[i] = '\0'; 91518678f8Sopenharmony_ci --i; 92518678f8Sopenharmony_ci } 93518678f8Sopenharmony_ci return; 94518678f8Sopenharmony_ci} 95518678f8Sopenharmony_ci 96518678f8Sopenharmony_civoid TrimString(char *buf) 97518678f8Sopenharmony_ci{ 98518678f8Sopenharmony_ci RightTrim(buf); 99518678f8Sopenharmony_ci LeftTrim(buf); 100518678f8Sopenharmony_ci return; 101518678f8Sopenharmony_ci} 102518678f8Sopenharmony_ci 103518678f8Sopenharmony_ciconst char *GetFilePath(const char *fileName) 104518678f8Sopenharmony_ci{ 105518678f8Sopenharmony_ci static char currFilePath[DHCP_MAX_PATH_LENGTH]; 106518678f8Sopenharmony_ci if (!fileName) { 107518678f8Sopenharmony_ci return 0; 108518678f8Sopenharmony_ci } 109518678f8Sopenharmony_ci int flen = strlen(fileName); 110518678f8Sopenharmony_ci if (flen == 0) { 111518678f8Sopenharmony_ci return 0; 112518678f8Sopenharmony_ci } 113518678f8Sopenharmony_ci char currName[DHCP_MAX_PATH_LENGTH] = {'\0'}; 114518678f8Sopenharmony_ci if (memcpy_s(currName, sizeof(currName), fileName, strlen(fileName)) != EOK) { 115518678f8Sopenharmony_ci return 0; 116518678f8Sopenharmony_ci } 117518678f8Sopenharmony_ci char *last = strrchr(currName, '/'); 118518678f8Sopenharmony_ci if (last) { 119518678f8Sopenharmony_ci *last = '\0'; 120518678f8Sopenharmony_ci } 121518678f8Sopenharmony_ci if (memset_s(currFilePath, sizeof(currFilePath), '\0', sizeof(currFilePath)) != EOK) { 122518678f8Sopenharmony_ci return 0; 123518678f8Sopenharmony_ci } 124518678f8Sopenharmony_ci if (memcpy_s(currFilePath, sizeof(currFilePath), currName, strlen(currName)) != EOK) { 125518678f8Sopenharmony_ci return 0; 126518678f8Sopenharmony_ci } 127518678f8Sopenharmony_ci return currFilePath; 128518678f8Sopenharmony_ci} 129518678f8Sopenharmony_ci 130518678f8Sopenharmony_ciconst char *GetLeaseFile(const char *fileName, const char *ifname) 131518678f8Sopenharmony_ci{ 132518678f8Sopenharmony_ci static char leaseFileName[DHCP_MAX_PATH_LENGTH]; 133518678f8Sopenharmony_ci if (!fileName || !ifname) { 134518678f8Sopenharmony_ci return 0; 135518678f8Sopenharmony_ci } 136518678f8Sopenharmony_ci if (strlen(fileName) == 0 || strlen(ifname) == 0) { 137518678f8Sopenharmony_ci return 0; 138518678f8Sopenharmony_ci } 139518678f8Sopenharmony_ci if (snprintf_s(leaseFileName, sizeof(leaseFileName), sizeof(leaseFileName) - 1, "%s.%s", fileName, ifname) < 0) { 140518678f8Sopenharmony_ci DHCP_LOGE("Failed to get dhcp lease file path!"); 141518678f8Sopenharmony_ci return 0; 142518678f8Sopenharmony_ci } 143518678f8Sopenharmony_ci return leaseFileName; 144518678f8Sopenharmony_ci} 145518678f8Sopenharmony_ci 146518678f8Sopenharmony_ciint CreatePath(const char *fileName) 147518678f8Sopenharmony_ci{ 148518678f8Sopenharmony_ci if (!fileName) { 149518678f8Sopenharmony_ci return RET_FAILED; 150518678f8Sopenharmony_ci } 151518678f8Sopenharmony_ci int len = strlen(fileName); 152518678f8Sopenharmony_ci if (!len) { 153518678f8Sopenharmony_ci return RET_FAILED; 154518678f8Sopenharmony_ci } 155518678f8Sopenharmony_ci char filePath[DHCP_MAX_PATH_LENGTH] = {'\0'}; 156518678f8Sopenharmony_ci if (strncpy_s(filePath, sizeof(filePath), fileName, len) != EOK) { 157518678f8Sopenharmony_ci return RET_FAILED; 158518678f8Sopenharmony_ci } 159518678f8Sopenharmony_ci for (int i = 0; i < len; i++) { 160518678f8Sopenharmony_ci if (filePath[i] == '/') { 161518678f8Sopenharmony_ci filePath[i] = '\0'; 162518678f8Sopenharmony_ci if (access(filePath, 0) != 0) { 163518678f8Sopenharmony_ci mkdir(filePath, NEW_FILEPATH_MODE); 164518678f8Sopenharmony_ci } 165518678f8Sopenharmony_ci filePath[i] = '/'; 166518678f8Sopenharmony_ci } 167518678f8Sopenharmony_ci } 168518678f8Sopenharmony_ci if (len > 0 && access(filePath, 0) != 0) { 169518678f8Sopenharmony_ci mkdir(filePath, NEW_FILEPATH_MODE); 170518678f8Sopenharmony_ci } 171518678f8Sopenharmony_ci return RET_SUCCESS; 172518678f8Sopenharmony_ci} 173518678f8Sopenharmony_ci 174518678f8Sopenharmony_ciint RemoveSpaceCharacters(char *buf, size_t bufSize) 175518678f8Sopenharmony_ci{ 176518678f8Sopenharmony_ci if ((buf == nullptr) || (strlen(buf) == 0) || (bufSize == 0)) { 177518678f8Sopenharmony_ci DHCP_LOGE("RemoveSpaceCharacters() buf == nullptr or len == 0!"); 178518678f8Sopenharmony_ci return DHCP_FALSE; 179518678f8Sopenharmony_ci } 180518678f8Sopenharmony_ci 181518678f8Sopenharmony_ci /* Handle rightmost spaces. */ 182518678f8Sopenharmony_ci int nEnd = strlen(buf) - 1; 183518678f8Sopenharmony_ci while ((nEnd >= 0) && isspace(buf[nEnd])) { 184518678f8Sopenharmony_ci buf[nEnd--] = '\0'; 185518678f8Sopenharmony_ci } 186518678f8Sopenharmony_ci 187518678f8Sopenharmony_ci /* Handle leftmost spaces. */ 188518678f8Sopenharmony_ci int i = 0; 189518678f8Sopenharmony_ci while (isspace(buf[i])) { 190518678f8Sopenharmony_ci buf[i++] = '\0'; 191518678f8Sopenharmony_ci } 192518678f8Sopenharmony_ci 193518678f8Sopenharmony_ci int j = 0; 194518678f8Sopenharmony_ci while (buf[i] != '\0') { 195518678f8Sopenharmony_ci buf[j++] = buf[i++]; 196518678f8Sopenharmony_ci } 197518678f8Sopenharmony_ci buf[j] = '\0'; 198518678f8Sopenharmony_ci 199518678f8Sopenharmony_ci return DHCP_TRUE; 200518678f8Sopenharmony_ci} 201