2 This file is part of GNUnet.
3 Copyright (C) 2009, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief helper library for handling HELLOs
22 * @author Christian Grothoff
23 * @author Matthias Wachs
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_plugin.h"
32 * Context used for building our own URI.
34 struct GNUNET_HELLO_ComposeUriContext
42 * Function for finding transport plugins by name.
44 GNUNET_HELLO_TransportPluginsFind plugins_find;
49 * Context for #add_address_to_hello().
51 struct GNUNET_HELLO_ParseUriContext
54 * Position in the URI with the next address to parse.
59 * Set to #GNUNET_SYSERR to indicate parse errors.
66 unsigned int counter_total;
69 * Counter skipped addresses
71 unsigned int counter_added;
74 * Function for finding transport plugins by name.
76 GNUNET_HELLO_TransportPluginsFind plugins_find;
83 * @param h HELLO Message to test
84 * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
87 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h)
89 if (GNUNET_YES == ntohl(h->friend_only))
96 * Copy the given address information into
97 * the given buffer using the format of HELLOs.
99 * @param address the address
100 * @param expiration expiration for the @a address
101 * @param target where to copy the @a address
102 * @param max maximum number of bytes to copy to target
103 * @return number of bytes copied, 0 if
104 * the target buffer was not big enough.
107 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
108 struct GNUNET_TIME_Absolute expiration,
114 struct GNUNET_TIME_AbsoluteNBO exp;
116 slen = strlen (address->transport_name) + 1;
117 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
118 address->address_length > max)
120 exp = GNUNET_TIME_absolute_hton (expiration);
121 alen = htons ((uint16_t) address->address_length);
122 GNUNET_memcpy (target, address->transport_name, slen);
123 GNUNET_memcpy (&target[slen], &alen, sizeof (uint16_t));
124 slen += sizeof (uint16_t);
125 GNUNET_memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
126 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
127 GNUNET_memcpy (&target[slen], address->address, address->address_length);
128 slen += address->address_length;
134 * Get the size of an address entry in a HELLO message.
136 * @param buf pointer to the start of the address entry
137 * @param max maximum size of the entry (end of @a buf)
138 * @param ralen set to the address length
139 * @return size of the entry, or 0 if @a max is not large enough
142 get_hello_address_size (const char *buf,
154 while ((left > 0) && ('\0' != *pos))
162 /* 0-termination not found */
167 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
169 /* not enough space for addrlen */
173 GNUNET_memcpy (&alen, pos, sizeof (uint16_t));
176 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
179 /* not enough space for addr */
188 * Construct a HELLO message given the public key,
189 * expiration time and an iterator that spews the
190 * transport addresses.
192 * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
193 * will not be gossiped to other peers.
195 * @param public_key public key to include in the HELLO
196 * @param addrgen callback to invoke to get addresses
197 * @param addrgen_cls closure for @a addrgen
198 * @param friend_only should the returned HELLO be only visible to friends?
199 * @return the hello message
201 struct GNUNET_HELLO_Message *
202 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
203 GNUNET_HELLO_GenerateAddressListCallback addrgen,
207 char buffer[GNUNET_MAX_MESSAGE_SIZE - 1 - 256 -
208 sizeof (struct GNUNET_HELLO_Message)];
212 struct GNUNET_HELLO_Message *hello;
214 GNUNET_assert (NULL != public_key);
215 GNUNET_assert ( (GNUNET_YES == friend_only) ||
216 (GNUNET_NO == friend_only) );
217 max = sizeof (buffer);
221 while (GNUNET_SYSERR != (ret = addrgen (addrgen_cls,
229 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
230 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
231 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
232 hello->friend_only = htonl (friend_only);
233 hello->publicKey = *public_key;
234 GNUNET_memcpy (&hello[1],
242 * Iterate over all of the addresses in the HELLO.
244 * @param msg HELLO to iterate over
245 * @param return_modified if a modified copy should be returned,
246 * otherwise NULL will be returned
247 * @param it iterator to call on each address
248 * @param it_cls closure for @a it
249 * @return modified HELLO message
251 struct GNUNET_HELLO_Message *
252 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
254 GNUNET_HELLO_AddressIterator it,
257 struct GNUNET_HELLO_Address address;
259 struct GNUNET_HELLO_Message *ret;
266 struct GNUNET_TIME_AbsoluteNBO expire;
269 msize = GNUNET_HELLO_size (msg);
270 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
271 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
279 ret = GNUNET_malloc (msize);
284 inptr = (const char *) &msg[1];
285 insize = msize - sizeof (struct GNUNET_HELLO_Message);
287 woff = (NULL != ret) ? (char *) &ret[1] : NULL;
288 address.peer.public_key = msg->publicKey;
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "HELLO has %u bytes of address data\n",
291 (unsigned int) insize);
295 esize = get_hello_address_size (inptr,
301 GNUNET_free_non_null (ret);
304 /* need GNUNET_memcpy() due to possibility of misalignment */
305 GNUNET_memcpy (&expire,
306 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
307 sizeof (struct GNUNET_TIME_AbsoluteNBO));
308 address.address = &inptr[esize - alen];
309 address.address_length = alen;
310 address.transport_name = inptr;
311 address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
314 GNUNET_TIME_absolute_ntoh (expire));
315 if (GNUNET_SYSERR == iret)
317 if ( (GNUNET_OK == iret) &&
320 /* copy address over */
331 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
337 * Closure for #get_match_exp().
342 * Address we are looking for.
344 const struct GNUNET_HELLO_Address *address;
347 * Set to #GNUNET_YES if we found the @e address.
352 * Set to the expiration of the match if @e found is #GNUNET_YES.
354 struct GNUNET_TIME_Absolute expiration;
359 * Store the expiration time of an address that matches the template.
361 * @param cls the `struct ExpireContext`
362 * @param address address to match against the template
363 * @param expiration expiration time of @a address, to store in @a cls
364 * @return #GNUNET_SYSERR if we found a matching address, #GNUNET_OK otherwise
367 get_match_exp (void *cls,
368 const struct GNUNET_HELLO_Address *address,
369 struct GNUNET_TIME_Absolute expiration)
371 struct ExpireContext *ec = cls;
373 if (0 != GNUNET_HELLO_address_cmp (address,
376 ec->found = GNUNET_YES;
377 ec->expiration = expiration;
378 return GNUNET_SYSERR; /* done here */
383 * Context for a #GNUNET_HELLO_Merge operation.
388 * First HELLO we are merging.
390 const struct GNUNET_HELLO_Message *h1;
393 * Second HELLO we are merging.
395 const struct GNUNET_HELLO_Message *h2;
398 * Either @e h1 or @e h2, used when copying
399 * to compare against (so we only copy the
400 * most recent entry).
402 const struct GNUNET_HELLO_Message *other;
405 * Buffer where we copy to.
410 * Number of bytes allocated in @e buf
415 * Current (write) offset in @e buf.
420 * Should we copy addresses with an identical value
421 * and expiration time in @e other, or do we only
422 * copy addresses with strictly later expiration times?
430 * Append the address @a address to the buffer from
431 * the merge context IF it is more recent than equivalent
432 * addresses in `other`.
434 * @param cls the `struct MergeContext`
435 * @param address the HELLO address we might copy
436 * @param expiration expiration time for @a address
437 * @return always #GNUNET_OK
440 copy_latest (void *cls,
441 const struct GNUNET_HELLO_Address *address,
442 struct GNUNET_TIME_Absolute expiration)
444 struct MergeContext *mc = cls;
445 struct ExpireContext ec;
447 ec.address = address;
448 ec.found = GNUNET_NO;
449 /* check if address exists in other */
450 GNUNET_HELLO_iterate_addresses (mc->other,
454 if ( (GNUNET_NO == ec.found) ||
455 (ec.expiration.abs_value_us < expiration.abs_value_us) ||
456 ( (ec.expiration.abs_value_us == expiration.abs_value_us) &&
457 (GNUNET_YES == mc->take_equal) ) )
459 /* copy address to buffer */
461 GNUNET_HELLO_add_address (address,
471 * Function called to build the HELLO during
472 * #GNUNET_HELLO_merge() by merging addresses from
473 * two original HELLOs.
475 * @param cls the `struct MergeContext`
476 * @param max number of bytes we can write at most in @a buf
477 * @param buf where to copy the addresses
478 * @return #GNUNET_SYSERR to end iteration, otherwise number of bytes written to @a buf
481 merge_addr (void *cls,
485 struct MergeContext *mc = cls;
488 return GNUNET_SYSERR; /* Stop iteration */
492 mc->take_equal = GNUNET_NO;
494 /* copy addresses from h1, if strictly larger expiration than h2 */
495 GNUNET_HELLO_iterate_addresses (mc->h1,
499 mc->take_equal = GNUNET_YES;
501 /* copy addresses from h2, if larger or equal expiration than h1 */
502 GNUNET_HELLO_iterate_addresses (mc->h2,
506 /* set marker to stop iteration */
513 * Construct a HELLO message by merging the
514 * addresses in two existing HELLOs (which
515 * must be for the same peer).
517 * @param h1 first HELLO message
518 * @param h2 the second HELLO message
519 * @return the combined HELLO message
521 struct GNUNET_HELLO_Message *
522 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
523 const struct GNUNET_HELLO_Message *h2)
525 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
528 if (h1->friend_only != h2->friend_only)
529 friend_only = GNUNET_YES; /* One of the HELLOs is friend only */
531 friend_only = ntohl (h1->friend_only); /* Both HELLO's have the same type */
533 return GNUNET_HELLO_create (&h1->publicKey,
541 * Context used in #GNUNET_HELLO_iterate_new_addresses() to
542 * figure out which addresses are in fact 'new'.
547 * We should ignore addresses that expire before this time.
549 struct GNUNET_TIME_Absolute expiration_limit;
552 * Function to call on addresses that are indeed new.
554 GNUNET_HELLO_AddressIterator it;
562 * HELLO with known addresses, addresses in this HELLO
563 * we must always ignore.
565 const struct GNUNET_HELLO_Message *old_hello;
570 * Check if the given address is 'new', and if so, call
571 * the iterator. Compares the existing address against
572 * addresses in the context's `old_hello` and calls the
573 * iterator on those that are new (and not expired).
575 * @param cls the `struct DeltaContext`
576 * @param address an address to check whether it is new
577 * @param expiration expiration time for @a address
578 * @return #GNUNET_YES if the address is ignored, otherwise
579 * whatever the iterator returned.
582 delta_match (void *cls,
583 const struct GNUNET_HELLO_Address *address,
584 struct GNUNET_TIME_Absolute expiration)
586 struct DeltaContext *dc = cls;
588 struct ExpireContext ec;
590 ec.address = address;
591 ec.found = GNUNET_NO;
592 GNUNET_HELLO_iterate_addresses (dc->old_hello,
596 if ( (GNUNET_YES == ec.found) &&
597 ( (ec.expiration.abs_value_us > expiration.abs_value_us) ||
598 (ec.expiration.abs_value_us >= dc->expiration_limit.abs_value_us)))
599 return GNUNET_YES; /* skip: found and boring */
600 ret = dc->it (dc->it_cls,
608 * Iterate over addresses in @a new_hello that are NOT already present
609 * in @a old_hello. Note that if the address is present in @a old_hello
610 * but the expiration time in @a new_hello is more recent, the iterator
613 * @param new_hello a HELLO message
614 * @param old_hello a HELLO message
615 * @param expiration_limit ignore addresses in @a old_hello
616 * that expired before the given time stamp
617 * @param it iterator to call on each address
618 * @param it_cls closure for @a it
621 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
622 const struct GNUNET_HELLO_Message *old_hello,
623 struct GNUNET_TIME_Absolute expiration_limit,
624 GNUNET_HELLO_AddressIterator it,
627 struct DeltaContext dc;
629 dc.expiration_limit = expiration_limit;
632 dc.old_hello = old_hello;
633 GNUNET_assert (NULL ==
634 GNUNET_HELLO_iterate_addresses (new_hello,
642 * Return the size of the given HELLO message.
643 * @param hello to inspect
644 * @return the size, 0 if HELLO is invalid
647 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
649 uint16_t ret = ntohs (hello->header.size);
651 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
652 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
659 * Get the peer identity from a HELLO message.
661 * @param hello the hello message
662 * @param peer where to store the peer's identity
663 * @return #GNUNET_SYSERR if the HELLO was malformed
666 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
667 struct GNUNET_PeerIdentity *peer)
669 uint16_t ret = ntohs (hello->header.size);
671 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
672 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
673 return GNUNET_SYSERR;
674 peer->public_key = hello->publicKey;
680 * Get the header from a HELLO message, used so other code
681 * can correctly send HELLO messages.
683 * @param hello the hello message
685 * @return header or NULL if the HELLO was malformed
687 struct GNUNET_MessageHeader *
688 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
690 uint16_t ret = ntohs (hello->header.size);
692 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
693 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
696 return &hello->header;
701 * Context used for comparing HELLOs in #GNUNET_HELLO_equals().
706 * Addresses that expired before this date are ignored for
709 struct GNUNET_TIME_Absolute expiration_limit;
712 * Earliest expiration time for which we found a match
713 * with a difference in expiration times.
714 * At this time, the two HELLOs may start to diverge.
716 struct GNUNET_TIME_Absolute result;
719 * HELLO message to compare against. (First set to the second
720 * HELLO, then set to the first HELLO.)
722 const struct GNUNET_HELLO_Message *ref;
725 * Address we are currently looking for.
727 const struct GNUNET_HELLO_Address *address;
730 * Expiration time of @e address.
732 struct GNUNET_TIME_Absolute expiration;
735 * Did we find the address we were looking for?
743 * Check if the given address matches the address we are currently
744 * looking for. If so, sets `found` to #GNUNET_YES and, if the
745 * expiration times for the two addresses differ, updates `result` to
746 * the minimum of our @a expiration and the existing value
748 * @param cls the `struct EqualsContext`
749 * @param address address from the reference HELLO
750 * @param expiration expiration time for @a address
751 * @return #GNUNET_YES if the address is expired or does not match
752 * #GNUNET_SYSERR if the address does match.
755 find_other_matching (void *cls,
756 const struct GNUNET_HELLO_Address *address,
757 struct GNUNET_TIME_Absolute expiration)
759 struct EqualsContext *ec = cls;
761 if (expiration.abs_value_us < ec->expiration_limit.abs_value_us)
763 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
765 ec->found = GNUNET_YES;
766 if (expiration.abs_value_us < ec->expiration.abs_value_us)
767 ec->result = GNUNET_TIME_absolute_min (expiration,
769 return GNUNET_SYSERR;
776 * Helper function for #GNUNET_HELLO_equals(). Checks
777 * if the given @a address exists also in the other HELLO;
778 * if not, the result time is set to zero and the iteration
781 * @param cls the `struct EqualsContext`
782 * @param address address to locate
783 * @param expiration expiration time of the current address
784 * @return #GNUNET_OK if the address exists or is expired,
785 * #GNUNET_SYSERR if it was not found
788 find_matching (void *cls,
789 const struct GNUNET_HELLO_Address *address,
790 struct GNUNET_TIME_Absolute expiration)
792 struct EqualsContext *ec = cls;
794 if (expiration.abs_value_us < ec->expiration_limit.abs_value_us)
795 return GNUNET_OK; /* expired, we don't care */
796 ec->address = address;
797 ec->expiration = expiration;
798 ec->found = GNUNET_NO;
799 GNUNET_HELLO_iterate_addresses (ec->ref,
801 &find_other_matching,
803 if (GNUNET_NO == ec->found)
805 /* not found, we differ *now* */
806 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
807 return GNUNET_SYSERR;
814 * Test if two HELLO messages contain the same addresses.
815 * If they only differ in expiration time, the lowest
816 * expiration time larger than @a now where they differ
819 * @param h1 first HELLO message
820 * @param h2 the second HELLO message
821 * @param now time to use for deciding which addresses have
822 * expired and should not be considered at all
823 * @return absolute time forever if the two HELLOs are
824 * totally identical; smallest timestamp >= @a now if
825 * they only differ in timestamps;
826 * zero if the some addresses with expirations >= @a now
827 * do not match at all
829 struct GNUNET_TIME_Absolute
830 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
831 const struct GNUNET_HELLO_Message *h2,
832 struct GNUNET_TIME_Absolute now)
834 struct EqualsContext ec;
836 if (h1->header.type != h2->header.type)
837 return GNUNET_TIME_UNIT_ZERO_ABS;
839 memcmp (&h1->publicKey,
841 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
842 return GNUNET_TIME_UNIT_ZERO_ABS;
843 ec.expiration_limit = now;
844 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
846 GNUNET_HELLO_iterate_addresses (h1,
850 if (ec.result.abs_value_us == GNUNET_TIME_UNIT_ZERO.rel_value_us)
853 GNUNET_HELLO_iterate_addresses (h2,
862 * Iterator to find the time when the last address will expire.
863 * Updates the maximum value stored in @a cls.
865 * @param cls where to store the max, a `struct GNUNET_TIME_Absolute`
866 * @param address an address (ignored)
867 * @param expiration expiration time for @a address
868 * @return #GNUNET_OK (always)
871 find_max_expire (void *cls,
872 const struct GNUNET_HELLO_Address *address,
873 struct GNUNET_TIME_Absolute expiration)
875 struct GNUNET_TIME_Absolute *max = cls;
877 *max = GNUNET_TIME_absolute_max (*max, expiration);
883 * When does the last address in the given HELLO expire?
885 * @param msg HELLO to inspect
886 * @return time the last address expires, 0 if there are no addresses in the HELLO
888 struct GNUNET_TIME_Absolute
889 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg)
891 struct GNUNET_TIME_Absolute ret;
893 ret = GNUNET_TIME_UNIT_ZERO_ABS;
894 GNUNET_HELLO_iterate_addresses (msg,
903 * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
904 * The specific structure of "IDENTIFIER" depends on the module and
905 * maybe differenciated into additional subcategories if applicable.
906 * This module only deals with hello identifiers (MODULE = "hello").
909 * The concrete URI format is:
911 * "gnunet://hello/PEER[+YYYYMMDDHHMMSS+<TYPE>+<ADDRESS>]...".
912 * These URIs can be used to add a peer record to peerinfo service.
913 * PEER is the string representation of peer's public key.
914 * YYYYMMDDHHMMSS is the expiration date.
915 * TYPE is a transport type.
916 * ADDRESS is the address, its format depends upon the transport type.
917 * The concrete transport types and corresponding address formats are:
921 * <TCP|UDP>!IPADDRESS
922 * IPVDDRESS is either IPV4 .-delimited address in form of XXX.XXX.XXX.XXX:PPPPP
923 * or IPV6 :-delimited address with '[' and ']' (according to RFC2732):
924 * [XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX]:PPPPP
925 * PPPPP is the port number. May be 0.
929 * [add SMTP, HTTP and other addresses here]
933 * The encoding for hexadecimal values is defined in the crypto_hash.c
934 * module in the gnunetutil library and discussed there.
938 * gnunet://hello/V8XXK9GAN5ZJFRFQP8MQX3D83BZTSBQVHKWWD0JPE63Z821906EG+20120302010059+TCP+192.168.0.1:2086+TCP+64.23.8.174:0
939 * gnunet://hello/V8XXK9GAN5ZJFRFQP8MQX3D83BZTSBQVHKWWD0JPE63Z821906EG+20120302010059+TCP+[2001:db8:85a3:8d3:1319:8a2e:370:7348]:2086
947 * Function that is called on each address of this peer.
948 * Expands the corresponding URI string.
950 * @param cls the `struct GNUNET_HELLO_ComposeUriContext`
951 * @param address address to add
952 * @param expiration expiration time for the address
953 * @return #GNUNET_OK (continue iteration).
956 add_address_to_uri (void *cls,
957 const struct GNUNET_HELLO_Address *address,
958 struct GNUNET_TIME_Absolute expiration)
960 struct GNUNET_HELLO_ComposeUriContext *ctx = cls;
961 struct GNUNET_TRANSPORT_PluginFunctions *papi;
967 char *client_str = "_client";
971 papi = ctx->plugins_find (address->transport_name);
974 /* Not an error - we might just not have the right plugin. */
977 if (NULL == papi->address_to_string)
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "URI conversion not implemented for plugin `%s'\n",
981 address->transport_name);
984 addr = papi->address_to_string (papi->cls,
986 address->address_length);
987 if ( (NULL == addr) ||
988 (0 == strlen(addr)) )
991 addr_dup = GNUNET_strdup (addr);
992 if (NULL != (pos = strstr (addr_dup, "_server")))
995 strlen (client_str)); /* Replace all server addresses with client addresses */
997 seconds = expiration.abs_value_us / 1000LL / 1000LL;
998 t = gmtime (&seconds);
1000 GNUNET_asprintf (&ret,
1003 GNUNET_HELLO_URI_SEP,
1008 GNUNET_HELLO_URI_SEP,
1009 address->transport_name,
1010 GNUNET_HELLO_URI_SEP,
1012 GNUNET_free (addr_dup);
1013 GNUNET_free (ctx->uri);
1020 * Compose a hello URI string from a hello message.
1022 * @param hello Hello message
1023 * @param plugins_find Function to find transport plugins by name
1024 * @return Hello URI string
1027 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
1028 GNUNET_HELLO_TransportPluginsFind plugins_find)
1030 struct GNUNET_HELLO_ComposeUriContext ctx;
1033 ctx.plugins_find = plugins_find;
1034 pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&hello->publicKey);
1035 GNUNET_asprintf (&ctx.uri,
1037 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello))
1038 ? GNUNET_FRIEND_HELLO_URI_PREFIX
1039 : GNUNET_HELLO_URI_PREFIX,
1042 GNUNET_HELLO_iterate_addresses (hello,
1044 &add_address_to_uri,
1050 /* ************************* Parse HELLO URI ********************* */
1054 * We're building a HELLO. Parse the next address from the
1055 * parsing context and append it.
1057 * @param cls the `struct GNUNET_HELLO_ParseUriContext`
1058 * @param max number of bytes available for HELLO construction
1059 * @param buffer where to copy the next address (in binary format)
1060 * @return number of bytes added to buffer, #GNUNET_SYSERR on error
1063 add_address_to_hello (void *cls,
1067 struct GNUNET_HELLO_ParseUriContext *ctx = cls;
1069 const char *address;
1073 struct tm expiration_time;
1074 time_t expiration_seconds;
1075 struct GNUNET_TIME_Absolute expire;
1076 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1079 struct GNUNET_HELLO_Address haddr;
1082 if (NULL == ctx->pos)
1083 return GNUNET_SYSERR;
1084 if (GNUNET_HELLO_URI_SEP != ctx->pos[0])
1086 ctx->ret = GNUNET_SYSERR;
1088 return GNUNET_SYSERR;
1092 if ( ('0' == ctx->pos[0]) &&
1093 (GNUNET_HELLO_URI_SEP == ctx->pos[1]) )
1095 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
1096 tname = ctx->pos + 1;
1100 memset (&expiration_time, 0, sizeof (expiration_time));
1101 tname = strptime (ctx->pos,
1106 ctx->ret = GNUNET_SYSERR;
1107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 _("Failed to parse HELLO message: missing expiration time\n"));
1110 return GNUNET_SYSERR;
1113 expiration_seconds = mktime (&expiration_time);
1114 if (expiration_seconds == (time_t) -1)
1116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1117 _("Failed to parse HELLO message: invalid expiration time\n"));
1118 ctx->ret = GNUNET_SYSERR;
1120 return GNUNET_SYSERR;
1122 expire.abs_value_us = expiration_seconds * 1000LL * 1000LL;
1124 if (GNUNET_HELLO_URI_SEP != tname[0])
1126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1127 _("Failed to parse HELLO message: malformed\n"));
1128 ctx->ret = GNUNET_SYSERR;
1130 return GNUNET_SYSERR;
1133 address = strchr (tname,
1134 (int) GNUNET_HELLO_URI_SEP);
1135 if (NULL == address)
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138 _("Failed to parse HELLO message: missing transport plugin\n"));
1139 ctx->ret = GNUNET_SYSERR;
1141 return GNUNET_SYSERR;
1144 end = strchr (address, (int) GNUNET_HELLO_URI_SEP);
1146 ctx->counter_total ++;
1147 plugin_name = GNUNET_strndup (tname, address - (tname+1));
1148 papi = ctx->plugins_find (plugin_name);
1151 /* Not an error - we might just not have the right plugin.
1152 * Skip this part, advance to the next one and recurse.
1153 * But only if this is not the end of string.
1155 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1156 _("Plugin `%s' not found, skipping address\n"),
1158 GNUNET_free (plugin_name);
1161 if (NULL == papi->string_to_address)
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 _("Plugin `%s' does not support URIs yet\n"),
1166 GNUNET_free (plugin_name);
1170 uri_address = GNUNET_strndup (address, end - address);
1172 papi->string_to_address (papi->cls,
1174 strlen (uri_address) + 1,
1178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1179 _("Failed to parse `%s' as an address for plugin `%s'\n"),
1182 GNUNET_free (plugin_name);
1183 GNUNET_free (uri_address);
1186 GNUNET_free (uri_address);
1187 /* address.peer is unset - not used by add_address() */
1188 haddr.address_length = addr_len;
1189 haddr.address = addr;
1190 haddr.transport_name = plugin_name;
1191 ret = GNUNET_HELLO_add_address (&haddr,
1195 ctx->counter_added ++;
1197 GNUNET_free (plugin_name);
1203 * Parse a hello URI string to a hello message.
1205 * @param uri URI string to parse
1206 * @param pubkey Pointer to struct where public key is parsed
1207 * @param hello Pointer to struct where hello message is parsed
1208 * @param plugins_find Function to find transport plugins by name
1209 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
1212 GNUNET_HELLO_parse_uri (const char *uri,
1213 struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
1214 struct GNUNET_HELLO_Message **hello,
1215 GNUNET_HELLO_TransportPluginsFind plugins_find)
1220 struct GNUNET_HELLO_ParseUriContext ctx;
1222 if (0 == strncmp (uri,
1223 GNUNET_HELLO_URI_PREFIX,
1224 strlen (GNUNET_HELLO_URI_PREFIX)))
1226 pks = &uri[strlen (GNUNET_HELLO_URI_PREFIX)];
1227 friend_only = GNUNET_NO;
1229 else if (0 == strncmp (uri,
1230 GNUNET_FRIEND_HELLO_URI_PREFIX,
1231 strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)))
1233 pks = &uri[strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)];
1234 friend_only = GNUNET_YES;
1237 return GNUNET_SYSERR;
1238 exc = strchr (pks, GNUNET_HELLO_URI_SEP);
1241 GNUNET_STRINGS_string_to_data (pks,
1242 (NULL == exc) ? strlen (pks) : (exc - pks),
1243 (unsigned char *) pubkey,
1245 return GNUNET_SYSERR;
1248 ctx.ret = GNUNET_OK;
1249 ctx.counter_total = 0;
1250 ctx.counter_added = 0;
1251 ctx.plugins_find = plugins_find;
1252 *hello = GNUNET_HELLO_create (pubkey,
1253 &add_address_to_hello,
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "HELLO URI contained %u addresses, added %u addresses\n",
1266 /* end of hello.c */