Updated Bengali translation, a month and a half after it was submitted. Fixes #12141.
[pidgin-git.git] / libpurple / stun.c
blobc6f71da0b491153a9ca3d55fbd27928084ab8d26
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_remove(callbacks, cb);
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 strcpy(nattype.publicip, ip);
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;
283 int ret;
285 if(fd < 0) {
286 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
287 nattype.lookup_time = time(NULL);
288 do_callbacks();
289 return;
292 sc = g_new0(struct stun_conn, 1);
293 sc->fd = fd;
295 sc->addr.sin_family = AF_INET;
296 sc->addr.sin_port = htons(purple_network_get_port_from_fd(fd));
297 sc->addr.sin_addr.s_addr = INADDR_ANY;
299 sc->incb = purple_input_add(fd, PURPLE_INPUT_READ, reply_cb, sc);
301 ret = GPOINTER_TO_INT(hosts->data);
302 hosts = g_slist_remove(hosts, hosts->data);
303 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in));
304 g_free(hosts->data);
305 hosts = g_slist_remove(hosts, hosts->data);
306 while(hosts) {
307 hosts = g_slist_remove(hosts, hosts->data);
308 g_free(hosts->data);
309 hosts = g_slist_remove(hosts, hosts->data);
312 hdr_data.type = htons(MSGTYPE_BINDINGREQUEST);
313 hdr_data.len = 0;
314 hdr_data.transid[0] = rand();
315 hdr_data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
316 hdr_data.transid[2] = rand();
317 hdr_data.transid[3] = rand();
319 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0,
320 (struct sockaddr *)&(sc->addr),
321 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
322 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
323 nattype.lookup_time = time(NULL);
324 do_callbacks();
325 close_stun_conn(sc);
326 return;
328 sc->test = 1;
329 sc->packet = &hdr_data;
330 sc->packetsize = sizeof(struct stun_header);
331 sc->timeout = purple_timeout_add(500, (GSourceFunc) timeoutfunc, sc);
334 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) {
336 if(!hosts || !hosts->data) {
337 nattype.status = PURPLE_STUN_STATUS_UNDISCOVERED;
338 nattype.lookup_time = time(NULL);
339 do_callbacks();
340 return;
343 if (!purple_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) {
344 while(hosts) {
345 hosts = g_slist_remove(hosts, hosts->data);
346 g_free(hosts->data);
347 hosts = g_slist_remove(hosts, hosts->data);
350 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
351 nattype.lookup_time = time(NULL);
352 do_callbacks();
353 return;
359 static void do_test1(PurpleSrvResponse *resp, int results, gpointer sdata) {
360 const char *servername = sdata;
361 int port = 3478;
363 if(results) {
364 servername = resp[0].hostname;
365 port = resp[0].port;
367 purple_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n",
368 results, servername, port);
370 purple_dnsquery_a(servername, port, hbn_cb, NULL);
371 g_free(resp);
374 static gboolean call_callback(gpointer data) {
375 StunCallback cb = data;
376 cb(&nattype);
377 return FALSE;
380 PurpleStunNatDiscovery *purple_stun_discover(StunCallback cb) {
381 const char *servername = purple_prefs_get_string("/purple/network/stun_server");
383 purple_debug_info("stun", "using server %s\n", servername);
385 if(nattype.status == PURPLE_STUN_STATUS_DISCOVERING) {
386 if(cb)
387 callbacks = g_slist_append(callbacks, cb);
388 return &nattype;
391 if(nattype.status != PURPLE_STUN_STATUS_UNDISCOVERED) {
392 gboolean use_cached_result = TRUE;
394 /** Deal with the server name having changed since we did the
395 lookup */
396 if (servername && strlen(servername) > 1
397 && !purple_strequal(servername, nattype.servername)) {
398 use_cached_result = FALSE;
401 /* If we don't have a successful status and it has been 5
402 minutes since we last did a lookup, redo the lookup */
403 if (nattype.status != PURPLE_STUN_STATUS_DISCOVERED
404 && (time(NULL) - nattype.lookup_time) > 300) {
405 use_cached_result = FALSE;
408 if (use_cached_result) {
409 if(cb)
410 purple_timeout_add(10, call_callback, cb);
411 return &nattype;
415 if(!servername || (strlen(servername) < 2)) {
416 nattype.status = PURPLE_STUN_STATUS_UNKNOWN;
417 nattype.lookup_time = time(NULL);
418 if(cb)
419 purple_timeout_add(10, call_callback, cb);
420 return &nattype;
423 nattype.status = PURPLE_STUN_STATUS_DISCOVERING;
424 nattype.publicip[0] = '\0';
425 g_free(nattype.servername);
426 nattype.servername = g_strdup(servername);
428 callbacks = g_slist_append(callbacks, cb);
429 purple_srv_resolve("stun", "udp", servername, do_test1,
430 (gpointer) servername);
432 return &nattype;
435 void purple_stun_init() {
436 purple_prefs_add_string("/purple/network/stun_server", "");