2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2011 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
24 * Helper library for handling HELLOs
26 * @defgroup hello Hello library
27 * Helper library for handling HELLOs
29 * @see [Documentation](https://gnunet.org/gnunets-hostlist-subsystem)
34 #ifndef GNUNET_HELLO_LIB_H
35 #define GNUNET_HELLO_LIB_H
40 #if 0 /* keep Emacsens' auto-indent happy */
45 #include "gnunet_util_lib.h"
48 * Prefix that every HELLO URI must start with.
50 #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
53 * Prefix that every FRIEND HELLO URI must start with.
55 #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
58 * Separator used in HELLO URI
60 #define GNUNET_HELLO_URI_SEP '+'
64 * Additional local information about an address
66 * These information are only valid for the local peer and are not serialized
67 * when a #GNUNET_HELLO_Message is created
69 enum GNUNET_HELLO_AddressInfo
72 * No additional information
74 GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
77 * This is an inbound address and cannot be used to initiate an outbound
78 * connection to another peer
80 GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
85 * An address for communicating with a peer. We frequently
86 * need this tuple and the components cannot really be
87 * separated. This is NOT the format that would be used
90 struct GNUNET_HELLO_Address
94 * For which peer is this an address?
96 struct GNUNET_PeerIdentity peer;
99 * Name of the transport plugin enabling the communication using
102 const char *transport_name;
105 * Binary representation of the address (plugin-specific).
110 * Number of bytes in @e address.
112 size_t address_length;
115 * Extended information about address
117 * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
118 * to indicate an address is inbound and cannot be used to initiate an
119 * outbound connection.
121 * These information are only valid for the local peer and are not serialized
122 * when a #GNUNET_HELLO_Message is created
124 enum GNUNET_HELLO_AddressInfo local_info;
130 * Allocate an address struct.
132 * @param peer the peer
133 * @param transport_name plugin name
134 * @param address binary address
135 * @param address_length number of bytes in @a address
136 * @param local_info additional local information for the address
137 * @return the address struct
139 struct GNUNET_HELLO_Address *
140 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
141 const char *transport_name,
143 size_t address_length,
144 enum GNUNET_HELLO_AddressInfo local_info);
148 * Copy an address struct.
150 * @param address address to copy
151 * @return a copy of the address struct
153 struct GNUNET_HELLO_Address *
154 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
158 * Compare two addresses. Does NOT compare the peer identity,
159 * that is assumed already to match!
161 * @param a1 first address
162 * @param a2 second address
163 * @return 0 if the addresses are equal, -1 if @a a1< @a a2, 1 if @a a1> @a a2.
166 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
167 const struct GNUNET_HELLO_Address *a2);
171 * Get the size of an address struct.
173 * @param address address
177 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
181 * Check if an address has a local option set
183 * @param address the address to check
184 * @param option the respective option to check for
185 * @return #GNUNET_YES or #GNUNET_NO
188 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
189 enum GNUNET_HELLO_AddressInfo option);
195 * @param addr address to free
197 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
200 GNUNET_NETWORK_STRUCT_BEGIN
203 * A HELLO message is used to exchange information about
204 * transports with other peers. This struct is always
205 * followed by the actual network addresses which have
208 * 1) transport-name (0-terminated)
209 * 2) address-length (uint16_t, network byte order; possibly
211 * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
213 * 4) address (address-length bytes; possibly unaligned!)
215 struct GNUNET_HELLO_Message
218 * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
220 struct GNUNET_MessageHeader header;
223 * Use in F2F mode: Do not gossip this HELLO message
225 uint32_t friend_only GNUNET_PACKED;
228 * The public key of the peer.
230 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
233 GNUNET_NETWORK_STRUCT_END
240 * @param h HELLO Message to test
241 * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
244 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
248 * Copy the given address information into
249 * the given buffer using the format of HELLOs.
251 * @param address address to add
252 * @param expiration expiration for the address
253 * @param target where to copy the address
254 * @param max maximum number of bytes to copy to @a target
255 * @return number of bytes copied, 0 if
256 * the target buffer was not big enough.
259 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
260 struct GNUNET_TIME_Absolute expiration,
266 * Callback function used to fill a buffer of max bytes with a list of
267 * addresses in the format used by HELLOs. Should use
268 * #GNUNET_HELLO_add_address() as a helper function.
271 * @param max maximum number of bytes that can be written to @a buf
272 * @param buf where to write the address information
273 * @return number of bytes written or 0, #GNUNET_SYSERR to signal the
274 * end of the iteration.
277 (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
283 * Construct a HELLO message given the public key,
284 * expiration time and an iterator that spews the
285 * transport addresses.
287 * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
288 * will not be gossiped to other peers.
290 * @param public_key public key to include in the HELLO
291 * @param addrgen callback to invoke to get addresses
292 * @param addrgen_cls closure for @a addrgen
293 * @param friend_only should the returned HELLO be only visible to friends?
294 * @return the hello message
296 struct GNUNET_HELLO_Message *
297 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
298 GNUNET_HELLO_GenerateAddressListCallback addrgen,
304 * Return the size of the given HELLO message.
306 * @param hello to inspect
307 * @return the size, 0 if HELLO is invalid
310 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
314 * Construct a HELLO message by merging the
315 * addresses in two existing HELLOs (which
316 * must be for the same peer).
318 * @param h1 first HELLO message
319 * @param h2 the second HELLO message
320 * @return the combined hello message
322 struct GNUNET_HELLO_Message *
323 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
324 const struct GNUNET_HELLO_Message *h2);
328 * Test if two HELLO messages contain the same addresses.
329 * If they only differ in expiration time, the lowest
330 * expiration time larger than 'now' where they differ
333 * @param h1 first HELLO message
334 * @param h2 the second HELLO message
335 * @param now time to use for deciding which addresses have
336 * expired and should not be considered at all
337 * @return absolute time forever if the two HELLOs are
338 * totally identical; smallest timestamp >= now if
339 * they only differ in timestamps;
340 * zero if the some addresses with expirations >= now
341 * do not match at all
343 struct GNUNET_TIME_Absolute
344 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
345 const struct GNUNET_HELLO_Message *h2,
346 struct GNUNET_TIME_Absolute now);
350 * Iterator callback to go over all addresses.
353 * @param address the address
354 * @param expiration expiration time
355 * @return #GNUNET_OK to keep the address,
356 * #GNUNET_NO to delete it from the HELLO
357 * #GNUNET_SYSERR to stop iterating (but keep current address)
360 (*GNUNET_HELLO_AddressIterator) (void *cls,
361 const struct GNUNET_HELLO_Address *address,
362 struct GNUNET_TIME_Absolute expiration);
366 * When does the last address in the given HELLO expire?
368 * @param msg HELLO to inspect
369 * @return time the last address expires, 0 if there are no addresses in the HELLO
371 struct GNUNET_TIME_Absolute
372 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
376 * Iterate over all of the addresses in the HELLO.
378 * @param msg HELLO to iterate over; client does not need to
379 * have verified that msg is well-formed (beyond starting
380 * with a GNUNET_MessageHeader of the right type).
381 * @param return_modified if a modified copy should be returned,
382 * otherwise NULL will be returned
383 * @param it iterator to call on each address
384 * @param it_cls closure for @a it
385 * @return the modified HELLO or NULL
387 struct GNUNET_HELLO_Message *
388 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
390 GNUNET_HELLO_AddressIterator it, void *it_cls);
394 * Iterate over addresses in @a new_hello that are NOT already present
395 * in @a old_hello. Note that if the address is present in @a old_hello
396 * but the expiration time in @a new_hello is more recent, the
397 * iterator is also called.
399 * @param new_hello a HELLO message
400 * @param old_hello a HELLO message
401 * @param expiration_limit ignore addresses in old_hello
402 * that expired before the given time stamp
403 * @param it iterator to call on each address
404 * @param it_cls closure for @a it
407 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
408 const struct GNUNET_HELLO_Message *old_hello,
409 struct GNUNET_TIME_Absolute expiration_limit,
410 GNUNET_HELLO_AddressIterator it,
415 * Get the peer identity from a HELLO message.
417 * @param hello the hello message
418 * @param peer where to store the peer's identity
419 * @return #GNUNET_SYSERR if the HELLO was malformed
422 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
423 struct GNUNET_PeerIdentity *peer);
427 * Get the header from a HELLO message, used so other code
428 * can correctly send HELLO messages.
430 * @param hello the hello message
432 * @return header or NULL if the HELLO was malformed
434 struct GNUNET_MessageHeader *
435 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
439 * Helper function to load/access transport plugins.
440 * FIXME: pass closure!
442 * @param name name of the transport plugin to load
443 * @return NULL if a plugin with name @a name is not known/loadable
445 typedef struct GNUNET_TRANSPORT_PluginFunctions *
446 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
450 * Compose a hello URI string from a hello message.
452 * @param hello Hello message
453 * @param plugins_find Function to find transport plugins by name
454 * @return Hello URI string
457 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
458 GNUNET_HELLO_TransportPluginsFind plugins_find);
462 * Parse a hello URI string to a hello message.
464 * @param uri URI string to parse
465 * @param pubkey Pointer to struct where public key is parsed
466 * @param hello Pointer to struct where hello message is parsed
467 * @param plugins_find Function to find transport plugins by name
468 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
471 GNUNET_HELLO_parse_uri (const char *uri,
472 struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
473 struct GNUNET_HELLO_Message **hello,
474 GNUNET_HELLO_TransportPluginsFind plugins_find);
479 #include "gnunet_nt_lib.h"
482 * Key used for storing HELLOs in the peerstore
484 #define GNUNET_HELLO_PEERSTORE_KEY "hello"
487 * Build address record by signing raw information with private key.
489 * @param address text address to sign
490 * @param nt network type of @a address
491 * @param expiration how long is @a address valid
492 * @param private_key signing key to use
493 * @param result[out] where to write address record (allocated)
494 * @param result_size[out] set to size of @a result
497 GNUNET_HELLO_sign_address (const char *address,
498 enum GNUNET_NetworkType nt,
499 struct GNUNET_TIME_Absolute expiration,
500 const struct GNUNET_CRYPTO_EddsaPrivateKey *private_key,
502 size_t *result_size);
506 * Check signature and extract address record.
508 * @param raw raw signed address
509 * @param raw_size size of @a raw
510 * @param pid public key to use for signature verification
511 * @param nt[out] set to network type
512 * @param expiration[out] how long is the address valid
513 * @return NULL on error, otherwise the address
516 GNUNET_HELLO_extract_address (const void *raw,
518 const struct GNUNET_PeerIdentity *pid,
519 enum GNUNET_NetworkType *nt,
520 struct GNUNET_TIME_Absolute *expiration);
524 * Given an address as a string, extract the prefix that identifies
525 * the communicator offering transmissions to that address.
527 * @param address a peer's address
528 * @return NULL if the address is mal-formed, otherwise the prefix
531 GNUNET_HELLO_address_to_prefix (const char *address);
534 #if 0 /* keep Emacsens' auto-indent happy */
541 /* ifndef GNUNET_HELLO_LIB_H */
544 /** @} */ /* end of group */
546 /* end of gnunet_hello_lib.h */