Mercurial > pidgin
annotate libgaim/upnp.c @ 15334:fa49e24d24f3
[gaim-migrate @ 18126]
It's safer to use the utf8 version of strlen.
committer: Tailor Script <tailor@pidgin.im>
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Mon, 15 Jan 2007 01:10:55 +0000 |
parents | 96abd13cc0b3 |
children |
rev | line source |
---|---|
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", | |
14837 | 218 "parse_description_response(): Failed In HTTP_OK\n"); |
14192 | 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", | |
14837 | 225 "parse_description_response(): Failed finding root\n"); |
14192 | 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", | |
14837 | 233 "parse_description_response(): Could not parse xml root node\n"); |
14192 | 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", | |
14837 | 255 "parse_description_response(): could not get serviceTypeNode 1\n"); |
14192 | 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", | |
14837 | 263 "parse_description_response(): could not get serviceTypeNode 2\n"); |
14192 | 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", | |
14837 | 278 "parse_description_response(): could not get serviceTypeNode 3\n"); |
14192 | 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", | |
14837 | 286 "parse_description_response(): could not get serviceTypeNode 4\n"); |
14192 | 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", | |
14837 | 300 "parse_description_response(): could not get serviceTypeNode 5\n"); |
14192 | 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", | |
14837 | 308 "parse_description_response(): could not get serviceTypeNode 6\n"); |
14192 | 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", | |
14837 | 325 "parse_description_response(): could not get serviceTypeNode 7\n"); |
14192 | 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", | |
14837 | 335 "parse_description_response(): Could not find controlURL\n"); |
14192 | 336 g_free(baseURL); |
337 xmlnode_free(xmlRootNode); | |
338 return NULL; | |
339 } | |
340 | |
341 tmp = xmlnode_get_data(controlURLNode); | |
15228
192c66c0c2cb
[gaim-migrate @ 18018]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14837
diff
changeset
|
342 if(baseURL && !gaim_str_has_prefix(tmp, "http://") && |
14192 | 343 !gaim_str_has_prefix(tmp, "HTTP://")) { |
15286 | 344 /* Handle absolute paths in a relative URL. This probably |
345 * belongs in util.c. */ | |
346 if (tmp[0] == '/') { | |
347 size_t length; | |
348 const char *path, *start = strstr(baseURL, "://"); | |
349 start = start ? start + 3 : baseURL; | |
350 path = strchr(start, '/'); | |
351 length = path ? path - baseURL : strlen(baseURL); | |
352 controlURL = g_strdup_printf("%.*s%s", length, baseURL, tmp); | |
353 } else { | |
354 controlURL = g_strdup_printf("%s%s", baseURL, tmp); | |
355 } | |
14192 | 356 g_free(tmp); |
357 }else{ | |
358 controlURL = tmp; | |
359 } | |
360 g_free(baseURL); | |
361 xmlnode_free(xmlRootNode); | |
362 | |
363 return controlURL; | |
364 } | |
365 | |
366 static void | |
14354 | 367 upnp_parse_description_cb(GaimUtilFetchUrlData *url_data, gpointer user_data, |
368 const gchar *httpResponse, gsize len, const gchar *error_message) | |
14192 | 369 { |
14354 | 370 UPnPDiscoveryData *dd = user_data; |
14192 | 371 gchar *control_url = NULL; |
372 | |
373 if (len > 0) | |
374 control_url = gaim_upnp_parse_description_response( | |
375 httpResponse, len, dd->full_url, dd->service_type); | |
376 | |
377 g_free(dd->full_url); | |
378 | |
379 if(control_url == NULL) { | |
380 gaim_debug_error("upnp", | |
14837 | 381 "gaim_upnp_parse_description(): control URL is NULL\n"); |
14192 | 382 } |
383 | |
384 control_info.status = control_url ? GAIM_UPNP_STATUS_DISCOVERED | |
385 : GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER; | |
386 control_info.lookup_time = time(NULL); | |
387 control_info.control_url = control_url; | |
388 strncpy(control_info.service_type, dd->service_type, | |
389 sizeof(control_info.service_type)); | |
390 | |
391 fire_discovery_callbacks(control_url != NULL); | |
392 | |
393 /* Look up the public and internal IPs */ | |
394 if(control_url != NULL) { | |
395 lookup_public_ip(); | |
396 lookup_internal_ip(); | |
397 } | |
398 | |
399 g_free(dd); | |
400 } | |
401 | |
402 static void | |
403 gaim_upnp_parse_description(const gchar* descriptionURL, UPnPDiscoveryData *dd) | |
404 { | |
405 gchar* httpRequest; | |
406 gchar* descriptionXMLAddress; | |
407 gchar* descriptionAddress; | |
408 int port = 0; | |
409 | |
410 /* parse the 4 above variables out of the descriptionURL | |
411 example description URL: http://192.168.1.1:5678/rootDesc.xml */ | |
412 | |
413 /* parse the url into address, port, path variables */ | |
414 if(!gaim_url_parse(descriptionURL, &descriptionAddress, | |
415 &port, &descriptionXMLAddress, NULL, NULL)) { | |
416 return; | |
417 } | |
418 if(port == 0 || port == -1) { | |
419 port = DEFAULT_HTTP_PORT; | |
420 } | |
421 | |
422 /* for example... | |
423 GET /rootDesc.xml HTTP/1.1\r\nHost: 192.168.1.1:5678\r\n\r\n */ | |
424 httpRequest = g_strdup_printf( | |
425 "GET /%s HTTP/1.1\r\n" | |
426 "Connection: close\r\n" | |
427 "Host: %s:%d\r\n\r\n", | |
428 descriptionXMLAddress, descriptionAddress, port); | |
429 | |
430 g_free(descriptionXMLAddress); | |
431 | |
432 dd->full_url = g_strdup_printf("http://%s:%d", | |
433 descriptionAddress, port); | |
434 g_free(descriptionAddress); | |
435 | |
436 /* Remove the timeout because everything it is waiting for has | |
437 * successfully completed */ | |
438 gaim_timeout_remove(dd->tima); | |
439 dd->tima = 0; | |
440 | |
14354 | 441 gaim_util_fetch_url_request(descriptionURL, TRUE, NULL, TRUE, httpRequest, |
14192 | 442 TRUE, upnp_parse_description_cb, dd); |
443 | |
444 g_free(httpRequest); | |
445 | |
446 } | |
447 | |
448 static void | |
449 gaim_upnp_parse_discover_response(const gchar* buf, unsigned int buf_len, | |
450 UPnPDiscoveryData *dd) | |
451 { | |
452 gchar* startDescURL; | |
453 gchar* endDescURL; | |
454 gchar* descURL; | |
455 | |
456 if(g_strstr_len(buf, buf_len, HTTP_OK) == NULL) { | |
457 gaim_debug_error("upnp", | |
14837 | 458 "parse_discover_response(): Failed In HTTP_OK\n"); |
14192 | 459 return; |
460 } | |
461 | |
462 if((startDescURL = g_strstr_len(buf, buf_len, "http://")) == NULL) { | |
463 gaim_debug_error("upnp", | |
14837 | 464 "parse_discover_response(): Failed In finding http://\n"); |
14192 | 465 return; |
466 } | |
467 | |
468 endDescURL = g_strstr_len(startDescURL, buf_len - (startDescURL - buf), | |
469 "\r"); | |
470 if(endDescURL == NULL) { | |
471 endDescURL = g_strstr_len(startDescURL, | |
472 buf_len - (startDescURL - buf), "\n"); | |
473 if(endDescURL == NULL) { | |
474 gaim_debug_error("upnp", | |
14837 | 475 "parse_discover_response(): Failed In endDescURL\n"); |
14192 | 476 return; |
477 } | |
478 } | |
479 | |
480 /* XXX: I'm not sure how this could ever happen */ | |
481 if(endDescURL == startDescURL) { | |
482 gaim_debug_error("upnp", | |
14837 | 483 "parse_discover_response(): endDescURL == startDescURL\n"); |
14192 | 484 return; |
485 } | |
486 | |
487 descURL = g_strndup(startDescURL, endDescURL - startDescURL); | |
488 | |
489 gaim_upnp_parse_description(descURL, dd); | |
490 | |
491 g_free(descURL); | |
492 | |
493 } | |
494 | |
495 static gboolean | |
496 gaim_upnp_discover_timeout(gpointer data) | |
497 { | |
498 UPnPDiscoveryData* dd = data; | |
499 | |
500 if (dd->inpa) | |
501 gaim_input_remove(dd->inpa); | |
502 dd->inpa = 0; | |
503 dd->tima = 0; | |
504 | |
505 if (dd->retry_count < NUM_UDP_ATTEMPTS) { | |
14837 | 506 /* TODO: We probably shouldn't be incrementing retry_count in two places */ |
14192 | 507 dd->retry_count++; |
508 gaim_upnp_discover_send_broadcast(dd); | |
509 } else { | |
510 if (dd->fd) | |
511 close(dd->fd); | |
512 | |
513 control_info.status = GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER; | |
514 control_info.lookup_time = time(NULL); | |
515 control_info.service_type[0] = '\0'; | |
516 g_free(control_info.control_url); | |
517 control_info.control_url = NULL; | |
518 | |
519 fire_discovery_callbacks(FALSE); | |
520 | |
521 g_free(dd); | |
522 } | |
523 | |
524 return FALSE; | |
525 } | |
526 | |
527 static void | |
528 gaim_upnp_discover_udp_read(gpointer data, gint sock, GaimInputCondition cond) | |
529 { | |
530 int len; | |
531 UPnPDiscoveryData *dd = data; | |
532 gchar buf[65536]; | |
533 | |
534 do { | |
535 len = recv(dd->fd, buf, | |
536 sizeof(buf) - 1, 0); | |
537 | |
538 if(len > 0) { | |
539 buf[len] = '\0'; | |
540 break; | |
541 } else if(errno != EINTR) { | |
542 /* We'll either get called again, or time out */ | |
543 return; | |
544 } | |
545 } while (errno == EINTR); | |
546 | |
547 gaim_input_remove(dd->inpa); | |
548 dd->inpa = 0; | |
549 | |
550 close(dd->fd); | |
551 dd->fd = 0; | |
552 | |
553 /* parse the response, and see if it was a success */ | |
554 gaim_upnp_parse_discover_response(buf, len, dd); | |
555 | |
556 /* We'll either time out or continue successfully */ | |
557 } | |
558 | |
14354 | 559 static void |
14192 | 560 gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd) |
561 { | |
562 gchar *sendMessage = NULL; | |
563 gsize totalSize; | |
564 gboolean sentSuccess; | |
565 | |
566 /* because we are sending over UDP, if there is a failure | |
567 we should retry the send NUM_UDP_ATTEMPTS times. Also, | |
568 try different requests for WANIPConnection and WANPPPConnection*/ | |
569 for(; dd->retry_count < NUM_UDP_ATTEMPTS; dd->retry_count++) { | |
570 sentSuccess = FALSE; | |
571 | |
572 if((dd->retry_count % 2) == 0) { | |
573 strncpy(dd->service_type, WAN_IP_CONN_SERVICE, sizeof(dd->service_type)); | |
574 } else { | |
575 strncpy(dd->service_type, WAN_PPP_CONN_SERVICE, sizeof(dd->service_type)); | |
576 } | |
577 | |
578 sendMessage = g_strdup_printf(SEARCH_REQUEST_STRING, dd->service_type); | |
579 | |
580 totalSize = strlen(sendMessage); | |
581 | |
582 do { | |
583 if(sendto(dd->fd, sendMessage, totalSize, 0, | |
584 (struct sockaddr*) &(dd->server), | |
585 sizeof(struct sockaddr_in) | |
586 ) == totalSize) { | |
587 sentSuccess = TRUE; | |
588 break; | |
589 } | |
590 } while (errno == EINTR || errno == EAGAIN); | |
591 | |
592 g_free(sendMessage); | |
593 | |
594 if(sentSuccess) { | |
595 dd->tima = gaim_timeout_add(DISCOVERY_TIMEOUT, | |
596 gaim_upnp_discover_timeout, dd); | |
597 dd->inpa = gaim_input_add(dd->fd, GAIM_INPUT_READ, | |
598 gaim_upnp_discover_udp_read, dd); | |
599 | |
600 return; | |
601 } | |
602 } | |
603 | |
604 /* We have already done all our retries. Make sure that the callback | |
605 * doesn't get called before the original function returns */ | |
606 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd); | |
607 } | |
608 | |
609 void | |
610 gaim_upnp_discover(GaimUPnPCallback cb, gpointer cb_data) | |
611 { | |
612 /* Socket Setup Variables */ | |
613 int sock; | |
614 struct hostent* hp; | |
615 | |
616 /* UDP RECEIVE VARIABLES */ | |
617 UPnPDiscoveryData *dd; | |
618 | |
619 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERING) { | |
620 if (cb) { | |
621 discovery_callbacks = g_slist_append( | |
622 discovery_callbacks, cb); | |
623 discovery_callbacks = g_slist_append( | |
624 discovery_callbacks, cb_data); | |
625 } | |
626 return; | |
627 } | |
628 | |
629 dd = g_new0(UPnPDiscoveryData, 1); | |
630 if (cb) { | |
631 discovery_callbacks = g_slist_append(discovery_callbacks, cb); | |
632 discovery_callbacks = g_slist_append(discovery_callbacks, | |
633 cb_data); | |
634 } | |
635 | |
636 /* Set up the sockets */ | |
637 sock = socket(AF_INET, SOCK_DGRAM, 0); | |
638 if(sock == -1) { | |
639 gaim_debug_error("upnp", | |
14837 | 640 "gaim_upnp_discover(): Failed In sock creation\n"); |
14192 | 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 dd->fd = sock; | |
648 | |
14837 | 649 /* TODO: Non-blocking! */ |
14192 | 650 if((hp = gethostbyname(HTTPMU_HOST_ADDRESS)) == NULL) { |
651 gaim_debug_error("upnp", | |
14837 | 652 "gaim_upnp_discover(): Failed In gethostbyname\n"); |
14192 | 653 /* Short circuit the retry attempts */ |
654 dd->retry_count = NUM_UDP_ATTEMPTS; | |
655 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd); | |
656 return; | |
657 } | |
658 | |
659 memset(&(dd->server), 0, sizeof(struct sockaddr)); | |
660 dd->server.sin_family = AF_INET; | |
661 memcpy(&(dd->server.sin_addr), hp->h_addr_list[0], hp->h_length); | |
662 dd->server.sin_port = htons(HTTPMU_HOST_PORT); | |
663 | |
664 control_info.status = GAIM_UPNP_STATUS_DISCOVERING; | |
665 | |
666 gaim_upnp_discover_send_broadcast(dd); | |
667 } | |
668 | |
669 static void | |
670 gaim_upnp_generate_action_message_and_send(const gchar* actionName, | |
14354 | 671 const gchar* actionParams, GaimUtilFetchUrlCallback cb, |
14192 | 672 gpointer cb_data) |
673 { | |
674 | |
675 gchar* soapMessage; | |
676 gchar* totalSendMessage; | |
677 gchar* pathOfControl; | |
678 gchar* addressOfControl; | |
679 int port = 0; | |
680 | |
681 /* parse the url into address, port, path variables */ | |
682 if(!gaim_url_parse(control_info.control_url, &addressOfControl, | |
683 &port, &pathOfControl, NULL, NULL)) { | |
684 gaim_debug_error("upnp", | |
14837 | 685 "generate_action_message_and_send(): Failed In Parse URL\n"); |
14192 | 686 /* XXX: This should probably be async */ |
687 if(cb) | |
14354 | 688 cb(NULL, cb_data, NULL, 0, NULL); |
14192 | 689 } |
690 if(port == 0 || port == -1) { | |
691 port = DEFAULT_HTTP_PORT; | |
692 } | |
693 | |
694 /* set the soap message */ | |
695 soapMessage = g_strdup_printf(SOAP_ACTION, actionName, | |
696 control_info.service_type, actionParams, actionName); | |
697 | |
698 /* set the HTTP Header, and append the body to it */ | |
699 totalSendMessage = g_strdup_printf(HTTP_HEADER_ACTION "%s", | |
700 pathOfControl, addressOfControl, port, | |
701 control_info.service_type, actionName, | |
702 strlen(soapMessage), soapMessage); | |
703 g_free(pathOfControl); | |
704 g_free(soapMessage); | |
705 | |
14354 | 706 gaim_util_fetch_url_request(control_info.control_url, FALSE, NULL, TRUE, |
14192 | 707 totalSendMessage, TRUE, cb, cb_data); |
708 | |
709 g_free(totalSendMessage); | |
710 g_free(addressOfControl); | |
711 } | |
712 | |
713 const gchar * | |
714 gaim_upnp_get_public_ip() | |
715 { | |
716 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED | |
717 && control_info.publicip | |
718 && strlen(control_info.publicip) > 0) | |
719 return control_info.publicip; | |
720 | |
721 /* Trigger another UPnP discovery if 5 minutes have elapsed since the | |
722 * last one, and it wasn't successful */ | |
723 if (control_info.status < GAIM_UPNP_STATUS_DISCOVERING | |
724 && (time(NULL) - control_info.lookup_time) > 300) | |
725 gaim_upnp_discover(NULL, NULL); | |
726 | |
727 return NULL; | |
728 } | |
729 | |
730 static void | |
14354 | 731 looked_up_public_ip_cb(GaimUtilFetchUrlData *url_data, gpointer user_data, |
732 const gchar *httpResponse, gsize len, const gchar *error_message) | |
14192 | 733 { |
734 gchar* temp, *temp2; | |
735 | |
14354 | 736 if ((error_message != NULL) || (httpResponse == NULL)) |
14192 | 737 return; |
738 | |
739 /* extract the ip, or see if there is an error */ | |
740 if((temp = g_strstr_len(httpResponse, len, | |
741 "<NewExternalIPAddress")) == NULL) { | |
742 gaim_debug_error("upnp", | |
14837 | 743 "looked_up_public_ip_cb(): Failed Finding <NewExternalIPAddress\n"); |
14192 | 744 return; |
745 } | |
746 if(!(temp = g_strstr_len(temp, len - (temp - httpResponse), ">"))) { | |
747 gaim_debug_error("upnp", | |
14837 | 748 "looked_up_public_ip_cb(): Failed In Finding >\n"); |
14192 | 749 return; |
750 } | |
751 if(!(temp2 = g_strstr_len(temp, len - (temp - httpResponse), "<"))) { | |
752 gaim_debug_error("upnp", | |
14837 | 753 "looked_up_public_ip_cb(): Failed In Finding <\n"); |
14192 | 754 return; |
755 } | |
756 *temp2 = '\0'; | |
757 | |
758 strncpy(control_info.publicip, temp + 1, | |
759 sizeof(control_info.publicip)); | |
760 | |
761 gaim_debug_info("upnp", "NAT Returned IP: %s\n", control_info.publicip); | |
762 } | |
763 | |
14354 | 764 static void |
14192 | 765 lookup_public_ip() |
766 { | |
767 gaim_upnp_generate_action_message_and_send("GetExternalIPAddress", "", | |
768 looked_up_public_ip_cb, NULL); | |
769 } | |
770 | |
771 /* TODO: This could be exported */ | |
772 static const gchar * | |
773 gaim_upnp_get_internal_ip() | |
774 { | |
775 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED | |
776 && control_info.internalip | |
777 && strlen(control_info.internalip) > 0) | |
778 return control_info.internalip; | |
779 | |
780 /* Trigger another UPnP discovery if 5 minutes have elapsed since the | |
781 * last one, and it wasn't successful */ | |
782 if (control_info.status < GAIM_UPNP_STATUS_DISCOVERING | |
783 && (time(NULL) - control_info.lookup_time) > 300) | |
784 gaim_upnp_discover(NULL, NULL); | |
785 | |
786 return NULL; | |
787 } | |
788 | |
789 static void | |
790 looked_up_internal_ip_cb(gpointer data, gint source, const gchar *error_message) | |
791 { | |
792 if (source) { | |
793 strncpy(control_info.internalip, | |
794 gaim_network_get_local_system_ip(source), | |
795 sizeof(control_info.internalip)); | |
796 gaim_debug_info("upnp", "Local IP: %s\n", | |
797 control_info.internalip); | |
798 close(source); | |
799 } else | |
800 gaim_debug_info("upnp", "Unable to look up local IP\n"); | |
801 | |
802 } | |
803 | |
14354 | 804 static void |
14192 | 805 lookup_internal_ip() |
806 { | |
807 gchar* addressOfControl; | |
808 int port = 0; | |
809 | |
810 if(!gaim_url_parse(control_info.control_url, &addressOfControl, &port, | |
811 NULL, NULL, NULL)) { | |
812 gaim_debug_error("upnp", | |
14837 | 813 "lookup_internal_ip(): Failed In Parse URL\n"); |
14192 | 814 return; |
815 } | |
816 if(port == 0 || port == -1) { | |
817 port = DEFAULT_HTTP_PORT; | |
818 } | |
819 | |
14837 | 820 if(gaim_proxy_connect(NULL, NULL, addressOfControl, port, |
14192 | 821 looked_up_internal_ip_cb, NULL) == NULL) |
822 { | |
823 gaim_debug_error("upnp", "Get Local IP Connect Failed: Address: %s @@@ Port %d\n", | |
824 addressOfControl, port); | |
825 } | |
826 | |
827 g_free(addressOfControl); | |
828 } | |
829 | |
830 static void | |
14354 | 831 done_port_mapping_cb(GaimUtilFetchUrlData *url_data, gpointer user_data, |
832 const gchar *httpResponse, gsize len, const gchar *error_message) | |
14192 | 833 { |
14354 | 834 UPnPMappingAddRemove *ar = user_data; |
14192 | 835 |
836 gboolean success = TRUE; | |
837 | |
838 /* determine if port mapping was a success */ | |
14354 | 839 if ((error_message != NULL) || (httpResponse == NULL) || |
840 (g_strstr_len(httpResponse, len, HTTP_OK) == NULL)) | |
841 { | |
14192 | 842 gaim_debug_error("upnp", |
14837 | 843 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n%s\n", |
14192 | 844 httpResponse ? httpResponse : "(null)"); |
845 success = FALSE; | |
846 } else | |
847 gaim_debug_info("upnp", "Successfully completed port mapping operation\n"); | |
848 | |
849 if (ar->cb) | |
850 ar->cb(success, ar->cb_data); | |
851 g_free(ar); | |
852 } | |
853 | |
854 static void | |
855 do_port_mapping_cb(gboolean has_control_mapping, gpointer data) | |
856 { | |
857 UPnPMappingAddRemove *ar = data; | |
858 | |
859 if (has_control_mapping) { | |
860 gchar action_name[25]; | |
861 gchar *action_params; | |
862 if(ar->add) { | |
863 const gchar *internal_ip; | |
864 /* get the internal IP */ | |
865 if(!(internal_ip = gaim_upnp_get_internal_ip())) { | |
866 gaim_debug_error("upnp", | |
14837 | 867 "gaim_upnp_set_port_mapping(): couldn't get local ip\n"); |
14192 | 868 /* UGLY */ |
869 if (ar->cb) | |
870 ar->cb(FALSE, ar->cb_data); | |
871 g_free(ar); | |
872 return; | |
873 } | |
874 strncpy(action_name, "AddPortMapping", | |
875 sizeof(action_name)); | |
876 action_params = g_strdup_printf( | |
877 ADD_PORT_MAPPING_PARAMS, | |
878 ar->portmap, ar->protocol, ar->portmap, | |
879 internal_ip); | |
880 } else { | |
881 strncpy(action_name, "DeletePortMapping", sizeof(action_name)); | |
882 action_params = g_strdup_printf( | |
883 DELETE_PORT_MAPPING_PARAMS, | |
884 ar->portmap, ar->protocol); | |
885 } | |
886 | |
887 gaim_upnp_generate_action_message_and_send(action_name, | |
888 action_params, done_port_mapping_cb, ar); | |
889 | |
890 g_free(action_params); | |
891 return; | |
892 } | |
893 | |
894 | |
895 if (ar->cb) | |
896 ar->cb(FALSE, ar->cb_data); | |
897 g_free(ar); | |
898 } | |
899 | |
900 static gboolean | |
901 fire_port_mapping_failure_cb(gpointer data) | |
902 { | |
903 do_port_mapping_cb(FALSE, data); | |
904 return FALSE; | |
905 } | |
906 | |
907 void | |
908 gaim_upnp_set_port_mapping(unsigned short portmap, const gchar* protocol, | |
909 GaimUPnPCallback cb, gpointer cb_data) | |
910 { | |
911 UPnPMappingAddRemove *ar; | |
912 | |
913 ar = g_new0(UPnPMappingAddRemove, 1); | |
914 ar->cb = cb; | |
915 ar->cb_data = cb_data; | |
916 ar->add = TRUE; | |
917 ar->portmap = portmap; | |
918 strncpy(ar->protocol, protocol, sizeof(ar->protocol)); | |
919 | |
920 /* If we're waiting for a discovery, add to the callbacks list */ | |
921 if(control_info.status == GAIM_UPNP_STATUS_DISCOVERING) { | |
922 /* TODO: This will fail because when this cb is triggered, | |
923 * the internal IP lookup won't be complete */ | |
924 discovery_callbacks = g_slist_append( | |
925 discovery_callbacks, do_port_mapping_cb); | |
926 discovery_callbacks = g_slist_append( | |
927 discovery_callbacks, ar); | |
928 return; | |
929 } | |
930 | |
931 /* If we haven't had a successful UPnP discovery, check if 5 minutes has | |
932 * elapsed since the last try, try again */ | |
933 if(control_info.status == GAIM_UPNP_STATUS_UNDISCOVERED || | |
934 (control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER | |
935 && (time(NULL) - control_info.lookup_time) > 300)) { | |
936 gaim_upnp_discover(do_port_mapping_cb, ar); | |
937 return; | |
938 } else if(control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER) { | |
939 if (cb) { | |
940 /* Asynchronously trigger a failed response */ | |
941 gaim_timeout_add(10, fire_port_mapping_failure_cb, ar); | |
942 } else { | |
943 /* No need to do anything if nobody expects a response*/ | |
944 g_free(ar); | |
945 } | |
946 return; | |
947 } | |
948 | |
949 do_port_mapping_cb(TRUE, ar); | |
950 } | |
951 | |
952 void | |
953 gaim_upnp_remove_port_mapping(unsigned short portmap, const char* protocol, | |
954 GaimUPnPCallback cb, gpointer cb_data) | |
955 { | |
956 UPnPMappingAddRemove *ar; | |
957 | |
958 ar = g_new0(UPnPMappingAddRemove, 1); | |
959 ar->cb = cb; | |
960 ar->cb_data = cb_data; | |
961 ar->add = FALSE; | |
962 ar->portmap = portmap; | |
963 strncpy(ar->protocol, protocol, sizeof(ar->protocol)); | |
964 | |
965 /* If we're waiting for a discovery, add to the callbacks list */ | |
966 if(control_info.status == GAIM_UPNP_STATUS_DISCOVERING) { | |
967 discovery_callbacks = g_slist_append( | |
968 discovery_callbacks, do_port_mapping_cb); | |
969 discovery_callbacks = g_slist_append( | |
970 discovery_callbacks, ar); | |
971 return; | |
972 } | |
973 | |
974 /* If we haven't had a successful UPnP discovery, check if 5 minutes has | |
975 * elapsed since the last try, try again */ | |
976 if(control_info.status == GAIM_UPNP_STATUS_UNDISCOVERED || | |
977 (control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER | |
978 && (time(NULL) - control_info.lookup_time) > 300)) { | |
979 gaim_upnp_discover(do_port_mapping_cb, ar); | |
980 return; | |
981 } else if(control_info.status == GAIM_UPNP_STATUS_UNABLE_TO_DISCOVER) { | |
982 if (cb) { | |
983 /* Asynchronously trigger a failed response */ | |
984 gaim_timeout_add(10, fire_port_mapping_failure_cb, ar); | |
985 } else { | |
986 /* No need to do anything if nobody expects a response*/ | |
987 g_free(ar); | |
988 } | |
989 return; | |
990 } | |
991 | |
992 do_port_mapping_cb(TRUE, ar); | |
993 } |