gnunet_hello_lib.h 15 KB

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