Increment version number
[pidgin-git.git] / libpurple / stun.c
blob809bf6b86f4493bf5d2789f707e93be21dc429bf
1 /**
2 * @file stun.c STUN (RFC3489) Implementation
3 * @ingroup core
4 */
6 /* purple
8 * STUN implementation inspired by jstun [http://jstun.javawi.de/]
10 * Purple is the legal property of its developers, whose names are too numerous
11 * to list here. Please refer to the COPYRIGHT file distributed with this
12 * source distribution.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include "internal.h"
32 #ifndef _WIN32
33 #include <net/if.h>
34 #include <sys/ioctl.h>
35 #endif
37 /* Solaris */
38 #if defined (__SVR4) && defined (__sun)
39 #include <sys/sockio.h>
40 #endif
42 #include "debug.h"
43 #include "account.h"
44 #include "dnsquery.h"
45 #include "dnssrv.h"
46 #include "network.h"
47 #include "proxy.h"
48 #include "stun.h"
49 #include "prefs.h"
51 #define MSGTYPE_BINDINGREQUEST 0x0001
52 #define MSGTYPE_BINDINGRESPONSE 0x0101
54 #define ATTRIB_MAPPEDADDRESS 0x0001
56 struct stun_header {
57 guint16 type;
58 guint16 len;
59 guint32 transid[4];
62 struct stun_attrib {
63 guint16 type;
64 guint16 len;
67 #ifdef NOTYET
68 struct stun_change {
69 struct stun_header hdr;
70 struct stun_attrib attrib;
71 char value[4];
73 #endif
75 struct stun_conn {
76 int fd;
77 struct sockaddr_in addr;
78 int test;
79 int retry;
80 guint incb;
81 guint timeout;
82 struct stun_header *packet;
83 size_t packetsize;
86 static PurpleStunNatDiscovery nattype = {
87 PURPLE_STUN_STATUS_UNDISCOVERED,
88 PURPLE_STUN_NAT_TYPE_PUBLIC_IP,
89 "\0", NULL, 0};
91 static GSList *callbacks = NULL;
93 static void close_stun_conn(struct stun_conn *sc) {
95 if (sc->incb)
96 purple_input_remove(sc->incb);
98 if (sc->timeout)
99 purple_timeout_remove(sc->timeout);
101 if (sc->fd)
102 close(sc->fd);
104 g_free(sc);
107 static void do_callbacks(void) {
108 while (callbacks) {
109 StunCallback cb = callbacks->data;
110 if (cb)
111 cb(&nattype);
112 callbacks = g_slist_delete_link(callbacks, callbacks);
116 static gboolean timeoutfunc(gpointer data) {
117 struct stun_conn *sc = data;
118 if(sc->retry >= 2) {
119 purple_debug_warning("stun", "request timed out, giving up.\n");
120 if(sc->test == 2)
121 nattype.type = PURPLE_STUN_NAT_TYPE_SYMMETRIC;
123 /* set unknown */
124 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
126 nattype.lookup_time = time(NULL);
128 /* callbacks */
129 do_callbacks();
131 /* we don't need to remove the timeout (returning FALSE) */
132 sc->timeout = 0;
133 close_stun_conn(sc);
135 return FALSE;
137 purple_debug_info("stun", "request timed out, retrying.\n");
138 sc->retry++;
139 sendto(sc->fd, sc->packet, sc->packetsize, 0,
140 (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in));
141 return TRUE;
144 #ifdef NOTYET
145 static void do_test2(struct stun_conn *sc) {
146 struct stun_change data;
147 data.hdr.type = htons(0x0001);
148 data.hdr.len = 0;
149 data.hdr.transid[0] = rand();
150 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
151 data.hdr.transid[2] = rand();
152 data.hdr.transid[3] = rand();
153 data.attrib.type = htons(0x003);
154 data.attrib.len = htons(4);
155 data.value[3] = 6;
156 sc->packet = (struct stun_header*)&data;
157 sc->packetsize = sizeof(struct stun_change);
158 sc->retry = 0;
159 sc->test = 2;
160 sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in));
161 sc->timeout = purple_timeout_add(500, (GSourceFunc) timeoutfunc, sc);
163 #endif
165 static void reply_cb(gpointer data, gint source, PurpleInputCondition cond) {
166 struct stun_conn *sc = data;
167 char buffer[65536];
168 char *tmp;
169 int len;
170 struct in_addr in;
171 struct stun_attrib *attrib;
172 struct stun_header *hdr;
173 struct ifconf ifc;
174 struct ifreq *ifr;
175 struct sockaddr_in *sinptr;
177 len = recv(source, buffer, sizeof(buffer) - 1, 0);
178 if (!len) {
179 purple_debug_warning("stun", "unable to read stun response\n");
180 return;
182 buffer[len] = '\0';
184 if (len < sizeof(struct stun_header)) {
185 purple_debug_warning("stun", "got invalid response\n");
186 return;
189 hdr = (struct stun_header*) buffer;
190 if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) {
191 purple_debug_warning("stun", "got incomplete response\n");
192 return;
195 /* wrong transaction */
196 if(hdr->transid[0] != sc->packet->transid[0]
197 || hdr->transid[1] != sc->packet->transid[1]
198 || hdr->transid[2] != sc->packet->transid[2]
199 || hdr->transid[3] != sc->packet->transid[3]) {
200 purple_debug_warning("stun", "got wrong transid\n");
201 return;
204 if(sc->test==1) {
205 if (hdr->type != MSGTYPE_BINDINGRESPONSE) {
206 purple_debug_warning("stun",
207 "Expected Binding Response, got %d\n",
208 hdr->type);
209 return;
212 tmp = buffer + sizeof(struct stun_header);
213 while((buffer + len) > (tmp + sizeof(struct stun_attrib))) {
214 attrib = (struct stun_attrib*) tmp;
215 tmp += sizeof(struct stun_attrib);
217 if (!((buffer + len) > (tmp + ntohs(attrib->len))))
218 break;
220 if(attrib->type == htons(ATTRIB_MAPPEDADDRESS)
221 && ntohs(attrib->len) == 8) {
222 char *ip;
223 /* Skip the first unused byte,
224 * the family(1 byte), and the port(2 bytes);
225 * then read the 4 byte IPv4 address */
226 memcpy(&in.s_addr, tmp + 4, 4);
227 ip = inet_ntoa(in);
228 if(ip)
229 g_strlcpy(nattype.publicip, ip, sizeof(nattype.publicip));
232 tmp += ntohs(attrib->len);
234 purple_debug_info("stun", "got public ip %s\n", nattype.publicip);
235 nattype.status = PURPLE_STUN_STATUS_DISCOVERED;
236 nattype.type = PURPLE_STUN_NAT_TYPE_UNKNOWN_NAT;
237 nattype.lookup_time = time(NULL);
239 /* is it a NAT? */
241 ifc.ifc_len = sizeof(buffer);
242 ifc.ifc_req = (struct ifreq *) buffer;
243 ioctl(source, SIOCGIFCONF, &ifc);
245 tmp = buffer;
246 while(tmp < buffer + ifc.ifc_len) {
247 ifr = (struct ifreq *) tmp;
249 tmp += sizeof(struct ifreq);
251 if(ifr->ifr_addr.sa_family == AF_INET) {
252 /* we only care about ipv4 interfaces */
253 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
254 if(sinptr->sin_addr.s_addr == in.s_addr) {
255 /* no NAT */
256 purple_debug_info("stun", "no nat\n");
257 nattype.type = PURPLE_STUN_NAT_TYPE_PUBLIC_IP;
262 #ifndef NOTYET
263 close_stun_conn(sc);
264 do_callbacks();
265 #else
266 purple_timeout_remove(sc->timeout);
267 sc->timeout = 0;
269 do_test2(sc);
270 } else if(sc->test == 2) {
271 close_stun_conn(sc);
272 nattype.type = PURPLE_STUN_NAT_TYPE_FULL_CONE;
273 do_callbacks();
274 #endif
279 static void hbn_listen_cb(int fd, gpointer data) {
280 GSList *hosts = data;
281 struct stun_conn *sc;
282 static struct stun_header hdr_data;
284 if(fd < 0) {
285 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
286 nattype.lookup_time = time(NULL);
287 do_callbacks();
288 return;
291 sc = g_new0(struct stun_conn, 1);
292 sc->fd = fd;
294 sc->addr.sin_family = AF_INET;
295 sc->addr.sin_port = htons(purple_network_get_port_from_fd(fd));
296 sc->addr.sin_addr.s_addr = INADDR_ANY;
298 sc->incb = purple_input_add(fd, PURPLE_INPUT_READ, reply_cb, sc);
300 hosts = g_slist_delete_link(hosts, hosts);
301 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in));
302 g_free(hosts->data);
303 hosts = g_slist_delete_link(hosts, hosts);
304 while (hosts) {
305 hosts = g_slist_delete_link(hosts, hosts);
306 g_free(hosts->data);
307 hosts = g_slist_delete_link(hosts, hosts);
310 hdr_data.type = htons(MSGTYPE_BINDINGREQUEST);
311 hdr_data.len = 0;
312 hdr_data.transid[0] = rand();
313 hdr_data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
314 hdr_data.transid[2] = rand();
315 hdr_data.transid[3] = rand();
317 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0,
318 (struct sockaddr *)&(sc->addr),
319 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
320 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
321 nattype.lookup_time = time(NULL);
322 do_callbacks();
323 close_stun_conn(sc);
324 return;
326 sc->test = 1;
327 sc->packet = &hdr_data;
328 sc->packetsize = sizeof(struct stun_header);
329 sc->timeout = purple_timeout_add(500, (GSourceFunc) timeoutfunc, sc);
332 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) {
334 if(!hosts || !hosts->data) {
335 nattype.status = PURPLE_STUN_STATUS_UNDISCOVERED;
336 nattype.lookup_time = time(NULL);
337 do_callbacks();
338 return;
341 if (!purple_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) {
342 while (hosts) {
343 hosts = g_slist_delete_link(hosts, hosts);
344 g_free(hosts->data);
345 hosts = g_slist_delete_link(hosts, hosts);
348 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
349 nattype.lookup_time = time(NULL);
350 do_callbacks();
351 return;
357 static void do_test1(PurpleSrvResponse *resp, int results, gpointer sdata) {
358 const char *servername = sdata;
359 int port = 3478;
361 if(results) {
362 servername = resp[0].hostname;
363 port = resp[0].port;
365 purple_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n",
366 results, servername, port);
368 purple_dnsquery_a_account(NULL, servername, port, hbn_cb, NULL);
369 g_free(resp);
372 static gboolean call_callback(gpointer data) {
373 StunCallback cb = data;
374 cb(&nattype);
375 return FALSE;
378 PurpleStunNatDiscovery *purple_stun_discover(StunCallback cb) {
379 const char *servername = purple_prefs_get_string("/purple/network/stun_server");
381 purple_debug_info("stun", "using server %s\n", servername);
383 if(nattype.status == PURPLE_STUN_STATUS_DISCOVERING) {
384 if(cb)
385 callbacks = g_slist_append(callbacks, cb);
386 return &nattype;
389 if(nattype.status != PURPLE_STUN_STATUS_UNDISCOVERED) {
390 gboolean use_cached_result = TRUE;
392 /** Deal with the server name having changed since we did the
393 lookup */
394 if (servername && strlen(servername) > 1
395 && !purple_strequal(servername, nattype.servername)) {
396 use_cached_result = FALSE;
399 /* If we don't have a successful status and it has been 5
400 minutes since we last did a lookup, redo the lookup */
401 if (nattype.status != PURPLE_STUN_STATUS_DISCOVERED
402 && (time(NULL) - nattype.lookup_time) > 300) {
403 use_cached_result = FALSE;
406 if (use_cached_result) {
407 if(cb)
408 purple_timeout_add(10, call_callback, cb);
409 return &nattype;
413 if(!servername || (strlen(servername) < 2)) {
414 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
415 nattype.lookup_time = time(NULL);
416 if(cb)
417 purple_timeout_add(10, call_callback, cb);
418 return &nattype;
421 nattype.status = PURPLE_STUN_STATUS_DISCOVERING;
422 nattype.publicip[0] = '\0';
423 g_free(nattype.servername);
424 nattype.servername = g_strdup(servername);
426 callbacks = g_slist_append(callbacks, cb);
427 purple_srv_resolve_account(NULL, "stun", "udp", servername, do_test1,
428 (gpointer) servername);
430 return &nattype;
433 void purple_stun_init() {
434 purple_prefs_add_string("/purple/network/stun_server", "");