14192
|
1 /**
|
|
2 * @file upnp.c UPnP Implementation
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Gaim is the legal property of its developers, whose names are too numerous
|
|
8 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
9 * source distribution.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 */
|
|
25 #include "internal.h"
|
|
26
|
|
27 #include "debug.h"
|
|
28 #include "util.h"
|
|
29 #include "proxy.h"
|
|
30 #include "xmlnode.h"
|
|
31 #include "network.h"
|
|
32 #include "eventloop.h"
|
|
33 #include "upnp.h"
|
|
34
|
|
35
|
|
36 /***************************************************************
|
|
37 ** General Defines *
|
|
38 ****************************************************************/
|
|
39 #define HTTP_OK "200 OK"
|
|
40 #define DEFAULT_HTTP_PORT 80
|
|
41 #define DISCOVERY_TIMEOUT 1000
|
|
42
|
|
43 /***************************************************************
|
|
44 ** Discovery/Description Defines *
|
|
45 ****************************************************************/
|
|
46 #define NUM_UDP_ATTEMPTS 2
|
|
47
|
|
48 /* Address and port of an SSDP request used for discovery */
|
|
49 #define HTTPMU_HOST_ADDRESS "239.255.255.250"
|
|
50 #define HTTPMU_HOST_PORT 1900
|
|
51
|
|
52 #define SEARCH_REQUEST_DEVICE "urn:schemas-upnp-org:service:%s"
|
|
53
|
|
54 #define SEARCH_REQUEST_STRING \
|
|
55 "M-SEARCH * HTTP/1.1\r\n" \
|
|
56 "MX: 2\r\n" \
|
|
57 "HOST: 239.255.255.250:1900\r\n" \
|
|
58 "MAN: \"ssdp:discover\"\r\n" \
|
|
59 "ST: urn:schemas-upnp-org:service:%s\r\n" \
|
|
60 "\r\n"
|
|
61
|
|
62 #define WAN_IP_CONN_SERVICE "WANIPConnection:1"
|
|
63 #define WAN_PPP_CONN_SERVICE "WANPPPConnection:1"
|
|
64
|
|
65 /******************************************************************
|
|
66 ** Action Defines *
|
|
67 *******************************************************************/
|
|
68 #define HTTP_HEADER_ACTION \
|
|
69 "POST /%s HTTP/1.1\r\n" \
|
|
70 "HOST: %s:%d\r\n" \
|
|
71 "SOAPACTION: \"urn:schemas-upnp-org:service:%s#%s\"\r\n" \
|
|
72 "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n" \
|
|
73 "CONTENT-LENGTH: %" G_GSIZE_FORMAT "\r\n\r\n"
|
|
74
|
|
75 #define SOAP_ACTION \
|
|
76 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
|
|
77 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " \
|
|
78 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
|
|
79 "<s:Body>\r\n" \
|
|
80 "<u:%s xmlns:u=\"urn:schemas-upnp-org:service:%s\">\r\n" \
|
|
81 "%s" \
|
|
82 "</u:%s>\r\n" \
|
|
83 "</s:Body>\r\n" \
|
|
84 "</s:Envelope>"
|
|
85
|
|
86 #define PORT_MAPPING_LEASE_TIME "0"
|
|
87 #define PORT_MAPPING_DESCRIPTION "GAIM_UPNP_PORT_FORWARD"
|
|
88
|
|
89 #define ADD_PORT_MAPPING_PARAMS \
|
|
90 "<NewRemoteHost></NewRemoteHost>\r\n" \
|
|
91 "<NewExternalPort>%i</NewExternalPort>\r\n" \
|
|
92 "<NewProtocol>%s</NewProtocol>\r\n" \
|
|
93 "<NewInternalPort>%i</NewInternalPort>\r\n" \
|
|
94 "<NewInternalClient>%s</NewInternalClient>\r\n" \
|
|
95 "<NewEnabled>1</NewEnabled>\r\n" \
|
|
96 "<NewPortMappingDescription>" \
|
|
97 PORT_MAPPING_DESCRIPTION \
|
|
98 "</NewPortMappingDescription>\r\n" \
|
|
99 "<NewLeaseDuration>" \
|
|
100 PORT_MAPPING_LEASE_TIME \
|
|
101 "</NewLeaseDuration>\r\n"
|
|
102
|
|
103 #define DELETE_PORT_MAPPING_PARAMS \
|
|
104 "<NewRemoteHost></NewRemoteHost>\r\n" \
|
|
105 "<NewExternalPort>%i</NewExternalPort>\r\n" \
|
|
106 "<NewProtocol>%s</NewProtocol>\r\n"
|
|
107
|
|
108 typedef enum {
|
|
109 GAIM_UPNP_STATUS_UNDISCOVERED = -1,
|
|
110 GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER,
|
|
111 GAIM_UPNP_STATUS_DISCOVERING,
|
|
112 GAIM_UPNP_STATUS_DISCOVERED
|
|
113 } GaimUPnPStatus;
|
|
114
|
|
115 typedef struct {
|
|
116 GaimUPnPStatus status;
|
|
117 gchar* control_url;
|
|
118 gchar service_type[20];
|
|
119 char publicip[16];
|
|
120 char internalip[16];
|
|
121 time_t lookup_time;
|
|
122 } GaimUPnPControlInfo;
|
|
123
|
|
124 typedef struct {
|
|
125 guint inpa; /* gaim_input_add handle */
|
|
126 guint tima; /* gaim_timeout_add handle */
|
|
127 int fd;
|
|
128 struct sockaddr_in server;
|
|
129 gchar service_type[25];
|
|
130 int retry_count;
|
|
131 gchar *full_url;
|
|
132 } UPnPDiscoveryData;
|
|
133
|
|
134 typedef struct {
|
|
135 unsigned short portmap;
|
|
136 gchar protocol[4];
|
|
137 gboolean add;
|
|
138 GaimUPnPCallback cb;
|
|
139 gpointer cb_data;
|
|
140 } UPnPMappingAddRemove;
|
|
141
|
|
142 static GaimUPnPControlInfo control_info = {
|
|
143 GAIM_UPNP_STATUS_UNDISCOVERED,
|
|
144 NULL, "\0", "\0", "\0", 0};
|
|
145
|
|
146 static GSList *discovery_callbacks = NULL;
|
|
147
|
|
148 static void gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd);
|
|
149 static void lookup_public_ip(void);
|
|
150 static void lookup_internal_ip(void);
|
|
151
|
|
152 static void
|
|
153 fire_discovery_callbacks(gboolean success)
|
|
154 {
|
|
155 while(discovery_callbacks) {
|
|
156 gpointer data;
|
|
157 GaimUPnPCallback cb = discovery_callbacks->data;
|
|
158 discovery_callbacks = g_slist_remove(discovery_callbacks, cb);
|
|
159 data = discovery_callbacks->data;
|
|
160 discovery_callbacks = g_slist_remove(discovery_callbacks, data);
|
|
161 cb(success, data);
|
|
162 }
|
|
163 }
|
|
164
|
|
165 static gboolean
|
|
166 gaim_upnp_compare_device(const xmlnode* device, const gchar* deviceType)
|
|
167 {
|
|
168 xmlnode* deviceTypeNode = xmlnode_get_child(device, "deviceType");
|
|
169 char *tmp;
|
|
170 gboolean ret;
|
|
171
|
|
172 if(deviceTypeNode == NULL) {
|
|
173 return FALSE;
|
|
174 }
|
|
175
|
|
176 tmp = xmlnode_get_data(deviceTypeNode);
|
|
177 ret = !g_ascii_strcasecmp(tmp, deviceType);
|
|
178 g_free(tmp);
|
|
179
|
|
180 return ret;
|
|
181 }
|
|
182
|
|
183 static gboolean
|
|
184 gaim_upnp_compare_service(const xmlnode* service, const gchar* serviceType)
|
|
185 {
|
|
186 xmlnode* serviceTypeNode;
|
|
187 char *tmp;
|
|
188 gboolean ret;
|
|
189
|
|
190 if(service == NULL) {
|
|
191 return FALSE;
|
|
192 }
|
|
193
|
|
194 serviceTypeNode = xmlnode_get_child(service, "serviceType");
|
|
195
|
|
196 if(serviceTypeNode == NULL) {
|
|
197 return FALSE;
|
|
198 }
|
|
199
|
|
200 tmp = xmlnode_get_data(serviceTypeNode);
|
|
201 ret = !g_ascii_strcasecmp(tmp, serviceType);
|
|
202 g_free(tmp);
|
|
203
|
|
204 return ret;
|
|
205 }
|
|
206
|
|
207 static gchar*
|
|
208 gaim_upnp_parse_description_response(const gchar* httpResponse, gsize len,
|
|
209 const gchar* httpURL, const gchar* serviceType)
|
|
210 {
|
|
211 gchar *xmlRoot, *baseURL, *controlURL, *service;
|
|
212 xmlnode *xmlRootNode, *serviceTypeNode, *controlURLNode, *baseURLNode;
|
|
213 char *tmp;
|
|
214
|
|
215 /* make sure we have a valid http response */
|
|
216 if(g_strstr_len(httpResponse, len, HTTP_OK) == NULL) {
|
|
217 gaim_debug_error("upnp",
|
|
218 "parse_description_response(): Failed In HTTP_OK\n\n");
|
|
219 return NULL;
|
|
220 }
|
|
221
|
|
222 /* find the root of the xml document */
|
|
223 if((xmlRoot = g_strstr_len(httpResponse, len, "<root")) == NULL) {
|
|
224 gaim_debug_error("upnp",
|
|
225 "parse_description_response(): Failed finding root\n\n");
|
|
226 return NULL;
|
|
227 }
|
|
228
|
|
229 /* create the xml root node */
|
|
230 if((xmlRootNode = xmlnode_from_str(xmlRoot,
|
|
231 len - (xmlRoot - httpResponse))) == NULL) {
|
|
232 gaim_debug_error("upnp",
|
|
233 "parse_description_response(): Could not parse xml root node\n\n");
|
|
234 return NULL;
|
|
235 }
|
|
236
|
|
237 /* get the baseURL of the device */
|
|
238 if((baseURLNode = xmlnode_get_child(xmlRootNode, "URLBase")) != NULL) {
|
|
239 baseURL = xmlnode_get_data(baseURLNode);
|
|
240 } else {
|
|
241 baseURL = g_strdup(httpURL);
|
|
242 }
|
|
243
|
|
244 /* get the serviceType child that has the service type as its data */
|
|
245
|
|
246 /* get urn:schemas-upnp-org:device:InternetGatewayDevice:1 and its devicelist */
|
|
247 serviceTypeNode = xmlnode_get_child(xmlRootNode, "device");
|
|
248 while(!gaim_upnp_compare_device(serviceTypeNode,
|
|
249 "urn:schemas-upnp-org:device:InternetGatewayDevice:1") &&
|
|
250 serviceTypeNode != NULL) {
|
|
251 serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
|
|
252 }
|
|
253 if(serviceTypeNode == NULL) {
|
|
254 gaim_debug_error("upnp",
|
|
255 "parse_description_response(): could not get serviceTypeNode 1\n\n");
|
|
256 g_free(baseURL);
|
|
257 xmlnode_free(xmlRootNode);
|
|
258 return NULL;
|
|
259 }
|
|
260 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "deviceList");
|
|
261 if(serviceTypeNode == NULL) {
|
|
262 gaim_debug_error("upnp",
|
|
263 "parse_description_response(): could not get serviceTypeNode 2\n\n");
|
|
264 g_free(baseURL);
|
|
265 xmlnode_free(xmlRootNode);
|
|
266 return NULL;
|
|
267 }
|
|
268
|
|
269 /* get urn:schemas-upnp-org:device:WANDevice:1 and its devicelist */
|
|
270 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "device");
|
|
271 while(!gaim_upnp_compare_device(serviceTypeNode,
|
|
272 "urn:schemas-upnp-org:device:WANDevice:1") &&
|
|
273 serviceTypeNode != NULL) {
|
|
274 serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
|
|
275 }
|
|
276 if(serviceTypeNode == NULL) {
|
|
277 gaim_debug_error("upnp",
|
|
278 "parse_description_response(): could not get serviceTypeNode 3\n\n");
|
|
279 g_free(baseURL);
|
|
280 xmlnode_free(xmlRootNode);
|
|
281 return NULL;
|
|
282 }
|
|
283 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "deviceList");
|
|
284 if(serviceTypeNode == NULL) {
|
|
285 gaim_debug_error("upnp",
|
|
286 "parse_description_response(): could not get serviceTypeNode 4\n\n");
|
|
287 g_free(baseURL);
|
|
288 xmlnode_free(xmlRootNode);
|
|
289 return NULL;
|
|
290 }
|
|
291
|
|
292 /* get urn:schemas-upnp-org:device:WANConnectionDevice:1 and its servicelist */
|
|
293 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "device");
|
|
294 while(serviceTypeNode && !gaim_upnp_compare_device(serviceTypeNode,
|
|
295 "urn:schemas-upnp-org:device:WANConnectionDevice:1")) {
|
|
296 serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
|
|
297 }
|
|
298 if(serviceTypeNode == NULL) {
|
|
299 gaim_debug_error("upnp",
|
|
300 "parse_description_response(): could not get serviceTypeNode 5\n\n");
|
|
301 g_free(baseURL);
|
|
302 xmlnode_free(xmlRootNode);
|
|
303 return NULL;
|
|
304 }
|
|
305 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "serviceList");
|
|
306 if(serviceTypeNode == NULL) {
|
|
307 gaim_debug_error("upnp",
|
|
308 "parse_description_response(): could not get serviceTypeNode 6\n\n");
|
|
309 g_free(baseURL);
|
|
310 xmlnode_free(xmlRootNode);
|
|
311 return NULL;
|
|
312 }
|
|
313
|
|
314 /* get the serviceType variable passed to this function */
|
|
315 service = g_strdup_printf(SEARCH_REQUEST_DEVICE, serviceType);
|
|
316 serviceTypeNode = xmlnode_get_child(serviceTypeNode, "service");
|
|
317 while(!gaim_upnp_compare_service(serviceTypeNode, service) &&
|
|
318 serviceTypeNode != NULL) {
|
|
319 serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
|
|
320 }
|
|
321
|
|
322 g_free(service);
|
|
323 if(serviceTypeNode == NULL) {
|
|
324 gaim_debug_error("upnp",
|
|
325 "parse_description_response(): could not get serviceTypeNode 7\n\n");
|
|
326 g_free(baseURL);
|
|
327 xmlnode_free(xmlRootNode);
|
|
328 return NULL;
|
|
329 }
|
|
330
|
|
331 /* get the controlURL of the service */
|
|
332 if((controlURLNode = xmlnode_get_child(serviceTypeNode,
|
|
333 "controlURL")) == NULL) {
|
|
334 gaim_debug_error("upnp",
|
|
335 "parse_description_response(): Could not find controlURL\n\n");
|
|
336 g_free(baseURL);
|
|
337 xmlnode_free(xmlRootNode);
|
|
338 return NULL;
|
|
339 }
|
|
340
|
|
341 tmp = xmlnode_get_data(controlURLNode);
|
|
342 if(!gaim_str_has_prefix(tmp, "http://") &&
|
|
343 !gaim_str_has_prefix(tmp, "HTTP://")) {
|
|
344 controlURL = g_strdup_printf("%s%s", baseURL, tmp);
|
|
345 g_free(tmp);
|
|
346 }else{
|
|
347 controlURL = tmp;
|
|
348 }
|
|
349 g_free(baseURL);
|
|
350 xmlnode_free(xmlRootNode);
|
|
351
|
|
352 return controlURL;
|
|
353 }
|
|
354
|
|
355 static void
|
14354
|
356 upnp_parse_description_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
|
|
357 const gchar *httpResponse, gsize len, const gchar *error_message)
|
14192
|
358 {
|
14354
|
359 UPnPDiscoveryData *dd = user_data;
|
14192
|
360 gchar *control_url = NULL;
|
|
361
|
|
362 if (len > 0)
|
|
363 control_url = gaim_upnp_parse_description_response(
|
|
364 httpResponse, len, dd->full_url, dd->service_type);
|
|
365
|
|
366 g_free(dd->full_url);
|
|
367
|
|
368 if(control_url == NULL) {
|
|
369 gaim_debug_error("upnp",
|
|
370 "gaim_upnp_parse_description(): control URL is NULL\n\n");
|
|
371 }
|
|
372
|
|
373 control_info.status = control_url ? GAIM_UPNP_STATUS_DISCOVERED
|
|
374 : GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER;
|
|
375 control_info.lookup_time = time(NULL);
|
|
376 control_info.control_url = control_url;
|
|
377 strncpy(control_info.service_type, dd->service_type,
|
|
378 sizeof(control_info.service_type));
|
|
379
|
|
380 fire_discovery_callbacks(control_url != NULL);
|
|
381
|
|
382 /* Look up the public and internal IPs */
|
|
383 if(control_url != NULL) {
|
|
384 lookup_public_ip();
|
|
385 lookup_internal_ip();
|
|
386 }
|
|
387
|
|
388 g_free(dd);
|
|
389 }
|
|
390
|
|
391 static void
|
|
392 gaim_upnp_parse_description(const gchar* descriptionURL, UPnPDiscoveryData *dd)
|
|
393 {
|
|
394 gchar* httpRequest;
|
|
395 gchar* descriptionXMLAddress;
|
|
396 gchar* descriptionAddress;
|
|
397 int port = 0;
|
|
398
|
|
399 /* parse the 4 above variables out of the descriptionURL
|
|
400 example description URL: http://192.168.1.1:5678/rootDesc.xml */
|
|
401
|
|
402 /* parse the url into address, port, path variables */
|
|
403 if(!gaim_url_parse(descriptionURL, &descriptionAddress,
|
|
404 &port, &descriptionXMLAddress, NULL, NULL)) {
|
|
405 return;
|
|
406 }
|
|
407 if(port == 0 || port == -1) {
|
|
408 port = DEFAULT_HTTP_PORT;
|
|
409 }
|
|
410
|
|
411 /* for example...
|
|
412 GET /rootDesc.xml HTTP/1.1\r\nHost: 192.168.1.1:5678\r\n\r\n */
|
|
413 httpRequest = g_strdup_printf(
|
|
414 "GET /%s HTTP/1.1\r\n"
|
|
415 "Connection: close\r\n"
|
|
416 "Host: %s:%d\r\n\r\n",
|
|
417 descriptionXMLAddress, descriptionAddress, port);
|
|
418
|
|
419 g_free(descriptionXMLAddress);
|
|
420
|
|
421 dd->full_url = g_strdup_printf("http://%s:%d",
|
|
422 descriptionAddress, port);
|
|
423 g_free(descriptionAddress);
|
|
424
|
|
425 /* Remove the timeout because everything it is waiting for has
|
|
426 * successfully completed */
|
|
427 gaim_timeout_remove(dd->tima);
|
|
428 dd->tima = 0;
|
|
429
|
14354
|
430 gaim_util_fetch_url_request(descriptionURL, TRUE, NULL, TRUE, httpRequest,
|
14192
|
431 TRUE, upnp_parse_description_cb, dd);
|
|
432
|
|
433 g_free(httpRequest);
|
|
434
|
|
435 }
|
|
436
|
|
437 static void
|
|
438 gaim_upnp_parse_discover_response(const gchar* buf, unsigned int buf_len,
|
|
439 UPnPDiscoveryData *dd)
|
|
440 {
|
|
441 gchar* startDescURL;
|
|
442 gchar* endDescURL;
|
|
443 gchar* descURL;
|
|
444
|
|
445 if(g_strstr_len(buf, buf_len, HTTP_OK) == NULL) {
|
|
446 gaim_debug_error("upnp",
|
|
447 "parse_discover_response(): Failed In HTTP_OK\n\n");
|
|
448 return;
|
|
449 }
|
|
450
|
|
451 if((startDescURL = g_strstr_len(buf, buf_len, "http://")) == NULL) {
|
|
452 gaim_debug_error("upnp",
|
|
453 "parse_discover_response(): Failed In finding http://\n\n");
|
|
454 return;
|
|
455 }
|
|
456
|
|
457 endDescURL = g_strstr_len(startDescURL, buf_len - (startDescURL - buf),
|
|
458 "\r");
|
|
459 if(endDescURL == NULL) {
|
|
460 endDescURL = g_strstr_len(startDescURL,
|
|
461 buf_len - (startDescURL - buf), "\n");
|
|
462 if(endDescURL == NULL) {
|
|
463 gaim_debug_error("upnp",
|
|
464 "parse_discover_response(): Failed In endDescURL\n\n");
|
|
465 return;
|
|
466 }
|
|
467 }
|
|
468
|
|
469 /* XXX: I'm not sure how this could ever happen */
|
|
470 if(endDescURL == startDescURL) {
|
|
471 gaim_debug_error("upnp",
|
|
472 "parse_discover_response(): endDescURL == startDescURL\n\n");
|
|
473 return;
|
|
474 }
|
|
475
|
|
476 descURL = g_strndup(startDescURL, endDescURL - startDescURL);
|
|
477
|
|
478 gaim_upnp_parse_description(descURL, dd);
|
|
479
|
|
480 g_free(descURL);
|
|
481
|
|
482 }
|
|
483
|
|
484 static gboolean
|
|
485 gaim_upnp_discover_timeout(gpointer data)
|
|
486 {
|
|
487 UPnPDiscoveryData* dd = data;
|
|
488
|
|
489 if (dd->inpa)
|
|
490 gaim_input_remove(dd->inpa);
|
|
491 dd->inpa = 0;
|
|
492 dd->tima = 0;
|
|
493
|
|
494 if (dd->retry_count < NUM_UDP_ATTEMPTS) {
|
|
495 dd->retry_count++;
|
|
496 gaim_upnp_discover_send_broadcast(dd);
|
|
497 } else {
|
|
498 if (dd->fd)
|
|
499 close(dd->fd);
|
|
500
|
|
501 control_info.status = GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER;
|
|
502 control_info.lookup_time = time(NULL);
|
|
503 control_info.service_type[0] = '\0';
|
|
504 g_free(control_info.control_url);
|
|
505 control_info.control_url = NULL;
|
|
506
|
|
507 fire_discovery_callbacks(FALSE);
|
|
508
|
|
509 g_free(dd);
|
|
510 }
|
|
511
|
|
512 return FALSE;
|
|
513 }
|
|
514
|
|
515 static void
|
|
516 gaim_upnp_discover_udp_read(gpointer data, gint sock, GaimInputCondition cond)
|
|
517 {
|
|
518 int len;
|
|
519 UPnPDiscoveryData *dd = data;
|
|
520 gchar buf[65536];
|
|
521
|
|
522 do {
|
|
523 len = recv(dd->fd, buf,
|
|
524 sizeof(buf) - 1, 0);
|
|
525
|
|
526 if(len > 0) {
|
|
527 buf[len] = '\0';
|
|
528 break;
|
|
529 } else if(errno != EINTR) {
|
|
530 /* We'll either get called again, or time out */
|
|
531 return;
|
|
532 }
|
|
533 } while (errno == EINTR);
|
|
534
|
|
535 gaim_input_remove(dd->inpa);
|
|
536 dd->inpa = 0;
|
|
537
|
|
538 close(dd->fd);
|
|
539 dd->fd = 0;
|
|
540
|
|
541 /* parse the response, and see if it was a success */
|
|
542 gaim_upnp_parse_discover_response(buf, len, dd);
|
|
543
|
|
544 /* We'll either time out or continue successfully */
|
|
545 }
|
|
546
|
14354
|
547 static void
|
14192
|
548 gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd)
|
|
549 {
|
|
550 gchar *sendMessage = NULL;
|
|
551 gsize totalSize;
|
|
552 gboolean sentSuccess;
|
|
553
|
|
554 /* because we are sending over UDP, if there is a failure
|
|
555 we should retry the send NUM_UDP_ATTEMPTS times. Also,
|
|
556 try different requests for WANIPConnection and WANPPPConnection*/
|
|
557 for(; dd->retry_count < NUM_UDP_ATTEMPTS; dd->retry_count++) {
|
|
558 sentSuccess = FALSE;
|
|
559
|
|
560 if((dd->retry_count % 2) == 0) {
|
|
561 strncpy(dd->service_type, WAN_IP_CONN_SERVICE, sizeof(dd->service_type));
|
|
562 } else {
|
|
563 strncpy(dd->service_type, WAN_PPP_CONN_SERVICE, sizeof(dd->service_type));
|
|
564 }
|
|
565
|
|
566 sendMessage = g_strdup_printf(SEARCH_REQUEST_STRING, dd->service_type);
|
|
567
|
|
568 totalSize = strlen(sendMessage);
|
|
569
|
|
570 do {
|
|
571 if(sendto(dd->fd, sendMessage, totalSize, 0,
|
|
572 (struct sockaddr*) &(dd->server),
|
|
573 sizeof(struct sockaddr_in)
|
|
574 ) == totalSize) {
|
|
575 sentSuccess = TRUE;
|
|
576 break;
|
|
577 }
|
|
578 } while (errno == EINTR || errno == EAGAIN);
|
|
579
|
|
580 g_free(sendMessage);
|
|
581
|
|
582 if(sentSuccess) {
|
|
583 dd->tima = gaim_timeout_add(DISCOVERY_TIMEOUT,
|
|
584 gaim_upnp_discover_timeout, dd);
|
|
585 dd->inpa = gaim_input_add(dd->fd, GAIM_INPUT_READ,
|
|
586 gaim_upnp_discover_udp_read, dd);
|
|
587
|
|
588 return;
|
|
589 }
|
|
590 }
|
|
591
|
|
592 /* We have already done all our retries. Make sure that the callback
|
|
593 * doesn't get called before the original function returns */
|
|
594 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd);
|
|
595 }
|
|
596
|
|
597 void
|
|
598 gaim_upnp_discover(GaimUPnPCallback cb, gpointer cb_data)
|
|
599 {
|
|
600 /* Socket Setup Variables */
|
|
601 int sock;
|
|
602 struct hostent* hp;
|
|
603
|
|
604 /* UDP RECEIVE VARIABLES */
|
|
605 UPnPDiscoveryData *dd;
|
|
606
|
|
607 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERING) {
|
|
608 if (cb) {
|
|
609 discovery_callbacks = g_slist_append(
|
|
610 discovery_callbacks, cb);
|
|
611 discovery_callbacks = g_slist_append(
|
|
612 discovery_callbacks, cb_data);
|
|
613 }
|
|
614 return;
|
|
615 }
|
|
616
|
|
617 dd = g_new0(UPnPDiscoveryData, 1);
|
|
618 if (cb) {
|
|
619 discovery_callbacks = g_slist_append(discovery_callbacks, cb);
|
|
620 discovery_callbacks = g_slist_append(discovery_callbacks,
|
|
621 cb_data);
|
|
622 }
|
|
623
|
|
624 /* Set up the sockets */
|
|
625 sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
626 if(sock == -1) {
|
|
627 gaim_debug_error("upnp",
|
|
628 "gaim_upnp_discover(): Failed In sock creation\n\n");
|
|
629 /* Short circuit the retry attempts */
|
|
630 dd->retry_count = NUM_UDP_ATTEMPTS;
|
|
631 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd);
|
|
632 return;
|
|
633 }
|
|
634
|
|
635 dd->fd = sock;
|
|
636
|
|
637 /* This shouldn't block */
|
|
638 if((hp = gethostbyname(HTTPMU_HOST_ADDRESS)) == NULL) {
|
|
639 gaim_debug_error("upnp",
|
|
640 "gaim_upnp_discover(): Failed In gethostbyname\n\n");
|
|
641 /* Short circuit the retry attempts */
|
|
642 dd->retry_count = NUM_UDP_ATTEMPTS;
|
|
643 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd);
|
|
644 return;
|
|
645 }
|
|
646
|
|
647 memset(&(dd->server), 0, sizeof(struct sockaddr));
|
|
648 dd->server.sin_family = AF_INET;
|
|
649 memcpy(&(dd->server.sin_addr), hp->h_addr_list[0], hp->h_length);
|
|
650 dd->server.sin_port = htons(HTTPMU_HOST_PORT);
|
|
651
|
|
652 control_info.status = GAIM_UPNP_STATUS_DISCOVERING;
|
|
653
|
|
654 gaim_upnp_discover_send_broadcast(dd);
|
|
655 }
|
|
656
|
|
657 static void
|
|
658 gaim_upnp_generate_action_message_and_send(const gchar* actionName,
|
14354
|
659 const gchar* actionParams, GaimUtilFetchUrlCallback cb,
|
14192
|
660 gpointer cb_data)
|
|
661 {
|
|
662
|
|
663 gchar* soapMessage;
|
|
664 gchar* totalSendMessage;
|
|
665 gchar* pathOfControl;
|
|
666 gchar* addressOfControl;
|
|
667 int port = 0;
|
|
668
|
|
669 /* parse the url into address, port, path variables */
|
|
670 if(!gaim_url_parse(control_info.control_url, &addressOfControl,
|
|
671 &port, &pathOfControl, NULL, NULL)) {
|
|
672 gaim_debug_error("upnp",
|
|
673 "generate_action_message_and_send(): Failed In Parse URL\n\n");
|
|
674 /* XXX: This should probably be async */
|
|
675 if(cb)
|
14354
|
676 cb(NULL, cb_data, NULL, 0, NULL);
|
14192
|
677 }
|
|
678 if(port == 0 || port == -1) {
|
|
679 port = DEFAULT_HTTP_PORT;
|
|
680 }
|
|
681
|
|
682 /* set the soap message */
|
|
683 soapMessage = g_strdup_printf(SOAP_ACTION, actionName,
|
|
684 control_info.service_type, actionParams, actionName);
|
|
685
|
|
686 /* set the HTTP Header, and append the body to it */
|
|
687 totalSendMessage = g_strdup_printf(HTTP_HEADER_ACTION "%s",
|
|
688 pathOfControl, addressOfControl, port,
|
|
689 control_info.service_type, actionName,
|
|
690 strlen(soapMessage), soapMessage);
|
|
691 g_free(pathOfControl);
|
|
692 g_free(soapMessage);
|
|
693
|
14354
|
694 gaim_util_fetch_url_request(control_info.control_url, FALSE, NULL, TRUE,
|
14192
|
695 totalSendMessage, TRUE, cb, cb_data);
|
|
696
|
|
697 g_free(totalSendMessage);
|
|
698 g_free(addressOfControl);
|
|
699 }
|
|
700
|
|
701 const gchar *
|
|
702 gaim_upnp_get_public_ip()
|
|
703 {
|
|
704 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED
|
|
705 && control_info.publicip
|
|
706 && strlen(control_info.publicip) > 0)
|
|
707 return control_info.publicip;
|
|
708
|
|
709 /* Trigger another UPnP discovery if 5 minutes have elapsed since the
|
|
710 * last one, and it wasn't successful */
|
|
711 if (control_info.status < GAIM_UPNP_STATUS_DISCOVERING
|
|
712 && (time(NULL) - control_info.lookup_time) > 300)
|
|
713 gaim_upnp_discover(NULL, NULL);
|
|
714
|
|
715 return NULL;
|
|
716 }
|
|
717
|
|
718 static void
|
14354
|
719 looked_up_public_ip_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
|
|
720 const gchar *httpResponse, gsize len, const gchar *error_message)
|
14192
|
721 {
|
|
722 gchar* temp, *temp2;
|
|
723
|
14354
|
724 if ((error_message != NULL) || (httpResponse == NULL))
|
14192
|
725 return;
|
|
726
|
|
727 /* extract the ip, or see if there is an error */
|
|
728 if((temp = g_strstr_len(httpResponse, len,
|
|
729 "<NewExternalIPAddress")) == NULL) {
|
|
730 gaim_debug_error("upnp",
|
|
731 "looked_up_public_ip_cb(): Failed Finding <NewExternalIPAddress\n\n");
|
|
732 return;
|
|
733 }
|
|
734 if(!(temp = g_strstr_len(temp, len - (temp - httpResponse), ">"))) {
|
|
735 gaim_debug_error("upnp",
|
|
736 "looked_up_public_ip_cb(): Failed In Finding >\n\n");
|
|
737 return;
|
|
738 }
|
|
739 if(!(temp2 = g_strstr_len(temp, len - (temp - httpResponse), "<"))) {
|
|
740 gaim_debug_error("upnp",
|
|
741 "looked_up_public_ip_cb(): Failed In Finding <\n\n");
|
|
742 return;
|
|
743 }
|
|
744 *temp2 = '\0';
|
|
745
|
|
746 strncpy(control_info.publicip, temp + 1,
|
|
747 sizeof(control_info.publicip));
|
|
748
|
|
749 gaim_debug_info("upnp", "NAT Returned IP: %s\n", control_info.publicip);
|
|
750 }
|
|
751
|
14354
|
752 static void
|
14192
|
753 lookup_public_ip()
|
|
754 {
|
|
755 gaim_upnp_generate_action_message_and_send("GetExternalIPAddress", "",
|
|
756 looked_up_public_ip_cb, NULL);
|
|
757 }
|
|
758
|
|
759 /* TODO: This could be exported */
|
|
760 static const gchar *
|
|
761 gaim_upnp_get_internal_ip()
|
|
762 {
|
|
763 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED
|
|
764 && control_info.internalip
|
|
765 && strlen(control_info.internalip) > 0)
|
|
766 return control_info.internalip;
|
|
767
|
|
768 /* Trigger another UPnP discovery if 5 minutes have elapsed since the
|
|
769 * last one, and it wasn't successful */
|
|
770 if (control_info.status < GAIM_UPNP_STATUS_DISCOVERING
|
|
771 && (time(NULL) - control_info.lookup_time) > 300)
|
|
772 gaim_upnp_discover(NULL, NULL);
|
|
773
|
|
774 return NULL;
|
|
775 }
|
|
776
|
|
777 static void
|
|
778 looked_up_internal_ip_cb(gpointer data, gint source, const gchar *error_message)
|
|
779 {
|
|
780 if (source) {
|
|
781 strncpy(control_info.internalip,
|
|
782 gaim_network_get_local_system_ip(source),
|
|
783 sizeof(control_info.internalip));
|
|
784 gaim_debug_info("upnp", "Local IP: %s\n",
|
|
785 control_info.internalip);
|
|
786 close(source);
|
|
787 } else
|
|
788 gaim_debug_info("upnp", "Unable to look up local IP\n");
|
|
789
|
|
790 }
|
|
791
|
14354
|
792 static void
|
14192
|
793 lookup_internal_ip()
|
|
794 {
|
|
795 gchar* addressOfControl;
|
|
796 int port = 0;
|
|
797
|
|
798 if(!gaim_url_parse(control_info.control_url, &addressOfControl, &port,
|
|
799 NULL, NULL, NULL)) {
|
|
800 gaim_debug_error("upnp",
|
|
801 "lookup_internal_ip(): Failed In Parse URL\n\n");
|
|
802 return;
|
|
803 }
|
|
804 if(port == 0 || port == -1) {
|
|
805 port = DEFAULT_HTTP_PORT;
|
|
806 }
|
|
807
|
|
808 if(gaim_proxy_connect(NULL, addressOfControl, port,
|
|
809 looked_up_internal_ip_cb, NULL) == NULL)
|
|
810 {
|
|
811 gaim_debug_error("upnp", "Get Local IP Connect Failed: Address: %s @@@ Port %d\n",
|
|
812 addressOfControl, port);
|
|
813 }
|
|
814
|
|
815 g_free(addressOfControl);
|
|
816 }
|
|
817
|
|
818 static void
|
14354
|
819 done_port_mapping_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
|
|
820 const gchar *httpResponse, gsize len, const gchar *error_message)
|
14192
|
821 {
|
14354
|
822 UPnPMappingAddRemove *ar = user_data;
|
14192
|
823
|
|
824 gboolean success = TRUE;
|
|
825
|
|
826 /* determine if port mapping was a success */
|
14354
|
827 if ((error_message != NULL) || (httpResponse == NULL) ||
|
|
828 (g_strstr_len(httpResponse, len, HTTP_OK) == NULL))
|
|
829 {
|
14192
|
830 gaim_debug_error("upnp",
|
|
831 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n",
|
|
832 httpResponse ? httpResponse : "(null)");
|
|
833 success = FALSE;
|
|
834 } else
|
|
835 gaim_debug_info("upnp", "Successfully completed port mapping operation\n");
|
|
836
|
|
837 if (ar->cb)
|
|
838 ar->cb(success, ar->cb_data);
|
|
839 g_free(ar);
|
|
840 }
|
|
841
|
|
842 static void
|
|
843 do_port_mapping_cb(gboolean has_control_mapping, gpointer data)
|
|
844 {
|
|
845 UPnPMappingAddRemove *ar = data;
|
|
846
|
|
847 if (has_control_mapping) {
|
|
848 gchar action_name[25];
|
|
849 gchar *action_params;
|
|
850 if(ar->add) {
|
|
851 const gchar *internal_ip;
|
|
852 /* get the internal IP */
|
|
853 if(!(internal_ip = gaim_upnp_get_internal_ip())) {
|
|
854 gaim_debug_error("upnp",
|
|
855 "gaim_upnp_set_port_mapping(): couldn't get local ip\n\n");
|
|
856 /* UGLY */
|
|
857 if (ar->cb)
|
|
858 ar->cb(FALSE, ar->cb_data);
|
|
859 g_free(ar);
|
|
860 return;
|
|
861 }
|
|
862 strncpy(action_name, "AddPortMapping",
|
|
863 sizeof(action_name));
|
|
864 action_params = g_strdup_printf(
|
|
865 ADD_PORT_MAPPING_PARAMS,
|
|
866 ar->portmap, ar->protocol, ar->portmap,
|
|
867 internal_ip);
|
|
868 } else {
|
|
869 strncpy(action_name, "DeletePortMapping", sizeof(action_name));
|
|
870 action_params = g_strdup_printf(
|
|
871 DELETE_PORT_MAPPING_PARAMS,
|
|
872 ar->portmap, ar->protocol);
|
|
873 }
|
|
874
|
|
875 gaim_upnp_generate_action_message_and_send(action_name,
|
|
876 action_params, done_port_mapping_cb, ar);
|
|
877
|
|
878 g_free(action_params);
|
|
879 return;
|
|
880 }
|
|
881
|
|
882
|
|
883 if (ar->cb)
|
|
884 ar->cb(FALSE, ar->cb_data);
|
|
885 g_free(ar);
|
|
886 }
|
|
887
|
|
888 static gboolean
|
|
889 fire_port_mapping_failure_cb(gpointer data)
|
|
890 {
|
|
891 do_port_mapping_cb(FALSE, data);
|
|
892 return FALSE;
|
|
893 }
|
|
894
|
|
895 void
|
|
896 gaim_upnp_set_port_mapping(unsigned short portmap, const gchar* protocol,
|
|
897 GaimUPnPCallback cb, gpointer cb_data)
|
|
898 {
|
|
899 UPnPMappingAddRemove *ar;
|
|
900
|
|
901 ar = g_new0(UPnPMappingAddRemove, 1);
|
|
902 ar->cb = cb;
|
|
903 ar->cb_data = cb_data;
|
|
904 ar->add = TRUE;
|
|
905 ar->portmap = portmap;
|
|
906 strncpy(ar->protocol, protocol, sizeof(ar->protocol));
|
|
907
|
|
908 /* If we're waiting for a discovery, add to the callbacks list */
|
|
909 if(control_info.status == GAIM_UPNP_STATUS_DISCOVERING) {
|
|
910 /* TODO: This will fail because when this cb is triggered,
|
|
911 * the internal IP lookup won't be complete */
|
|
912 discovery_callbacks = g_slist_append(
|
|
913 discovery_callbacks, do_port_mapping_cb);
|
|
914 discovery_callbacks = g_slist_append(
|
|
915 discovery_callbacks, ar);
|
|
916 return;
|
|
917 }
|
|
918
|
|
919 /* If we haven't had a successful UPnP discovery, check if 5 minutes has
|
|
920 * elapsed since the last try, try again */
|
|
921 if(control_info.status == GAIM_UPNP_STATUS_UNDISCOVERED ||
|
|
922 (control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER
|
|
923 && (time(NULL) - control_info.lookup_time) > 300)) {
|
|
924 gaim_upnp_discover(do_port_mapping_cb, ar);
|
|
925 return;
|
|
926 } else if(control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER) {
|
|
927 if (cb) {
|
|
928 /* Asynchronously trigger a failed response */
|
|
929 gaim_timeout_add(10, fire_port_mapping_failure_cb, ar);
|
|
930 } else {
|
|
931 /* No need to do anything if nobody expects a response*/
|
|
932 g_free(ar);
|
|
933 }
|
|
934 return;
|
|
935 }
|
|
936
|
|
937 do_port_mapping_cb(TRUE, ar);
|
|
938 }
|
|
939
|
|
940 void
|
|
941 gaim_upnp_remove_port_mapping(unsigned short portmap, const char* protocol,
|
|
942 GaimUPnPCallback cb, gpointer cb_data)
|
|
943 {
|
|
944 UPnPMappingAddRemove *ar;
|
|
945
|
|
946 ar = g_new0(UPnPMappingAddRemove, 1);
|
|
947 ar->cb = cb;
|
|
948 ar->cb_data = cb_data;
|
|
949 ar->add = FALSE;
|
|
950 ar->portmap = portmap;
|
|
951 strncpy(ar->protocol, protocol, sizeof(ar->protocol));
|
|
952
|
|
953 /* If we're waiting for a discovery, add to the callbacks list */
|
|
954 if(control_info.status == GAIM_UPNP_STATUS_DISCOVERING) {
|
|
955 discovery_callbacks = g_slist_append(
|
|
956 discovery_callbacks, do_port_mapping_cb);
|
|
957 discovery_callbacks = g_slist_append(
|
|
958 discovery_callbacks, ar);
|
|
959 return;
|
|
960 }
|
|
961
|
|
962 /* If we haven't had a successful UPnP discovery, check if 5 minutes has
|
|
963 * elapsed since the last try, try again */
|
|
964 if(control_info.status == GAIM_UPNP_STATUS_UNDISCOVERED ||
|
|
965 (control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER
|
|
966 && (time(NULL) - control_info.lookup_time) > 300)) {
|
|
967 gaim_upnp_discover(do_port_mapping_cb, ar);
|
|
968 return;
|
|
969 } else if(control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER) {
|
|
970 if (cb) {
|
|
971 /* Asynchronously trigger a failed response */
|
|
972 gaim_timeout_add(10, fire_port_mapping_failure_cb, ar);
|
|
973 } else {
|
|
974 /* No need to do anything if nobody expects a response*/
|
|
975 g_free(ar);
|
|
976 }
|
|
977 return;
|
|
978 }
|
|
979
|
|
980 do_port_mapping_cb(TRUE, ar);
|
|
981 }
|