diff options
Diffstat (limited to 'src/hello/hello.c')
-rw-r--r-- | src/hello/hello.c | 243 |
1 files changed, 114 insertions, 129 deletions
diff --git a/src/hello/hello.c b/src/hello/hello.c index 9affad3c1..0df888c21 100644 --- a/src/hello/hello.c +++ b/src/hello/hello.c | |||
@@ -121,35 +121,35 @@ get_hello_address_size (const char *buf, size_t max, uint16_t * ralen) | |||
121 | pos = buf; | 121 | pos = buf; |
122 | slen = 1; | 122 | slen = 1; |
123 | while ((left > 0) && ('\0' != *pos)) | 123 | while ((left > 0) && ('\0' != *pos)) |
124 | { | 124 | { |
125 | left--; | 125 | left--; |
126 | pos++; | 126 | pos++; |
127 | slen++; | 127 | slen++; |
128 | } | 128 | } |
129 | if (left == 0) | 129 | if (left == 0) |
130 | { | 130 | { |
131 | /* 0-termination not found */ | 131 | /* 0-termination not found */ |
132 | GNUNET_break_op (0); | 132 | GNUNET_break_op (0); |
133 | abort (); | 133 | abort (); |
134 | return 0; | 134 | return 0; |
135 | } | 135 | } |
136 | pos++; | 136 | pos++; |
137 | if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO)) | 137 | if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO)) |
138 | { | 138 | { |
139 | /* not enough space for addrlen */ | 139 | /* not enough space for addrlen */ |
140 | GNUNET_break_op (0); | 140 | GNUNET_break_op (0); |
141 | return 0; | 141 | return 0; |
142 | } | 142 | } |
143 | memcpy (&alen, pos, sizeof (uint16_t)); | 143 | memcpy (&alen, pos, sizeof (uint16_t)); |
144 | alen = ntohs (alen); | 144 | alen = ntohs (alen); |
145 | *ralen = alen; | 145 | *ralen = alen; |
146 | slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO); | 146 | slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO); |
147 | if (max < slen) | 147 | if (max < slen) |
148 | { | 148 | { |
149 | /* not enough space for addr */ | 149 | /* not enough space for addr */ |
150 | GNUNET_break_op (0); | 150 | GNUNET_break_op (0); |
151 | return 0; | 151 | return 0; |
152 | } | 152 | } |
153 | return slen; | 153 | return slen; |
154 | } | 154 | } |
155 | 155 | ||
@@ -177,13 +177,13 @@ GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded | |||
177 | max = sizeof (buffer); | 177 | max = sizeof (buffer); |
178 | used = 0; | 178 | used = 0; |
179 | if (addrgen != NULL) | 179 | if (addrgen != NULL) |
180 | { | ||
181 | while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used]))) | ||
180 | { | 182 | { |
181 | while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used]))) | 183 | max -= ret; |
182 | { | 184 | used += ret; |
183 | max -= ret; | ||
184 | used += ret; | ||
185 | } | ||
186 | } | 185 | } |
186 | } | ||
187 | hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used); | 187 | hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used); |
188 | hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO); | 188 | hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO); |
189 | hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used); | 189 | hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used); |
@@ -225,46 +225,44 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg, | |||
225 | return NULL; | 225 | return NULL; |
226 | ret = NULL; | 226 | ret = NULL; |
227 | if (return_modified) | 227 | if (return_modified) |
228 | { | 228 | { |
229 | ret = GNUNET_malloc (msize); | 229 | ret = GNUNET_malloc (msize); |
230 | memcpy (ret, msg, msize); | 230 | memcpy (ret, msg, msize); |
231 | } | 231 | } |
232 | inptr = (const char *) &msg[1]; | 232 | inptr = (const char *) &msg[1]; |
233 | insize = msize - sizeof (struct GNUNET_HELLO_Message); | 233 | insize = msize - sizeof (struct GNUNET_HELLO_Message); |
234 | wpos = 0; | 234 | wpos = 0; |
235 | woff = (ret != NULL) ? (char *) &ret[1] : NULL; | 235 | woff = (ret != NULL) ? (char *) &ret[1] : NULL; |
236 | while (insize > 0) | 236 | while (insize > 0) |
237 | { | ||
238 | esize = get_hello_address_size (inptr, insize, &alen); | ||
239 | if (esize == 0) | ||
240 | { | ||
241 | GNUNET_break (0); | ||
242 | GNUNET_free_non_null (ret); | ||
243 | return NULL; | ||
244 | } | ||
245 | memcpy (&expire, | ||
246 | &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)], | ||
247 | sizeof (struct GNUNET_TIME_AbsoluteNBO)); | ||
248 | iret = it (it_cls, | ||
249 | inptr, | ||
250 | GNUNET_TIME_absolute_ntoh (expire), &inptr[esize - alen], alen); | ||
251 | if (iret == GNUNET_SYSERR) | ||
252 | { | ||
253 | if (ret != NULL) | ||
254 | ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos); | ||
255 | return ret; | ||
256 | } | ||
257 | if ((iret == GNUNET_OK) && (ret != NULL)) | ||
237 | { | 258 | { |
238 | esize = get_hello_address_size (inptr, insize, &alen); | 259 | memcpy (woff, inptr, esize); |
239 | if (esize == 0) | 260 | woff += esize; |
240 | { | 261 | wpos += esize; |
241 | GNUNET_break (0); | ||
242 | GNUNET_free_non_null (ret); | ||
243 | return NULL; | ||
244 | } | ||
245 | memcpy (&expire, | ||
246 | &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)], | ||
247 | sizeof (struct GNUNET_TIME_AbsoluteNBO)); | ||
248 | iret = it (it_cls, | ||
249 | inptr, | ||
250 | GNUNET_TIME_absolute_ntoh (expire), | ||
251 | &inptr[esize - alen], alen); | ||
252 | if (iret == GNUNET_SYSERR) | ||
253 | { | ||
254 | if (ret != NULL) | ||
255 | ret->header.size = | ||
256 | ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos); | ||
257 | return ret; | ||
258 | } | ||
259 | if ((iret == GNUNET_OK) && (ret != NULL)) | ||
260 | { | ||
261 | memcpy (woff, inptr, esize); | ||
262 | woff += esize; | ||
263 | wpos += esize; | ||
264 | } | ||
265 | insize -= esize; | ||
266 | inptr += esize; | ||
267 | } | 262 | } |
263 | insize -= esize; | ||
264 | inptr += esize; | ||
265 | } | ||
268 | if (ret != NULL) | 266 | if (ret != NULL) |
269 | ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos); | 267 | ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos); |
270 | return ret; | 268 | return ret; |
@@ -289,14 +287,14 @@ get_match_exp (void *cls, | |||
289 | { | 287 | { |
290 | struct ExpireContext *ec = cls; | 288 | struct ExpireContext *ec = cls; |
291 | 289 | ||
292 | if ( (addrlen == ec->addrlen) && | 290 | if ((addrlen == ec->addrlen) && |
293 | (0 == memcmp (addr, ec->addr, addrlen)) && | 291 | (0 == memcmp (addr, ec->addr, addrlen)) && |
294 | (0 == strcmp (tname, ec->tname)) ) | 292 | (0 == strcmp (tname, ec->tname))) |
295 | { | 293 | { |
296 | ec->found = GNUNET_YES; | 294 | ec->found = GNUNET_YES; |
297 | ec->expiration = expiration; | 295 | ec->expiration = expiration; |
298 | return GNUNET_SYSERR; /* done here */ | 296 | return GNUNET_SYSERR; /* done here */ |
299 | } | 297 | } |
300 | return GNUNET_OK; | 298 | return GNUNET_OK; |
301 | } | 299 | } |
302 | 300 | ||
@@ -328,18 +326,17 @@ copy_latest (void *cls, | |||
328 | ec.found = GNUNET_NO; | 326 | ec.found = GNUNET_NO; |
329 | ec.tname = tname; | 327 | ec.tname = tname; |
330 | GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec); | 328 | GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec); |
331 | if ( (ec.found == GNUNET_NO) || | 329 | if ((ec.found == GNUNET_NO) || |
332 | (ec.expiration.abs_value < expiration.abs_value) || | 330 | (ec.expiration.abs_value < expiration.abs_value) || |
333 | ( (ec.expiration.abs_value == expiration.abs_value) && | 331 | ((ec.expiration.abs_value == expiration.abs_value) && |
334 | (mc->take_equal == GNUNET_YES) ) ) | 332 | (mc->take_equal == GNUNET_YES))) |
335 | { | 333 | { |
336 | mc->ret += GNUNET_HELLO_add_address (tname, | 334 | mc->ret += GNUNET_HELLO_add_address (tname, |
337 | expiration, | 335 | expiration, |
338 | addr, | 336 | addr, |
339 | addrlen, | 337 | addrlen, |
340 | &mc->buf[mc->ret], | 338 | &mc->buf[mc->ret], mc->max - mc->ret); |
341 | mc->max - mc->ret); | 339 | } |
342 | } | ||
343 | return GNUNET_OK; | 340 | return GNUNET_OK; |
344 | } | 341 | } |
345 | 342 | ||
@@ -461,6 +458,7 @@ uint16_t | |||
461 | GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello) | 458 | GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello) |
462 | { | 459 | { |
463 | uint16_t ret = ntohs (hello->header.size); | 460 | uint16_t ret = ntohs (hello->header.size); |
461 | |||
464 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || | 462 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || |
465 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) | 463 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) |
466 | return 0; | 464 | return 0; |
@@ -477,10 +475,10 @@ GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello) | |||
477 | */ | 475 | */ |
478 | int | 476 | int |
479 | GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello, | 477 | GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello, |
480 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded | 478 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey) |
481 | *publicKey) | ||
482 | { | 479 | { |
483 | uint16_t ret = ntohs (hello->header.size); | 480 | uint16_t ret = ntohs (hello->header.size); |
481 | |||
484 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || | 482 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || |
485 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) | 483 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) |
486 | return GNUNET_SYSERR; | 484 | return GNUNET_SYSERR; |
@@ -498,15 +496,16 @@ GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello, | |||
498 | */ | 496 | */ |
499 | int | 497 | int |
500 | GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello, | 498 | GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello, |
501 | struct GNUNET_PeerIdentity *peer) | 499 | struct GNUNET_PeerIdentity *peer) |
502 | { | 500 | { |
503 | uint16_t ret = ntohs (hello->header.size); | 501 | uint16_t ret = ntohs (hello->header.size); |
502 | |||
504 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || | 503 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || |
505 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) | 504 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) |
506 | return GNUNET_SYSERR; | 505 | return GNUNET_SYSERR; |
507 | GNUNET_CRYPTO_hash (&hello->publicKey, | 506 | GNUNET_CRYPTO_hash (&hello->publicKey, |
508 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | 507 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), |
509 | &peer->hashPubKey); | 508 | &peer->hashPubKey); |
510 | return GNUNET_OK; | 509 | return GNUNET_OK; |
511 | } | 510 | } |
512 | 511 | ||
@@ -523,6 +522,7 @@ struct GNUNET_MessageHeader * | |||
523 | GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello) | 522 | GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello) |
524 | { | 523 | { |
525 | uint16_t ret = ntohs (hello->header.size); | 524 | uint16_t ret = ntohs (hello->header.size); |
525 | |||
526 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || | 526 | if ((ret < sizeof (struct GNUNET_HELLO_Message)) || |
527 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) | 527 | (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO)) |
528 | return NULL; | 528 | return NULL; |
@@ -538,11 +538,11 @@ struct EqualsContext | |||
538 | struct GNUNET_TIME_Absolute result; | 538 | struct GNUNET_TIME_Absolute result; |
539 | 539 | ||
540 | const struct GNUNET_HELLO_Message *h2; | 540 | const struct GNUNET_HELLO_Message *h2; |
541 | 541 | ||
542 | const char *tname; | 542 | const char *tname; |
543 | 543 | ||
544 | const void *addr; | 544 | const void *addr; |
545 | 545 | ||
546 | struct GNUNET_TIME_Absolute expiration; | 546 | struct GNUNET_TIME_Absolute expiration; |
547 | 547 | ||
548 | int found; | 548 | int found; |
@@ -554,27 +554,23 @@ struct EqualsContext | |||
554 | 554 | ||
555 | static int | 555 | static int |
556 | find_other_matching (void *cls, | 556 | find_other_matching (void *cls, |
557 | const char *tname, | 557 | const char *tname, |
558 | struct GNUNET_TIME_Absolute expiration, | 558 | struct GNUNET_TIME_Absolute expiration, |
559 | const void *addr, uint16_t addrlen) | 559 | const void *addr, uint16_t addrlen) |
560 | { | 560 | { |
561 | struct EqualsContext *ec = cls; | 561 | struct EqualsContext *ec = cls; |
562 | 562 | ||
563 | if (expiration.abs_value < ec->expiration_limit.abs_value) | 563 | if (expiration.abs_value < ec->expiration_limit.abs_value) |
564 | return GNUNET_YES; | 564 | return GNUNET_YES; |
565 | if ( (addrlen == ec->addrlen) && | 565 | if ((addrlen == ec->addrlen) && |
566 | (0 == strcmp (tname, | 566 | (0 == strcmp (tname, |
567 | ec->tname)) && | 567 | ec->tname)) && (0 == memcmp (addr, ec->addr, addrlen))) |
568 | (0 == memcmp (addr, | 568 | { |
569 | ec->addr, | 569 | ec->found = GNUNET_YES; |
570 | addrlen)) ) | 570 | if (expiration.abs_value < ec->expiration.abs_value) |
571 | { | 571 | ec->result = GNUNET_TIME_absolute_min (expiration, ec->result); |
572 | ec->found = GNUNET_YES; | 572 | return GNUNET_SYSERR; |
573 | if (expiration.abs_value < ec->expiration.abs_value) | 573 | } |
574 | ec->result = GNUNET_TIME_absolute_min (expiration, | ||
575 | ec->result); | ||
576 | return GNUNET_SYSERR; | ||
577 | } | ||
578 | return GNUNET_YES; | 574 | return GNUNET_YES; |
579 | } | 575 | } |
580 | 576 | ||
@@ -594,15 +590,12 @@ find_matching (void *cls, | |||
594 | ec->addr = addr; | 590 | ec->addr = addr; |
595 | ec->addrlen = addrlen; | 591 | ec->addrlen = addrlen; |
596 | ec->found = GNUNET_NO; | 592 | ec->found = GNUNET_NO; |
597 | GNUNET_HELLO_iterate_addresses (ec->h2, | 593 | GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec); |
598 | GNUNET_NO, | ||
599 | &find_other_matching, | ||
600 | ec); | ||
601 | if (ec->found == GNUNET_NO) | 594 | if (ec->found == GNUNET_NO) |
602 | { | 595 | { |
603 | ec->result = GNUNET_TIME_UNIT_ZERO_ABS; | 596 | ec->result = GNUNET_TIME_UNIT_ZERO_ABS; |
604 | return GNUNET_SYSERR; | 597 | return GNUNET_SYSERR; |
605 | } | 598 | } |
606 | return GNUNET_OK; | 599 | return GNUNET_OK; |
607 | } | 600 | } |
608 | 601 | ||
@@ -622,34 +615,26 @@ find_matching (void *cls, | |||
622 | * zero if the some addresses with expirations >= now | 615 | * zero if the some addresses with expirations >= now |
623 | * do not match at all | 616 | * do not match at all |
624 | */ | 617 | */ |
625 | struct GNUNET_TIME_Absolute | 618 | struct GNUNET_TIME_Absolute |
626 | GNUNET_HELLO_equals (const struct | 619 | GNUNET_HELLO_equals (const struct |
627 | GNUNET_HELLO_Message *h1, | 620 | GNUNET_HELLO_Message *h1, |
628 | const struct | 621 | const struct |
629 | GNUNET_HELLO_Message *h2, | 622 | GNUNET_HELLO_Message *h2, struct GNUNET_TIME_Absolute now) |
630 | struct GNUNET_TIME_Absolute now) | ||
631 | { | 623 | { |
632 | struct EqualsContext ec; | 624 | struct EqualsContext ec; |
633 | 625 | ||
634 | if (0 != memcmp (&h1->publicKey, | 626 | if (0 != memcmp (&h1->publicKey, |
635 | &h2->publicKey, | 627 | &h2->publicKey, |
636 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) | 628 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) |
637 | return GNUNET_TIME_UNIT_ZERO_ABS; | 629 | return GNUNET_TIME_UNIT_ZERO_ABS; |
638 | ec.expiration_limit = now; | 630 | ec.expiration_limit = now; |
639 | ec.result = GNUNET_TIME_UNIT_FOREVER_ABS; | 631 | ec.result = GNUNET_TIME_UNIT_FOREVER_ABS; |
640 | ec.h2 = h2; | 632 | ec.h2 = h2; |
641 | GNUNET_HELLO_iterate_addresses (h1, | 633 | GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec); |
642 | GNUNET_NO, | 634 | if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value) |
643 | &find_matching, | 635 | return ec.result; |
644 | &ec); | ||
645 | if (ec.result.abs_value == | ||
646 | GNUNET_TIME_UNIT_ZERO.rel_value) | ||
647 | return ec.result; | ||
648 | ec.h2 = h1; | 636 | ec.h2 = h1; |
649 | GNUNET_HELLO_iterate_addresses (h2, | 637 | GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec); |
650 | GNUNET_NO, | ||
651 | &find_matching, | ||
652 | &ec); | ||
653 | return ec.result; | 638 | return ec.result; |
654 | } | 639 | } |
655 | 640 | ||