Mercurial > pidgin
annotate src/stun.c @ 13623:51d436a267ac
[gaim-migrate @ 16009]
Use the GSlice allocator (for logs and log sets) when compiling with glib 2.10 or greater and eliminate some unnecessary memory zeroing. This cuts the log loading time in half for one of Luke's contacts.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Tue, 11 Apr 2006 16:06:24 +0000 |
parents | 8e3b85fe4a55 |
children | c3167a1dd817 |
rev | line source |
---|---|
11225 | 1 /** |
2 * @file stun.c STUN (RFC3489) Implementation | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
11336 | 7 * STUN implementation inspired by jstun [http://jstun.javawi.de/] |
8 * | |
11225 | 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 | |
11898 | 29 #include "internal.h" |
30 | |
11364
e1840eb860e7
[gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11354
diff
changeset
|
31 #ifndef _WIN32 |
11225 | 32 #include <net/if.h> |
33 #include <sys/ioctl.h> | |
11364
e1840eb860e7
[gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11354
diff
changeset
|
34 #endif |
11225 | 35 |
12700
cf0d9e207cdc
[gaim-migrate @ 15043]
Richard Laager <rlaager@wiktel.com>
parents:
12686
diff
changeset
|
36 /* Solaris */ |
cf0d9e207cdc
[gaim-migrate @ 15043]
Richard Laager <rlaager@wiktel.com>
parents:
12686
diff
changeset
|
37 #if defined (__SVR4) && defined (__sun) |
cf0d9e207cdc
[gaim-migrate @ 15043]
Richard Laager <rlaager@wiktel.com>
parents:
12686
diff
changeset
|
38 #include <sys/sockio.h> |
cf0d9e207cdc
[gaim-migrate @ 15043]
Richard Laager <rlaager@wiktel.com>
parents:
12686
diff
changeset
|
39 #endif |
cf0d9e207cdc
[gaim-migrate @ 15043]
Richard Laager <rlaager@wiktel.com>
parents:
12686
diff
changeset
|
40 |
11225 | 41 #include "debug.h" |
42 #include "account.h" | |
11424 | 43 #include "dnssrv.h" |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
44 #include "network.h" |
11429 | 45 #include "proxy.h" |
11225 | 46 #include "stun.h" |
47 #include "prefs.h" | |
48 | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
49 #define MSGTYPE_BINDINGREQUEST 0x0001 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
50 #define MSGTYPE_BINDINGRESPONSE 0x0101 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
51 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
52 #define ATTRIB_MAPPEDADDRESS 0x0001 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
53 |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
54 struct stun_header { |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
55 guint16 type; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
56 guint16 len; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
57 guint32 transid[4]; |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
58 }; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
59 |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
60 struct stun_attrib { |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
61 guint16 type; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
62 guint16 len; |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
63 }; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
64 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
65 #ifdef NOTYET |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
66 struct stun_change { |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
67 struct stun_header hdr; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
68 struct stun_attrib attrib; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
69 char value[4]; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
70 }; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
71 #endif |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
72 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
73 struct stun_conn { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
74 int fd; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
75 struct sockaddr_in addr; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
76 int test; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
77 int retry; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
78 guint incb; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
79 guint timeout; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
80 struct stun_header *packet; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
81 size_t packetsize; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
82 }; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
83 |
12877
a7bf698eb37c
[gaim-migrate @ 15229]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12757
diff
changeset
|
84 static GaimStunNatDiscovery nattype = { |
a7bf698eb37c
[gaim-migrate @ 15229]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12757
diff
changeset
|
85 GAIM_STUN_STATUS_UNDISCOVERED, |
a7bf698eb37c
[gaim-migrate @ 15229]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12757
diff
changeset
|
86 GAIM_STUN_NAT_TYPE_PUBLIC_IP, |
a7bf698eb37c
[gaim-migrate @ 15229]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12757
diff
changeset
|
87 "\0", NULL, 0}; |
11225 | 88 |
12877
a7bf698eb37c
[gaim-migrate @ 15229]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12757
diff
changeset
|
89 static GSList *callbacks = NULL; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
90 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
91 static void close_stun_conn(struct stun_conn *sc) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
92 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
93 if (sc->incb) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
94 gaim_input_remove(sc->incb); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
95 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
96 if (sc->timeout) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
97 gaim_timeout_remove(sc->timeout); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
98 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
99 if (sc->fd) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
100 close(sc->fd); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
101 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
102 g_free(sc); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
103 } |
11225 | 104 |
105 static void do_callbacks() { | |
106 while(callbacks) { | |
107 StunCallback cb = callbacks->data; | |
11336 | 108 if(cb) |
109 cb(&nattype); | |
11225 | 110 callbacks = g_slist_remove(callbacks, cb); |
111 } | |
112 } | |
113 | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
114 static gboolean timeoutfunc(gpointer data) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
115 struct stun_conn *sc = data; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
116 if(sc->retry >= 2) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
117 gaim_debug_info("stun", "request timed out, giving up.\n"); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
118 if(sc->test == 2) |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
119 nattype.type = GAIM_STUN_NAT_TYPE_SYMMETRIC; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
120 |
11354 | 121 /* set unknown */ |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
122 nattype.status = GAIM_STUN_STATUS_UNKNOWN; |
11354 | 123 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
124 nattype.lookup_time = time(NULL); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
125 |
11354 | 126 /* callbacks */ |
127 do_callbacks(); | |
128 | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
129 /* we don't need to remove the timeout (returning FALSE) */ |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
130 sc->timeout = 0; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
131 close_stun_conn(sc); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
132 |
11354 | 133 return FALSE; |
134 } | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
135 gaim_debug_info("stun", "request timed out, retrying.\n"); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
136 sc->retry++; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
137 sendto(sc->fd, sc->packet, sc->packetsize, 0, |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
138 (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); |
11354 | 139 return TRUE; |
140 } | |
141 | |
142 #ifdef NOTYET | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
143 static void do_test2(struct stun_conn *sc) { |
11354 | 144 struct stun_change data; |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
145 data.hdr.type = htons(0x0001); |
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
146 data.hdr.len = 0; |
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
147 data.hdr.transid[0] = rand(); |
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
148 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
|
149 data.hdr.transid[2] = rand(); |
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
150 data.hdr.transid[3] = rand(); |
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
151 data.attrib.type = htons(0x003); |
11354 | 152 data.attrib.len = htons(4); |
153 data.value[3] = 6; | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
154 sc->packet = (struct stun_header*)&data; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
155 sc->packetsize = sizeof(struct stun_change); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
156 sc->retry = 0; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
157 sc->test = 2; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
158 sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
159 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); |
11354 | 160 } |
161 #endif | |
162 | |
11225 | 163 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
164 struct stun_conn *sc = data; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
165 char buffer[65536]; |
11225 | 166 char *tmp; |
167 int len; | |
168 struct in_addr in; | |
169 struct stun_attrib *attrib; | |
170 struct stun_header *hdr; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
171 struct ifconf ifc; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
172 struct ifreq *ifr; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
173 struct sockaddr_in *sinptr; |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
174 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
175 len = recv(source, buffer, sizeof(buffer) - 1, 0); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
176 if (!len) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
177 gaim_debug_info("stun", "unable to read stun response\n"); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
178 return; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
179 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
180 buffer[len] = '\0'; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
181 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
182 if (len < sizeof(struct stun_header)) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
183 gaim_debug_info("stun", "got invalid response\n"); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
184 return; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
185 } |
11225 | 186 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
187 hdr = (struct stun_header*) buffer; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
188 if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
189 gaim_debug_info("stun", "got incomplete response\n"); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
190 return; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
191 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
192 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
193 /* wrong transaction */ |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
194 if(hdr->transid[0] != sc->packet->transid[0] |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
195 || hdr->transid[1] != sc->packet->transid[1] |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
196 || hdr->transid[2] != sc->packet->transid[2] |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
197 || hdr->transid[3] != sc->packet->transid[3]) { |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
198 gaim_debug_info("stun", "got wrong transid\n"); |
11225 | 199 return; |
200 } | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
201 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
202 if(sc->test==1) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
203 if (hdr->type != MSGTYPE_BINDINGRESPONSE) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
204 gaim_debug_info("stun", |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
205 "Expected Binding Response, got %d\n", |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
206 hdr->type); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
207 return; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
208 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
209 |
11354 | 210 tmp = buffer + sizeof(struct stun_header); |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
211 while((buffer + len) > (tmp + sizeof(struct stun_attrib))) { |
11354 | 212 attrib = (struct stun_attrib*) tmp; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
213 tmp += sizeof(struct stun_attrib); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
214 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
215 if (!((buffer + len) > (tmp + ntohs(attrib->len)))) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
216 break; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
217 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
218 if(attrib->type == htons(ATTRIB_MAPPEDADDRESS) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
219 && ntohs(attrib->len) == 8) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
220 char *ip; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
221 /* Skip the first unused byte, |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
222 * the family(1 byte), and the port(2 bytes); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
223 * then read the 4 byte IPv4 address */ |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
224 memcpy(&in.s_addr, tmp + 4, 4); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
225 ip = inet_ntoa(in); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
226 if(ip) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
227 strcpy(nattype.publicip, ip); |
11354 | 228 } |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
229 |
12757
e2f1f49fad1a
[gaim-migrate @ 15104]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12756
diff
changeset
|
230 tmp += ntohs(attrib->len); |
11225 | 231 } |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
232 gaim_debug_info("stun", "got public ip %s\n", nattype.publicip); |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
233 nattype.status = GAIM_STUN_STATUS_DISCOVERED; |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
234 nattype.type = GAIM_STUN_NAT_TYPE_UNKNOWN_NAT; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
235 nattype.lookup_time = time(NULL); |
11354 | 236 |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
237 /* is it a NAT? */ |
11225 | 238 |
11354 | 239 ifc.ifc_len = sizeof(buffer); |
240 ifc.ifc_req = (struct ifreq *) buffer; | |
241 ioctl(source, SIOCGIFCONF, &ifc); | |
11225 | 242 |
11354 | 243 tmp = buffer; |
244 while(tmp < buffer + ifc.ifc_len) { | |
245 ifr = (struct ifreq *) tmp; | |
11225 | 246 |
11364
e1840eb860e7
[gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11354
diff
changeset
|
247 tmp += sizeof(struct ifreq); |
11225 | 248 |
11354 | 249 if(ifr->ifr_addr.sa_family == AF_INET) { |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
250 /* we only care about ipv4 interfaces */ |
11354 | 251 sinptr = (struct sockaddr_in *) &ifr->ifr_addr; |
252 if(sinptr->sin_addr.s_addr == in.s_addr) { | |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
253 /* no NAT */ |
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
254 gaim_debug_info("stun", "no nat"); |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
255 nattype.type = GAIM_STUN_NAT_TYPE_PUBLIC_IP; |
11354 | 256 } |
11225 | 257 } |
258 } | |
259 | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
260 #ifndef NOTYET |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
261 close_stun_conn(sc); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
262 do_callbacks(); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
263 #else |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
264 gaim_timeout_remove(sc->timeout); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
265 sc->timeout = 0; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
266 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
267 do_test2(sc); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
268 } else if(sc->test == 2) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
269 close_stun_conn(sc); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
270 nattype.type = GAIM_STUN_NAT_TYPE_FULL_CONE; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
271 do_callbacks(); |
11354 | 272 #endif |
273 } | |
11336 | 274 } |
11424 | 275 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
276 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
277 static void hbn_listen_cb(int fd, gpointer data) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
278 GSList *hosts = data; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
279 struct stun_conn *sc; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
280 static struct stun_header hdr_data; |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
281 int ret; |
11425 | 282 |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
283 if(fd < 0) { |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
284 nattype.status = GAIM_STUN_STATUS_UNKNOWN; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
285 nattype.lookup_time = time(NULL); |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
286 do_callbacks(); |
11424 | 287 return; |
11225 | 288 } |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
289 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
290 sc = g_new0(struct stun_conn, 1); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
291 sc->fd = fd; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
292 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
293 sc->addr.sin_family = AF_INET; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
294 sc->addr.sin_port = htons(gaim_network_get_port_from_fd(fd)); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
295 sc->addr.sin_addr.s_addr = INADDR_ANY; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
296 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
297 sc->incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, sc); |
11225 | 298 |
11429 | 299 ret = GPOINTER_TO_INT(hosts->data); |
300 hosts = g_slist_remove(hosts, hosts->data); | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
301 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in)); |
11429 | 302 g_free(hosts->data); |
303 hosts = g_slist_remove(hosts, hosts->data); | |
304 while(hosts) { | |
305 hosts = g_slist_remove(hosts, hosts->data); | |
306 g_free(hosts->data); | |
307 hosts = g_slist_remove(hosts, hosts->data); | |
308 } | |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
309 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
310 hdr_data.type = htons(MSGTYPE_BINDINGREQUEST); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
311 hdr_data.len = 0; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
312 hdr_data.transid[0] = rand(); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
313 hdr_data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
314 hdr_data.transid[2] = rand(); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
315 hdr_data.transid[3] = rand(); |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
316 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
317 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0, |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
318 (struct sockaddr *)&(sc->addr), |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
319 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
320 nattype.status = GAIM_STUN_STATUS_UNKNOWN; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
321 nattype.lookup_time = time(NULL); |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
322 do_callbacks(); |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
323 close_stun_conn(sc); |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
324 return; |
11225 | 325 } |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
326 sc->test = 1; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
327 sc->packet = &hdr_data; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
328 sc->packetsize = sizeof(struct stun_header); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
329 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); |
11429 | 330 } |
331 | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
332 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
333 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
334 if(!hosts || !hosts->data) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
335 nattype.status = GAIM_STUN_STATUS_UNDISCOVERED; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
336 nattype.lookup_time = time(NULL); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
337 do_callbacks(); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
338 return; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
339 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
340 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
341 if (!gaim_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) { |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
342 nattype.status = GAIM_STUN_STATUS_UNKNOWN; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
343 nattype.lookup_time = time(NULL); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
344 do_callbacks(); |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
345 return; |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
346 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
347 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
348 |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
349 } |
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
350 |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
351 static void do_test1(GaimSrvResponse *resp, int results, gpointer sdata) { |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
352 const char *servername = sdata; |
11429 | 353 int port = 3478; |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
354 |
11429 | 355 if(results) { |
356 servername = resp[0].hostname; | |
357 port = resp[0].port; | |
358 } | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
359 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
360 results, servername, port); |
11429 | 361 |
362 gaim_gethostbyname_async(servername, port, hbn_cb, NULL); | |
11424 | 363 g_free(resp); |
11225 | 364 } |
11424 | 365 |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
366 static gboolean call_callback(gpointer data) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
367 StunCallback cb = data; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
368 cb(&nattype); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
369 return FALSE; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
370 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
371 |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
372 GaimStunNatDiscovery *gaim_stun_discover(StunCallback cb) { |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
373 const char *servername = gaim_prefs_get_string("/core/network/stun_server"); |
11424 | 374 |
375 gaim_debug_info("stun", "using server %s\n", servername); | |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
376 |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
377 if(nattype.status == GAIM_STUN_STATUS_DISCOVERING) { |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
378 if(cb) |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
379 callbacks = g_slist_append(callbacks, cb); |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
380 return &nattype; |
11424 | 381 } |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
382 |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
383 if(nattype.status != GAIM_STUN_STATUS_UNDISCOVERED) { |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
384 gboolean use_cached_result = TRUE; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
385 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
386 /** Deal with the server name having changed since we did the |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
387 lookup */ |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
388 if (servername && strlen(servername) > 1 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
389 && ((nattype.servername |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
390 && strcmp(servername, nattype.servername)) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
391 || !nattype.servername)) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
392 use_cached_result = FALSE; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
393 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
394 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
395 /* If we don't have a successful status and it has been 5 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
396 minutes since we last did a lookup, redo the lookup */ |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
397 if (nattype.status != GAIM_STUN_STATUS_DISCOVERED |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
398 && (time(NULL) - nattype.lookup_time) > 300) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
399 use_cached_result = FALSE; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
400 } |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
401 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
402 if (use_cached_result) { |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
403 if(cb) |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
404 gaim_timeout_add(10, call_callback, cb); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
405 return &nattype; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
406 } |
11424 | 407 } |
408 | |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
409 if(!servername || (strlen(servername) < 2)) { |
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
410 nattype.status = GAIM_STUN_STATUS_UNKNOWN; |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
411 nattype.lookup_time = time(NULL); |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
412 if(cb) |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
413 gaim_timeout_add(10, call_callback, cb); |
11424 | 414 return &nattype; |
415 } | |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
416 |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
417 nattype.status = GAIM_STUN_STATUS_DISCOVERING; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
418 nattype.publicip[0] = '\0'; |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
419 g_free(nattype.servername); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
420 nattype.servername = g_strdup(servername); |
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
421 |
11424 | 422 callbacks = g_slist_append(callbacks, cb); |
12686
5f65a0cca87c
[gaim-migrate @ 15029]
Richard Laager <rlaager@wiktel.com>
parents:
11898
diff
changeset
|
423 gaim_srv_resolve("stun", "udp", servername, do_test1, |
11431
d2e44c8085e0
[gaim-migrate @ 13668]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11429
diff
changeset
|
424 (gpointer) servername); |
12756
6ef1cdc26b40
[gaim-migrate @ 15103]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12700
diff
changeset
|
425 |
11424 | 426 return &nattype; |
427 } | |
428 | |
429 void gaim_stun_init() { | |
430 gaim_prefs_add_string("/core/network/stun_server", ""); | |
12909
8e3b85fe4a55
[gaim-migrate @ 15262]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12877
diff
changeset
|
431 gaim_stun_discover(NULL); |
11424 | 432 } |