summaryrefslogtreecommitdiff
path: root/src/nat/nat_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat/nat_api.c')
-rw-r--r--src/nat/nat_api.c548
1 files changed, 274 insertions, 274 deletions
diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c
index 0dea501d3..877589d85 100644
--- a/src/nat/nat_api.c
+++ b/src/nat/nat_api.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @author Christian Grothoff 22 * @author Christian Grothoff
@@ -35,9 +35,7 @@
35/** 35/**
36 * Entry in DLL of addresses of this peer. 36 * Entry in DLL of addresses of this peer.
37 */ 37 */
38struct AddrEntry 38struct AddrEntry {
39{
40
41 /** 39 /**
42 * DLL. 40 * DLL.
43 */ 41 */
@@ -69,9 +67,7 @@ struct AddrEntry
69/** 67/**
70 * Handle for active NAT registrations. 68 * Handle for active NAT registrations.
71 */ 69 */
72struct GNUNET_NAT_Handle 70struct GNUNET_NAT_Handle {
73{
74
75 /** 71 /**
76 * Configuration we use. 72 * Configuration we use.
77 */ 73 */
@@ -130,7 +126,7 @@ struct GNUNET_NAT_Handle
130 * @param cls our `struct GNUNET_NAT_Handle *` 126 * @param cls our `struct GNUNET_NAT_Handle *`
131 */ 127 */
132static void 128static void
133do_connect (void *cls); 129do_connect(void *cls);
134 130
135 131
136/** 132/**
@@ -139,29 +135,29 @@ do_connect (void *cls);
139 * @param nh handle to reconnect 135 * @param nh handle to reconnect
140 */ 136 */
141static void 137static void
142reconnect (struct GNUNET_NAT_Handle *nh) 138reconnect(struct GNUNET_NAT_Handle *nh)
143{ 139{
144 struct AddrEntry *ae; 140 struct AddrEntry *ae;
145 141
146 if (NULL != nh->mq) 142 if (NULL != nh->mq)
147 { 143 {
148 GNUNET_MQ_destroy (nh->mq); 144 GNUNET_MQ_destroy(nh->mq);
149 nh->mq = NULL; 145 nh->mq = NULL;
150 } 146 }
151 while (NULL != (ae = nh->ae_head)) 147 while (NULL != (ae = nh->ae_head))
152 { 148 {
153 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); 149 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae);
154 nh->address_callback (nh->callback_cls, 150 nh->address_callback(nh->callback_cls,
155 &ae->app_ctx, 151 &ae->app_ctx,
156 GNUNET_NO, 152 GNUNET_NO,
157 ae->ac, 153 ae->ac,
158 (const struct sockaddr *) &ae[1], 154 (const struct sockaddr *)&ae[1],
159 ae->addrlen); 155 ae->addrlen);
160 GNUNET_free (ae); 156 GNUNET_free(ae);
161 } 157 }
162 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay); 158 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF(nh->reconnect_delay);
163 nh->reconnect_task = 159 nh->reconnect_task =
164 GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay, &do_connect, nh); 160 GNUNET_SCHEDULER_add_delayed(nh->reconnect_delay, &do_connect, nh);
165} 161}
166 162
167 163
@@ -173,15 +169,15 @@ reconnect (struct GNUNET_NAT_Handle *nh)
173 * @return #GNUNET_OK if @a crm is well-formed 169 * @return #GNUNET_OK if @a crm is well-formed
174 */ 170 */
175static int 171static int
176check_connection_reversal_request ( 172check_connection_reversal_request(
177 void *cls, 173 void *cls,
178 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 174 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
179{ 175{
180 if (ntohs (crm->header.size) != sizeof (*crm) + sizeof (struct sockaddr_in)) 176 if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in))
181 { 177 {
182 GNUNET_break (0); 178 GNUNET_break(0);
183 return GNUNET_SYSERR; 179 return GNUNET_SYSERR;
184 } 180 }
185 return GNUNET_OK; 181 return GNUNET_OK;
186} 182}
187 183
@@ -193,15 +189,15 @@ check_connection_reversal_request (
193 * @param crm the message 189 * @param crm the message
194 */ 190 */
195static void 191static void
196handle_connection_reversal_request ( 192handle_connection_reversal_request(
197 void *cls, 193 void *cls,
198 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 194 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
199{ 195{
200 struct GNUNET_NAT_Handle *nh = cls; 196 struct GNUNET_NAT_Handle *nh = cls;
201 197
202 nh->reversal_callback (nh->callback_cls, 198 nh->reversal_callback(nh->callback_cls,
203 (const struct sockaddr *) &crm[1], 199 (const struct sockaddr *)&crm[1],
204 sizeof (struct sockaddr_in)); 200 sizeof(struct sockaddr_in));
205} 201}
206 202
207 203
@@ -213,36 +209,38 @@ handle_connection_reversal_request (
213 * @return #GNUNET_OK if @a crm is well-formed 209 * @return #GNUNET_OK if @a crm is well-formed
214 */ 210 */
215static int 211static int
216check_address_change_notification ( 212check_address_change_notification(
217 void *cls, 213 void *cls,
218 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 214 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
219{ 215{
220 size_t alen = ntohs (acn->header.size) - sizeof (*acn); 216 size_t alen = ntohs(acn->header.size) - sizeof(*acn);
221 217
222 switch (alen) 218 switch (alen)
223 {
224 case sizeof (struct sockaddr_in): {
225 const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1];
226 if (AF_INET != s4->sin_family)
227 { 219 {
228 GNUNET_break (0); 220 case sizeof(struct sockaddr_in): {
229 return GNUNET_SYSERR; 221 const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1];
222 if (AF_INET != s4->sin_family)
223 {
224 GNUNET_break(0);
225 return GNUNET_SYSERR;
226 }
230 } 227 }
231 } 228 break;
232 break; 229
233 case sizeof (struct sockaddr_in6): { 230 case sizeof(struct sockaddr_in6): {
234 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1]; 231 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1];
235 if (AF_INET6 != s6->sin6_family) 232 if (AF_INET6 != s6->sin6_family)
236 { 233 {
237 GNUNET_break (0); 234 GNUNET_break(0);
235 return GNUNET_SYSERR;
236 }
237 }
238 break;
239
240 default:
241 GNUNET_break(0);
238 return GNUNET_SYSERR; 242 return GNUNET_SYSERR;
239 } 243 }
240 }
241 break;
242 default:
243 GNUNET_break (0);
244 return GNUNET_SYSERR;
245 }
246 return GNUNET_OK; 244 return GNUNET_OK;
247} 245}
248 246
@@ -254,53 +252,53 @@ check_address_change_notification (
254 * @param acn the message 252 * @param acn the message
255 */ 253 */
256static void 254static void
257handle_address_change_notification ( 255handle_address_change_notification(
258 void *cls, 256 void *cls,
259 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 257 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
260{ 258{
261 struct GNUNET_NAT_Handle *nh = cls; 259 struct GNUNET_NAT_Handle *nh = cls;
262 size_t alen = ntohs (acn->header.size) - sizeof (*acn); 260 size_t alen = ntohs(acn->header.size) - sizeof(*acn);
263 const struct sockaddr *sa = (const struct sockaddr *) &acn[1]; 261 const struct sockaddr *sa = (const struct sockaddr *)&acn[1];
264 enum GNUNET_NAT_AddressClass ac; 262 enum GNUNET_NAT_AddressClass ac;
265 struct AddrEntry *ae; 263 struct AddrEntry *ae;
266 264
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "Received address change notification\n"); 266 "Received address change notification\n");
269 ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class); 267 ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class);
270 if (GNUNET_YES == ntohl (acn->add_remove)) 268 if (GNUNET_YES == ntohl(acn->add_remove))
271 { 269 {
272 ae = GNUNET_malloc (sizeof (*ae) + alen); 270 ae = GNUNET_malloc(sizeof(*ae) + alen);
273 ae->ac = ac; 271 ae->ac = ac;
274 ae->addrlen = alen; 272 ae->addrlen = alen;
275 GNUNET_memcpy (&ae[1], sa, alen); 273 GNUNET_memcpy(&ae[1], sa, alen);
276 GNUNET_CONTAINER_DLL_insert (nh->ae_head, nh->ae_tail, ae); 274 GNUNET_CONTAINER_DLL_insert(nh->ae_head, nh->ae_tail, ae);
277 nh->address_callback (nh->callback_cls, 275 nh->address_callback(nh->callback_cls,
278 &ae->app_ctx, 276 &ae->app_ctx,
279 ntohl (acn->add_remove), 277 ntohl(acn->add_remove),
280 ac, 278 ac,
281 sa, 279 sa,
282 alen); 280 alen);
283 } 281 }
284 else 282 else
285 {
286 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
287 if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen)))
288 break;
289 if (NULL == ae)
290 { 283 {
291 GNUNET_break (0); 284 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
292 reconnect (nh); 285 if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen)))
293 return; 286 break;
287 if (NULL == ae)
288 {
289 GNUNET_break(0);
290 reconnect(nh);
291 return;
292 }
293 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae);
294 nh->address_callback(nh->callback_cls,
295 &ae->app_ctx,
296 ntohl(acn->add_remove),
297 ac,
298 sa,
299 alen);
300 GNUNET_free(ae);
294 } 301 }
295 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae);
296 nh->address_callback (nh->callback_cls,
297 &ae->app_ctx,
298 ntohl (acn->add_remove),
299 ac,
300 sa,
301 alen);
302 GNUNET_free (ae);
303 }
304} 302}
305 303
306 304
@@ -311,11 +309,11 @@ handle_address_change_notification (
311 * @param error details about the error 309 * @param error details about the error
312 */ 310 */
313static void 311static void
314mq_error_handler (void *cls, enum GNUNET_MQ_Error error) 312mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
315{ 313{
316 struct GNUNET_NAT_Handle *nh = cls; 314 struct GNUNET_NAT_Handle *nh = cls;
317 315
318 reconnect (nh); 316 reconnect(nh);
319} 317}
320 318
321 319
@@ -325,32 +323,32 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
325 * @param cls our `struct GNUNET_NAT_Handle *` 323 * @param cls our `struct GNUNET_NAT_Handle *`
326 */ 324 */
327static void 325static void
328do_connect (void *cls) 326do_connect(void *cls)
329{ 327{
330 struct GNUNET_NAT_Handle *nh = cls; 328 struct GNUNET_NAT_Handle *nh = cls;
331 struct GNUNET_MQ_MessageHandler handlers[] = 329 struct GNUNET_MQ_MessageHandler handlers[] =
332 {GNUNET_MQ_hd_var_size (connection_reversal_request, 330 { GNUNET_MQ_hd_var_size(connection_reversal_request,
333 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, 331 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED,
334 struct 332 struct
335 GNUNET_NAT_ConnectionReversalRequestedMessage, 333 GNUNET_NAT_ConnectionReversalRequestedMessage,
336 nh), 334 nh),
337 GNUNET_MQ_hd_var_size (address_change_notification, 335 GNUNET_MQ_hd_var_size(address_change_notification,
338 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, 336 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE,
339 struct GNUNET_NAT_AddressChangeNotificationMessage, 337 struct GNUNET_NAT_AddressChangeNotificationMessage,
340 nh), 338 nh),
341 GNUNET_MQ_handler_end ()}; 339 GNUNET_MQ_handler_end() };
342 struct GNUNET_MQ_Envelope *env; 340 struct GNUNET_MQ_Envelope *env;
343 341
344 nh->reconnect_task = NULL; 342 nh->reconnect_task = NULL;
345 nh->mq = 343 nh->mq =
346 GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh); 344 GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh);
347 if (NULL == nh->mq) 345 if (NULL == nh->mq)
348 { 346 {
349 reconnect (nh); 347 reconnect(nh);
350 return; 348 return;
351 } 349 }
352 env = GNUNET_MQ_msg_copy (nh->reg); 350 env = GNUNET_MQ_msg_copy(nh->reg);
353 GNUNET_MQ_send (nh->mq, env); 351 GNUNET_MQ_send(nh->mq, env);
354} 352}
355 353
356 354
@@ -375,15 +373,15 @@ do_connect (void *cls)
375 * @return NULL on error, otherwise handle that can be used to unregister 373 * @return NULL on error, otherwise handle that can be used to unregister
376 */ 374 */
377struct GNUNET_NAT_Handle * 375struct GNUNET_NAT_Handle *
378GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, 376GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg,
379 const char *config_section, 377 const char *config_section,
380 uint8_t proto, 378 uint8_t proto,
381 unsigned int num_addrs, 379 unsigned int num_addrs,
382 const struct sockaddr **addrs, 380 const struct sockaddr **addrs,
383 const socklen_t *addrlens, 381 const socklen_t *addrlens,
384 GNUNET_NAT_AddressCallback address_callback, 382 GNUNET_NAT_AddressCallback address_callback,
385 GNUNET_NAT_ReversalCallback reversal_callback, 383 GNUNET_NAT_ReversalCallback reversal_callback,
386 void *callback_cls) 384 void *callback_cls)
387{ 385{
388 struct GNUNET_NAT_Handle *nh; 386 struct GNUNET_NAT_Handle *nh;
389 struct GNUNET_NAT_RegisterMessage *rm; 387 struct GNUNET_NAT_RegisterMessage *rm;
@@ -394,73 +392,75 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
394 len = 0; 392 len = 0;
395 for (unsigned int i = 0; i < num_addrs; i++) 393 for (unsigned int i = 0; i < num_addrs; i++)
396 len += addrlens[i]; 394 len += addrlens[i];
397 str_len = strlen (config_section) + 1; 395 str_len = strlen(config_section) + 1;
398 len += str_len; 396 len += str_len;
399 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof (*rm)) || 397 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
400 (num_addrs > UINT16_MAX)) 398 (num_addrs > UINT16_MAX))
401 { 399 {
402 GNUNET_break (0); 400 GNUNET_break(0);
403 return NULL; 401 return NULL;
404 } 402 }
405 rm = GNUNET_malloc (sizeof (*rm) + len); 403 rm = GNUNET_malloc(sizeof(*rm) + len);
406 rm->header.size = htons (sizeof (*rm) + len); 404 rm->header.size = htons(sizeof(*rm) + len);
407 rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER); 405 rm->header.type = htons(GNUNET_MESSAGE_TYPE_NAT_REGISTER);
408 rm->flags = GNUNET_NAT_RF_NONE; 406 rm->flags = GNUNET_NAT_RF_NONE;
409 if (NULL != address_callback) 407 if (NULL != address_callback)
410 rm->flags |= GNUNET_NAT_RF_ADDRESSES; 408 rm->flags |= GNUNET_NAT_RF_ADDRESSES;
411 if (NULL != reversal_callback) 409 if (NULL != reversal_callback)
412 rm->flags |= GNUNET_NAT_RF_REVERSAL; 410 rm->flags |= GNUNET_NAT_RF_REVERSAL;
413 rm->proto = proto; 411 rm->proto = proto;
414 rm->str_len = htons (str_len); 412 rm->str_len = htons(str_len);
415 rm->num_addrs = htons ((uint16_t) num_addrs); 413 rm->num_addrs = htons((uint16_t)num_addrs);
416 off = (char *) &rm[1]; 414 off = (char *)&rm[1];
417 for (unsigned int i = 0; i < num_addrs; i++) 415 for (unsigned int i = 0; i < num_addrs; i++)
418 {
419 switch (addrs[i]->sa_family)
420 { 416 {
421 case AF_INET: 417 switch (addrs[i]->sa_family)
422 if (sizeof (struct sockaddr_in) != addrlens[i]) 418 {
423 { 419 case AF_INET:
424 GNUNET_break (0); 420 if (sizeof(struct sockaddr_in) != addrlens[i])
425 GNUNET_free (rm); 421 {
426 return NULL; 422 GNUNET_break(0);
427 } 423 GNUNET_free(rm);
428 break; 424 return NULL;
429 case AF_INET6: 425 }
430 if (sizeof (struct sockaddr_in6) != addrlens[i]) 426 break;
431 { 427
432 GNUNET_break (0); 428 case AF_INET6:
433 GNUNET_free (rm); 429 if (sizeof(struct sockaddr_in6) != addrlens[i])
434 return NULL; 430 {
435 } 431 GNUNET_break(0);
436 break; 432 GNUNET_free(rm);
433 return NULL;
434 }
435 break;
436
437#if AF_UNIX 437#if AF_UNIX
438 case AF_UNIX: 438 case AF_UNIX:
439 if (sizeof (struct sockaddr_un) != addrlens[i]) 439 if (sizeof(struct sockaddr_un) != addrlens[i])
440 { 440 {
441 GNUNET_break (0); 441 GNUNET_break(0);
442 GNUNET_free (rm); 442 GNUNET_free(rm);
443 return NULL; 443 return NULL;
444 } 444 }
445 break; 445 break;
446#endif 446#endif
447 default: 447 default:
448 GNUNET_break (0); 448 GNUNET_break(0);
449 GNUNET_free (rm); 449 GNUNET_free(rm);
450 return NULL; 450 return NULL;
451 }
452 GNUNET_memcpy(off, addrs[i], addrlens[i]);
453 off += addrlens[i];
451 } 454 }
452 GNUNET_memcpy (off, addrs[i], addrlens[i]); 455 GNUNET_memcpy(off, config_section, str_len);
453 off += addrlens[i];
454 }
455 GNUNET_memcpy (off, config_section, str_len);
456 456
457 nh = GNUNET_new (struct GNUNET_NAT_Handle); 457 nh = GNUNET_new(struct GNUNET_NAT_Handle);
458 nh->reg = &rm->header; 458 nh->reg = &rm->header;
459 nh->cfg = cfg; 459 nh->cfg = cfg;
460 nh->address_callback = address_callback; 460 nh->address_callback = address_callback;
461 nh->reversal_callback = reversal_callback; 461 nh->reversal_callback = reversal_callback;
462 nh->callback_cls = callback_cls; 462 nh->callback_cls = callback_cls;
463 do_connect (nh); 463 do_connect(nh);
464 return nh; 464 return nh;
465} 465}
466 466
@@ -474,7 +474,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
474 * #GNUNET_NO if the packet is invalid (not a stun packet) 474 * #GNUNET_NO if the packet is invalid (not a stun packet)
475 */ 475 */
476static int 476static int
477test_stun_packet (const void *data, size_t len) 477test_stun_packet(const void *data, size_t len)
478{ 478{
479 const struct stun_header *hdr; 479 const struct stun_header *hdr;
480 const struct stun_attr *attr; 480 const struct stun_attr *attr;
@@ -485,71 +485,71 @@ test_stun_packet (const void *data, size_t len)
485 * initial checks it becomes the size of unprocessed options, 485 * initial checks it becomes the size of unprocessed options,
486 * while 'data' is advanced accordingly. 486 * while 'data' is advanced accordingly.
487 */ 487 */
488 if (len < sizeof (struct stun_header)) 488 if (len < sizeof(struct stun_header))
489 { 489 {
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
491 "STUN packet too short (only %d, wanting at least %d)\n", 491 "STUN packet too short (only %d, wanting at least %d)\n",
492 (int) len, 492 (int)len,
493 (int) sizeof (struct stun_header)); 493 (int)sizeof(struct stun_header));
494 return GNUNET_NO; 494 return GNUNET_NO;
495 } 495 }
496 hdr = (const struct stun_header *) data; 496 hdr = (const struct stun_header *)data;
497 /* Skip header as it is already in hdr */ 497 /* Skip header as it is already in hdr */
498 len -= sizeof (struct stun_header); 498 len -= sizeof(struct stun_header);
499 data += sizeof (struct stun_header); 499 data += sizeof(struct stun_header);
500 500
501 /* len as advertised in the message */ 501 /* len as advertised in the message */
502 advertised_message_size = ntohs (hdr->msglen); 502 advertised_message_size = ntohs(hdr->msglen);
503 503
504 message_magic_cookie = ntohl (hdr->magic); 504 message_magic_cookie = ntohl(hdr->magic);
505 /* Compare if the cookie match */ 505 /* Compare if the cookie match */
506 if (STUN_MAGIC_COOKIE != message_magic_cookie) 506 if (STUN_MAGIC_COOKIE != message_magic_cookie)
507 {
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
509 return GNUNET_NO;
510 }
511
512 if (advertised_message_size > len)
513 {
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Scrambled STUN packet length (got %d, expecting %d)\n",
516 advertised_message_size,
517 (int) len);
518 return GNUNET_NO;
519 }
520 len = advertised_message_size;
521 while (len > 0)
522 {
523 if (len < sizeof (struct stun_attr))
524 { 507 {
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
526 "Attribute too short in STUN packet (got %d, expecting %d)\n",
527 (int) len,
528 (int) sizeof (struct stun_attr));
529 return GNUNET_NO; 509 return GNUNET_NO;
530 } 510 }
531 attr = (const struct stun_attr *) data;
532
533 /* compute total attribute length */
534 advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);
535 511
536 /* Check if we still have space in our buffer */ 512 if (advertised_message_size > len)
537 if (advertised_message_size > len)
538 { 513 {
539 GNUNET_log ( 514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 GNUNET_ERROR_TYPE_DEBUG, 515 "Scrambled STUN packet length (got %d, expecting %d)\n",
541 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", 516 advertised_message_size,
542 advertised_message_size, 517 (int)len);
543 (int) len);
544 return GNUNET_NO; 518 return GNUNET_NO;
545 } 519 }
546 data += advertised_message_size; 520 len = advertised_message_size;
547 len -= advertised_message_size; 521 while (len > 0)
548 } 522 {
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 523 if (len < sizeof(struct stun_attr))
550 "STUN Packet, msg %04x, length: %d\n", 524 {
551 ntohs (hdr->msgtype), 525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
552 advertised_message_size); 526 "Attribute too short in STUN packet (got %d, expecting %d)\n",
527 (int)len,
528 (int)sizeof(struct stun_attr));
529 return GNUNET_NO;
530 }
531 attr = (const struct stun_attr *)data;
532
533 /* compute total attribute length */
534 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
535
536 /* Check if we still have space in our buffer */
537 if (advertised_message_size > len)
538 {
539 GNUNET_log(
540 GNUNET_ERROR_TYPE_DEBUG,
541 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
542 advertised_message_size,
543 (int)len);
544 return GNUNET_NO;
545 }
546 data += advertised_message_size;
547 len -= advertised_message_size;
548 }
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
550 "STUN Packet, msg %04x, length: %d\n",
551 ntohs(hdr->msgtype),
552 advertised_message_size);
553 return GNUNET_OK; 553 return GNUNET_OK;
554} 554}
555 555
@@ -578,30 +578,30 @@ test_stun_packet (const void *data, size_t len)
578 * #GNUNET_SYSERR on internal error handling the packet 578 * #GNUNET_SYSERR on internal error handling the packet
579 */ 579 */
580int 580int
581GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, 581GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh,
582 const struct sockaddr *sender_addr, 582 const struct sockaddr *sender_addr,
583 size_t sender_addr_len, 583 size_t sender_addr_len,
584 const void *data, 584 const void *data,
585 size_t data_size) 585 size_t data_size)
586{ 586{
587 struct GNUNET_MQ_Envelope *env; 587 struct GNUNET_MQ_Envelope *env;
588 struct GNUNET_NAT_HandleStunMessage *hsn; 588 struct GNUNET_NAT_HandleStunMessage *hsn;
589 char *buf; 589 char *buf;
590 590
591 if (GNUNET_YES != test_stun_packet (data, data_size)) 591 if (GNUNET_YES != test_stun_packet(data, data_size))
592 return GNUNET_NO; 592 return GNUNET_NO;
593 if (NULL == nh->mq) 593 if (NULL == nh->mq)
594 return GNUNET_SYSERR; 594 return GNUNET_SYSERR;
595 env = GNUNET_MQ_msg_extra (hsn, 595 env = GNUNET_MQ_msg_extra(hsn,
596 data_size + sender_addr_len, 596 data_size + sender_addr_len,
597 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); 597 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN);
598 hsn->sender_addr_size = htons ((uint16_t) sender_addr_len); 598 hsn->sender_addr_size = htons((uint16_t)sender_addr_len);
599 hsn->payload_size = htons ((uint16_t) data_size); 599 hsn->payload_size = htons((uint16_t)data_size);
600 buf = (char *) &hsn[1]; 600 buf = (char *)&hsn[1];
601 GNUNET_memcpy (buf, sender_addr, sender_addr_len); 601 GNUNET_memcpy(buf, sender_addr, sender_addr_len);
602 buf += sender_addr_len; 602 buf += sender_addr_len;
603 GNUNET_memcpy (buf, data, data_size); 603 GNUNET_memcpy(buf, data, data_size);
604 GNUNET_MQ_send (nh->mq, env); 604 GNUNET_MQ_send(nh->mq, env);
605 return GNUNET_OK; 605 return GNUNET_OK;
606} 606}
607 607
@@ -620,20 +620,20 @@ GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
620 * #GNUNET_SYSERR if the address is malformed 620 * #GNUNET_SYSERR if the address is malformed
621 */ 621 */
622int 622int
623GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, 623GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh,
624 const void *addr, 624 const void *addr,
625 socklen_t addrlen) 625 socklen_t addrlen)
626{ 626{
627 struct AddrEntry *ae; 627 struct AddrEntry *ae;
628 628
629 if ((addrlen != sizeof (struct sockaddr_in)) && 629 if ((addrlen != sizeof(struct sockaddr_in)) &&
630 (addrlen != sizeof (struct sockaddr_in6))) 630 (addrlen != sizeof(struct sockaddr_in6)))
631 { 631 {
632 GNUNET_break (0); 632 GNUNET_break(0);
633 return GNUNET_SYSERR; 633 return GNUNET_SYSERR;
634 } 634 }
635 for (ae = nh->ae_head; NULL != ae; ae = ae->next) 635 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
636 if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen))) 636 if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen)))
637 return GNUNET_YES; 637 return GNUNET_YES;
638 return GNUNET_NO; 638 return GNUNET_NO;
639} 639}
@@ -652,9 +652,9 @@ GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
652 * #GNUNET_OK otherwise (presumably in progress) 652 * #GNUNET_OK otherwise (presumably in progress)
653 */ 653 */
654int 654int
655GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, 655GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh,
656 const struct sockaddr_in *local_sa, 656 const struct sockaddr_in *local_sa,
657 const struct sockaddr_in *remote_sa) 657 const struct sockaddr_in *remote_sa)
658{ 658{
659 struct GNUNET_MQ_Envelope *env; 659 struct GNUNET_MQ_Envelope *env;
660 struct GNUNET_NAT_RequestConnectionReversalMessage *req; 660 struct GNUNET_NAT_RequestConnectionReversalMessage *req;
@@ -662,19 +662,19 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
662 662
663 if (NULL == nh->mq) 663 if (NULL == nh->mq)
664 return GNUNET_SYSERR; 664 return GNUNET_SYSERR;
665 GNUNET_break (AF_INET == local_sa->sin_family); 665 GNUNET_break(AF_INET == local_sa->sin_family);
666 GNUNET_break (AF_INET == remote_sa->sin_family); 666 GNUNET_break(AF_INET == remote_sa->sin_family);
667 env = 667 env =
668 GNUNET_MQ_msg_extra (req, 668 GNUNET_MQ_msg_extra(req,
669 2 * sizeof (struct sockaddr_in), 669 2 * sizeof(struct sockaddr_in),
670 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); 670 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL);
671 req->local_addr_size = htons (sizeof (struct sockaddr_in)); 671 req->local_addr_size = htons(sizeof(struct sockaddr_in));
672 req->remote_addr_size = htons (sizeof (struct sockaddr_in)); 672 req->remote_addr_size = htons(sizeof(struct sockaddr_in));
673 buf = (char *) &req[1]; 673 buf = (char *)&req[1];
674 GNUNET_memcpy (buf, local_sa, sizeof (struct sockaddr_in)); 674 GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in));
675 buf += sizeof (struct sockaddr_in); 675 buf += sizeof(struct sockaddr_in);
676 GNUNET_memcpy (buf, remote_sa, sizeof (struct sockaddr_in)); 676 GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in));
677 GNUNET_MQ_send (nh->mq, env); 677 GNUNET_MQ_send(nh->mq, env);
678 return GNUNET_OK; 678 return GNUNET_OK;
679} 679}
680 680
@@ -687,20 +687,20 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
687 * @param nh the handle to stop 687 * @param nh the handle to stop
688 */ 688 */
689void 689void
690GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh) 690GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
691{ 691{
692 if (NULL != nh->mq) 692 if (NULL != nh->mq)
693 { 693 {
694 GNUNET_MQ_destroy (nh->mq); 694 GNUNET_MQ_destroy(nh->mq);
695 nh->mq = NULL; 695 nh->mq = NULL;
696 } 696 }
697 if (NULL != nh->reconnect_task) 697 if (NULL != nh->reconnect_task)
698 { 698 {
699 GNUNET_SCHEDULER_cancel (nh->reconnect_task); 699 GNUNET_SCHEDULER_cancel(nh->reconnect_task);
700 nh->reconnect_task = NULL; 700 nh->reconnect_task = NULL;
701 } 701 }
702 GNUNET_free (nh->reg); 702 GNUNET_free(nh->reg);
703 GNUNET_free (nh); 703 GNUNET_free(nh);
704} 704}
705 705
706 706