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"
31 GNUNET_NETWORK_STRUCT_BEGIN
34 * A HELLO message is used to exchange information about
35 * transports with other peers. This struct is always
36 * followed by the actual network addresses which have
39 * 1) transport-name (0-terminated)
40 * 2) address-length (uint16_t, network byte order; possibly
42 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
44 * 4) address (address-length bytes; possibly unaligned!)
46 struct GNUNET_HELLO_Message
49 * Type will be GNUNET_MESSAGE_TYPE_HELLO.
51 struct GNUNET_MessageHeader header;
54 * Always zero (for alignment).
56 uint32_t reserved GNUNET_PACKED;
59 * The public key of the peer.
61 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
64 GNUNET_NETWORK_STRUCT_END
67 * Copy the given address information into
68 * the given buffer using the format of HELLOs.
70 * @param address the address
71 * @param expiration expiration for the address
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 struct GNUNET_HELLO_Address *address,
79 struct GNUNET_TIME_Absolute expiration, char *target,
84 struct GNUNET_TIME_AbsoluteNBO exp;
86 slen = strlen (address->transport_name) + 1;
87 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
88 address->address_length > max)
90 exp = GNUNET_TIME_absolute_hton (expiration);
91 alen = htons ((uint16_t) address->address_length);
92 memcpy (target, address->transport_name, slen);
93 memcpy (&target[slen], &alen, sizeof (uint16_t));
94 slen += sizeof (uint16_t);
95 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
96 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
97 memcpy (&target[slen], address->address, address->address_length);
98 slen += address->address_length;
104 * Get the size of an address entry in a HELLO message.
106 * @param buf pointer to the start of the address entry
107 * @param max maximum size of the entry (end of buf)
108 * @param ralen set to the address length
109 * @return size of the entry, or 0 if max is not large enough
112 get_hello_address_size (const char *buf, size_t max, uint16_t * ralen)
122 while ((left > 0) && ('\0' != *pos))
130 /* 0-termination not found */
135 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
137 /* not enough space for addrlen */
141 memcpy (&alen, pos, sizeof (uint16_t));
144 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
147 /* not enough space for addr */
156 * Construct a HELLO message given the public key,
157 * expiration time and an iterator that spews the
158 * transport addresses.
160 * @return the hello message
162 struct GNUNET_HELLO_Message *
163 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
165 GNUNET_HELLO_GenerateAddressListCallback addrgen,
168 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
169 sizeof (struct GNUNET_HELLO_Message)];
173 struct GNUNET_HELLO_Message *hello;
175 max = sizeof (buffer);
179 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
185 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
186 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
187 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
188 memcpy (&hello->publicKey, publicKey,
189 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
190 memcpy (&hello[1], buffer, used);
196 * Iterate over all of the addresses in the HELLO.
198 * @param msg HELLO to iterate over
199 * @param return_modified if a modified copy should be returned,
200 * otherwise NULL will be returned
201 * @param it iterator to call on each address
202 * @param it_cls closure for it
204 struct GNUNET_HELLO_Message *
205 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
207 GNUNET_HELLO_AddressIterator it, void *it_cls)
209 struct GNUNET_HELLO_Address address;
211 struct GNUNET_HELLO_Message *ret;
218 struct GNUNET_TIME_AbsoluteNBO expire;
221 msize = GNUNET_HELLO_size (msg);
222 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
223 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
228 ret = GNUNET_malloc (msize);
229 memcpy (ret, msg, msize);
231 inptr = (const char *) &msg[1];
232 insize = msize - sizeof (struct GNUNET_HELLO_Message);
234 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
235 GNUNET_CRYPTO_hash (&msg->publicKey,
236 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
237 &address.peer.hashPubKey);
240 esize = get_hello_address_size (inptr, insize, &alen);
244 GNUNET_free_non_null (ret);
248 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
249 sizeof (struct GNUNET_TIME_AbsoluteNBO));
250 address.address = &inptr[esize - alen];
251 address.address_length = alen;
252 address.transport_name = inptr;
253 iret = it (it_cls, &address, GNUNET_TIME_absolute_ntoh (expire));
254 if (iret == GNUNET_SYSERR)
257 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
260 if ((iret == GNUNET_OK) && (ret != NULL))
262 memcpy (woff, inptr, esize);
270 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
277 const struct GNUNET_HELLO_Address *address;
279 struct GNUNET_TIME_Absolute expiration;
284 get_match_exp (void *cls, 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, ec->address))
291 ec->found = GNUNET_YES;
292 ec->expiration = expiration;
293 return GNUNET_SYSERR; /* done here */
301 const struct GNUNET_HELLO_Message *h1;
302 const struct GNUNET_HELLO_Message *h2;
303 const struct GNUNET_HELLO_Message *other;
313 copy_latest (void *cls, const struct GNUNET_HELLO_Address *address,
314 struct GNUNET_TIME_Absolute expiration)
316 struct MergeContext *mc = cls;
317 struct ExpireContext ec;
319 ec.address = address;
320 ec.found = GNUNET_NO;
321 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
322 if ((ec.found == GNUNET_NO) ||
323 (ec.expiration.abs_value < expiration.abs_value) ||
324 ((ec.expiration.abs_value == expiration.abs_value) &&
325 (mc->take_equal == GNUNET_YES)))
328 GNUNET_HELLO_add_address (address, expiration, &mc->buf[mc->ret],
336 merge_addr (void *cls, size_t max, void *buf)
338 struct MergeContext *mc = cls;
345 mc->take_equal = GNUNET_NO;
347 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
348 mc->take_equal = GNUNET_YES;
350 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
357 * Construct a HELLO message by merging the
358 * addresses in two existing HELLOs (which
359 * must be for the same peer).
361 * @param h1 first HELLO message
362 * @param h2 the second HELLO message
363 * @return the combined hello message
365 struct GNUNET_HELLO_Message *
366 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
367 const struct GNUNET_HELLO_Message *h2)
369 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
371 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
377 struct GNUNET_TIME_Absolute expiration_limit;
379 GNUNET_HELLO_AddressIterator it;
383 const struct GNUNET_HELLO_Message *old_hello;
388 delta_match (void *cls, const struct GNUNET_HELLO_Address *address,
389 struct GNUNET_TIME_Absolute expiration)
391 struct DeltaContext *dc = cls;
393 struct ExpireContext ec;
395 ec.address = address;
396 ec.found = GNUNET_NO;
397 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
399 if ((ec.found == GNUNET_YES) &&
400 ((ec.expiration.abs_value > expiration.abs_value) ||
401 (ec.expiration.abs_value >= dc->expiration_limit.abs_value)))
402 return GNUNET_YES; /* skip */
403 ret = dc->it (dc->it_cls, address, expiration);
409 * Iterate over addresses in "new_hello" that
410 * are NOT already present in "old_hello".
412 * @param new_hello a HELLO message
413 * @param old_hello a HELLO message
414 * @param expiration_limit ignore addresses in old_hello
415 * that expired before the given time stamp
416 * @param it iterator to call on each address
417 * @param it_cls closure for it
420 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
422 const struct GNUNET_HELLO_Message
424 struct GNUNET_TIME_Absolute
426 GNUNET_HELLO_AddressIterator it,
429 struct DeltaContext dc;
431 dc.expiration_limit = expiration_limit;
434 dc.old_hello = old_hello;
435 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
440 * Return the size of the given HELLO message.
441 * @param hello to inspect
442 * @return the size, 0 if HELLO is invalid
445 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
447 uint16_t ret = ntohs (hello->header.size);
449 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
450 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
457 * Get the public key from a HELLO message.
459 * @param hello the hello message
460 * @param publicKey where to copy the public key information, can be NULL
461 * @return GNUNET_SYSERR if the HELLO was malformed
464 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
465 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
467 uint16_t ret = ntohs (hello->header.size);
469 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
470 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
471 return GNUNET_SYSERR;
472 *publicKey = hello->publicKey;
478 * Get the peer identity from a HELLO message.
480 * @param hello the hello message
481 * @param peer where to store the peer's identity
482 * @return GNUNET_SYSERR if the HELLO was malformed
485 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
486 struct GNUNET_PeerIdentity *peer)
488 uint16_t ret = ntohs (hello->header.size);
490 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
491 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
492 return GNUNET_SYSERR;
493 GNUNET_CRYPTO_hash (&hello->publicKey,
494 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
501 * Get the header from a HELLO message, used so other code
502 * can correctly send HELLO messages.
504 * @param hello the hello message
506 * @return header or NULL if the HELLO was malformed
508 struct GNUNET_MessageHeader *
509 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
511 uint16_t ret = ntohs (hello->header.size);
513 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
514 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
517 return &hello->header;
523 struct GNUNET_TIME_Absolute expiration_limit;
525 struct GNUNET_TIME_Absolute result;
527 const struct GNUNET_HELLO_Message *h2;
529 const struct GNUNET_HELLO_Address *address;
531 struct GNUNET_TIME_Absolute expiration;
539 find_other_matching (void *cls, const struct GNUNET_HELLO_Address *address,
540 struct GNUNET_TIME_Absolute expiration)
542 struct EqualsContext *ec = cls;
544 if (expiration.abs_value < ec->expiration_limit.abs_value)
546 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
548 ec->found = GNUNET_YES;
549 if (expiration.abs_value < ec->expiration.abs_value)
550 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
551 return GNUNET_SYSERR;
558 find_matching (void *cls, const struct GNUNET_HELLO_Address *address,
559 struct GNUNET_TIME_Absolute expiration)
561 struct EqualsContext *ec = cls;
563 if (expiration.abs_value < ec->expiration_limit.abs_value)
565 ec->address = address;
566 ec->expiration = expiration;
567 ec->found = GNUNET_NO;
568 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
569 if (ec->found == GNUNET_NO)
571 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
572 return GNUNET_SYSERR;
579 * Test if two HELLO messages contain the same addresses.
580 * If they only differ in expiration time, the lowest
581 * expiration time larger than 'now' where they differ
584 * @param h1 first HELLO message
585 * @param h2 the second HELLO message
586 * @param now time to use for deciding which addresses have
587 * expired and should not be considered at all
588 * @return absolute time forever if the two HELLOs are
589 * totally identical; smallest timestamp >= now if
590 * they only differ in timestamps;
591 * zero if the some addresses with expirations >= now
592 * do not match at all
594 struct GNUNET_TIME_Absolute
595 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
596 const struct GNUNET_HELLO_Message *h2,
597 struct GNUNET_TIME_Absolute now)
599 struct EqualsContext ec;
602 memcmp (&h1->publicKey, &h2->publicKey,
603 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
604 return GNUNET_TIME_UNIT_ZERO_ABS;
605 ec.expiration_limit = now;
606 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
608 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
609 if (ec.result.abs_value == GNUNET_TIME_UNIT_ZERO.rel_value)
612 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);
618 find_min_expire (void *cls, const struct GNUNET_HELLO_Address *address,
619 struct GNUNET_TIME_Absolute expiration)
621 struct GNUNET_TIME_Absolute *min = cls;
623 *min = GNUNET_TIME_absolute_min (*min, expiration);
629 * When does the last address in the given HELLO expire?
631 * @param msg HELLO to inspect
632 * @return time the last address expires, 0 if there are no addresses in the HELLO
634 struct GNUNET_TIME_Absolute
635 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg)
637 struct GNUNET_TIME_Absolute ret;
640 GNUNET_HELLO_iterate_addresses (msg, GNUNET_NO, &find_min_expire, &ret);
645 * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
646 * The specific structure of "IDENTIFIER" depends on the module and
647 * maybe differenciated into additional subcategories if applicable.
648 * This module only deals with hello identifiers (MODULE = "hello").
651 * The concrete URI format is:
653 * "gnunet://hello/PEER[!YYYYMMDDHHNNSS!<TYPE>!<ADDRESS>]...".
654 * These URIs can be used to add a peer record to peerinfo service.
655 * PEER is the string representation of peer's public key.
656 * YYYYMMDDHHNNSS is the expiration date.
657 * TYPE is a transport type.
658 * ADDRESS is the address, its format depends upon the transport type.
659 * The concrete transport types and corresponding address formats are:
663 * <TCP|UDP>!IPADDRESS
664 * IPVDDRESS is either IPV4 .-delimited address in form of XXX.XXX.XXX.XXX:PPPPP
665 * or IPV6 :-delimited address, but with '(' and ')' instead of '[' and ']' (RFC2396 advises against using square brackets in URIs):
666 * (XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX):PPPPP
667 * PPPPP is the port number. May be 0.
671 * [add SMTP, HTTP and other addresses here]
675 * The encoding for hexadecimal values is defined in the crypto_hash.c
676 * module in the gnunetutil library and discussed there.
680 * gnunet://hello/0430205UC7D56PTQK8NV05776671CNN44FK4TL6D0GQ35OMF8MEN4RNMKA5UF6AL3DQO8B1SC5AQF50SQ2MABIRU4HC8H2HAJKJ59JL1JVRJAK308F9GASRFLMGUBB5TQ5AKR94AS5T3MDG8B9O1EMPRKB0HVCG7T6QPP4CDJ913LAEHVJ2DI1TOBB15Q1JIT5ARBOD12U4SIGRFDV3Q7T66G4TBVSJJ90UQF1BG29TGJJKLGEIMSPHHKO544D6EALQ4F2K0416311JC22GVAD48R616I7VK03K7MP7N0RS2MBV1TE9JV8CK1LSQMR7KCDRTLDA6917UGA67DHTGHERIACCGQ54TGSR48RMSGS9BA5HLMOKASFC1I6V4TT09TUGCU8GNDHQF0JF3H7LPV59UL5I38QID040G000!20120302010059!TCP!192.168.0.1:2086!TCP!64.23.8.174:0
681 * gnunet://hello/0430205UC7D56PTQK8NV05776671CNN44FK4TL6D0GQ35OMF8MEN4RNMKA5UF6AL3DQO8B1SC5AQF50SQ2MABIRU4HC8H2HAJKJ59JL1JVRJAK308F9GASRFLMGUBB5TQ5AKR94AS5T3MDG8B9O1EMPRKB0HVCG7T6QPP4CDJ913LAEHVJ2DI1TOBB15Q1JIT5ARBOD12U4SIGRFDV3Q7T66G4TBVSJJ90UQF1BG29TGJJKLGEIMSPHHKO544D6EALQ4F2K0416311JC22GVAD48R616I7VK03K7MP7N0RS2MBV1TE9JV8CK1LSQMR7KCDRTLDA6917UGA67DHTGHERIACCGQ54TGSR48RMSGS9BA5HLMOKASFC1I6V4TT09TUGCU8GNDHQF0JF3H7LPV59UL5I38QID040G000!20120302010059!TCP!(2001:db8:85a3:8d3:1319:8a2e:370:7348):2086