2 // ip/impl/address_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef ASIO_IP_IMPL_ADDRESS_V4_IPP
12 #define ASIO_IP_IMPL_ADDRESS_V4_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include "asio/detail/config.hpp"
21 #include <boost/throw_exception.hpp>
22 #include "asio/error.hpp"
23 #include "asio/detail/socket_ops.hpp"
24 #include "asio/detail/throw_error.hpp"
25 #include "asio/ip/address_v4.hpp"
27 #include "asio/detail/push_options.hpp"
32 address_v4::address_v4(const address_v4::bytes_type& bytes)
35 if (bytes[0] > 0xFF || bytes[1] > 0xFF
36 || bytes[2] > 0xFF || bytes[3] > 0xFF)
38 std::out_of_range ex("address_v4 from bytes_type");
39 boost::throw_exception(ex);
41 #endif // UCHAR_MAX > 0xFF
43 using namespace std; // For memcpy.
44 memcpy(&addr_.s_addr, bytes.elems, 4);
47 address_v4::address_v4(unsigned long addr)
49 #if ULONG_MAX > 0xFFFFFFFF
50 if (addr > 0xFFFFFFFF)
52 std::out_of_range ex("address_v4 from unsigned long");
53 boost::throw_exception(ex);
55 #endif // ULONG_MAX > 0xFFFFFFFF
57 addr_.s_addr = asio::detail::socket_ops::host_to_network_long(addr);
60 address_v4::bytes_type address_v4::to_bytes() const
62 using namespace std; // For memcpy.
64 memcpy(bytes.elems, &addr_.s_addr, 4);
68 unsigned long address_v4::to_ulong() const
70 return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
73 std::string address_v4::to_string() const
76 std::string addr = to_string(ec);
77 asio::detail::throw_error(ec);
81 std::string address_v4::to_string(asio::error_code& ec) const
83 char addr_str[asio::detail::max_addr_v4_str_len];
85 asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str,
86 asio::detail::max_addr_v4_str_len, 0, ec);
92 address_v4 address_v4::from_string(const char* str)
95 address_v4 addr = from_string(str, ec);
96 asio::detail::throw_error(ec);
100 address_v4 address_v4::from_string(
101 const char* str, asio::error_code& ec)
104 if (asio::detail::socket_ops::inet_pton(
105 AF_INET, str, &tmp.addr_, 0, ec) <= 0)
110 address_v4 address_v4::from_string(const std::string& str)
112 return from_string(str.c_str());
115 address_v4 address_v4::from_string(
116 const std::string& str, asio::error_code& ec)
118 return from_string(str.c_str(), ec);
121 bool address_v4::is_class_a() const
123 return IN_CLASSA(to_ulong());
126 bool address_v4::is_class_b() const
128 return IN_CLASSB(to_ulong());
131 bool address_v4::is_class_c() const
133 return IN_CLASSC(to_ulong());
136 bool address_v4::is_multicast() const
138 return IN_MULTICAST(to_ulong());
141 address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
143 return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
146 address_v4 address_v4::netmask(const address_v4& addr)
148 if (addr.is_class_a())
149 return address_v4(0xFF000000);
150 if (addr.is_class_b())
151 return address_v4(0xFFFF0000);
152 if (addr.is_class_c())
153 return address_v4(0xFFFFFF00);
154 return address_v4(0xFFFFFFFF);
160 #include "asio/detail/pop_options.hpp"
162 #endif // ASIO_IP_IMPL_ADDRESS_V4_IPP