Mercurial > pidgin.yaz
comparison libpurple/stun.c @ 15823:32c366eeeb99
sed -ie 's/gaim/purple/g'
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Mon, 19 Mar 2007 07:01:17 +0000 |
parents | 5fe8042783c1 |
children | 4999bbc52881 |
comparison
equal
deleted
inserted
replaced
15822:84b0f9b23ede | 15823:32c366eeeb99 |
---|---|
1 /** | 1 /** |
2 * @file stun.c STUN (RFC3489) Implementation | 2 * @file stun.c STUN (RFC3489) Implementation |
3 * @ingroup core | 3 * @ingroup core |
4 * | 4 * |
5 * gaim | 5 * purple |
6 * | 6 * |
7 * STUN implementation inspired by jstun [http://jstun.javawi.de/] | 7 * STUN implementation inspired by jstun [http://jstun.javawi.de/] |
8 * | 8 * |
9 * Gaim is the legal property of its developers, whose names are too numerous | 9 * Purple 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 | 10 * to list here. Please refer to the COPYRIGHT file distributed with this |
11 * source distribution. | 11 * source distribution. |
12 * | 12 * |
13 * This program is free software; you can redistribute it and/or modify | 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 | 14 * it under the terms of the GNU General Public License as published by |
80 guint timeout; | 80 guint timeout; |
81 struct stun_header *packet; | 81 struct stun_header *packet; |
82 size_t packetsize; | 82 size_t packetsize; |
83 }; | 83 }; |
84 | 84 |
85 static GaimStunNatDiscovery nattype = { | 85 static PurpleStunNatDiscovery nattype = { |
86 GAIM_STUN_STATUS_UNDISCOVERED, | 86 PURPLE_STUN_STATUS_UNDISCOVERED, |
87 GAIM_STUN_NAT_TYPE_PUBLIC_IP, | 87 PURPLE_STUN_NAT_TYPE_PUBLIC_IP, |
88 "\0", NULL, 0}; | 88 "\0", NULL, 0}; |
89 | 89 |
90 static GSList *callbacks = NULL; | 90 static GSList *callbacks = NULL; |
91 | 91 |
92 static void close_stun_conn(struct stun_conn *sc) { | 92 static void close_stun_conn(struct stun_conn *sc) { |
93 | 93 |
94 if (sc->incb) | 94 if (sc->incb) |
95 gaim_input_remove(sc->incb); | 95 purple_input_remove(sc->incb); |
96 | 96 |
97 if (sc->timeout) | 97 if (sc->timeout) |
98 gaim_timeout_remove(sc->timeout); | 98 purple_timeout_remove(sc->timeout); |
99 | 99 |
100 if (sc->fd) | 100 if (sc->fd) |
101 close(sc->fd); | 101 close(sc->fd); |
102 | 102 |
103 g_free(sc); | 103 g_free(sc); |
113 } | 113 } |
114 | 114 |
115 static gboolean timeoutfunc(gpointer data) { | 115 static gboolean timeoutfunc(gpointer data) { |
116 struct stun_conn *sc = data; | 116 struct stun_conn *sc = data; |
117 if(sc->retry >= 2) { | 117 if(sc->retry >= 2) { |
118 gaim_debug_info("stun", "request timed out, giving up.\n"); | 118 purple_debug_info("stun", "request timed out, giving up.\n"); |
119 if(sc->test == 2) | 119 if(sc->test == 2) |
120 nattype.type = GAIM_STUN_NAT_TYPE_SYMMETRIC; | 120 nattype.type = PURPLE_STUN_NAT_TYPE_SYMMETRIC; |
121 | 121 |
122 /* set unknown */ | 122 /* set unknown */ |
123 nattype.status = GAIM_STUN_STATUS_UNKNOWN; | 123 nattype.status = PURPLE_STUN_STATUS_UNKNOWN; |
124 | 124 |
125 nattype.lookup_time = time(NULL); | 125 nattype.lookup_time = time(NULL); |
126 | 126 |
127 /* callbacks */ | 127 /* callbacks */ |
128 do_callbacks(); | 128 do_callbacks(); |
131 sc->timeout = 0; | 131 sc->timeout = 0; |
132 close_stun_conn(sc); | 132 close_stun_conn(sc); |
133 | 133 |
134 return FALSE; | 134 return FALSE; |
135 } | 135 } |
136 gaim_debug_info("stun", "request timed out, retrying.\n"); | 136 purple_debug_info("stun", "request timed out, retrying.\n"); |
137 sc->retry++; | 137 sc->retry++; |
138 sendto(sc->fd, sc->packet, sc->packetsize, 0, | 138 sendto(sc->fd, sc->packet, sc->packetsize, 0, |
139 (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); | 139 (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); |
140 return TRUE; | 140 return TRUE; |
141 } | 141 } |
155 sc->packet = (struct stun_header*)&data; | 155 sc->packet = (struct stun_header*)&data; |
156 sc->packetsize = sizeof(struct stun_change); | 156 sc->packetsize = sizeof(struct stun_change); |
157 sc->retry = 0; | 157 sc->retry = 0; |
158 sc->test = 2; | 158 sc->test = 2; |
159 sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); | 159 sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); |
160 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); | 160 sc->timeout = purple_timeout_add(500, (GSourceFunc) timeoutfunc, sc); |
161 } | 161 } |
162 #endif | 162 #endif |
163 | 163 |
164 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { | 164 static void reply_cb(gpointer data, gint source, PurpleInputCondition cond) { |
165 struct stun_conn *sc = data; | 165 struct stun_conn *sc = data; |
166 char buffer[65536]; | 166 char buffer[65536]; |
167 char *tmp; | 167 char *tmp; |
168 int len; | 168 int len; |
169 struct in_addr in; | 169 struct in_addr in; |
173 struct ifreq *ifr; | 173 struct ifreq *ifr; |
174 struct sockaddr_in *sinptr; | 174 struct sockaddr_in *sinptr; |
175 | 175 |
176 len = recv(source, buffer, sizeof(buffer) - 1, 0); | 176 len = recv(source, buffer, sizeof(buffer) - 1, 0); |
177 if (!len) { | 177 if (!len) { |
178 gaim_debug_info("stun", "unable to read stun response\n"); | 178 purple_debug_info("stun", "unable to read stun response\n"); |
179 return; | 179 return; |
180 } | 180 } |
181 buffer[len] = '\0'; | 181 buffer[len] = '\0'; |
182 | 182 |
183 if (len < sizeof(struct stun_header)) { | 183 if (len < sizeof(struct stun_header)) { |
184 gaim_debug_info("stun", "got invalid response\n"); | 184 purple_debug_info("stun", "got invalid response\n"); |
185 return; | 185 return; |
186 } | 186 } |
187 | 187 |
188 hdr = (struct stun_header*) buffer; | 188 hdr = (struct stun_header*) buffer; |
189 if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) { | 189 if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) { |
190 gaim_debug_info("stun", "got incomplete response\n"); | 190 purple_debug_info("stun", "got incomplete response\n"); |
191 return; | 191 return; |
192 } | 192 } |
193 | 193 |
194 /* wrong transaction */ | 194 /* wrong transaction */ |
195 if(hdr->transid[0] != sc->packet->transid[0] | 195 if(hdr->transid[0] != sc->packet->transid[0] |
196 || hdr->transid[1] != sc->packet->transid[1] | 196 || hdr->transid[1] != sc->packet->transid[1] |
197 || hdr->transid[2] != sc->packet->transid[2] | 197 || hdr->transid[2] != sc->packet->transid[2] |
198 || hdr->transid[3] != sc->packet->transid[3]) { | 198 || hdr->transid[3] != sc->packet->transid[3]) { |
199 gaim_debug_info("stun", "got wrong transid\n"); | 199 purple_debug_info("stun", "got wrong transid\n"); |
200 return; | 200 return; |
201 } | 201 } |
202 | 202 |
203 if(sc->test==1) { | 203 if(sc->test==1) { |
204 if (hdr->type != MSGTYPE_BINDINGRESPONSE) { | 204 if (hdr->type != MSGTYPE_BINDINGRESPONSE) { |
205 gaim_debug_info("stun", | 205 purple_debug_info("stun", |
206 "Expected Binding Response, got %d\n", | 206 "Expected Binding Response, got %d\n", |
207 hdr->type); | 207 hdr->type); |
208 return; | 208 return; |
209 } | 209 } |
210 | 210 |
228 strcpy(nattype.publicip, ip); | 228 strcpy(nattype.publicip, ip); |
229 } | 229 } |
230 | 230 |
231 tmp += ntohs(attrib->len); | 231 tmp += ntohs(attrib->len); |
232 } | 232 } |
233 gaim_debug_info("stun", "got public ip %s\n", nattype.publicip); | 233 purple_debug_info("stun", "got public ip %s\n", nattype.publicip); |
234 nattype.status = GAIM_STUN_STATUS_DISCOVERED; | 234 nattype.status = PURPLE_STUN_STATUS_DISCOVERED; |
235 nattype.type = GAIM_STUN_NAT_TYPE_UNKNOWN_NAT; | 235 nattype.type = PURPLE_STUN_NAT_TYPE_UNKNOWN_NAT; |
236 nattype.lookup_time = time(NULL); | 236 nattype.lookup_time = time(NULL); |
237 | 237 |
238 /* is it a NAT? */ | 238 /* is it a NAT? */ |
239 | 239 |
240 ifc.ifc_len = sizeof(buffer); | 240 ifc.ifc_len = sizeof(buffer); |
250 if(ifr->ifr_addr.sa_family == AF_INET) { | 250 if(ifr->ifr_addr.sa_family == AF_INET) { |
251 /* we only care about ipv4 interfaces */ | 251 /* we only care about ipv4 interfaces */ |
252 sinptr = (struct sockaddr_in *) &ifr->ifr_addr; | 252 sinptr = (struct sockaddr_in *) &ifr->ifr_addr; |
253 if(sinptr->sin_addr.s_addr == in.s_addr) { | 253 if(sinptr->sin_addr.s_addr == in.s_addr) { |
254 /* no NAT */ | 254 /* no NAT */ |
255 gaim_debug_info("stun", "no nat"); | 255 purple_debug_info("stun", "no nat"); |
256 nattype.type = GAIM_STUN_NAT_TYPE_PUBLIC_IP; | 256 nattype.type = PURPLE_STUN_NAT_TYPE_PUBLIC_IP; |
257 } | 257 } |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 #ifndef NOTYET | 261 #ifndef NOTYET |
262 close_stun_conn(sc); | 262 close_stun_conn(sc); |
263 do_callbacks(); | 263 do_callbacks(); |
264 #else | 264 #else |
265 gaim_timeout_remove(sc->timeout); | 265 purple_timeout_remove(sc->timeout); |
266 sc->timeout = 0; | 266 sc->timeout = 0; |
267 | 267 |
268 do_test2(sc); | 268 do_test2(sc); |
269 } else if(sc->test == 2) { | 269 } else if(sc->test == 2) { |
270 close_stun_conn(sc); | 270 close_stun_conn(sc); |
271 nattype.type = GAIM_STUN_NAT_TYPE_FULL_CONE; | 271 nattype.type = PURPLE_STUN_NAT_TYPE_FULL_CONE; |
272 do_callbacks(); | 272 do_callbacks(); |
273 #endif | 273 #endif |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
280 struct stun_conn *sc; | 280 struct stun_conn *sc; |
281 static struct stun_header hdr_data; | 281 static struct stun_header hdr_data; |
282 int ret; | 282 int ret; |
283 | 283 |
284 if(fd < 0) { | 284 if(fd < 0) { |
285 nattype.status = GAIM_STUN_STATUS_UNKNOWN; | 285 nattype.status = PURPLE_STUN_STATUS_UNKNOWN; |
286 nattype.lookup_time = time(NULL); | 286 nattype.lookup_time = time(NULL); |
287 do_callbacks(); | 287 do_callbacks(); |
288 return; | 288 return; |
289 } | 289 } |
290 | 290 |
291 sc = g_new0(struct stun_conn, 1); | 291 sc = g_new0(struct stun_conn, 1); |
292 sc->fd = fd; | 292 sc->fd = fd; |
293 | 293 |
294 sc->addr.sin_family = AF_INET; | 294 sc->addr.sin_family = AF_INET; |
295 sc->addr.sin_port = htons(gaim_network_get_port_from_fd(fd)); | 295 sc->addr.sin_port = htons(purple_network_get_port_from_fd(fd)); |
296 sc->addr.sin_addr.s_addr = INADDR_ANY; | 296 sc->addr.sin_addr.s_addr = INADDR_ANY; |
297 | 297 |
298 sc->incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, sc); | 298 sc->incb = purple_input_add(fd, PURPLE_INPUT_READ, reply_cb, sc); |
299 | 299 |
300 ret = GPOINTER_TO_INT(hosts->data); | 300 ret = GPOINTER_TO_INT(hosts->data); |
301 hosts = g_slist_remove(hosts, hosts->data); | 301 hosts = g_slist_remove(hosts, hosts->data); |
302 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in)); | 302 memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in)); |
303 g_free(hosts->data); | 303 g_free(hosts->data); |
316 hdr_data.transid[3] = rand(); | 316 hdr_data.transid[3] = rand(); |
317 | 317 |
318 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0, | 318 if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0, |
319 (struct sockaddr *)&(sc->addr), | 319 (struct sockaddr *)&(sc->addr), |
320 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { | 320 sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { |
321 nattype.status = GAIM_STUN_STATUS_UNKNOWN; | 321 nattype.status = PURPLE_STUN_STATUS_UNKNOWN; |
322 nattype.lookup_time = time(NULL); | 322 nattype.lookup_time = time(NULL); |
323 do_callbacks(); | 323 do_callbacks(); |
324 close_stun_conn(sc); | 324 close_stun_conn(sc); |
325 return; | 325 return; |
326 } | 326 } |
327 sc->test = 1; | 327 sc->test = 1; |
328 sc->packet = &hdr_data; | 328 sc->packet = &hdr_data; |
329 sc->packetsize = sizeof(struct stun_header); | 329 sc->packetsize = sizeof(struct stun_header); |
330 sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); | 330 sc->timeout = purple_timeout_add(500, (GSourceFunc) timeoutfunc, sc); |
331 } | 331 } |
332 | 332 |
333 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) { | 333 static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) { |
334 | 334 |
335 if(!hosts || !hosts->data) { | 335 if(!hosts || !hosts->data) { |
336 nattype.status = GAIM_STUN_STATUS_UNDISCOVERED; | 336 nattype.status = PURPLE_STUN_STATUS_UNDISCOVERED; |
337 nattype.lookup_time = time(NULL); | 337 nattype.lookup_time = time(NULL); |
338 do_callbacks(); | 338 do_callbacks(); |
339 return; | 339 return; |
340 } | 340 } |
341 | 341 |
342 if (!gaim_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) { | 342 if (!purple_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) { |
343 nattype.status = GAIM_STUN_STATUS_UNKNOWN; | 343 nattype.status = PURPLE_STUN_STATUS_UNKNOWN; |
344 nattype.lookup_time = time(NULL); | 344 nattype.lookup_time = time(NULL); |
345 do_callbacks(); | 345 do_callbacks(); |
346 return; | 346 return; |
347 } | 347 } |
348 | 348 |
349 | 349 |
350 } | 350 } |
351 | 351 |
352 static void do_test1(GaimSrvResponse *resp, int results, gpointer sdata) { | 352 static void do_test1(PurpleSrvResponse *resp, int results, gpointer sdata) { |
353 const char *servername = sdata; | 353 const char *servername = sdata; |
354 int port = 3478; | 354 int port = 3478; |
355 | 355 |
356 if(results) { | 356 if(results) { |
357 servername = resp[0].hostname; | 357 servername = resp[0].hostname; |
358 port = resp[0].port; | 358 port = resp[0].port; |
359 } | 359 } |
360 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", | 360 purple_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", |
361 results, servername, port); | 361 results, servername, port); |
362 | 362 |
363 gaim_dnsquery_a(servername, port, hbn_cb, NULL); | 363 purple_dnsquery_a(servername, port, hbn_cb, NULL); |
364 g_free(resp); | 364 g_free(resp); |
365 } | 365 } |
366 | 366 |
367 static gboolean call_callback(gpointer data) { | 367 static gboolean call_callback(gpointer data) { |
368 StunCallback cb = data; | 368 StunCallback cb = data; |
369 cb(&nattype); | 369 cb(&nattype); |
370 return FALSE; | 370 return FALSE; |
371 } | 371 } |
372 | 372 |
373 GaimStunNatDiscovery *gaim_stun_discover(StunCallback cb) { | 373 PurpleStunNatDiscovery *purple_stun_discover(StunCallback cb) { |
374 const char *servername = gaim_prefs_get_string("/core/network/stun_server"); | 374 const char *servername = purple_prefs_get_string("/core/network/stun_server"); |
375 | 375 |
376 gaim_debug_info("stun", "using server %s\n", servername); | 376 purple_debug_info("stun", "using server %s\n", servername); |
377 | 377 |
378 if(nattype.status == GAIM_STUN_STATUS_DISCOVERING) { | 378 if(nattype.status == PURPLE_STUN_STATUS_DISCOVERING) { |
379 if(cb) | 379 if(cb) |
380 callbacks = g_slist_append(callbacks, cb); | 380 callbacks = g_slist_append(callbacks, cb); |
381 return &nattype; | 381 return &nattype; |
382 } | 382 } |
383 | 383 |
384 if(nattype.status != GAIM_STUN_STATUS_UNDISCOVERED) { | 384 if(nattype.status != PURPLE_STUN_STATUS_UNDISCOVERED) { |
385 gboolean use_cached_result = TRUE; | 385 gboolean use_cached_result = TRUE; |
386 | 386 |
387 /** Deal with the server name having changed since we did the | 387 /** Deal with the server name having changed since we did the |
388 lookup */ | 388 lookup */ |
389 if (servername && strlen(servername) > 1 | 389 if (servername && strlen(servername) > 1 |
393 use_cached_result = FALSE; | 393 use_cached_result = FALSE; |
394 } | 394 } |
395 | 395 |
396 /* If we don't have a successful status and it has been 5 | 396 /* If we don't have a successful status and it has been 5 |
397 minutes since we last did a lookup, redo the lookup */ | 397 minutes since we last did a lookup, redo the lookup */ |
398 if (nattype.status != GAIM_STUN_STATUS_DISCOVERED | 398 if (nattype.status != PURPLE_STUN_STATUS_DISCOVERED |
399 && (time(NULL) - nattype.lookup_time) > 300) { | 399 && (time(NULL) - nattype.lookup_time) > 300) { |
400 use_cached_result = FALSE; | 400 use_cached_result = FALSE; |
401 } | 401 } |
402 | 402 |
403 if (use_cached_result) { | 403 if (use_cached_result) { |
404 if(cb) | 404 if(cb) |
405 gaim_timeout_add(10, call_callback, cb); | 405 purple_timeout_add(10, call_callback, cb); |
406 return &nattype; | 406 return &nattype; |
407 } | 407 } |
408 } | 408 } |
409 | 409 |
410 if(!servername || (strlen(servername) < 2)) { | 410 if(!servername || (strlen(servername) < 2)) { |
411 nattype.status = GAIM_STUN_STATUS_UNKNOWN; | 411 nattype.status = PURPLE_STUN_STATUS_UNKNOWN; |
412 nattype.lookup_time = time(NULL); | 412 nattype.lookup_time = time(NULL); |
413 if(cb) | 413 if(cb) |
414 gaim_timeout_add(10, call_callback, cb); | 414 purple_timeout_add(10, call_callback, cb); |
415 return &nattype; | 415 return &nattype; |
416 } | 416 } |
417 | 417 |
418 nattype.status = GAIM_STUN_STATUS_DISCOVERING; | 418 nattype.status = PURPLE_STUN_STATUS_DISCOVERING; |
419 nattype.publicip[0] = '\0'; | 419 nattype.publicip[0] = '\0'; |
420 g_free(nattype.servername); | 420 g_free(nattype.servername); |
421 nattype.servername = g_strdup(servername); | 421 nattype.servername = g_strdup(servername); |
422 | 422 |
423 callbacks = g_slist_append(callbacks, cb); | 423 callbacks = g_slist_append(callbacks, cb); |
424 gaim_srv_resolve("stun", "udp", servername, do_test1, | 424 purple_srv_resolve("stun", "udp", servername, do_test1, |
425 (gpointer) servername); | 425 (gpointer) servername); |
426 | 426 |
427 return &nattype; | 427 return &nattype; |
428 } | 428 } |
429 | 429 |
430 void gaim_stun_init() { | 430 void purple_stun_init() { |
431 gaim_prefs_add_string("/core/network/stun_server", ""); | 431 purple_prefs_add_string("/core/network/stun_server", ""); |
432 gaim_stun_discover(NULL); | 432 purple_stun_discover(NULL); |
433 } | 433 } |