• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 <arpa/inet.h>
17 
18 #include "netmanager_base_common_utils.h"
19 
20 namespace OHOS::NetManagerStandard::CommonUtils {
21 constexpr int32_t INET_PTION_SUC = 1;
22 constexpr uint32_t CONST_MASK = 0x80000000;
Split(const std::string & str,const std::string & sep)23 std::vector<std::string> Split(const std::string &str, const std::string &sep)
24 {
25     std::string s = str;
26     std::vector<std::string> res;
27     while (!s.empty()) {
28         size_t pos = s.find(sep);
29         if (pos == std::string::npos) {
30             res.emplace_back(s);
31             break;
32         }
33         res.emplace_back(s.substr(0, pos));
34         s = s.substr(pos + sep.size());
35     }
36     return res;
37 }
38 
Strip(const std::string & str,char ch)39 std::string Strip(const std::string &str, char ch)
40 {
41     int64_t i = 0;
42     while (i < str.size() && str[i] == ch) {
43         ++i;
44     }
45     int64_t j = static_cast<int64_t>(str.size()) - 1;
46     while (j > 0 && str[j] == ch) {
47         --j;
48     }
49     if (i >= 0 && i < str.size() && j >= 0 && j < str.size() && j - i + 1 > 0) {
50         return str.substr(i, j - i + 1);
51     }
52     return "";
53 }
54 
ToLower(const std::string & s)55 std::string ToLower(const std::string &s)
56 {
57     std::string res = s;
58     std::transform(res.begin(), res.end(), res.begin(), tolower);
59     return res;
60 }
61 
IsValidIPV4(const std::string & ip)62 bool IsValidIPV4(const std::string &ip)
63 {
64     if (ip.empty()) {
65         return false;
66     }
67     struct in_addr s;
68     int32_t result = inet_pton(AF_INET, ip.c_str(), reinterpret_cast<void *>(&s));
69     if (result == INET_PTION_SUC) {
70         return true;
71     }
72     return false;
73 }
74 
IsValidIPV6(const std::string & ip)75 bool IsValidIPV6(const std::string &ip)
76 {
77     if (ip.empty()) {
78         return false;
79     }
80     struct in6_addr s;
81     int32_t result = inet_pton(AF_INET6, ip.c_str(), reinterpret_cast<void *>(&s));
82     if (result == INET_PTION_SUC) {
83         return true;
84     }
85     return false;
86 }
87 
GetAddrFamily(const std::string & ip)88 int8_t GetAddrFamily(const std::string &ip)
89 {
90     if (IsValidIPV4(ip)) {
91         return AF_INET;
92     }
93     if (IsValidIPV6(ip)) {
94         return AF_INET6;
95     }
96     return 0;
97 }
98 
GetMaskLength(const std::string & mask)99 int GetMaskLength(const std::string &mask)
100 {
101     int netMask = 0;
102     unsigned int maskTmp = ntohl(static_cast<int>(inet_addr(mask.c_str())));
103     while (maskTmp & CONST_MASK) {
104         ++netMask;
105         maskTmp = (maskTmp << 1);
106     }
107     return netMask;
108 }
109 } // namespace OHOS::NetManagerStandard::CommonUtils