revamped part of the port mapping code (UPnP and NAT-PMP). Added documentation for start_{lsd,natpmp,upnp} and stop_{lsd,natpmp,upnp}
This commit is contained in:
@@ -3,6 +3,9 @@ use-project /torrent : .. ;
|
||||
exe test_upnp : test_upnp.cpp /torrent//torrent
|
||||
: <link>static <threading>multi <logging>verbose <upnp-logging>on ;
|
||||
|
||||
exe test_natpmp : test_natpmp.cpp /torrent//torrent
|
||||
: <link>static <threading>multi <logging>verbose <upnp-logging>on ;
|
||||
|
||||
lib test_common
|
||||
:
|
||||
main.cpp
|
||||
|
56
test/test_natpmp.cpp
Normal file
56
test/test_natpmp.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "libtorrent/natpmp.hpp"
|
||||
#include "libtorrent/socket.hpp"
|
||||
#include "libtorrent/connection_queue.hpp"
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/intrusive_ptr.hpp>
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
void callback(int mapping, int port, std::string const& err)
|
||||
{
|
||||
std::cerr << "mapping: " << mapping << ", port: " << port << ", error: \"" << err << "\"\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
io_service ios;
|
||||
std::string user_agent = "test agent";
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "usage: " << argv[0] << " tcp-port udp-port" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
connection_queue cc(ios);
|
||||
boost::intrusive_ptr<natpmp> natpmp_handler = new natpmp(ios, address_v4(), &callback);
|
||||
|
||||
deadline_timer timer(ios);
|
||||
|
||||
int tcp_map = natpmp_handler->add_mapping(natpmp::tcp, atoi(argv[1]), atoi(argv[1]));
|
||||
int udp_map = natpmp_handler->add_mapping(natpmp::udp, atoi(argv[2]), atoi(argv[2]));
|
||||
|
||||
timer.expires_from_now(seconds(2));
|
||||
timer.async_wait(boost::bind(&io_service::stop, boost::ref(ios)));
|
||||
std::cerr << "mapping ports TCP: " << argv[1]
|
||||
<< " UDP: " << argv[2] << std::endl;
|
||||
|
||||
ios.reset();
|
||||
ios.run();
|
||||
timer.expires_from_now(seconds(2));
|
||||
timer.async_wait(boost::bind(&io_service::stop, boost::ref(ios)));
|
||||
std::cerr << "removing mapping " << tcp_map << std::endl;
|
||||
natpmp_handler->delete_mapping(tcp_map);
|
||||
|
||||
ios.reset();
|
||||
ios.run();
|
||||
std::cerr << "removing mappings" << std::endl;
|
||||
natpmp_handler->close();
|
||||
|
||||
ios.reset();
|
||||
ios.run();
|
||||
std::cerr << "closing" << std::endl;
|
||||
}
|
||||
|
||||
|
@@ -7,9 +7,9 @@
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
void callback(int tcp, int udp, std::string const& err)
|
||||
void callback(int mapping, int port, std::string const& err)
|
||||
{
|
||||
std::cerr << "tcp: " << tcp << ", udp: " << udp << ", error: \"" << err << "\"\n";
|
||||
std::cerr << "mapping: " << mapping << ", port: " << port << ", error: \"" << err << "\"\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
@@ -36,8 +36,9 @@ int main(int argc, char* argv[])
|
||||
ios.reset();
|
||||
ios.run();
|
||||
|
||||
upnp_handler->set_mappings(atoi(argv[1]), atoi(argv[2]));
|
||||
timer.expires_from_now(seconds(5));
|
||||
upnp_handler->add_mapping(upnp::tcp, atoi(argv[1]), atoi(argv[1]));
|
||||
upnp_handler->add_mapping(upnp::udp, atoi(argv[2]), atoi(argv[2]));
|
||||
timer.expires_from_now(seconds(10));
|
||||
timer.async_wait(boost::bind(&io_service::stop, boost::ref(ios)));
|
||||
std::cerr << "mapping ports TCP: " << argv[1]
|
||||
<< " UDP: " << argv[2] << std::endl;
|
||||
|
Reference in New Issue
Block a user