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