Mercurial > pidgin.yaz
comparison src/stun.c @ 11431:d2e44c8085e0
[gaim-migrate @ 13668]
Win32 fixes for sendto() and also some whitespace fixes
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Fri, 02 Sep 2005 21:55:28 +0000 |
parents | 835dbb1b3337 |
children | f18930698b5c |
comparison
equal
deleted
inserted
replaced
11430:4b7087777411 | 11431:d2e44c8085e0 |
---|---|
69 static gboolean timeoutfunc(void *blah) { | 69 static gboolean timeoutfunc(void *blah) { |
70 if(retry > 2) { | 70 if(retry > 2) { |
71 if(test == 2) nattype.type = 5; | 71 if(test == 2) nattype.type = 5; |
72 /* remove input */ | 72 /* remove input */ |
73 gaim_input_remove(incb); | 73 gaim_input_remove(incb); |
74 | 74 |
75 /* set unknown */ | 75 /* set unknown */ |
76 nattype.status = 0; | 76 nattype.status = 0; |
77 | 77 |
78 /* callbacks */ | 78 /* callbacks */ |
79 do_callbacks(); | 79 do_callbacks(); |
86 } | 86 } |
87 | 87 |
88 #ifdef NOTYET | 88 #ifdef NOTYET |
89 static void do_test2() { | 89 static void do_test2() { |
90 struct stun_change data; | 90 struct stun_change data; |
91 data.hdr.type = htons(0x0001); | 91 data.hdr.type = htons(0x0001); |
92 data.hdr.len = 0; | 92 data.hdr.len = 0; |
93 data.hdr.transid[0] = rand(); | 93 data.hdr.transid[0] = rand(); |
94 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | 94 data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); |
95 data.hdr.transid[2] = rand(); | 95 data.hdr.transid[2] = rand(); |
96 data.hdr.transid[3] = rand(); data.attrib.type = htons(0x003); | 96 data.hdr.transid[3] = rand(); |
97 data.attrib.type = htons(0x003); | |
97 data.attrib.len = htons(4); | 98 data.attrib.len = htons(4); |
98 data.value[3] = 6; | 99 data.value[3] = 6; |
99 packet = (struct stun_header*)&data; | 100 packet = (struct stun_header*)&data; |
100 packetsize = sizeof(struct stun_change); | 101 packetsize = sizeof(struct stun_change); |
101 retry = 0; | 102 retry = 0; |
102 test = 2; | 103 test = 2; |
103 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); | 104 sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); |
104 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); | 105 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); |
105 } | 106 } |
106 #endif | 107 #endif |
107 | 108 |
108 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { | 109 static void reply_cb(gpointer data, gint source, GaimInputCondition cond) { |
109 char buffer[1024]; | 110 char buffer[1024]; |
113 struct stun_attrib *attrib; | 114 struct stun_attrib *attrib; |
114 struct stun_header *hdr; | 115 struct stun_header *hdr; |
115 struct ifconf ifc; | 116 struct ifconf ifc; |
116 struct ifreq *ifr; | 117 struct ifreq *ifr; |
117 struct sockaddr_in *sinptr; | 118 struct sockaddr_in *sinptr; |
118 | 119 |
119 len = recv(source, buffer, 1024, 0); | 120 len = recv(source, buffer, 1024, 0); |
120 | 121 |
121 hdr = (struct stun_header*)buffer; | 122 hdr = (struct stun_header*)buffer; |
122 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 */ | 123 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 */ |
123 gaim_debug_info("stun", "got wrong transid\n"); | 124 gaim_debug_info("stun", "got wrong transid\n"); |
124 return; | 125 return; |
125 } | 126 } |
126 if(test==1) { | 127 if(test==1) { |
127 tmp = buffer + sizeof(struct stun_header); | 128 tmp = buffer + sizeof(struct stun_header); |
128 while(buffer+len > tmp) { | 129 while(buffer+len > tmp) { |
129 | 130 |
130 attrib = (struct stun_attrib*) tmp; | 131 attrib = (struct stun_attrib*) tmp; |
131 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) { | 132 if(attrib->type == htons(0x0001) && attrib->len == htons(8)) { |
193 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) { | 194 while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) { |
194 addr.sin_port = htons(ntohs(addr.sin_port)+1); | 195 addr.sin_port = htons(ntohs(addr.sin_port)+1); |
195 } | 196 } |
196 if( ret < 0 ) { | 197 if( ret < 0 ) { |
197 nattype.status = 0; | 198 nattype.status = 0; |
198 do_callbacks(); | 199 do_callbacks(); |
199 return; | 200 return; |
200 } | 201 } |
201 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); | 202 incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL); |
202 | 203 |
203 ret = GPOINTER_TO_INT(hosts->data); | 204 ret = GPOINTER_TO_INT(hosts->data); |
208 while(hosts) { | 209 while(hosts) { |
209 hosts = g_slist_remove(hosts, hosts->data); | 210 hosts = g_slist_remove(hosts, hosts->data); |
210 g_free(hosts->data); | 211 g_free(hosts->data); |
211 hosts = g_slist_remove(hosts, hosts->data); | 212 hosts = g_slist_remove(hosts, hosts->data); |
212 } | 213 } |
213 | 214 |
214 data.type = htons(0x0001); | 215 data.type = htons(0x0001); |
215 data.len = 0; | 216 data.len = 0; |
216 data.transid[0] = rand(); | 217 data.transid[0] = rand(); |
217 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); | 218 data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m'); |
218 data.transid[2] = rand(); | 219 data.transid[2] = rand(); |
219 data.transid[3] = rand(); | 220 data.transid[3] = rand(); |
220 | 221 |
221 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { | 222 if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) { |
222 nattype.status = 0; | 223 nattype.status = 0; |
223 do_callbacks(); | 224 do_callbacks(); |
224 return; | 225 return; |
225 } | 226 } |
226 test = 1; | 227 test = 1; |
227 packet = &data; | 228 packet = &data; |
228 packetsize = sizeof(struct stun_header); | 229 packetsize = sizeof(struct stun_header); |
229 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); | 230 timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL); |
230 } | 231 } |
231 | 232 |
232 static void do_test1(struct srv_response *resp, int results, gpointer sdata) { | 233 static void do_test1(struct srv_response *resp, int results, gpointer sdata) { |
233 char *servername = (char*)sdata; | 234 const char *servername = sdata; |
234 int port = 3478; | 235 int port = 3478; |
235 | 236 |
236 if(results) { | 237 if(results) { |
237 servername = resp[0].hostname; | 238 servername = resp[0].hostname; |
238 port = resp[0].port; | 239 port = resp[0].port; |
239 } | 240 } |
240 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); | 241 gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); |
242 gaim_gethostbyname_async(servername, port, hbn_cb, NULL); | 243 gaim_gethostbyname_async(servername, port, hbn_cb, NULL); |
243 g_free(resp); | 244 g_free(resp); |
244 } | 245 } |
245 | 246 |
246 struct stun_nattype *gaim_stun_discover(StunCallback cb) { | 247 struct stun_nattype *gaim_stun_discover(StunCallback cb) { |
247 char *servername = (char*)gaim_prefs_get_string("/core/network/stun_server"); | 248 const char *servername = gaim_prefs_get_string("/core/network/stun_server"); |
248 | 249 |
249 gaim_debug_info("stun", "using server %s\n", servername); | 250 gaim_debug_info("stun", "using server %s\n", servername); |
250 if(nattype.status == 1) { /* currently discovering */ | 251 if(nattype.status == 1) { /* currently discovering */ |
251 if(cb) callbacks = g_slist_append(callbacks, cb); | 252 if(cb) callbacks = g_slist_append(callbacks, cb); |
252 return NULL; | 253 return NULL; |
260 nattype.status = 0; | 261 nattype.status = 0; |
261 if(cb) cb(&nattype); | 262 if(cb) cb(&nattype); |
262 return &nattype; | 263 return &nattype; |
263 } | 264 } |
264 callbacks = g_slist_append(callbacks, cb); | 265 callbacks = g_slist_append(callbacks, cb); |
265 gaim_srv_resolve("stun","udp",servername, do_test1, servername); | 266 gaim_srv_resolve("stun","udp",servername, do_test1, |
267 (gpointer) servername); | |
266 return &nattype; | 268 return &nattype; |
267 } | 269 } |
268 | 270 |
269 void gaim_stun_init() { | 271 void gaim_stun_init() { |
270 gaim_prefs_add_string("/core/network/stun_server", ""); | 272 gaim_prefs_add_string("/core/network/stun_server", ""); |