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
//
2
// ip/impl/address_v4.ipp
3
// ~~~~~~~~~~~~~~~~~~~~~~
4
//
5
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
//
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)
9
//
10

    
11
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
12
#define BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP
13

    
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
# pragma once
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17

    
18
#include <boost/asio/detail/config.hpp>
19
#include <climits>
20
#include <stdexcept>
21
#include <boost/asio/error.hpp>
22
#include <boost/asio/detail/socket_ops.hpp>
23
#include <boost/asio/detail/throw_error.hpp>
24
#include <boost/asio/detail/throw_exception.hpp>
25
#include <boost/asio/ip/address_v4.hpp>
26

    
27
#include <boost/asio/detail/push_options.hpp>
28

    
29
namespace boost {
30
namespace asio {
31
namespace ip {
32

    
33
address_v4::address_v4(const address_v4::bytes_type& bytes)
34
{
35
#if UCHAR_MAX > 0xFF
36
  if (bytes[0] > 0xFF || bytes[1] > 0xFF
37
      || bytes[2] > 0xFF || bytes[3] > 0xFF)
38
  {
39
    std::out_of_range ex("address_v4 from bytes_type");
40
    boost::asio::detail::throw_exception(ex);
41
  }
42
#endif // UCHAR_MAX > 0xFF
43

    
44
  using namespace std; // For memcpy.
45
  memcpy(&addr_.s_addr, bytes.data(), 4);
46
}
47

    
48
address_v4::address_v4(unsigned long addr)
49
{
50
#if ULONG_MAX > 0xFFFFFFFF
51
  if (addr > 0xFFFFFFFF)
52
  {
53
    std::out_of_range ex("address_v4 from unsigned long");
54
    boost::asio::detail::throw_exception(ex);
55
  }
56
#endif // ULONG_MAX > 0xFFFFFFFF
57

    
58
  addr_.s_addr = boost::asio::detail::socket_ops::host_to_network_long(
59
      static_cast<boost::asio::detail::u_long_type>(addr));
60
}
61

    
62
address_v4::bytes_type address_v4::to_bytes() const
63
{
64
  using namespace std; // For memcpy.
65
  bytes_type bytes;
66
#if defined(BOOST_ASIO_HAS_STD_ARRAY)
67
  memcpy(bytes.data(), &addr_.s_addr, 4);
68
#else // defined(BOOST_ASIO_HAS_STD_ARRAY)
69
  memcpy(bytes.elems, &addr_.s_addr, 4);
70
#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
71
  return bytes;
72
}
73

    
74
unsigned long address_v4::to_ulong() const
75
{
76
  return boost::asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
77
}
78

    
79
std::string address_v4::to_string() const
80
{
81
  boost::system::error_code ec;
82
  std::string addr = to_string(ec);
83
  boost::asio::detail::throw_error(ec);
84
  return addr;
85
}
86

    
87
std::string address_v4::to_string(boost::system::error_code& ec) const
88
{
89
  char addr_str[boost::asio::detail::max_addr_v4_str_len];
90
  const char* addr =
91
    boost::asio::detail::socket_ops::inet_ntop(
92
        BOOST_ASIO_OS_DEF(AF_INET), &addr_, addr_str,
93
        boost::asio::detail::max_addr_v4_str_len, 0, ec);
94
  if (addr == 0)
95
    return std::string();
96
  return addr;
97
}
98

    
99
address_v4 address_v4::from_string(const char* str)
100
{
101
  boost::system::error_code ec;
102
  address_v4 addr = from_string(str, ec);
103
  boost::asio::detail::throw_error(ec);
104
  return addr;
105
}
106

    
107
address_v4 address_v4::from_string(
108
    const char* str, boost::system::error_code& ec)
109
{
110
  address_v4 tmp;
111
  if (boost::asio::detail::socket_ops::inet_pton(
112
        BOOST_ASIO_OS_DEF(AF_INET), str, &tmp.addr_, 0, ec) <= 0)
113
    return address_v4();
114
  return tmp;
115
}
116

    
117
address_v4 address_v4::from_string(const std::string& str)
118
{
119
  return from_string(str.c_str());
120
}
121

    
122
address_v4 address_v4::from_string(
123
    const std::string& str, boost::system::error_code& ec)
124
{
125
  return from_string(str.c_str(), ec);
126
}
127

    
128
bool address_v4::is_loopback() const
129
{
130
  return (to_ulong() & 0xFF000000) == 0x7F000000;
131
}
132

    
133
bool address_v4::is_unspecified() const
134
{
135
  return to_ulong() == 0;
136
}
137

    
138
bool address_v4::is_class_a() const
139
{
140
  return (to_ulong() & 0x80000000) == 0;
141
}
142

    
143
bool address_v4::is_class_b() const
144
{
145
  return (to_ulong() & 0xC0000000) == 0x80000000;
146
}
147

    
148
bool address_v4::is_class_c() const
149
{
150
  return (to_ulong() & 0xE0000000) == 0xC0000000;
151
}
152

    
153
bool address_v4::is_multicast() const
154
{
155
  return (to_ulong() & 0xF0000000) == 0xE0000000;
156
}
157

    
158
address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
159
{
160
  return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
161
}
162

    
163
address_v4 address_v4::netmask(const address_v4& addr)
164
{
165
  if (addr.is_class_a())
166
    return address_v4(0xFF000000);
167
  if (addr.is_class_b())
168
    return address_v4(0xFFFF0000);
169
  if (addr.is_class_c())
170
    return address_v4(0xFFFFFF00);
171
  return address_v4(0xFFFFFFFF);
172
}
173

    
174
} // namespace ip
175
} // namespace asio
176
} // namespace boost
177

    
178
#include <boost/asio/detail/pop_options.hpp>
179

    
180
#endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_IPP