forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_upnp.cpp
277 lines (230 loc) · 8.4 KB
/
test_upnp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
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.
*/
#include "libtorrent/upnp.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/socket_io.hpp" // print_endpoint
#include "libtorrent/http_parser.hpp"
#include "test.hpp"
#include "setup_transfer.hpp"
#include "libtorrent/aux_/path.hpp"
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
using namespace lt;
using lt::portmap_protocol;
broadcast_socket* sock = nullptr;
int g_port = 0;
char const* soap_add_response[] = {
"<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\">"
"</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>"};
char const* soap_delete_response[] = {
"<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\">"
"</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>"};
void incoming_msearch(udp::endpoint const& from, char* buffer
, int size)
{
http_parser p;
bool error = false;
p.incoming(span<char const>(buffer, size), error);
if (error || !p.header_finished())
{
std::cout << "*** malformed HTTP from "
<< print_endpoint(from) << std::endl;
return;
}
if (p.method() != "m-search") return;
std::cout << "< incoming m-search from " << from << std::endl;
char msg[] = "HTTP/1.1 200 OK\r\n"
"ST:upnp:rootdevice\r\n"
"USN:uuid:000f-66d6-7296000099dc::upnp:rootdevice\r\n"
"Location: http://127.0.0.1:%d/upnp.xml\r\n"
"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";
TORRENT_ASSERT(g_port != 0);
char buf[sizeof(msg) + 30];
int len = std::snprintf(buf, sizeof(buf), msg, g_port);
error_code ec;
sock->send(buf, len, ec);
if (ec) std::cout << "*** error sending " << ec.message() << std::endl;
}
struct callback_info
{
port_mapping_t 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;
namespace
{
struct upnp_callback : aux::portmap_callback
{
void on_port_mapping(port_mapping_t const mapping
, address const& ip, int port
, portmap_protocol const protocol, error_code const& err
, portmap_transport const transport) override
{
callback_info info = {mapping, port, err};
callbacks.push_back(info);
std::cout << "mapping: " << static_cast<int>(mapping)
<< ", port: " << port << ", IP: " << ip
<< ", proto: " << static_cast<int>(protocol)
<< ", error: \"" << err.message() << "\"\n";
}
#ifndef TORRENT_DISABLE_LOGGING
virtual bool should_log_portmap(portmap_transport transport) const override
{
return true;
}
virtual void log_portmap(portmap_transport transport, char const* msg) const override
{
std::cout << "UPnP: " << msg << std::endl;
//TODO: store the log and verify that some key messages are there
}
#endif
};
}
void run_upnp_test(char const* root_filename, char const* router_model, char const* control_name, int igd_version)
{
lt::io_service ios;
g_port = start_web_server();
std::vector<char> buf;
error_code ec;
load_file(root_filename, buf, ec);
buf.push_back(0);
FILE* xml_file = fopen("upnp.xml", "w+");
if (xml_file == nullptr)
{
std::printf("failed to open file 'upnp.xml': %s\n", strerror(errno));
TEST_CHECK(false);
return;
}
std::fprintf(xml_file, &buf[0], g_port);
fclose(xml_file);
std::ofstream xml(control_name, std::ios::trunc);
xml.write(soap_add_response[igd_version-1], sizeof(soap_add_response[igd_version-1])-1);
xml.close();
sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250")
, 1900));
sock->open(&incoming_msearch, ios, ec);
std::string user_agent = "test agent";
upnp_callback cb;
auto upnp_handler = std::make_shared<upnp>(ios, user_agent, cb, false);
upnp_handler->start();
upnp_handler->discover_device();
for (int i = 0; i < 20; ++i)
{
ios.reset();
ios.poll(ec);
if (ec)
{
std::printf("io_service::run(): %s\n", ec.message().c_str());
ec.clear();
break;
}
if (upnp_handler->router_model() != "") break;
std::this_thread::sleep_for(lt::milliseconds(100));
}
std::cout << "router: " << upnp_handler->router_model() << std::endl;
TEST_EQUAL(upnp_handler->router_model(), router_model);
auto const mapping1 = upnp_handler->add_mapping(portmap_protocol::tcp, 500, ep("127.0.0.1", 500));
auto const mapping2 = upnp_handler->add_mapping(portmap_protocol::udp, 501, ep("127.0.0.1", 501));
for (int i = 0; i < 40; ++i)
{
ios.reset();
ios.poll(ec);
if (ec)
{
std::printf("io_service::run(): %s\n", ec.message().c_str());
ec.clear();
break;
}
if (callbacks.size() >= 2) break;
std::this_thread::sleep_for(lt::milliseconds(100));
}
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);
xml.write(soap_delete_response[igd_version-1], sizeof(soap_delete_response[igd_version-1])-1);
xml.close();
upnp_handler->close();
sock->close();
for (int i = 0; i < 40; ++i)
{
ios.reset();
ios.poll(ec);
if (ec)
{
std::printf("io_service::run(): %s\n", ec.message().c_str());
ec.clear();
break;
}
if (callbacks.size() >= 4) break;
std::this_thread::sleep_for(lt::milliseconds(100));
}
// there should have been two DeleteMapping calls
TEST_EQUAL(callbacks.size(), 4);
stop_web_server();
callbacks.clear();
delete sock;
}
TORRENT_TEST(upnp)
{
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);
}
TORRENT_TEST(upnp_max_mappings)
{
lt::io_service ios;
upnp_callback cb;
auto upnp_handler = std::make_shared<upnp>(ios, "test agent", cb, false);
for (int i = 0; i < 50; ++i)
{
auto const mapping = upnp_handler->add_mapping(portmap_protocol::tcp
, 500 + i, ep("127.0.0.1", 500 + i));
TEST_CHECK(mapping != port_mapping_t{-1});
}
}