Coverage Report

Created: 2026-04-16 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/network_util.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/network-util.cc
19
// and modified by Doris
20
21
#include "util/network_util.h"
22
23
#include <arpa/inet.h>
24
// IWYU pragma: no_include <bits/local_lim.h>
25
// IWYU pragma: no_include <bthread/errno.h>
26
#include <errno.h> // IWYU pragma: keep
27
#include <fmt/format.h>
28
#include <gen_cpp/Types_types.h>
29
#include <ifaddrs.h>
30
#include <netdb.h>
31
#include <netinet/in.h>
32
#include <string.h>
33
#include <sys/socket.h>
34
#include <unistd.h>
35
36
#include <chrono>
37
#include <cstdint>
38
#include <sstream>
39
40
#include "common/cast_set.h"
41
42
#ifdef __APPLE__
43
#ifndef HOST_NAME_MAX
44
#define HOST_NAME_MAX MAXHOSTNAMELEN
45
#endif
46
#endif
47
48
namespace doris {
49
InetAddress::InetAddress(std::string ip, sa_family_t family, bool is_loopback)
50
72
        : _ip_addr(ip), _family(family), _is_loopback(is_loopback) {}
51
52
10
bool InetAddress::is_loopback() const {
53
10
    return _is_loopback;
54
10
}
55
56
33
std::string InetAddress::get_host_address() const {
57
33
    return _ip_addr;
58
33
}
59
60
20
bool InetAddress::is_ipv6() const {
61
20
    return _family == AF_INET6;
62
20
}
63
64
static const std::string LOCALHOST("127.0.0.1");
65
66
0
Status get_hostname(std::string* hostname) {
67
0
    char name[HOST_NAME_MAX];
68
0
    int ret = gethostname(name, HOST_NAME_MAX);
69
70
0
    if (ret != 0) {
71
0
        return Status::InternalError("Could not get hostname: errno: {}", errno);
72
0
    }
73
74
0
    *hostname = std::string(name);
75
0
    return Status::OK();
76
0
}
77
78
1.51M
bool is_valid_ip(const std::string& ip) {
79
1.51M
    unsigned char buf[sizeof(struct in6_addr)];
80
1.51M
    return (inet_pton(AF_INET6, ip.data(), buf) > 0) || (inet_pton(AF_INET, ip.data(), buf) > 0);
81
1.51M
}
82
83
1.50k
bool parse_endpoint(const std::string& endpoint, std::string* host, uint16_t* port) {
84
1.50k
    auto p = endpoint.find_last_of(':');
85
1.50k
    if (p == std::string::npos || p + 1 == endpoint.size()) {
86
0
        return false;
87
0
    }
88
89
1.50k
    const char* port_base = endpoint.c_str() + p + 1;
90
1.50k
    char* end = nullptr;
91
1.50k
    long value = strtol(port_base, &end, 10);
92
1.50k
    if (port_base == end) {
93
0
        return false;
94
1.50k
    } else if (*end) {
95
0
        while (std::isspace(*end)) {
96
0
            end++;
97
0
        }
98
0
        if (*end) {
99
0
            return false;
100
0
        }
101
1.50k
    } else if (value < 0 || 65535 < value) {
102
0
        return false;
103
0
    }
104
105
1.50k
    std::string::size_type i = 0;
106
1.50k
    const char* host_base = endpoint.c_str();
107
1.50k
    while (std::isspace(host_base[i])) {
108
0
        i++;
109
0
    }
110
1.50k
    if (i < p && host_base[i] == '[' && host_base[p - 1] == ']') {
111
0
        i += 1;
112
0
        p -= 1;
113
0
    }
114
1.50k
    if (i >= p) {
115
0
        return false;
116
0
    }
117
1.50k
    *host = endpoint.substr(i, p - i);
118
1.50k
    *port = cast_set<uint16_t>(value);
119
1.50k
    return true;
120
1.50k
}
121
122
0
Status hostname_to_ip(const std::string& host, std::string& ip) {
123
0
    auto start = std::chrono::high_resolution_clock::now();
124
0
    Status status = hostname_to_ipv4(host, ip);
125
0
    if (status.ok()) {
126
0
        return status;
127
0
    }
128
0
    status = hostname_to_ipv6(host, ip);
129
130
0
    auto current = std::chrono::high_resolution_clock::now();
131
0
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(current - start);
132
0
    if (duration.count() >= 500) {
133
0
        LOG(WARNING) << "hostname_to_ip cost to mush time, cost_time:" << duration.count()
134
0
                     << "ms hostname:" << host << " ip:" << ip;
135
0
    }
136
0
    return status;
137
0
}
138
139
226
Status hostname_to_ip(const std::string& host, std::string& ip, bool ipv6) {
140
226
    if (ipv6) {
141
0
        return hostname_to_ipv6(host, ip);
142
226
    } else {
143
226
        return hostname_to_ipv4(host, ip);
144
226
    }
145
226
}
146
147
226
Status hostname_to_ipv4(const std::string& host, std::string& ip) {
148
226
    addrinfo hints, *res;
149
226
    in_addr addr;
150
151
226
    memset(&hints, 0, sizeof(addrinfo));
152
226
    hints.ai_socktype = SOCK_STREAM;
153
226
    hints.ai_family = AF_INET;
154
226
    int err = getaddrinfo(host.c_str(), NULL, &hints, &res);
155
226
    if (err != 0) {
156
0
        LOG(WARNING) << "failed to get ip from host: " << host << "err:" << gai_strerror(err);
157
0
        return Status::InternalError("failed to get ip from host: {}, err: {}", host,
158
0
                                     gai_strerror(err));
159
0
    }
160
161
226
    addr.s_addr = ((sockaddr_in*)(res->ai_addr))->sin_addr.s_addr;
162
226
    ip = inet_ntoa(addr);
163
164
226
    freeaddrinfo(res);
165
226
    return Status::OK();
166
226
}
167
168
0
Status hostname_to_ipv6(const std::string& host, std::string& ip) {
169
0
    char ipstr2[128];
170
0
    struct sockaddr_in6* sockaddr_ipv6;
171
172
0
    struct addrinfo *answer, hint;
173
0
    bzero(&hint, sizeof(hint));
174
0
    hint.ai_family = AF_INET6;
175
0
    hint.ai_socktype = SOCK_STREAM;
176
177
0
    int err = getaddrinfo(host.c_str(), NULL, &hint, &answer);
178
0
    if (err != 0) {
179
0
        LOG(WARNING) << "failed to get ip from host: " << host << "err:" << gai_strerror(err);
180
0
        return Status::InternalError("failed to get ip from host: {}, err: {}", host,
181
0
                                     gai_strerror(err));
182
0
    }
183
184
0
    sockaddr_ipv6 = reinterpret_cast<struct sockaddr_in6*>(answer->ai_addr);
185
0
    inet_ntop(AF_INET6, &sockaddr_ipv6->sin6_addr, ipstr2, sizeof(ipstr2));
186
0
    ip = ipstr2;
187
0
    fflush(NULL);
188
0
    freeaddrinfo(answer);
189
0
    return Status::OK();
190
0
}
191
192
0
bool find_first_non_localhost(const std::vector<std::string>& addresses, std::string* addr) {
193
0
    for (const std::string& candidate : addresses) {
194
0
        if (candidate != LOCALHOST) {
195
0
            *addr = candidate;
196
0
            return true;
197
0
        }
198
0
    }
199
200
0
    return false;
201
0
}
202
203
7
Status get_hosts(std::vector<InetAddress>* hosts) {
204
7
    ifaddrs* if_addrs = nullptr;
205
7
    if (getifaddrs(&if_addrs)) {
206
0
        std::stringstream ss;
207
0
        char buf[64];
208
0
        ss << "getifaddrs failed because " << strerror_r(errno, buf, sizeof(buf));
209
0
        return Status::InternalError(ss.str());
210
0
    }
211
212
107
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
213
100
        if (!if_addr->ifa_addr) {
214
0
            continue;
215
0
        }
216
100
        auto addr = if_addr->ifa_addr;
217
100
        if (addr->sa_family == AF_INET) {
218
            // check legitimacy of IP4 Address
219
33
            char addr_buf[INET_ADDRSTRLEN];
220
33
            auto tmp_addr = &((struct sockaddr_in*)if_addr->ifa_addr)->sin_addr;
221
33
            inet_ntop(AF_INET, tmp_addr, addr_buf, INET_ADDRSTRLEN);
222
            // check is loopback Address
223
33
            in_addr_t s_addr = ((struct sockaddr_in*)addr)->sin_addr.s_addr;
224
33
            bool is_loopback = (ntohl(s_addr) & 0xFF000000) == 0x7F000000;
225
33
            hosts->emplace_back(std::string(addr_buf), AF_INET, is_loopback);
226
67
        } else if (addr->sa_family == AF_INET6) {
227
            // check legitimacy of IP6 Address
228
26
            auto tmp_addr = &((struct sockaddr_in6*)if_addr->ifa_addr)->sin6_addr;
229
26
            char addr_buf[INET6_ADDRSTRLEN];
230
26
            inet_ntop(AF_INET6, tmp_addr, addr_buf, sizeof(addr_buf));
231
            // check is loopback Address
232
26
            bool is_loopback = IN6_IS_ADDR_LOOPBACK(tmp_addr);
233
26
            hosts->emplace_back(std::string(addr_buf), AF_INET6, is_loopback);
234
41
        } else {
235
41
            continue;
236
41
        }
237
100
    }
238
239
7
    if (if_addrs != nullptr) {
240
7
        freeifaddrs(if_addrs);
241
7
    }
242
243
7
    return Status::OK();
244
7
}
245
246
31.1k
TNetworkAddress make_network_address(const std::string& hostname, int port) {
247
31.1k
    TNetworkAddress ret;
248
31.1k
    ret.__set_hostname(hostname);
249
31.1k
    ret.__set_port(port);
250
31.1k
    return ret;
251
31.1k
}
252
253
4
Status get_inet_interfaces(std::vector<std::string>* interfaces, bool include_ipv6) {
254
4
    ifaddrs* if_addrs = nullptr;
255
4
    if (getifaddrs(&if_addrs)) {
256
0
        std::stringstream ss;
257
0
        char buf[64];
258
0
        ss << "getifaddrs failed, errno:" << errno << ", message"
259
0
           << strerror_r(errno, buf, sizeof(buf));
260
0
        return Status::InternalError(ss.str());
261
0
    }
262
263
66
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
264
62
        if (if_addr->ifa_addr == nullptr || if_addr->ifa_name == nullptr) {
265
0
            continue;
266
0
        }
267
62
        if (if_addr->ifa_addr->sa_family == AF_INET ||
268
62
            (include_ipv6 && if_addr->ifa_addr->sa_family == AF_INET6)) {
269
22
            interfaces->emplace_back(if_addr->ifa_name);
270
22
        }
271
62
    }
272
4
    if (if_addrs != nullptr) {
273
4
        freeifaddrs(if_addrs);
274
4
    }
275
4
    return Status::OK();
276
4
}
277
278
1.54k
std::string get_host_port(const std::string& host, int port) {
279
1.54k
    std::stringstream ss;
280
1.55k
    if (host.find(':') == std::string::npos) {
281
1.55k
        ss << host << ":" << port;
282
18.4E
    } else {
283
18.4E
        ss << "[" << host << "]"
284
18.4E
           << ":" << port;
285
18.4E
    }
286
1.54k
    return ss.str();
287
1.54k
}
288
289
0
std::string get_brpc_http_url(const std::string& host, int port) {
290
0
    if (host.find(':') != std::string::npos) {
291
0
        return fmt::format("list://[{}]:{}", host, port);
292
0
    } else {
293
0
        return fmt::format("http://{}:{}", host, port);
294
0
    }
295
0
}
296
} // namespace doris