2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2011 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file include/gnunet_hello_lib.h
23 * @brief helper library for handling HELLOs
24 * @author Christian Grothoff
27 #ifndef GNUNET_HELLO_LIB_H
28 #define GNUNET_HELLO_LIB_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_util_lib.h"
42 * Prefix that every HELLO URI must start with.
44 #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
47 * Prefix that every FRIEND HELLO URI must start with.
49 #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
52 * Separator used in HELLO URI
54 #define GNUNET_HELLO_URI_SEP '+'
58 * Additional local information about an address
60 * These information are only valid for the local peer and are not serialized
61 * when a #GNUNET_HELLO_Message is created
63 enum GNUNET_HELLO_AddressInfo
66 * No additional information
68 GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
71 * This is an inbound address and cannot be used to initiate an outbound
72 * connection to another peer
74 GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
79 * An address for communicating with a peer. We frequently
80 * need this tuple and the components cannot really be
81 * separated. This is NOT the format that would be used
84 struct GNUNET_HELLO_Address
88 * For which peer is this an address?
90 struct GNUNET_PeerIdentity peer;
93 * Name of the transport plugin enabling the communication using
96 const char *transport_name;
99 * Binary representation of the address (plugin-specific).
104 * Number of bytes in @e address.
106 size_t address_length;
109 * Extended information about address
111 * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
112 * to indicate an address is inbound and cannot be used to initiate an
113 * outbound connection.
115 * These information are only valid for the local peer and are not serialized
116 * when a #GNUNET_HELLO_Message is created
118 enum GNUNET_HELLO_AddressInfo local_info;
124 * Allocate an address struct.
126 * @param peer the peer
127 * @param transport_name plugin name
128 * @param address binary address
129 * @param address_length number of bytes in @a address
130 * @param local_info additional local information for the address
131 * @return the address struct
133 struct GNUNET_HELLO_Address *
134 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
135 const char *transport_name,
137 size_t address_length,
138 enum GNUNET_HELLO_AddressInfo local_info);
142 * Copy an address struct.
144 * @param address address to copy
145 * @return a copy of the address struct
147 struct GNUNET_HELLO_Address *
148 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
152 * Compare two addresses. Does NOT compare the peer identity,
153 * that is assumed already to match!
155 * @param a1 first address
156 * @param a2 second address
157 * @return 0 if the addresses are equal, -1 if @a a1< @a a2, 1 if @a a1> @a a2.
160 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
161 const struct GNUNET_HELLO_Address *a2);
165 * Get the size of an address struct.
167 * @param address address
171 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
175 * Check if an address has a local option set
177 * @param address the address to check
178 * @param option the respective option to check for
179 * @return #GNUNET_YES or #GNUNET_NO
182 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
183 enum GNUNET_HELLO_AddressInfo option);
189 * @param addr address to free
191 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
195 * A HELLO message is used to exchange information about
196 * transports with other peers. This struct is guaranteed
197 * to start with a `struct GNUNET_MessageHeader`, everything else
198 * should be internal to the HELLO library.
200 struct GNUNET_HELLO_Message;
206 * @param h HELLO Message to test
207 * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
210 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
214 * Copy the given address information into
215 * the given buffer using the format of HELLOs.
217 * @param address address to add
218 * @param expiration expiration for the address
219 * @param target where to copy the address
220 * @param max maximum number of bytes to copy to @a target
221 * @return number of bytes copied, 0 if
222 * the target buffer was not big enough.
225 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
226 struct GNUNET_TIME_Absolute expiration,
232 * Callback function used to fill a buffer of max bytes with a list of
233 * addresses in the format used by HELLOs. Should use
234 * #GNUNET_HELLO_add_address() as a helper function.
237 * @param max maximum number of bytes that can be written to @a buf
238 * @param buf where to write the address information
239 * @return number of bytes written or 0, #GNUNET_SYSERR to signal the
240 * end of the iteration.
243 (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
249 * Construct a HELLO message given the public key,
250 * expiration time and an iterator that spews the
251 * transport addresses.
253 * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
254 * will not be gossiped to other peers.
256 * @param public_key public key to include in the HELLO
257 * @param addrgen callback to invoke to get addresses
258 * @param addrgen_cls closure for @a addrgen
259 * @param friend_only should the returned HELLO be only visible to friends?
260 * @return the hello message
262 struct GNUNET_HELLO_Message *
263 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
264 GNUNET_HELLO_GenerateAddressListCallback addrgen,
270 * Return the size of the given HELLO message.
272 * @param hello to inspect
273 * @return the size, 0 if HELLO is invalid
276 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
280 * Construct a HELLO message by merging the
281 * addresses in two existing HELLOs (which
282 * must be for the same peer).
284 * @param h1 first HELLO message
285 * @param h2 the second HELLO message
286 * @return the combined hello message
288 struct GNUNET_HELLO_Message *
289 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
290 const struct GNUNET_HELLO_Message *h2);
294 * Test if two HELLO messages contain the same addresses.
295 * If they only differ in expiration time, the lowest
296 * expiration time larger than 'now' where they differ
299 * @param h1 first HELLO message
300 * @param h2 the second HELLO message
301 * @param now time to use for deciding which addresses have
302 * expired and should not be considered at all
303 * @return absolute time forever if the two HELLOs are
304 * totally identical; smallest timestamp >= now if
305 * they only differ in timestamps;
306 * zero if the some addresses with expirations >= now
307 * do not match at all
309 struct GNUNET_TIME_Absolute
310 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
311 const struct GNUNET_HELLO_Message *h2,
312 struct GNUNET_TIME_Absolute now);
316 * Iterator callback to go over all addresses.
319 * @param address the address
320 * @param expiration expiration time
321 * @return #GNUNET_OK to keep the address,
322 * #GNUNET_NO to delete it from the HELLO
323 * #GNUNET_SYSERR to stop iterating (but keep current address)
326 (*GNUNET_HELLO_AddressIterator) (void *cls,
327 const struct GNUNET_HELLO_Address *address,
328 struct GNUNET_TIME_Absolute expiration);
332 * When does the last address in the given HELLO expire?
334 * @param msg HELLO to inspect
335 * @return time the last address expires, 0 if there are no addresses in the HELLO
337 struct GNUNET_TIME_Absolute
338 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
342 * Iterate over all of the addresses in the HELLO.
344 * @param msg HELLO to iterate over; client does not need to
345 * have verified that msg is well-formed (beyond starting
346 * with a GNUNET_MessageHeader of the right type).
347 * @param return_modified if a modified copy should be returned,
348 * otherwise NULL will be returned
349 * @param it iterator to call on each address
350 * @param it_cls closure for @a it
351 * @return the modified HELLO or NULL
353 struct GNUNET_HELLO_Message *
354 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
356 GNUNET_HELLO_AddressIterator it, void *it_cls);
360 * Iterate over addresses in @a new_hello that are NOT already present
361 * in @a old_hello. Note that if the address is present in @a old_hello
362 * but the expiration time in @a new_hello is more recent, the
363 * iterator is also called.
365 * @param new_hello a HELLO message
366 * @param old_hello a HELLO message
367 * @param expiration_limit ignore addresses in old_hello
368 * that expired before the given time stamp
369 * @param it iterator to call on each address
370 * @param it_cls closure for @a it
373 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
374 const struct GNUNET_HELLO_Message *old_hello,
375 struct GNUNET_TIME_Absolute expiration_limit,
376 GNUNET_HELLO_AddressIterator it,
381 * Get the peer identity from a HELLO message.
383 * @param hello the hello message
384 * @param peer where to store the peer's identity
385 * @return #GNUNET_SYSERR if the HELLO was malformed
388 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
389 struct GNUNET_PeerIdentity *peer);
393 * Get the header from a HELLO message, used so other code
394 * can correctly send HELLO messages.
396 * @param hello the hello message
398 * @return header or NULL if the HELLO was malformed
400 struct GNUNET_MessageHeader *
401 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
405 * Helper function to load/access transport plugins.
406 * FIXME: pass closure!
408 * @param name name of the transport plugin to load
409 * @return NULL if a plugin with name @a name is not known/loadable
411 typedef struct GNUNET_TRANSPORT_PluginFunctions *
412 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
416 * Compose a hello URI string from a hello message.
418 * @param hello Hello message
419 * @param plugins_find Function to find transport plugins by name
420 * @return Hello URI string
423 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
424 GNUNET_HELLO_TransportPluginsFind plugins_find);
428 * Parse a hello URI string to a hello message.
430 * @param uri URI string to parse
431 * @param pubkey Pointer to struct where public key is parsed
432 * @param hello Pointer to struct where hello message is parsed
433 * @param plugins_find Function to find transport plugins by name
434 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
437 GNUNET_HELLO_parse_uri (const char *uri,
438 struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
439 struct GNUNET_HELLO_Message **hello,
440 GNUNET_HELLO_TransportPluginsFind plugins_find);
442 #if 0 /* keep Emacsens' auto-indent happy */
450 /* ifndef GNUNET_HELLO_LIB_H */
452 /* end of gnunet_hello_lib.h */