1 #include "libtorrent/natpmp.hpp"
2 #include "libtorrent/socket.hpp"
3 #include "libtorrent/connection_queue.hpp"
4 #include <boost/bind.hpp>
5 #include <boost/ref.hpp>
6 #include <boost/intrusive_ptr.hpp>
8 using namespace libtorrent
;
10 void callback(int mapping
, int port
, std::string
const& err
)
12 std::cerr
<< "mapping: " << mapping
<< ", port: " << port
<< ", error: \"" << err
<< "\"\n";
15 int main(int argc
, char* argv
[])
18 std::string user_agent
= "test agent";
22 std::cerr
<< "usage: " << argv
[0] << " tcp-port udp-port" << std::endl
;
26 connection_queue
cc(ios
);
27 boost::intrusive_ptr
<natpmp
> natpmp_handler
= new natpmp(ios
, address_v4(), &callback
);
29 deadline_timer
timer(ios
);
31 int tcp_map
= natpmp_handler
->add_mapping(natpmp::tcp
, atoi(argv
[1]), atoi(argv
[1]));
32 int udp_map
= natpmp_handler
->add_mapping(natpmp::udp
, atoi(argv
[2]), atoi(argv
[2]));
34 timer
.expires_from_now(seconds(2));
35 timer
.async_wait(boost::bind(&io_service::stop
, boost::ref(ios
)));
36 std::cerr
<< "mapping ports TCP: " << argv
[1]
37 << " UDP: " << argv
[2] << std::endl
;
41 timer
.expires_from_now(seconds(2));
42 timer
.async_wait(boost::bind(&io_service::stop
, boost::ref(ios
)));
43 std::cerr
<< "removing mapping " << tcp_map
<< std::endl
;
44 natpmp_handler
->delete_mapping(tcp_map
);
48 std::cerr
<< "removing mappings" << std::endl
;
49 natpmp_handler
->close();
53 std::cerr
<< "closing" << std::endl
;