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"
30 #include "gnunet_transport_plugin.h"
32 GNUNET_NETWORK_STRUCT_BEGIN
35 * A HELLO message is used to exchange information about
36 * transports with other peers. This struct is always
37 * followed by the actual network addresses which have
40 * 1) transport-name (0-terminated)
41 * 2) address-length (uint16_t, network byte order; possibly
43 * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
45 * 4) address (address-length bytes; possibly unaligned!)
47 struct GNUNET_HELLO_Message
50 * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
52 struct GNUNET_MessageHeader header;
55 * Use in F2F mode: Do not gossip this HELLO message
57 uint32_t friend_only GNUNET_PACKED;
60 * The public key of the peer.
62 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
65 GNUNET_NETWORK_STRUCT_END
69 * Context used for building our own URI.
71 struct GNUNET_HELLO_ComposeUriContext
79 * Function for finding transport plugins by name.
81 GNUNET_HELLO_TransportPluginsFind plugins_find;
86 * Context for 'add_address_to_hello'.
88 struct GNUNET_HELLO_ParseUriContext
91 * Position in the URI with the next address to parse.
96 * Set to #GNUNET_SYSERR to indicate parse errors.
101 * Function for finding transport plugins by name.
103 GNUNET_HELLO_TransportPluginsFind plugins_find;
107 /** Return HELLO type
109 * @param h HELLO Message to test
110 * @return #GNUNET_YES or #GNUNET_NO
113 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h)
115 if (GNUNET_YES == ntohl(h->friend_only))
123 * Copy the given address information into
124 * the given buffer using the format of HELLOs.
126 * @param address the address
127 * @param expiration expiration for the @a address
128 * @param target where to copy the @a address
129 * @param max maximum number of bytes to copy to target
130 * @return number of bytes copied, 0 if
131 * the target buffer was not big enough.
134 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
135 struct GNUNET_TIME_Absolute expiration, char *target,
140 struct GNUNET_TIME_AbsoluteNBO exp;
142 slen = strlen (address->transport_name) + 1;
143 if (slen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
144 address->address_length > max)
146 exp = GNUNET_TIME_absolute_hton (expiration);
147 alen = htons ((uint16_t) address->address_length);
148 memcpy (target, address->transport_name, slen);
149 memcpy (&target[slen], &alen, sizeof (uint16_t));
150 slen += sizeof (uint16_t);
151 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
152 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
153 memcpy (&target[slen], address->address, address->address_length);
154 slen += address->address_length;
160 * Get the size of an address entry in a HELLO message.
162 * @param buf pointer to the start of the address entry
163 * @param max maximum size of the entry (end of buf)
164 * @param ralen set to the address length
165 * @return size of the entry, or 0 if max is not large enough
168 get_hello_address_size (const char *buf,
180 while ((left > 0) && ('\0' != *pos))
188 /* 0-termination not found */
193 if (left < sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
195 /* not enough space for addrlen */
199 memcpy (&alen, pos, sizeof (uint16_t));
202 slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
205 /* not enough space for addr */
214 * Construct a HELLO message given the public key,
215 * expiration time and an iterator that spews the
216 * transport addresses.
218 * @return the hello message
220 struct GNUNET_HELLO_Message *
221 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *publicKey,
222 GNUNET_HELLO_GenerateAddressListCallback addrgen,
226 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
227 sizeof (struct GNUNET_HELLO_Message)];
231 struct GNUNET_HELLO_Message *hello;
233 GNUNET_assert (NULL != publicKey);
234 GNUNET_assert ((GNUNET_YES == friend_only) || (GNUNET_NO == friend_only));
236 max = sizeof (buffer);
240 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
246 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
247 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
248 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
249 hello->friend_only = htonl (friend_only);
251 memcpy (&hello->publicKey, publicKey,
252 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
253 memcpy (&hello[1], buffer, used);
259 * Iterate over all of the addresses in the HELLO.
261 * @param msg HELLO to iterate over
262 * @param return_modified if a modified copy should be returned,
263 * otherwise NULL will be returned
264 * @param it iterator to call on each address
265 * @param it_cls closure for @a it
266 * @return modified HELLO message
268 struct GNUNET_HELLO_Message *
269 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
271 GNUNET_HELLO_AddressIterator it, void *it_cls)
273 struct GNUNET_HELLO_Address address;
275 struct GNUNET_HELLO_Message *ret;
282 struct GNUNET_TIME_AbsoluteNBO expire;
285 msize = GNUNET_HELLO_size (msg);
286 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
287 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
292 ret = GNUNET_malloc (msize);
293 memcpy (ret, msg, msize);
295 inptr = (const char *) &msg[1];
296 insize = msize - sizeof (struct GNUNET_HELLO_Message);
298 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
299 address.peer.public_key = msg->publicKey;
302 esize = get_hello_address_size (inptr, insize, &alen);
306 GNUNET_free_non_null (ret);
310 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
311 sizeof (struct GNUNET_TIME_AbsoluteNBO));
312 address.address = &inptr[esize - alen];
313 address.address_length = alen;
314 address.transport_name = inptr;
315 address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
316 iret = it (it_cls, &address, GNUNET_TIME_absolute_ntoh (expire));
317 if (iret == GNUNET_SYSERR)
320 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
323 if ((iret == GNUNET_OK) && (ret != NULL))
325 memcpy (woff, inptr, esize);
333 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
340 const struct GNUNET_HELLO_Address *address;
342 struct GNUNET_TIME_Absolute expiration;
347 get_match_exp (void *cls, const struct GNUNET_HELLO_Address *address,
348 struct GNUNET_TIME_Absolute expiration)
350 struct ExpireContext *ec = cls;
352 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
354 ec->found = GNUNET_YES;
355 ec->expiration = expiration;
356 return GNUNET_SYSERR; /* done here */
364 const struct GNUNET_HELLO_Message *h1;
365 const struct GNUNET_HELLO_Message *h2;
366 const struct GNUNET_HELLO_Message *other;
376 copy_latest (void *cls, const struct GNUNET_HELLO_Address *address,
377 struct GNUNET_TIME_Absolute expiration)
379 struct MergeContext *mc = cls;
380 struct ExpireContext ec;
382 ec.address = address;
383 ec.found = GNUNET_NO;
384 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
385 if ((ec.found == GNUNET_NO) ||
386 (ec.expiration.abs_value_us < expiration.abs_value_us) ||
387 ((ec.expiration.abs_value_us == expiration.abs_value_us) &&
388 (mc->take_equal == GNUNET_YES)))
391 GNUNET_HELLO_add_address (address, expiration, &mc->buf[mc->ret],
399 merge_addr (void *cls, size_t max, void *buf)
401 struct MergeContext *mc = cls;
408 mc->take_equal = GNUNET_NO;
410 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
411 mc->take_equal = GNUNET_YES;
413 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
420 * Construct a HELLO message by merging the
421 * addresses in two existing HELLOs (which
422 * must be for the same peer).
424 * @param h1 first HELLO message
425 * @param h2 the second HELLO message
426 * @return the combined hello message
428 struct GNUNET_HELLO_Message *
429 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
430 const struct GNUNET_HELLO_Message *h2)
432 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
435 if (h1->friend_only != h2->friend_only)
436 friend_only = GNUNET_YES; /* One of the HELLOs is friend only */
438 friend_only = ntohl (h1->friend_only); /* Both HELLO's have the same type */
440 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc, friend_only);
446 struct GNUNET_TIME_Absolute expiration_limit;
448 GNUNET_HELLO_AddressIterator it;
452 const struct GNUNET_HELLO_Message *old_hello;
457 delta_match (void *cls, const struct GNUNET_HELLO_Address *address,
458 struct GNUNET_TIME_Absolute expiration)
460 struct DeltaContext *dc = cls;
462 struct ExpireContext ec;
464 ec.address = address;
465 ec.found = GNUNET_NO;
466 GNUNET_HELLO_iterate_addresses (dc->old_hello, GNUNET_NO, &get_match_exp,
468 if ((ec.found == GNUNET_YES) &&
469 ((ec.expiration.abs_value_us > expiration.abs_value_us) ||
470 (ec.expiration.abs_value_us >= dc->expiration_limit.abs_value_us)))
471 return GNUNET_YES; /* skip */
472 ret = dc->it (dc->it_cls, address, expiration);
478 * Iterate over addresses in "new_hello" that
479 * are NOT already present in "old_hello".
481 * @param new_hello a HELLO message
482 * @param old_hello a HELLO message
483 * @param expiration_limit ignore addresses in old_hello
484 * that expired before the given time stamp
485 * @param it iterator to call on each address
486 * @param it_cls closure for it
489 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
491 const struct GNUNET_HELLO_Message
493 struct GNUNET_TIME_Absolute
495 GNUNET_HELLO_AddressIterator it,
498 struct DeltaContext dc;
500 dc.expiration_limit = expiration_limit;
503 dc.old_hello = old_hello;
504 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
509 * Return the size of the given HELLO message.
510 * @param hello to inspect
511 * @return the size, 0 if HELLO is invalid
514 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
516 uint16_t ret = ntohs (hello->header.size);
518 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
519 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
526 * Get the public key from a HELLO message.
528 * @param hello the hello message
529 * @param publicKey where to copy the public key information, can be NULL
530 * @return GNUNET_SYSERR if the HELLO was malformed
533 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
534 struct GNUNET_CRYPTO_EddsaPublicKey *publicKey)
536 uint16_t ret = ntohs (hello->header.size);
538 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
539 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
540 return GNUNET_SYSERR;
541 *publicKey = hello->publicKey;
547 * Get the peer identity from a HELLO message.
549 * @param hello the hello message
550 * @param peer where to store the peer's identity
551 * @return #GNUNET_SYSERR if the HELLO was malformed
554 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
555 struct GNUNET_PeerIdentity *peer)
557 uint16_t ret = ntohs (hello->header.size);
559 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
560 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
561 return GNUNET_SYSERR;
562 peer->public_key = hello->publicKey;
568 * Get the header from a HELLO message, used so other code
569 * can correctly send HELLO messages.
571 * @param hello the hello message
573 * @return header or NULL if the HELLO was malformed
575 struct GNUNET_MessageHeader *
576 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
578 uint16_t ret = ntohs (hello->header.size);
580 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
581 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
584 return &hello->header;
590 struct GNUNET_TIME_Absolute expiration_limit;
592 struct GNUNET_TIME_Absolute result;
594 const struct GNUNET_HELLO_Message *h2;
596 const struct GNUNET_HELLO_Address *address;
598 struct GNUNET_TIME_Absolute expiration;
606 find_other_matching (void *cls, const struct GNUNET_HELLO_Address *address,
607 struct GNUNET_TIME_Absolute expiration)
609 struct EqualsContext *ec = cls;
611 if (expiration.abs_value_us < ec->expiration_limit.abs_value_us)
613 if (0 == GNUNET_HELLO_address_cmp (address, ec->address))
615 ec->found = GNUNET_YES;
616 if (expiration.abs_value_us < ec->expiration.abs_value_us)
617 ec->result = GNUNET_TIME_absolute_min (expiration, ec->result);
618 return GNUNET_SYSERR;
625 find_matching (void *cls, const struct GNUNET_HELLO_Address *address,
626 struct GNUNET_TIME_Absolute expiration)
628 struct EqualsContext *ec = cls;
630 if (expiration.abs_value_us < ec->expiration_limit.abs_value_us)
632 ec->address = address;
633 ec->expiration = expiration;
634 ec->found = GNUNET_NO;
635 GNUNET_HELLO_iterate_addresses (ec->h2, GNUNET_NO, &find_other_matching, ec);
636 if (ec->found == GNUNET_NO)
638 ec->result = GNUNET_TIME_UNIT_ZERO_ABS;
639 return GNUNET_SYSERR;
646 * Test if two HELLO messages contain the same addresses.
647 * If they only differ in expiration time, the lowest
648 * expiration time larger than 'now' where they differ
651 * @param h1 first HELLO message
652 * @param h2 the second HELLO message
653 * @param now time to use for deciding which addresses have
654 * expired and should not be considered at all
655 * @return absolute time forever if the two HELLOs are
656 * totally identical; smallest timestamp >= now if
657 * they only differ in timestamps;
658 * zero if the some addresses with expirations >= now
659 * do not match at all
661 struct GNUNET_TIME_Absolute
662 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
663 const struct GNUNET_HELLO_Message *h2,
664 struct GNUNET_TIME_Absolute now)
666 struct EqualsContext ec;
668 if (h1->header.type != h2->header.type)
669 return GNUNET_TIME_UNIT_ZERO_ABS;
672 memcmp (&h1->publicKey, &h2->publicKey,
673 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
674 return GNUNET_TIME_UNIT_ZERO_ABS;
675 ec.expiration_limit = now;
676 ec.result = GNUNET_TIME_UNIT_FOREVER_ABS;
678 GNUNET_HELLO_iterate_addresses (h1, GNUNET_NO, &find_matching, &ec);
679 if (ec.result.abs_value_us == GNUNET_TIME_UNIT_ZERO.rel_value_us)
682 GNUNET_HELLO_iterate_addresses (h2, GNUNET_NO, &find_matching, &ec);
688 find_max_expire (void *cls, const struct GNUNET_HELLO_Address *address,
689 struct GNUNET_TIME_Absolute expiration)
691 struct GNUNET_TIME_Absolute *max = cls;
693 *max = GNUNET_TIME_absolute_max (*max, expiration);
699 * When does the last address in the given HELLO expire?
701 * @param msg HELLO to inspect
702 * @return time the last address expires, 0 if there are no addresses in the HELLO
704 struct GNUNET_TIME_Absolute
705 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg)
707 struct GNUNET_TIME_Absolute ret;
709 ret.abs_value_us = 0;
710 GNUNET_HELLO_iterate_addresses (msg, GNUNET_NO, &find_max_expire, &ret);
715 * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
716 * The specific structure of "IDENTIFIER" depends on the module and
717 * maybe differenciated into additional subcategories if applicable.
718 * This module only deals with hello identifiers (MODULE = "hello").
721 * The concrete URI format is:
723 * "gnunet://hello/PEER[!YYYYMMDDHHMMSS!<TYPE>!<ADDRESS>]...".
724 * These URIs can be used to add a peer record to peerinfo service.
725 * PEER is the string representation of peer's public key.
726 * YYYYMMDDHHMMSS is the expiration date.
727 * TYPE is a transport type.
728 * ADDRESS is the address, its format depends upon the transport type.
729 * The concrete transport types and corresponding address formats are:
733 * <TCP|UDP>!IPADDRESS
734 * IPVDDRESS is either IPV4 .-delimited address in form of XXX.XXX.XXX.XXX:PPPPP
735 * or IPV6 :-delimited address, but with '(' and ')' instead of '[' and ']' (RFC2396 advises against using square brackets in URIs):
736 * (XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX):PPPPP
737 * PPPPP is the port number. May be 0.
741 * [add SMTP, HTTP and other addresses here]
745 * The encoding for hexadecimal values is defined in the crypto_hash.c
746 * module in the gnunetutil library and discussed there.
750 * gnunet://hello/0430205UC7D56PTQK8NV05776671CNN44FK4TL6D0GQ35OMF8MEN4RNMKA5UF6AL3DQO8B1SC5AQF50SQ2MABIRU4HC8H2HAJKJ59JL1JVRJAK308F9GASRFLMGUBB5TQ5AKR94AS5T3MDG8B9O1EMPRKB0HVCG7T6QPP4CDJ913LAEHVJ2DI1TOBB15Q1JIT5ARBOD12U4SIGRFDV3Q7T66G4TBVSJJ90UQF1BG29TGJJKLGEIMSPHHKO544D6EALQ4F2K0416311JC22GVAD48R616I7VK03K7MP7N0RS2MBV1TE9JV8CK1LSQMR7KCDRTLDA6917UGA67DHTGHERIACCGQ54TGSR48RMSGS9BA5HLMOKASFC1I6V4TT09TUGCU8GNDHQF0JF3H7LPV59UL5I38QID040G000!20120302010059!TCP!192.168.0.1:2086!TCP!64.23.8.174:0
751 * gnunet://hello/0430205UC7D56PTQK8NV05776671CNN44FK4TL6D0GQ35OMF8MEN4RNMKA5UF6AL3DQO8B1SC5AQF50SQ2MABIRU4HC8H2HAJKJ59JL1JVRJAK308F9GASRFLMGUBB5TQ5AKR94AS5T3MDG8B9O1EMPRKB0HVCG7T6QPP4CDJ913LAEHVJ2DI1TOBB15Q1JIT5ARBOD12U4SIGRFDV3Q7T66G4TBVSJJ90UQF1BG29TGJJKLGEIMSPHHKO544D6EALQ4F2K0416311JC22GVAD48R616I7VK03K7MP7N0RS2MBV1TE9JV8CK1LSQMR7KCDRTLDA6917UGA67DHTGHERIACCGQ54TGSR48RMSGS9BA5HLMOKASFC1I6V4TT09TUGCU8GNDHQF0JF3H7LPV59UL5I38QID040G000!20120302010059!TCP!(2001:db8:85a3:8d3:1319:8a2e:370:7348):2086
757 /* ************************* Compose HELLO URI ************************** */
761 * Replace all characters in the input 'in' according
762 * to the mapping. The mapping says to map each character
763 * in 'oldchars' to the corresponding character (by offset)
766 * @param in input string to remap
767 * @param oldchars characters to replace
768 * @param newchars replacement characters, must have same length as 'oldchars'
769 * @return copy of string with replacement applied.
772 map_characters (const char *in,
773 const char *oldchars,
774 const char *newchars)
780 GNUNET_assert (strlen (oldchars) == strlen (newchars));
781 ret = GNUNET_strdup (in);
783 while (ret[i] != '\0')
785 off = strchr (oldchars, ret[i]);
787 ret[i] = newchars[off - oldchars];
795 * Function that is called on each address of this peer.
796 * Expands the corresponding URI string.
798 * @param cls the 'GNUNET_HELLO_GetUriContext'
799 * @param address address to add
800 * @param expiration expiration time for the address
801 * @return GNUNET_OK (continue iteration).
804 add_address_to_uri (void *cls, const struct GNUNET_HELLO_Address *address,
805 struct GNUNET_TIME_Absolute expiration)
807 struct GNUNET_HELLO_ComposeUriContext *ctx = cls;
808 struct GNUNET_TRANSPORT_PluginFunctions *papi;
815 char *client_str = "_client";
819 papi = ctx->plugins_find (address->transport_name);
822 /* Not an error - we might just not have the right plugin. */
825 if (NULL == papi->address_to_string)
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "URI conversion not implemented for plugin `%s'\n",
829 address->transport_name);
832 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
834 if ( (addr == NULL) || (strlen(addr) == 0) )
837 addr_dup = GNUNET_strdup (addr);
838 if (NULL != (pos = strstr (addr_dup, "_server")))
839 memcpy (pos, client_str, strlen(client_str)); /* Replace all server addresses with client addresses */
841 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
842 characters in URIs */
843 uri_addr = map_characters (addr_dup, "[]", "()");
844 GNUNET_free (addr_dup);
845 seconds = expiration.abs_value_us / 1000LL / 1000LL;
846 t = gmtime (&seconds);
848 GNUNET_asprintf (&ret,
851 strftime (tbuf, sizeof (tbuf), "%Y%m%d%H%M%S", t) ? tbuf : "0",
852 address->transport_name,
854 GNUNET_free (uri_addr);
855 GNUNET_free (ctx->uri);
862 * Compose a hello URI string from a hello message.
864 * @param hello Hello message
865 * @param plugins_find Function to find transport plugins by name
866 * @return Hello URI string
869 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
870 GNUNET_HELLO_TransportPluginsFind plugins_find)
872 struct GNUNET_HELLO_ComposeUriContext ctx;
873 ctx.plugins_find = plugins_find;
875 char *pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&(hello->publicKey));
877 GNUNET_asprintf (&(ctx.uri),
879 (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? GNUNET_FRIEND_HELLO_URI_PREFIX : GNUNET_HELLO_URI_PREFIX,
883 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &add_address_to_uri, &ctx);
888 /* ************************* Parse HELLO URI ********************* */
892 * We're building a HELLO. Parse the next address from the
893 * parsing context and append it.
895 * @param cls the 'struct GNUNET_HELLO_AddressParsingContext'
896 * @param max number of bytes available for HELLO construction
897 * @param buffer where to copy the next address (in binary format)
898 * @return number of bytes added to buffer
901 add_address_to_hello (void *cls, size_t max, void *buffer)
903 struct GNUNET_HELLO_ParseUriContext *ctx = cls;
907 char *plugin_address;
910 struct tm expiration_time;
911 time_t expiration_seconds;
912 struct GNUNET_TIME_Absolute expire;
913 struct GNUNET_TRANSPORT_PluginFunctions *papi;
916 struct GNUNET_HELLO_Address haddr;
919 if (NULL == ctx->pos)
921 if ('!' != ctx->pos[0])
923 ctx->ret = GNUNET_SYSERR;
929 if ('0' == ctx->pos[0] && '!' == ctx->pos[1])
931 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
932 tname = ctx->pos + 1;
936 memset (&expiration_time, 0, sizeof (expiration_time));
937 tname = strptime (ctx->pos,
942 ctx->ret = GNUNET_SYSERR;
943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
944 _("Failed to parse HELLO message: missing expiration time\n"));
949 expiration_seconds = mktime (&expiration_time);
950 if (expiration_seconds == (time_t) -1)
952 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
953 _("Failed to parse HELLO message: invalid expiration time\n"));
954 ctx->ret = GNUNET_SYSERR;
958 expire.abs_value_us = expiration_seconds * 1000LL * 1000LL;
962 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
963 _("Failed to parse HELLO message: malformed\n"));
964 ctx->ret = GNUNET_SYSERR;
969 address = strchr (tname, (int) '!');
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 _("Failed to parse HELLO message: missing transport plugin\n"));
974 ctx->ret = GNUNET_SYSERR;
979 end = strchr (address, (int) '!');
981 plugin_name = GNUNET_strndup (tname, address - (tname+1));
982 papi = ctx->plugins_find (plugin_name);
985 /* Not an error - we might just not have the right plugin.
986 * Skip this part, advance to the next one and recurse.
987 * But only if this is not the end of string.
989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
990 _("Plugin `%s' not found\n"),
992 GNUNET_free (plugin_name);
996 if (NULL == papi->string_to_address)
998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
999 _("Plugin `%s' does not support URIs yet\n"),
1001 GNUNET_free (plugin_name);
1005 uri_address = GNUNET_strndup (address, end - address);
1006 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
1007 characters in URIs; need to convert back to '[]' for the plugin */
1008 plugin_address = map_characters (uri_address, "()", "[]");
1009 GNUNET_free (uri_address);
1011 papi->string_to_address (papi->cls,
1013 strlen (plugin_address) + 1,
1017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1018 _("Failed to parse `%s' as an address for plugin `%s'\n"),
1021 GNUNET_free (plugin_name);
1022 GNUNET_free (plugin_address);
1025 GNUNET_free (plugin_address);
1026 /* address.peer is unset - not used by add_address() */
1027 haddr.address_length = addr_len;
1028 haddr.address = addr;
1029 haddr.transport_name = plugin_name;
1030 ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
1032 GNUNET_free (plugin_name);
1038 * Parse a hello URI string to a hello message.
1040 * @param uri URI string to parse
1041 * @param pubkey Pointer to struct where public key is parsed
1042 * @param hello Pointer to struct where hello message is parsed
1043 * @param plugins_find Function to find transport plugins by name
1044 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
1047 GNUNET_HELLO_parse_uri (const char *uri,
1048 struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
1049 struct GNUNET_HELLO_Message **hello,
1050 GNUNET_HELLO_TransportPluginsFind plugins_find)
1055 struct GNUNET_HELLO_ParseUriContext ctx;
1057 if (0 == strncmp (uri,
1058 GNUNET_HELLO_URI_PREFIX,
1059 strlen (GNUNET_HELLO_URI_PREFIX)))
1061 pks = &uri[strlen (GNUNET_HELLO_URI_PREFIX)];
1062 friend_only = GNUNET_NO;
1064 else if (0 == strncmp (uri,
1065 GNUNET_FRIEND_HELLO_URI_PREFIX,
1066 strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)))
1068 pks = &uri[strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)];
1069 friend_only = GNUNET_YES;
1072 return GNUNET_SYSERR;
1073 exc = strstr (pks, "!");
1076 GNUNET_STRINGS_string_to_data (pks,
1077 (NULL == exc) ? strlen (pks) : (exc - pks),
1078 (unsigned char *) pubkey,
1080 return GNUNET_SYSERR;
1083 ctx.ret = GNUNET_OK;
1084 ctx.plugins_find = plugins_find;
1085 *hello = GNUNET_HELLO_create (pubkey, &add_address_to_hello, &ctx, friend_only);
1091 /* end of hello.c */