Coverage Report

Created: 2026-04-14 17:06

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
14
        : _ip_addr(ip), _family(family), _is_loopback(is_loopback) {}
51
52
8
bool InetAddress::is_loopback() const {
53
8
    return _is_loopback;
54
8
}
55
56
7
std::string InetAddress::get_host_address() const {
57
7
    return _ip_addr;
58
7
}
59
60
11
bool InetAddress::is_ipv6() const {
61
11
    return _family == AF_INET6;
62
11
}
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
0
bool is_valid_ip(const std::string& ip) {
79
0
    unsigned char buf[sizeof(struct in6_addr)];
80
0
    return (inet_pton(AF_INET6, ip.data(), buf) > 0) || (inet_pton(AF_INET, ip.data(), buf) > 0);
81
0
}
82
83
0
bool parse_endpoint(const std::string& endpoint, std::string* host, uint16_t* port) {
84
0
    auto p = endpoint.find_last_of(':');
85
0
    if (p == std::string::npos || p + 1 == endpoint.size()) {
86
0
        return false;
87
0
    }
88
89
0
    const char* port_base = endpoint.c_str() + p + 1;
90
0
    char* end = nullptr;
91
0
    long value = strtol(port_base, &end, 10);
92
0
    if (port_base == end) {
93
0
        return false;
94
0
    } 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
0
    } else if (value < 0 || 65535 < value) {
102
0
        return false;
103
0
    }
104
105
0
    std::string::size_type i = 0;
106
0
    const char* host_base = endpoint.c_str();
107
0
    while (std::isspace(host_base[i])) {
108
0
        i++;
109
0
    }
110
0
    if (i < p && host_base[i] == '[' && host_base[p - 1] == ']') {
111
0
        i += 1;
112
0
        p -= 1;
113
0
    }
114
0
    if (i >= p) {
115
0
        return false;
116
0
    }
117
0
    *host = endpoint.substr(i, p - i);
118
0
    *port = cast_set<uint16_t>(value);
119
0
    return true;
120
0
}
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
0
Status hostname_to_ip(const std::string& host, std::string& ip, bool ipv6) {
140
0
    if (ipv6) {
141
0
        return hostname_to_ipv6(host, ip);
142
0
    } else {
143
0
        return hostname_to_ipv4(host, ip);
144
0
    }
145
0
}
146
147
0
Status hostname_to_ipv4(const std::string& host, std::string& ip) {
148
0
    addrinfo hints, *res;
149
0
    in_addr addr;
150
151
0
    memset(&hints, 0, sizeof(addrinfo));
152
0
    hints.ai_socktype = SOCK_STREAM;
153
0
    hints.ai_family = AF_INET;
154
0
    int err = getaddrinfo(host.c_str(), NULL, &hints, &res);
155
0
    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
0
    addr.s_addr = ((sockaddr_in*)(res->ai_addr))->sin_addr.s_addr;
162
0
    ip = inet_ntoa(addr);
163
164
0
    freeaddrinfo(res);
165
0
    return Status::OK();
166
0
}
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
1
Status get_hosts(std::vector<InetAddress>* hosts) {
204
1
    ifaddrs* if_addrs = nullptr;
205
1
    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
10
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
213
9
        if (!if_addr->ifa_addr) {
214
0
            continue;
215
0
        }
216
9
        auto addr = if_addr->ifa_addr;
217
9
        if (addr->sa_family == AF_INET) {
218
            // check legitimacy of IP4 Address
219
3
            char addr_buf[INET_ADDRSTRLEN];
220
3
            auto tmp_addr = &((struct sockaddr_in*)if_addr->ifa_addr)->sin_addr;
221
3
            inet_ntop(AF_INET, tmp_addr, addr_buf, INET_ADDRSTRLEN);
222
            // check is loopback Address
223
3
            in_addr_t s_addr = ((struct sockaddr_in*)addr)->sin_addr.s_addr;
224
3
            bool is_loopback = (ntohl(s_addr) & 0xFF000000) == 0x7F000000;
225
3
            hosts->emplace_back(std::string(addr_buf), AF_INET, is_loopback);
226
6
        } else if (addr->sa_family == AF_INET6) {
227
            // check legitimacy of IP6 Address
228
3
            auto tmp_addr = &((struct sockaddr_in6*)if_addr->ifa_addr)->sin6_addr;
229
3
            char addr_buf[INET6_ADDRSTRLEN];
230
3
            inet_ntop(AF_INET6, tmp_addr, addr_buf, sizeof(addr_buf));
231
            // check is loopback Address
232
3
            bool is_loopback = IN6_IS_ADDR_LOOPBACK(tmp_addr);
233
3
            hosts->emplace_back(std::string(addr_buf), AF_INET6, is_loopback);
234
3
        } else {
235
3
            continue;
236
3
        }
237
9
    }
238
239
1
    if (if_addrs != nullptr) {
240
1
        freeifaddrs(if_addrs);
241
1
    }
242
243
1
    return Status::OK();
244
1
}
245
246
0
TNetworkAddress make_network_address(const std::string& hostname, int port) {
247
0
    TNetworkAddress ret;
248
0
    ret.__set_hostname(hostname);
249
0
    ret.__set_port(port);
250
0
    return ret;
251
0
}
252
253
0
Status get_inet_interfaces(std::vector<std::string>* interfaces, bool include_ipv6) {
254
0
    ifaddrs* if_addrs = nullptr;
255
0
    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
0
    for (ifaddrs* if_addr = if_addrs; if_addr != nullptr; if_addr = if_addr->ifa_next) {
264
0
        if (if_addr->ifa_addr == nullptr || if_addr->ifa_name == nullptr) {
265
0
            continue;
266
0
        }
267
0
        if (if_addr->ifa_addr->sa_family == AF_INET ||
268
0
            (include_ipv6 && if_addr->ifa_addr->sa_family == AF_INET6)) {
269
0
            interfaces->emplace_back(if_addr->ifa_name);
270
0
        }
271
0
    }
272
0
    if (if_addrs != nullptr) {
273
0
        freeifaddrs(if_addrs);
274
0
    }
275
0
    return Status::OK();
276
0
}
277
278
6
std::string get_host_port(const std::string& host, int port) {
279
6
    std::stringstream ss;
280
6
    if (host.find(':') == std::string::npos) {
281
6
        ss << host << ":" << port;
282
6
    } else {
283
0
        ss << "[" << host << "]"
284
0
           << ":" << port;
285
0
    }
286
6
    return ss.str();
287
6
}
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