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