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/>.
20 * @author Christian Grothoff
22 * Helper library for handling HELLOs
24 * @defgroup hello Hello library
25 * Helper library for handling HELLOs
27 * @see [Documentation](https://gnunet.org/gnunets-hostlist-subsystem)
32 #ifndef GNUNET_HELLO_LIB_H
33 #define GNUNET_HELLO_LIB_H
38 #if 0 /* keep Emacsens' auto-indent happy */
43 #include "gnunet_util_lib.h"
47 * Prefix that every HELLO URI must start with.
49 #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
52 * Prefix that every FRIEND HELLO URI must start with.
54 #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
57 * Separator used in HELLO URI
59 #define GNUNET_HELLO_URI_SEP '+'
63 * Additional local information about an address
65 * These information are only valid for the local peer and are not serialized
66 * when a #GNUNET_HELLO_Message is created
68 enum GNUNET_HELLO_AddressInfo
71 * No additional information
73 GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
76 * This is an inbound address and cannot be used to initiate an outbound
77 * connection to another peer
79 GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
84 * An address for communicating with a peer. We frequently
85 * need this tuple and the components cannot really be
86 * separated. This is NOT the format that would be used
89 struct GNUNET_HELLO_Address
93 * For which peer is this an address?
95 struct GNUNET_PeerIdentity peer;
98 * Name of the transport plugin enabling the communication using
101 const char *transport_name;
104 * Binary representation of the address (plugin-specific).
109 * Number of bytes in @e address.
111 size_t address_length;
114 * Extended information about address
116 * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
117 * to indicate an address is inbound and cannot be used to initiate an
118 * outbound connection.
120 * These information are only valid for the local peer and are not serialized
121 * when a #GNUNET_HELLO_Message is created
123 enum GNUNET_HELLO_AddressInfo local_info;
129 * Allocate an address struct.
131 * @param peer the peer
132 * @param transport_name plugin name
133 * @param address binary address
134 * @param address_length number of bytes in @a address
135 * @param local_info additional local information for the address
136 * @return the address struct
138 struct GNUNET_HELLO_Address *
139 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
140 const char *transport_name,
142 size_t address_length,
143 enum GNUNET_HELLO_AddressInfo local_info);
147 * Copy an address struct.
149 * @param address address to copy
150 * @return a copy of the address struct
152 struct GNUNET_HELLO_Address *
153 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
157 * Compare two addresses. Does NOT compare the peer identity,
158 * that is assumed already to match!
160 * @param a1 first address
161 * @param a2 second address
162 * @return 0 if the addresses are equal, -1 if @a a1< @a a2, 1 if @a a1> @a a2.
165 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
166 const struct GNUNET_HELLO_Address *a2);
170 * Get the size of an address struct.
172 * @param address address
176 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
180 * Check if an address has a local option set
182 * @param address the address to check
183 * @param option the respective option to check for
184 * @return #GNUNET_YES or #GNUNET_NO
187 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
188 enum GNUNET_HELLO_AddressInfo option);
194 * @param addr address to free
196 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
199 GNUNET_NETWORK_STRUCT_BEGIN
202 * A HELLO message is used to exchange information about
203 * transports with other peers. This struct is always
204 * followed by the actual network addresses which have
207 * 1) transport-name (0-terminated)
208 * 2) address-length (uint16_t, network byte order; possibly
210 * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
212 * 4) address (address-length bytes; possibly unaligned!)
214 struct GNUNET_HELLO_Message
217 * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
219 struct GNUNET_MessageHeader header;
222 * Use in F2F mode: Do not gossip this HELLO message
224 uint32_t friend_only GNUNET_PACKED;
227 * The public key of the peer.
229 struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
232 GNUNET_NETWORK_STRUCT_END
239 * @param h HELLO Message to test
240 * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
243 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
247 * Copy the given address information into
248 * the given buffer using the format of HELLOs.
250 * @param address address to add
251 * @param expiration expiration for the address
252 * @param target where to copy the address
253 * @param max maximum number of bytes to copy to @a target
254 * @return number of bytes copied, 0 if
255 * the target buffer was not big enough.
258 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
259 struct GNUNET_TIME_Absolute expiration,
265 * Callback function used to fill a buffer of max bytes with a list of
266 * addresses in the format used by HELLOs. Should use
267 * #GNUNET_HELLO_add_address() as a helper function.
270 * @param max maximum number of bytes that can be written to @a buf
271 * @param buf where to write the address information
272 * @return number of bytes written or 0, #GNUNET_SYSERR to signal the
273 * end of the iteration.
276 (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
282 * Construct a HELLO message given the public key,
283 * expiration time and an iterator that spews the
284 * transport addresses.
286 * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
287 * will not be gossiped to other peers.
289 * @param public_key public key to include in the HELLO
290 * @param addrgen callback to invoke to get addresses
291 * @param addrgen_cls closure for @a addrgen
292 * @param friend_only should the returned HELLO be only visible to friends?
293 * @return the hello message
295 struct GNUNET_HELLO_Message *
296 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
297 GNUNET_HELLO_GenerateAddressListCallback addrgen,
303 * Return the size of the given HELLO message.
305 * @param hello to inspect
306 * @return the size, 0 if HELLO is invalid
309 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
313 * Construct a HELLO message by merging the
314 * addresses in two existing HELLOs (which
315 * must be for the same peer).
317 * @param h1 first HELLO message
318 * @param h2 the second HELLO message
319 * @return the combined hello message
321 struct GNUNET_HELLO_Message *
322 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
323 const struct GNUNET_HELLO_Message *h2);
327 * Test if two HELLO messages contain the same addresses.
328 * If they only differ in expiration time, the lowest
329 * expiration time larger than 'now' where they differ
332 * @param h1 first HELLO message
333 * @param h2 the second HELLO message
334 * @param now time to use for deciding which addresses have
335 * expired and should not be considered at all
336 * @return absolute time forever if the two HELLOs are
337 * totally identical; smallest timestamp >= now if
338 * they only differ in timestamps;
339 * zero if the some addresses with expirations >= now
340 * do not match at all
342 struct GNUNET_TIME_Absolute
343 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
344 const struct GNUNET_HELLO_Message *h2,
345 struct GNUNET_TIME_Absolute now);
349 * Iterator callback to go over all addresses.
352 * @param address the address
353 * @param expiration expiration time
354 * @return #GNUNET_OK to keep the address,
355 * #GNUNET_NO to delete it from the HELLO
356 * #GNUNET_SYSERR to stop iterating (but keep current address)
359 (*GNUNET_HELLO_AddressIterator) (void *cls,
360 const struct GNUNET_HELLO_Address *address,
361 struct GNUNET_TIME_Absolute expiration);
365 * When does the last address in the given HELLO expire?
367 * @param msg HELLO to inspect
368 * @return time the last address expires, 0 if there are no addresses in the HELLO
370 struct GNUNET_TIME_Absolute
371 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
375 * Iterate over all of the addresses in the HELLO.
377 * @param msg HELLO to iterate over; client does not need to
378 * have verified that msg is well-formed (beyond starting
379 * with a GNUNET_MessageHeader of the right type).
380 * @param return_modified if a modified copy should be returned,
381 * otherwise NULL will be returned
382 * @param it iterator to call on each address
383 * @param it_cls closure for @a it
384 * @return the modified HELLO or NULL
386 struct GNUNET_HELLO_Message *
387 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
389 GNUNET_HELLO_AddressIterator it, void *it_cls);
393 * Iterate over addresses in @a new_hello that are NOT already present
394 * in @a old_hello. Note that if the address is present in @a old_hello
395 * but the expiration time in @a new_hello is more recent, the
396 * iterator is also called.
398 * @param new_hello a HELLO message
399 * @param old_hello a HELLO message
400 * @param expiration_limit ignore addresses in old_hello
401 * that expired before the given time stamp
402 * @param it iterator to call on each address
403 * @param it_cls closure for @a it
406 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
407 const struct GNUNET_HELLO_Message *old_hello,
408 struct GNUNET_TIME_Absolute expiration_limit,
409 GNUNET_HELLO_AddressIterator it,
414 * Get the peer identity from a HELLO message.
416 * @param hello the hello message
417 * @param peer where to store the peer's identity
418 * @return #GNUNET_SYSERR if the HELLO was malformed
421 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
422 struct GNUNET_PeerIdentity *peer);
426 * Get the header from a HELLO message, used so other code
427 * can correctly send HELLO messages.
429 * @param hello the hello message
431 * @return header or NULL if the HELLO was malformed
433 struct GNUNET_MessageHeader *
434 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
438 * Helper function to load/access transport plugins.
439 * FIXME: pass closure!
441 * @param name name of the transport plugin to load
442 * @return NULL if a plugin with name @a name is not known/loadable
444 typedef struct GNUNET_TRANSPORT_PluginFunctions *
445 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
449 * Compose a hello URI string from a hello message.
451 * @param hello Hello message
452 * @param plugins_find Function to find transport plugins by name
453 * @return Hello URI string
456 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
457 GNUNET_HELLO_TransportPluginsFind plugins_find);
461 * Parse a hello URI string to a hello message.
463 * @param uri URI string to parse
464 * @param pubkey Pointer to struct where public key is parsed
465 * @param hello Pointer to struct where hello message is parsed
466 * @param plugins_find Function to find transport plugins by name
467 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
470 GNUNET_HELLO_parse_uri (const char *uri,
471 struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
472 struct GNUNET_HELLO_Message **hello,
473 GNUNET_HELLO_TransportPluginsFind plugins_find);
476 #if 0 /* keep Emacsens' auto-indent happy */
483 /* ifndef GNUNET_HELLO_LIB_H */
486 /** @} */ /* end of group */
488 /* end of gnunet_hello_lib.h */