2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @brief helper library for handling HELLOs
24 * @author Christian Grothoff
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_server_lib.h"
32 * A HELLO message is used to exchange information about
33 * transports with other peers. This struct is always
34 * followed by the actual network addresses which have
37 * 1) transport-name (0-terminated)
38 * 2) address-length (uint32_t, network byte order; possibly
40 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
42 * 4) address (address-length bytes; possibly unaligned!)
44 struct GNUNET_HELLO_Message
47 * Type will be GNUNET_MESSAGE_TYPE_HELLO.
49 struct GNUNET_MessageHeader header;
52 * Always zero (for alignment).
54 uint32_t reserved GNUNET_PACKED;
57 * The public key of the peer.
59 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
65 * Copy the given address information into
66 * the given buffer using the format of HELLOs.
68 * @param tname name of the transport plugin
69 * @param expiration expiration for the address
70 * @param addr the address
71 * @param addr_len length of the address in bytes
72 * @param target where to copy the address
73 * @param max maximum number of bytes to copy to target
74 * @return number of bytes copied, 0 if
75 * the target buffer was not big enough.
78 GNUNET_HELLO_add_address (const char *tname,
79 struct GNUNET_TIME_Absolute expiration,
81 size_t addr_len, char *target, size_t max)
85 struct GNUNET_TIME_AbsoluteNBO exp;
87 slen = strlen (tname) + 1;
88 if (slen + sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
91 exp = GNUNET_TIME_absolute_hton (expiration);
92 alen = htonl ((uint32_t) addr_len);
93 memcpy (target, tname, slen);
94 memcpy (&target[slen], &alen, sizeof (uint32_t));
95 slen += sizeof (uint32_t);
96 memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
97 slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
98 memcpy (&target[slen], addr, addr_len);
105 * Get the size of an address entry in a HELLO message.
107 * @param buf pointer to the start of the address entry
108 * @param max maximum size of the entry (end of buf)
109 * @param ralen set to the address length
110 * @return size of the entry, or 0 if max is not large enough
113 get_hello_address_size (const char *buf, size_t max, uint32_t * ralen)
123 while ((left > 0) && ('\0' != *pos))
131 /* 0-termination not found */
136 if (left < sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO))
138 /* not enough space for addrlen */
142 memcpy (&alen, pos, sizeof (uint32_t));
145 slen += alen + sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
148 /* not enough space for addr */
157 * Construct a HELLO message given the public key,
158 * expiration time and an iterator that spews the
159 * transport addresses.
161 * @return the hello message
163 struct GNUNET_HELLO_Message *
164 GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
166 GNUNET_HELLO_GenerateAddressListCallback addrgen,
169 char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 256 -
170 sizeof (struct GNUNET_HELLO_Message)];
174 struct GNUNET_HELLO_Message *hello;
176 max = sizeof (buffer);
178 while (0 != (ret = addrgen (addrgen_cls, max, &buffer[used])))
183 hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
184 hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
185 hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
186 memcpy (&hello->publicKey, publicKey,
187 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
188 memcpy (&hello[1], buffer, used);
194 * Iterate over all of the addresses in the HELLO.
196 * @param msg HELLO to iterate over
197 * @param return_modified if a modified copy should be returned,
198 * otherwise NULL will be returned
199 * @param it iterator to call on each address
200 * @param it_cls closure for it
202 struct GNUNET_HELLO_Message *
203 GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
205 GNUNET_HELLO_AddressIterator it, void *it_cls)
208 struct GNUNET_HELLO_Message *ret;
215 struct GNUNET_TIME_AbsoluteNBO expire;
218 msize = GNUNET_HELLO_size (msg);
219 if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
220 (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
225 ret = GNUNET_malloc (msize);
226 memcpy (ret, msg, msize);
228 inptr = (const char *) &msg[1];
229 insize = msize - sizeof (struct GNUNET_HELLO_Message);
231 woff = (ret != NULL) ? (char *) &ret[1] : NULL;
234 esize = get_hello_address_size (inptr, insize, &alen);
238 GNUNET_free_non_null (ret);
242 &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
243 sizeof (struct GNUNET_TIME_AbsoluteNBO));
246 GNUNET_TIME_absolute_ntoh (expire),
247 &inptr[esize - alen], alen);
248 if (iret == GNUNET_SYSERR)
252 ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
255 if ((iret == GNUNET_OK) && (ret != NULL))
257 memcpy (woff, inptr, esize);
265 ret->header.size = ntohs (sizeof (struct GNUNET_HELLO_Message) + wpos);
275 struct GNUNET_TIME_Absolute expiration;
280 get_match_exp (void *cls,
282 struct GNUNET_TIME_Absolute expiration,
283 const void *addr, size_t addrlen)
285 struct ExpireContext *ec = cls;
287 if ((addrlen == ec->addrlen) && (0 == memcmp (addr, ec->addr, addrlen)))
289 ec->found = GNUNET_YES;
290 ec->expiration = expiration;
291 return GNUNET_SYSERR; /* done here */
299 const struct GNUNET_HELLO_Message *h1;
300 const struct GNUNET_HELLO_Message *h2;
301 const struct GNUNET_HELLO_Message *other;
311 copy_latest (void *cls,
313 struct GNUNET_TIME_Absolute expiration,
314 const void *addr, size_t addrlen)
316 struct MergeContext *mc = cls;
317 struct ExpireContext ec;
320 ec.addrlen = addrlen;
321 ec.found = GNUNET_NO;
322 GNUNET_HELLO_iterate_addresses (mc->other, GNUNET_NO, &get_match_exp, &ec);
323 if ((ec.found == GNUNET_NO) ||
324 ((ec.expiration.value < expiration.value) ||
325 ((ec.expiration.value == expiration.value) &&
326 (mc->take_equal == GNUNET_YES))))
327 mc->ret += GNUNET_HELLO_add_address (tname,
338 merge_addr (void *cls, size_t max, void *buf)
340 struct MergeContext *mc = cls;
347 mc->take_equal = GNUNET_NO;
349 GNUNET_HELLO_iterate_addresses (mc->h1, GNUNET_NO, ©_latest, mc);
350 mc->take_equal = GNUNET_YES;
352 GNUNET_HELLO_iterate_addresses (mc->h2, GNUNET_NO, ©_latest, mc);
359 * Construct a HELLO message by merging the
360 * addresses in two existing HELLOs (which
361 * must be for the same peer).
363 * @param h1 first HELLO message
364 * @param h2 the second HELLO message
365 * @return the combined hello message
367 struct GNUNET_HELLO_Message *
368 GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
369 const struct GNUNET_HELLO_Message *h2)
371 struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
373 return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
379 struct GNUNET_TIME_Absolute expiration_limit;
381 GNUNET_HELLO_AddressIterator it;
385 const struct GNUNET_HELLO_Message *old_hello;
390 delta_match (void *cls,
392 struct GNUNET_TIME_Absolute expiration,
393 const void *addr, size_t addrlen)
395 struct DeltaContext *dc = cls;
397 struct ExpireContext ec;
400 ec.addrlen = addrlen;
401 ec.found = GNUNET_NO;
402 GNUNET_HELLO_iterate_addresses (dc->old_hello,
403 GNUNET_NO, &get_match_exp, &ec);
404 if ((ec.found == GNUNET_YES) &&
405 ((ec.expiration.value > expiration.value) ||
406 (ec.expiration.value >= dc->expiration_limit.value)))
407 return GNUNET_YES; /* skip */
408 ret = dc->it (dc->it_cls, tname, expiration, addr, addrlen);
414 * Iterate over addresses in "new_hello" that
415 * are NOT already present in "old_hello".
417 * @param new_hello a HELLO message
418 * @param old_hello a HELLO message
419 * @param expiration_limit ignore addresses in old_hello
420 * that expired before the given time stamp
421 * @param it iterator to call on each address
422 * @param it_cls closure for it
425 GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
427 const struct GNUNET_HELLO_Message
429 struct GNUNET_TIME_Absolute
431 GNUNET_HELLO_AddressIterator it,
434 struct DeltaContext dc;
436 dc.expiration_limit = expiration_limit;
439 dc.old_hello = old_hello;
440 GNUNET_HELLO_iterate_addresses (new_hello, GNUNET_NO, &delta_match, &dc);
445 * Return the size of the given HELLO message.
446 * @param hello to inspect
447 * @return the size, 0 if HELLO is invalid
450 GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
452 uint16_t ret = ntohs (hello->header.size);
453 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
454 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
461 * Get the public key from a HELLO message.
463 * @param hello the hello message
464 * @param publicKey where to copy the public key information, can be NULL
465 * @return GNUNET_SYSERR if the HELLO was malformed
468 GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
469 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
472 uint16_t ret = ntohs (hello->header.size);
473 if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
474 (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
475 return GNUNET_SYSERR;
476 *publicKey = hello->publicKey;