comparison src/stun.c @ 11424:e1ab173ef3b5

[gaim-migrate @ 13661] prefs for STUN adjustments for NTLM in SIP committer: Tailor Script <tailor@pidgin.im>
author Thomas Butter <tbutter>
date Fri, 02 Sep 2005 09:09:04 +0000
parents 6e02e20e3a58
children 54fa445aff32
comparison
equal deleted inserted replaced
11423:202a3b3c5a88 11424:e1ab173ef3b5
38 38
39 #include "internal.h" 39 #include "internal.h"
40 40
41 #include "debug.h" 41 #include "debug.h"
42 #include "account.h" 42 #include "account.h"
43 #include "dnssrv.h"
43 #include "stun.h" 44 #include "stun.h"
44 #include "prefs.h" 45 #include "prefs.h"
45 46
46 struct stun_nattype nattype = {-1, 0, "\0"}; 47 struct stun_nattype nattype = {-1, 0, "\0"};
47 48
169 gaim_input_remove(incb); 170 gaim_input_remove(incb);
170 gaim_timeout_remove(timeout); 171 gaim_timeout_remove(timeout);
171 nattype.type = 2; 172 nattype.type = 2;
172 } 173 }
173 } 174 }
174 175
175 struct stun_nattype *gaim_stun_discover(StunCallback cb) { 176 static void do_test1(struct srv_response *resp, int results, gpointer sdata) {
177 char *servername = (char*)sdata;
176 static struct stun_header data; 178 static struct stun_header data;
177 int ret = 0; 179 int port = 3478;
178 const char *ip = gaim_prefs_get_string("/core/network/stun_ip"); 180 int ret;
179 int port = gaim_prefs_get_int("/core/network/stun_port"); 181 struct hostent *host;
180 182
181 if(!ip || !port) { 183 if(results) {
182 nattype.status = 0; 184 servername = resp[0].hostname;
183 if(cb) cb(&nattype); 185 port = resp[0].port;
184 return &nattype; 186 }
185 } 187 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port);
186 188 if(!host->h_addr_list) {
187 if(nattype.status == 1) { /* currently discovering */ 189 return;
188 if(cb) callbacks = g_slist_append(callbacks, cb); 190 }
189 return NULL; 191 host = gethostbyname(servername);
190 } 192
191 if(nattype.status != -1) { /* already discovered */
192 if(cb) cb(&nattype);
193 return &nattype;
194 }
195 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 193 if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
196 nattype.status = 0; 194 nattype.status = 0;
197 if(cb) cb(&nattype); 195 do_callbacks();
198 return &nattype; 196 return;
199 } 197 }
200 198
201 addr.sin_family = AF_INET; 199 addr.sin_family = AF_INET;
202 addr.sin_port = htons(12108); 200 addr.sin_port = htons(12108);
203 addr.sin_addr.s_addr = INADDR_ANY; 201 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) { 202 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); 203 addr.sin_port = htons(ntohs(addr.sin_port)+1);
206 } 204 }
207 if( ret < 0 ) { 205 if( ret < 0 ) {
208 nattype.status = 0; 206 nattype.status = 0;
209 if(cb) cb(&nattype); 207 do_callbacks();
210 return &nattype; 208 return;
211 } 209 }
212 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); 210 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL);
213
214 if(port == 0 || ip == NULL || ip[0] == '\0') return NULL;
215 211
216 addr.sin_family = AF_INET; 212 addr.sin_family = AF_INET;
217 addr.sin_port = htons(port); 213 addr.sin_port = htons(port);
218 addr.sin_addr.s_addr = inet_addr(ip); 214 memcpy(&addr.sin_addr.s_addr,*(host->h_addr_list),4);
219 215
220 data.type = htons(0x0001); 216 data.type = htons(0x0001);
221 data.len = 0; 217 data.len = 0;
222 data.transid[0] = rand(); 218 data.transid[0] = rand();
223 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); 219 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
224 data.transid[2] = rand(); 220 data.transid[2] = rand();
225 data.transid[3] = rand(); 221 data.transid[3] = rand();
226 222
227 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { 223 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
228 nattype.status = 0; 224 nattype.status = 0;
229 if(cb) cb(&nattype); 225 do_callbacks();
230 return &nattype; 226 return;
231 } 227 }
232 test = 1; 228 test = 1;
233 packet = &data; 229 packet = &data;
234 packetsize = sizeof(struct stun_header); 230 packetsize = sizeof(struct stun_header);
235 if(cb) callbacks = g_slist_append(callbacks, cb);
236 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); 231 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
237 return NULL; 232 g_free(resp);
238 } 233 }
234
235 struct stun_nattype *gaim_stun_discover(StunCallback cb) {
236 const char *servername = gaim_prefs_get_string("/core/network/stun_server");
237
238 gaim_debug_info("stun", "using server %s\n", servername);
239 if(nattype.status == 1) { /* currently discovering */
240 if(cb) callbacks = g_slist_append(callbacks, cb);
241 return NULL;
242 }
243 if(nattype.status != -1) { /* already discovered */
244 if(cb) cb(&nattype);
245 return &nattype;
246 }
247
248 if(!servername || (strlen(servername)<2)) {
249 nattype.status = 0;
250 if(cb) cb(&nattype);
251 return &nattype;
252 }
253 callbacks = g_slist_append(callbacks, cb);
254 gaim_srv_resolve("stun","udp",servername, do_test1, servername);
255 return &nattype;
256 }
257
258 void gaim_stun_init() {
259 gaim_prefs_add_string("/core/network/stun_server", "");
260 }