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