Coverage Report

Created: 2026-08-03 17:57

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
76
        : _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.21M
bool is_valid_ip(const std::string& ip) {
79
1.21M
    unsigned char buf[sizeof(struct in6_addr)];
80
1.21M
    return (inet_pton(AF_INET6, ip.data(), buf) > 0) || (inet_pton(AF_INET, ip.data(), buf) > 0);
81
1.21M
}
82
83
1.37k
bool parse_endpoint(const std::string& endpoint, std::string* host, uint16_t* port) {
84
1.37k
    auto p = endpoint.find_last_of(':');
85
1.37k
    if (p == std::string::npos || p + 1 == endpoint.size()) {
86
0
        return false;
87
0
    }
88
89
1.37k
    const char* port_base = endpoint.c_str() + p + 1;
90
1.37k
    char* end = nullptr;
91
1.37k
    long value = strtol(port_base, &end, 10);
92
1.37k
    if (port_base == end) {
93
0
        return false;
94
1.37k
    } 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.38k
    } else if (value < 0 || 65535 < value) {
102
0
        return false;
103
0
    }
104
105
1.37k
    std::string::size_type i = 0;
106
1.37k
    const char* host_base = endpoint.c_str();
107
1.37k
    while (std::isspace(host_base[i])) {
108
0
        i++;
109
0
    }
110
1.37k
    if (i < p && host_base[i] == '[' && host_base[p - 1] == ']') {
111
0
        i += 1;
112
0
        p -= 1;
113
0
    }
114
1.37k
    if (i >= p) {
115
0
        return false;
116
0
    }
117
1.37k
    *host = endpoint.substr(i, p - i);
118
1.37k
    *port = cast_set<uint16_t>(value);
119
1.37k
    return true;
120
1.37k
}
121
122
0
Status hostname_to_ip(const std::string& host, std::string& ip, int* gai_err) {
123
0
    auto start = std::chrono::high_resolution_clock::now();
124
0
    Status status = hostname_to_ipv4(host, ip, gai_err);
125
0
    if (status.ok()) {
126
0
        return status;
127
0
    }
128
0
    status = hostname_to_ipv6(host, ip, gai_err);
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
248
Status hostname_to_ip(const std::string& host, std::string& ip, bool ipv6, int* gai_err) {
140
248
    if (ipv6) {
141
0
        return hostname_to_ipv6(host, ip, gai_err);
142
248
    } else {
143
248
        return hostname_to_ipv4(host, ip, gai_err);
144
248
    }
145
248
}
146
147
248
Status hostname_to_ipv4(const std::string& host, std::string& ip, int* gai_err) {
148
248
    addrinfo hints, *res;
149
248
    in_addr addr;
150
151
248
    memset(&hints, 0, sizeof(addrinfo));
152
248
    hints.ai_socktype = SOCK_STREAM;
153
248
    hints.ai_family = AF_INET;
154
248
    int err = getaddrinfo(host.c_str(), NULL, &hints, &res);
155
248
    if (gai_err != nullptr) {
156
248
        *gai_err = err;
157
248
    }
158
248
    if (err != 0) {
159
1
        LOG(WARNING) << "failed to get ip from host: " << host << "err:" << gai_strerror(err);
160
        // No stack trace: the WARNING above already carries the host and the resolver error,
161
        // and this failure is expected often enough (a decommissioned backend, a hostname
162
        // whose DNS record has not propagated yet) that attaching a stack to every
163
        // occurrence floods be.WARNING without adding information.
164
1
        return Status::InternalError<false>("failed to get ip from host: {}, err: {}", host,
165
1
                                            gai_strerror(err));
166
1
    }
167
168
247
    addr.s_addr = ((sockaddr_in*)(res->ai_addr))->sin_addr.s_addr;
169
247
    ip = inet_ntoa(addr);
170
171
247
    freeaddrinfo(res);
172
247
    return Status::OK();
173
248
}
174
175
0
Status hostname_to_ipv6(const std::string& host, std::string& ip, int* gai_err) {
176
0
    char ipstr2[128];
177
0
    struct sockaddr_in6* sockaddr_ipv6;
178
179
0
    struct addrinfo *answer, hint;
180
0
    bzero(&hint, sizeof(hint));
181
0
    hint.ai_family = AF_INET6;
182
0
    hint.ai_socktype = SOCK_STREAM;
183
184
0
    int err = getaddrinfo(host.c_str(), NULL, &hint, &answer);
185
0
    if (gai_err != nullptr) {
186
0
        *gai_err = err;
187
0
    }
188
0
    if (err != 0) {
189
0
        LOG(WARNING) << "failed to get ip from host: " << host << "err:" << gai_strerror(err);
190
        // See hostname_to_ipv4() for why this error carries no stack trace.
191
0
        return Status::InternalError<false>("failed to get ip from host: {}, err: {}", host,
192
0
                                            gai_strerror(err));
193
0
    }
194
195
0
    sockaddr_ipv6 = reinterpret_cast<struct sockaddr_in6*>(answer->ai_addr);
196
0
    inet_ntop(AF_INET6, &sockaddr_ipv6->sin6_addr, ipstr2, sizeof(ipstr2));
197
0
    ip = ipstr2;
198
0
    fflush(NULL);
199
0
    freeaddrinfo(answer);
200
0
    return Status::OK();
201
0
}
202
203
0
bool find_first_non_localhost(const std::vector<std::string>& addresses, std::string* addr) {
204
0
    for (const std::string& candidate : addresses) {
205
0
        if (candidate != LOCALHOST) {
206
0
            *addr = candidate;
207
0
            return true;
208
0
        }
209
0
    }
210
211
0
    return false;
212
0
}
213
214
7
Status get_hosts(std::vector<InetAddress>* hosts) {
215
7
    ifaddrs* if_addrs = nullptr;
216
7
    if (getifaddrs(&if_addrs)) {
217
0
        std::stringstream ss;
218
0
        char buf[64];
219
0
        ss << "getifaddrs failed because " << strerror_r(errno, buf, sizeof(buf));
220
0
        return Status::InternalError(ss.str());
221
0
    }
222
223
112
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
224
105
        if (!if_addr->ifa_addr) {
225
0
            continue;
226
0
        }
227
105
        auto addr = if_addr->ifa_addr;
228
105
        if (addr->sa_family == AF_INET) {
229
            // check legitimacy of IP4 Address
230
34
            char addr_buf[INET_ADDRSTRLEN];
231
34
            auto tmp_addr = &((struct sockaddr_in*)if_addr->ifa_addr)->sin_addr;
232
34
            inet_ntop(AF_INET, tmp_addr, addr_buf, INET_ADDRSTRLEN);
233
            // check is loopback Address
234
34
            in_addr_t s_addr = ((struct sockaddr_in*)addr)->sin_addr.s_addr;
235
34
            bool is_loopback = (ntohl(s_addr) & 0xFF000000) == 0x7F000000;
236
34
            hosts->emplace_back(std::string(addr_buf), AF_INET, is_loopback);
237
71
        } else if (addr->sa_family == AF_INET6) {
238
            // check legitimacy of IP6 Address
239
28
            auto tmp_addr = &((struct sockaddr_in6*)if_addr->ifa_addr)->sin6_addr;
240
28
            char addr_buf[INET6_ADDRSTRLEN];
241
28
            inet_ntop(AF_INET6, tmp_addr, addr_buf, sizeof(addr_buf));
242
            // check is loopback Address
243
28
            bool is_loopback = IN6_IS_ADDR_LOOPBACK(tmp_addr);
244
28
            hosts->emplace_back(std::string(addr_buf), AF_INET6, is_loopback);
245
43
        } else {
246
43
            continue;
247
43
        }
248
105
    }
249
250
7
    if (if_addrs != nullptr) {
251
7
        freeifaddrs(if_addrs);
252
7
    }
253
254
7
    return Status::OK();
255
7
}
256
257
34.8k
TNetworkAddress make_network_address(const std::string& hostname, int port) {
258
34.8k
    TNetworkAddress ret;
259
34.8k
    ret.__set_hostname(hostname);
260
34.8k
    ret.__set_port(port);
261
34.8k
    return ret;
262
34.8k
}
263
264
2
Status get_inet_interfaces(std::vector<std::string>* interfaces, bool include_ipv6) {
265
2
    ifaddrs* if_addrs = nullptr;
266
2
    if (getifaddrs(&if_addrs)) {
267
0
        std::stringstream ss;
268
0
        char buf[64];
269
0
        ss << "getifaddrs failed, errno:" << errno << ", message"
270
0
           << strerror_r(errno, buf, sizeof(buf));
271
0
        return Status::InternalError(ss.str());
272
0
    }
273
274
32
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
275
30
        if (if_addr->ifa_addr == nullptr || if_addr->ifa_name == nullptr) {
276
0
            continue;
277
0
        }
278
30
        if (if_addr->ifa_addr->sa_family == AF_INET ||
279
30
            (include_ipv6 && if_addr->ifa_addr->sa_family == AF_INET6)) {
280
8
            interfaces->emplace_back(if_addr->ifa_name);
281
8
        }
282
30
    }
283
2
    if (if_addrs != nullptr) {
284
2
        freeifaddrs(if_addrs);
285
2
    }
286
2
    return Status::OK();
287
2
}
288
289
1.48k
std::string get_host_port(const std::string& host, int port) {
290
1.48k
    std::stringstream ss;
291
1.48k
    if (host.find(':') == std::string::npos) {
292
1.48k
        ss << host << ":" << port;
293
18.4E
    } else {
294
18.4E
        ss << "[" << host << "]"
295
18.4E
           << ":" << port;
296
18.4E
    }
297
1.48k
    return ss.str();
298
1.48k
}
299
300
0
std::string get_brpc_http_url(const std::string& host, int port) {
301
0
    if (host.find(':') != std::string::npos) {
302
0
        return fmt::format("list://[{}]:{}", host, port);
303
0
    } else {
304
0
        return fmt::format("http://{}:{}", host, port);
305
0
    }
306
0
}
307
} // namespace doris