a47162f998e6fa7e155dcf7e1a6469804516a4d9
[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
19 /**
20  * @author Christian Grothoff
21  * @file
22  * Helper library for handling HELLOs
23  *
24  * @defgroup hello  Hello library
25  * Helper library for handling HELLOs
26  *
27  * @see [Documentation](https://gnunet.org/gnunets-hostlist-subsystem)
28  *
29  * @{
30  */
31
32 #ifndef GNUNET_HELLO_LIB_H
33 #define GNUNET_HELLO_LIB_H
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #if 0                           /* keep Emacsens' auto-indent happy */
39 }
40 #endif
41 #endif
42
43 #include "gnunet_util_lib.h"
44
45 /**
46  * Prefix that every HELLO URI must start with.
47  */
48 #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
49
50 /**
51  * Prefix that every FRIEND HELLO URI must start with.
52  */
53 #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
54
55 /**
56  * Separator used in HELLO URI
57  */
58 #define GNUNET_HELLO_URI_SEP '+'
59
60
61 /**
62  * Additional local information about an address
63  *
64  * These information are only valid for the local peer and are not serialized
65  * when a #GNUNET_HELLO_Message is created
66  */
67 enum GNUNET_HELLO_AddressInfo
68 {
69   /**
70    * No additional information
71    */
72   GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
73
74   /**
75    * This is an inbound address and cannot be used to initiate an outbound
76    * connection to another peer
77    */
78   GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
79 };
80
81
82 /**
83  * An address for communicating with a peer.  We frequently
84  * need this tuple and the components cannot really be
85  * separated.  This is NOT the format that would be used
86  * on the wire.
87  */
88 struct GNUNET_HELLO_Address
89 {
90
91   /**
92    * For which peer is this an address?
93    */
94   struct GNUNET_PeerIdentity peer;
95
96   /**
97    * Name of the transport plugin enabling the communication using
98    * this address.
99    */
100   const char *transport_name;
101
102   /**
103    * Binary representation of the address (plugin-specific).
104    */
105   const void *address;
106
107   /**
108    * Number of bytes in @e address.
109    */
110   size_t address_length;
111
112   /**
113    * Extended information about address
114    *
115    * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
116    * to indicate an address is inbound and cannot be used to initiate an
117    * outbound connection.
118    *
119    * These information are only valid for the local peer and are not serialized
120    * when a #GNUNET_HELLO_Message is created
121    */
122   enum GNUNET_HELLO_AddressInfo local_info;
123
124 };
125
126
127 /**
128  * Allocate an address struct.
129  *
130  * @param peer the peer
131  * @param transport_name plugin name
132  * @param address binary address
133  * @param address_length number of bytes in @a address
134  * @param local_info additional local information for the address
135  * @return the address struct
136  */
137 struct GNUNET_HELLO_Address *
138 GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
139                                const char *transport_name,
140                                const void *address,
141                                size_t address_length,
142                                enum GNUNET_HELLO_AddressInfo local_info);
143
144
145 /**
146  * Copy an address struct.
147  *
148  * @param address address to copy
149  * @return a copy of the address struct
150  */
151 struct GNUNET_HELLO_Address *
152 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
153
154
155 /**
156  * Compare two addresses.  Does NOT compare the peer identity,
157  * that is assumed already to match!
158  *
159  * @param a1 first address
160  * @param a2 second address
161  * @return 0 if the addresses are equal, -1 if @a a1< @a a2, 1 if @a a1> @a a2.
162  */
163 int
164 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
165                           const struct GNUNET_HELLO_Address *a2);
166
167
168 /**
169  * Get the size of an address struct.
170  *
171  * @param address address
172  * @return the size
173  */
174 size_t
175 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
176
177
178 /**
179  * Check if an address has a local option set
180  *
181  * @param address the address to check
182  * @param option the respective option to check for
183  * @return #GNUNET_YES or #GNUNET_NO
184  */
185 int
186 GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
187                                    enum GNUNET_HELLO_AddressInfo option);
188
189
190 /**
191  * Free an address.
192  *
193  * @param addr address to free
194  */
195 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
196
197
198 GNUNET_NETWORK_STRUCT_BEGIN
199
200 /**
201  * A HELLO message is used to exchange information about
202  * transports with other peers.  This struct is always
203  * followed by the actual network addresses which have
204  * the format:
205  *
206  * 1) transport-name (0-terminated)
207  * 2) address-length (uint16_t, network byte order; possibly
208  *    unaligned!)
209  * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
210  *    unaligned!)
211  * 4) address (address-length bytes; possibly unaligned!)
212  */
213 struct GNUNET_HELLO_Message
214 {
215   /**
216    * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
217    */
218   struct GNUNET_MessageHeader header;
219
220   /**
221    * Use in F2F mode: Do not gossip this HELLO message
222    */
223   uint32_t friend_only GNUNET_PACKED;
224
225   /**
226    * The public key of the peer.
227    */
228   struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
229
230 };
231 GNUNET_NETWORK_STRUCT_END
232
233
234
235 /**
236  * Return HELLO type
237  *
238  * @param h HELLO Message to test
239  * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
240  */
241 int
242 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
243
244
245 /**
246  * Copy the given address information into
247  * the given buffer using the format of HELLOs.
248  *
249  * @param address address to add
250  * @param expiration expiration for the address
251  * @param target where to copy the address
252  * @param max maximum number of bytes to copy to @a target
253  * @return number of bytes copied, 0 if
254  *         the target buffer was not big enough.
255  */
256 size_t
257 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
258                           struct GNUNET_TIME_Absolute expiration,
259                           char *target,
260                           size_t max);
261
262
263 /**
264  * Callback function used to fill a buffer of max bytes with a list of
265  * addresses in the format used by HELLOs.  Should use
266  * #GNUNET_HELLO_add_address() as a helper function.
267  *
268  * @param cls closure
269  * @param max maximum number of bytes that can be written to @a buf
270  * @param buf where to write the address information
271  * @return number of bytes written or 0, #GNUNET_SYSERR to signal the
272  *         end of the iteration.
273  */
274 typedef ssize_t
275 (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
276                                              size_t max,
277                                              void *buf);
278
279
280 /**
281  * Construct a HELLO message given the public key,
282  * expiration time and an iterator that spews the
283  * transport addresses.
284  *
285  * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
286  * will not be gossiped to other peers.
287  *
288  * @param public_key public key to include in the HELLO
289  * @param addrgen callback to invoke to get addresses
290  * @param addrgen_cls closure for @a addrgen
291  * @param friend_only should the returned HELLO be only visible to friends?
292  * @return the hello message
293  */
294 struct GNUNET_HELLO_Message *
295 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
296                      GNUNET_HELLO_GenerateAddressListCallback addrgen,
297                      void *addrgen_cls,
298                      int friend_only);
299
300
301 /**
302  * Return the size of the given HELLO message.
303  *
304  * @param hello to inspect
305  * @return the size, 0 if HELLO is invalid
306  */
307 uint16_t
308 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
309
310
311 /**
312  * Construct a HELLO message by merging the
313  * addresses in two existing HELLOs (which
314  * must be for the same peer).
315  *
316  * @param h1 first HELLO message
317  * @param h2 the second HELLO message
318  * @return the combined hello message
319  */
320 struct GNUNET_HELLO_Message *
321 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
322                     const struct GNUNET_HELLO_Message *h2);
323
324
325 /**
326  * Test if two HELLO messages contain the same addresses.
327  * If they only differ in expiration time, the lowest
328  * expiration time larger than 'now' where they differ
329  * is returned.
330  *
331  * @param h1 first HELLO message
332  * @param h2 the second HELLO message
333  * @param now time to use for deciding which addresses have
334  *            expired and should not be considered at all
335  * @return absolute time forever if the two HELLOs are
336  *         totally identical; smallest timestamp >= now if
337  *         they only differ in timestamps;
338  *         zero if the some addresses with expirations >= now
339  *         do not match at all
340  */
341 struct GNUNET_TIME_Absolute
342 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
343                      const struct GNUNET_HELLO_Message *h2,
344                      struct GNUNET_TIME_Absolute now);
345
346
347 /**
348  * Iterator callback to go over all addresses.
349  *
350  * @param cls closure
351  * @param address the address
352  * @param expiration expiration time
353  * @return #GNUNET_OK to keep the address,
354  *         #GNUNET_NO to delete it from the HELLO
355  *         #GNUNET_SYSERR to stop iterating (but keep current address)
356  */
357 typedef int
358 (*GNUNET_HELLO_AddressIterator) (void *cls,
359                                  const struct GNUNET_HELLO_Address *address,
360                                  struct GNUNET_TIME_Absolute expiration);
361
362
363 /**
364  * When does the last address in the given HELLO expire?
365  *
366  * @param msg HELLO to inspect
367  * @return time the last address expires, 0 if there are no addresses in the HELLO
368  */
369 struct GNUNET_TIME_Absolute
370 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
371
372
373 /**
374  * Iterate over all of the addresses in the HELLO.
375  *
376  * @param msg HELLO to iterate over; client does not need to
377  *        have verified that msg is well-formed (beyond starting
378  *        with a GNUNET_MessageHeader of the right type).
379  * @param return_modified if a modified copy should be returned,
380  *         otherwise NULL will be returned
381  * @param it iterator to call on each address
382  * @param it_cls closure for @a it
383  * @return the modified HELLO or NULL
384  */
385 struct GNUNET_HELLO_Message *
386 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
387                                 int return_modified,
388                                 GNUNET_HELLO_AddressIterator it, void *it_cls);
389
390
391 /**
392  * Iterate over addresses in @a new_hello that are NOT already present
393  * in @a old_hello. Note that if the address is present in @a old_hello
394  * but the expiration time in @a new_hello is more recent, the
395  * iterator is also called.
396  *
397  * @param new_hello a HELLO message
398  * @param old_hello a HELLO message
399  * @param expiration_limit ignore addresses in old_hello
400  *        that expired before the given time stamp
401  * @param it iterator to call on each address
402  * @param it_cls closure for @a it
403  */
404 void
405 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
406                                     const struct GNUNET_HELLO_Message *old_hello,
407                                     struct GNUNET_TIME_Absolute expiration_limit,
408                                     GNUNET_HELLO_AddressIterator it,
409                                     void *it_cls);
410
411
412 /**
413  * Get the peer identity from a HELLO message.
414  *
415  * @param hello the hello message
416  * @param peer where to store the peer's identity
417  * @return #GNUNET_SYSERR if the HELLO was malformed
418  */
419 int
420 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
421                      struct GNUNET_PeerIdentity *peer);
422
423
424 /**
425  * Get the header from a HELLO message, used so other code
426  * can correctly send HELLO messages.
427  *
428  * @param hello the hello message
429  *
430  * @return header or NULL if the HELLO was malformed
431  */
432 struct GNUNET_MessageHeader *
433 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
434
435
436 /**
437  * Helper function to load/access transport plugins.
438  * FIXME: pass closure!
439  *
440  * @param name name of the transport plugin to load
441  * @return NULL if a plugin with name @a name is not known/loadable
442  */
443 typedef struct GNUNET_TRANSPORT_PluginFunctions *
444 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
445
446
447 /**
448  * Compose a hello URI string from a hello message.
449  *
450  * @param hello Hello message
451  * @param plugins_find Function to find transport plugins by name
452  * @return Hello URI string
453  */
454 char *
455 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
456                           GNUNET_HELLO_TransportPluginsFind plugins_find);
457
458
459 /**
460  * Parse a hello URI string to a hello message.
461  *
462  * @param uri URI string to parse
463  * @param pubkey Pointer to struct where public key is parsed
464  * @param hello Pointer to struct where hello message is parsed
465  * @param plugins_find Function to find transport plugins by name
466  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
467  */
468 int
469 GNUNET_HELLO_parse_uri (const char *uri,
470                         struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
471                         struct GNUNET_HELLO_Message **hello,
472                         GNUNET_HELLO_TransportPluginsFind plugins_find);
473
474
475
476 /* NG API */
477 #include "gnunet_nt_lib.h"
478
479 /**
480  * Key used for storing HELLOs in the peerstore
481  */
482 #define GNUNET_HELLO_PEERSTORE_KEY "hello"
483
484 /**
485  * Build address record by signing raw information with private key.
486  *
487  * @param address text address to sign
488  * @param nt network type of @a address
489  * @param expiration how long is @a address valid
490  * @param private_key signing key to use
491  * @param result[out] where to write address record (allocated)
492  * @param result_size[out] set to size of @a result
493  */
494 void
495 GNUNET_HELLO_sign_address (const char *address,
496                            enum GNUNET_NetworkType nt,
497                            struct GNUNET_TIME_Absolute expiration,
498                            const struct GNUNET_CRYPTO_EddsaPrivateKey *private_key,
499                            void **result,
500                            size_t *result_size);
501
502
503 /**
504  * Check signature and extract address record.
505  *
506  * @param raw raw signed address
507  * @param raw_size size of @a raw
508  * @param pid public key to use for signature verification
509  * @param nt[out] set to network type
510  * @param expiration[out] how long is the address valid
511  * @return NULL on error, otherwise the address
512  */
513 char *
514 GNUNET_HELLO_extract_address (const void *raw,
515                               size_t raw_size,
516                               const struct GNUNET_PeerIdentity *pid,
517                               enum GNUNET_NetworkType *nt,
518                               struct GNUNET_TIME_Absolute *expiration);
519
520
521
522 #if 0                           /* keep Emacsens' auto-indent happy */
523 {
524 #endif
525 #ifdef __cplusplus
526 }
527 #endif
528
529 /* ifndef GNUNET_HELLO_LIB_H */
530 #endif
531
532 /** @} */  /* end of group */
533
534 /* end of gnunet_hello_lib.h */