forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork_util.cpp
More file actions
249 lines (220 loc) · 8.02 KB
/
Copy pathnetwork_util.cpp
File metadata and controls
249 lines (220 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "common/network_util.h"
#include <arpa/inet.h>
#include <butil/endpoint.h>
#include <butil/strings/string_split.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sstream>
#include <vector>
#include "common/config.h"
#include "common/logging.h"
namespace doris::cloud {
class CIDR {
public:
CIDR() : address_(0), netmask_(0xffffffff) {}
bool reset(const std::string& cidr_str) {
address_ = 0;
netmask_ = 0xffffffff;
// check if have mask
std::string cidr_format_str = cidr_str;
int32_t have_mask = cidr_str.find("/");
if (have_mask == -1) {
cidr_format_str.assign(cidr_str + "/32");
}
VLOG_DEBUG << "cidr format str: " << cidr_format_str;
std::vector<std::string> cidr_items;
butil::SplitString(cidr_format_str, '/', &cidr_items);
if (cidr_items.size() != 2) {
LOG(WARNING) << "wrong CIDR format. network=" << cidr_str;
return false;
}
if (cidr_items[1].empty()) {
LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str;
return false;
}
char* endptr = nullptr;
int32_t mask_length = strtol(cidr_items[1].c_str(), &endptr, 10);
if (errno != 0 && mask_length == 0) {
char errmsg[64];
// Ignore unused return value
auto ret = strerror_r(errno, errmsg, 64);
LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str
<< ", mask_length=" << mask_length << ", errno=" << errno
<< ", errmsg=" << errmsg << ", strerror_r returns=" << ret;
return false;
}
if (mask_length <= 0 || mask_length > 32) {
LOG(WARNING) << "wrong CIDR mask format. network=" << cidr_str
<< ", mask_length=" << mask_length;
return false;
}
uint32_t address = 0;
if (!ip_to_int(cidr_items[0], &address)) {
LOG(WARNING) << "wrong CIDR IP value. network=" << cidr_str;
return false;
}
address_ = address;
netmask_ = 0xffffffff;
netmask_ = netmask_ << (32 - mask_length);
return true;
}
bool contains(const std::string& ip) {
uint32_t ip_int = 0;
if (!ip_to_int(ip, &ip_int)) {
return false;
}
if ((address_ & netmask_) == (ip_int & netmask_)) {
return true;
}
return false;
}
private:
bool ip_to_int(const std::string& ip_str, uint32_t* value) {
struct in_addr addr;
int flag = inet_aton(ip_str.c_str(), &addr);
if (flag == 0) {
return false;
}
*value = ntohl(addr.s_addr);
return true;
}
uint32_t address_;
uint32_t netmask_;
};
class InetAddress {
public:
InetAddress(struct sockaddr* addr) { this->addr_ = *(struct sockaddr_in*)addr; }
bool is_address_v4() const { return addr_.sin_family == AF_INET; }
bool is_loopback_v4() const {
in_addr_t s_addr = addr_.sin_addr.s_addr;
return (ntohl(s_addr) & 0xFF000000) == 0x7F000000;
}
std::string get_host_address_v4() {
char addr_buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr_.sin_addr), addr_buf, INET_ADDRSTRLEN);
return std::string(addr_buf);
}
private:
struct sockaddr_in addr_;
};
static bool get_hosts_v4(std::vector<InetAddress>* hosts) {
ifaddrs* if_addrs = nullptr;
if (getifaddrs(&if_addrs)) {
std::stringstream ss;
char buf[64];
LOG(FATAL) << "getifaddrs failed because " << strerror_r(errno, buf, sizeof(buf));
return false;
}
for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
if (!if_addr->ifa_addr) {
continue;
}
if (if_addr->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
hosts->emplace_back(if_addr->ifa_addr);
}
}
if (if_addrs != nullptr) {
freeifaddrs(if_addrs);
}
return true;
}
std::string get_local_ip(const std::string& priority_networks) {
std::string localhost_str = butil::my_ip_cstr();
std::unique_ptr<int, std::function<void(int*)>> defer((int*)0x01, [&localhost_str](int*) {
// Check if ip eq 127.0.0.1, ms/recycler exit
LOG(INFO) << "get the IP for ms is " << localhost_str;
if (config::enable_loopback_address_for_ms || localhost_str != "127.0.0.1") return;
LOG(WARNING) << "localhost IP is loopback address (127.0.0.1), "
<< "there may be multiple NICs for use, "
<< "please set priority_networks with a CIDR expression in doris_cloud.conf "
<< "to choose a non-loopback address accordingly";
exit(-1);
});
if (priority_networks == "") {
LOG(INFO) << "use butil::my_ip_cstr(), local host ip=" << localhost_str;
return localhost_str;
}
std::vector<CIDR> priority_cidrs;
LOG(INFO) << "priority CIDRs: " << priority_networks;
std::vector<std::string> cidr_strs;
butil::SplitString(priority_networks, ';', &cidr_strs);
for (auto& cidr_str : cidr_strs) {
CIDR cidr;
if (!cidr.reset(cidr_str)) {
LOG(FATAL) << "wrong cidr format. cidr_str=" << cidr_str;
return localhost_str;
}
priority_cidrs.push_back(cidr);
}
std::vector<InetAddress> hosts;
if (!get_hosts_v4(&hosts)) {
LOG(FATAL) << "failed to getifaddrs";
return localhost_str;
}
if (hosts.empty()) {
LOG(FATAL) << "failed to get host";
return localhost_str;
}
auto is_in_prior_network = [&priority_cidrs](const std::string& ip) {
for (auto& cidr : priority_cidrs) {
if (cidr.contains(ip)) {
return true;
}
}
return false;
};
std::string loopback;
localhost_str = "";
for (auto addr_it = hosts.begin(); addr_it != hosts.end(); ++addr_it) {
if ((*addr_it).is_address_v4()) {
VLOG_DEBUG << "check ip=" << addr_it->get_host_address_v4();
if ((*addr_it).is_loopback_v4()) {
loopback = addr_it->get_host_address_v4();
} else if (!priority_cidrs.empty()) {
if (is_in_prior_network(addr_it->get_host_address_v4())) {
localhost_str = addr_it->get_host_address_v4();
break;
}
} else {
localhost_str = addr_it->get_host_address_v4();
break;
}
}
}
if (!localhost_str.empty()) {
LOG(INFO) << "local host ip=" << localhost_str;
return localhost_str;
}
if (!loopback.empty()) {
localhost_str = loopback;
LOG(WARNING) << "fail to find one valid non-loopback address, use loopback address: "
<< localhost_str;
} else {
localhost_str = butil::my_ip_cstr();
LOG(WARNING)
<< "fail to find valid address of priority cidrs in conf, use butil::my_ip_cstr(): "
<< localhost_str;
}
return localhost_str;
}
} // namespace doris::cloud