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(std::string &data, int base = DECIMAL_NOTATION); 37 unsigned int CheckDataToUint(std::string &data, int base = DECIMAL_NOTATION); 38 long long CheckDataTolonglong(std::string &data, int base = DECIMAL_NOTATION); 39 int64_t GetElapsedSecondsSinceBoot(); 40 41 /** 42 * @Description IP address transfer to string 43 * 44 * @param str - Input Ip address 45 * @param bHost - whether is bHost or not 46 * @return std::string - Ip address 47 */ 48 std::string Ip4IntConvertToStr(uint32_t uIp, bool bHost); 49 50 /** 51 * @Description add ip and mac into arp table 52 * 53 * @param iface - Input iface name 54 * @param ipAddr - ip Addr 55 * @param macAddr - mac address 56 * @return int32_t - 0: sucess; 1: fail 57 */ 58 int32_t AddArpEntry(const std::string& iface, const std::string& ipAddr, const std::string& macAddr); 59 } 60 } 61 #endif 62