implementing 0003268
[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    * Extended information about address
89    *
90    * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
91    * to indicate an address is inbound and cannot be used to initiate an
92    * outbound connection.
93    *
94    * These information are only valid for the local peer and are not serialized
95    * when a #GNUNET_HELLO_Message is created
96    */
97   enum GNUNET_HELLO_AddressInfo local_info;
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 'address'.
112    */
113   size_t address_length;
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 'address'
125  * @param local_info additional address information, will not get serialized
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, const void *address,
131                                size_t address_length,
132                                enum GNUNET_HELLO_AddressInfo local_info);
133
134
135 /**
136  * Copy an address struct.
137  *
138  * @param address address to copy
139  * @return a copy of the address struct
140  */
141 struct GNUNET_HELLO_Address *
142 GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
143
144
145 /**
146  * Compare two addresses.  Does NOT compare the peer identity,
147  * that is assumed already to match!
148  *
149  * @param a1 first address
150  * @param a2 second address
151  * @return 0 if the addresses are equal, -1 if a1<a2, 1 if a1>a2.
152  */
153 int
154 GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
155                           const struct GNUNET_HELLO_Address *a2);
156
157 /**
158  * Get the size of an address struct.
159  *
160  * @param address address
161  * @return the size
162  */
163 size_t
164 GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
165
166
167 /**
168  * Free an address.
169  *
170  * @param addr address to free
171  */
172 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
173
174
175 /**
176  * A HELLO message is used to exchange information about
177  * transports with other peers.  This struct is guaranteed
178  * to start with a "GNUNET_MessageHeader", everything else
179  * should be internal to the HELLO library.
180  */
181 struct GNUNET_HELLO_Message;
182
183
184 /** Return HELLO type
185  *
186  * @param h HELLO Message to test
187  * @return GNUNET_YES or GNUNET_NO
188  */
189 int
190 GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
191
192
193 /**
194  * Copy the given address information into
195  * the given buffer using the format of HELLOs.
196  *
197  * @param address address to add
198  * @param expiration expiration for the address
199  * @param target where to copy the address
200  * @param max maximum number of bytes to copy to target
201  * @return number of bytes copied, 0 if
202  *         the target buffer was not big enough.
203  */
204 size_t
205 GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
206                           struct GNUNET_TIME_Absolute expiration, char *target,
207                           size_t max);
208
209
210 /**
211  * Callback function used to fill a buffer of max bytes with a list of
212  * addresses in the format used by HELLOs.  Should use
213  * "GNUNET_HELLO_add_address" as a helper function.
214  *
215  * @param cls closure
216  * @param max maximum number of bytes that can be written to buf
217  * @param buf where to write the address information
218  * @return number of bytes written, 0 to signal the
219  *         end of the iteration.
220  */
221 typedef size_t (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
222                                                             size_t max,
223                                                             void *buf);
224
225
226 /**
227  * Construct a HELLO message given the public key,
228  * expiration time and an iterator that spews the
229  * transport addresses.
230  *
231  * If friend only is set to GNUNET_YES we create a FRIEND_HELLO which will
232  * not be gossiped to other peers
233  *
234  * @return the hello message
235  */
236 struct GNUNET_HELLO_Message *
237 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *publicKey,
238                      GNUNET_HELLO_GenerateAddressListCallback addrgen,
239                      void *addrgen_cls,
240                      int friend_only);
241
242
243 /**
244  * Return the size of the given HELLO message.
245  * @param hello to inspect
246  * @return the size, 0 if HELLO is invalid
247  */
248 uint16_t
249 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
250
251
252 /**
253  * Construct a HELLO message by merging the
254  * addresses in two existing HELLOs (which
255  * must be for the same peer).
256  *
257  * @param h1 first HELLO message
258  * @param h2 the second HELLO message
259  * @return the combined hello message
260  */
261 struct GNUNET_HELLO_Message *
262 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
263                     const struct GNUNET_HELLO_Message *h2);
264
265
266 /**
267  * Test if two HELLO messages contain the same addresses.
268  * If they only differ in expiration time, the lowest
269  * expiration time larger than 'now' where they differ
270  * is returned.
271  *
272  * @param h1 first HELLO message
273  * @param h2 the second HELLO message
274  * @param now time to use for deciding which addresses have
275  *            expired and should not be considered at all
276  * @return absolute time forever if the two HELLOs are
277  *         totally identical; smallest timestamp >= now if
278  *         they only differ in timestamps;
279  *         zero if the some addresses with expirations >= now
280  *         do not match at all
281  */
282 struct GNUNET_TIME_Absolute
283 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
284                      const struct GNUNET_HELLO_Message *h2,
285                      struct GNUNET_TIME_Absolute now);
286
287
288 /**
289  * Iterator callback to go over all addresses.
290  *
291  * @param cls closure
292  * @param address the address
293  * @param expiration expiration time
294  * @return GNUNET_OK to keep the address,
295  *         GNUNET_NO to delete it from the HELLO
296  *         GNUNET_SYSERR to stop iterating (but keep current address)
297  */
298 typedef int (*GNUNET_HELLO_AddressIterator) (void *cls,
299                                              const struct GNUNET_HELLO_Address *
300                                              address,
301                                              struct GNUNET_TIME_Absolute
302                                              expiration);
303
304
305 /**
306  * When does the last address in the given HELLO expire?
307  *
308  * @param msg HELLO to inspect
309  * @return time the last address expires, 0 if there are no addresses in the HELLO
310  */
311 struct GNUNET_TIME_Absolute
312 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
313
314
315 /**
316  * Iterate over all of the addresses in the HELLO.
317  *
318  * @param msg HELLO to iterate over; client does not need to
319  *        have verified that msg is well-formed (beyond starting
320  *        with a GNUNET_MessageHeader of the right type).
321  * @param return_modified if a modified copy should be returned,
322  *         otherwise NULL will be returned
323  * @param it iterator to call on each address
324  * @param it_cls closure for it
325  * @return the modified HELLO or NULL
326  */
327 struct GNUNET_HELLO_Message *
328 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
329                                 int return_modified,
330                                 GNUNET_HELLO_AddressIterator it, void *it_cls);
331
332
333 /**
334  * Iterate over addresses in "new_hello" that
335  * are NOT already present in "old_hello".
336  *
337  * @param new_hello a HELLO message
338  * @param old_hello a HELLO message
339  * @param expiration_limit ignore addresses in old_hello
340  *        that expired before the given time stamp
341  * @param it iterator to call on each address
342  * @param it_cls closure for it
343  */
344 void
345 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
346                                     *new_hello,
347                                     const struct GNUNET_HELLO_Message
348                                     *old_hello,
349                                     struct GNUNET_TIME_Absolute
350                                     expiration_limit,
351                                     GNUNET_HELLO_AddressIterator it,
352                                     void *it_cls);
353
354
355 /**
356  * Get the public key from a HELLO message.
357  *
358  * @param hello the hello message
359  * @param publicKey where to copy the public key information, can be NULL
360  * @return GNUNET_SYSERR if the HELLO was malformed
361  */
362 int
363 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
364                       struct GNUNET_CRYPTO_EddsaPublicKey *publicKey);
365
366
367 /**
368  * Get the peer identity from a HELLO message.
369  *
370  * @param hello the hello message
371  * @param peer where to store the peer's identity
372  * @return GNUNET_SYSERR if the HELLO was malformed
373  */
374 int
375 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
376                      struct GNUNET_PeerIdentity *peer);
377
378
379 /**
380  * Get the header from a HELLO message, used so other code
381  * can correctly send HELLO messages.
382  *
383  * @param hello the hello message
384  *
385  * @return header or NULL if the HELLO was malformed
386  */
387 struct GNUNET_MessageHeader *
388 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
389
390
391 typedef struct GNUNET_TRANSPORT_PluginFunctions *
392 (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
393
394
395 /**
396  * Compose a hello URI string from a hello message.
397  *
398  * @param hello Hello message
399  * @param plugins_find Function to find transport plugins by name
400  * @return Hello URI string
401  */
402 char *
403 GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
404                           GNUNET_HELLO_TransportPluginsFind plugins_find);
405
406 /**
407  * Parse a hello URI string to a hello message.
408  *
409  * @param uri URI string to parse
410  * @param pubkey Pointer to struct where public key is parsed
411  * @param hello Pointer to struct where hello message is parsed
412  * @param plugins_find Function to find transport plugins by name
413  * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
414  */
415 int
416 GNUNET_HELLO_parse_uri (const char *uri,
417                         struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
418                         struct GNUNET_HELLO_Message **hello,
419                         GNUNET_HELLO_TransportPluginsFind plugins_find);
420
421 #if 0                           /* keep Emacsens' auto-indent happy */
422 {
423 #endif
424 #ifdef __cplusplus
425 }
426 #endif
427
428
429 /* ifndef GNUNET_HELLO_LIB_H */
430 #endif
431 /* end of gnunet_hello_lib.h */