]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/asio/asio/ip/impl/address_v4.ipp
Added the asio framework to start developing a GameServ server
[irc/gameservirc.git] / gameserv-2.0 / asio / asio / ip / impl / address_v4.ipp
1 //
2 // ip/impl/address_v4.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2011 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 ASIO_IP_IMPL_ADDRESS_V4_IPP
12 #define 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 "asio/detail/config.hpp"
19 #include <climits>
20 #include <stdexcept>
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"
26
27 #include "asio/detail/push_options.hpp"
28
29 namespace asio {
30 namespace ip {
31
32 address_v4::address_v4(const address_v4::bytes_type& bytes)
33 {
34 #if UCHAR_MAX > 0xFF
35 if (bytes[0] > 0xFF || bytes[1] > 0xFF
36 || bytes[2] > 0xFF || bytes[3] > 0xFF)
37 {
38 std::out_of_range ex("address_v4 from bytes_type");
39 boost::throw_exception(ex);
40 }
41 #endif // UCHAR_MAX > 0xFF
42
43 using namespace std; // For memcpy.
44 memcpy(&addr_.s_addr, bytes.elems, 4);
45 }
46
47 address_v4::address_v4(unsigned long addr)
48 {
49 #if ULONG_MAX > 0xFFFFFFFF
50 if (addr > 0xFFFFFFFF)
51 {
52 std::out_of_range ex("address_v4 from unsigned long");
53 boost::throw_exception(ex);
54 }
55 #endif // ULONG_MAX > 0xFFFFFFFF
56
57 addr_.s_addr = asio::detail::socket_ops::host_to_network_long(addr);
58 }
59
60 address_v4::bytes_type address_v4::to_bytes() const
61 {
62 using namespace std; // For memcpy.
63 bytes_type bytes;
64 memcpy(bytes.elems, &addr_.s_addr, 4);
65 return bytes;
66 }
67
68 unsigned long address_v4::to_ulong() const
69 {
70 return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
71 }
72
73 std::string address_v4::to_string() const
74 {
75 asio::error_code ec;
76 std::string addr = to_string(ec);
77 asio::detail::throw_error(ec);
78 return addr;
79 }
80
81 std::string address_v4::to_string(asio::error_code& ec) const
82 {
83 char addr_str[asio::detail::max_addr_v4_str_len];
84 const char* addr =
85 asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str,
86 asio::detail::max_addr_v4_str_len, 0, ec);
87 if (addr == 0)
88 return std::string();
89 return addr;
90 }
91
92 address_v4 address_v4::from_string(const char* str)
93 {
94 asio::error_code ec;
95 address_v4 addr = from_string(str, ec);
96 asio::detail::throw_error(ec);
97 return addr;
98 }
99
100 address_v4 address_v4::from_string(
101 const char* str, asio::error_code& ec)
102 {
103 address_v4 tmp;
104 if (asio::detail::socket_ops::inet_pton(
105 AF_INET, str, &tmp.addr_, 0, ec) <= 0)
106 return address_v4();
107 return tmp;
108 }
109
110 address_v4 address_v4::from_string(const std::string& str)
111 {
112 return from_string(str.c_str());
113 }
114
115 address_v4 address_v4::from_string(
116 const std::string& str, asio::error_code& ec)
117 {
118 return from_string(str.c_str(), ec);
119 }
120
121 bool address_v4::is_class_a() const
122 {
123 return IN_CLASSA(to_ulong());
124 }
125
126 bool address_v4::is_class_b() const
127 {
128 return IN_CLASSB(to_ulong());
129 }
130
131 bool address_v4::is_class_c() const
132 {
133 return IN_CLASSC(to_ulong());
134 }
135
136 bool address_v4::is_multicast() const
137 {
138 return IN_MULTICAST(to_ulong());
139 }
140
141 address_v4 address_v4::broadcast(const address_v4& addr, const address_v4& mask)
142 {
143 return address_v4(addr.to_ulong() | (mask.to_ulong() ^ 0xFFFFFFFF));
144 }
145
146 address_v4 address_v4::netmask(const address_v4& addr)
147 {
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);
155 }
156
157 } // namespace ip
158 } // namespace asio
159
160 #include "asio/detail/pop_options.hpp"
161
162 #endif // ASIO_IP_IMPL_ADDRESS_V4_IPP