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

« back to all changes in this revision

Viewing changes to src/broadcast_socket.cpp

  • 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
 
 
3
Copyright (c) 2007, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
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.
 
18
 
 
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.
 
30
 
 
31
*/
 
32
 
 
33
#include <asio/ip/host_name.hpp>
 
34
#include <asio/ip/multicast.hpp>
 
35
#include <boost/bind.hpp>
 
36
 
 
37
#include "libtorrent/socket.hpp"
 
38
#include "libtorrent/enum_net.hpp"
 
39
#include "libtorrent/broadcast_socket.hpp"
 
40
#include "libtorrent/assert.hpp"
 
41
 
 
42
namespace libtorrent
 
43
{
 
44
        bool is_local(address const& a)
 
45
        {
 
46
                if (a.is_v6()) return a.to_v6().is_link_local();
 
47
                address_v4 a4 = a.to_v4();
 
48
                unsigned long ip = a4.to_ulong();
 
49
                return ((ip & 0xff000000) == 0x0a000000
 
50
                        || (ip & 0xfff00000) == 0xac100000
 
51
                        || (ip & 0xffff0000) == 0xc0a80000);
 
52
        }
 
53
 
 
54
        bool is_loopback(address const& addr)
 
55
        {
 
56
                if (addr.is_v4())
 
57
                  return addr.to_v4() == address_v4::loopback();
 
58
                else
 
59
                        return addr.to_v6() == address_v6::loopback();
 
60
        }
 
61
 
 
62
        bool is_multicast(address const& addr)
 
63
        {
 
64
                if (addr.is_v4())
 
65
                        return addr.to_v4().is_multicast();
 
66
                else
 
67
                        return addr.to_v6().is_multicast();
 
68
        }
 
69
 
 
70
        bool is_any(address const& addr)
 
71
        {
 
72
                if (addr.is_v4())
 
73
                        return addr.to_v4() == address_v4::any();
 
74
                else
 
75
                        return addr.to_v6() == address_v6::any();
 
76
        }
 
77
 
 
78
        address guess_local_address(asio::io_service& ios)
 
79
        {
 
80
                // make a best guess of the interface we're using and its IP
 
81
                asio::error_code ec;
 
82
                std::vector<ip_interface> const& interfaces = enum_net_interfaces(ios, ec);
 
83
                address ret = address_v4::any();
 
84
                for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
 
85
                        , end(interfaces.end()); i != end; ++i)
 
86
                {
 
87
                        address const& a = i->interface_address;
 
88
                        if (is_loopback(a)
 
89
                                || is_multicast(a)
 
90
                                || is_any(a)) continue;
 
91
 
 
92
                        // prefer a v4 address, but return a v6 if
 
93
                        // there are no v4
 
94
                        if (a.is_v4()) return a;
 
95
 
 
96
                        if (ret != address_v4::any())
 
97
                                ret = a;
 
98
                }
 
99
                return ret;
 
100
        }
 
101
 
 
102
        // count the length of the common bit prefix
 
103
        int common_bits(unsigned char const* b1
 
104
                , unsigned char const* b2, int n)
 
105
        {
 
106
                for (int i = 0; i < n; ++i, ++b1, ++b2)
 
107
                {
 
108
                        unsigned char a = *b1 ^ *b2;
 
109
                        if (a == 0) continue;
 
110
                        int ret = i * 8 + 8;
 
111
                        for (; a > 0; a >>= 1) --ret;
 
112
                        return ret;
 
113
                }
 
114
                return n * 8;
 
115
        }
 
116
 
 
117
        // returns the number of bits in that differ from the right
 
118
        // between the addresses.
 
119
        int cidr_distance(address const& a1, address const& a2)
 
120
        {
 
121
                if (a1.is_v4() == a2.is_v4())
 
122
                {
 
123
                        // both are v4
 
124
                        address_v4::bytes_type b1 = a1.to_v4().to_bytes();
 
125
                        address_v4::bytes_type b2 = a2.to_v4().to_bytes();
 
126
                        return address_v4::bytes_type::static_size * 8
 
127
                                - common_bits(b1.c_array(), b2.c_array(), b1.size());
 
128
                }
 
129
        
 
130
                address_v6::bytes_type b1;
 
131
                address_v6::bytes_type b2;
 
132
                if (a1.is_v4()) b1 = address_v6::v4_mapped(a1.to_v4()).to_bytes();
 
133
                else b1 = a1.to_v6().to_bytes();
 
134
                if (a2.is_v4()) b2 = address_v6::v4_mapped(a2.to_v4()).to_bytes();
 
135
                else b2 = a2.to_v6().to_bytes();
 
136
                return address_v6::bytes_type::static_size * 8
 
137
                        - common_bits(b1.c_array(), b2.c_array(), b1.size());
 
138
        }
 
139
 
 
140
        broadcast_socket::broadcast_socket(asio::io_service& ios
 
141
                , udp::endpoint const& multicast_endpoint
 
142
                , receive_handler_t const& handler
 
143
                , bool loopback)
 
144
                : m_multicast_endpoint(multicast_endpoint)
 
145
                , m_on_receive(handler)
 
146
        {
 
147
                TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address()));
 
148
 
 
149
                using namespace asio::ip::multicast;
 
150
        
 
151
                asio::error_code ec;
 
152
                std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
 
153
 
 
154
                for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
 
155
                        , end(interfaces.end()); i != end; ++i)
 
156
                {
 
157
                        // only broadcast to IPv4 addresses that are not local
 
158
                        if (!is_local(i->interface_address)) continue;
 
159
                        // only multicast on compatible networks
 
160
                        if (i->interface_address.is_v4() != multicast_endpoint.address().is_v4()) continue;
 
161
                        // ignore any loopback interface
 
162
                        if (is_loopback(i->interface_address)) continue;
 
163
 
 
164
                        boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
 
165
                        if (i->interface_address.is_v4())
 
166
                        {
 
167
                                s->open(udp::v4(), ec);
 
168
                                if (ec) continue;
 
169
                                s->set_option(datagram_socket::reuse_address(true), ec);
 
170
                                if (ec) continue;
 
171
                                s->bind(udp::endpoint(address_v4::any(), multicast_endpoint.port()), ec);
 
172
                                if (ec) continue;
 
173
                                s->set_option(join_group(multicast_endpoint.address()), ec);
 
174
                                if (ec) continue;
 
175
                                s->set_option(outbound_interface(i->interface_address.to_v4()), ec);
 
176
                                if (ec) continue;
 
177
                        }
 
178
                        else
 
179
                        {
 
180
                                s->open(udp::v6(), ec);
 
181
                                if (ec) continue;
 
182
                                s->set_option(datagram_socket::reuse_address(true), ec);
 
183
                                if (ec) continue;
 
184
                                s->bind(udp::endpoint(address_v6::any(), multicast_endpoint.port()), ec);
 
185
                                if (ec) continue;
 
186
                                s->set_option(join_group(multicast_endpoint.address()), ec);
 
187
                                if (ec) continue;
 
188
//                              s->set_option(outbound_interface(i->interface_address.to_v6()), ec);
 
189
//                              if (ec) continue;
 
190
                        }
 
191
                        s->set_option(hops(255), ec);
 
192
                        if (ec) continue;
 
193
                        s->set_option(enable_loopback(loopback), ec);
 
194
                        if (ec) continue;
 
195
                        m_sockets.push_back(socket_entry(s));
 
196
                        socket_entry& se = m_sockets.back();
 
197
                        s->async_receive_from(asio::buffer(se.buffer, sizeof(se.buffer))
 
198
                                , se.remote, bind(&broadcast_socket::on_receive, this, &se, _1, _2));
 
199
#ifndef NDEBUG
 
200
//                      std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
 
201
//                              << " group: " << multicast_endpoint.address() << " ]" << std::endl;
 
202
#endif
 
203
                }
 
204
        }
 
205
 
 
206
        void broadcast_socket::send(char const* buffer, int size, asio::error_code& ec)
 
207
        {
 
208
                for (std::list<socket_entry>::iterator i = m_sockets.begin()
 
209
                        , end(m_sockets.end()); i != end; ++i)
 
210
                {
 
211
                        if (!i->socket) continue;
 
212
                        asio::error_code e;
 
213
                        i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e);
 
214
#ifndef NDEBUG
 
215
//                      std::cerr << " sending on " << i->socket->local_endpoint().address().to_string() << std::endl;
 
216
#endif
 
217
                        if (e)
 
218
                        {
 
219
                                i->socket->close(e);
 
220
                                i->socket.reset();
 
221
                        }
 
222
                }
 
223
        }
 
224
 
 
225
        void broadcast_socket::on_receive(socket_entry* s, asio::error_code const& ec
 
226
                , std::size_t bytes_transferred)
 
227
        {
 
228
                if (ec || bytes_transferred == 0 || !m_on_receive) return;
 
229
                m_on_receive(s->remote, s->buffer, bytes_transferred);
 
230
                if (!s->socket) return;
 
231
                s->socket->async_receive_from(asio::buffer(s->buffer, sizeof(s->buffer))
 
232
                        , s->remote, bind(&broadcast_socket::on_receive, this, s, _1, _2));
 
233
        }
 
234
 
 
235
        void broadcast_socket::close()
 
236
        {
 
237
                m_on_receive.clear();
 
238
 
 
239
                for (std::list<socket_entry>::iterator i = m_sockets.begin()
 
240
                        , end(m_sockets.end()); i != end; ++i)
 
241
                {
 
242
                        if (!i->socket) continue;
 
243
                        i->socket->close();
 
244
                }
 
245
        }
 
246
}
 
247
 
 
248