annotate src/upnp.c @ 11195:3aeb85cc9cda

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