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