aboutsummaryrefslogtreecommitdiff
path: root/src/hello
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
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')
-rw-r--r--src/hello/address.c45
-rw-r--r--src/hello/hello.c102
-rw-r--r--src/hello/test_hello.c39
3 files changed, 112 insertions, 74 deletions
diff --git a/src/hello/address.c b/src/hello/address.c
index 51fb296c3..5cfa10185 100644
--- a/src/hello/address.c
+++ b/src/hello/address.c
@@ -61,4 +61,49 @@ GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
61 return addr; 61 return addr;
62} 62}
63 63
64
65/**
66 * Copy an address struct.
67 *
68 * @param address address to copy
69 * @return a copy of the address struct
70 */
71struct GNUNET_HELLO_Address *
72GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address)
73{
74 return GNUNET_HELLO_address_allocate (&address->peer,
75 address->transport_name,
76 address->address,
77 address->address_length);
78}
79
80
81/**
82 * Compare two addresses. Does NOT compare the peer identity,
83 * that is assumed already to match!
84 *
85 * @param a1 first address
86 * @param a2 second address
87 * @return 0 if the addresses are equal, -1 if a1<a2, 1 if a1>a2.
88 */
89int
90GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
91 const struct GNUNET_HELLO_Address *a2)
92{
93 int ret;
94
95 ret = strcmp (a1->transport_name,
96 a2->transport_name);
97 if (0 != ret)
98 return ret;
99 if (a1->address_length < a2->address_length)
100 return -1;
101 if (a1->address_length > a2->address_length)
102 return 1;
103 return memcmp (a1->address,
104 a1->address,
105 a1->address_length);
106}
107
108
64/* end of address.c */ 109/* end of address.c */
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
diff --git a/src/hello/test_hello.c b/src/hello/test_hello.c
index b6cdfcf66..ca69dfc18 100644
--- a/src/hello/test_hello.c
+++ b/src/hello/test_hello.c
@@ -35,55 +35,58 @@ my_addr_gen (void *cls, size_t max, void *buf)
35{ 35{
36 unsigned int *i = cls; 36 unsigned int *i = cls;
37 size_t ret; 37 size_t ret;
38 struct GNUNET_HELLO_Address address;
38 39
39#if DEBUG 40#if DEBUG
40 fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i); 41 fprintf (stderr, "DEBUG: my_addr_gen called with i = %d\n", *i);
41#endif 42#endif
42 if (0 == *i) 43 if (0 == *i)
43 return 0; 44 return 0;
45 memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
46 address.address = "address_information";
47 address.transport_name = "test";
48 address.address_length = *i;
44 ret = 49 ret =
45 GNUNET_HELLO_add_address ("test", GNUNET_TIME_absolute_get (), 50 GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (),
46 "address_information", *i, buf, max); 51 buf, max);
47 (*i)--; 52 (*i)--;
48 return ret; 53 return ret;
49} 54}
50 55
51 56
52static int 57static int
53check_addr (void *cls, const char *tname, 58check_addr (void *cls, const struct GNUNET_HELLO_Address *address,
54 struct GNUNET_TIME_Absolute expiration, const void *addr, 59 struct GNUNET_TIME_Absolute expiration)
55 uint16_t addrlen)
56{ 60{
57 unsigned int *i = cls; 61 unsigned int *i = cls;
58 62
59#if DEBUG 63#if DEBUG
60 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n", 64 fprintf (stderr, "DEBUG: check_addr called with i = %d and addrlen = %u\n",
61 *i, addrlen); 65 *i, address->address_length);
62#endif 66#endif
63 GNUNET_assert (addrlen > 0); 67 GNUNET_assert (address->address_length > 0);
64 GNUNET_assert (*i & (1 << (addrlen - 1))); 68 GNUNET_assert (*i & (1 << (address->address_length - 1)));
65 *i -= (1 << (addrlen - 1)); 69 *i -= (1 << (address->address_length - 1));
66 GNUNET_assert (0 == strncmp ("address_information", addr, addrlen)); 70 GNUNET_assert (0 == strncmp ("address_information", address->address, address->address_length));
67 GNUNET_assert (0 == strcmp ("test", tname)); 71 GNUNET_assert (0 == strcmp ("test", address->transport_name));
68 return GNUNET_OK; 72 return GNUNET_OK;
69} 73}
70 74
71 75
72static int 76static int
73remove_some (void *cls, const char *tname, 77remove_some (void *cls, const struct GNUNET_HELLO_Address *address,
74 struct GNUNET_TIME_Absolute expiration, const void *addr, 78 struct GNUNET_TIME_Absolute expiration)
75 uint16_t addrlen)
76{ 79{
77 unsigned int *i = cls; 80 unsigned int *i = cls;
78 81
79#if DEBUG 82#if DEBUG
80 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n", 83 fprintf (stderr, "DEBUG: remove_some called with i = %d and addrlen = %u\n",
81 *i, addrlen); 84 *i, address->address_length);
82#endif 85#endif
83 GNUNET_assert (addrlen > 0); 86 GNUNET_assert (address->address_length > 0);
84 if (*i & (1 << (addrlen - 1))) 87 if (*i & (1 << (address->address_length - 1)))
85 { 88 {
86 *i -= (1 << (addrlen - 1)); 89 *i -= (1 << (address->address_length - 1));
87 return GNUNET_NO; 90 return GNUNET_NO;
88 } 91 }
89 return GNUNET_OK; 92 return GNUNET_OK;