2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-04-02 07:14:20 +02:00
|
|
|
#include "libtorrent/upnp.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
2009-12-16 11:42:34 +01:00
|
|
|
#include "libtorrent/socket_io.hpp" // print_endpoint
|
2009-06-29 10:09:32 +02:00
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
2017-01-06 07:39:01 +01:00
|
|
|
#include "libtorrent/file.hpp"
|
2009-06-29 10:09:32 +02:00
|
|
|
#include <fstream>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2016-06-23 07:23:59 +02:00
|
|
|
|
2016-06-26 15:24:06 +02:00
|
|
|
namespace lt = libtorrent;
|
2007-04-02 07:14:20 +02:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2016-09-18 16:11:56 +02:00
|
|
|
using libtorrent::aux::portmap_protocol;
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
broadcast_socket* sock = nullptr;
|
2010-01-24 00:57:11 +01:00
|
|
|
int g_port = 0;
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2015-07-08 13:44:54 +02:00
|
|
|
char const* soap_add_response[] = {
|
2009-06-29 10:09:32 +02:00
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">"
|
2015-07-08 13:44:54 +02:00
|
|
|
"</u:AddPortMapping></s:Body></s:Envelope>",
|
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">"
|
|
|
|
"</u:AddPortMapping></s:Body></s:Envelope>"};
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2015-07-08 13:44:54 +02:00
|
|
|
char const* soap_delete_response[] = {
|
2009-06-29 10:09:32 +02:00
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">"
|
2015-07-08 13:44:54 +02:00
|
|
|
"</u:DeletePortMapping></s:Body></s:Envelope>",
|
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body><u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:2\">"
|
|
|
|
"</u:DeletePortMapping></s:Body></s:Envelope>"};
|
2009-06-29 10:09:32 +02:00
|
|
|
|
|
|
|
void incoming_msearch(udp::endpoint const& from, char* buffer
|
|
|
|
, int size)
|
2007-04-02 07:14:20 +02:00
|
|
|
{
|
2009-06-29 10:09:32 +02:00
|
|
|
http_parser p;
|
|
|
|
bool error = false;
|
2016-07-24 09:52:20 +02:00
|
|
|
p.incoming(span<char const>(buffer, size), error);
|
2009-06-29 10:09:32 +02:00
|
|
|
if (error || !p.header_finished())
|
2008-10-28 23:00:34 +01:00
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "*** malformed HTTP from "
|
2009-12-15 14:11:07 +01:00
|
|
|
<< print_endpoint(from) << std::endl;
|
2008-10-28 23:00:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-06-29 10:09:32 +02:00
|
|
|
|
|
|
|
if (p.method() != "m-search") return;
|
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "< incoming m-search from " << from << std::endl;
|
2009-06-29 10:09:32 +02:00
|
|
|
|
|
|
|
char msg[] = "HTTP/1.1 200 OK\r\n"
|
|
|
|
"ST:upnp:rootdevice\r\n"
|
|
|
|
"USN:uuid:000f-66d6-7296000099dc::upnp:rootdevice\r\n"
|
2010-01-24 00:57:11 +01:00
|
|
|
"Location: http://127.0.0.1:%d/upnp.xml\r\n"
|
2009-06-29 10:09:32 +02:00
|
|
|
"Server: Custom/1.0 UPnP/1.0 Proc/Ver\r\n"
|
|
|
|
"EXT:\r\n"
|
|
|
|
"Cache-Control:max-age=180\r\n"
|
|
|
|
"DATE: Fri, 02 Jan 1970 08:10:38 GMT\r\n\r\n";
|
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
TORRENT_ASSERT(g_port != 0);
|
|
|
|
char buf[sizeof(msg) + 30];
|
2016-05-17 15:24:06 +02:00
|
|
|
int len = std::snprintf(buf, sizeof(buf), msg, g_port);
|
2010-01-24 00:57:11 +01:00
|
|
|
|
2009-06-29 10:09:32 +02:00
|
|
|
error_code ec;
|
2010-01-24 00:57:11 +01:00
|
|
|
sock->send(buf, len, ec);
|
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
if (ec) std::cout << "*** error sending " << ec.message() << std::endl;
|
2009-06-29 10:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct callback_info
|
|
|
|
{
|
|
|
|
int mapping;
|
|
|
|
int port;
|
|
|
|
error_code ec;
|
|
|
|
bool operator==(callback_info const& e)
|
|
|
|
{ return mapping == e.mapping && port == e.port && !ec == !e.ec; }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::list<callback_info> callbacks;
|
|
|
|
|
2016-09-16 15:53:17 +02:00
|
|
|
namespace
|
2009-06-29 10:09:32 +02:00
|
|
|
{
|
2016-09-16 15:53:17 +02:00
|
|
|
struct upnp_callback : aux::portmap_callback
|
|
|
|
{
|
|
|
|
void on_port_mapping(int mapping, address const& ip, int port
|
2016-09-18 16:11:56 +02:00
|
|
|
, portmap_protocol const protocol, error_code const& err
|
2016-09-16 15:53:17 +02:00
|
|
|
, aux::portmap_transport transport) override
|
|
|
|
{
|
|
|
|
callback_info info = {mapping, port, err};
|
|
|
|
callbacks.push_back(info);
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "mapping: " << mapping << ", port: " << port << ", IP: " << ip
|
2016-09-18 16:11:56 +02:00
|
|
|
<< ", proto: " << static_cast<int>(protocol)
|
|
|
|
<< ", error: \"" << err.message() << "\"\n";
|
2016-09-16 15:53:17 +02:00
|
|
|
}
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
virtual bool should_log_portmap(aux::portmap_transport transport) const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void log_portmap(aux::portmap_transport transport, char const* msg) const override
|
|
|
|
{
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "UPnP: " << msg << std::endl;
|
2016-09-16 15:53:17 +02:00
|
|
|
//TODO: store the log and verify that some key messages are there
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
2007-04-02 07:14:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-08 13:44:54 +02:00
|
|
|
void run_upnp_test(char const* root_filename, char const* router_model, char const* control_name, int igd_version)
|
2007-04-02 07:14:20 +02:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
libtorrent::io_service ios;
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
g_port = start_web_server();
|
2012-06-28 08:47:59 +02:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
error_code ec;
|
|
|
|
load_file(root_filename, buf, ec);
|
|
|
|
buf.push_back(0);
|
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
FILE* xml_file = fopen("upnp.xml", "w+");
|
2016-07-09 22:26:26 +02:00
|
|
|
if (xml_file == nullptr)
|
2013-07-23 09:32:38 +02:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("failed to open file 'upnp.xml': %s\n", strerror(errno));
|
2013-07-23 09:32:38 +02:00
|
|
|
TEST_CHECK(false);
|
2015-05-31 14:21:16 +02:00
|
|
|
return;
|
2013-07-23 09:32:38 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(xml_file, &buf[0], g_port);
|
2010-01-24 00:57:11 +01:00
|
|
|
fclose(xml_file);
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2012-06-28 08:47:59 +02:00
|
|
|
std::ofstream xml(control_name, std::ios::trunc);
|
2015-07-08 13:44:54 +02:00
|
|
|
xml.write(soap_add_response[igd_version-1], sizeof(soap_add_response[igd_version-1])-1);
|
2009-06-29 10:09:32 +02:00
|
|
|
xml.close();
|
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250")
|
|
|
|
, 1900));
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
sock->open(&incoming_msearch, ios, ec);
|
2012-10-09 06:16:37 +02:00
|
|
|
|
2009-06-29 10:09:32 +02:00
|
|
|
std::string user_agent = "test agent";
|
2007-05-05 22:00:28 +02:00
|
|
|
|
2016-09-16 15:53:17 +02:00
|
|
|
upnp_callback cb;
|
|
|
|
auto upnp_handler = std::make_shared<upnp>(ios, user_agent, cb, false);
|
2015-01-06 09:08:49 +01:00
|
|
|
upnp_handler->start();
|
2008-01-03 20:03:52 +01:00
|
|
|
upnp_handler->discover_device();
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
for (int i = 0; i < 20; ++i)
|
|
|
|
{
|
|
|
|
ios.reset();
|
|
|
|
ios.poll(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("io_service::run(): %s\n", ec.message().c_str());
|
2013-09-03 06:41:49 +02:00
|
|
|
ec.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (upnp_handler->router_model() != "") break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2013-09-03 06:41:49 +02:00
|
|
|
}
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2017-01-02 16:16:33 +01:00
|
|
|
std::cout << "router: " << upnp_handler->router_model() << std::endl;
|
2013-09-03 06:41:49 +02:00
|
|
|
TEST_EQUAL(upnp_handler->router_model(), router_model);
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2016-09-18 16:11:56 +02:00
|
|
|
int mapping1 = upnp_handler->add_mapping(portmap_protocol::tcp, 500, 500);
|
|
|
|
int mapping2 = upnp_handler->add_mapping(portmap_protocol::udp, 501, 501);
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
for (int i = 0; i < 40; ++i)
|
|
|
|
{
|
|
|
|
ios.reset();
|
|
|
|
ios.poll(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("io_service::run(): %s\n", ec.message().c_str());
|
2013-09-03 06:41:49 +02:00
|
|
|
ec.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (callbacks.size() >= 2) break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2013-09-03 06:41:49 +02:00
|
|
|
}
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
callback_info expected1 = {mapping1, 500, error_code()};
|
|
|
|
callback_info expected2 = {mapping2, 501, error_code()};
|
|
|
|
TEST_EQUAL(std::count(callbacks.begin(), callbacks.end(), expected1), 1);
|
|
|
|
TEST_EQUAL(std::count(callbacks.begin(), callbacks.end(), expected2), 1);
|
|
|
|
|
|
|
|
xml.open(control_name, std::ios::trunc);
|
2015-07-08 13:44:54 +02:00
|
|
|
xml.write(soap_delete_response[igd_version-1], sizeof(soap_delete_response[igd_version-1])-1);
|
2009-06-29 10:09:32 +02:00
|
|
|
xml.close();
|
|
|
|
|
2007-11-28 04:09:58 +01:00
|
|
|
upnp_handler->close();
|
2009-06-29 10:09:32 +02:00
|
|
|
sock->close();
|
2007-04-02 07:14:20 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
for (int i = 0; i < 40; ++i)
|
|
|
|
{
|
|
|
|
ios.reset();
|
|
|
|
ios.poll(ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf("io_service::run(): %s\n", ec.message().c_str());
|
2013-09-03 06:41:49 +02:00
|
|
|
ec.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (callbacks.size() >= 4) break;
|
2016-06-26 15:24:06 +02:00
|
|
|
std::this_thread::sleep_for(lt::milliseconds(100));
|
2013-09-03 06:41:49 +02:00
|
|
|
}
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
// there should have been two DeleteMapping calls
|
|
|
|
TEST_EQUAL(callbacks.size(), 4);
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
stop_web_server();
|
2009-06-29 10:09:32 +02:00
|
|
|
|
2012-06-28 08:47:59 +02:00
|
|
|
callbacks.clear();
|
|
|
|
|
2009-06-29 10:09:32 +02:00
|
|
|
delete sock;
|
2007-04-02 07:14:20 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(upnp)
|
2012-06-28 08:47:59 +02:00
|
|
|
{
|
2015-07-08 13:44:54 +02:00
|
|
|
run_upnp_test(combine_path("..", "root1.xml").c_str(), "Xtreme N GIGABIT Router", "wipconn", 1);
|
|
|
|
run_upnp_test(combine_path("..", "root2.xml").c_str(), "D-Link Router", "WANIPConnection", 1);
|
|
|
|
run_upnp_test(combine_path("..", "root3.xml").c_str(), "D-Link Router", "WANIPConnection_2", 2);
|
2012-06-28 08:47:59 +02:00
|
|
|
}
|