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