~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/ip/address_v4.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// address_v4.hpp
 
3
// ~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2008 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_ADDRESS_V4_HPP
 
12
#define ASIO_IP_ADDRESS_V4_HPP
 
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/push_options.hpp"
 
19
 
 
20
#include "asio/detail/push_options.hpp"
 
21
#include <string>
 
22
#include <boost/array.hpp>
 
23
#include <boost/throw_exception.hpp>
 
24
#include "asio/detail/pop_options.hpp"
 
25
 
 
26
#include "asio/error.hpp"
 
27
#include "asio/detail/socket_ops.hpp"
 
28
#include "asio/detail/socket_types.hpp"
 
29
#include "asio/detail/throw_error.hpp"
 
30
 
 
31
namespace asio {
 
32
namespace ip {
 
33
 
 
34
/// Implements IP version 4 style addresses.
 
35
/**
 
36
 * The asio::ip::address_v4 class provides the ability to use and
 
37
 * manipulate IP version 4 addresses.
 
38
 *
 
39
 * @par Thread Safety
 
40
 * @e Distinct @e objects: Safe.@n
 
41
 * @e Shared @e objects: Unsafe.
 
42
 */
 
43
class address_v4
 
44
{
 
45
public:
 
46
  /// The type used to represent an address as an array of bytes.
 
47
  typedef boost::array<unsigned char, 4> bytes_type;
 
48
 
 
49
  /// Default constructor.
 
50
  address_v4()
 
51
  {
 
52
    addr_.s_addr = 0;
 
53
  }
 
54
 
 
55
  /// Construct an address from raw bytes.
 
56
  explicit address_v4(const bytes_type& bytes)
 
57
  {
 
58
    using namespace std; // For memcpy.
 
59
    memcpy(&addr_.s_addr, bytes.elems, 4);
 
60
  }
 
61
 
 
62
  /// Construct an address from a unsigned long in host byte order.
 
63
  explicit address_v4(unsigned long addr)
 
64
  {
 
65
    addr_.s_addr = asio::detail::socket_ops::host_to_network_long(addr);
 
66
  }
 
67
 
 
68
  /// Copy constructor.
 
69
  address_v4(const address_v4& other)
 
70
    : addr_(other.addr_)
 
71
  {
 
72
  }
 
73
 
 
74
  /// Assign from another address.
 
75
  address_v4& operator=(const address_v4& other)
 
76
  {
 
77
    addr_ = other.addr_;
 
78
    return *this;
 
79
  }
 
80
 
 
81
  /// Get the address in bytes.
 
82
  bytes_type to_bytes() const
 
83
  {
 
84
    using namespace std; // For memcpy.
 
85
    bytes_type bytes;
 
86
    memcpy(bytes.elems, &addr_.s_addr, 4);
 
87
    return bytes;
 
88
  }
 
89
 
 
90
  /// Get the address as an unsigned long in host byte order
 
91
  unsigned long to_ulong() const
 
92
  {
 
93
    return asio::detail::socket_ops::network_to_host_long(addr_.s_addr);
 
94
  }
 
95
 
 
96
  /// Get the address as a string in dotted decimal format.
 
97
  std::string to_string() const
 
98
  {
 
99
    asio::error_code ec;
 
100
    std::string addr = to_string(ec);
 
101
    asio::detail::throw_error(ec);
 
102
    return addr;
 
103
  }
 
104
 
 
105
  /// Get the address as a string in dotted decimal format.
 
106
  std::string to_string(asio::error_code& ec) const
 
107
  {
 
108
    char addr_str[asio::detail::max_addr_v4_str_len];
 
109
    const char* addr =
 
110
      asio::detail::socket_ops::inet_ntop(AF_INET, &addr_, addr_str,
 
111
          asio::detail::max_addr_v4_str_len, 0, ec);
 
112
    if (addr == 0)
 
113
      return std::string();
 
114
    return addr;
 
115
  }
 
116
 
 
117
  /// Create an address from an IP address string in dotted decimal form.
 
118
  static address_v4 from_string(const char* str)
 
119
  {
 
120
    asio::error_code ec;
 
121
    address_v4 addr = from_string(str, ec);
 
122
    asio::detail::throw_error(ec);
 
123
    return addr;
 
124
  }
 
125
 
 
126
  /// Create an address from an IP address string in dotted decimal form.
 
127
  static address_v4 from_string(const char* str, asio::error_code& ec)
 
128
  {
 
129
    address_v4 tmp;
 
130
    if (asio::detail::socket_ops::inet_pton(
 
131
          AF_INET, str, &tmp.addr_, 0, ec) <= 0)
 
132
      return address_v4();
 
133
    return tmp;
 
134
  }
 
135
 
 
136
  /// Create an address from an IP address string in dotted decimal form.
 
137
  static address_v4 from_string(const std::string& str)
 
138
  {
 
139
    return from_string(str.c_str());
 
140
  }
 
141
 
 
142
  /// Create an address from an IP address string in dotted decimal form.
 
143
  static address_v4 from_string(const std::string& str,
 
144
      asio::error_code& ec)
 
145
  {
 
146
    return from_string(str.c_str(), ec);
 
147
  }
 
148
 
 
149
  /// Determine whether the address is a class A address.
 
150
  bool is_class_a() const
 
151
  {
 
152
    return IN_CLASSA(to_ulong());
 
153
  }
 
154
 
 
155
  /// Determine whether the address is a class B address.
 
156
  bool is_class_b() const
 
157
  {
 
158
    return IN_CLASSB(to_ulong());
 
159
  }
 
160
 
 
161
  /// Determine whether the address is a class C address.
 
162
  bool is_class_c() const
 
163
  {
 
164
    return IN_CLASSC(to_ulong());
 
165
  }
 
166
 
 
167
  /// Determine whether the address is a multicast address.
 
168
  bool is_multicast() const
 
169
  {
 
170
    return IN_MULTICAST(to_ulong());
 
171
  }
 
172
 
 
173
  /// Compare two addresses for equality.
 
174
  friend bool operator==(const address_v4& a1, const address_v4& a2)
 
175
  {
 
176
    return a1.addr_.s_addr == a2.addr_.s_addr;
 
177
  }
 
178
 
 
179
  /// Compare two addresses for inequality.
 
180
  friend bool operator!=(const address_v4& a1, const address_v4& a2)
 
181
  {
 
182
    return a1.addr_.s_addr != a2.addr_.s_addr;
 
183
  }
 
184
 
 
185
  /// Compare addresses for ordering.
 
186
  friend bool operator<(const address_v4& a1, const address_v4& a2)
 
187
  {
 
188
    return a1.to_ulong() < a2.to_ulong();
 
189
  }
 
190
 
 
191
  /// Compare addresses for ordering.
 
192
  friend bool operator>(const address_v4& a1, const address_v4& a2)
 
193
  {
 
194
    return a1.to_ulong() > a2.to_ulong();
 
195
  }
 
196
 
 
197
  /// Compare addresses for ordering.
 
198
  friend bool operator<=(const address_v4& a1, const address_v4& a2)
 
199
  {
 
200
    return a1.to_ulong() <= a2.to_ulong();
 
201
  }
 
202
 
 
203
  /// Compare addresses for ordering.
 
204
  friend bool operator>=(const address_v4& a1, const address_v4& a2)
 
205
  {
 
206
    return a1.to_ulong() >= a2.to_ulong();
 
207
  }
 
208
 
 
209
  /// Obtain an address object that represents any address.
 
210
  static address_v4 any()
 
211
  {
 
212
    return address_v4(static_cast<unsigned long>(INADDR_ANY));
 
213
  }
 
214
 
 
215
  /// Obtain an address object that represents the loopback address.
 
216
  static address_v4 loopback()
 
217
  {
 
218
    return address_v4(static_cast<unsigned long>(INADDR_LOOPBACK));
 
219
  }
 
220
 
 
221
  /// Obtain an address object that represents the broadcast address.
 
222
  static address_v4 broadcast()
 
223
  {
 
224
    return address_v4(static_cast<unsigned long>(INADDR_BROADCAST));
 
225
  }
 
226
 
 
227
  /// Obtain an address object that represents the broadcast address that
 
228
  /// corresponds to the specified address and netmask.
 
229
  static address_v4 broadcast(const address_v4& addr, const address_v4& mask)
 
230
  {
 
231
    return address_v4(addr.to_ulong() | ~mask.to_ulong());
 
232
  }
 
233
 
 
234
  /// Obtain the netmask that corresponds to the address, based on its address
 
235
  /// class.
 
236
  static address_v4 netmask(const address_v4& addr)
 
237
  {
 
238
    if (addr.is_class_a())
 
239
      return address_v4(0xFF000000);
 
240
    if (addr.is_class_b())
 
241
      return address_v4(0xFFFF0000);
 
242
    if (addr.is_class_c())
 
243
      return address_v4(0xFFFFFF00);
 
244
    return address_v4(0xFFFFFFFF);
 
245
  }
 
246
 
 
247
private:
 
248
  // The underlying IPv4 address.
 
249
  asio::detail::in4_addr_type addr_;
 
250
};
 
251
 
 
252
/// Output an address as a string.
 
253
/**
 
254
 * Used to output a human-readable string for a specified address.
 
255
 *
 
256
 * @param os The output stream to which the string will be written.
 
257
 *
 
258
 * @param addr The address to be written.
 
259
 *
 
260
 * @return The output stream.
 
261
 *
 
262
 * @relates asio::ip::address_v4
 
263
 */
 
264
template <typename Elem, typename Traits>
 
265
std::basic_ostream<Elem, Traits>& operator<<(
 
266
    std::basic_ostream<Elem, Traits>& os, const address_v4& addr)
 
267
{
 
268
  asio::error_code ec;
 
269
  std::string s = addr.to_string(ec);
 
270
  if (ec)
 
271
  {
 
272
    if (os.exceptions() & std::ios::failbit)
 
273
      asio::detail::throw_error(ec);
 
274
    else
 
275
      os.setstate(std::ios_base::failbit);
 
276
  }
 
277
  else
 
278
    for (std::string::iterator i = s.begin(); i != s.end(); ++i)
 
279
      os << os.widen(*i);
 
280
  return os;
 
281
}
 
282
 
 
283
} // namespace ip
 
284
} // namespace asio
 
285
 
 
286
#include "asio/detail/pop_options.hpp"
 
287
 
 
288
#endif // ASIO_IP_ADDRESS_V4_HPP