YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/net/net_util.h
Line
Count
Source (jump to first uncovered line)
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
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_UTIL_NET_NET_UTIL_H
33
#define YB_UTIL_NET_NET_UTIL_H
34
35
#include <string>
36
#include <vector>
37
#include <memory>
38
39
#include <boost/container/small_vector.hpp>
40
#include <boost/optional/optional_fwd.hpp>
41
42
#include <gflags/gflags_declare.h>
43
44
#include "yb/util/status_fwd.h"
45
#include "yb/util/net/net_fwd.h"
46
47
DECLARE_string(net_address_filter);
48
namespace yb {
49
50
class FileLock;
51
class Slice;
52
53
// A container for a host:port pair.
54
class HostPort {
55
 public:
56
  HostPort();
57
  HostPort(Slice host, uint16_t port);
58
  HostPort(std::string host, uint16_t port);
59
  HostPort(const char* host, uint16_t port);
60
  explicit HostPort(const Endpoint& endpoint);
61
62
  static HostPort FromBoundEndpoint(const Endpoint& endpoint);
63
64
  // Parse a "host:port" pair into this object.
65
  // If there is no port specified in the string, then 'default_port' is used.
66
  CHECKED_STATUS ParseString(const std::string& str, uint16_t default_port);
67
68
  static Result<HostPort> FromString(const std::string& str, uint16_t default_port);
69
70
  // Resolve any addresses corresponding to this host:port pair.
71
  // Note that a host may resolve to more than one IP address.
72
  //
73
  // 'addresses' may be NULL, in which case this function simply checks that
74
  // the host/port pair can be resolved, without returning anything.
75
  CHECKED_STATUS ResolveAddresses(std::vector<Endpoint>* addresses) const;
76
77
  std::string ToString() const;
78
79
36.2M
  const std::string& host() const { return host_; }
80
761k
  void set_host(const std::string& host) { host_ = host; }
81
82
15.9M
  uint16_t port() const { return port_; }
83
766k
  void set_port(uint16_t port) { port_ = port; }
84
85
  // Parse a comma separated list of "host:port" pairs into a vector
86
  // HostPort objects. If no port is specified for an entry in the
87
  // comma separated list, 'default_port' is used for that entry's
88
  // pair.
89
  static CHECKED_STATUS ParseStrings(
90
      const std::string& comma_sep_addrs,
91
      uint16_t default_port,
92
      std::vector<HostPort>* res,
93
      const char* separator = ",");
94
95
  static Result<std::vector<HostPort>> ParseStrings(
96
      const std::string& comma_sep_addrs, uint16_t default_port,
97
      const char* separator = ",");
98
99
  template <class PB>
100
20.0k
  static HostPort FromPB(const PB& pb) {
101
20.0k
    return HostPort(pb.host(), pb.port());
102
20.0k
  }
103
104
  // Takes a vector of HostPort objects and returns a comma separated
105
  // string containing of "host:port" pairs. This method is the
106
  // "inverse" of ParseStrings().
107
  static std::string ToCommaSeparatedString(const std::vector<HostPort>& host_ports);
108
109
  // Checks if the host/port are same as the sockaddr
110
  bool equals(const Endpoint& endpoint) const;
111
112
  // Remove a given host/port from a vector of comma separated server multiple addresses, each in
113
  // [host:port,]+ format and returns a final list as a remaining vector of hostports.
114
  static CHECKED_STATUS RemoveAndGetHostPortList(
115
      const Endpoint& remove,
116
      const std::vector<std::string>& multiple_server_addresses,
117
      uint16_t default_port,
118
      std::vector<HostPort> *res);
119
120
5.15M
  bool operator==(const HostPort& other) const {
121
5.15M
    return host() == other.host() && port() == other.port();
122
5.15M
  }
123
124
0
  friend bool operator<(const HostPort& lhs, const HostPort& rhs) {
125
0
    auto cmp_hosts = lhs.host_.compare(rhs.host_);
126
0
    return cmp_hosts < 0 || (cmp_hosts == 0 && lhs.port_ < rhs.port_);
127
0
  }
128
129
 private:
130
  std::string host_;
131
  uint16_t port_;
132
};
133
134
7.90k
inline std::ostream& operator<<(std::ostream& out, const HostPort& value) {
135
7.90k
  return out << value.ToString();
136
7.90k
}
137
138
struct HostPortHash {
139
  size_t operator()(const HostPort& hostPort) const;
140
};
141
142
// Parse and resolve the given comma-separated list of addresses.
143
//
144
// The resulting addresses will be resolved, made unique, and added to
145
// the 'addresses' vector.
146
//
147
// Any elements which do not include a port will be assigned 'default_port'.
148
CHECKED_STATUS ParseAddressList(const std::string& addr_list,
149
                                uint16_t default_port,
150
                                std::vector<Endpoint>* addresses);
151
152
// Return true if the given port is likely to need root privileges to bind to.
153
bool IsPrivilegedPort(uint16_t port);
154
155
// Return the local machine's hostname.
156
CHECKED_STATUS GetHostname(std::string* hostname);
157
158
// Return the local machine's hostname as a Result.
159
Result<std::string> GetHostname();
160
161
// Return the local machine's FQDN.
162
CHECKED_STATUS GetFQDN(std::string* fqdn);
163
164
// Returns a single socket address from a HostPort.
165
// If the hostname resolves to multiple addresses, returns the first in the
166
// list and logs a message in verbose mode.
167
CHECKED_STATUS EndpointFromHostPort(const HostPort& host_port, Endpoint* endpoint);
168
169
// Converts the given Endpoint into a HostPort, substituting the FQDN
170
// in the case that the provided address is the wildcard.
171
//
172
// In the case of other addresses, the returned HostPort will contain just the
173
// stringified form of the IP.
174
CHECKED_STATUS HostPortFromEndpointReplaceWildcard(const Endpoint& addr, HostPort* hp);
175
176
// Try to run 'lsof' to determine which process is preventing binding to
177
// the given 'addr'. If pids can be determined, outputs full 'ps' and 'pstree'
178
// output for that process.
179
//
180
// Output is issued to the log at WARNING level, or appended to 'log' if it
181
// is non-NULL (mostly useful for testing).
182
void TryRunLsof(const Endpoint& addr, std::vector<std::string>* log = NULL);
183
184
// Get a free port that a local server could listen to. For use in tests. Tries up to a 1000 times
185
// and fatals after that.
186
// @param file_lock This will be set to a file lock ensuring the port does not get taken by
187
//                  another call to this function, possibly in another process.
188
uint16_t GetFreePort(std::unique_ptr<FileLock>* file_lock);
189
190
enum class AddressFilter {
191
  ANY, // all interfaces would be listed.
192
  EXTERNAL, // don't list local loopbacks
193
};
194
Status GetLocalAddresses(std::vector<IpAddress>* result, AddressFilter filter);
195
196
// Get local addresses, filtered and ordered by the filter_spec specified
197
// For details of the filter_spec, see inetaddress.h
198
Status GetLocalAddresses(const std::string &filter_spec,
199
                         std::vector<IpAddress> *result);
200
201
// Convert the given host/port pair to a string of the host:port format.
202
std::string HostPortToString(const std::string& host, int port);
203
204
template <class PB>
205
36
static std::string HostPortPBToString(const PB& pb) {
206
36
  return HostPortToString(pb.host(), pb.port());
207
36
}
Unexecuted instantiation: yb-admin-test.cc:_ZN2ybL18HostPortPBToStringINS_10HostPortPBEEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERKT_
master-path-handlers.cc:_ZN2ybL18HostPortPBToStringINS_10HostPortPBEEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERKT_
Line
Count
Source
205
36
static std::string HostPortPBToString(const PB& pb) {
206
36
  return HostPortToString(pb.host(), pb.port());
207
36
}
Unexecuted instantiation: yb-admin_client.cc:_ZN2ybL18HostPortPBToStringINS_10HostPortPBEEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEERKT_
208
209
CHECKED_STATUS HostToAddresses(
210
    const std::string& host,
211
    boost::container::small_vector_base<IpAddress>* addresses);
212
213
Result<IpAddress> HostToAddress(const std::string& host);
214
boost::optional<IpAddress> TryFastResolve(const std::string& host);
215
Result<IpAddress> ParseIpAddress(const std::string& host);
216
217
// Returns true if host_str is 0.0.0.0 or [::]
218
bool IsWildcardAddress(const std::string& host_str);
219
220
} // namespace yb
221
222
#endif  // YB_UTIL_NET_NET_UTIL_H