Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / ip / impl / address_v4.ipp @ 2486

History | View | Annotate | Download (4.3 kB)

1 2486 sjacqu01
//
2 2486 sjacqu01
// ip/impl/address_v4.ipp
3 2486 sjacqu01
// ~~~~~~~~~~~~~~~~~~~~~~
4 2486 sjacqu01
//
5 2486 sjacqu01
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 2486 sjacqu01
//
7 2486 sjacqu01
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8 2486 sjacqu01
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 2486 sjacqu01
//
10 2486 sjacqu01
11 2486 sjacqu01
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
12 2486 sjacqu01
#define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
13 2486 sjacqu01
14 2486 sjacqu01
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 2486 sjacqu01
# pragma once
16 2486 sjacqu01
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 2486 sjacqu01
18 2486 sjacqu01
#include <boost/asio/detail/config.hpp>
19 2486 sjacqu01
#include <climits>
20 2486 sjacqu01
#include <stdexcept>
21 2486 sjacqu01
#include <boost/asio/error.hpp>
22 2486 sjacqu01
#include <boost/asio/detail/socket_ops.hpp>
23 2486 sjacqu01
#include <boost/asio/detail/throw_error.hpp>
24 2486 sjacqu01
#include <boost/asio/detail/throw_exception.hpp>
25 2486 sjacqu01
#include <boost/asio/ip/address_v4.hpp>
26 2486 sjacqu01
27 2486 sjacqu01
#include <boost/asio/detail/push_options.hpp>
28 2486 sjacqu01
29 2486 sjacqu01
namespace boost {
30 2486 sjacqu01
namespace asio {
31 2486 sjacqu01
namespace ip {
32 2486 sjacqu01
33 2486 sjacqu01
address_v4::address_v4(const address_v4::bytes_type& bytes)
34 2486 sjacqu01
{
35 2486 sjacqu01
#if UCHAR_MAX > 0xFF
36 2486 sjacqu01
  if (bytes[0] > 0xFF || bytes[1] > 0xFF
37 2486 sjacqu01
      || bytes[2] > 0xFF || bytes[3] > 0xFF)
38 2486 sjacqu01
  {
39 2486 sjacqu01
    std::out_of_range ex("address_v4 from bytes_type");
40 2486 sjacqu01
    boost::asio::detail::throw_exception(ex);
41 2486 sjacqu01
  }
42 2486 sjacqu01
#endif // UCHAR_MAX > 0xFF
43 2486 sjacqu01
44 2486 sjacqu01
  using namespace std; // For memcpy.
45 2486 sjacqu01
  memcpy(&addr_.s_addr, bytes.data(), 4);
46 2486 sjacqu01
}
47 2486 sjacqu01
48 2486 sjacqu01
address_v4::address_v4(unsigned long addr)
49 2486 sjacqu01
{
50 2486 sjacqu01
#if ULONG_MAX > 0xFFFFFFFF
51 2486 sjacqu01
  if (addr > 0xFFFFFFFF)
52 2486 sjacqu01
  {
53 2486 sjacqu01
    std::out_of_range ex("address_v4 from unsigned long");
54 2486 sjacqu01
    boost::asio::detail::throw_exception(ex);
55 2486 sjacqu01
  }
56 2486 sjacqu01
#endif // ULONG_MAX > 0xFFFFFFFF
57 2486 sjacqu01
58 2486 sjacqu01
  addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
59 2486 sjacqu01
      static_cast<boost::asio::detail::u_long_type>(addr));
60 2486 sjacqu01
}
61 2486 sjacqu01
62 2486 sjacqu01
address_v4::bytes_type address_v4::to_bytes() const
63 2486 sjacqu01
{
64 2486 sjacqu01
  using namespace std; // For memcpy.
65 2486 sjacqu01
  bytes_type bytes;
66 2486 sjacqu01
#if defined(BOOST_ASIO_HAS_STD_ARRAY)
67 2486 sjacqu01
  memcpy(bytes.data(), &addr_.s_addr, 4);
68 2486 sjacqu01
#else // defined(BOOST_ASIO_HAS_STD_ARRAY)
69 2486 sjacqu01
  memcpy(bytes.elems, &addr_.s_addr, 4);
70 2486 sjacqu01
#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
71 2486 sjacqu01
  return bytes;
72 2486 sjacqu01
}
73 2486 sjacqu01
74 2486 sjacqu01
unsigned long address_v4::to_ulong() const
75 2486 sjacqu01
{
76 2486 sjacqu01
  return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
77 2486 sjacqu01
}
78 2486 sjacqu01
79 2486 sjacqu01
std::string address_v4::to_string() const
80 2486 sjacqu01
{
81 2486 sjacqu01
  boost::system::error_code ec;
82 2486 sjacqu01
  std::string addr = to_string(ec);
83 2486 sjacqu01
  boost::asio::detail::throw_error(ec);
84 2486 sjacqu01
  return addr;
85 2486 sjacqu01
}
86 2486 sjacqu01
87 2486 sjacqu01
std::string address_v4::to_string(boost::system::error_code& ec) const
88 2486 sjacqu01
{
89 2486 sjacqu01
  char addr_str[boost::asio::detail::max_addr_v4_str_len];
90 2486 sjacqu01
  const char* addr =
91 2486 sjacqu01
    boost::asio::detail::socket_ops::inet_ntop(
92 2486 sjacqu01
        BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
93 2486 sjacqu01
        boost::asio::detail::max_addr_v4_str_len, 0, ec);
94 2486 sjacqu01
  if (addr == 0)
95 2486 sjacqu01
    return std::string();
96 2486 sjacqu01
  return addr;
97 2486 sjacqu01
}
98 2486 sjacqu01
99 2486 sjacqu01
address_v4 address_v4::from_string(const char* str)
100 2486 sjacqu01
{
101 2486 sjacqu01
  boost::system::error_code ec;
102 2486 sjacqu01
  address_v4 addr = from_string(str, ec);
103 2486 sjacqu01
  boost::asio::detail::throw_error(ec);
104 2486 sjacqu01
  return addr;
105 2486 sjacqu01
}
106 2486 sjacqu01
107 2486 sjacqu01
address_v4 address_v4::from_string(
108 2486 sjacqu01
    const char* str, boost::system::error_code& ec)
109 2486 sjacqu01
{
110 2486 sjacqu01
  address_v4 tmp;
111 2486 sjacqu01
  if (boost::asio::detail::socket_ops::inet_pton(
112 2486 sjacqu01
        BOOST_ASIO_OS_DEF(AF_INET), str, &tmp.addr_, 0, ec) <= 0)
113 2486 sjacqu01
    return address_v4();
114 2486 sjacqu01
  return tmp;
115 2486 sjacqu01
}
116 2486 sjacqu01
117 2486 sjacqu01
address_v4 address_v4::from_string(const std::string& str)
118 2486 sjacqu01
{
119 2486 sjacqu01
  return from_string(str.c_str());
120 2486 sjacqu01
}
121 2486 sjacqu01
122 2486 sjacqu01
address_v4 address_v4::from_string(
123 2486 sjacqu01
    const std::string& str, boost::system::error_code& ec)
124 2486 sjacqu01
{
125 2486 sjacqu01
  return from_string(str.c_str(), ec);
126 2486 sjacqu01
}
127 2486 sjacqu01
128 2486 sjacqu01
bool address_v4::is_loopback() const
129 2486 sjacqu01
{
130 2486 sjacqu01
  return (to_ulong() & 0xFF000000) == 0x7F000000;
131 2486 sjacqu01
}
132 2486 sjacqu01
133 2486 sjacqu01
bool address_v4::is_unspecified() const
134 2486 sjacqu01
{
135 2486 sjacqu01
  return to_ulong() == 0;
136 2486 sjacqu01
}
137 2486 sjacqu01
138 2486 sjacqu01
bool address_v4::is_class_a() const
139 2486 sjacqu01
{
140 2486 sjacqu01
  return (to_ulong() & 0x80000000) == 0;
141 2486 sjacqu01
}
142 2486 sjacqu01
143 2486 sjacqu01
bool address_v4::is_class_b() const
144 2486 sjacqu01
{
145 2486 sjacqu01
  return (to_ulong() & 0xC0000000) == 0x80000000;
146 2486 sjacqu01
}
147 2486 sjacqu01
148 2486 sjacqu01
bool address_v4::is_class_c() const
149 2486 sjacqu01
{
150 2486 sjacqu01
  return (to_ulong() & 0xE0000000) == 0xC0000000;
151 2486 sjacqu01
}
152 2486 sjacqu01
153 2486 sjacqu01
bool address_v4::is_multicast() const
154 2486 sjacqu01
{
155 2486 sjacqu01
  return (to_ulong() & 0xF0000000) == 0xE0000000;
156 2486 sjacqu01
}
157 2486 sjacqu01
158 2486 sjacqu01
address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
159 2486 sjacqu01
{
160 2486 sjacqu01
  return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
161 2486 sjacqu01
}
162 2486 sjacqu01
163 2486 sjacqu01
address_v4 address_v4::netmask(const address_v4& addr)
164 2486 sjacqu01
{
165 2486 sjacqu01
  if (addr.is_class_a())
166 2486 sjacqu01
    return address_v4(0xFF000000);
167 2486 sjacqu01
  if (addr.is_class_b())
168 2486 sjacqu01
    return address_v4(0xFFFF0000);
169 2486 sjacqu01
  if (addr.is_class_c())
170 2486 sjacqu01
    return address_v4(0xFFFFFF00);
171 2486 sjacqu01
  return address_v4(0xFFFFFFFF);
172 2486 sjacqu01
}
173 2486 sjacqu01
174 2486 sjacqu01
} // namespace ip
175 2486 sjacqu01
} // namespace asio
176 2486 sjacqu01
} // namespace boost
177 2486 sjacqu01
178 2486 sjacqu01
#include <boost/asio/detail/pop_options.hpp>
179 2486 sjacqu01
180 2486 sjacqu01
#endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP