YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/net/inetaddress.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_NET_INETADDRESS_H
15
#define YB_UTIL_NET_INETADDRESS_H
16
17
#include <string.h>
18
19
#include <functional>
20
#include <string>
21
22
#include <boost/asio/ip/address.hpp>
23
#include <boost/asio/ip/address_v4.hpp>
24
#include <boost/asio/ip/address_v6.hpp>
25
#include <boost/system/error_code.hpp>
26
27
#include "yb/gutil/stringprintf.h"
28
29
#include "yb/util/status_fwd.h"
30
#include "yb/util/slice.h"
31
32
namespace yb {
33
34
constexpr size_t kInetAddressV4Size = 4;
35
constexpr size_t kInetAddressV6Size = 16;
36
37
// Generic class that encapsulates IPv4 and IPv6 addresses and uses the boost implementation
38
// underneath.
39
class InetAddress {
40
 public:
41
  InetAddress();
42
43
  explicit InetAddress(const boost::asio::ip::address& address);
44
45
  InetAddress(const InetAddress& other);
46
47
  // Fills in strval with the string representation of an IPv4 or IPv6 address.
48
  CHECKED_STATUS ToString(std::string* strval) const;
49
50
  // Returns string representation of an IPv4 or IPv6 address. This method doesn't return a
51
  // Status for usecases in the code where we don't support returning a status.
52
  std::string ToString() const;
53
54
  // Fills in the given string with the raw bytes for the appropriate address in network byte order.
55
  void AppendToBytes(std::string* bytes) const;
56
57
  std::string ToBytes() const;
58
59
  // Given a string holding the raw bytes in network byte order, it builds the appropriate
60
  // InetAddress object.
61
  CHECKED_STATUS FromBytes(const std::string& bytes);
62
63
  // Give a slice holding raw bytes in network byte order, build the appropriate InetAddress
64
  // object. If size_hint is specified, it indicates the number of bytes to decode from the slice.
65
  CHECKED_STATUS FromSlice(const Slice& slice, size_t size_hint = 0);
66
67
1
  const boost::asio::ip::address& address() const {
68
1
    return boost_addr_;
69
1
  }
70
71
  bool isV4() const;
72
73
  bool isV6() const;
74
75
108
  bool operator==(const InetAddress& other) const {
76
108
    return (boost_addr_ == other.boost_addr_);
77
108
  }
78
79
  bool operator!=(const InetAddress& other) const {
80
    return !(*this == other);
81
  }
82
83
  bool operator<(const InetAddress& other) const;
84
85
427
  bool operator>(const InetAddress& other) const {
86
427
    return (other < *this);
87
427
  }
88
89
  bool operator<=(const InetAddress& other) const {
90
    return !(other < *this);
91
  }
92
93
  bool operator>=(const InetAddress& other) const {
94
    return !(*this < other);
95
  }
96
97
400
  InetAddress& operator=(const InetAddress& other) {
98
400
    boost_addr_ = other.boost_addr_;
99
400
    return *this;
100
400
  }
101
102
 private:
103
  boost::asio::ip::address boost_addr_;
104
};
105
106
void FilterAddresses(const string &transform_spec,
107
                     vector<boost::asio::ip::address> *addresses);
108
109
} // namespace yb
110
111
#endif // YB_UTIL_NET_INETADDRESS_H