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 #ifndef OHOS_DHCP_COMMON_UTILS_H 17 #define OHOS_DHCP_COMMON_UTILS_H 18 19 #include <string> 20 21 namespace OHOS { 22 namespace DHCP { 23 /** 24 * @Description IP address anonymization 25 * 26 * <p> eg: 11.11.11.1 -> 11.11.11.* 27 * 28 * @param str - Input MAC address 29 * @return std::string - Processed MAC 30 */ 31 constexpr int DECIMAL_NOTATION = 10; 32 std::string Ipv4Anonymize(const std::string str); 33 char *UintIp4ToStr(uint32_t uIp, bool bHost); 34 std::string IntIpv4ToAnonymizeStr(uint32_t ip); 35 std::string MacArray2Str(uint8_t *macArray, int32_t len); 36 int CheckDataLegal(const std::string &data, int base = DECIMAL_NOTATION); 37 unsigned int CheckDataToUint(const std::string &data, int base = DECIMAL_NOTATION); 38 long long CheckDataTolonglong(const std::string &data, int base = DECIMAL_NOTATION); 39 uint64_t CheckDataToUint64(const std::string &data, int base = DECIMAL_NOTATION); 40 int64_t GetElapsedSecondsSinceBoot(); 41 42 /** 43 * @Description IP address transfer to string 44 * 45 * @param str - Input Ip address 46 * @param bHost - whether is bHost or not 47 * @return std::string - Ip address 48 */ 49 std::string Ip4IntConvertToStr(uint32_t uIp, bool bHost); 50 51 /** 52 * @Description add ip and mac into arp table 53 * 54 * @param iface - Input iface name 55 * @param ipAddr - ip Addr 56 * @param macAddr - mac address 57 * @return int32_t - 0: sucess; 1: fail 58 */ 59 int32_t AddArpEntry(const std::string& iface, const std::string& ipAddr, const std::string& macAddr); 60 61 std::string Ipv6Anonymize(const std::string &str); 62 63 bool IsValidPath(const std::string &filePath); 64 } 65 } 66 #endif 67