added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / broadcast_socket.hpp
blob3a8d6a76e4789913840d5328613ef09752a381df
1 /*
3 Copyright (c) 2007, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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>
40 #include <list>
42 namespace libtorrent
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 int common_bits(unsigned char const* b1
52 , unsigned char const* b2, int n);
54 TORRENT_EXPORT address guess_local_address(io_service&);
56 typedef boost::function<void(udp::endpoint const& from
57 , char* buffer, int size)> receive_handler_t;
59 class TORRENT_EXPORT broadcast_socket
61 public:
62 broadcast_socket(io_service& ios, udp::endpoint const& multicast_endpoint
63 , receive_handler_t const& handler, bool loopback = true);
64 ~broadcast_socket() { close(); }
66 void send(char const* buffer, int size, error_code& ec);
67 void close();
69 private:
71 struct socket_entry
73 socket_entry(boost::shared_ptr<datagram_socket> const& s): socket(s) {}
74 boost::shared_ptr<datagram_socket> socket;
75 char buffer[1024];
76 udp::endpoint remote;
77 void close()
79 if (!socket) return;
80 error_code ec;
81 socket->close(ec);
85 void on_receive(socket_entry* s, error_code const& ec
86 , std::size_t bytes_transferred);
87 void open_unicast_socket(io_service& ios, address const& addr);
88 void open_multicast_socket(io_service& ios, address const& addr
89 , bool loopback);
91 // these sockets are used to
92 // join the multicast group (on each interface)
93 // and receive multicast messages
94 std::list<socket_entry> m_sockets;
95 // these sockets are not bound to any
96 // specific port and are used to
97 // send messages to the multicast group
98 // and receive unicast responses
99 std::list<socket_entry> m_unicast_sockets;
100 udp::endpoint m_multicast_endpoint;
101 receive_handler_t m_on_receive;
106 #endif