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, char *target,
82 struct GNUNET_TIME_AbsoluteNBO exp;
84 slen = strlen (address->transport_name) + 1;
85 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
86 address->address_length > max)
88 exp = GNUNET_TIME_absolute_hton (expiration);
89 alen = htons ((uint16_t) address->address_length);
90 memcpy (target, address->transport_name, slen);
91 memcpy (&target[slen], &alen, sizeof (uint16_t));
92 slen += sizeof (uint16_t);
93 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
94 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
95 memcpy (&target[slen], address->address, address->address_length);
96 slen += address->address_length;
102 * Get the size of an address entry in a HELLO message.
104 * @param buf pointer to the start of the address entry
105 * @param max maximum size of the entry (end of buf)
106 * @param ralen set to the address length
107 * @return size of the entry, or 0 if max is not large enough
110 get_hello_address_size (const char *buf, size_t max, uint16_t * ralen)
120 while ((left > 0) && ('\0' != *pos))
128 /* 0-termination not found */
133 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
135 /* not enough space for addrlen */
139 memcpy (&alen, pos, sizeof (uint16_t));
142 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
145 /* not enough space for addr */
154 * Construct a HELLO message given the public key,
155 * expiration time and an iterator that spews the
156 * transport addresses.
158 * @return the hello message
160 struct GNUNET_HELLO_Message *
161 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
163 GNUNET_HELLO_GenerateAddressListCallback addrgen,
166 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
167 sizeof (struct GNUNET_HELLO_Message)];
171 struct GNUNET_HELLO_Message *hello;
173 max = sizeof (buffer);
177 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
183 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
184 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
185 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
186 memcpy (&hello->publicKey, publicKey,
187 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
188 memcpy (&hello[1], buffer, used);
194 * Iterate over all of the addresses in the HELLO.
196 * @param msg HELLO to iterate over
197 * @param return_modified if a modified copy should be returned,
198 * otherwise NULL will be returned
199 * @param it iterator to call on each address
200 * @param it_cls closure for it
202 struct GNUNET_HELLO_Message *
203 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
205 GNUNET_HELLO_AddressIterator it, void *it_cls)
207 struct GNUNET_HELLO_Address address;
209 struct GNUNET_HELLO_Message *ret;
216 struct GNUNET_TIME_AbsoluteNBO expire;
219 msize = GNUNET_HELLO_size (msg);
220 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
221 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
226 ret = GNUNET_malloc (msize);
227 memcpy (ret, msg, msize);
229 inptr = (const char *) &msg[1];
230 insize = msize - sizeof (struct GNUNET_HELLO_Message);
232 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
233 GNUNET_CRYPTO_hash (&msg->publicKey,
234 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
235 &address.peer.hashPubKey);
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));
248 address.address = &inptr[esize - alen];
249 address.address_length = alen;
250 address.transport_name = inptr;
251 iret = it (it_cls, &address, GNUNET_TIME_absolute_ntoh (expire));
252 if (iret == GNUNET_SYSERR)
255 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
258 if ((iret == GNUNET_OK) && (ret != NULL))
260 memcpy (woff, inptr, esize);
268 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
275 const struct GNUNET_HELLO_Address *address;
277 struct GNUNET_TIME_Absolute expiration;
282 get_match_exp (void *cls, const struct GNUNET_HELLO_Address *address,
283 struct GNUNET_TIME_Absolute expiration)
285 struct ExpireContext *ec = cls;
287 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
289 ec->found = GNUNET_YES;
290 ec->expiration = expiration;
291 return GNUNET_SYSERR; /* done here */
299 const struct GNUNET_HELLO_Message *h1;
300 const struct GNUNET_HELLO_Message *h2;
301 const struct GNUNET_HELLO_Message *other;
311 copy_latest (void *cls, const struct GNUNET_HELLO_Address *address,
312 struct GNUNET_TIME_Absolute expiration)
314 struct MergeContext *mc = cls;
315 struct ExpireContext ec;
317 ec.address = address;
318 ec.found = GNUNET_NO;
319 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
320 if ((ec.found == GNUNET_NO) ||
321 (ec.expiration.abs_value < expiration.abs_value) ||
322 ((ec.expiration.abs_value == expiration.abs_value) &&
323 (mc->take_equal == GNUNET_YES)))
326 GNUNET_HELLO_add_address (address, expiration, &mc->buf[mc->ret],
334 merge_addr (void *cls, size_t max, void *buf)
336 struct MergeContext *mc = cls;
343 mc->take_equal = GNUNET_NO;
345 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
346 mc->take_equal = GNUNET_YES;
348 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
355 * Construct a HELLO message by merging the
356 * addresses in two existing HELLOs (which
357 * must be for the same peer).
359 * @param h1 first HELLO message
360 * @param h2 the second HELLO message
361 * @return the combined hello message
363 struct GNUNET_HELLO_Message *
364 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
365 const struct GNUNET_HELLO_Message *h2)
367 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
369 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
375 struct GNUNET_TIME_Absolute expiration_limit;
377 GNUNET_HELLO_AddressIterator it;
381 const struct GNUNET_HELLO_Message *old_hello;
386 delta_match (void *cls, const struct GNUNET_HELLO_Address *address,
387 struct GNUNET_TIME_Absolute expiration)
389 struct DeltaContext *dc = cls;
391 struct ExpireContext ec;
393 ec.address = address;
394 ec.found = GNUNET_NO;
395 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
397 if ((ec.found == GNUNET_YES) &&
398 ((ec.expiration.abs_value > expiration.abs_value) ||
399 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
400 return GNUNET_YES; /* skip */
401 ret = dc->it (dc->it_cls, address, expiration);
407 * Iterate over addresses in "new_hello" that
408 * are NOT already present in "old_hello".
410 * @param new_hello a HELLO message
411 * @param old_hello a HELLO message
412 * @param expiration_limit ignore addresses in old_hello
413 * that expired before the given time stamp
414 * @param it iterator to call on each address
415 * @param it_cls closure for it
418 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
420 const struct GNUNET_HELLO_Message
422 struct GNUNET_TIME_Absolute
424 GNUNET_HELLO_AddressIterator it,
427 struct DeltaContext dc;
429 dc.expiration_limit = expiration_limit;
432 dc.old_hello = old_hello;
433 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
438 * Return the size of the given HELLO message.
439 * @param hello to inspect
440 * @return the size, 0 if HELLO is invalid
443 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
445 uint16_t ret = ntohs (hello->header.size);
447 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
448 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
455 * Get the public key from a HELLO message.
457 * @param hello the hello message
458 * @param publicKey where to copy the public key information, can be NULL
459 * @return GNUNET_SYSERR if the HELLO was malformed
462 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
463 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
465 uint16_t ret = ntohs (hello->header.size);
467 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
468 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
469 return GNUNET_SYSERR;
470 *publicKey = hello->publicKey;
476 * Get the peer identity from a HELLO message.
478 * @param hello the hello message
479 * @param peer where to store the peer's identity
480 * @return GNUNET_SYSERR if the HELLO was malformed
483 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
484 struct GNUNET_PeerIdentity *peer)
486 uint16_t ret = ntohs (hello->header.size);
488 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
489 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
490 return GNUNET_SYSERR;
491 GNUNET_CRYPTO_hash (&hello->publicKey,
492 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
499 * Get the header from a HELLO message, used so other code
500 * can correctly send HELLO messages.
502 * @param hello the hello message
504 * @return header or NULL if the HELLO was malformed
506 struct GNUNET_MessageHeader *
507 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
509 uint16_t ret = ntohs (hello->header.size);
511 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
512 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
515 return &hello->header;
521 struct GNUNET_TIME_Absolute expiration_limit;
523 struct GNUNET_TIME_Absolute result;
525 const struct GNUNET_HELLO_Message *h2;
527 const struct GNUNET_HELLO_Address *address;
529 struct GNUNET_TIME_Absolute expiration;
537 find_other_matching (void *cls, const struct GNUNET_HELLO_Address *address,
538 struct GNUNET_TIME_Absolute expiration)
540 struct EqualsContext *ec = cls;
542 if (expiration.abs_value < ec->expiration_limit.abs_value)
544 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
546 ec->found = GNUNET_YES;
547 if (expiration.abs_value < ec->expiration.abs_value)
548 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
549 return GNUNET_SYSERR;
556 find_matching (void *cls, const struct GNUNET_HELLO_Address *address,
557 struct GNUNET_TIME_Absolute expiration)
559 struct EqualsContext *ec = cls;
561 if (expiration.abs_value < ec->expiration_limit.abs_value)
563 ec->address = address;
564 ec->expiration = expiration;
565 ec->found = GNUNET_NO;
566 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
567 if (ec->found == GNUNET_NO)
569 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
570 return GNUNET_SYSERR;
577 * Test if two HELLO messages contain the same addresses.
578 * If they only differ in expiration time, the lowest
579 * expiration time larger than 'now' where they differ
582 * @param h1 first HELLO message
583 * @param h2 the second HELLO message
584 * @param now time to use for deciding which addresses have
585 * expired and should not be considered at all
586 * @return absolute time forever if the two HELLOs are
587 * totally identical; smallest timestamp >= now if
588 * they only differ in timestamps;
589 * zero if the some addresses with expirations >= now
590 * do not match at all
592 struct GNUNET_TIME_Absolute
593 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
594 const struct GNUNET_HELLO_Message *h2,
595 struct GNUNET_TIME_Absolute now)
597 struct EqualsContext ec;
600 memcmp (&h1->publicKey, &h2->publicKey,
601 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
602 return GNUNET_TIME_UNIT_ZERO_ABS;
603 ec.expiration_limit = now;
604 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
606 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
607 if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value)
610 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);
616 find_min_expire (void *cls, const struct GNUNET_HELLO_Address *address,
617 struct GNUNET_TIME_Absolute expiration)
619 struct GNUNET_TIME_Absolute *min = cls;
621 *min = GNUNET_TIME_absolute_min (*min, expiration);
627 * When does the last address in the given HELLO expire?
629 * @param msg HELLO to inspect
630 * @return time the last address expires, 0 if there are no addresses in the HELLO
632 struct GNUNET_TIME_Absolute
633 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg)
635 struct GNUNET_TIME_Absolute ret;
638 GNUNET_HELLO_iterate_addresses (msg, GNUNET_NO, &find_min_expire, &ret);