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