annotate src/stun.c @ 11476:5d3f8d9e8f92

[gaim-migrate @ 13718] Kevin Stange requested this of me... committer: Tailor Script <tailor@pidgin.im>
author Gary Kramlich <grim@reaperworld.com>
date Fri, 09 Sep 2005 05:06:18 +0000
parents d2e44c8085e0
children f18930698b5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
1 /**
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
2 * @file stun.c STUN (RFC3489) Implementation
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
3 * @ingroup core
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
4 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
5 * gaim
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
6 *
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
7 * STUN implementation inspired by jstun [http://jstun.javawi.de/]
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
8 *
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
9 * Gaim is the legal property of its developers, whose names are too numerous
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
10 * to list here. Please refer to the COPYRIGHT file distributed with this
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
11 * source distribution.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
12 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
13 * This program is free software; you can redistribute it and/or modify
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
14 * it under the terms of the GNU General Public License as published by
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
15 * the Free Software Foundation; either version 2 of the License, or
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
16 * (at your option) any later version.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
17 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
18 * This program is distributed in the hope that it will be useful,
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
21 * GNU General Public License for more details.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
22 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
23 * You should have received a copy of the GNU General Public License
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
24 * along with this program; if not, write to the Free Software
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
26 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
27 */
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
28
11364
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
29 #ifndef _WIN32
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
30 #include <sys/socket.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
31 #include <ifaddrs.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
32 #include <net/if.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
33 #include <sys/ioctl.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
34 #include <resolv.h>
11364
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
35 #else
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
36 #include "libc_interface.h"
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
37 #endif
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
38
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
39 #include "internal.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
40
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
41 #include "debug.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
42 #include "account.h"
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
43 #include "dnssrv.h"
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
44 #include "proxy.h"
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
45 #include "stun.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
46 #include "prefs.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
47
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
48 struct stun_nattype nattype = {-1, 0, "\0"};
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
49
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
50 static GSList *callbacks = 0;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
51 static int fd = -1;
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
52 static gint incb = -1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
53 static gint timeout = -1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
54 static struct stun_header *packet;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
55 static int packetsize = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
56 static int test = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
57 static int retry = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
58 static struct sockaddr_in addr;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
59
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
60 static void do_callbacks() {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
61 while(callbacks) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
62 StunCallback cb = callbacks->data;
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
63 if(cb)
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
64 cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
65 callbacks = g_slist_remove(callbacks, cb);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
66 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
67 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
68
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
69 static gboolean timeoutfunc(void *blah) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
70 if(retry > 2) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
71 if(test == 2) nattype.type = 5;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
72 /* remove input */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
73 gaim_input_remove(incb);
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
74
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
75 /* set unknown */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
76 nattype.status = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
77
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
78 /* callbacks */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
79 do_callbacks();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
80
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
81 return FALSE;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
82 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
83 retry++;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
84 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
85 return TRUE;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
86 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
87
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
88 #ifdef NOTYET
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
89 static void do_test2() {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
90 struct stun_change data;
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
91 data.hdr.type = htons(0x0001);
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
92 data.hdr.len = 0;
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
93 data.hdr.transid[0] = rand();
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
94 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
95 data.hdr.transid[2] = rand();
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
96 data.hdr.transid[3] = rand();
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
97 data.attrib.type = htons(0x003);
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
98 data.attrib.len = htons(4);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
99 data.value[3] = 6;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
100 packet = (struct stun_header*)&data;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
101 packetsize = sizeof(struct stun_change);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
102 retry = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
103 test = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
104 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
105 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
106 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
107 #endif
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
108
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
109 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) {
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
110 char buffer[1024];
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
111 char *tmp;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
112 int len;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
113 struct in_addr in;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
114 struct stun_attrib *attrib;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
115 struct stun_header *hdr;
11300
dd1a5969b2e5 [gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents: 11229
diff changeset
116 struct ifconf ifc;
dd1a5969b2e5 [gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents: 11229
diff changeset
117 struct ifreq *ifr;
dd1a5969b2e5 [gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents: 11229
diff changeset
118 struct sockaddr_in *sinptr;
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
119
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
120 len = recv(source, buffer, 1024, 0);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
121
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
122 hdr = (struct stun_header*)buffer;
11371
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
123 if(hdr->transid[0]!=packet->transid[0] || hdr->transid[1]!=packet->transid[1] || hdr->transid[2]!=packet->transid[2] || hdr->transid[3]!=packet->transid[3]) { /* wrong transaction */
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
124 gaim_debug_info("stun", "got wrong transid\n");
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
125 return;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
126 }
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
127 if(test==1) {
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
128 tmp = buffer + sizeof(struct stun_header);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
129 while(buffer+len > tmp) {
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
130
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
131 attrib = (struct stun_attrib*) tmp;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
132 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
133 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
134 strcpy(nattype.publicip, inet_ntoa(in));
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
135 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
136 tmp += sizeof(struct stun_attrib) + attrib->len;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
137 }
11371
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
138 gaim_debug_info("stun", "got public ip %s\n", nattype.publicip);
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
139 nattype.status = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
140 nattype.type = 1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
141
11371
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
142 /* is it a NAT? */
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
143
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
144 ifc.ifc_len = sizeof(buffer);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
145 ifc.ifc_req = (struct ifreq *) buffer;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
146 ioctl(source, SIOCGIFCONF, &ifc);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
147
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
148 tmp = buffer;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
149 while(tmp < buffer + ifc.ifc_len) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
150 ifr = (struct ifreq *) tmp;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
151
11364
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
152 tmp += sizeof(struct ifreq);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
153
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
154 if(ifr->ifr_addr.sa_family == AF_INET) {
11371
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
155 /* we only care about ipv4 interfaces */
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
156 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
157 if(sinptr->sin_addr.s_addr == in.s_addr) {
11371
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
158 /* no NAT */
6e02e20e3a58 [gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents: 11364
diff changeset
159 gaim_debug_info("stun", "no nat");
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
160 nattype.type = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
161 }
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
162 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
163 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
164 gaim_timeout_remove(timeout);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
165
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
166 #ifdef NOTYET
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
167 do_test2();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
168 #endif
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
169 return;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
170 } else if(test == 2) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
171 do_callbacks();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
172 gaim_input_remove(incb);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
173 gaim_timeout_remove(timeout);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
174 nattype.type = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
175 }
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
176 }
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
177
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
178 static void hbn_cb(GSList *hosts, gpointer edata, const char *error_message) {
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
179 static struct stun_header data;
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
180 int ret;
11425
54fa445aff32 [gaim-migrate @ 13662]
Thomas Butter <tbutter>
parents: 11424
diff changeset
181
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
182 if(!hosts) return;
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
183 if(!hosts->data) return;
11425
54fa445aff32 [gaim-migrate @ 13662]
Thomas Butter <tbutter>
parents: 11424
diff changeset
184
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
185 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
186 nattype.status = 0;
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
187 do_callbacks();
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
188 return;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
189 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
190
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
191 addr.sin_family = AF_INET;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
192 addr.sin_port = htons(12108);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
193 addr.sin_addr.s_addr = INADDR_ANY;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
194 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
195 addr.sin_port = htons(ntohs(addr.sin_port)+1);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
196 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
197 if( ret < 0 ) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
198 nattype.status = 0;
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
199 do_callbacks();
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
200 return;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
201 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
202 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
203
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
204 ret = GPOINTER_TO_INT(hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
205 hosts = g_slist_remove(hosts, hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
206 memcpy(&addr, hosts->data, sizeof(struct sockaddr_in));
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
207 g_free(hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
208 hosts = g_slist_remove(hosts, hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
209 while(hosts) {
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
210 hosts = g_slist_remove(hosts, hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
211 g_free(hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
212 hosts = g_slist_remove(hosts, hosts->data);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
213 }
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
214
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
215 data.type = htons(0x0001);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
216 data.len = 0;
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
217 data.transid[0] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
218 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
219 data.transid[2] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
220 data.transid[3] = rand();
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
221
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
222 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
223 nattype.status = 0;
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
224 do_callbacks();
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
225 return;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
226 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
227 test = 1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
228 packet = &data;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
229 packetsize = sizeof(struct stun_header);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
230 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
231 }
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
232
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
233 static void do_test1(struct srv_response *resp, int results, gpointer sdata) {
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
234 const char *servername = sdata;
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
235 int port = 3478;
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
236
11429
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
237 if(results) {
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
238 servername = resp[0].hostname;
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
239 port = resp[0].port;
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
240 }
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
241 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port);
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
242
835dbb1b3337 [gaim-migrate @ 13666]
Thomas Butter <tbutter>
parents: 11425
diff changeset
243 gaim_gethostbyname_async(servername, port, hbn_cb, NULL);
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
244 g_free(resp);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
245 }
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
246
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
247 struct stun_nattype *gaim_stun_discover(StunCallback cb) {
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
248 const char *servername = gaim_prefs_get_string("/core/network/stun_server");
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
249
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
250 gaim_debug_info("stun", "using server %s\n", servername);
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
251 if(nattype.status == 1) { /* currently discovering */
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
252 if(cb) callbacks = g_slist_append(callbacks, cb);
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
253 return NULL;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
254 }
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
255 if(nattype.status != -1) { /* already discovered */
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
256 if(cb) cb(&nattype);
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
257 return &nattype;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
258 }
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
259
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
260 if(!servername || (strlen(servername)<2)) {
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
261 nattype.status = 0;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
262 if(cb) cb(&nattype);
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
263 return &nattype;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
264 }
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
265 callbacks = g_slist_append(callbacks, cb);
11431
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
266 gaim_srv_resolve("stun","udp",servername, do_test1,
d2e44c8085e0 [gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11429
diff changeset
267 (gpointer) servername);
11424
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
268 return &nattype;
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
269 }
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
270
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
271 void gaim_stun_init() {
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
272 gaim_prefs_add_string("/core/network/stun_server", "");
e1ab173ef3b5 [gaim-migrate @ 13661]
Thomas Butter <tbutter>
parents: 11371
diff changeset
273 }