11195
|
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 "gtkgaim.h"
|
|
27
|
|
28 #include "debug.h"
|
|
29 #include "network.h"
|
|
30 #include "eventloop.h"
|
|
31 #include "upnp.h"
|
|
32
|
|
33
|
|
34 /**
|
|
35 * Information on the httpResponse callback
|
|
36 */
|
|
37 typedef struct
|
|
38 {
|
|
39 guint inpa; /* gaim_input_add handle */
|
|
40 guint tima; /* gaim_timout_add handle */
|
|
41 char* recvBuffer; /* response data */
|
|
42 guint totalSizeRecv;
|
|
43 gboolean done;
|
|
44
|
|
45 } HRD;
|
|
46
|
|
47
|
|
48 /***************************************************************
|
|
49 ** General Defines *
|
|
50 ****************************************************************/
|
|
51 #define HTTP_OK "200 OK"
|
|
52 #define SIZEOF_HTTP 7 /* size of "http://" */
|
|
53 #define RECIEVE_TIMEOUT 10000
|
|
54 #define CONSECUTIVE_RECIEVE_TIMEOUT 500
|
|
55 #define DISCOVERY_TIMEOUT 1000
|
|
56
|
|
57
|
|
58 /***************************************************************
|
|
59 ** Discovery/Description Defines *
|
|
60 ****************************************************************/
|
|
61 #define NUM_UDP_ATTEMPTS 2
|
|
62
|
|
63 /* Address and port of an SSDP request used for discovery */
|
|
64 #define HTTPMU_HOST_ADDRESS "239.255.255.250"
|
|
65 #define HTTPMU_HOST_PORT 1900
|
|
66
|
|
67 #define SEARCH_REQUEST_DEVICE "urn:schemas-upnp-org:service:" \
|
|
68 "WANIPConnection:1"
|
|
69
|
|
70 #define SEARCH_REQUEST_STRING "M-SEARCH * HTTP/1.1\r\n" \
|
|
71 "MX: 2\r\n" \
|
|
72 "HOST: 239.255.255.250:1900\r\n" \
|
|
73 "MAN: \"ssdp:discover\"\r\n" \
|
|
74 "ST: urn:schemas-upnp-org:service:" \
|
|
75 "WANIPConnection:1\r\n" \
|
|
76 "\r\n"
|
|
77
|
|
78 #define MAX_DISCOVERY_RECIEVE_SIZE 400
|
|
79 #define MAX_DESCRIPTION_RECIEVE_SIZE 7000
|
|
80 #define MAX_DESCRIPTION_HTTP_HEADER_SIZE 100
|
|
81
|
|
82
|
|
83 /******************************************************************
|
|
84 ** Action Defines *
|
|
85 *******************************************************************/
|
|
86 #define HTTP_HEADER_ACTION "POST %s HTTP/1.1\r\n" \
|
|
87 "HOST: %s\r\n" \
|
|
88 "SOAPACTION: " \
|
|
89 "\"urn:schemas-upnp-org:" \
|
|
90 "service:%s#%s\"\r\n" \
|
|
91 "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n"\
|
|
92 "Content-Length: %i\r\n\r\n"
|
|
93
|
|
94 #define SOAP_ACTION "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
|
|
95 "<s:Envelope xmlns:s=" \
|
|
96 "\"http://schemas.xmlsoap.org/soap/envelope/\" " \
|
|
97 "s:encodingStyle=" \
|
|
98 "\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
|
|
99 "<s:Body>\r\n" \
|
|
100 "<u:%s xmlns:u=" \
|
|
101 "\"urn:schemas-upnp-org:service:%s\">\r\n%s" \
|
|
102 "</u:%s>\r\n" \
|
|
103 "</s:Body>\r\n" \
|
|
104 "</s:Envelope>\r\n"
|
|
105
|
|
106 #define PORT_MAPPING_LEASE_TIME "0"
|
|
107 #define PORT_MAPPING_DESCRIPTION "GAIM_UPNP_PORT_FORWARD"
|
|
108
|
|
109 #define ADD_PORT_MAPPING_PARAMS "<NewRemoteHost></NewRemoteHost>\r\n" \
|
|
110 "<NewExternalPort>%i</NewExternalPort>\r\n"\
|
|
111 "<NewProtocol>%s</NewProtocol>\r\n" \
|
|
112 "<NewInternalPort>%i</NewInternalPort>\r\n"\
|
|
113 "<NewInternalClient>%s" \
|
|
114 "</NewInternalClient>\r\n" \
|
|
115 "<NewEnabled>1</NewEnabled>\r\n" \
|
|
116 "<NewPortMappingDescription>" \
|
|
117 PORT_MAPPING_DESCRIPTION \
|
|
118 "</NewPortMappingDescription>\r\n" \
|
|
119 "<NewLeaseDuration>" \
|
|
120 PORT_MAPPING_LEASE_TIME \
|
|
121 "</NewLeaseDuration>\r\n"
|
|
122
|
|
123 #define DELETE_PORT_MAPPING_PARAMS "<NewRemoteHost></NewRemoteHost>\r\n" \
|
|
124 "<NewExternalPort>%i" \
|
|
125 "</NewExternalPort>\r\n" \
|
|
126 "<NewProtocol>%s</NewProtocol>\r\n"
|
|
127
|
|
128
|
|
129
|
|
130 /* validate an http url without a port */
|
|
131 static gboolean
|
|
132 gaim_upnp_validate_url(const char* url)
|
|
133 {
|
|
134 int i1, i2, i3, i4, r;
|
|
135
|
|
136 if(url == NULL) {
|
|
137 gaim_debug_info("upnp",
|
|
138 "gaim_upnp_validate_url(): url == NULL\n\n");
|
|
139 return FALSE;
|
|
140 }
|
|
141 r = sscanf(url, "http://%3i.%3i.%3i.%3i/", &i1, &i2, &i3, &i4);
|
|
142 if(r == 4) {
|
|
143 return TRUE;
|
|
144 }
|
|
145
|
|
146 gaim_debug_info("upnp",
|
|
147 "gaim_upnp_validate_url(): Failed In validate URL\n\n");
|
|
148 return FALSE;
|
|
149 }
|
|
150
|
|
151
|
|
152 static void
|
|
153 gaim_upnp_timeout(gpointer data,
|
11213
|
154 gint source,
|
11195
|
155 GaimInputCondition cond)
|
|
156 {
|
|
157 HRD* hrd = data;
|
|
158
|
|
159 gaim_input_remove(hrd->inpa);
|
|
160 gaim_timeout_remove(hrd->tima);
|
|
161
|
|
162 if(hrd->totalSizeRecv == 0) {
|
|
163 free(hrd->recvBuffer);
|
|
164 hrd->recvBuffer = NULL;
|
|
165 } else {
|
|
166 hrd->recvBuffer[hrd->totalSizeRecv] = '\0';
|
|
167 }
|
|
168
|
|
169 hrd->done = TRUE;
|
|
170 }
|
|
171
|
|
172
|
|
173 static void
|
|
174 gaim_upnp_http_read(gpointer data,
|
|
175 gint sock,
|
|
176 GaimInputCondition cond)
|
|
177 {
|
|
178 int sizeRecv;
|
|
179 extern int errno;
|
|
180 HRD* hrd = data;
|
|
181
|
|
182 sizeRecv = recv(sock, &(hrd->recvBuffer[hrd->totalSizeRecv]),
|
|
183 MAX_DESCRIPTION_RECIEVE_SIZE-hrd->totalSizeRecv, 0);
|
|
184 if(sizeRecv < 0 && errno != EINTR) {
|
|
185 gaim_debug_info("upnp",
|
|
186 "gaim_upnp_http_read(): recv < 0: %i!\n\n", errno);
|
|
187 free(hrd->recvBuffer);
|
|
188 hrd->recvBuffer = NULL;
|
|
189 gaim_timeout_remove(hrd->tima);
|
|
190 gaim_input_remove(hrd->inpa);
|
|
191 hrd->done = TRUE;
|
|
192 return;
|
|
193 }else if(errno == EINTR) {
|
|
194 sizeRecv = 0;
|
|
195 }
|
|
196 hrd->totalSizeRecv += sizeRecv;
|
|
197
|
|
198 if(sizeRecv == 0) {
|
|
199 if(hrd->totalSizeRecv == 0) {
|
|
200 gaim_debug_info("upnp",
|
|
201 "gaim_upnp_http_read(): totalSizeRecv == 0\n\n");
|
|
202 free(hrd->recvBuffer);
|
|
203 hrd->recvBuffer = NULL;
|
|
204 } else {
|
|
205 hrd->recvBuffer[hrd->totalSizeRecv] = '\0';
|
|
206 }
|
|
207 gaim_timeout_remove(hrd->tima);
|
|
208 gaim_input_remove(hrd->inpa);
|
|
209 hrd->done = TRUE;
|
|
210 } else {
|
|
211 gaim_timeout_remove(hrd->tima);
|
|
212 gaim_input_remove(hrd->inpa);
|
|
213 hrd->tima = gaim_timeout_add(CONSECUTIVE_RECIEVE_TIMEOUT,
|
|
214 (GSourceFunc)gaim_upnp_timeout, hrd);
|
|
215 hrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ,
|
|
216 gaim_upnp_http_read, hrd);
|
|
217 }
|
|
218
|
|
219 }
|
|
220
|
|
221
|
|
222
|
|
223 static char*
|
|
224 gaim_upnp_http_request(const char* address,
|
|
225 unsigned short port,
|
|
226 const char* httpRequest)
|
|
227 {
|
|
228 int sock;
|
|
229 int sizeSent, totalSizeSent = 0;
|
|
230 extern int errno;
|
|
231 struct sockaddr_in serv_addr;
|
|
232 struct hostent *server;
|
|
233 char* recvBuffer;
|
|
234
|
|
235 HRD* hrd = (HRD*)malloc(sizeof(HRD));
|
|
236 if(hrd == NULL) {
|
|
237 gaim_debug_info("upnp",
|
|
238 "gaim_upnp_http_request(): Failed in hrd MALLOC\n\n");
|
|
239 return NULL;
|
|
240 }
|
|
241
|
|
242 hrd->recvBuffer = NULL;
|
|
243 hrd->totalSizeRecv = 0;
|
|
244 hrd->done = FALSE;
|
|
245
|
|
246 hrd->recvBuffer = (char*)malloc(MAX_DESCRIPTION_RECIEVE_SIZE);
|
11213
|
247 if(hrd->recvBuffer == NULL) {
|
11195
|
248 gaim_debug_info("upnp",
|
|
249 "gaim_upnp_http_request(): Failed in recvBuffer MALLOC\n\n");
|
|
250 free(hrd);
|
|
251 return NULL;
|
|
252 }
|
|
253
|
|
254 sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
255 if(sock < 0) {
|
|
256 gaim_debug_info("upnp",
|
|
257 "gaim_upnp_http_request(): Failed In sock creation\n\n");
|
|
258 free(hrd->recvBuffer);
|
|
259 free(hrd);
|
|
260 return NULL;
|
|
261 }
|
|
262
|
|
263 server = gethostbyname(address);
|
|
264 if(server == NULL) {
|
|
265 gaim_debug_info("upnp",
|
|
266 "gaim_upnp_http_request(): Failed In gethostbyname\n\n");
|
|
267 free(hrd->recvBuffer);
|
|
268 free(hrd);
|
|
269 close(sock);
|
|
270 return NULL;
|
|
271 }
|
|
272 memset((char*)&serv_addr, 0, sizeof(serv_addr));
|
|
273 serv_addr.sin_family = AF_INET;
|
|
274 memcpy(&serv_addr.sin_addr,
|
|
275 server->h_addr_list[0],
|
|
276 server->h_length);
|
|
277 serv_addr.sin_port = htons(port);
|
|
278
|
|
279 if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) {
|
|
280 gaim_debug_info("upnp",
|
|
281 "gaim_upnp_http_request(): Failed In connect\n\n");
|
|
282 free(hrd->recvBuffer);
|
|
283 free(hrd);
|
|
284 close(sock);
|
|
285 return NULL;
|
|
286 }
|
|
287
|
|
288 while(totalSizeSent < strlen(httpRequest)) {
|
|
289 sizeSent = send(sock,(char*)((int)httpRequest+totalSizeSent),
|
|
290 strlen(httpRequest),0);
|
|
291 if(sizeSent <= 0 && errno != EINTR) {
|
|
292 gaim_debug_info("upnp",
|
|
293 "gaim_upnp_http_request(): Failed In send\n\n");
|
|
294 free(hrd->recvBuffer);
|
|
295 free(hrd);
|
|
296 close(sock);
|
|
297 return NULL;
|
|
298 }else if(errno == EINTR) {
|
|
299 sizeSent = 0;
|
|
300 }
|
|
301 totalSizeSent += sizeSent;
|
|
302 }
|
|
303
|
|
304 hrd->tima = gaim_timeout_add(RECIEVE_TIMEOUT,
|
|
305 (GSourceFunc)gaim_upnp_timeout, hrd);
|
|
306 hrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ,
|
|
307 gaim_upnp_http_read, hrd);
|
|
308 while (!hrd->done) {
|
|
309 gtk_main_iteration();
|
|
310 }
|
|
311 close(sock);
|
|
312
|
|
313 recvBuffer = hrd->recvBuffer;
|
|
314 free(hrd);
|
|
315 return recvBuffer;
|
|
316 }
|
|
317
|
|
318
|
|
319
|
|
320 /* This function takes the HTTP response requesting the description xml from
|
|
321 * the UPnP enabled IGD. It also takes as input the URL the request was sent
|
|
322 * to.
|
|
323 *
|
|
324 * The description contains the URL needed to control the actions of the UPnP
|
|
325 * enabled IGD. This URL can be in the form of just a path, in whcih case we
|
|
326 * have to append the path to the base URL. The base URL might be specified
|
|
327 * in the XML, or it might not, in which case u append to the httpURL.
|
|
328 */
|
|
329
|
|
330 /* at some point, maybe parse the description response using an xml library */
|
|
331 static char*
|
|
332 gaim_upnp_parse_description_response(const char* httpResponse,
|
|
333 const char* httpURL)
|
|
334 {
|
|
335 char* wanIPConnectionStart;
|
|
336 char urlBaseTag[] = "<URLBase>";
|
|
337 char urlBaseEndTag[] = "</URLBase>";
|
|
338 char controlURLTag[] = "<controlURL>";
|
|
339 char controlURLEndTag[] = "</controlURL>";
|
|
340 char* urlBaseTagLoc;
|
|
341 char* urlBaseEndTagLoc;
|
|
342 char* controlURLTagLoc;
|
|
343 char* controlURLEndTagLoc;
|
|
344 int controlURLSize;
|
|
345 int baseURLSize;
|
|
346 char* controlURL;
|
|
347 char* baseURL;
|
|
348
|
|
349 if(strstr(httpResponse, HTTP_OK) == NULL) {
|
|
350 gaim_debug_info("upnp",
|
|
351 "parse_description_response(): Failed In HTTP_OK\n\n");
|
|
352 return NULL;
|
|
353 }
|
|
354
|
|
355 if((wanIPConnectionStart =
|
|
356 strstr(httpResponse, SEARCH_REQUEST_DEVICE)) == NULL) {
|
|
357 gaim_debug_info("upnp",
|
|
358 "parse_description_response(): Failed SEARCH_REQUEST_DEVICE\n\n");
|
|
359 return NULL;
|
|
360 }
|
|
361 if((controlURLTagLoc = strstr(wanIPConnectionStart, controlURLTag))
|
|
362 == NULL) {
|
|
363 gaim_debug_info("upnp",
|
|
364 "parse_description_response(): Failed In controlURLTagLoc\n\n");
|
|
365 return NULL;
|
|
366 }
|
|
367 if((controlURLEndTagLoc =
|
|
368 strstr(wanIPConnectionStart, controlURLEndTag)) == NULL) {
|
|
369 gaim_debug_info("upnp",
|
|
370 "parse_description_response(): Failed In controlURLEndTagLoc\n\n");
|
|
371 return NULL;
|
|
372 }
|
|
373
|
|
374
|
|
375 controlURLSize =
|
|
376 controlURLEndTagLoc-&controlURLTagLoc[strlen(controlURLTag)];
|
|
377 if((controlURL = (char*)malloc(controlURLSize+1)) == NULL) {
|
|
378 gaim_debug_info("upnp",
|
|
379 "parse_description_response(): Failed In MALLOC controlURL\n\n");
|
|
380 return NULL;
|
|
381 }
|
|
382 memcpy(controlURL, &controlURLTagLoc[strlen(controlURLTag)],
|
|
383 controlURLSize);
|
|
384 controlURL[controlURLSize] = '\0';
|
|
385
|
|
386
|
|
387 if((urlBaseTagLoc = strstr(httpResponse, urlBaseTag)) == NULL) {
|
|
388 if((baseURL = (char*)malloc(strlen(httpURL)+controlURLSize+1)) == NULL) {
|
|
389 gaim_debug_info("upnp",
|
|
390 "parse_description_response(): Failed In MALLOC baseURL\n\n");
|
|
391 return NULL;
|
|
392 }
|
|
393 memcpy(baseURL, httpURL, strlen(httpURL));
|
|
394 baseURL[strlen(httpURL)] = '\0';
|
|
395 } else {
|
|
396 if((urlBaseEndTagLoc = strstr(httpResponse, urlBaseEndTag)) == NULL) {
|
|
397 gaim_debug_info("upnp",
|
|
398 "parse_description_response(): Failed In urlBaseEndTagLoc\n\n");
|
|
399 return NULL;
|
|
400 }
|
|
401 baseURLSize =
|
|
402 urlBaseEndTagLoc - &urlBaseTagLoc[strlen(urlBaseTag)];
|
|
403 if((baseURL = (char*)malloc(baseURLSize+controlURLSize+1)) == NULL) {
|
|
404 gaim_debug_info("upnp",
|
|
405 "parse_description_response(): Failed 2nd MALLOC baseURL\n\n");
|
|
406 return NULL;
|
|
407 }
|
|
408 memcpy(baseURL, &urlBaseTagLoc[strlen(urlBaseTag)], baseURLSize);
|
|
409 baseURL[baseURLSize] = '\0';
|
|
410 }
|
|
411
|
|
412 if(strstr(controlURL, "http://") == NULL) {
|
|
413 memcpy(&baseURL[strlen(baseURL)], controlURL, strlen(controlURL)+1);
|
|
414 free(controlURL);
|
|
415 controlURL = baseURL;
|
|
416 }else{
|
|
417 free(baseURL);
|
|
418 }
|
|
419
|
|
420 return controlURL;
|
|
421 }
|
|
422
|
|
423
|
|
424
|
|
425
|
|
426 /* parse a url into it's approrpiate parts:
|
|
427 address, port, path. return true on success,
|
|
428 false on failure */
|
|
429 static gboolean
|
|
430 gaim_upnp_parse_url(const char* url, char** path,
|
|
431 char** address, char** port)
|
|
432 {
|
|
433 char* temp, *temp2;
|
|
434
|
|
435 /* get the path */
|
|
436 if((temp = strchr(&url[SIZEOF_HTTP], '/')) < 0) {
|
|
437 gaim_debug_info("upnp",
|
|
438 "gaim_upnp_parse_url(): Failed In 1\n\n");
|
|
439 return FALSE;
|
|
440 }
|
|
441 if((*path = (char*)malloc(strlen(temp)+1)) == NULL) {
|
|
442 gaim_debug_info("upnp",
|
|
443 "gaim_upnp_parse_url(): Failed In 2\n\n");
|
|
444 return FALSE;
|
|
445 }
|
|
446 memcpy(*path, temp, strlen(temp));
|
|
447 (*path)[strlen(temp)] = '\0';
|
|
448
|
|
449 /* get the port */
|
|
450 if((temp2 = strchr(&url[SIZEOF_HTTP], ':')) == NULL) {
|
|
451 gaim_debug_info("upnp",
|
|
452 "gaim_upnp_parse_url(): Failed In 3\n\n");
|
|
453 free(*path);
|
|
454 return FALSE;
|
|
455 }
|
|
456 if((*port = (char*)malloc(temp-temp2)) == NULL) {
|
|
457 gaim_debug_info("upnp",
|
|
458 "gaim_upnp_parse_url(): Failed In 4\n\n");
|
|
459 free(*path);
|
|
460 return FALSE;
|
|
461 }
|
|
462 memcpy(*port, &temp2[1], (temp-1)-temp2);
|
|
463 (*port)[(temp-1)-temp2] = '\0';
|
|
464
|
|
465 /* get the address */
|
|
466 if((*address = (char*)malloc((temp2-&url[SIZEOF_HTTP])+1)) == NULL) {
|
|
467 gaim_debug_info("upnp",
|
|
468 "gaim_upnp_parse_url(): Failed In 5\n\n");
|
|
469 free(*path);
|
|
470 free(*port);
|
|
471 return FALSE;
|
|
472 }
|
|
473 memcpy(*address, &url[SIZEOF_HTTP],
|
|
474 temp2-&url[SIZEOF_HTTP]);
|
|
475 (*address)[temp2-&url[SIZEOF_HTTP]] = '\0';
|
|
476
|
|
477 return TRUE;
|
|
478 }
|
|
479
|
|
480
|
|
481
|
|
482 /*
|
|
483 * This function takes the URL where the UPnP enabled IGD description is
|
|
484 * located at, and sends an HTTP request to retrieve that description. Once
|
|
485 * the description is retrieved, we send the response to the
|
|
486 * gaim_upnp_parse_description_response() function. This will parse the
|
|
487 * description, and return the control URL needed to control the IGD, which
|
|
488 * this function returns.
|
|
489 */
|
|
490 static char*
|
|
491 gaim_upnp_parse_description(const char* descriptionURL)
|
|
492 {
|
|
493 char* fullURL;
|
|
494 char* controlURL;
|
|
495 char* httpResponse;
|
|
496 char httpRequest[MAX_DESCRIPTION_HTTP_HEADER_SIZE];
|
|
497
|
|
498 char* descriptionXMLAddress;
|
|
499 char* descriptionAddressPort;
|
|
500 char* descriptionAddress;
|
|
501 char* descriptionPort;
|
|
502 unsigned short port;
|
|
503
|
|
504 /* parse the 4 above variables out of the descriptionURL
|
|
505 example description URL: http://192.168.1.1:5678/rootDesc.xml */
|
|
506
|
|
507 /* parse the url into address, port, path variables */
|
|
508 if(!gaim_upnp_parse_url(descriptionURL, &descriptionXMLAddress,
|
|
509 &descriptionAddress, &descriptionPort)) {
|
|
510 return NULL;
|
|
511 }
|
|
512 if((port = atoi(descriptionPort)) == 0) {
|
|
513 gaim_debug_info("upnp",
|
|
514 "gaim_upnp_parse_description(): failed atoi\n\n");
|
|
515 free(descriptionXMLAddress);
|
|
516 free(descriptionAddress);
|
|
517 free(descriptionPort);
|
|
518 return NULL;
|
|
519 }
|
|
520
|
|
521 if((descriptionAddressPort = (char*)malloc(strlen(descriptionAddress) +
|
|
522 strlen(descriptionPort) + 2))
|
|
523 == NULL) {
|
|
524 gaim_debug_info("upnp",
|
|
525 "gaim_upnp_parse_description(): failed MALLOC desAddPort\n\n");
|
|
526 free(descriptionXMLAddress);
|
|
527 free(descriptionAddress);
|
|
528 free(descriptionPort);
|
|
529 return NULL;
|
|
530 }
|
|
531 sprintf(descriptionAddressPort, "%s:%s",
|
|
532 descriptionAddress, descriptionPort);
|
|
533
|
|
534 if((fullURL = (char*)malloc(strlen(descriptionAddressPort) +
|
|
535 SIZEOF_HTTP + 1)) == NULL) {
|
|
536 gaim_debug_info("upnp",
|
|
537 "gaim_upnp_parse_description(): failed MALLOC fullURL\n\n");
|
|
538 free(descriptionXMLAddress);
|
|
539 free(descriptionAddress);
|
|
540 free(descriptionPort);
|
|
541 free(descriptionAddressPort);
|
|
542 }
|
|
543 sprintf(fullURL, "http://%s", descriptionAddressPort);
|
|
544
|
|
545 /* for example...
|
|
546 GET /rootDesc.xml HTTP/1.1\r\nHost: 192.168.1.1:5678\r\n\r\n */
|
|
547 sprintf(httpRequest, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
548 descriptionXMLAddress, descriptionAddressPort);
|
|
549
|
|
550 httpResponse = gaim_upnp_http_request(descriptionAddress,
|
|
551 port, httpRequest);
|
|
552 if(httpResponse == NULL) {
|
|
553 gaim_debug_info("upnp",
|
|
554 "gaim_upnp_parse_description(): httpResponse is NULL\n\n");
|
|
555 free(descriptionXMLAddress);
|
|
556 free(descriptionAddress);
|
|
557 free(descriptionPort);
|
|
558 free(descriptionAddressPort);
|
|
559 free(fullURL);
|
|
560 return NULL;
|
|
561 }
|
|
562
|
|
563 controlURL = gaim_upnp_parse_description_response(httpResponse, fullURL);
|
|
564
|
|
565 free(descriptionXMLAddress);
|
|
566 free(descriptionAddress);
|
|
567 free(descriptionPort);
|
|
568 free(descriptionAddressPort);
|
|
569 free(fullURL);
|
|
570 free(httpResponse);
|
|
571
|
|
572 if(controlURL == NULL) {
|
|
573 gaim_debug_info("upnp",
|
|
574 "gaim_upnp_parse_description(): controlURL is NULL\n\n");
|
|
575 return NULL;
|
|
576 }
|
|
577
|
|
578 return controlURL;
|
|
579 }
|
|
580
|
|
581
|
|
582 static char*
|
|
583 gaim_upnp_parse_discover_response(const char* buf,
|
|
584 unsigned int bufSize)
|
|
585 {
|
|
586 char* startDescURL;
|
|
587 char* endDescURL;
|
|
588 char* descURL;
|
|
589 unsigned int descURLSize;
|
|
590 char* retVal;
|
|
591
|
|
592 if(strstr(buf, HTTP_OK) == NULL) {
|
|
593 gaim_debug_info("upnp",
|
|
594 "parse_discover_response(): Failed In HTTP_OK\n\n");
|
|
595 return NULL;
|
|
596 }
|
|
597
|
|
598 if((startDescURL = strstr(buf, "http://")) == NULL) {
|
|
599 gaim_debug_info("upnp",
|
|
600 "parse_description_response(): Failed In finding http://\n\n");
|
|
601 return NULL;
|
|
602 }
|
|
603
|
|
604 endDescURL = strchr(startDescURL, '\r');
|
|
605 if(endDescURL == NULL) {
|
|
606 endDescURL = strchr(startDescURL, '\n');
|
|
607 if(endDescURL == NULL) {
|
|
608 gaim_debug_info("upnp",
|
|
609 "parse_description_response(): Failed In endDescURL\n\n");
|
|
610 return NULL;
|
|
611 }else if(endDescURL == startDescURL) {
|
|
612 gaim_debug_info("upnp",
|
|
613 "parse_description_response(): endDescURL == startDescURL\n\n");
|
|
614 return NULL;
|
|
615 }
|
|
616 }else if(endDescURL == startDescURL) {
|
|
617 gaim_debug_info("upnp",
|
|
618 "parse_description_response(): 2nd endDescURL == startDescURL\n\n");
|
|
619 return NULL;
|
|
620 }
|
|
621
|
|
622 descURLSize = (endDescURL-startDescURL)+1;
|
|
623 descURL = (char*)malloc(descURLSize);
|
|
624 memcpy(descURL, startDescURL, descURLSize-1);
|
|
625 descURL[descURLSize-1] = '\0';
|
|
626
|
|
627 retVal = gaim_upnp_parse_description(descURL);
|
|
628 free(descURL);
|
|
629 return retVal;
|
|
630 }
|
|
631
|
|
632
|
|
633
|
|
634 static void
|
|
635 gaim_upnp_discover_udp_read(gpointer data,
|
|
636 gint sock,
|
|
637 GaimInputCondition cond)
|
|
638 {
|
|
639 unsigned int length;
|
|
640 extern int errno;
|
|
641 struct sockaddr_in from;
|
|
642 int sizeRecv;
|
|
643 HRD* hrd = data;
|
|
644
|
|
645 gaim_timeout_remove(hrd->tima);
|
|
646 length = sizeof(struct sockaddr_in);
|
|
647
|
|
648 do {
|
|
649 sizeRecv = recvfrom(sock, hrd->recvBuffer,
|
|
650 MAX_DISCOVERY_RECIEVE_SIZE, 0,
|
|
651 (struct sockaddr*)&from, &length);
|
|
652
|
|
653 if(sizeRecv > 0) {
|
|
654 hrd->recvBuffer[sizeRecv] = '\0';
|
|
655 }else if(errno != EINTR) {
|
|
656 free(hrd->recvBuffer);
|
|
657 hrd->recvBuffer = NULL;
|
|
658 }
|
|
659 }while(errno == EINTR);
|
|
660
|
|
661 gaim_input_remove(hrd->inpa);
|
|
662 hrd->done = TRUE;
|
|
663 return;
|
|
664 }
|
|
665
|
|
666
|
11213
|
667
|
11204
|
668 char*
|
11195
|
669 gaim_upnp_discover(void)
|
|
670 {
|
|
671 int sock, i;
|
|
672 int sizeSent, totalSizeSent;
|
|
673 extern int errno;
|
|
674 gboolean sentSuccess, recvSuccess;
|
|
675 struct sockaddr_in server;
|
|
676 struct hostent *hp;
|
|
677 char sendMessage[] = SEARCH_REQUEST_STRING;
|
|
678 char *controlURL = NULL;
|
|
679
|
|
680 HRD* hrd = (HRD*)malloc(sizeof(HRD));
|
11213
|
681 if(hrd == NULL) {
|
|
682 gaim_debug_info("upnp",
|
|
683 "gaim_upnp_discover(): Failed in hrd MALLOC\n\n");
|
|
684 return NULL;
|
|
685 }
|
11195
|
686
|
|
687 sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
688 if (sock == -1) {
|
|
689 close(sock);
|
|
690 gaim_debug_info("upnp",
|
|
691 "gaim_upnp_discover(): Failed In sock creation\n\n");
|
11213
|
692 free(hrd->recvBuffer);
|
|
693 free(hrd);
|
11195
|
694 return NULL;
|
|
695 }
|
|
696
|
|
697 memset(&server, 0, sizeof(struct sockaddr));
|
|
698 server.sin_family = AF_INET;
|
|
699 if((hp = gethostbyname(HTTPMU_HOST_ADDRESS)) == NULL) {
|
|
700 close(sock);
|
|
701 gaim_debug_info("upnp",
|
|
702 "gaim_upnp_discover(): Failed In gethostbyname\n\n");
|
11213
|
703 free(hrd->recvBuffer);
|
|
704 free(hrd);
|
11195
|
705 return NULL;
|
|
706 }
|
|
707
|
|
708 memcpy(&server.sin_addr,
|
|
709 hp->h_addr_list[0],
|
|
710 hp->h_length);
|
|
711 server.sin_port = htons(HTTPMU_HOST_PORT);
|
|
712
|
|
713 /* because we are sending over UDP, if there is a failure
|
|
714 we should retry the send NUM_UDP_ATTEMPTS times */
|
|
715 for(i = 0; i < NUM_UDP_ATTEMPTS; i++) {
|
|
716 sentSuccess = TRUE;
|
|
717 recvSuccess = TRUE;
|
|
718 totalSizeSent = 0;
|
|
719
|
11213
|
720 hrd->recvBuffer = NULL;
|
|
721 hrd->totalSizeRecv = 0;
|
|
722 hrd->done = FALSE;
|
|
723
|
|
724 hrd->recvBuffer = (char*)malloc(MAX_DISCOVERY_RECIEVE_SIZE);
|
|
725 if(hrd->recvBuffer == NULL) {
|
|
726 gaim_debug_info("upnp",
|
|
727 "gaim_upnp_discover(): Failed in hrd->recvBuffer MALLOC\n\n");
|
|
728 free(hrd);
|
|
729 return NULL;
|
|
730 }
|
|
731
|
11195
|
732 while(totalSizeSent < strlen(sendMessage)) {
|
|
733 sizeSent = sendto(sock,(void*)&sendMessage[totalSizeSent],
|
|
734 strlen(&sendMessage[totalSizeSent]),0,
|
|
735 (struct sockaddr*)&server,
|
|
736 sizeof(struct sockaddr_in));
|
|
737 if(sizeSent <= 0 && errno != EINTR) {
|
|
738 sentSuccess = FALSE;
|
|
739 break;
|
|
740 }else if(errno == EINTR) {
|
|
741 sizeSent = 0;
|
|
742 }
|
|
743 totalSizeSent += sizeSent;
|
|
744 }
|
|
745
|
|
746 if(sentSuccess) {
|
|
747 hrd->tima = gaim_timeout_add(DISCOVERY_TIMEOUT,
|
|
748 (GSourceFunc)gaim_upnp_timeout, hrd);
|
|
749 hrd->inpa = gaim_input_add(sock, GAIM_INPUT_READ,
|
|
750 gaim_upnp_discover_udp_read, hrd);
|
|
751 while (!hrd->done) {
|
|
752 gtk_main_iteration();
|
|
753 }
|
|
754 if(hrd->recvBuffer == NULL) {
|
|
755 recvSuccess = FALSE;
|
|
756 } else {
|
|
757 /* parse the response, and see if it was a success */
|
|
758 close(sock);
|
|
759 if((controlURL=
|
|
760 gaim_upnp_parse_discover_response(hrd->recvBuffer,
|
|
761 strlen(hrd->recvBuffer)))==NULL) {
|
|
762 gaim_debug_info("upnp",
|
|
763 "gaim_upnp_discover(): Failed In parse response\n\n");
|
11213
|
764 free(hrd->recvBuffer);
|
|
765 free(hrd);
|
11195
|
766 return NULL;
|
|
767 }
|
|
768 }
|
|
769 }
|
|
770
|
|
771 /* if sent success and recv successful, then break */
|
|
772 if(sentSuccess && recvSuccess) {
|
|
773 i = NUM_UDP_ATTEMPTS;
|
|
774 }
|
|
775 }
|
11213
|
776
|
|
777 if(hrd->recvBuffer != NULL) {
|
|
778 free(hrd->recvBuffer);
|
|
779 }
|
|
780 free(hrd);
|
|
781
|
11195
|
782 if(!sentSuccess || !recvSuccess) {
|
|
783 close(sock);
|
|
784 gaim_debug_info("upnp",
|
|
785 "gaim_upnp_discover(): Failed In sent/recv success\n\n");
|
|
786 return NULL;
|
|
787 }
|
|
788
|
|
789 return controlURL;
|
|
790 }
|
|
791
|
|
792
|
|
793
|
|
794
|
|
795 static char*
|
|
796 gaim_upnp_generate_action_message_and_send(const char* controlURL,
|
|
797 const char* actionName,
|
|
798 const char* actionParams)
|
|
799 {
|
|
800 char serviceType[] = "WANIPConnection:1";
|
|
801 char* actionMessage;
|
|
802 char* soapMessage;
|
|
803 char* httpResponse;
|
|
804
|
|
805 char* pathOfControl;
|
|
806 char* addressOfControl;
|
|
807 char* addressPortOfControl;
|
|
808 char* portOfControl;
|
|
809 unsigned short port;
|
|
810
|
|
811 /* set the soap message */
|
|
812 if((soapMessage = (char*)malloc(strlen(SOAP_ACTION) +
|
|
813 strlen(serviceType) +
|
|
814 strlen(actionParams) +
|
|
815 strlen(actionName) +
|
|
816 strlen(actionName))) == NULL) {
|
|
817 gaim_debug_info("upnp",
|
|
818 "generate_action_message_and_send(): Failed MALLOC soapMessage\n\n");
|
|
819 return NULL;
|
|
820 }
|
|
821 sprintf(soapMessage, SOAP_ACTION, actionName, serviceType,
|
|
822 actionParams, actionName);
|
|
823
|
|
824 /* parse the url into address, port, path variables */
|
|
825 if(!gaim_upnp_parse_url(controlURL, &pathOfControl,
|
|
826 &addressOfControl, &portOfControl)) {
|
|
827 gaim_debug_info("upnp",
|
|
828 "generate_action_message_and_send(): Failed In Parse URL\n\n");
|
|
829 free(soapMessage);
|
|
830 return NULL;
|
|
831 }
|
|
832
|
|
833 /* set the addressPortOfControl variable which should have a
|
|
834 form like the following: 192.168.1.1:8000 */
|
|
835 if((addressPortOfControl = (char*)malloc(strlen(addressOfControl) +
|
|
836 strlen(portOfControl) +
|
|
837 2)) == NULL) {
|
|
838 gaim_debug_info("upnp",
|
|
839 "generate_action_message_and_send(): MALLOC addressPortOfControl\n\n");
|
|
840 free(soapMessage);
|
|
841 free(pathOfControl);
|
|
842 free(addressOfControl);
|
|
843 free(portOfControl);
|
|
844 return NULL;
|
|
845 }
|
|
846 sprintf(addressPortOfControl, "%s:%s", addressOfControl, portOfControl);
|
|
847 if((port = atoi(portOfControl)) == 0) {
|
|
848 gaim_debug_info("upnp",
|
|
849 "generate_action_message_and_send(): Failed In port = atoi\n\n");
|
|
850 free(soapMessage);
|
|
851 free(pathOfControl);
|
|
852 free(addressOfControl);
|
|
853 free(portOfControl);
|
|
854 free(addressPortOfControl);
|
|
855 return NULL;
|
|
856 }
|
|
857
|
|
858 /* set the HTTP Header */
|
|
859 if((actionMessage = (char*)malloc(strlen(HTTP_HEADER_ACTION) +
|
|
860 strlen(pathOfControl) +
|
|
861 strlen(addressPortOfControl) +
|
|
862 strlen(serviceType) +
|
|
863 strlen(actionName) +
|
|
864 strlen(soapMessage))) == NULL) {
|
|
865 gaim_debug_info("upnp",
|
|
866 "generate_action_message_and_send(): Failed MALLOC actionMessage\n\n");
|
|
867 free(soapMessage);
|
|
868 free(pathOfControl);
|
|
869 free(addressOfControl);
|
|
870 free(portOfControl);
|
|
871 free(addressPortOfControl);
|
|
872 return NULL;
|
|
873 }
|
|
874 sprintf(actionMessage, HTTP_HEADER_ACTION,
|
|
875 pathOfControl, addressPortOfControl,
|
|
876 serviceType, actionName, strlen(soapMessage));
|
|
877
|
|
878 /* append to the header the body */
|
|
879 strcat(actionMessage, soapMessage);
|
|
880
|
|
881 /* get the return of the http response */
|
|
882 httpResponse = gaim_upnp_http_request(addressOfControl,
|
|
883 port, actionMessage);
|
|
884 if(httpResponse == NULL) {
|
|
885 gaim_debug_info("upnp",
|
|
886 "generate_action_message_and_send(): Failed In httpResponse\n\n");
|
|
887 }
|
|
888
|
|
889 free(actionMessage);
|
|
890 free(soapMessage);
|
|
891 free(pathOfControl);
|
|
892 free(addressOfControl);
|
|
893 free(portOfControl);
|
|
894 free(addressPortOfControl);
|
|
895
|
|
896 return httpResponse;
|
|
897 }
|
|
898
|
|
899
|
|
900
|
|
901
|
11204
|
902 char*
|
11195
|
903 gaim_upnp_get_public_ip(const char* controlURL)
|
|
904 {
|
|
905 char* extIPAddress;
|
|
906 char* httpResponse;
|
|
907 char actionName[] = "GetExternalIPAddress";
|
|
908 char actionParams[] = "";
|
|
909 char* temp, *temp2;
|
|
910
|
|
911 /* make sure controlURL is a valid URL */
|
|
912 if(!gaim_upnp_validate_url(controlURL)) {
|
|
913 gaim_debug_info("upnp",
|
|
914 "gaim_upnp_get_public_ip(): Failed In Validate URL\n\n");
|
|
915 return NULL;
|
|
916 }
|
|
917
|
|
918 httpResponse = gaim_upnp_generate_action_message_and_send(controlURL,
|
|
919 actionName,
|
|
920 actionParams);
|
|
921 if(httpResponse == NULL) {
|
|
922 gaim_debug_info("upnp",
|
|
923 "gaim_upnp_get_public_ip(): Failed In httpResponse\n\n");
|
|
924 return NULL;
|
|
925 }
|
|
926
|
|
927 /* extract the ip, or see if there is an error */
|
|
928 /* at some point, maybe extract the ip using an xml library */
|
|
929 if((temp = strstr(httpResponse, "<NewExternalIPAddress")) == NULL) {
|
|
930 gaim_debug_info("upnp",
|
|
931 "gaim_upnp_get_public_ip(): Failed Finding <NewExternalIPAddress\n\n");
|
|
932 free(httpResponse);
|
|
933 return NULL;
|
|
934 }
|
|
935 if((temp = strchr(temp, '>')) == NULL) {
|
|
936 gaim_debug_info("upnp",
|
|
937 "gaim_upnp_get_public_ip(): Failed In Finding >\n\n");
|
|
938 free(httpResponse);
|
|
939 return NULL;
|
|
940 }
|
|
941 if((temp2 = strchr(temp, '<')) == NULL) {
|
|
942 gaim_debug_info("upnp",
|
|
943 "gaim_upnp_get_public_ip(): Failed In Finding <\n\n");
|
|
944 free(httpResponse);
|
|
945 return NULL;
|
|
946 }
|
|
947 if((extIPAddress = (char*)malloc(temp2-temp)) == NULL) {
|
|
948 gaim_debug_info("upnp",
|
|
949 "gaim_upnp_get_public_ip(): Failed In MALLOC extIPAddress\n\n");
|
|
950 free(httpResponse);
|
|
951 return NULL;
|
|
952 }
|
|
953 memcpy(extIPAddress, &temp[1], (temp2-1)-temp);
|
|
954 extIPAddress[(temp2-1)-temp] = '\0';
|
|
955
|
|
956 free(httpResponse);
|
|
957 gaim_debug_info("upnp",
|
|
958 "gaim_upnp_get_public_ip() IP: %s\n\n", extIPAddress);
|
|
959 return extIPAddress;
|
|
960 }
|
|
961
|
|
962
|
|
963
|
|
964 static const char*
|
|
965 gaim_upnp_get_local_ip_address(const char* address) {
|
|
966 const char* ip;
|
|
967 int sock;
|
|
968 struct sockaddr_in serv_addr;
|
|
969 struct hostent *server;
|
|
970 char* pathOfControl;
|
|
971 char* addressOfControl;
|
|
972 char* portOfControl;
|
|
973 unsigned short port;
|
|
974
|
|
975 /* parse the url into address, port, path variables */
|
|
976 if(!gaim_upnp_parse_url(address, &pathOfControl,
|
|
977 &addressOfControl, &portOfControl)) {
|
|
978 gaim_debug_info("upnp",
|
|
979 "get_local_ip_address(): Failed In Parse URL\n\n");
|
|
980 return NULL;
|
|
981 }
|
|
982 if((port = atoi(portOfControl)) == 0) {
|
|
983 gaim_debug_info("upnp",
|
|
984 "get_local_ip_address(): Failed In port = atoi\n\n");
|
|
985 return NULL;
|
|
986 }
|
|
987
|
|
988 sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
989 if(sock < 0) {
|
|
990 gaim_debug_info("upnp",
|
|
991 "get_local_ip_address(): Failed In sock creation\n\n");
|
|
992 return NULL;
|
|
993 }
|
|
994
|
|
995 server = gethostbyname(addressOfControl);
|
|
996 if(server == NULL) {
|
|
997 gaim_debug_info("upnp",
|
|
998 "get_local_ip_address(): Failed In gethostbyname\n\n");
|
|
999 close(sock);
|
|
1000 return NULL;
|
|
1001 }
|
|
1002 memset((char*)&serv_addr, 0, sizeof(serv_addr));
|
|
1003 serv_addr.sin_family = AF_INET;
|
|
1004 memcpy(&serv_addr.sin_addr,
|
|
1005 server->h_addr_list[0],
|
|
1006 server->h_length);
|
|
1007 serv_addr.sin_port = htons(port);
|
|
1008
|
|
1009 if(connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) {
|
|
1010 gaim_debug_info("upnp",
|
|
1011 "get_local_ip_address(): Failed In connect\n\n");
|
|
1012 close(sock);
|
|
1013 return NULL;
|
|
1014 }
|
|
1015
|
|
1016 ip = gaim_network_get_local_system_ip(sock);
|
|
1017
|
|
1018 close(sock);
|
|
1019 return ip;
|
|
1020 }
|
|
1021
|
|
1022
|
|
1023
|
|
1024 gboolean
|
|
1025 gaim_upnp_set_port_mapping(const char* controlURL, unsigned short portMap,
|
|
1026 const char* protocol)
|
|
1027 {
|
|
1028 char* httpResponse;
|
|
1029 char actionName[] = "AddPortMapping";
|
|
1030 char* actionParams;
|
|
1031 const char* internalIP;
|
|
1032
|
|
1033 /* make sure controlURL is a valid URL */
|
|
1034 if(!gaim_upnp_validate_url(controlURL)) {
|
|
1035 gaim_debug_info("upnp",
|
|
1036 "gaim_upnp_set_port_mapping(): Failed In Validate URL\n\n");
|
|
1037 return FALSE;
|
|
1038 }
|
|
1039
|
|
1040 /* get the internal IP */
|
|
1041 if((internalIP = gaim_upnp_get_local_ip_address(controlURL)) == NULL) {
|
|
1042 gaim_debug_info("upnp",
|
|
1043 "gaim_upnp_set_port_mapping(): couldn't get local ip\n\n");
|
|
1044 return FALSE;
|
|
1045 }
|
|
1046
|
|
1047 /* make the portMappingParams variable */
|
|
1048 if((actionParams = (char*)malloc(strlen(ADD_PORT_MAPPING_PARAMS) +
|
|
1049 strlen(internalIP) +
|
|
1050 10 + /* size of port as int * 2 */
|
|
1051 strlen(protocol))) == NULL) {
|
|
1052 gaim_debug_info("upnp",
|
|
1053 "gaim_upnp_set_port_mapping(): Failed MALLOC portMappingParams\n\n");
|
|
1054 return FALSE;
|
|
1055 }
|
|
1056 sprintf(actionParams, ADD_PORT_MAPPING_PARAMS, portMap,
|
|
1057 protocol, portMap, internalIP);
|
|
1058
|
|
1059 httpResponse = gaim_upnp_generate_action_message_and_send(controlURL,
|
|
1060 actionName,
|
|
1061 actionParams);
|
|
1062
|
|
1063 if(httpResponse == NULL) {
|
|
1064 gaim_debug_info("upnp",
|
|
1065 "gaim_upnp_set_port_mapping(): Failed In httpResponse\n\n");
|
|
1066 free(actionParams);
|
|
1067 return FALSE;
|
|
1068 }
|
|
1069
|
|
1070 /* determine if port mapping was a success */
|
|
1071 if(strstr(httpResponse, HTTP_OK) == NULL) {
|
|
1072 gaim_debug_info("upnp",
|
|
1073 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n", httpResponse);
|
|
1074 free(actionParams);
|
|
1075 free(httpResponse);
|
|
1076 return FALSE;
|
|
1077 }
|
|
1078
|
|
1079 free(actionParams);
|
|
1080 free(httpResponse);
|
|
1081 return TRUE;
|
|
1082 }
|
|
1083
|
|
1084
|
|
1085 gboolean
|
|
1086 gaim_upnp_remove_port_mapping(const char* controlURL, unsigned short portMap,
|
|
1087 const char* protocol)
|
|
1088 {
|
|
1089 char* httpResponse;
|
|
1090 char actionName[] = "DeletePortMapping";
|
|
1091 char* actionParams;
|
|
1092
|
|
1093 /* make sure controlURL is a valid URL */
|
|
1094 if(!gaim_upnp_validate_url(controlURL)) {
|
|
1095 gaim_debug_info("upnp",
|
|
1096 "gaim_upnp_set_port_mapping(): Failed In Validate URL\n\n");
|
|
1097 return FALSE;
|
|
1098 }
|
|
1099
|
|
1100 /* make the portMappingParams variable */
|
|
1101 if((actionParams = (char*)malloc(strlen(DELETE_PORT_MAPPING_PARAMS) +
|
|
1102 5 + /* size of port as int */
|
|
1103 strlen(protocol))) == NULL) {
|
|
1104 gaim_debug_info("upnp",
|
|
1105 "gaim_upnp_set_port_mapping(): Failed MALLOC portMappingParams\n\n");
|
|
1106 return FALSE;
|
|
1107 }
|
|
1108 sprintf(actionParams, DELETE_PORT_MAPPING_PARAMS,
|
|
1109 portMap, protocol);
|
|
1110
|
|
1111 httpResponse = gaim_upnp_generate_action_message_and_send(controlURL,
|
|
1112 actionName,
|
|
1113 actionParams);
|
|
1114
|
|
1115 if(httpResponse == NULL) {
|
|
1116 gaim_debug_info("upnp",
|
|
1117 "gaim_upnp_set_port_mapping(): Failed In httpResponse\n\n");
|
|
1118 free(actionParams);
|
|
1119 return FALSE;
|
|
1120 }
|
|
1121
|
|
1122 /* determine if port mapping was a success */
|
|
1123 if(strstr(httpResponse, HTTP_OK) == NULL) {
|
|
1124 gaim_debug_info("upnp",
|
|
1125 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n", httpResponse);
|
|
1126 free(actionParams);
|
|
1127 free(httpResponse);
|
|
1128 return FALSE;
|
|
1129 }
|
|
1130
|
|
1131 free(actionParams);
|
|
1132 free(httpResponse);
|
|
1133 return TRUE;
|
|
1134 }
|