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_UPNP_HPP
34 #define TORRENT_UPNP_HPP
36 #include "libtorrent/socket.hpp"
37 #include "libtorrent/broadcast_socket.hpp"
38 #include "libtorrent/http_connection.hpp"
39 #include "libtorrent/connection_queue.hpp"
40 #include "libtorrent/intrusive_ptr_base.hpp"
42 #include <boost/function.hpp>
43 #include <boost/noncopyable.hpp>
44 #include <boost/shared_ptr.hpp>
45 #include <boost/thread/mutex.hpp>
46 #include <boost/thread/condition.hpp>
50 #if (defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)) && !defined (TORRENT_UPNP_LOGGING)
51 #define TORRENT_UPNP_LOGGING
54 #if defined(TORRENT_UPNP_LOGGING)
61 // int: port-mapping index
63 // std::string: error message
64 // an empty string as error means success
65 typedef boost::function
<void(int, int, std::string
const&)> portmap_callback_t
;
67 class upnp
: public intrusive_ptr_base
<upnp
>
70 upnp(io_service
& ios
, connection_queue
& cc
71 , address
const& listen_interface
, std::string
const& user_agent
72 , portmap_callback_t
const& cb
, bool ignore_nonrouters
, void* state
= 0);
77 enum protocol_type
{ none
= 0, udp
= 1, tcp
= 2 };
78 int add_mapping(protocol_type p
, int external_port
, int local_port
);
79 void delete_mapping(int mapping_index
);
81 void discover_device();
84 std::string
router_model()
86 mutex_t::scoped_lock
l(m_mutex
);
92 void discover_device_impl();
93 static address_v4 upnp_multicast_address
;
94 static udp::endpoint upnp_multicast_endpoint
;
96 enum { default_lease_time
= 3600 };
98 void resend_request(error_code
const& e
);
99 void on_reply(udp::endpoint
const& from
, char* buffer
100 , std::size_t bytes_transferred
);
103 void next(rootdevice
& d
, int i
);
104 void update_map(rootdevice
& d
, int i
);
107 void on_upnp_xml(error_code
const& e
108 , libtorrent::http_parser
const& p
, rootdevice
& d
109 , http_connection
& c
);
110 void on_upnp_map_response(error_code
const& e
111 , libtorrent::http_parser
const& p
, rootdevice
& d
112 , int mapping
, http_connection
& c
);
113 void on_upnp_unmap_response(error_code
const& e
114 , libtorrent::http_parser
const& p
, rootdevice
& d
115 , int mapping
, http_connection
& c
);
116 void on_expire(error_code
const& e
);
118 void disable(char const* msg
);
119 void return_error(int mapping
, int code
);
121 void delete_port_mapping(rootdevice
& d
, int i
);
122 void create_port_mapping(http_connection
& c
, rootdevice
& d
, int i
);
123 void post(upnp::rootdevice
const& d
, std::string
const& soap
124 , std::string
const& soap_action
);
126 int num_mappings() const { return int(m_mappings
.size()); }
128 struct global_mapping_t
142 enum action_t
{ action_none
, action_add
, action_delete
};
144 : action(action_none
)
151 // the time the port mapping will expire
156 // the local port for this mapping. If this is set
157 // to 0, the mapping is not in use
160 // the external (on the NAT router) port
161 // for the mapping. This is the port we
162 // should announce to others
168 // the number of times this mapping has failed
174 rootdevice(): service_namespace(0)
175 , lease_duration(default_lease_time
)
176 , supports_specific_external(true)
187 TORRENT_ASSERT(magic
== 1337);
192 // the interface url, through which the list of
193 // supported interfaces are fetched
196 // the url to the WANIP or WANPPP interface
197 std::string control_url
;
198 // either the WANIP namespace or the WANPPP namespace
199 char const* service_namespace
;
201 std::vector
<mapping_t
> mapping
;
203 std::string hostname
;
208 // true if the device supports specifying a
209 // specific external port, false if it doesn't
210 bool supports_specific_external
;
214 mutable boost::shared_ptr
<http_connection
> upnp_connection
;
221 TORRENT_ASSERT(magic
== 1337);
222 if (!upnp_connection
) return;
223 upnp_connection
->close();
224 upnp_connection
.reset();
227 bool operator<(rootdevice
const& rhs
) const
228 { return url
< rhs
.url
; }
233 std::vector
<global_mapping_t
> mappings
;
234 std::set
<rootdevice
> devices
;
237 std::vector
<global_mapping_t
> m_mappings
;
239 std::string
const& m_user_agent
;
241 // the set of devices we've found
242 std::set
<rootdevice
> m_devices
;
244 portmap_callback_t m_callback
;
246 // current retry count
249 io_service
& m_io_service
;
251 // the udp socket used to send and receive
252 // multicast messages on the network
253 broadcast_socket m_socket
;
255 // used to resend udp packets in case
257 deadline_timer m_broadcast_timer
;
259 // timer used to refresh mappings
260 deadline_timer m_refresh_timer
;
264 bool m_ignore_non_routers
;
266 connection_queue
& m_cc
;
268 typedef boost::mutex mutex_t
;
273 #ifdef TORRENT_UPNP_LOGGING