towards flow control in TNG
[oweals/gnunet.git] / src / include / gnunet_hello_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2011 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @author Christian Grothoff
23  * @file
24  * Helper library for handling HELLOs
25  *
26  * @defgroup hello  Hello library
27  * Helper library for handling HELLOs
28  *
29  * @see [Documentation](https://gnunet.org/gnunets-hostlist-subsystem)
30  *
31  * @{
32  */
33
34 #ifndef GNUNET_HELLO_LIB_H
35 #define GNUNET_HELLO_LIB_H
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #if 0                           /* keep Emacsens' auto-indent happy */
41 }
42 #endif
43 #endif
44
45 #include "gnunet_util_lib.h"
46
47 /**
48  * Prefix that every HELLO URI must start with.
49  */
50 #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
51
52 /**
53  * Prefix that every FRIEND HELLO URI must start with.
54  */
55 #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
56
57 /**
58  * Separator used in HELLO URI
59  */
60 #define GNUNET_HELLO_URI_SEP '+'
61
62
63 /**
64  * Additional local information about an address
65  *
66  * These information are only valid for the local peer and are not serialized
67  * when a #GNUNET_HELLO_Message is created
68  */
69 enum GNUNET_HELLO_AddressInfo
70 {
71   /**
72    * No additional information
73    */
74   GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
75
76   /**
77    * This is an inbound address and cannot be used to initiate an outbound
78    * connection to another peer
79    */
80   GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
81 };
82
83
84 /**
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
88  * on the wire.
89  */
90 struct GNUNET_HELLO_Address
91 {
92
93   /**
94    * For which peer is this an address?
95    */
96   struct GNUNET_PeerIdentity peer;
97
98   /**
99    * Name of the transport plugin enabling the communication using
100    * this address.
101    */
102   const char *transport_name;
103
104   /**
105    * Binary representation of the address (plugin-specific).
106    */
107   const void *address;
108
109   /**
110    * Number of bytes in @e address.
111    */
112   size_t address_length;
113
114   /**
115    * Extended information about address
116    *
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.
120    *
121    * These information are only valid for the local peer and are not serialized
122    * when a #GNUNET_HELLO_Message is created
123    */
124   enum GNUNET_HELLO_AddressInfo local_info;
125
126 };
127
128
129 /**
130  * Allocate an address struct.
131  *
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
138  */
139 struct GNUNET_HELLO_Address *
140 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
141                                const char *transport_name,
142                                const void *address,
143                                size_t address_length,
144                                enum GNUNET_HELLO_AddressInfo local_info);
145
146
147 /**
148  * Copy an address struct.
149  *
150  * @param address address to copy
151  * @return a copy of the address struct
152  */
153 struct GNUNET_HELLO_Address *
154 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
155
156
157 /**
158  * Compare two addresses.  Does NOT compare the peer identity,
159  * that is assumed already to match!
160  *
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.
164  */
165 int
166 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
167                           const struct GNUNET_HELLO_Address *a2);
168
169
170 /**
171  * Get the size of an address struct.
172  *
173  * @param address address
174  * @return the size
175  */
176 size_t
177 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
178
179
180 /**
181  * Check if an address has a local option set
182  *
183  * @param address the address to check
184  * @param option the respective option to check for
185  * @return #GNUNET_YES or #GNUNET_NO
186  */
187 int
188 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
189                                    enum GNUNET_HELLO_AddressInfo option);
190
191
192 /**
193  * Free an address.
194  *
195  * @param addr address to free
196  */
197 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
198
199
200 GNUNET_NETWORK_STRUCT_BEGIN
201
202 /**
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
206  * the format:
207  *
208  * 1) transport-name (0-terminated)
209  * 2) address-length (uint16_t, network byte order; possibly
210  *    unaligned!)
211  * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
212  *    unaligned!)
213  * 4) address (address-length bytes; possibly unaligned!)
214  */
215 struct GNUNET_HELLO_Message
216 {
217   /**
218    * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
219    */
220   struct GNUNET_MessageHeader header;
221
222   /**
223    * Use in F2F mode: Do not gossip this HELLO message
224    */
225   uint32_t friend_only GNUNET_PACKED;
226
227   /**
228    * The public key of the peer.
229    */
230   struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
231
232 };
233 GNUNET_NETWORK_STRUCT_END
234
235
236
237 /**
238  * Return HELLO type
239  *
240  * @param h HELLO Message to test
241  * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
242  */
243 int
244 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
245
246
247 /**
248  * Copy the given address information into
249  * the given buffer using the format of HELLOs.
250  *
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.
257  */
258 size_t
259 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
260                           struct GNUNET_TIME_Absolute expiration,
261                           char *target,
262                           size_t max);
263
264
265 /**
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.
269  *
270  * @param cls closure
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.
275  */
276 typedef ssize_t
277 (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
278                                              size_t max,
279                                              void *buf);
280
281
282 /**
283  * Construct a HELLO message given the public key,
284  * expiration time and an iterator that spews the
285  * transport addresses.
286  *
287  * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
288  * will not be gossiped to other peers.
289  *
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
295  */
296 struct GNUNET_HELLO_Message *
297 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
298                      GNUNET_HELLO_GenerateAddressListCallback addrgen,
299                      void *addrgen_cls,
300                      int friend_only);
301
302
303 /**
304  * Return the size of the given HELLO message.
305  *
306  * @param hello to inspect
307  * @return the size, 0 if HELLO is invalid
308  */
309 uint16_t
310 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
311
312
313 /**
314  * Construct a HELLO message by merging the
315  * addresses in two existing HELLOs (which
316  * must be for the same peer).
317  *
318  * @param h1 first HELLO message
319  * @param h2 the second HELLO message
320  * @return the combined hello message
321  */
322 struct GNUNET_HELLO_Message *
323 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
324                     const struct GNUNET_HELLO_Message *h2);
325
326
327 /**
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
331  * is returned.
332  *
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
342  */
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);
347
348
349 /**
350  * Iterator callback to go over all addresses.
351  *
352  * @param cls closure
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)
358  */
359 typedef int
360 (*GNUNET_HELLO_AddressIterator) (void *cls,
361                                  const struct GNUNET_HELLO_Address *address,
362                                  struct GNUNET_TIME_Absolute expiration);
363
364
365 /**
366  * When does the last address in the given HELLO expire?
367  *
368  * @param msg HELLO to inspect
369  * @return time the last address expires, 0 if there are no addresses in the HELLO
370  */
371 struct GNUNET_TIME_Absolute
372 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
373
374
375 /**
376  * Iterate over all of the addresses in the HELLO.
377  *
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
386  */
387 struct GNUNET_HELLO_Message *
388 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
389                                 int return_modified,
390                                 GNUNET_HELLO_AddressIterator it, void *it_cls);
391
392
393 /**
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.
398  *
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
405  */
406 void
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,
411                                     void *it_cls);
412
413
414 /**
415  * Get the peer identity from a HELLO message.
416  *
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
420  */
421 int
422 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
423                      struct GNUNET_PeerIdentity *peer);
424
425
426 /**
427  * Get the header from a HELLO message, used so other code
428  * can correctly send HELLO messages.
429  *
430  * @param hello the hello message
431  *
432  * @return header or NULL if the HELLO was malformed
433  */
434 struct GNUNET_MessageHeader *
435 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
436
437
438 /**
439  * Helper function to load/access transport plugins.
440  * FIXME: pass closure!
441  *
442  * @param name name of the transport plugin to load
443  * @return NULL if a plugin with name @a name is not known/loadable
444  */
445 typedef struct GNUNET_TRANSPORT_PluginFunctions *
446 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
447
448
449 /**
450  * Compose a hello URI string from a hello message.
451  *
452  * @param hello Hello message
453  * @param plugins_find Function to find transport plugins by name
454  * @return Hello URI string
455  */
456 char *
457 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
458                           GNUNET_HELLO_TransportPluginsFind plugins_find);
459
460
461 /**
462  * Parse a hello URI string to a hello message.
463  *
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
469  */
470 int
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);
475
476
477
478 /* NG API */
479 #include "gnunet_nt_lib.h"
480
481
482 /**
483  * Build address record by signing raw information with private key.
484  *
485  * @param address text address to sign
486  * @param nt network type of @a address
487  * @param expiration how long is @a address valid
488  * @param private_key signing key to use
489  * @param result[out] where to write address record (allocated)
490  * @param result_size[out] set to size of @a result
491  */
492 void
493 GNUNET_HELLO_sign_address (const char *address,
494                            enum GNUNET_NetworkType nt,
495                            struct GNUNET_TIME_Absolute expiration,
496                            const struct GNUNET_CRYPTO_EddsaPrivateKey *private_key,
497                            void **result,
498                            size_t *result_size);
499
500
501 /**
502  * Check signature and extract address record.
503  *
504  * @param raw raw signed address
505  * @param raw_size size of @a raw
506  * @param pid public key to use for signature verification
507  * @param nt[out] set to network type
508  * @param expiration[out] how long is the address valid
509  * @return NULL on error, otherwise the address
510  */
511 char *
512 GNUNET_HELLO_extract_address (const void *raw,
513                               size_t raw_size,
514                               const struct GNUNET_PeerIdentity *pid,
515                               enum GNUNET_NetworkType *nt,
516                               struct GNUNET_TIME_Absolute *expiration);
517
518
519 /**
520  * Given an address as a string, extract the prefix that identifies
521  * the communicator offering transmissions to that address.
522  *
523  * @param address a peer's address
524  * @return NULL if the address is mal-formed, otherwise the prefix
525  */
526 char *
527 GNUNET_HELLO_address_to_prefix (const char *address);
528
529
530 #if 0                           /* keep Emacsens' auto-indent happy */
531 {
532 #endif
533 #ifdef __cplusplus
534 }
535 #endif
536
537 /* ifndef GNUNET_HELLO_LIB_H */
538 #endif
539
540 /** @} */  /* end of group */
541
542 /* end of gnunet_hello_lib.h */