YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
  CHECKED_STATUS ToBytes(std::string* bytes) const;
56
57
  // Given a string holding the raw bytes in network byte order, it builds the appropriate
58
  // InetAddress object.
59
  CHECKED_STATUS FromBytes(const std::string& bytes);
60
61
  // Give a slice holding raw bytes in network byte order, build the appropriate InetAddress
62
  // object. If size_hint is specified, it indicates the number of bytes to decode from the slice.
63
  CHECKED_STATUS FromSlice(const Slice& slice, size_t size_hint = 0);
64
65
1
  const boost::asio::ip::address& address() const {
66
1
    return boost_addr_;
67
1
  }
68
69
  bool isV4() const;
70
71
  bool isV6() const;
72
73
116
  bool operator==(const InetAddress& other) const {
74
116
    return (boost_addr_ == other.boost_addr_);
75
116
  }
76
77
1
  bool operator!=(const InetAddress& other) const {
78
1
    return !(*this == other);
79
1
  }
80
81
  bool operator<(const InetAddress& other) const;
82
83
193
  bool operator>(const InetAddress& other) const {
84
193
    return (other < *this);
85
193
  }
86
87
2
  bool operator<=(const InetAddress& other) const {
88
2
    return !(other < *this);
89
2
  }
90
91
1
  bool operator>=(const InetAddress& other) const {
92
1
    return !(*this < other);
93
1
  }
94
95
359
  InetAddress& operator=(const InetAddress& other) {
96
359
    boost_addr_ = other.boost_addr_;
97
359
    return *this;
98
359
  }
99
100
 private:
101
  boost::asio::ip::address boost_addr_;
102
};
103
104
void FilterAddresses(const string &transform_spec,
105
                     vector<boost::asio::ip::address> *addresses);
106
107
} // namespace yb
108
109
#endif // YB_UTIL_NET_INETADDRESS_H