YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
189M
  const std::string& host() const { return host_; }
80
2.49M
  void set_host(const std::string& host) { host_ = host; }
81
82
113M
  uint16_t port() const { return port_; }
83
2.50M
  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
32.8k
  static HostPort FromPB(const PB& pb) {
101
32.8k
    return HostPort(pb.host(), pb.port());
102
32.8k
  }
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
36.8M
  bool operator==(const HostPort& other) const {
121
36.8M
    return host() == other.host() && 
port() == other.port()36.5M
;
122
36.8M
  }
123
124
3
  friend bool operator<(const HostPort& lhs, const HostPort& rhs) {
125
3
    auto cmp_hosts = lhs.host_.compare(rhs.host_);
126
3
    return cmp_hosts < 0 || (cmp_hosts == 0 && 
lhs.port_ < rhs.port_0
);
127
3
  }
128
129
 private:
130
  std::string host_;
131
  uint16_t port_;
132
};
133
134
412k
inline std::ostream& operator<<(std::ostream& out, const HostPort& value) {
135
412k
  return out << value.ToString();
136
412k
}
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
103
static std::string HostPortPBToString(const PB& pb) {
206
103
  return HostPortToString(pb.host(), pb.port());
207
103
}
master-path-handlers.cc:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::HostPortPBToString<yb::HostPortPB>(yb::HostPortPB const&)
Line
Count
Source
205
54
static std::string HostPortPBToString(const PB& pb) {
206
54
  return HostPortToString(pb.host(), pb.port());
207
54
}
yb-admin_client.cc:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > yb::HostPortPBToString<yb::HostPortPB>(yb::HostPortPB const&)
Line
Count
Source
205
49
static std::string HostPortPBToString(const PB& pb) {
206
49
  return HostPortToString(pb.host(), pb.port());
207
49
}
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
void TEST_SetFailToFastResolveAddress(const std::string& address);
221
222
} // namespace yb
223
224
#endif  // YB_UTIL_NET_NET_UTIL_H