gnunet_hello_lib.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2003, 2004, 2005, 2006, 2010 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_hello_lib.h
  19. * @brief helper library for handling HELLOs
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_HELLO_LIB_H
  23. #define GNUNET_HELLO_LIB_H
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #if 0 /* keep Emacsens' auto-indent happy */
  28. }
  29. #endif
  30. #endif
  31. #include "gnunet_common.h"
  32. #include "gnunet_crypto_lib.h"
  33. /**
  34. * A HELLO message is used to exchange information about
  35. * transports with other peers. This struct is guaranteed
  36. * to start with a "GNUNET_MessageHeader", everything else
  37. * should be internal to the HELLO library.
  38. */
  39. struct GNUNET_HELLO_Message;
  40. /**
  41. * Copy the given address information into
  42. * the given buffer using the format of HELLOs.
  43. *
  44. * @param tname name of the transport plugin
  45. * @param expiration expiration for the address
  46. * @param addr the address
  47. * @param addr_len length of the address in bytes
  48. * @param target where to copy the address
  49. * @param max maximum number of bytes to copy to target
  50. * @return number of bytes copied, 0 if
  51. * the target buffer was not big enough.
  52. */
  53. size_t
  54. GNUNET_HELLO_add_address (const char *tname,
  55. struct GNUNET_TIME_Absolute expiration,
  56. const void *addr, uint16_t addr_len, char *target,
  57. size_t max);
  58. /**
  59. * Callback function used to fill a buffer of max bytes with a list of
  60. * addresses in the format used by HELLOs. Should use
  61. * "GNUNET_HELLO_add_address" as a helper function.
  62. *
  63. * @param cls closure
  64. * @param max maximum number of bytes that can be written to buf
  65. * @param buf where to write the address information
  66. * @return number of bytes written, 0 to signal the
  67. * end of the iteration.
  68. */
  69. typedef size_t (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
  70. size_t max,
  71. void *buf);
  72. /**
  73. * Construct a HELLO message given the public key,
  74. * expiration time and an iterator that spews the
  75. * transport addresses.
  76. *
  77. * @return the hello message
  78. */
  79. struct GNUNET_HELLO_Message *
  80. GNUNET_HELLO_create (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
  81. *publicKey,
  82. GNUNET_HELLO_GenerateAddressListCallback addrgen,
  83. void *addrgen_cls);
  84. /**
  85. * Return the size of the given HELLO message.
  86. * @param hello to inspect
  87. * @return the size, 0 if HELLO is invalid
  88. */
  89. uint16_t
  90. GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
  91. /**
  92. * Construct a HELLO message by merging the
  93. * addresses in two existing HELLOs (which
  94. * must be for the same peer).
  95. *
  96. * @param h1 first HELLO message
  97. * @param h2 the second HELLO message
  98. * @return the combined hello message
  99. */
  100. struct GNUNET_HELLO_Message *
  101. GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
  102. const struct GNUNET_HELLO_Message *h2);
  103. /**
  104. * Test if two HELLO messages contain the same addresses.
  105. * If they only differ in expiration time, the lowest
  106. * expiration time larger than 'now' where they differ
  107. * is returned.
  108. *
  109. * @param h1 first HELLO message
  110. * @param h2 the second HELLO message
  111. * @param now time to use for deciding which addresses have
  112. * expired and should not be considered at all
  113. * @return absolute time forever if the two HELLOs are
  114. * totally identical; smallest timestamp >= now if
  115. * they only differ in timestamps;
  116. * zero if the some addresses with expirations >= now
  117. * do not match at all
  118. */
  119. struct GNUNET_TIME_Absolute
  120. GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
  121. const struct GNUNET_HELLO_Message *h2,
  122. struct GNUNET_TIME_Absolute now);
  123. /**
  124. * Iterator callback to go over all addresses.
  125. *
  126. * @param cls closure
  127. * @param tname name of the transport
  128. * @param expiration expiration time
  129. * @param addr the address
  130. * @param addrlen length of the address
  131. * @return GNUNET_OK to keep the address,
  132. * GNUNET_NO to delete it from the HELLO
  133. * GNUNET_SYSERR to stop iterating (but keep current address)
  134. */
  135. typedef int (*GNUNET_HELLO_AddressIterator) (void *cls, const char *tname,
  136. struct GNUNET_TIME_Absolute
  137. expiration, const void *addr,
  138. uint16_t addrlen);
  139. /**
  140. * Iterate over all of the addresses in the HELLO.
  141. *
  142. * @param msg HELLO to iterate over; client does not need to
  143. * have verified that msg is well-formed (beyond starting
  144. * with a GNUNET_MessageHeader of the right type).
  145. * @param return_modified if a modified copy should be returned,
  146. * otherwise NULL will be returned
  147. * @param it iterator to call on each address
  148. * @param it_cls closure for it
  149. * @return the modified HELLO or NULL
  150. */
  151. struct GNUNET_HELLO_Message *
  152. GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
  153. int return_modified,
  154. GNUNET_HELLO_AddressIterator it, void *it_cls);
  155. /**
  156. * Iterate over addresses in "new_hello" that
  157. * are NOT already present in "old_hello".
  158. *
  159. * @param new_hello a HELLO message
  160. * @param old_hello a HELLO message
  161. * @param expiration_limit ignore addresses in old_hello
  162. * that expired before the given time stamp
  163. * @param it iterator to call on each address
  164. * @param it_cls closure for it
  165. */
  166. void
  167. GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message
  168. *new_hello,
  169. const struct GNUNET_HELLO_Message
  170. *old_hello,
  171. struct GNUNET_TIME_Absolute
  172. expiration_limit,
  173. GNUNET_HELLO_AddressIterator it,
  174. void *it_cls);
  175. /**
  176. * Get the public key from a HELLO message.
  177. *
  178. * @param hello the hello message
  179. * @param publicKey where to copy the public key information, can be NULL
  180. * @return GNUNET_SYSERR if the HELLO was malformed
  181. */
  182. int
  183. GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
  184. struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
  185. *publicKey);
  186. /**
  187. * Get the peer identity from a HELLO message.
  188. *
  189. * @param hello the hello message
  190. * @param peer where to store the peer's identity
  191. * @return GNUNET_SYSERR if the HELLO was malformed
  192. */
  193. int
  194. GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
  195. struct GNUNET_PeerIdentity *peer);
  196. /**
  197. * Get the header from a HELLO message, used so other code
  198. * can correctly send HELLO messages.
  199. *
  200. * @param hello the hello message
  201. *
  202. * @return header or NULL if the HELLO was malformed
  203. */
  204. struct GNUNET_MessageHeader *
  205. GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
  206. /* ifndef GNUNET_HELLO_LIB_H */
  207. #endif
  208. /* end of gnunet_hello_lib.h */