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 2, 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 - 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),
251 &inptr[esize - alen], alen);
252 if (iret == GNUNET_SYSERR)
256 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);
280 struct GNUNET_TIME_Absolute expiration;
285 get_match_exp (void *cls,
287 struct GNUNET_TIME_Absolute expiration,
288 const void *addr, uint16_t addrlen)
290 struct ExpireContext *ec = cls;
292 if ( (addrlen == ec->addrlen) &&
293 (0 == memcmp (addr, ec->addr, addrlen)) &&
294 (0 == strcmp (tname, ec->tname)) )
296 ec->found = GNUNET_YES;
297 ec->expiration = expiration;
298 return GNUNET_SYSERR; /* done here */
306 const struct GNUNET_HELLO_Message *h1;
307 const struct GNUNET_HELLO_Message *h2;
308 const struct GNUNET_HELLO_Message *other;
318 copy_latest (void *cls,
320 struct GNUNET_TIME_Absolute expiration,
321 const void *addr, uint16_t addrlen)
323 struct MergeContext *mc = cls;
324 struct ExpireContext ec;
327 ec.addrlen = addrlen;
328 ec.found = GNUNET_NO;
330 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
331 if ( (ec.found == GNUNET_NO) ||
332 (ec.expiration.value < expiration.value) ||
333 ( (ec.expiration.value == expiration.value) &&
334 (mc->take_equal == GNUNET_YES) ) )
336 mc->ret += GNUNET_HELLO_add_address (tname,
348 merge_addr (void *cls, size_t max, void *buf)
350 struct MergeContext *mc = cls;
357 mc->take_equal = GNUNET_NO;
359 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
360 mc->take_equal = GNUNET_YES;
362 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
369 * Construct a HELLO message by merging the
370 * addresses in two existing HELLOs (which
371 * must be for the same peer).
373 * @param h1 first HELLO message
374 * @param h2 the second HELLO message
375 * @return the combined hello message
377 struct GNUNET_HELLO_Message *
378 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
379 const struct GNUNET_HELLO_Message *h2)
381 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
383 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
389 struct GNUNET_TIME_Absolute expiration_limit;
391 GNUNET_HELLO_AddressIterator it;
395 const struct GNUNET_HELLO_Message *old_hello;
400 delta_match (void *cls,
402 struct GNUNET_TIME_Absolute expiration,
403 const void *addr, uint16_t addrlen)
405 struct DeltaContext *dc = cls;
407 struct ExpireContext ec;
410 ec.addrlen = addrlen;
411 ec.found = GNUNET_NO;
413 GNUNET_HELLO_iterate_addresses (dc->old_hello,
414 GNUNET_NO, &get_match_exp, &ec);
415 if ((ec.found == GNUNET_YES) &&
416 ((ec.expiration.value > expiration.value) ||
417 (ec.expiration.value >= dc->expiration_limit.value)))
418 return GNUNET_YES; /* skip */
419 ret = dc->it (dc->it_cls, tname, expiration, addr, addrlen);
425 * Iterate over addresses in "new_hello" that
426 * are NOT already present in "old_hello".
428 * @param new_hello a HELLO message
429 * @param old_hello a HELLO message
430 * @param expiration_limit ignore addresses in old_hello
431 * that expired before the given time stamp
432 * @param it iterator to call on each address
433 * @param it_cls closure for it
436 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
438 const struct GNUNET_HELLO_Message
440 struct GNUNET_TIME_Absolute
442 GNUNET_HELLO_AddressIterator it,
445 struct DeltaContext dc;
447 dc.expiration_limit = expiration_limit;
450 dc.old_hello = old_hello;
451 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
456 * Return the size of the given HELLO message.
457 * @param hello to inspect
458 * @return the size, 0 if HELLO is invalid
461 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
463 uint16_t ret = ntohs (hello->header.size);
464 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
465 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
472 * Get the public key from a HELLO message.
474 * @param hello the hello message
475 * @param publicKey where to copy the public key information, can be NULL
476 * @return GNUNET_SYSERR if the HELLO was malformed
479 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
480 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
483 uint16_t ret = ntohs (hello->header.size);
484 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
485 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
486 return GNUNET_SYSERR;
487 *publicKey = hello->publicKey;
493 * Get the peer identity from a HELLO message.
495 * @param hello the hello message
496 * @param peer where to store the peer's identity
497 * @return GNUNET_SYSERR if the HELLO was malformed
500 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
501 struct GNUNET_PeerIdentity *peer)
503 uint16_t ret = ntohs (hello->header.size);
504 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
505 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
506 return GNUNET_SYSERR;
507 GNUNET_CRYPTO_hash (&hello->publicKey,
508 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
515 * Get the header from a HELLO message, used so other code
516 * can correctly send HELLO messages.
518 * @param hello the hello message
520 * @return header or NULL if the HELLO was malformed
522 struct GNUNET_MessageHeader *
523 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
525 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.value < ec->expiration_limit.value)
565 if ( (addrlen == ec->addrlen) &&
572 ec->found = GNUNET_YES;
573 if (expiration.value < ec->expiration.value)
574 ec->result = GNUNET_TIME_absolute_min (expiration,
576 return GNUNET_SYSERR;
583 find_matching (void *cls,
585 struct GNUNET_TIME_Absolute expiration,
586 const void *addr, uint16_t addrlen)
588 struct EqualsContext *ec = cls;
590 if (expiration.value < ec->expiration_limit.value)
593 ec->expiration = expiration;
595 ec->addrlen = addrlen;
596 ec->found = GNUNET_NO;
597 GNUNET_HELLO_iterate_addresses (ec->h2,
599 &find_other_matching,
601 if (ec->found == GNUNET_NO)
603 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
604 return GNUNET_SYSERR;
610 * Test if two HELLO messages contain the same addresses.
611 * If they only differ in expiration time, the lowest
612 * expiration time larger than 'now' where they differ
615 * @param h1 first HELLO message
616 * @param h2 the second HELLO message
617 * @param now time to use for deciding which addresses have
618 * expired and should not be considered at all
619 * @return absolute time forever if the two HELLOs are
620 * totally identical; smallest timestamp >= now if
621 * they only differ in timestamps;
622 * zero if the some addresses with expirations >= now
623 * do not match at all
625 struct GNUNET_TIME_Absolute
626 GNUNET_HELLO_equals (const struct
627 GNUNET_HELLO_Message *h1,
629 GNUNET_HELLO_Message *h2,
630 struct GNUNET_TIME_Absolute now)
632 struct EqualsContext ec;
634 if (0 != memcmp (&h1->publicKey,
636 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
637 return GNUNET_TIME_UNIT_ZERO_ABS;
638 ec.expiration_limit = now;
639 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
641 GNUNET_HELLO_iterate_addresses (h1,
645 if (ec.result.value ==
646 GNUNET_TIME_UNIT_ZERO.value)
649 GNUNET_HELLO_iterate_addresses (h2,