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