File indexing completed on 2025-01-18 09:29:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_ASIO_IP_NETWORK_V4_HPP
0013 #define BOOST_ASIO_IP_NETWORK_V4_HPP
0014
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif
0018
0019 #include <boost/asio/detail/config.hpp>
0020 #include <string>
0021 #include <boost/asio/detail/string_view.hpp>
0022 #include <boost/system/error_code.hpp>
0023 #include <boost/asio/ip/address_v4_range.hpp>
0024
0025 #include <boost/asio/detail/push_options.hpp>
0026
0027 namespace boost {
0028 namespace asio {
0029 namespace ip {
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 class network_v4
0041 {
0042 public:
0043
0044 network_v4() noexcept
0045 : address_(),
0046 prefix_length_(0)
0047 {
0048 }
0049
0050
0051 BOOST_ASIO_DECL network_v4(const address_v4& addr,
0052 unsigned short prefix_len);
0053
0054
0055 BOOST_ASIO_DECL network_v4(const address_v4& addr,
0056 const address_v4& mask);
0057
0058
0059 network_v4(const network_v4& other) noexcept
0060 : address_(other.address_),
0061 prefix_length_(other.prefix_length_)
0062 {
0063 }
0064
0065
0066 network_v4(network_v4&& other) noexcept
0067 : address_(static_cast<address_v4&&>(other.address_)),
0068 prefix_length_(other.prefix_length_)
0069 {
0070 }
0071
0072
0073 network_v4& operator=(const network_v4& other) noexcept
0074 {
0075 address_ = other.address_;
0076 prefix_length_ = other.prefix_length_;
0077 return *this;
0078 }
0079
0080
0081 network_v4& operator=(network_v4&& other) noexcept
0082 {
0083 address_ = static_cast<address_v4&&>(other.address_);
0084 prefix_length_ = other.prefix_length_;
0085 return *this;
0086 }
0087
0088
0089 address_v4 address() const noexcept
0090 {
0091 return address_;
0092 }
0093
0094
0095
0096 unsigned short prefix_length() const noexcept
0097 {
0098 return prefix_length_;
0099 }
0100
0101
0102 BOOST_ASIO_DECL address_v4 netmask() const noexcept;
0103
0104
0105 address_v4 network() const noexcept
0106 {
0107 return address_v4(address_.to_uint() & netmask().to_uint());
0108 }
0109
0110
0111 address_v4 broadcast() const noexcept
0112 {
0113 return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF));
0114 }
0115
0116
0117 BOOST_ASIO_DECL address_v4_range hosts() const noexcept;
0118
0119
0120 network_v4 canonical() const noexcept
0121 {
0122 return network_v4(network(), prefix_length());
0123 }
0124
0125
0126 bool is_host() const noexcept
0127 {
0128 return prefix_length_ == 32;
0129 }
0130
0131
0132 BOOST_ASIO_DECL bool is_subnet_of(const network_v4& other) const;
0133
0134
0135 BOOST_ASIO_DECL std::string to_string() const;
0136
0137
0138 BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
0139
0140
0141 friend bool operator==(const network_v4& a, const network_v4& b)
0142 {
0143 return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
0144 }
0145
0146
0147 friend bool operator!=(const network_v4& a, const network_v4& b)
0148 {
0149 return !(a == b);
0150 }
0151
0152 private:
0153 address_v4 address_;
0154 unsigned short prefix_length_;
0155 };
0156
0157
0158
0159
0160
0161 inline network_v4 make_network_v4(
0162 const address_v4& addr, unsigned short prefix_len)
0163 {
0164 return network_v4(addr, prefix_len);
0165 }
0166
0167
0168
0169
0170
0171 inline network_v4 make_network_v4(
0172 const address_v4& addr, const address_v4& mask)
0173 {
0174 return network_v4(addr, mask);
0175 }
0176
0177
0178
0179
0180
0181
0182 BOOST_ASIO_DECL network_v4 make_network_v4(const char* str);
0183
0184
0185
0186
0187
0188
0189 BOOST_ASIO_DECL network_v4 make_network_v4(
0190 const char* str, boost::system::error_code& ec);
0191
0192
0193
0194
0195
0196
0197 BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str);
0198
0199
0200
0201
0202
0203
0204 BOOST_ASIO_DECL network_v4 make_network_v4(
0205 const std::string& str, boost::system::error_code& ec);
0206
0207 #if defined(BOOST_ASIO_HAS_STRING_VIEW) \
0208 || defined(GENERATING_DOCUMENTATION)
0209
0210
0211
0212
0213
0214
0215 BOOST_ASIO_DECL network_v4 make_network_v4(string_view str);
0216
0217
0218
0219
0220
0221
0222 BOOST_ASIO_DECL network_v4 make_network_v4(
0223 string_view str, boost::system::error_code& ec);
0224
0225 #endif
0226
0227
0228 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 template <typename Elem, typename Traits>
0243 std::basic_ostream<Elem, Traits>& operator<<(
0244 std::basic_ostream<Elem, Traits>& os, const network_v4& net);
0245
0246 #endif
0247
0248 }
0249 }
0250 }
0251
0252 #include <boost/asio/detail/pop_options.hpp>
0253
0254 #include <boost/asio/ip/impl/network_v4.hpp>
0255 #if defined(BOOST_ASIO_HEADER_ONLY)
0256 # include <boost/asio/ip/impl/network_v4.ipp>
0257 #endif
0258
0259 #endif