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