* @file stun.c STUN (RFC3489) Implementation * STUN implementation inspired by jstun [http://jstun.javawi.de/] * Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #if defined (__SVR4) && defined (__sun) #define MSGTYPE_BINDINGREQUEST 0x0001 #define MSGTYPE_BINDINGRESPONSE 0x0101 #define ATTRIB_MAPPEDADDRESS 0x0001 struct stun_attrib attrib; struct stun_header *packet; static GaimStunNatDiscovery nattype = { GAIM_STUN_STATUS_UNDISCOVERED, GAIM_STUN_NAT_TYPE_PUBLIC_IP, static GSList *callbacks = NULL; static void close_stun_conn(struct stun_conn *sc) { gaim_input_remove(sc->incb); gaim_timeout_remove(sc->timeout); static void do_callbacks() { StunCallback cb = callbacks->data; callbacks = g_slist_remove(callbacks, cb); static gboolean timeoutfunc(gpointer data) { struct stun_conn *sc = data; gaim_debug_info("stun", "request timed out, giving up.\n"); nattype.type = GAIM_STUN_NAT_TYPE_SYMMETRIC; nattype.status = GAIM_STUN_STATUS_UNKNOWN; nattype.lookup_time = time(NULL); /* we don't need to remove the timeout (returning FALSE) */ gaim_debug_info("stun", "request timed out, retrying.\n"); sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); static void do_test2(struct stun_conn *sc) { data.hdr.type = htons(0x0001); data.hdr.transid[0] = rand(); data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); data.hdr.transid[2] = rand(); data.hdr.transid[3] = rand(); data.attrib.type = htons(0x003); data.attrib.len = htons(4); sc->packet = (struct stun_header*)&data; sc->packetsize = sizeof(struct stun_change); sendto(sc->fd, sc->packet, sc->packetsize, 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)); sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { struct stun_conn *sc = data; struct stun_attrib *attrib; struct sockaddr_in *sinptr; len = recv(source, buffer, sizeof(buffer) - 1, 0); gaim_debug_info("stun", "unable to read stun response\n"); if (len < sizeof(struct stun_header)) { gaim_debug_info("stun", "got invalid response\n"); hdr = (struct stun_header*) buffer; if (len != (ntohs(hdr->len) + sizeof(struct stun_header))) { gaim_debug_info("stun", "got incomplete response\n"); if(hdr->transid[0] != sc->packet->transid[0] || hdr->transid[1] != sc->packet->transid[1] || hdr->transid[2] != sc->packet->transid[2] || hdr->transid[3] != sc->packet->transid[3]) { gaim_debug_info("stun", "got wrong transid\n"); if (hdr->type != MSGTYPE_BINDINGRESPONSE) { "Expected Binding Response, got %d\n", tmp = buffer + sizeof(struct stun_header); while((buffer + len) > (tmp + sizeof(struct stun_attrib))) { attrib = (struct stun_attrib*) tmp; tmp += sizeof(struct stun_attrib); if (!((buffer + len) > (tmp + ntohs(attrib->len)))) if(attrib->type == htons(ATTRIB_MAPPEDADDRESS) && ntohs(attrib->len) == 8) { /* Skip the first unused byte, * the family(1 byte), and the port(2 bytes); * then read the 4 byte IPv4 address */ memcpy(&in.s_addr, tmp + 4, 4); strcpy(nattype.publicip, ip); tmp += ntohs(attrib->len); gaim_debug_info("stun", "got public ip %s\n", nattype.publicip); nattype.status = GAIM_STUN_STATUS_DISCOVERED; nattype.type = GAIM_STUN_NAT_TYPE_UNKNOWN_NAT; nattype.lookup_time = time(NULL); ifc.ifc_len = sizeof(buffer); ifc.ifc_req = (struct ifreq *) buffer; ioctl(source, SIOCGIFCONF, &ifc); while(tmp < buffer + ifc.ifc_len) { ifr = (struct ifreq *) tmp; tmp += sizeof(struct ifreq); if(ifr->ifr_addr.sa_family == AF_INET) { /* we only care about ipv4 interfaces */ sinptr = (struct sockaddr_in *) &ifr->ifr_addr; if(sinptr->sin_addr.s_addr == in.s_addr) { gaim_debug_info("stun", "no nat"); nattype.type = GAIM_STUN_NAT_TYPE_PUBLIC_IP; gaim_timeout_remove(sc->timeout); } else if(sc->test == 2) { nattype.type = GAIM_STUN_NAT_TYPE_FULL_CONE; static void hbn_listen_cb(int fd, gpointer data) { static struct stun_header hdr_data; nattype.status = GAIM_STUN_STATUS_UNKNOWN; nattype.lookup_time = time(NULL); sc = g_new0(struct stun_conn, 1); sc->addr.sin_family = AF_INET; sc->addr.sin_port = htons(gaim_network_get_port_from_fd(fd)); sc->addr.sin_addr.s_addr = INADDR_ANY; sc->incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, sc); ret = GPOINTER_TO_INT(hosts->data); hosts = g_slist_remove(hosts, hosts->data); memcpy(&(sc->addr), hosts->data, sizeof(struct sockaddr_in)); hosts = g_slist_remove(hosts, hosts->data); hosts = g_slist_remove(hosts, hosts->data); hosts = g_slist_remove(hosts, hosts->data); hdr_data.type = htons(MSGTYPE_BINDINGREQUEST); hdr_data.transid[0] = rand(); hdr_data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); hdr_data.transid[2] = rand(); hdr_data.transid[3] = rand(); if(sendto(sc->fd, &hdr_data, sizeof(struct stun_header), 0, (struct sockaddr *)&(sc->addr), sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { nattype.status = GAIM_STUN_STATUS_UNKNOWN; nattype.lookup_time = time(NULL); sc->packetsize = sizeof(struct stun_header); sc->timeout = gaim_timeout_add(500, (GSourceFunc) timeoutfunc, sc); static void hbn_cb(GSList *hosts, gpointer data, const char *error_message) { if(!hosts || !hosts->data) { nattype.status = GAIM_STUN_STATUS_UNDISCOVERED; nattype.lookup_time = time(NULL); if (!gaim_network_listen_range(12108, 12208, SOCK_DGRAM, hbn_listen_cb, hosts)) { nattype.status = GAIM_STUN_STATUS_UNKNOWN; nattype.lookup_time = time(NULL); static void do_test1(GaimSrvResponse *resp, int results, gpointer sdata) { const char *servername = sdata; servername = resp[0].hostname; gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); gaim_gethostbyname_async(servername, port, hbn_cb, NULL); static gboolean call_callback(gpointer data) { GaimStunNatDiscovery *gaim_stun_discover(StunCallback cb) { const char *servername = gaim_prefs_get_string("/core/network/stun_server"); gaim_debug_info("stun", "using server %s\n", servername); if(nattype.status == GAIM_STUN_STATUS_DISCOVERING) { callbacks = g_slist_append(callbacks, cb); if(nattype.status != GAIM_STUN_STATUS_UNDISCOVERED) { gboolean use_cached_result = TRUE; /** Deal with the server name having changed since we did the if (servername && strlen(servername) > 1 && strcmp(servername, nattype.servername)) || !nattype.servername)) { use_cached_result = FALSE; /* If we don't have a successful status and it has been 5 minutes since we last did a lookup, redo the lookup */ if (nattype.status != GAIM_STUN_STATUS_DISCOVERED && (time(NULL) - nattype.lookup_time) > 300) { use_cached_result = FALSE; gaim_timeout_add(10, call_callback, cb); if(!servername || (strlen(servername) < 2)) { nattype.status = GAIM_STUN_STATUS_UNKNOWN; nattype.lookup_time = time(NULL); gaim_timeout_add(10, call_callback, cb); nattype.status = GAIM_STUN_STATUS_DISCOVERING; nattype.publicip[0] = '\0'; g_free(nattype.servername); nattype.servername = g_strdup(servername); callbacks = g_slist_append(callbacks, cb); gaim_srv_resolve("stun", "udp", servername, do_test1, gaim_prefs_add_string("/core/network/stun_server", ""); gaim_stun_discover(NULL);