• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "netstack_common_utils.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 
24 #include "curl/curl.h"
25 #include "netstack_log.h"
26 
27 namespace OHOS::NetStack::CommonUtils {
Split(const std::string & str,const std::string & sep)28 std::vector<std::string> Split(const std::string &str, const std::string &sep)
29 {
30     std::string s = str;
31     std::vector<std::string> res;
32     while (!s.empty()) {
33         auto pos = s.find(sep);
34         if (pos == std::string::npos) {
35             res.emplace_back(s);
36             break;
37         }
38         res.emplace_back(s.substr(0, pos));
39         s = s.substr(pos + sep.size());
40     }
41     return res;
42 }
43 
Split(const std::string & str,const std::string & sep,size_t size)44 std::vector<std::string> Split(const std::string &str, const std::string &sep, size_t size)
45 {
46     std::string s = str;
47     std::vector<std::string> res;
48     while (!s.empty()) {
49         if (res.size() + 1 == size) {
50             res.emplace_back(s);
51             break;
52         }
53 
54         auto pos = s.find(sep);
55         if (pos == std::string::npos) {
56             res.emplace_back(s);
57             break;
58         }
59         res.emplace_back(s.substr(0, pos));
60         s = s.substr(pos + sep.size());
61     }
62     return res;
63 }
64 
Strip(const std::string & str,char ch)65 std::string Strip(const std::string &str, char ch)
66 {
67     int64_t i = 0;
68     while (static_cast<size_t>(i) < str.size() && str[i] == ch) {
69         ++i;
70     }
71     int64_t j = static_cast<int64_t>(str.size()) - 1;
72     while (j > 0 && str[j] == ch) {
73         --j;
74     }
75     if (i >= 0 && static_cast<size_t>(i) < str.size() && j >= 0 && static_cast<size_t>(j) < str.size() &&
76         j - i + 1 > 0) {
77         return str.substr(i, j - i + 1);
78     }
79     return "";
80 }
81 
ToLower(const std::string & s)82 std::string ToLower(const std::string &s)
83 {
84     std::string res = s;
85     std::transform(res.begin(), res.end(), res.begin(), tolower);
86     return res;
87 }
88 
HasInternetPermission()89 bool HasInternetPermission()
90 {
91 #ifndef OH_CORE_NETSTACK_PERMISSION_CHECK
92     int testSock = socket(AF_INET, SOCK_STREAM, 0);
93     if (testSock < 0 && errno == EPERM) {
94         NETSTACK_LOGE("make tcp testSock failed errno is %{public}d %{public}s", errno, strerror(errno));
95         return false;
96     }
97     if (testSock > 0) {
98         close(testSock);
99     }
100     return true;
101 #else
102     constexpr int inetGroup = 40002003; // 3003 in gateway shell.
103     int groupNum = getgroups(0, nullptr);
104     if (groupNum <= 0) {
105         NETSTACK_LOGE("no group of INTERNET permission");
106         return false;
107     }
108     auto groups = (gid_t*) malloc(groupNum * sizeof(gid_t));
109     groupNum = getgroups(groupNum, groups);
110     for (int i = 0; i < groupNum; i++) {
111         if (groups[i] == inetGroup) {
112             free(groups);
113             return true;
114         }
115     }
116     free(groups);
117     NETSTACK_LOGE("INTERNET permission denied by group");
118     return false;
119 #endif
120 }
121 } // namespace OHOS::NetStack::CommonUtils