4e8554b52b42f7b0d0c182f6e263331f62c87fb8
[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,
87                                const void *address,
88                                size_t address_length);
89
90
91 /**
92  * Free an address.
93  * 
94  * @param addr address to free
95  */
96 #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
97
98
99 /**
100  * A HELLO message is used to exchange information about
101  * transports with other peers.  This struct is guaranteed
102  * to start with a "GNUNET_MessageHeader", everything else
103  * should be internal to the HELLO library.
104  */
105 struct GNUNET_HELLO_Message;
106
107
108 /**
109  * Copy the given address information into
110  * the given buffer using the format of HELLOs.
111  *
112  * @param tname name of the transport plugin
113  * @param expiration expiration for the address
114  * @param addr the address
115  * @param addr_len length of the address in bytes
116  * @param target where to copy the address
117  * @param max maximum number of bytes to copy to target
118  * @return number of bytes copied, 0 if
119  *         the target buffer was not big enough.
120  */
121 size_t
122 GNUNET_HELLO_add_address (const char *tname,
123                           struct GNUNET_TIME_Absolute expiration,
124                           const void *addr, uint16_t addr_len, char *target,
125                           size_t max);
126
127
128 /**
129  * Callback function used to fill a buffer of max bytes with a list of
130  * addresses in the format used by HELLOs.  Should use
131  * "GNUNET_HELLO_add_address" as a helper function.
132  *
133  * @param cls closure
134  * @param max maximum number of bytes that can be written to buf
135  * @param buf where to write the address information
136  * @return number of bytes written, 0 to signal the
137  *         end of the iteration.
138  */
139 typedef size_t (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
140                                                             size_t max,
141                                                             void *buf);
142
143
144 /**
145  * Construct a HELLO message given the public key,
146  * expiration time and an iterator that spews the
147  * transport addresses.
148  *
149  * @return the hello message
150  */
151 struct GNUNET_HELLO_Message *
152 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
153                      *publicKey,
154                      GNUNET_HELLO_GenerateAddressListCallback addrgen,
155                      void *addrgen_cls);
156
157
158 /**
159  * Return the size of the given HELLO message.
160  * @param hello to inspect
161  * @return the size, 0 if HELLO is invalid
162  */
163 uint16_t
164 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
165
166
167 /**
168  * Construct a HELLO message by merging the
169  * addresses in two existing HELLOs (which
170  * must be for the same peer).
171  *
172  * @param h1 first HELLO message
173  * @param h2 the second HELLO message
174  * @return the combined hello message
175  */
176 struct GNUNET_HELLO_Message *
177 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
178                     const struct GNUNET_HELLO_Message *h2);
179
180
181 /**
182  * Test if two HELLO messages contain the same addresses.
183  * If they only differ in expiration time, the lowest
184  * expiration time larger than 'now' where they differ
185  * is returned.
186  *
187  * @param h1 first HELLO message
188  * @param h2 the second HELLO message
189  * @param now time to use for deciding which addresses have
190  *            expired and should not be considered at all
191  * @return absolute time forever if the two HELLOs are
192  *         totally identical; smallest timestamp >= now if
193  *         they only differ in timestamps;
194  *         zero if the some addresses with expirations >= now
195  *         do not match at all
196  */
197 struct GNUNET_TIME_Absolute
198 GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
199                      const struct GNUNET_HELLO_Message *h2,
200                      struct GNUNET_TIME_Absolute now);
201
202
203 /**
204  * Iterator callback to go over all addresses.
205  *
206  * @param cls closure
207  * @param tname name of the transport
208  * @param expiration expiration time
209  * @param addr the address
210  * @param addrlen length of the address
211  * @return GNUNET_OK to keep the address,
212  *         GNUNET_NO to delete it from the HELLO
213  *         GNUNET_SYSERR to stop iterating (but keep current address)
214  */
215 typedef int (*GNUNET_HELLO_AddressIterator) (void *cls, const char *tname,
216                                              struct GNUNET_TIME_Absolute
217                                              expiration, const void *addr,
218                                              uint16_t addrlen);
219
220
221 /**
222  * When does the last address in the given HELLO expire?
223  *
224  * @param msg HELLO to inspect
225  * @return time the last address expires, 0 if there are no addresses in the HELLO
226  */
227 struct GNUNET_TIME_Absolute
228 GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
229
230
231 /**
232  * Iterate over all of the addresses in the HELLO.
233  *
234  * @param msg HELLO to iterate over; client does not need to
235  *        have verified that msg is well-formed (beyond starting
236  *        with a GNUNET_MessageHeader of the right type).
237  * @param return_modified if a modified copy should be returned,
238  *         otherwise NULL will be returned
239  * @param it iterator to call on each address
240  * @param it_cls closure for it
241  * @return the modified HELLO or NULL
242  */
243 struct GNUNET_HELLO_Message *
244 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
245                                 int return_modified,
246                                 GNUNET_HELLO_AddressIterator it, void *it_cls);
247
248
249 /**
250  * Iterate over addresses in "new_hello" that
251  * are NOT already present in "old_hello".
252  *
253  * @param new_hello a HELLO message
254  * @param old_hello a HELLO message
255  * @param expiration_limit ignore addresses in old_hello
256  *        that expired before the given time stamp
257  * @param it iterator to call on each address
258  * @param it_cls closure for it
259  */
260 void
261 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
262                                     *new_hello,
263                                     const struct GNUNET_HELLO_Message
264                                     *old_hello,
265                                     struct GNUNET_TIME_Absolute
266                                     expiration_limit,
267                                     GNUNET_HELLO_AddressIterator it,
268                                     void *it_cls);
269
270
271 /**
272  * Get the public key from a HELLO message.
273  *
274  * @param hello the hello message
275  * @param publicKey where to copy the public key information, can be NULL
276  * @return GNUNET_SYSERR if the HELLO was malformed
277  */
278 int
279 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
280                       struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
281                       *publicKey);
282
283
284 /**
285  * Get the peer identity from a HELLO message.
286  *
287  * @param hello the hello message
288  * @param peer where to store the peer's identity
289  * @return GNUNET_SYSERR if the HELLO was malformed
290  */
291 int
292 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
293                      struct GNUNET_PeerIdentity *peer);
294
295
296 /**
297  * Get the header from a HELLO message, used so other code
298  * can correctly send HELLO messages.
299  *
300  * @param hello the hello message
301  *
302  * @return header or NULL if the HELLO was malformed
303  */
304 struct GNUNET_MessageHeader *
305 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
306
307 /* ifndef GNUNET_HELLO_LIB_H */
308 #endif
309 /* end of gnunet_hello_lib.h */