14192
|
1 /**
|
|
2 * @file stun.c STUN (RFC3489) Implementation
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * STUN implementation inspired by jstun [http://jstun.javawi.de/]
|
|
8 *
|
|
9 * Gaim is the legal property of its developers, whose names are too numerous
|
|
10 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
11 * source distribution.
|
|
12 *
|
|
13 * This program is free software; you can redistribute it and/or modify
|
|
14 * it under the terms of the GNU General Public License as published by
|
|
15 * the Free Software Foundation; either version 2 of the License, or
|
|
16 * (at your option) any later version.
|
|
17 *
|
|
18 * This program is distributed in the hope that it will be useful,
|
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 * GNU General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU General Public License
|
|
24 * along with this program; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 *
|
|
27 */
|
|
28
|
|
29 #include "internal.h"
|
|
30
|
|
31 #ifndef _WIN32
|
|
32 #include <net/if.h>
|
|
33 #include <sys/ioctl.h>
|
|
34 #endif
|
|
35
|
|
36 /* Solaris */
|
|
37 #if defined (__SVR4) && defined (__sun)
|
|
38 #include <sys/sockio.h>
|
|
39 #endif
|
|
40
|
|
41 #include "debug.h"
|
|
42 #include "account.h"
|
|
43 #include "dnsquery.h"
|
|
44 #include "dnssrv.h"
|
|
45 #include "network.h"
|
|
46 #include "proxy.h"
|
|
47 #include "stun.h"
|
|
48 #include "prefs.h"
|
|
49
|
|
50 #define MSGTYPE_BINDINGREQUEST 0x0001
|
|
51 #define MSGTYPE_BINDINGRESPONSE 0x0101
|
|
52
|
|
53 #define ATTRIB_MAPPEDADDRESS 0x0001
|
|
54
|
|
55 struct stun_header {
|
|
56 guint16 type;
|
|
57 guint16 len;
|
|
58 guint32 transid[4];
|
|
59 };
|
|
60
|
|
61 struct stun_attrib {
|
|
62 guint16 type;
|
|
63 guint16 len;
|
|
64 };
|
|
65
|
|
66 #ifdef NOTYET
|
|
67 struct stun_change {
|
|
68 struct stun_header hdr;
|
|
69 struct stun_attrib attrib;
|
|
70 char value[4];
|
|
71 };
|
|
72 #endif
|
|
73
|
|
74 struct stun_conn {
|
|
75 int fd;
|
|
76 struct sockaddr_in addr;
|
|
77 int test;
|
|
78 int retry;
|
|
79 guint incb;
|
|
80 guint timeout;
|
|
81 struct stun_header *packet;
|
|
82 size_t packetsize;
|
|
83 };
|
|
84
|
|
85 static GaimStunNatDiscovery nattype = {
|
|
86 GAIM_STUN_STATUS_UNDISCOVERED,
|
|
87 GAIM_STUN_NAT_TYPE_PUBLIC_IP,
|
|
88 "\0", NULL, 0};
|
|
89
|
|
90 static GSList *callbacks = NULL;
|
|
91
|
|
92 static void close_stun_conn(struct stun_conn *sc) {
|
|
93
|
|
94 if (sc->incb)
|
|
95 gaim_input_remove(sc->incb);
|
|
96
|
|
97 if (sc->timeout)
|
|
98 gaim_timeout_remove(sc->timeout);
|
|
99
|
|
100 if (sc->fd)
|
|
101 close(sc->fd);
|
|
102
|
|
103 g_free(sc);
|
|
104 }
|
|
105
|
|
106 static void do_callbacks() {
|
|
107 while(callbacks) {
|
|
108 StunCallback cb = callbacks->data;
|
|
109 if(cb)
|
|
110 cb(&nattype);
|
|
111 callbacks = g_slist_remove(callbacks, cb);
|
|
112 }
|
|
113 }
|
|
114
|
|
115 static gboolean timeoutfunc(gpointer data) {
|
|
116 struct stun_conn *sc = data;
|
|
117 if(sc->retry >= 2) {
|
|
118 gaim_debug_info("stun", "request timed out, giving up.\n");
|
|
119 if(sc->test == 2)
|
|
120 nattype.type = GAIM_STUN_NAT_TYPE_SYMMETRIC;
|
|
121
|
|
122 /* set unknown */
|
|
123 nattype.status = GAIM_STUN_STATUS_UNKNOWN;
|
|
124
|
|
125 nattype.lookup_time = time(NULL);
|
|
126
|
|
127 /* callbacks */
|
|
128 do_callbacks();
|
|
129
|
|
130 /* we don't need to remove the timeout (returning FALSE) */
|
|
131 sc->timeout = 0;
|
|
132 close_stun_conn(sc);
|
|
133
|
|
134 return FALSE;
|
|
135 }
|
|
136 gaim_debug_info("stun", "request timed out, retrying.\n");
|
|
137 sc->retry++;
|
|
138 sendto(sc->fd, sc->packet, sc->packetsize, 0,
|
|
139 (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in));
|
|
140 return TRUE;
|
|
141 }
|
|
142
|
|
143 #ifdef NOTYET
|
|
144 static void do_test2(struct stun_conn *sc) {
|
|
145 struct stun_change data;
|
|
146 data.hdr.type = htons(0x0001);
|
|
147 data.hdr.len = 0;
|
|
148 data.hdr.transid[0] = rand();
|
|
149 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
|
|
150 data.hdr.transid[2] = rand();
|
|
151 data.hdr.transid[3] = rand();
|
|
152 data.attrib.type = htons(0x003);
|
|
153 data.attrib.len = htons(4);
|
|
154 data.value[3] = 6;
|
|
155 sc->packet = (struct stun_header*)&data;
|
|
156 sc->packetsize = sizeof(struct stun_change);
|
|
157 sc->retry = 0;
|
|
158 sc->test = 2;
|
|
159 sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in));
|
|
160 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc);
|
|
161 }
|
|
162 #endif
|
|
163
|
|
164 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) {
|
|
165 struct stun_conn *sc = data;
|
|
166 char buffer[65536];
|
|
167 char *tmp;
|
|
168 int len;
|
|
169 struct in_addr in;
|
|
170 struct stun_attrib *attrib;
|
|
171 struct stun_header *hdr;
|
|
172 struct ifconf ifc;
|
|
173 struct ifreq *ifr;
|
|
174 struct sockaddr_in *sinptr;
|
|
175
|
|
176 len = recv(source, buffer, sizeof(buffer) - 1, 0);
|
|
177 if (!len) {
|
|
178 gaim_debug_info("stun", "unable to read stun response\n");
|
|
179 return;
|
|
180 }
|
|
181 buffer[len] = '\0';
|
|
182
|
|
183 if (len < sizeof(struct stun_header)) {
|
|
184 gaim_debug_info("stun", "got invalid response\n");
|
|
185 return;
|
|
186 }
|
|
187
|
|
188 hdr = (struct stun_header*) buffer;
|
|
189 if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) {
|
|
190 gaim_debug_info("stun", "got incomplete response\n");
|
|
191 return;
|
|
192 }
|
|
193
|
|
194 /* wrong transaction */
|
|
195 if(hdr->transid[0] != sc->packet->transid[0]
|
|
196 || hdr->transid[1] != sc->packet->transid[1]
|
|
197 || hdr->transid[2] != sc->packet->transid[2]
|
|
198 || hdr->transid[3] != sc->packet->transid[3]) {
|
|
199 gaim_debug_info("stun", "got wrong transid\n");
|
|
200 return;
|
|
201 }
|
|
202
|
|
203 if(sc->test==1) {
|
|
204 if (hdr->type != MSGTYPE_BINDINGRESPONSE) {
|
|
205 gaim_debug_info("stun",
|
|
206 "Expected Binding Response, got %d\n",
|
|
207 hdr->type);
|
|
208 return;
|
|
209 }
|
|
210
|
|
211 tmp = buffer + sizeof(struct stun_header);
|
|
212 while((buffer + len) > (tmp + sizeof(struct stun_attrib))) {
|
|
213 attrib = (struct stun_attrib*) tmp;
|
|
214 tmp += sizeof(struct stun_attrib);
|
|
215
|
|
216 if (!((buffer + len) > (tmp + ntohs(attrib->len))))
|
|
217 break;
|
|
218
|
|
219 if(attrib->type == htons(ATTRIB_MAPPEDADDRESS)
|
|
220 && ntohs(attrib->len) == 8) {
|
|
221 char *ip;
|
|
222 /* Skip the first unused byte,
|
|
223 * the family(1 byte), and the port(2 bytes);
|
|
224 * then read the 4 byte IPv4 address */
|
|
225 memcpy(&in.s_addr, tmp + 4, 4);
|
|
226 ip = inet_ntoa(in);
|
|
227 if(ip)
|
|
228 strcpy(nattype.publicip, ip);
|
|
229 }
|
|
230
|
|
231 tmp += ntohs(attrib->len);
|
|
232 }
|
|
233 gaim_debug_info("stun", "got public ip %s\n", nattype.publicip);
|
|
234 nattype.status = GAIM_STUN_STATUS_DISCOVERED;
|
|
235 nattype.type = GAIM_STUN_NAT_TYPE_UNKNOWN_NAT;
|
|
236 nattype.lookup_time = time(NULL);
|
|
237
|
|
238 /* is it a NAT? */
|
|
239
|
|
240 ifc.ifc_len = sizeof(buffer);
|
|
241 ifc.ifc_req = (struct ifreq *) buffer;
|
|
242 ioctl(source, SIOCGIFCONF, &ifc);
|
|
243
|
|
244 tmp = buffer;
|
|
245 while(tmp < buffer + ifc.ifc_len) {
|
|
246 ifr = (struct ifreq *) tmp;
|
|
247
|
|
248 tmp += sizeof(struct ifreq);
|
|
249
|
|
250 if(ifr->ifr_addr.sa_family == AF_INET) {
|
|
251 /* we only care about ipv4 interfaces */
|
|
252 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
|
|
253 if(sinptr->sin_addr.s_addr == in.s_addr) {
|
|
254 /* no NAT */
|
|
255 gaim_debug_info("stun", "no nat");
|
|
256 nattype.type = GAIM_STUN_NAT_TYPE_PUBLIC_IP;
|
|
257 }
|
|
258 }
|
|
259 }
|
|
260
|
|
261 #ifndef NOTYET
|
|
262 close_stun_conn(sc);
|
|
263 do_callbacks();
|
|
264 #else
|
|
265 gaim_timeout_remove(sc->timeout);
|
|
266 sc->timeout = 0;
|
|
267
|
|
268 do_test2(sc);
|
|
269 } else if(sc->test == 2) {
|
|
270 close_stun_conn(sc);
|
|
271 nattype.type = GAIM_STUN_NAT_TYPE_FULL_CONE;
|
|
272 do_callbacks();
|
|
273 #endif
|
|
274 }
|
|
275 }
|
|
276
|
|
277
|
|
278 static void hbn_listen_cb(int fd, gpointer data) {
|
|
279 GSList *hosts = data;
|
|
280 struct stun_conn *sc;
|
|
281 static struct stun_header hdr_data;
|
|
282 int ret;
|
|
283
|
|
284 if(fd < 0) {
|
|
285 nattype.status = GAIM_STUN_STATUS_UNKNOWN;
|
|
286 nattype.lookup_time = time(NULL);
|
|
287 do_callbacks();
|
|
288 return;
|
|
289 }
|
|
290
|
|
291 sc = g_new0(struct stun_conn, 1);
|
|
292 sc->fd = fd;
|
|
293
|
|
294 sc->addr.sin_family = AF_INET;
|
|
295 sc->addr.sin_port = htons(gaim_network_get_port_from_fd(fd));
|
|
296 sc->addr.sin_addr.s_addr = INADDR_ANY;
|
|
297
|
|
298 sc->incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, sc);
|
|
299
|
|
300 ret = GPOINTER_TO_INT(hosts->data);
|
|
301 hosts = g_slist_remove(hosts, hosts->data);
|
|
302 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in));
|
|
303 g_free(hosts->data);
|
|
304 hosts = g_slist_remove(hosts, hosts->data);
|
|
305 while(hosts) {
|
|
306 hosts = g_slist_remove(hosts, hosts->data);
|
|
307 g_free(hosts->data);
|
|
308 hosts = g_slist_remove(hosts, hosts->data);
|
|
309 }
|
|
310
|
|
311 hdr_data.type = htons(MSGTYPE_BINDINGREQUEST);
|
|
312 hdr_data.len = 0;
|
|
313 hdr_data.transid[0] = rand();
|
|
314 hdr_data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
|
|
315 hdr_data.transid[2] = rand();
|
|
316 hdr_data.transid[3] = rand();
|
|
317
|
|
318 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0,
|
|
319 (struct sockaddr *)&(sc->addr),
|
|
320 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
|
|
321 nattype.status = GAIM_STUN_STATUS_UNKNOWN;
|
|
322 nattype.lookup_time = time(NULL);
|
|
323 do_callbacks();
|
|
324 close_stun_conn(sc);
|
|
325 return;
|
|
326 }
|
|
327 sc->test = 1;
|
|
328 sc->packet = &hdr_data;
|
|
329 sc->packetsize = sizeof(struct stun_header);
|
|
330 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc);
|
|
331 }
|
|
332
|
|
333 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) {
|
|
334
|
|
335 if(!hosts || !hosts->data) {
|
|
336 nattype.status = GAIM_STUN_STATUS_UNDISCOVERED;
|
|
337 nattype.lookup_time = time(NULL);
|
|
338 do_callbacks();
|
|
339 return;
|
|
340 }
|
|
341
|
|
342 if (!gaim_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) {
|
|
343 nattype.status = GAIM_STUN_STATUS_UNKNOWN;
|
|
344 nattype.lookup_time = time(NULL);
|
|
345 do_callbacks();
|
|
346 return;
|
|
347 }
|
|
348
|
|
349
|
|
350 }
|
|
351
|
|
352 static void do_test1(GaimSrvResponse *resp, int results, gpointer sdata) {
|
|
353 const char *servername = sdata;
|
|
354 int port = 3478;
|
|
355
|
|
356 if(results) {
|
|
357 servername = resp[0].hostname;
|
|
358 port = resp[0].port;
|
|
359 }
|
|
360 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n",
|
|
361 results, servername, port);
|
|
362
|
|
363 gaim_dnsquery_a(servername, port, hbn_cb, NULL);
|
|
364 g_free(resp);
|
|
365 }
|
|
366
|
|
367 static gboolean call_callback(gpointer data) {
|
|
368 StunCallback cb = data;
|
|
369 cb(&nattype);
|
|
370 return FALSE;
|
|
371 }
|
|
372
|
|
373 GaimStunNatDiscovery *gaim_stun_discover(StunCallback cb) {
|
|
374 const char *servername = gaim_prefs_get_string("/core/network/stun_server");
|
|
375
|
|
376 gaim_debug_info("stun", "using server %s\n", servername);
|
|
377
|
|
378 if(nattype.status == GAIM_STUN_STATUS_DISCOVERING) {
|
|
379 if(cb)
|
|
380 callbacks = g_slist_append(callbacks, cb);
|
|
381 return &nattype;
|
|
382 }
|
|
383
|
|
384 if(nattype.status != GAIM_STUN_STATUS_UNDISCOVERED) {
|
|
385 gboolean use_cached_result = TRUE;
|
|
386
|
|
387 /** Deal with the server name having changed since we did the
|
|
388 lookup */
|
|
389 if (servername && strlen(servername) > 1
|
|
390 && ((nattype.servername
|
|
391 && strcmp(servername, nattype.servername))
|
|
392 || !nattype.servername)) {
|
|
393 use_cached_result = FALSE;
|
|
394 }
|
|
395
|
|
396 /* If we don't have a successful status and it has been 5
|
|
397 minutes since we last did a lookup, redo the lookup */
|
|
398 if (nattype.status != GAIM_STUN_STATUS_DISCOVERED
|
|
399 && (time(NULL) - nattype.lookup_time) > 300) {
|
|
400 use_cached_result = FALSE;
|
|
401 }
|
|
402
|
|
403 if (use_cached_result) {
|
|
404 if(cb)
|
|
405 gaim_timeout_add(10, call_callback, cb);
|
|
406 return &nattype;
|
|
407 }
|
|
408 }
|
|
409
|
|
410 if(!servername || (strlen(servername) < 2)) {
|
|
411 nattype.status = GAIM_STUN_STATUS_UNKNOWN;
|
|
412 nattype.lookup_time = time(NULL);
|
|
413 if(cb)
|
|
414 gaim_timeout_add(10, call_callback, cb);
|
|
415 return &nattype;
|
|
416 }
|
|
417
|
|
418 nattype.status = GAIM_STUN_STATUS_DISCOVERING;
|
|
419 nattype.publicip[0] = '\0';
|
|
420 g_free(nattype.servername);
|
|
421 nattype.servername = g_strdup(servername);
|
|
422
|
|
423 callbacks = g_slist_append(callbacks, cb);
|
|
424 gaim_srv_resolve("stun", "udp", servername, do_test1,
|
|
425 (gpointer) servername);
|
|
426
|
|
427 return &nattype;
|
|
428 }
|
|
429
|
|
430 void gaim_stun_init() {
|
|
431 gaim_prefs_add_string("/core/network/stun_server", "");
|
|
432 gaim_stun_discover(NULL);
|
|
433 }
|