Mercurial > pidgin.yaz
annotate src/stun.c @ 11424:e1ab173ef3b5
[gaim-migrate @ 13661]
prefs for STUN
adjustments for NTLM in SIP
committer: Tailor Script <tailor@pidgin.im>
author | Thomas Butter <tbutter> |
---|---|
date | Fri, 02 Sep 2005 09:09:04 +0000 |
parents | 6e02e20e3a58 |
children | 54fa445aff32 |
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 | |
11364
e1840eb860e7
[gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11354
diff
changeset
|
29 #ifndef _WIN32 |
11225 | 30 #include <sys/socket.h> |
31 #include <ifaddrs.h> | |
32 #include <net/if.h> | |
33 #include <sys/ioctl.h> | |
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 | 38 |
39 #include "internal.h" | |
40 | |
41 #include "debug.h" | |
42 #include "account.h" | |
11424 | 43 #include "dnssrv.h" |
11225 | 44 #include "stun.h" |
45 #include "prefs.h" | |
46 | |
47 struct stun_nattype nattype = {-1, 0, "\0"}; | |
48 | |
49 static GSList *callbacks = 0; | |
50 static int fd = -1; | |
11354 | 51 static gint incb = -1; |
52 static gint timeout = -1; | |
53 static struct stun_header *packet; | |
54 static int packetsize = 0; | |
55 static int test = 0; | |
56 static int retry = 0; | |
57 static struct sockaddr_in addr; | |
11225 | 58 |
59 static void do_callbacks() { | |
60 while(callbacks) { | |
61 StunCallback cb = callbacks->data; | |
11336 | 62 if(cb) |
63 cb(&nattype); | |
11225 | 64 callbacks = g_slist_remove(callbacks, cb); |
65 } | |
66 } | |
67 | |
11354 | 68 static gboolean timeoutfunc(void *blah) { |
69 if(retry > 2) { | |
70 if(test == 2) nattype.type = 5; | |
71 /* remove input */ | |
72 gaim_input_remove(incb); | |
73 | |
74 /* set unknown */ | |
75 nattype.status = 0; | |
76 | |
77 /* callbacks */ | |
78 do_callbacks(); | |
79 | |
80 return FALSE; | |
81 } | |
82 retry++; | |
83 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); | |
84 return TRUE; | |
85 } | |
86 | |
87 #ifdef NOTYET | |
88 static void do_test2() { | |
89 struct stun_change data; | |
90 data.hdr.type = htons(0x0001); | |
91 data.hdr.len = 0; | |
92 data.hdr.transid[0] = rand(); | |
93 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | |
94 data.hdr.transid[2] = rand(); | |
95 data.hdr.transid[3] = rand(); data.attrib.type = htons(0x003); | |
96 data.attrib.len = htons(4); | |
97 data.value[3] = 6; | |
98 packet = (struct stun_header*)&data; | |
99 packetsize = sizeof(struct stun_change); | |
100 retry = 0; | |
101 test = 2; | |
102 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); | |
103 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); | |
104 } | |
105 #endif | |
106 | |
11225 | 107 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { |
11336 | 108 char buffer[1024]; |
11225 | 109 char *tmp; |
110 int len; | |
111 struct in_addr in; | |
112 struct stun_attrib *attrib; | |
113 struct stun_header *hdr; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
114 struct ifconf ifc; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
115 struct ifreq *ifr; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
116 struct sockaddr_in *sinptr; |
11225 | 117 |
11336 | 118 len = recv(source, buffer, 1024, 0); |
11225 | 119 |
120 hdr = (struct stun_header*)buffer; | |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
121 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
|
122 gaim_debug_info("stun", "got wrong transid\n"); |
11225 | 123 return; |
124 } | |
11354 | 125 if(test==1) { |
126 tmp = buffer + sizeof(struct stun_header); | |
127 while(buffer+len > tmp) { | |
11225 | 128 |
11354 | 129 attrib = (struct stun_attrib*) tmp; |
130 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) { | |
131 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4); | |
132 strcpy(nattype.publicip, inet_ntoa(in)); | |
133 } | |
134 tmp += sizeof(struct stun_attrib) + attrib->len; | |
11225 | 135 } |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
136 gaim_debug_info("stun", "got public ip %s\n", nattype.publicip); |
11354 | 137 nattype.status = 2; |
138 nattype.type = 1; | |
139 | |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
140 /* is it a NAT? */ |
11225 | 141 |
11354 | 142 ifc.ifc_len = sizeof(buffer); |
143 ifc.ifc_req = (struct ifreq *) buffer; | |
144 ioctl(source, SIOCGIFCONF, &ifc); | |
11225 | 145 |
11354 | 146 tmp = buffer; |
147 while(tmp < buffer + ifc.ifc_len) { | |
148 ifr = (struct ifreq *) tmp; | |
11225 | 149 |
11364
e1840eb860e7
[gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11354
diff
changeset
|
150 tmp += sizeof(struct ifreq); |
11225 | 151 |
11354 | 152 if(ifr->ifr_addr.sa_family == AF_INET) { |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
153 /* we only care about ipv4 interfaces */ |
11354 | 154 sinptr = (struct sockaddr_in *) &ifr->ifr_addr; |
155 if(sinptr->sin_addr.s_addr == in.s_addr) { | |
11371
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
156 /* no NAT */ |
6e02e20e3a58
[gaim-migrate @ 13596]
Richard Laager <rlaager@wiktel.com>
parents:
11364
diff
changeset
|
157 gaim_debug_info("stun", "no nat"); |
11354 | 158 nattype.type = 0; |
159 } | |
11225 | 160 } |
161 } | |
11354 | 162 gaim_timeout_remove(timeout); |
11225 | 163 |
11354 | 164 #ifdef NOTYET |
165 do_test2(); | |
166 #endif | |
167 return; | |
168 } else if(test == 2) { | |
169 do_callbacks(); | |
170 gaim_input_remove(incb); | |
171 gaim_timeout_remove(timeout); | |
172 nattype.type = 2; | |
173 } | |
11336 | 174 } |
11424 | 175 |
176 static void do_test1(struct srv_response *resp, int results, gpointer sdata) { | |
177 char *servername = (char*)sdata; | |
11354 | 178 static struct stun_header data; |
11424 | 179 int port = 3478; |
180 int ret; | |
181 struct hostent *host; | |
182 | |
183 if(results) { | |
184 servername = resp[0].hostname; | |
185 port = resp[0].port; | |
11336 | 186 } |
11424 | 187 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); |
188 if(!host->h_addr_list) { | |
189 return; | |
11225 | 190 } |
11424 | 191 host = gethostbyname(servername); |
192 | |
11225 | 193 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
194 nattype.status = 0; | |
11424 | 195 do_callbacks(); |
196 return; | |
11225 | 197 } |
198 | |
199 addr.sin_family = AF_INET; | |
200 addr.sin_port = htons(12108); | |
201 addr.sin_addr.s_addr = INADDR_ANY; | |
202 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) { | |
203 addr.sin_port = htons(ntohs(addr.sin_port)+1); | |
204 } | |
205 if( ret < 0 ) { | |
206 nattype.status = 0; | |
11424 | 207 do_callbacks(); |
208 return; | |
11225 | 209 } |
210 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); | |
211 | |
212 addr.sin_family = AF_INET; | |
213 addr.sin_port = htons(port); | |
11424 | 214 memcpy(&addr.sin_addr.s_addr,*(host->h_addr_list),4); |
11225 | 215 |
216 data.type = htons(0x0001); | |
217 data.len = 0; | |
11354 | 218 data.transid[0] = rand(); |
219 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | |
220 data.transid[2] = rand(); | |
221 data.transid[3] = rand(); | |
11225 | 222 |
223 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { | |
224 nattype.status = 0; | |
11424 | 225 do_callbacks(); |
226 return; | |
11225 | 227 } |
11354 | 228 test = 1; |
229 packet = &data; | |
230 packetsize = sizeof(struct stun_header); | |
231 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); | |
11424 | 232 g_free(resp); |
11225 | 233 } |
11424 | 234 |
235 struct stun_nattype *gaim_stun_discover(StunCallback cb) { | |
236 const char *servername = gaim_prefs_get_string("/core/network/stun_server"); | |
237 | |
238 gaim_debug_info("stun", "using server %s\n", servername); | |
239 if(nattype.status == 1) { /* currently discovering */ | |
240 if(cb) callbacks = g_slist_append(callbacks, cb); | |
241 return NULL; | |
242 } | |
243 if(nattype.status != -1) { /* already discovered */ | |
244 if(cb) cb(&nattype); | |
245 return &nattype; | |
246 } | |
247 | |
248 if(!servername || (strlen(servername)<2)) { | |
249 nattype.status = 0; | |
250 if(cb) cb(&nattype); | |
251 return &nattype; | |
252 } | |
253 callbacks = g_slist_append(callbacks, cb); | |
254 gaim_srv_resolve("stun","udp",servername, do_test1, servername); | |
255 return &nattype; | |
256 } | |
257 | |
258 void gaim_stun_init() { | |
259 gaim_prefs_add_string("/core/network/stun_server", ""); | |
260 } |