aboutsummaryrefslogtreecommitdiff
path: root/src/hello/hello.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-11-08 23:20:42 +0000
committerChristian Grothoff <christian@grothoff.org>2011-11-08 23:20:42 +0000
commita983a0267109b1b6a8e16e476e2f2956a8771b94 (patch)
tree79bcae73cdb7b87b4f55d4396e79baea76ef53a6 /src/hello/hello.c
parenta3f8ef5b89dc44fc3acfb8f081a502f3409e4224 (diff)
downloadgnunet-a983a0267109b1b6a8e16e476e2f2956a8771b94.tar.gz
gnunet-a983a0267109b1b6a8e16e476e2f2956a8771b94.zip
refactoring how we handle peer addresses in peerinfo/ats/transport/hello subsystems -- use a struct instead of 3--4 arguments
Diffstat (limited to 'src/hello/hello.c')
-rw-r--r--src/hello/hello.c102
1 files changed, 46 insertions, 56 deletions
diff --git a/src/hello/hello.c b/src/hello/hello.c
index a95c0eee2..b7f29f539 100644
--- a/src/hello/hello.c
+++ b/src/hello/hello.c
@@ -65,38 +65,36 @@ struct GNUNET_HELLO_Message
65 * Copy the given address information into 65 * Copy the given address information into
66 * the given buffer using the format of HELLOs. 66 * the given buffer using the format of HELLOs.
67 * 67 *
68 * @param tname name of the transport plugin 68 * @param addess the address
69 * @param expiration expiration for the address 69 * @param expiration expiration for the address
70 * @param addr the address
71 * @param addr_len length of the address in bytes
72 * @param target where to copy the address 70 * @param target where to copy the address
73 * @param max maximum number of bytes to copy to target 71 * @param max maximum number of bytes to copy to target
74 * @return number of bytes copied, 0 if 72 * @return number of bytes copied, 0 if
75 * the target buffer was not big enough. 73 * the target buffer was not big enough.
76 */ 74 */
77size_t 75size_t
78GNUNET_HELLO_add_address (const char *tname, 76GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
79 struct GNUNET_TIME_Absolute expiration, 77 struct GNUNET_TIME_Absolute expiration,
80 const void *addr, uint16_t addr_len, char *target, 78 char *target,
81 size_t max) 79 size_t max)
82{ 80{
83 uint16_t alen; 81 uint16_t alen;
84 size_t slen; 82 size_t slen;
85 struct GNUNET_TIME_AbsoluteNBO exp; 83 struct GNUNET_TIME_AbsoluteNBO exp;
86 84
87 slen = strlen (tname) + 1; 85 slen = strlen (address->transport_name) + 1;
88 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) + 86 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
89 addr_len > max) 87 address->address_length > max)
90 return 0; 88 return 0;
91 exp = GNUNET_TIME_absolute_hton (expiration); 89 exp = GNUNET_TIME_absolute_hton (expiration);
92 alen = htons (addr_len); 90 alen = htons ((uint16_t) address->address_length);
93 memcpy (target, tname, slen); 91 memcpy (target, address->transport_name, slen);
94 memcpy (&target[slen], &alen, sizeof (uint16_t)); 92 memcpy (&target[slen], &alen, sizeof (uint16_t));
95 slen += sizeof (uint16_t); 93 slen += sizeof (uint16_t);
96 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO)); 94 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
97 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO); 95 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
98 memcpy (&target[slen], addr, addr_len); 96 memcpy (&target[slen], address->address, address->address_length);
99 slen += addr_len; 97 slen += address->address_length;
100 return slen; 98 return slen;
101} 99}
102 100
@@ -207,6 +205,7 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
207 int return_modified, 205 int return_modified,
208 GNUNET_HELLO_AddressIterator it, void *it_cls) 206 GNUNET_HELLO_AddressIterator it, void *it_cls)
209{ 207{
208 struct GNUNET_HELLO_Address address;
210 uint16_t msize; 209 uint16_t msize;
211 struct GNUNET_HELLO_Message *ret; 210 struct GNUNET_HELLO_Message *ret;
212 const char *inptr; 211 const char *inptr;
@@ -232,6 +231,9 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
232 insize = msize - sizeof (struct GNUNET_HELLO_Message); 231 insize = msize - sizeof (struct GNUNET_HELLO_Message);
233 wpos = 0; 232 wpos = 0;
234 woff = (ret != NULL) ? (char *) &ret[1] : NULL; 233 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
234 GNUNET_CRYPTO_hash (&msg->publicKey,
235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
236 &address.peer.hashPubKey);
235 while (insize > 0) 237 while (insize > 0)
236 { 238 {
237 esize = get_hello_address_size (inptr, insize, &alen); 239 esize = get_hello_address_size (inptr, insize, &alen);
@@ -244,9 +246,10 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
244 memcpy (&expire, 246 memcpy (&expire,
245 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)], 247 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
246 sizeof (struct GNUNET_TIME_AbsoluteNBO)); 248 sizeof (struct GNUNET_TIME_AbsoluteNBO));
247 iret = 249 address.address = &inptr[esize - alen];
248 it (it_cls, inptr, GNUNET_TIME_absolute_ntoh (expire), 250 address.address_length = alen;
249 &inptr[esize - alen], alen); 251 address.transport_name = inptr;
252 iret = it (it_cls, &address, GNUNET_TIME_absolute_ntoh (expire));
250 if (iret == GNUNET_SYSERR) 253 if (iret == GNUNET_SYSERR)
251 { 254 {
252 if (ret != NULL) 255 if (ret != NULL)
@@ -270,23 +273,21 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
270 273
271struct ExpireContext 274struct ExpireContext
272{ 275{
273 const void *addr; 276 const struct GNUNET_HELLO_Address *address;
274 const char *tname;
275 uint16_t addrlen;
276 int found; 277 int found;
277 struct GNUNET_TIME_Absolute expiration; 278 struct GNUNET_TIME_Absolute expiration;
278}; 279};
279 280
280 281
281static int 282static int
282get_match_exp (void *cls, const char *tname, 283get_match_exp (void *cls,
283 struct GNUNET_TIME_Absolute expiration, const void *addr, 284 const struct GNUNET_HELLO_Address *address,
284 uint16_t addrlen) 285 struct GNUNET_TIME_Absolute expiration)
285{ 286{
286 struct ExpireContext *ec = cls; 287 struct ExpireContext *ec = cls;
287 288
288 if ((addrlen == ec->addrlen) && (0 == memcmp (addr, ec->addr, addrlen)) && 289 if (0 == GNUNET_HELLO_address_cmp (address,
289 (0 == strcmp (tname, ec->tname))) 290 ec->address))
290 { 291 {
291 ec->found = GNUNET_YES; 292 ec->found = GNUNET_YES;
292 ec->expiration = expiration; 293 ec->expiration = expiration;
@@ -310,17 +311,15 @@ struct MergeContext
310 311
311 312
312static int 313static int
313copy_latest (void *cls, const char *tname, 314copy_latest (void *cls,
314 struct GNUNET_TIME_Absolute expiration, const void *addr, 315 const struct GNUNET_HELLO_Address *address,
315 uint16_t addrlen) 316 struct GNUNET_TIME_Absolute expiration)
316{ 317{
317 struct MergeContext *mc = cls; 318 struct MergeContext *mc = cls;
318 struct ExpireContext ec; 319 struct ExpireContext ec;
319 320
320 ec.addr = addr; 321 ec.address = address;
321 ec.addrlen = addrlen;
322 ec.found = GNUNET_NO; 322 ec.found = GNUNET_NO;
323 ec.tname = tname;
324 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec); 323 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
325 if ((ec.found == GNUNET_NO) || 324 if ((ec.found == GNUNET_NO) ||
326 (ec.expiration.abs_value < expiration.abs_value) || 325 (ec.expiration.abs_value < expiration.abs_value) ||
@@ -328,8 +327,9 @@ copy_latest (void *cls, const char *tname,
328 (mc->take_equal == GNUNET_YES))) 327 (mc->take_equal == GNUNET_YES)))
329 { 328 {
330 mc->ret += 329 mc->ret +=
331 GNUNET_HELLO_add_address (tname, expiration, addr, addrlen, 330 GNUNET_HELLO_add_address (address,
332 &mc->buf[mc->ret], mc->max - mc->ret); 331 expiration,
332 &mc->buf[mc->ret], mc->max - mc->ret);
333 } 333 }
334 return GNUNET_OK; 334 return GNUNET_OK;
335} 335}
@@ -388,25 +388,23 @@ struct DeltaContext
388 388
389 389
390static int 390static int
391delta_match (void *cls, const char *tname, 391delta_match (void *cls,
392 struct GNUNET_TIME_Absolute expiration, const void *addr, 392 const struct GNUNET_HELLO_Address *address,
393 uint16_t addrlen) 393 struct GNUNET_TIME_Absolute expiration)
394{ 394{
395 struct DeltaContext *dc = cls; 395 struct DeltaContext *dc = cls;
396 int ret; 396 int ret;
397 struct ExpireContext ec; 397 struct ExpireContext ec;
398 398
399 ec.addr = addr; 399 ec.address = address;
400 ec.addrlen = addrlen;
401 ec.found = GNUNET_NO; 400 ec.found = GNUNET_NO;
402 ec.tname = tname;
403 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp, 401 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
404 &ec); 402 &ec);
405 if ((ec.found == GNUNET_YES) && 403 if ((ec.found == GNUNET_YES) &&
406 ((ec.expiration.abs_value > expiration.abs_value) || 404 ((ec.expiration.abs_value > expiration.abs_value) ||
407 (ec.expiration.abs_value >= dc->expiration_limit.abs_value))) 405 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
408 return GNUNET_YES; /* skip */ 406 return GNUNET_YES; /* skip */
409 ret = dc->it (dc->it_cls, tname, expiration, addr, addrlen); 407 ret = dc->it (dc->it_cls, address, expiration);
410 return ret; 408 return ret;
411} 409}
412 410
@@ -532,30 +530,26 @@ struct EqualsContext
532 530
533 const struct GNUNET_HELLO_Message *h2; 531 const struct GNUNET_HELLO_Message *h2;
534 532
535 const char *tname; 533 const struct GNUNET_HELLO_Address *address;
536
537 const void *addr;
538 534
539 struct GNUNET_TIME_Absolute expiration; 535 struct GNUNET_TIME_Absolute expiration;
540 536
541 int found; 537 int found;
542 538
543 uint16_t addrlen;
544
545}; 539};
546 540
547 541
548static int 542static int
549find_other_matching (void *cls, const char *tname, 543find_other_matching (void *cls, const struct GNUNET_HELLO_Address *address,
550 struct GNUNET_TIME_Absolute expiration, const void *addr, 544 struct GNUNET_TIME_Absolute expiration)
551 uint16_t addrlen)
552{ 545{
553 struct EqualsContext *ec = cls; 546 struct EqualsContext *ec = cls;
554 547
555 if (expiration.abs_value < ec->expiration_limit.abs_value) 548 if (expiration.abs_value < ec->expiration_limit.abs_value)
556 return GNUNET_YES; 549 return GNUNET_YES;
557 if ((addrlen == ec->addrlen) && (0 == strcmp (tname, ec->tname)) && 550 if (0 ==
558 (0 == memcmp (addr, ec->addr, addrlen))) 551 GNUNET_HELLO_address_cmp (address,
552 ec->address))
559 { 553 {
560 ec->found = GNUNET_YES; 554 ec->found = GNUNET_YES;
561 if (expiration.abs_value < ec->expiration.abs_value) 555 if (expiration.abs_value < ec->expiration.abs_value)
@@ -567,18 +561,15 @@ find_other_matching (void *cls, const char *tname,
567 561
568 562
569static int 563static int
570find_matching (void *cls, const char *tname, 564find_matching (void *cls, const struct GNUNET_HELLO_Address *address,
571 struct GNUNET_TIME_Absolute expiration, const void *addr, 565 struct GNUNET_TIME_Absolute expiration)
572 uint16_t addrlen)
573{ 566{
574 struct EqualsContext *ec = cls; 567 struct EqualsContext *ec = cls;
575 568
576 if (expiration.abs_value < ec->expiration_limit.abs_value) 569 if (expiration.abs_value < ec->expiration_limit.abs_value)
577 return GNUNET_YES; 570 return GNUNET_YES;
578 ec->tname = tname; 571 ec->address = address;
579 ec->expiration = expiration; 572 ec->expiration = expiration;
580 ec->addr = addr;
581 ec->addrlen = addrlen;
582 ec->found = GNUNET_NO; 573 ec->found = GNUNET_NO;
583 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec); 574 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
584 if (ec->found == GNUNET_NO) 575 if (ec->found == GNUNET_NO)
@@ -630,9 +621,8 @@ GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
630 621
631 622
632static int 623static int
633find_min_expire (void *cls, const char *tname, 624find_min_expire (void *cls, const struct GNUNET_HELLO_Address *address,
634 struct GNUNET_TIME_Absolute expiration, const void *addr, 625 struct GNUNET_TIME_Absolute expiration)
635 uint16_t addrlen)
636{ 626{
637 struct GNUNET_TIME_Absolute *min = cls; 627 struct GNUNET_TIME_Absolute *min = cls;
638 628