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