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,
81 uint16_t addr_len, char *target, size_t max)
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));
250 GNUNET_TIME_absolute_ntoh (expire), &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,
285 struct GNUNET_TIME_Absolute expiration,
286 const void *addr, uint16_t addrlen)
288 struct ExpireContext *ec = cls;
290 if ((addrlen == ec->addrlen) &&
291 (0 == memcmp (addr, ec->addr, addrlen)) &&
292 (0 == strcmp (tname, ec->tname)))
294 ec->found = GNUNET_YES;
295 ec->expiration = expiration;
296 return GNUNET_SYSERR; /* done here */
304 const struct GNUNET_HELLO_Message *h1;
305 const struct GNUNET_HELLO_Message *h2;
306 const struct GNUNET_HELLO_Message *other;
316 copy_latest (void *cls,
318 struct GNUNET_TIME_Absolute expiration,
319 const void *addr, uint16_t addrlen)
321 struct MergeContext *mc = cls;
322 struct ExpireContext ec;
325 ec.addrlen = addrlen;
326 ec.found = GNUNET_NO;
328 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
329 if ((ec.found == GNUNET_NO) ||
330 (ec.expiration.abs_value < expiration.abs_value) ||
331 ((ec.expiration.abs_value == expiration.abs_value) &&
332 (mc->take_equal == GNUNET_YES)))
334 mc->ret += GNUNET_HELLO_add_address (tname,
338 &mc->buf[mc->ret], mc->max - mc->ret);
345 merge_addr (void *cls, size_t max, void *buf)
347 struct MergeContext *mc = cls;
354 mc->take_equal = GNUNET_NO;
356 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
357 mc->take_equal = GNUNET_YES;
359 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
366 * Construct a HELLO message by merging the
367 * addresses in two existing HELLOs (which
368 * must be for the same peer).
370 * @param h1 first HELLO message
371 * @param h2 the second HELLO message
372 * @return the combined hello message
374 struct GNUNET_HELLO_Message *
375 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
376 const struct GNUNET_HELLO_Message *h2)
378 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
380 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
386 struct GNUNET_TIME_Absolute expiration_limit;
388 GNUNET_HELLO_AddressIterator it;
392 const struct GNUNET_HELLO_Message *old_hello;
397 delta_match (void *cls,
399 struct GNUNET_TIME_Absolute expiration,
400 const void *addr, uint16_t addrlen)
402 struct DeltaContext *dc = cls;
404 struct ExpireContext ec;
407 ec.addrlen = addrlen;
408 ec.found = GNUNET_NO;
410 GNUNET_HELLO_iterate_addresses (dc->old_hello,
411 GNUNET_NO, &get_match_exp, &ec);
412 if ((ec.found == GNUNET_YES) &&
413 ((ec.expiration.abs_value > expiration.abs_value) ||
414 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
415 return GNUNET_YES; /* skip */
416 ret = dc->it (dc->it_cls, tname, expiration, addr, addrlen);
422 * Iterate over addresses in "new_hello" that
423 * are NOT already present in "old_hello".
425 * @param new_hello a HELLO message
426 * @param old_hello a HELLO message
427 * @param expiration_limit ignore addresses in old_hello
428 * that expired before the given time stamp
429 * @param it iterator to call on each address
430 * @param it_cls closure for it
433 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
435 const struct GNUNET_HELLO_Message
437 struct GNUNET_TIME_Absolute
439 GNUNET_HELLO_AddressIterator it,
442 struct DeltaContext dc;
444 dc.expiration_limit = expiration_limit;
447 dc.old_hello = old_hello;
448 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
453 * Return the size of the given HELLO message.
454 * @param hello to inspect
455 * @return the size, 0 if HELLO is invalid
458 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
460 uint16_t ret = ntohs (hello->header.size);
462 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
463 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
470 * Get the public key from a HELLO message.
472 * @param hello the hello message
473 * @param publicKey where to copy the public key information, can be NULL
474 * @return GNUNET_SYSERR if the HELLO was malformed
477 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
478 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
480 uint16_t ret = ntohs (hello->header.size);
482 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
483 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
484 return GNUNET_SYSERR;
485 *publicKey = hello->publicKey;
491 * Get the peer identity from a HELLO message.
493 * @param hello the hello message
494 * @param peer where to store the peer's identity
495 * @return GNUNET_SYSERR if the HELLO was malformed
498 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
499 struct GNUNET_PeerIdentity *peer)
501 uint16_t ret = ntohs (hello->header.size);
503 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
504 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
505 return GNUNET_SYSERR;
506 GNUNET_CRYPTO_hash (&hello->publicKey,
507 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
514 * Get the header from a HELLO message, used so other code
515 * can correctly send HELLO messages.
517 * @param hello the hello message
519 * @return header or NULL if the HELLO was malformed
521 struct GNUNET_MessageHeader *
522 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
524 uint16_t ret = ntohs (hello->header.size);
526 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
527 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
530 return &hello->header;
536 struct GNUNET_TIME_Absolute expiration_limit;
538 struct GNUNET_TIME_Absolute result;
540 const struct GNUNET_HELLO_Message *h2;
546 struct GNUNET_TIME_Absolute expiration;
556 find_other_matching (void *cls,
558 struct GNUNET_TIME_Absolute expiration,
559 const void *addr, uint16_t addrlen)
561 struct EqualsContext *ec = cls;
563 if (expiration.abs_value < ec->expiration_limit.abs_value)
565 if ((addrlen == ec->addrlen) &&
567 ec->tname)) && (0 == memcmp (addr, ec->addr, addrlen)))
569 ec->found = GNUNET_YES;
570 if (expiration.abs_value < ec->expiration.abs_value)
571 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
572 return GNUNET_SYSERR;
579 find_matching (void *cls,
581 struct GNUNET_TIME_Absolute expiration,
582 const void *addr, uint16_t addrlen)
584 struct EqualsContext *ec = cls;
586 if (expiration.abs_value < ec->expiration_limit.abs_value)
589 ec->expiration = expiration;
591 ec->addrlen = addrlen;
592 ec->found = GNUNET_NO;
593 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
594 if (ec->found == GNUNET_NO)
596 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
597 return GNUNET_SYSERR;
603 * Test if two HELLO messages contain the same addresses.
604 * If they only differ in expiration time, the lowest
605 * expiration time larger than 'now' where they differ
608 * @param h1 first HELLO message
609 * @param h2 the second HELLO message
610 * @param now time to use for deciding which addresses have
611 * expired and should not be considered at all
612 * @return absolute time forever if the two HELLOs are
613 * totally identical; smallest timestamp >= now if
614 * they only differ in timestamps;
615 * zero if the some addresses with expirations >= now
616 * do not match at all
618 struct GNUNET_TIME_Absolute
619 GNUNET_HELLO_equals (const struct
620 GNUNET_HELLO_Message *h1,
622 GNUNET_HELLO_Message *h2, struct GNUNET_TIME_Absolute now)
624 struct EqualsContext ec;
626 if (0 != memcmp (&h1->publicKey,
628 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
629 return GNUNET_TIME_UNIT_ZERO_ABS;
630 ec.expiration_limit = now;
631 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
633 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
634 if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value)
637 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);