indentation
[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 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  * A HELLO message is used to exchange information about
43  * transports with other peers.  This struct is guaranteed
44  * to start with a "GNUNET_MessageHeader", everything else
45  * should be internal to the HELLO library.
46  */
47 struct GNUNET_HELLO_Message;
48
49
50 /**
51  * Copy the given address information into
52  * the given buffer using the format of HELLOs.
53  *
54  * @param tname name of the transport plugin
55  * @param expiration expiration for the address
56  * @param addr the address
57  * @param addr_len length of the address in bytes
58  * @param target where to copy the address
59  * @param max maximum number of bytes to copy to target
60  * @return number of bytes copied, 0 if
61  *         the target buffer was not big enough.
62  */
63 size_t
64  
65  
66  
67  
68  
69  
70  
71 GNUNET_HELLO_add_address (const char *tname,
72                           struct GNUNET_TIME_Absolute expiration,
73                           const void *addr,
74                           uint16_t addr_len, char *target, size_t max);
75
76
77 /**
78  * Callback function used to fill a buffer of max bytes with a list of
79  * addresses in the format used by HELLOs.  Should use
80  * "GNUNET_HELLO_add_address" as a helper function.
81  *
82  * @param cls closure
83  * @param max maximum number of bytes that can be written to buf
84  * @param buf where to write the address information
85  * @return number of bytes written, 0 to signal the
86  *         end of the iteration.
87  */
88 typedef size_t
89     (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
90                                                  size_t max, void *buf);
91
92
93 /**
94  * Construct a HELLO message given the public key,
95  * expiration time and an iterator that spews the
96  * transport addresses.
97  *
98  * @return the hello message
99  */
100 struct GNUNET_HELLO_Message *GNUNET_HELLO_create (const struct
101                                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
102                                                   *publicKey,
103                                                   GNUNET_HELLO_GenerateAddressListCallback
104                                                   addrgen, void *addrgen_cls);
105
106
107 /**
108  * Return the size of the given HELLO message.
109  * @param hello to inspect
110  * @return the size, 0 if HELLO is invalid
111  */
112 uint16_t GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
113
114
115 /**
116  * Construct a HELLO message by merging the
117  * addresses in two existing HELLOs (which
118  * must be for the same peer).
119  *
120  * @param h1 first HELLO message
121  * @param h2 the second HELLO message
122  * @return the combined hello message
123  */
124 struct GNUNET_HELLO_Message *GNUNET_HELLO_merge (const struct
125                                                  GNUNET_HELLO_Message *h1,
126                                                  const struct
127                                                  GNUNET_HELLO_Message *h2);
128
129
130 /**
131  * Test if two HELLO messages contain the same addresses.
132  * If they only differ in expiration time, the lowest
133  * expiration time larger than 'now' where they differ
134  * is returned.
135  *
136  * @param h1 first HELLO message
137  * @param h2 the second HELLO message
138  * @param now time to use for deciding which addresses have
139  *            expired and should not be considered at all
140  * @return absolute time forever if the two HELLOs are 
141  *         totally identical; smallest timestamp >= now if
142  *         they only differ in timestamps; 
143  *         zero if the some addresses with expirations >= now
144  *         do not match at all
145  */
146 struct GNUNET_TIME_Absolute
147 GNUNET_HELLO_equals (const struct
148                      GNUNET_HELLO_Message *h1,
149                      const struct
150                      GNUNET_HELLO_Message *h2, struct GNUNET_TIME_Absolute now);
151
152
153 /**
154  * Iterator callback to go over all addresses.
155  *
156  * @param cls closure
157  * @param tname name of the transport
158  * @param expiration expiration time
159  * @param addr the address
160  * @param addrlen length of the address
161  * @return GNUNET_OK to keep the address,
162  *         GNUNET_NO to delete it from the HELLO
163  *         GNUNET_SYSERR to stop iterating (but keep current address)
164  */
165 typedef int
166     (*GNUNET_HELLO_AddressIterator) (void *cls,
167                                      const char *tname,
168                                      struct GNUNET_TIME_Absolute expiration,
169                                      const void *addr, uint16_t addrlen);
170
171
172 /**
173  * Iterate over all of the addresses in the HELLO.
174  *
175  * @param msg HELLO to iterate over; client does not need to
176  *        have verified that msg is well-formed (beyond starting
177  *        with a GNUNET_MessageHeader of the right type).
178  * @param return_modified if a modified copy should be returned,
179  *         otherwise NULL will be returned
180  * @param it iterator to call on each address
181  * @param it_cls closure for it
182  * @return the modified HELLO or NULL
183  */
184 struct GNUNET_HELLO_Message *GNUNET_HELLO_iterate_addresses (const struct
185                                                              GNUNET_HELLO_Message
186                                                              *msg,
187                                                              int
188                                                              return_modified,
189                                                              GNUNET_HELLO_AddressIterator
190                                                              it, void *it_cls);
191
192
193 /**
194  * Iterate over addresses in "new_hello" that
195  * are NOT already present in "old_hello".
196  *
197  * @param new_hello a HELLO message
198  * @param old_hello a HELLO message
199  * @param expiration_limit ignore addresses in old_hello
200  *        that expired before the given time stamp
201  * @param it iterator to call on each address
202  * @param it_cls closure for it
203  */
204 void
205 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
206                                     *new_hello,
207                                     const struct GNUNET_HELLO_Message
208                                     *old_hello,
209                                     struct GNUNET_TIME_Absolute
210                                     expiration_limit,
211                                     GNUNET_HELLO_AddressIterator it,
212                                     void *it_cls);
213
214
215 /**
216  * Get the public key from a HELLO message.
217  *
218  * @param hello the hello message
219  * @param publicKey where to copy the public key information, can be NULL
220  * @return GNUNET_SYSERR if the HELLO was malformed
221  */
222 int
223 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
224                       struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
225                       *publicKey);
226
227
228 /**
229  * Get the peer identity from a HELLO message.
230  *
231  * @param hello the hello message
232  * @param peer where to store the peer's identity
233  * @return GNUNET_SYSERR if the HELLO was malformed
234  */
235 int
236 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
237                      struct GNUNET_PeerIdentity *peer);
238
239
240 /**
241  * Get the header from a HELLO message, used so other code
242  * can correctly send HELLO messages.
243  *
244  * @param hello the hello message
245  *
246  * @return header or NULL if the HELLO was malformed
247  */
248 struct GNUNET_MessageHeader *GNUNET_HELLO_get_header (struct
249                                                       GNUNET_HELLO_Message
250                                                       *hello);
251
252 /* ifndef GNUNET_HELLO_LIB_H */
253 #endif
254 /* end of gnunet_hello_lib.h */