gnunet_hello_lib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2010, 2011 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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. * @file
  20. * Helper library for handling HELLOs
  21. *
  22. * @defgroup hello Hello library
  23. * Helper library for handling HELLOs
  24. * @{
  25. */
  26. #ifndef GNUNET_HELLO_LIB_H
  27. #define GNUNET_HELLO_LIB_H
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #if 0 /* keep Emacsens' auto-indent happy */
  32. }
  33. #endif
  34. #endif
  35. #include "gnunet_util_lib.h"
  36. /**
  37. * Prefix that every HELLO URI must start with.
  38. */
  39. #define GNUNET_HELLO_URI_PREFIX "gnunet://hello/"
  40. /**
  41. * Prefix that every FRIEND HELLO URI must start with.
  42. */
  43. #define GNUNET_FRIEND_HELLO_URI_PREFIX "gnunet://friend-hello/"
  44. /**
  45. * Separator used in HELLO URI
  46. */
  47. #define GNUNET_HELLO_URI_SEP '+'
  48. /**
  49. * Additional local information about an address
  50. *
  51. * These information are only valid for the local peer and are not serialized
  52. * when a #GNUNET_HELLO_Message is created
  53. */
  54. enum GNUNET_HELLO_AddressInfo
  55. {
  56. /**
  57. * No additional information
  58. */
  59. GNUNET_HELLO_ADDRESS_INFO_NONE = 0,
  60. /**
  61. * This is an inbound address and cannot be used to initiate an outbound
  62. * connection to another peer
  63. */
  64. GNUNET_HELLO_ADDRESS_INFO_INBOUND = 1
  65. };
  66. /**
  67. * An address for communicating with a peer. We frequently
  68. * need this tuple and the components cannot really be
  69. * separated. This is NOT the format that would be used
  70. * on the wire.
  71. */
  72. struct GNUNET_HELLO_Address
  73. {
  74. /**
  75. * For which peer is this an address?
  76. */
  77. struct GNUNET_PeerIdentity peer;
  78. /**
  79. * Name of the transport plugin enabling the communication using
  80. * this address.
  81. */
  82. const char *transport_name;
  83. /**
  84. * Binary representation of the address (plugin-specific).
  85. */
  86. const void *address;
  87. /**
  88. * Number of bytes in @e address.
  89. */
  90. size_t address_length;
  91. /**
  92. * Extended information about address
  93. *
  94. * This field contains additional #GNUNET_HELLO_AddressInfo flags e.g.
  95. * to indicate an address is inbound and cannot be used to initiate an
  96. * outbound connection.
  97. *
  98. * These information are only valid for the local peer and are not serialized
  99. * when a #GNUNET_HELLO_Message is created
  100. */
  101. enum GNUNET_HELLO_AddressInfo local_info;
  102. };
  103. /**
  104. * Allocate an address struct.
  105. *
  106. * @param peer the peer
  107. * @param transport_name plugin name
  108. * @param address binary address
  109. * @param address_length number of bytes in @a address
  110. * @param local_info additional local information for the address
  111. * @return the address struct
  112. */
  113. struct GNUNET_HELLO_Address *
  114. GNUNET_HELLO_address_allocate (const struct GNUNET_PeerIdentity *peer,
  115. const char *transport_name,
  116. const void *address,
  117. size_t address_length,
  118. enum GNUNET_HELLO_AddressInfo local_info);
  119. /**
  120. * Copy an address struct.
  121. *
  122. * @param address address to copy
  123. * @return a copy of the address struct
  124. */
  125. struct GNUNET_HELLO_Address *
  126. GNUNET_HELLO_address_copy (const struct GNUNET_HELLO_Address *address);
  127. /**
  128. * Compare two addresses. Does NOT compare the peer identity,
  129. * that is assumed already to match!
  130. *
  131. * @param a1 first address
  132. * @param a2 second address
  133. * @return 0 if the addresses are equal, -1 if @a a1< @a a2, 1 if @a a1> @a a2.
  134. */
  135. int
  136. GNUNET_HELLO_address_cmp (const struct GNUNET_HELLO_Address *a1,
  137. const struct GNUNET_HELLO_Address *a2);
  138. /**
  139. * Get the size of an address struct.
  140. *
  141. * @param address address
  142. * @return the size
  143. */
  144. size_t
  145. GNUNET_HELLO_address_get_size (const struct GNUNET_HELLO_Address *address);
  146. /**
  147. * Check if an address has a local option set
  148. *
  149. * @param address the address to check
  150. * @param option the respective option to check for
  151. * @return #GNUNET_YES or #GNUNET_NO
  152. */
  153. int
  154. GNUNET_HELLO_address_check_option (const struct GNUNET_HELLO_Address *address,
  155. enum GNUNET_HELLO_AddressInfo option);
  156. /**
  157. * Free an address.
  158. *
  159. * @param addr address to free
  160. */
  161. #define GNUNET_HELLO_address_free(addr) GNUNET_free(addr)
  162. /**
  163. * A HELLO message is used to exchange information about
  164. * transports with other peers. This struct is guaranteed
  165. * to start with a `struct GNUNET_MessageHeader`, everything else
  166. * should be internal to the HELLO library.
  167. */
  168. struct GNUNET_HELLO_Message;
  169. /**
  170. * Return HELLO type
  171. *
  172. * @param h HELLO Message to test
  173. * @return #GNUNET_YES for friend-only or #GNUNET_NO otherwise
  174. */
  175. int
  176. GNUNET_HELLO_is_friend_only (const struct GNUNET_HELLO_Message *h);
  177. /**
  178. * Copy the given address information into
  179. * the given buffer using the format of HELLOs.
  180. *
  181. * @param address address to add
  182. * @param expiration expiration for the address
  183. * @param target where to copy the address
  184. * @param max maximum number of bytes to copy to @a target
  185. * @return number of bytes copied, 0 if
  186. * the target buffer was not big enough.
  187. */
  188. size_t
  189. GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
  190. struct GNUNET_TIME_Absolute expiration,
  191. char *target,
  192. size_t max);
  193. /**
  194. * Callback function used to fill a buffer of max bytes with a list of
  195. * addresses in the format used by HELLOs. Should use
  196. * #GNUNET_HELLO_add_address() as a helper function.
  197. *
  198. * @param cls closure
  199. * @param max maximum number of bytes that can be written to @a buf
  200. * @param buf where to write the address information
  201. * @return number of bytes written or 0, #GNUNET_SYSERR to signal the
  202. * end of the iteration.
  203. */
  204. typedef ssize_t
  205. (*GNUNET_HELLO_GenerateAddressListCallback) (void *cls,
  206. size_t max,
  207. void *buf);
  208. /**
  209. * Construct a HELLO message given the public key,
  210. * expiration time and an iterator that spews the
  211. * transport addresses.
  212. *
  213. * If friend only is set to #GNUNET_YES we create a FRIEND_HELLO which
  214. * will not be gossiped to other peers.
  215. *
  216. * @param public_key public key to include in the HELLO
  217. * @param addrgen callback to invoke to get addresses
  218. * @param addrgen_cls closure for @a addrgen
  219. * @param friend_only should the returned HELLO be only visible to friends?
  220. * @return the hello message
  221. */
  222. struct GNUNET_HELLO_Message *
  223. GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
  224. GNUNET_HELLO_GenerateAddressListCallback addrgen,
  225. void *addrgen_cls,
  226. int friend_only);
  227. /**
  228. * Return the size of the given HELLO message.
  229. *
  230. * @param hello to inspect
  231. * @return the size, 0 if HELLO is invalid
  232. */
  233. uint16_t
  234. GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello);
  235. /**
  236. * Construct a HELLO message by merging the
  237. * addresses in two existing HELLOs (which
  238. * must be for the same peer).
  239. *
  240. * @param h1 first HELLO message
  241. * @param h2 the second HELLO message
  242. * @return the combined hello message
  243. */
  244. struct GNUNET_HELLO_Message *
  245. GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
  246. const struct GNUNET_HELLO_Message *h2);
  247. /**
  248. * Test if two HELLO messages contain the same addresses.
  249. * If they only differ in expiration time, the lowest
  250. * expiration time larger than 'now' where they differ
  251. * is returned.
  252. *
  253. * @param h1 first HELLO message
  254. * @param h2 the second HELLO message
  255. * @param now time to use for deciding which addresses have
  256. * expired and should not be considered at all
  257. * @return absolute time forever if the two HELLOs are
  258. * totally identical; smallest timestamp >= now if
  259. * they only differ in timestamps;
  260. * zero if the some addresses with expirations >= now
  261. * do not match at all
  262. */
  263. struct GNUNET_TIME_Absolute
  264. GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
  265. const struct GNUNET_HELLO_Message *h2,
  266. struct GNUNET_TIME_Absolute now);
  267. /**
  268. * Iterator callback to go over all addresses.
  269. *
  270. * @param cls closure
  271. * @param address the address
  272. * @param expiration expiration time
  273. * @return #GNUNET_OK to keep the address,
  274. * #GNUNET_NO to delete it from the HELLO
  275. * #GNUNET_SYSERR to stop iterating (but keep current address)
  276. */
  277. typedef int
  278. (*GNUNET_HELLO_AddressIterator) (void *cls,
  279. const struct GNUNET_HELLO_Address *address,
  280. struct GNUNET_TIME_Absolute expiration);
  281. /**
  282. * When does the last address in the given HELLO expire?
  283. *
  284. * @param msg HELLO to inspect
  285. * @return time the last address expires, 0 if there are no addresses in the HELLO
  286. */
  287. struct GNUNET_TIME_Absolute
  288. GNUNET_HELLO_get_last_expiration (const struct GNUNET_HELLO_Message *msg);
  289. /**
  290. * Iterate over all of the addresses in the HELLO.
  291. *
  292. * @param msg HELLO to iterate over; client does not need to
  293. * have verified that msg is well-formed (beyond starting
  294. * with a GNUNET_MessageHeader of the right type).
  295. * @param return_modified if a modified copy should be returned,
  296. * otherwise NULL will be returned
  297. * @param it iterator to call on each address
  298. * @param it_cls closure for @a it
  299. * @return the modified HELLO or NULL
  300. */
  301. struct GNUNET_HELLO_Message *
  302. GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
  303. int return_modified,
  304. GNUNET_HELLO_AddressIterator it, void *it_cls);
  305. /**
  306. * Iterate over addresses in @a new_hello that are NOT already present
  307. * in @a old_hello. Note that if the address is present in @a old_hello
  308. * but the expiration time in @a new_hello is more recent, the
  309. * iterator is also called.
  310. *
  311. * @param new_hello a HELLO message
  312. * @param old_hello a HELLO message
  313. * @param expiration_limit ignore addresses in old_hello
  314. * that expired before the given time stamp
  315. * @param it iterator to call on each address
  316. * @param it_cls closure for @a it
  317. */
  318. void
  319. GNUNET_HELLO_iterate_new_addresses (const struct GNUNET_HELLO_Message *new_hello,
  320. const struct GNUNET_HELLO_Message *old_hello,
  321. struct GNUNET_TIME_Absolute expiration_limit,
  322. GNUNET_HELLO_AddressIterator it,
  323. void *it_cls);
  324. /**
  325. * Get the peer identity from a HELLO message.
  326. *
  327. * @param hello the hello message
  328. * @param peer where to store the peer's identity
  329. * @return #GNUNET_SYSERR if the HELLO was malformed
  330. */
  331. int
  332. GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
  333. struct GNUNET_PeerIdentity *peer);
  334. /**
  335. * Get the header from a HELLO message, used so other code
  336. * can correctly send HELLO messages.
  337. *
  338. * @param hello the hello message
  339. *
  340. * @return header or NULL if the HELLO was malformed
  341. */
  342. struct GNUNET_MessageHeader *
  343. GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello);
  344. /**
  345. * Helper function to load/access transport plugins.
  346. * FIXME: pass closure!
  347. *
  348. * @param name name of the transport plugin to load
  349. * @return NULL if a plugin with name @a name is not known/loadable
  350. */
  351. typedef struct GNUNET_TRANSPORT_PluginFunctions *
  352. (*GNUNET_HELLO_TransportPluginsFind) (const char *name);
  353. /**
  354. * Compose a hello URI string from a hello message.
  355. *
  356. * @param hello Hello message
  357. * @param plugins_find Function to find transport plugins by name
  358. * @return Hello URI string
  359. */
  360. char *
  361. GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
  362. GNUNET_HELLO_TransportPluginsFind plugins_find);
  363. /**
  364. * Parse a hello URI string to a hello message.
  365. *
  366. * @param uri URI string to parse
  367. * @param pubkey Pointer to struct where public key is parsed
  368. * @param hello Pointer to struct where hello message is parsed
  369. * @param plugins_find Function to find transport plugins by name
  370. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the URI was invalid, #GNUNET_NO on other errors
  371. */
  372. int
  373. GNUNET_HELLO_parse_uri (const char *uri,
  374. struct GNUNET_CRYPTO_EddsaPublicKey *pubkey,
  375. struct GNUNET_HELLO_Message **hello,
  376. GNUNET_HELLO_TransportPluginsFind plugins_find);
  377. #if 0 /* keep Emacsens' auto-indent happy */
  378. {
  379. #endif
  380. #ifdef __cplusplus
  381. }
  382. #endif
  383. /* ifndef GNUNET_HELLO_LIB_H */
  384. #endif
  385. /** @} */ /* end of group */
  386. /* end of gnunet_hello_lib.h */