2 * @file stun.c STUN (RFC3489) Implementation
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
34 #include <sys/ioctl.h>
38 #if defined (__SVR4) && defined (__sun)
39 #include <sys/sockio.h>
51 #define MSGTYPE_BINDINGREQUEST 0x0001
52 #define MSGTYPE_BINDINGRESPONSE 0x0101
54 #define ATTRIB_MAPPEDADDRESS 0x0001
69 struct stun_header hdr
;
70 struct stun_attrib attrib
;
77 struct sockaddr_in addr
;
82 struct stun_header
*packet
;
86 static PurpleStunNatDiscovery nattype
= {
87 PURPLE_STUN_STATUS_UNDISCOVERED
,
88 PURPLE_STUN_NAT_TYPE_PUBLIC_IP
,
91 static GSList
*callbacks
= NULL
;
93 static void close_stun_conn(struct stun_conn
*sc
) {
96 purple_input_remove(sc
->incb
);
99 purple_timeout_remove(sc
->timeout
);
107 static void do_callbacks(void) {
109 StunCallback cb
= callbacks
->data
;
112 callbacks
= g_slist_delete_link(callbacks
, callbacks
);
116 static gboolean
timeoutfunc(gpointer data
) {
117 struct stun_conn
*sc
= data
;
119 purple_debug_warning("stun", "request timed out, giving up.\n");
121 nattype
.type
= PURPLE_STUN_NAT_TYPE_SYMMETRIC
;
124 nattype
.status
= PURPLE_STUN_STATUS_UNKNOWN
;
126 nattype
.lookup_time
= time(NULL
);
131 /* we don't need to remove the timeout (returning FALSE) */
137 purple_debug_info("stun", "request timed out, retrying.\n");
139 sendto(sc
->fd
, sc
->packet
, sc
->packetsize
, 0,
140 (struct sockaddr
*)&(sc
->addr
), sizeof(struct sockaddr_in
));
145 static void do_test2(struct stun_conn
*sc
) {
146 struct stun_change data
;
147 data
.hdr
.type
= htons(0x0001);
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);
156 sc
->packet
= (struct stun_header
*)&data
;
157 sc
->packetsize
= sizeof(struct stun_change
);
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
);
165 static void reply_cb(gpointer data
, gint source
, PurpleInputCondition cond
) {
166 struct stun_conn
*sc
= data
;
171 struct stun_attrib
*attrib
;
172 struct stun_header
*hdr
;
175 struct sockaddr_in
*sinptr
;
177 len
= recv(source
, buffer
, sizeof(buffer
) - 1, 0);
179 purple_debug_warning("stun", "unable to read stun response\n");
184 if (len
< sizeof(struct stun_header
)) {
185 purple_debug_warning("stun", "got invalid response\n");
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");
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");
205 if (hdr
->type
!= MSGTYPE_BINDINGRESPONSE
) {
206 purple_debug_warning("stun",
207 "Expected Binding Response, got %d\n",
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
))))
220 if(attrib
->type
== htons(ATTRIB_MAPPEDADDRESS
)
221 && ntohs(attrib
->len
) == 8) {
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);
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
);
241 ifc
.ifc_len
= sizeof(buffer
);
242 ifc
.ifc_req
= (struct ifreq
*) buffer
;
243 ioctl(source
, SIOCGIFCONF
, &ifc
);
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
) {
256 purple_debug_info("stun", "no nat\n");
257 nattype
.type
= PURPLE_STUN_NAT_TYPE_PUBLIC_IP
;
266 purple_timeout_remove(sc
->timeout
);
270 } else if(sc
->test
== 2) {
272 nattype
.type
= PURPLE_STUN_NAT_TYPE_FULL_CONE
;
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
;
285 nattype
.status
= PURPLE_STUN_STATUS_UNKNOWN
;
286 nattype
.lookup_time
= time(NULL
);
291 sc
= g_new0(struct stun_conn
, 1);
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
));
303 hosts
= g_slist_delete_link(hosts
, hosts
);
305 hosts
= g_slist_delete_link(hosts
, hosts
);
307 hosts
= g_slist_delete_link(hosts
, hosts
);
310 hdr_data
.type
= htons(MSGTYPE_BINDINGREQUEST
);
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
);
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
);
341 if (!purple_network_listen_range(12108, 12208, SOCK_DGRAM
, hbn_listen_cb
, hosts
)) {
343 hosts
= g_slist_delete_link(hosts
, hosts
);
345 hosts
= g_slist_delete_link(hosts
, hosts
);
348 nattype
.status
= PURPLE_STUN_STATUS_UNKNOWN
;
349 nattype
.lookup_time
= time(NULL
);
357 static void do_test1(PurpleSrvResponse
*resp
, int results
, gpointer sdata
) {
358 const char *servername
= sdata
;
362 servername
= resp
[0].hostname
;
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
);
372 static gboolean
call_callback(gpointer data
) {
373 StunCallback cb
= data
;
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
) {
385 callbacks
= g_slist_append(callbacks
, cb
);
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
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
) {
408 purple_timeout_add(10, call_callback
, cb
);
413 if(!servername
|| (strlen(servername
) < 2)) {
414 nattype
.status
= PURPLE_STUN_STATUS_UNKNOWN
;
415 nattype
.lookup_time
= time(NULL
);
417 purple_timeout_add(10, call_callback
, cb
);
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
);
433 void purple_stun_init() {
434 purple_prefs_add_string("/purple/network/stun_server", "");