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