Mercurial > pidgin.yaz
annotate src/stun.c @ 11351:23f5b11fd9eb
[gaim-migrate @ 13570]
*** empty log message ***
committer: Tailor Script <tailor@pidgin.im>
author | Piotr Zielinski <zielaj> |
---|---|
date | Fri, 26 Aug 2005 18:36:19 +0000 |
parents | 7d7dd22215ec |
children | 97028c1c69e9 |
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 * TODO: currently only detects if there is a NAT and not the type of NAT | |
28 */ | |
29 | |
30 #include <sys/socket.h> | |
31 #include <ifaddrs.h> | |
32 #include <net/if.h> | |
33 #include <sys/ioctl.h> | |
34 #include <resolv.h> | |
35 | |
36 #include "internal.h" | |
37 | |
38 #include "debug.h" | |
39 #include "account.h" | |
40 #include "stun.h" | |
41 #include "prefs.h" | |
42 | |
43 struct stun_nattype nattype = {-1, 0, "\0"}; | |
44 | |
45 static gint transid[4]; | |
46 | |
47 static GSList *callbacks = 0; | |
48 static int fd = -1; | |
49 gint incb = -1; | |
11336 | 50 gint timeout = -1; |
11225 | 51 |
52 static void do_callbacks() { | |
53 while(callbacks) { | |
54 StunCallback cb = callbacks->data; | |
11336 | 55 if(cb) |
56 cb(&nattype); | |
11225 | 57 callbacks = g_slist_remove(callbacks, cb); |
58 } | |
59 } | |
60 | |
61 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { | |
11336 | 62 char buffer[1024]; |
11225 | 63 char *tmp; |
64 int len; | |
65 struct in_addr in; | |
66 struct stun_attrib *attrib; | |
67 struct stun_header *hdr; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
68 struct ifconf ifc; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
69 struct ifreq *ifr; |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
70 struct sockaddr_in *sinptr; |
11225 | 71 |
11336 | 72 len = recv(source, buffer, 1024, 0); |
11225 | 73 |
74 hdr = (struct stun_header*)buffer; | |
75 if(hdr->transid[0]!=transid[0] || hdr->transid[1]!=transid[1] || hdr->transid[2]!=transid[2] || hdr->transid[3]!=transid[3]) { // wrong transaction | |
76 gaim_debug_info("simple", "got wrong transid\n"); | |
77 return; | |
78 } | |
79 | |
80 tmp = buffer + sizeof(struct stun_header); | |
81 while(buffer+len > tmp) { | |
82 | |
83 attrib = (struct stun_attrib*) tmp; | |
84 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) { | |
85 memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4); | |
86 strcpy(nattype.publicip, inet_ntoa(in)); | |
87 } | |
88 tmp += sizeof(struct stun_attrib) + attrib->len; | |
89 } | |
90 gaim_debug_info("simple", "got public ip %s\n",nattype.publicip); | |
91 nattype.status = 2; | |
92 nattype.type = 1; | |
93 | |
94 // is it a NAT? | |
95 | |
96 ifc.ifc_len = sizeof(buffer); | |
97 ifc.ifc_req = (struct ifreq *) buffer; | |
98 ioctl(source, SIOCGIFCONF, &ifc); | |
99 | |
100 tmp = buffer; | |
101 while(tmp < buffer + ifc.ifc_len) { | |
102 ifr = (struct ifreq *) tmp; | |
103 len = sizeof(struct sockaddr); | |
104 | |
105 tmp += sizeof(ifr->ifr_name) + len; | |
106 | |
107 if(ifr->ifr_addr.sa_family == AF_INET) { | |
108 // we only care about ipv4 interfaces | |
109 sinptr = (struct sockaddr_in *) &ifr->ifr_addr; | |
110 if(sinptr->sin_addr.s_addr == in.s_addr) { | |
111 // no NAT | |
112 gaim_debug_info("simple","no nat"); | |
113 nattype.type = 0; | |
114 } | |
115 } | |
116 } | |
117 | |
118 do_callbacks(); | |
11336 | 119 gaim_timeout_remove(timeout); |
11225 | 120 gaim_input_remove(incb); |
121 } | |
122 | |
11336 | 123 static gboolean timeoutfunc(void *blah) { |
124 /* remove input */ | |
125 gaim_input_remove(incb); | |
126 | |
127 /* set unknown */ | |
128 nattype.status = 0; | |
129 | |
130 /* callbacks */ | |
131 do_callbacks(); | |
132 | |
133 return FALSE; | |
134 } | |
135 | |
136 | |
11225 | 137 struct stun_nattype *gaim_stun_discover(StunCallback cb) { |
138 struct sockaddr_in addr; | |
139 struct stun_header data; | |
140 int ret = 0; | |
11300
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
141 const char *ip = gaim_prefs_get_string("/core/network/stun_ip"); |
dd1a5969b2e5
[gaim-migrate @ 13500]
Richard Laager <rlaager@wiktel.com>
parents:
11229
diff
changeset
|
142 int port = gaim_prefs_get_int("/core/network/stun_port"); |
11336 | 143 |
144 if(!ip || !port) { | |
145 nattype.status = 0; | |
146 if(cb) cb(&nattype); | |
147 return &nattype; | |
148 } | |
11225 | 149 |
150 if(nattype.status == 1) { // currently discovering | |
11336 | 151 if(cb) callbacks = g_slist_append(callbacks, cb); |
11225 | 152 return NULL; |
153 } | |
11336 | 154 if(nattype.status != -1) { // already discovered |
155 if(cb) cb(&nattype); | |
11225 | 156 return &nattype; |
157 } | |
158 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | |
159 nattype.status = 0; | |
11336 | 160 if(cb) cb(&nattype); |
11225 | 161 return &nattype; |
162 } | |
163 | |
164 addr.sin_family = AF_INET; | |
165 addr.sin_port = htons(12108); | |
166 addr.sin_addr.s_addr = INADDR_ANY; | |
167 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) { | |
168 addr.sin_port = htons(ntohs(addr.sin_port)+1); | |
169 } | |
170 if( ret < 0 ) { | |
171 nattype.status = 0; | |
11336 | 172 if(cb) cb(&nattype); |
11225 | 173 return &nattype; |
174 } | |
175 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); | |
176 | |
177 if(port == 0 || ip == NULL || ip[0] == '\0') return NULL; | |
178 | |
179 addr.sin_family = AF_INET; | |
180 addr.sin_port = htons(port); | |
181 addr.sin_addr.s_addr = inet_addr(ip); | |
182 | |
183 data.type = htons(0x0001); | |
184 data.len = 0; | |
185 transid[0] = data.transid[0] = rand(); | |
186 transid[1] = data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | |
187 transid[2] = data.transid[2] = rand(); | |
188 transid[3] = data.transid[3] = rand(); | |
189 | |
190 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { | |
191 nattype.status = 0; | |
11336 | 192 if(cb) cb(&nattype); |
11225 | 193 return &nattype; |
194 } | |
11336 | 195 if(cb) callbacks = g_slist_append(callbacks, cb); |
196 timeout = gaim_timeout_add(2000, (GSourceFunc)timeoutfunc, NULL); | |
11225 | 197 return NULL; |
198 } |