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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
1 /**
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
2 * @file stun.c STUN (RFC3489) Implementation
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
3 * @ingroup core
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
4 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
5 * gaim
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
6 *
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
7 * STUN implementation inspired by jstun [http://jstun.javawi.de/]
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
8 *
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
9 * Gaim is the legal property of its developers, whose names are too numerous
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
10 * to list here. Please refer to the COPYRIGHT file distributed with this
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
11 * source distribution.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
12 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
13 * This program is free software; you can redistribute it and/or modify
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
14 * it under the terms of the GNU General Public License as published by
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
15 * the Free Software Foundation; either version 2 of the License, or
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
16 * (at your option) any later version.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
17 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
18 * This program is distributed in the hope that it will be useful,
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
21 * GNU General Public License for more details.
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
22 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
23 * You should have received a copy of the GNU General Public License
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
24 * along with this program; if not, write to the Free Software
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
26 *
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
27 */
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
28
11364
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
29 #ifndef _WIN32
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
30 #include <sys/socket.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
31 #include <ifaddrs.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
32 #include <net/if.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
33 #include <sys/ioctl.h>
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
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
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
38
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
39 #include "internal.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
40
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
41 #include "debug.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
42 #include "account.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
43 #include "stun.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
44 #include "prefs.h"
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
45
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
46 struct stun_nattype nattype = {-1, 0, "\0"};
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
47
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
48 static GSList *callbacks = 0;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
49 static int fd = -1;
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
50 static gint incb = -1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
51 static gint timeout = -1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
52 static struct stun_header *packet;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
53 static int packetsize = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
54 static int test = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
55 static int retry = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
56 static struct sockaddr_in addr;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
57
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
58 static void do_callbacks() {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
59 while(callbacks) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
60 StunCallback cb = callbacks->data;
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
61 if(cb)
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
62 cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
63 callbacks = g_slist_remove(callbacks, cb);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
64 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
65 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
66
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
67 static gboolean timeoutfunc(void *blah) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
68 if(retry > 2) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
69 if(test == 2) nattype.type = 5;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
70 /* remove input */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
71 gaim_input_remove(incb);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
72
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
73 /* set unknown */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
74 nattype.status = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
75
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
76 /* callbacks */
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
77 do_callbacks();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
78
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
79 return FALSE;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
80 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
81 retry++;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
82 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
83 return TRUE;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
84 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
85
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
86 #ifdef NOTYET
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
87 static void do_test2() {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
88 struct stun_change data;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
89 data.hdr.type = htons(0x0001);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
90 data.hdr.len = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
91 data.hdr.transid[0] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
92 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
93 data.hdr.transid[2] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
94 data.hdr.transid[3] = rand(); data.attrib.type = htons(0x003);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
95 data.attrib.len = htons(4);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
96 data.value[3] = 6;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
97 packet = (struct stun_header*)&data;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
98 packetsize = sizeof(struct stun_change);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
99 retry = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
100 test = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
101 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
102 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
103 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
104 #endif
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
105
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
106 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) {
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
107 char buffer[1024];
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
108 char *tmp;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
109 int len;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
110 struct in_addr in;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
111 struct stun_attrib *attrib;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
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
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
116
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
117 len = recv(source, buffer, 1024, 0);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
118
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
119 hdr = (struct stun_header*)buffer;
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
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
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
121 gaim_debug_info("simple", "got wrong transid\n");
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
122 return;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
123 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
124 if(test==1) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
125 tmp = buffer + sizeof(struct stun_header);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
126 while(buffer+len > tmp) {
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
127
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
128 attrib = (struct stun_attrib*) tmp;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
129 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
130 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
131 strcpy(nattype.publicip, inet_ntoa(in));
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
132 }
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
133 tmp += sizeof(struct stun_attrib) + attrib->len;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
134 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
135 gaim_debug_info("simple", "got public ip %s\n",nattype.publicip);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
136 nattype.status = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
137 nattype.type = 1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
138
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
139 // is it a NAT?
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
140
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
141 ifc.ifc_len = sizeof(buffer);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
142 ifc.ifc_req = (struct ifreq *) buffer;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
143 ioctl(source, SIOCGIFCONF, &ifc);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
144
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
145 tmp = buffer;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
146 while(tmp < buffer + ifc.ifc_len) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
147 ifr = (struct ifreq *) tmp;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
148
11364
e1840eb860e7 [gaim-migrate @ 13588]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 11354
diff changeset
149 tmp += sizeof(struct ifreq);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
150
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
151 if(ifr->ifr_addr.sa_family == AF_INET) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
152 // we only care about ipv4 interfaces
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
153 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
154 if(sinptr->sin_addr.s_addr == in.s_addr) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
155 // no NAT
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
156 gaim_debug_info("simple","no nat");
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
157 nattype.type = 0;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
158 }
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
159 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
160 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
161 gaim_timeout_remove(timeout);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
162
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
163 #ifdef NOTYET
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
164 do_test2();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
165 #endif
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
166 return;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
167 } else if(test == 2) {
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
168 do_callbacks();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
169 gaim_input_remove(incb);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
170 gaim_timeout_remove(timeout);
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
171 nattype.type = 2;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
172 }
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
173 }
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
174
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
175 struct stun_nattype *gaim_stun_discover(StunCallback cb) {
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
176 static struct stun_header data;
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
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
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
180
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
181 if(!ip || !port) {
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
182 nattype.status = 0;
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
183 if(cb) cb(&nattype);
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
184 return &nattype;
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
185 }
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
186
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
187 if(nattype.status == 1) { // currently discovering
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
188 if(cb) callbacks = g_slist_append(callbacks, cb);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
189 return NULL;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
190 }
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
191 if(nattype.status != -1) { // already discovered
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
192 if(cb) cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
193 return &nattype;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
194 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
195 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
196 nattype.status = 0;
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
197 if(cb) cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
198 return &nattype;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
199 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
200
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
201 addr.sin_family = AF_INET;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
202 addr.sin_port = htons(12108);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
203 addr.sin_addr.s_addr = INADDR_ANY;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
204 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
205 addr.sin_port = htons(ntohs(addr.sin_port)+1);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
206 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
207 if( ret < 0 ) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
208 nattype.status = 0;
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
209 if(cb) cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
210 return &nattype;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
211 }
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
212 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
213
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
214 if(port == 0 || ip == NULL || ip[0] == '\0') return NULL;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
215
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
216 addr.sin_family = AF_INET;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
217 addr.sin_port = htons(port);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
218 addr.sin_addr.s_addr = inet_addr(ip);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
219
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
220 data.type = htons(0x0001);
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
221 data.len = 0;
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
222 data.transid[0] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
223 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
224 data.transid[2] = rand();
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
225 data.transid[3] = rand();
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
226
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
227 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
228 nattype.status = 0;
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
229 if(cb) cb(&nattype);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
230 return &nattype;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
231 }
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
232 test = 1;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
233 packet = &data;
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
234 packetsize = sizeof(struct stun_header);
11336
7d7dd22215ec [gaim-migrate @ 13549]
Thomas Butter <tbutter>
parents: 11300
diff changeset
235 if(cb) callbacks = g_slist_append(callbacks, cb);
11354
97028c1c69e9 [gaim-migrate @ 13573]
Thomas Butter <tbutter>
parents: 11336
diff changeset
236 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
11225
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
237 return NULL;
46facec74fc1 [gaim-migrate @ 13361]
Thomas Butter <tbutter>
parents:
diff changeset
238 }