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