verb
[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 2, 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 GNUNET_HELLO_add_address (const char *tname,
65                           struct GNUNET_TIME_Absolute expiration,
66                           const void *addr,
67                           size_t addr_len, char *target, size_t max);
68
69
70 /**
71  * Callback function used to fill a buffer of max bytes with a list of
72  * addresses in the format used by HELLOs.  Should use
73  * "GNUNET_HELLO_add_address" as a helper function.
74  *
75  * @param cls closure
76  * @param max maximum number of bytes that can be written to buf
77  * @param buf where to write the address information
78  * @return number of bytes written, 0 to signal the
79  *         end of the iteration.
80  */
81 typedef size_t
82   (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
83                                                size_t max, void *buf);
84
85
86 /**
87  * Construct a HELLO message given the public key,
88  * expiration time and an iterator that spews the
89  * transport addresses.
90  *
91  * @return the hello message
92  */
93 struct GNUNET_HELLO_Message *GNUNET_HELLO_create (const struct
94                                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
95                                                   *publicKey,
96                                                   GNUNET_HELLO_GenerateAddressListCallback
97                                                   addrgen, void *addrgen_cls);
98
99
100 /**
101  * Return the size of the given HELLO message.
102  * @param hello to inspect
103  * @return the size, 0 if HELLO is invalid
104  */
105 uint16_t GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
106
107
108 /**
109  * Construct a HELLO message by merging the
110  * addresses in two existing HELLOs (which
111  * must be for the same peer).
112  *
113  * @param h1 first HELLO message
114  * @param h2 the second HELLO message
115  * @return the combined hello message
116  */
117 struct GNUNET_HELLO_Message *GNUNET_HELLO_merge (const struct
118                                                  GNUNET_HELLO_Message *h1,
119                                                  const struct
120                                                  GNUNET_HELLO_Message *h2);
121
122
123
124 /**
125  * Iterator callback to go over all addresses.
126  *
127  * @param cls closure
128  * @param tname name of the transport
129  * @param expiration expiration time
130  * @param addr the address
131  * @param addrlen length of the address
132  * @return GNUNET_OK to keep the address,
133  *         GNUNET_NO to delete it from the HELLO
134  *         GNUNET_SYSERR to stop iterating (but keep current address)
135  */
136 typedef int
137   (*GNUNET_HELLO_AddressIterator) (void *cls,
138                                    const char *tname,
139                                    struct GNUNET_TIME_Absolute expiration,
140                                    const void *addr, size_t addrlen);
141
142
143 /**
144  * Iterate over all of the addresses in the HELLO.
145  *
146  * @param msg HELLO to iterate over; client does not need to
147  *        have verified that msg is well-formed (beyond starting
148  *        with a GNUNET_MessageHeader of the right type).
149  * @param return_modified if a modified copy should be returned,
150  *         otherwise NULL will be returned
151  * @param it iterator to call on each address
152  * @param it_cls closure for it
153  */
154 struct GNUNET_HELLO_Message *GNUNET_HELLO_iterate_addresses (const struct
155                                                              GNUNET_HELLO_Message
156                                                              *msg,
157                                                              int
158                                                              return_modified,
159                                                              GNUNET_HELLO_AddressIterator
160                                                              it,
161                                                              void *it_cls);
162
163
164 /**
165  * Iterate over addresses in "new_hello" that
166  * are NOT already present in "old_hello".
167  *
168  * @param new_hello a HELLO message
169  * @param old_hello a HELLO message
170  * @param expiration_limit ignore addresses in old_hello
171  *        that expired before the given time stamp
172  * @param it iterator to call on each address
173  * @param it_cls closure for it
174  */
175 void
176 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
177                                     *new_hello,
178                                     const struct GNUNET_HELLO_Message
179                                     *old_hello,
180                                     struct GNUNET_TIME_Absolute
181                                     expiration_limit,
182                                     GNUNET_HELLO_AddressIterator it,
183                                     void *it_cls);
184
185
186 /**
187  * Get the public key from a HELLO message.
188  *
189  * @param hello the hello message
190  * @param publicKey where to copy the public key information, can be NULL
191  * @return GNUNET_SYSERR if the HELLO was malformed
192  */
193 int
194 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
195                       struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
196                       *publicKey);
197
198
199 /**
200  * Get the peer identity from a HELLO message.
201  *
202  * @param hello the hello message
203  * @param peer where to store the peer's identity
204  * @return GNUNET_SYSERR if the HELLO was malformed
205  */
206 int
207 GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
208                      struct GNUNET_PeerIdentity *peer);
209
210
211 /**
212  * Get the header from a HELLO message, used so other code
213  * can correctly send HELLO messages.
214  *
215  * @param hello the hello message
216  *
217  * @return header or NULL if the HELLO was malformed
218  */
219 struct GNUNET_MessageHeader *
220 GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
221
222 /* ifndef GNUNET_HELLO_LIB_H */
223 #endif
224 /* end of gnunet_hello_lib.h */