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_server_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 tname name of the transport plugin
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
73 * @param max maximum number of bytes to copy to target
74 * @return number of bytes copied, 0 if
75 * the target buffer was not big enough.
78 GNUNET_HELLO_add_address (const char *tname,
79 struct GNUNET_TIME_Absolute expiration,
80 const void *addr, uint16_t addr_len, char *target,
85 struct GNUNET_TIME_AbsoluteNBO exp;
87 slen = strlen (tname) + 1;
88 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
91 exp = GNUNET_TIME_absolute_hton (expiration);
92 alen = htons (addr_len);
93 memcpy (target, tname, slen);
94 memcpy (&target[slen], &alen, sizeof (uint16_t));
95 slen += sizeof (uint16_t);
96 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
97 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
98 memcpy (&target[slen], addr, addr_len);
105 * Get the size of an address entry in a HELLO message.
107 * @param buf pointer to the start of the address entry
108 * @param max maximum size of the entry (end of buf)
109 * @param ralen set to the address length
110 * @return size of the entry, or 0 if max is not large enough
113 get_hello_address_size (const char *buf, size_t max, uint16_t * ralen)
123 while ((left > 0) && ('\0' != *pos))
131 /* 0-termination not found */
137 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
139 /* not enough space for addrlen */
143 memcpy (&alen, pos, sizeof (uint16_t));
146 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
149 /* not enough space for addr */
158 * Construct a HELLO message given the public key,
159 * expiration time and an iterator that spews the
160 * transport addresses.
162 * @return the hello message
164 struct GNUNET_HELLO_Message *
165 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
167 GNUNET_HELLO_GenerateAddressListCallback addrgen,
170 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
171 sizeof (struct GNUNET_HELLO_Message)];
175 struct GNUNET_HELLO_Message *hello;
177 max = sizeof (buffer);
181 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
187 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
188 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
189 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
190 memcpy (&hello->publicKey, publicKey,
191 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
192 memcpy (&hello[1], buffer, used);
198 * Iterate over all of the addresses in the HELLO.
200 * @param msg HELLO to iterate over
201 * @param return_modified if a modified copy should be returned,
202 * otherwise NULL will be returned
203 * @param it iterator to call on each address
204 * @param it_cls closure for it
206 struct GNUNET_HELLO_Message *
207 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
209 GNUNET_HELLO_AddressIterator it, void *it_cls)
212 struct GNUNET_HELLO_Message *ret;
219 struct GNUNET_TIME_AbsoluteNBO expire;
222 msize = GNUNET_HELLO_size (msg);
223 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
224 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
229 ret = GNUNET_malloc (msize);
230 memcpy (ret, msg, msize);
232 inptr = (const char *) &msg[1];
233 insize = msize - sizeof (struct GNUNET_HELLO_Message);
235 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
238 esize = get_hello_address_size (inptr, insize, &alen);
242 GNUNET_free_non_null (ret);
246 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
247 sizeof (struct GNUNET_TIME_AbsoluteNBO));
249 it (it_cls, inptr, GNUNET_TIME_absolute_ntoh (expire),
250 &inptr[esize - alen], alen);
251 if (iret == GNUNET_SYSERR)
254 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
257 if ((iret == GNUNET_OK) && (ret != NULL))
259 memcpy (woff, inptr, esize);
267 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
278 struct GNUNET_TIME_Absolute expiration;
283 get_match_exp (void *cls, const char *tname,
284 struct GNUNET_TIME_Absolute expiration, const void *addr,
287 struct ExpireContext *ec = cls;
289 if ((addrlen == ec->addrlen) && (0 == memcmp (addr, ec->addr, addrlen)) &&
290 (0 == strcmp (tname, ec->tname)))
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, const char *tname,
315 struct GNUNET_TIME_Absolute expiration, const void *addr,
318 struct MergeContext *mc = cls;
319 struct ExpireContext ec;
322 ec.addrlen = addrlen;
323 ec.found = GNUNET_NO;
325 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
326 if ((ec.found == GNUNET_NO) ||
327 (ec.expiration.abs_value < expiration.abs_value) ||
328 ((ec.expiration.abs_value == expiration.abs_value) &&
329 (mc->take_equal == GNUNET_YES)))
332 GNUNET_HELLO_add_address (tname, expiration, addr, addrlen,
333 &mc->buf[mc->ret], mc->max - mc->ret);
340 merge_addr (void *cls, size_t max, void *buf)
342 struct MergeContext *mc = cls;
349 mc->take_equal = GNUNET_NO;
351 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
352 mc->take_equal = GNUNET_YES;
354 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
361 * Construct a HELLO message by merging the
362 * addresses in two existing HELLOs (which
363 * must be for the same peer).
365 * @param h1 first HELLO message
366 * @param h2 the second HELLO message
367 * @return the combined hello message
369 struct GNUNET_HELLO_Message *
370 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
371 const struct GNUNET_HELLO_Message *h2)
373 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
375 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
381 struct GNUNET_TIME_Absolute expiration_limit;
383 GNUNET_HELLO_AddressIterator it;
387 const struct GNUNET_HELLO_Message *old_hello;
392 delta_match (void *cls, const char *tname,
393 struct GNUNET_TIME_Absolute expiration, const void *addr,
396 struct DeltaContext *dc = cls;
398 struct ExpireContext ec;
401 ec.addrlen = addrlen;
402 ec.found = GNUNET_NO;
404 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
406 if ((ec.found == GNUNET_YES) &&
407 ((ec.expiration.abs_value > expiration.abs_value) ||
408 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
409 return GNUNET_YES; /* skip */
410 ret = dc->it (dc->it_cls, tname, expiration, addr, addrlen);
416 * Iterate over addresses in "new_hello" that
417 * are NOT already present in "old_hello".
419 * @param new_hello a HELLO message
420 * @param old_hello a HELLO message
421 * @param expiration_limit ignore addresses in old_hello
422 * that expired before the given time stamp
423 * @param it iterator to call on each address
424 * @param it_cls closure for it
427 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
429 const struct GNUNET_HELLO_Message
431 struct GNUNET_TIME_Absolute
433 GNUNET_HELLO_AddressIterator it,
436 struct DeltaContext dc;
438 dc.expiration_limit = expiration_limit;
441 dc.old_hello = old_hello;
442 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
447 * Return the size of the given HELLO message.
448 * @param hello to inspect
449 * @return the size, 0 if HELLO is invalid
452 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
454 uint16_t ret = ntohs (hello->header.size);
456 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
457 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
464 * Get the public key from a HELLO message.
466 * @param hello the hello message
467 * @param publicKey where to copy the public key information, can be NULL
468 * @return GNUNET_SYSERR if the HELLO was malformed
471 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
472 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
474 uint16_t ret = ntohs (hello->header.size);
476 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
477 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
478 return GNUNET_SYSERR;
479 *publicKey = hello->publicKey;
485 * Get the peer identity from a HELLO message.
487 * @param hello the hello message
488 * @param peer where to store the peer's identity
489 * @return GNUNET_SYSERR if the HELLO was malformed
492 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
493 struct GNUNET_PeerIdentity *peer)
495 uint16_t ret = ntohs (hello->header.size);
497 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
498 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
499 return GNUNET_SYSERR;
500 GNUNET_CRYPTO_hash (&hello->publicKey,
501 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
508 * Get the header from a HELLO message, used so other code
509 * can correctly send HELLO messages.
511 * @param hello the hello message
513 * @return header or NULL if the HELLO was malformed
515 struct GNUNET_MessageHeader *
516 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
518 uint16_t ret = ntohs (hello->header.size);
520 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
521 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
524 return &hello->header;
530 struct GNUNET_TIME_Absolute expiration_limit;
532 struct GNUNET_TIME_Absolute result;
534 const struct GNUNET_HELLO_Message *h2;
540 struct GNUNET_TIME_Absolute expiration;
550 find_other_matching (void *cls, const char *tname,
551 struct GNUNET_TIME_Absolute expiration, const void *addr,
554 struct EqualsContext *ec = cls;
556 if (expiration.abs_value < ec->expiration_limit.abs_value)
558 if ((addrlen == ec->addrlen) && (0 == strcmp (tname, ec->tname)) &&
559 (0 == memcmp (addr, ec->addr, addrlen)))
561 ec->found = GNUNET_YES;
562 if (expiration.abs_value < ec->expiration.abs_value)
563 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
564 return GNUNET_SYSERR;
571 find_matching (void *cls, const char *tname,
572 struct GNUNET_TIME_Absolute expiration, const void *addr,
575 struct EqualsContext *ec = cls;
577 if (expiration.abs_value < ec->expiration_limit.abs_value)
580 ec->expiration = expiration;
582 ec->addrlen = addrlen;
583 ec->found = GNUNET_NO;
584 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
585 if (ec->found == GNUNET_NO)
587 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
588 return GNUNET_SYSERR;
594 * Test if two HELLO messages contain the same addresses.
595 * If they only differ in expiration time, the lowest
596 * expiration time larger than 'now' where they differ
599 * @param h1 first HELLO message
600 * @param h2 the second HELLO message
601 * @param now time to use for deciding which addresses have
602 * expired and should not be considered at all
603 * @return absolute time forever if the two HELLOs are
604 * totally identical; smallest timestamp >= now if
605 * they only differ in timestamps;
606 * zero if the some addresses with expirations >= now
607 * do not match at all
609 struct GNUNET_TIME_Absolute
610 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
611 const struct GNUNET_HELLO_Message *h2,
612 struct GNUNET_TIME_Absolute now)
614 struct EqualsContext ec;
617 memcmp (&h1->publicKey, &h2->publicKey,
618 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
619 return GNUNET_TIME_UNIT_ZERO_ABS;
620 ec.expiration_limit = now;
621 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
623 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
624 if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value)
627 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);