3
Copyright (c) 2007, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_BROADCAST_SOCKET_HPP_INCLUDED
34
#define TORRENT_BROADCAST_SOCKET_HPP_INCLUDED
36
#include "libtorrent/config.hpp"
37
#include "libtorrent/socket.hpp"
38
#include <boost/shared_ptr.hpp>
39
#include <boost/function.hpp>
45
TORRENT_EXPORT bool is_local(address const& a);
46
TORRENT_EXPORT bool is_loopback(address const& addr);
47
TORRENT_EXPORT bool is_multicast(address const& addr);
48
TORRENT_EXPORT bool is_any(address const& addr);
49
TORRENT_EXPORT int cidr_distance(address const& a1, address const& a2);
51
// determines if the operating system supports IPv6
52
TORRENT_EXPORT bool supports_ipv6();
54
int common_bits(unsigned char const* b1
55
, unsigned char const* b2, int n);
57
TORRENT_EXPORT address guess_local_address(io_service&);
59
typedef boost::function<void(udp::endpoint const& from
60
, char* buffer, int size)> receive_handler_t;
62
class TORRENT_EXPORT broadcast_socket
65
broadcast_socket(io_service& ios, udp::endpoint const& multicast_endpoint
66
, receive_handler_t const& handler, bool loopback = true);
67
~broadcast_socket() { close(); }
69
void send(char const* buffer, int size, error_code& ec);
76
socket_entry(boost::shared_ptr<datagram_socket> const& s): socket(s) {}
77
boost::shared_ptr<datagram_socket> socket;
88
void on_receive(socket_entry* s, error_code const& ec
89
, std::size_t bytes_transferred);
90
void open_unicast_socket(io_service& ios, address const& addr);
91
void open_multicast_socket(io_service& ios, address const& addr
94
// these sockets are used to
95
// join the multicast group (on each interface)
96
// and receive multicast messages
97
std::list<socket_entry> m_sockets;
98
// these sockets are not bound to any
99
// specific port and are used to
100
// send messages to the multicast group
101
// and receive unicast responses
102
std::list<socket_entry> m_unicast_sockets;
103
udp::endpoint m_multicast_endpoint;
104
receive_handler_t m_on_receive;