added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / natpmp.hpp
blob8ce54132bce76bf10f8744d88bee07b3bd64063b
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_NATPMP_HPP
34 #define TORRENT_NATPMP_HPP
36 #include "libtorrent/socket.hpp"
37 #include "libtorrent/intrusive_ptr_base.hpp"
39 #include <boost/function.hpp>
40 #include <boost/thread/mutex.hpp>
42 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
43 #include <fstream>
44 #endif
46 namespace libtorrent
49 // int: port mapping index
50 // int: external port
51 // std::string: error message
52 typedef boost::function<void(int, int, std::string const&)> portmap_callback_t;
54 class natpmp : public intrusive_ptr_base<natpmp>
56 public:
57 natpmp(io_service& ios, address const& listen_interface, portmap_callback_t const& cb);
59 void rebind(address const& listen_interface);
61 // maps the ports, if a port is set to 0
62 // it will not be mapped
63 enum protocol_type { none = 0, udp = 1, tcp = 2 };
64 int add_mapping(protocol_type p, int external_port, int local_port);
65 void delete_mapping(int mapping_index);
67 void close();
69 private:
71 void update_mapping(int i);
72 void send_map_request(int i);
73 void resend_request(int i, error_code const& e);
74 void on_reply(error_code const& e
75 , std::size_t bytes_transferred);
76 void try_next_mapping(int i);
77 void update_expiration_timer();
78 void mapping_expired(error_code const& e, int i);
80 void disable(char const* message);
82 struct mapping_t
84 enum action_t { action_none, action_add, action_delete };
85 mapping_t()
86 : action(action_none)
87 , local_port(0)
88 , external_port(0)
89 , protocol(none)
92 // indicates that the mapping has changed
93 // and needs an update
94 int action;
96 // the time the port mapping will expire
97 ptime expires;
99 // the local port for this mapping. If this is set
100 // to 0, the mapping is not in use
101 int local_port;
103 // the external (on the NAT router) port
104 // for the mapping. This is the port we
105 // should announce to others
106 int external_port;
108 int protocol;
111 portmap_callback_t m_callback;
113 std::vector<mapping_t> m_mappings;
115 // the endpoint to the nat router
116 udp::endpoint m_nat_endpoint;
118 // this is the mapping that is currently
119 // being updated. It is -1 in case no
120 // mapping is being updated at the moment
121 int m_currently_mapping;
123 // current retry count
124 int m_retry_count;
126 // used to receive responses in
127 char m_response_buffer[16];
129 // the endpoint we received the message from
130 udp::endpoint m_remote;
132 // the udp socket used to communicate
133 // with the NAT router
134 datagram_socket m_socket;
136 // used to resend udp packets in case
137 // they time out
138 deadline_timer m_send_timer;
140 // timer used to refresh mappings
141 deadline_timer m_refresh_timer;
143 // the mapping index that will expire next
144 int m_next_refresh;
146 bool m_disabled;
148 bool m_abort;
150 typedef boost::mutex mutex_t;
151 mutex_t m_mutex;
153 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
154 std::ofstream m_log;
155 #endif
161 #endif