added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / upnp.hpp
blob54e832c8a6e71d21d6ebd13d11a7623a19c0268e
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_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>
47 #include <set>
50 #if (defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)) && !defined (TORRENT_UPNP_LOGGING)
51 #define TORRENT_UPNP_LOGGING
52 #endif
54 #if defined(TORRENT_UPNP_LOGGING)
55 #include <fstream>
56 #endif
58 namespace libtorrent
61 // int: port-mapping index
62 // int: external port
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>
69 public:
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);
73 ~upnp();
75 void* drain_state();
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();
82 void close();
84 std::string router_model()
86 mutex_t::scoped_lock l(m_mutex);
87 return m_model;
90 private:
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);
102 struct rootdevice;
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
130 global_mapping_t()
131 : protocol(none)
132 , external_port(0)
133 , local_port(0)
135 int protocol;
136 int external_port;
137 int local_port;
140 struct mapping_t
142 enum action_t { action_none, action_add, action_delete };
143 mapping_t()
144 : action(action_none)
145 , local_port(0)
146 , external_port(0)
147 , protocol(none)
148 , failcount(0)
151 // the time the port mapping will expire
152 ptime expires;
154 int action;
156 // the local port for this mapping. If this is set
157 // to 0, the mapping is not in use
158 int local_port;
160 // the external (on the NAT router) port
161 // for the mapping. This is the port we
162 // should announce to others
163 int external_port;
165 // 2 = udp, 1 = tcp
166 int protocol;
168 // the number of times this mapping has failed
169 int failcount;
172 struct rootdevice
174 rootdevice(): service_namespace(0)
175 , lease_duration(default_lease_time)
176 , supports_specific_external(true)
177 , disabled(false)
179 #ifndef NDEBUG
180 magic = 1337;
181 #endif
184 #ifndef NDEBUG
185 ~rootdevice()
187 TORRENT_ASSERT(magic == 1337);
188 magic = 0;
190 #endif
192 // the interface url, through which the list of
193 // supported interfaces are fetched
194 std::string url;
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;
204 int port;
205 std::string path;
207 int lease_duration;
208 // true if the device supports specifying a
209 // specific external port, false if it doesn't
210 bool supports_specific_external;
212 bool disabled;
214 mutable boost::shared_ptr<http_connection> upnp_connection;
216 #ifndef NDEBUG
217 int magic;
218 #endif
219 void close() const
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; }
231 struct upnp_state_t
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
247 int m_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
256 // they time out
257 deadline_timer m_broadcast_timer;
259 // timer used to refresh mappings
260 deadline_timer m_refresh_timer;
262 bool m_disabled;
263 bool m_closing;
264 bool m_ignore_non_routers;
266 connection_queue& m_cc;
268 typedef boost::mutex mutex_t;
269 mutex_t m_mutex;
271 std::string m_model;
273 #ifdef TORRENT_UPNP_LOGGING
274 std::ofstream m_log;
275 #endif
281 #endif