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