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