5
// Copyright (c) 2003-2007 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_DETAIL_SOCKET_OPTION_HPP
12
#define ASIO_IP_DETAIL_SOCKET_OPTION_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
23
#include <boost/config.hpp>
24
#include "asio/detail/pop_options.hpp"
26
#include "asio/ip/address.hpp"
27
#include "asio/detail/socket_ops.hpp"
28
#include "asio/detail/socket_types.hpp"
33
namespace socket_option {
35
// Helper template for implementing boolean-based options.
36
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
41
typedef unsigned char value_type;
43
typedef int value_type;
46
// Default constructor.
52
// Construct with a specific option value.
53
explicit boolean(bool v)
58
// Set the value of the boolean.
59
boolean& operator=(bool v)
65
// Get the current value of the boolean.
78
bool operator!() const
83
// Get the level of the socket option.
84
template <typename Protocol>
85
int level(const Protocol& protocol) const
87
if (protocol.family() == PF_INET6)
92
// Get the name of the socket option.
93
template <typename Protocol>
94
int name(const Protocol& protocol) const
96
if (protocol.family() == PF_INET6)
101
// Get the address of the boolean data.
102
template <typename Protocol>
103
value_type* data(const Protocol&)
108
// Get the address of the boolean data.
109
template <typename Protocol>
110
const value_type* data(const Protocol&) const
115
// Get the size of the boolean data.
116
template <typename Protocol>
117
std::size_t size(const Protocol&) const
119
return sizeof(value_);
122
// Set the size of the boolean data.
123
template <typename Protocol>
124
void resize(const Protocol&, std::size_t s)
126
if (s != sizeof(value_))
127
throw std::length_error("boolean socket option resize");
134
// Helper template for implementing unicast hops options.
135
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
139
// Default constructor.
145
// Construct with a specific option value.
146
explicit unicast_hops(int v)
151
// Set the value of the option.
152
unicast_hops& operator=(int v)
158
// Get the current value of the option.
164
// Get the level of the socket option.
165
template <typename Protocol>
166
int level(const Protocol& protocol) const
168
if (protocol.family() == PF_INET6)
173
// Get the name of the socket option.
174
template <typename Protocol>
175
int name(const Protocol& protocol) const
177
if (protocol.family() == PF_INET6)
182
// Get the address of the data.
183
template <typename Protocol>
184
int* data(const Protocol&)
189
// Get the address of the data.
190
template <typename Protocol>
191
const int* data(const Protocol&) const
196
// Get the size of the data.
197
template <typename Protocol>
198
std::size_t size(const Protocol&) const
200
return sizeof(value_);
203
// Set the size of the data.
204
template <typename Protocol>
205
void resize(const Protocol&, std::size_t s)
207
if (s != sizeof(value_))
208
throw std::length_error("unicast hops socket option resize");
215
// Helper template for implementing multicast hops options.
216
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
220
// Default constructor.
227
// Construct with a specific option value.
228
explicit multicast_hops(int v)
230
if (v < 0 || v > 255)
231
throw std::out_of_range("multicast hops value out of range");
232
ipv4_value_ = static_cast<unsigned char>(v);
236
// Set the value of the option.
237
multicast_hops& operator=(int v)
239
if (v < 0 || v > 255)
240
throw std::out_of_range("multicast hops value out of range");
241
ipv4_value_ = static_cast<unsigned char>(v);
246
// Get the current value of the option.
252
// Get the level of the socket option.
253
template <typename Protocol>
254
int level(const Protocol& protocol) const
256
if (protocol.family() == PF_INET6)
261
// Get the name of the socket option.
262
template <typename Protocol>
263
int name(const Protocol& protocol) const
265
if (protocol.family() == PF_INET6)
270
// Get the address of the data.
271
template <typename Protocol>
272
void* data(const Protocol& protocol)
274
if (protocol.family() == PF_INET6)
279
// Get the address of the data.
280
template <typename Protocol>
281
const void* data(const Protocol& protocol) const
283
if (protocol.family() == PF_INET6)
288
// Get the size of the data.
289
template <typename Protocol>
290
std::size_t size(const Protocol& protocol) const
292
if (protocol.family() == PF_INET6)
293
return sizeof(ipv6_value_);
294
return sizeof(ipv4_value_);
297
// Set the size of the data.
298
template <typename Protocol>
299
void resize(const Protocol& protocol, std::size_t s)
301
if (protocol.family() == PF_INET6)
303
if (s != sizeof(ipv6_value_))
304
throw std::length_error("multicast hops socket option resize");
307
else if (ipv6_value_ > 255)
310
ipv4_value_ = static_cast<unsigned char>(ipv6_value_);
314
if (s != sizeof(ipv4_value_))
315
throw std::length_error("multicast hops socket option resize");
316
ipv6_value_ = ipv4_value_;
321
unsigned char ipv4_value_;
325
// Helper template for implementing ip_mreq-based options.
326
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
327
class multicast_request
330
// Default constructor.
333
ipv4_value_.imr_multiaddr.s_addr =
334
asio::detail::socket_ops::host_to_network_long(
335
asio::ip::address_v4::any().to_ulong());
336
ipv4_value_.imr_interface.s_addr =
337
asio::detail::socket_ops::host_to_network_long(
338
asio::ip::address_v4::any().to_ulong());
340
asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
341
ipv6_value_.ipv6mr_multiaddr = tmp_addr;
342
ipv6_value_.ipv6mr_interface = 0;
345
// Construct with multicast address only.
346
explicit multicast_request(const asio::ip::address& multicast_address)
348
if (multicast_address.is_v6())
350
ipv4_value_.imr_multiaddr.s_addr =
351
asio::detail::socket_ops::host_to_network_long(
352
asio::ip::address_v4::any().to_ulong());
353
ipv4_value_.imr_interface.s_addr =
354
asio::detail::socket_ops::host_to_network_long(
355
asio::ip::address_v4::any().to_ulong());
357
using namespace std; // For memcpy.
358
asio::ip::address_v6 ipv6_address = multicast_address.to_v6();
359
asio::ip::address_v6::bytes_type bytes = ipv6_address.to_bytes();
360
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
361
ipv6_value_.ipv6mr_interface = 0;
365
ipv4_value_.imr_multiaddr.s_addr =
366
asio::detail::socket_ops::host_to_network_long(
367
multicast_address.to_v4().to_ulong());
368
ipv4_value_.imr_interface.s_addr =
369
asio::detail::socket_ops::host_to_network_long(
370
asio::ip::address_v4::any().to_ulong());
372
asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
373
ipv6_value_.ipv6mr_multiaddr = tmp_addr;
374
ipv6_value_.ipv6mr_interface = 0;
378
// Construct with multicast address and IPv4 address specifying an interface.
379
explicit multicast_request(
380
const asio::ip::address_v4& multicast_address,
381
const asio::ip::address_v4& network_interface
382
= asio::ip::address_v4::any())
384
ipv4_value_.imr_multiaddr.s_addr =
385
asio::detail::socket_ops::host_to_network_long(
386
multicast_address.to_ulong());
387
ipv4_value_.imr_interface.s_addr =
388
asio::detail::socket_ops::host_to_network_long(
389
network_interface.to_ulong());
391
asio::detail::in6_addr_type tmp_addr = IN6ADDR_ANY_INIT;
392
ipv6_value_.ipv6mr_multiaddr = tmp_addr;
393
ipv6_value_.ipv6mr_interface = 0;
396
// Construct with multicast address and IPv6 network interface index.
397
explicit multicast_request(
398
const asio::ip::address_v6& multicast_address,
399
unsigned long network_interface = 0)
401
ipv4_value_.imr_multiaddr.s_addr =
402
asio::detail::socket_ops::host_to_network_long(
403
asio::ip::address_v4::any().to_ulong());
404
ipv4_value_.imr_interface.s_addr =
405
asio::detail::socket_ops::host_to_network_long(
406
asio::ip::address_v4::any().to_ulong());
408
using namespace std; // For memcpy.
409
asio::ip::address_v6::bytes_type bytes =
410
multicast_address.to_bytes();
411
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
412
ipv6_value_.ipv6mr_interface = network_interface;
415
// Get the level of the socket option.
416
template <typename Protocol>
417
int level(const Protocol& protocol) const
419
if (protocol.family() == PF_INET6)
424
// Get the name of the socket option.
425
template <typename Protocol>
426
int name(const Protocol& protocol) const
428
if (protocol.family() == PF_INET6)
433
// Get the address of the option data.
434
template <typename Protocol>
435
const void* data(const Protocol& protocol) const
437
if (protocol.family() == PF_INET6)
442
// Get the size of the option data.
443
template <typename Protocol>
444
std::size_t size(const Protocol& protocol) const
446
if (protocol.family() == PF_INET6)
447
return sizeof(ipv6_value_);
448
return sizeof(ipv4_value_);
452
asio::detail::in4_mreq_type ipv4_value_;
453
asio::detail::in6_mreq_type ipv6_value_;
456
// Helper template for implementing options that specify a network interface.
457
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
458
class network_interface
461
// Default constructor.
465
asio::detail::socket_ops::host_to_network_long(
466
asio::ip::address_v4::any().to_ulong());
470
// Construct with IPv4 interface.
471
explicit network_interface(const asio::ip::address_v4& ipv4_interface)
474
asio::detail::socket_ops::host_to_network_long(
475
ipv4_interface.to_ulong());
479
// Construct with IPv6 interface.
480
explicit network_interface(unsigned long ipv6_interface)
483
asio::detail::socket_ops::host_to_network_long(
484
asio::ip::address_v4::any().to_ulong());
485
ipv6_value_ = ipv6_interface;
488
// Get the level of the socket option.
489
template <typename Protocol>
490
int level(const Protocol& protocol) const
492
if (protocol.family() == PF_INET6)
497
// Get the name of the socket option.
498
template <typename Protocol>
499
int name(const Protocol& protocol) const
501
if (protocol.family() == PF_INET6)
506
// Get the address of the option data.
507
template <typename Protocol>
508
const void* data(const Protocol& protocol) const
510
if (protocol.family() == PF_INET6)
515
// Get the size of the option data.
516
template <typename Protocol>
517
std::size_t size(const Protocol& protocol) const
519
if (protocol.family() == PF_INET6)
520
return sizeof(ipv6_value_);
521
return sizeof(ipv4_value_);
525
asio::detail::in4_addr_type ipv4_value_;
526
unsigned long ipv6_value_;
529
} // namespace socket_option
530
} // namespace detail
534
#include "asio/detail/pop_options.hpp"
536
#endif // ASIO_IP_DETAIL_SOCKET_OPTION_HPP