2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief helper library for handling HELLOs
24 * @author Christian Grothoff
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_util_lib.h"
32 * A HELLO message is used to exchange information about
33 * transports with other peers. This struct is always
34 * followed by the actual network addresses which have
37 * 1) transport-name (0-terminated)
38 * 2) address-length (uint16_t, network byte order; possibly
40 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
42 * 4) address (address-length bytes; possibly unaligned!)
44 struct GNUNET_HELLO_Message
47 * Type will be GNUNET_MESSAGE_TYPE_HELLO.
49 struct GNUNET_MessageHeader header;
52 * Always zero (for alignment).
54 uint32_t reserved GNUNET_PACKED;
57 * The public key of the peer.
59 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
65 * Copy the given address information into
66 * the given buffer using the format of HELLOs.
68 * @param address the address
69 * @param expiration expiration for the address
70 * @param target where to copy the address
71 * @param max maximum number of bytes to copy to target
72 * @return number of bytes copied, 0 if
73 * the target buffer was not big enough.
76 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
77 struct GNUNET_TIME_Absolute expiration,
83 struct GNUNET_TIME_AbsoluteNBO exp;
85 slen = strlen (address->transport_name) + 1;
86 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
87 address->address_length > max)
89 exp = GNUNET_TIME_absolute_hton (expiration);
90 alen = htons ((uint16_t) address->address_length);
91 memcpy (target, address->transport_name, slen);
92 memcpy (&target[slen], &alen, sizeof (uint16_t));
93 slen += sizeof (uint16_t);
94 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
95 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
96 memcpy (&target[slen], address->address, address->address_length);
97 slen += address->address_length;
103 * Get the size of an address entry in a HELLO message.
105 * @param buf pointer to the start of the address entry
106 * @param max maximum size of the entry (end of buf)
107 * @param ralen set to the address length
108 * @return size of the entry, or 0 if max is not large enough
111 get_hello_address_size (const char *buf, size_t max, uint16_t * ralen)
121 while ((left > 0) && ('\0' != *pos))
129 /* 0-termination not found */
134 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
136 /* not enough space for addrlen */
140 memcpy (&alen, pos, sizeof (uint16_t));
143 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
146 /* not enough space for addr */
155 * Construct a HELLO message given the public key,
156 * expiration time and an iterator that spews the
157 * transport addresses.
159 * @return the hello message
161 struct GNUNET_HELLO_Message *
162 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
164 GNUNET_HELLO_GenerateAddressListCallback addrgen,
167 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
168 sizeof (struct GNUNET_HELLO_Message)];
172 struct GNUNET_HELLO_Message *hello;
174 max = sizeof (buffer);
178 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
184 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
185 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
186 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
187 memcpy (&hello->publicKey, publicKey,
188 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
189 memcpy (&hello[1], buffer, used);
195 * Iterate over all of the addresses in the HELLO.
197 * @param msg HELLO to iterate over
198 * @param return_modified if a modified copy should be returned,
199 * otherwise NULL will be returned
200 * @param it iterator to call on each address
201 * @param it_cls closure for it
203 struct GNUNET_HELLO_Message *
204 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
206 GNUNET_HELLO_AddressIterator it, void *it_cls)
208 struct GNUNET_HELLO_Address address;
210 struct GNUNET_HELLO_Message *ret;
217 struct GNUNET_TIME_AbsoluteNBO expire;
220 msize = GNUNET_HELLO_size (msg);
221 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
222 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
227 ret = GNUNET_malloc (msize);
228 memcpy (ret, msg, msize);
230 inptr = (const char *) &msg[1];
231 insize = msize - sizeof (struct GNUNET_HELLO_Message);
233 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
234 GNUNET_CRYPTO_hash (&msg->publicKey,
235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
236 &address.peer.hashPubKey);
239 esize = get_hello_address_size (inptr, insize, &alen);
243 GNUNET_free_non_null (ret);
247 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
248 sizeof (struct GNUNET_TIME_AbsoluteNBO));
249 address.address = &inptr[esize - alen];
250 address.address_length = alen;
251 address.transport_name = inptr;
252 iret = it (it_cls, &address, GNUNET_TIME_absolute_ntoh (expire));
253 if (iret == GNUNET_SYSERR)
256 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
259 if ((iret == GNUNET_OK) && (ret != NULL))
261 memcpy (woff, inptr, esize);
269 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
276 const struct GNUNET_HELLO_Address *address;
278 struct GNUNET_TIME_Absolute expiration;
283 get_match_exp (void *cls,
284 const struct GNUNET_HELLO_Address *address,
285 struct GNUNET_TIME_Absolute expiration)
287 struct ExpireContext *ec = cls;
289 if (0 == GNUNET_HELLO_address_cmp (address,
292 ec->found = GNUNET_YES;
293 ec->expiration = expiration;
294 return GNUNET_SYSERR; /* done here */
302 const struct GNUNET_HELLO_Message *h1;
303 const struct GNUNET_HELLO_Message *h2;
304 const struct GNUNET_HELLO_Message *other;
314 copy_latest (void *cls,
315 const struct GNUNET_HELLO_Address *address,
316 struct GNUNET_TIME_Absolute expiration)
318 struct MergeContext *mc = cls;
319 struct ExpireContext ec;
321 ec.address = address;
322 ec.found = GNUNET_NO;
323 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
324 if ((ec.found == GNUNET_NO) ||
325 (ec.expiration.abs_value < expiration.abs_value) ||
326 ((ec.expiration.abs_value == expiration.abs_value) &&
327 (mc->take_equal == GNUNET_YES)))
330 GNUNET_HELLO_add_address (address,
332 &mc->buf[mc->ret], mc->max - mc->ret);
339 merge_addr (void *cls, size_t max, void *buf)
341 struct MergeContext *mc = cls;
348 mc->take_equal = GNUNET_NO;
350 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
351 mc->take_equal = GNUNET_YES;
353 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
360 * Construct a HELLO message by merging the
361 * addresses in two existing HELLOs (which
362 * must be for the same peer).
364 * @param h1 first HELLO message
365 * @param h2 the second HELLO message
366 * @return the combined hello message
368 struct GNUNET_HELLO_Message *
369 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
370 const struct GNUNET_HELLO_Message *h2)
372 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
374 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
380 struct GNUNET_TIME_Absolute expiration_limit;
382 GNUNET_HELLO_AddressIterator it;
386 const struct GNUNET_HELLO_Message *old_hello;
391 delta_match (void *cls,
392 const struct GNUNET_HELLO_Address *address,
393 struct GNUNET_TIME_Absolute expiration)
395 struct DeltaContext *dc = cls;
397 struct ExpireContext ec;
399 ec.address = address;
400 ec.found = GNUNET_NO;
401 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
403 if ((ec.found == GNUNET_YES) &&
404 ((ec.expiration.abs_value > expiration.abs_value) ||
405 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
406 return GNUNET_YES; /* skip */
407 ret = dc->it (dc->it_cls, address, expiration);
413 * Iterate over addresses in "new_hello" that
414 * are NOT already present in "old_hello".
416 * @param new_hello a HELLO message
417 * @param old_hello a HELLO message
418 * @param expiration_limit ignore addresses in old_hello
419 * that expired before the given time stamp
420 * @param it iterator to call on each address
421 * @param it_cls closure for it
424 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
426 const struct GNUNET_HELLO_Message
428 struct GNUNET_TIME_Absolute
430 GNUNET_HELLO_AddressIterator it,
433 struct DeltaContext dc;
435 dc.expiration_limit = expiration_limit;
438 dc.old_hello = old_hello;
439 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
444 * Return the size of the given HELLO message.
445 * @param hello to inspect
446 * @return the size, 0 if HELLO is invalid
449 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
451 uint16_t ret = ntohs (hello->header.size);
453 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
454 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
461 * Get the public key from a HELLO message.
463 * @param hello the hello message
464 * @param publicKey where to copy the public key information, can be NULL
465 * @return GNUNET_SYSERR if the HELLO was malformed
468 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
469 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
471 uint16_t ret = ntohs (hello->header.size);
473 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
474 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
475 return GNUNET_SYSERR;
476 *publicKey = hello->publicKey;
482 * Get the peer identity from a HELLO message.
484 * @param hello the hello message
485 * @param peer where to store the peer's identity
486 * @return GNUNET_SYSERR if the HELLO was malformed
489 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
490 struct GNUNET_PeerIdentity *peer)
492 uint16_t ret = ntohs (hello->header.size);
494 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
495 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
496 return GNUNET_SYSERR;
497 GNUNET_CRYPTO_hash (&hello->publicKey,
498 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
505 * Get the header from a HELLO message, used so other code
506 * can correctly send HELLO messages.
508 * @param hello the hello message
510 * @return header or NULL if the HELLO was malformed
512 struct GNUNET_MessageHeader *
513 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
515 uint16_t ret = ntohs (hello->header.size);
517 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
518 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
521 return &hello->header;
527 struct GNUNET_TIME_Absolute expiration_limit;
529 struct GNUNET_TIME_Absolute result;
531 const struct GNUNET_HELLO_Message *h2;
533 const struct GNUNET_HELLO_Address *address;
535 struct GNUNET_TIME_Absolute expiration;
543 find_other_matching (void *cls, const struct GNUNET_HELLO_Address *address,
544 struct GNUNET_TIME_Absolute expiration)
546 struct EqualsContext *ec = cls;
548 if (expiration.abs_value < ec->expiration_limit.abs_value)
551 GNUNET_HELLO_address_cmp (address,
554 ec->found = GNUNET_YES;
555 if (expiration.abs_value < ec->expiration.abs_value)
556 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
557 return GNUNET_SYSERR;
564 find_matching (void *cls, const struct GNUNET_HELLO_Address *address,
565 struct GNUNET_TIME_Absolute expiration)
567 struct EqualsContext *ec = cls;
569 if (expiration.abs_value < ec->expiration_limit.abs_value)
571 ec->address = address;
572 ec->expiration = expiration;
573 ec->found = GNUNET_NO;
574 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
575 if (ec->found == GNUNET_NO)
577 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
578 return GNUNET_SYSERR;
585 * Test if two HELLO messages contain the same addresses.
586 * If they only differ in expiration time, the lowest
587 * expiration time larger than 'now' where they differ
590 * @param h1 first HELLO message
591 * @param h2 the second HELLO message
592 * @param now time to use for deciding which addresses have
593 * expired and should not be considered at all
594 * @return absolute time forever if the two HELLOs are
595 * totally identical; smallest timestamp >= now if
596 * they only differ in timestamps;
597 * zero if the some addresses with expirations >= now
598 * do not match at all
600 struct GNUNET_TIME_Absolute
601 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
602 const struct GNUNET_HELLO_Message *h2,
603 struct GNUNET_TIME_Absolute now)
605 struct EqualsContext ec;
608 memcmp (&h1->publicKey, &h2->publicKey,
609 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
610 return GNUNET_TIME_UNIT_ZERO_ABS;
611 ec.expiration_limit = now;
612 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
614 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
615 if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value)
618 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);
624 find_min_expire (void *cls, const struct GNUNET_HELLO_Address *address,
625 struct GNUNET_TIME_Absolute expiration)
627 struct GNUNET_TIME_Absolute *min = cls;
629 *min = GNUNET_TIME_absolute_min (*min, expiration);
635 * When does the last address in the given HELLO expire?
637 * @param msg HELLO to inspect
638 * @return time the last address expires, 0 if there are no addresses in the HELLO
640 struct GNUNET_TIME_Absolute
641 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg)
643 struct GNUNET_TIME_Absolute ret;
646 GNUNET_HELLO_iterate_addresses (msg, GNUNET_NO, &find_min_expire, &ret);