gnunet_messenger_service.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2020 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 Tobias Frisch
  18. *
  19. * @file
  20. * MESSENGER service; manages decentralized chat groups
  21. *
  22. * @defgroup messenger MESSENGER service
  23. * Instant messaging based on the CADET subsystem
  24. *
  25. * @{
  26. */
  27. #ifndef GNUNET_MESSENGER_SERVICE_H
  28. #define GNUNET_MESSENGER_SERVICE_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #if 0 /* keep Emacsens' auto-indent happy */
  32. }
  33. #endif
  34. #endif
  35. #include "platform.h"
  36. #include "gnunet_configuration_lib.h"
  37. #include "gnunet_crypto_lib.h"
  38. #include "gnunet_identity_service.h"
  39. #include "gnunet_mq_lib.h"
  40. #include "gnunet_protocols.h"
  41. #include "gnunet_scheduler_lib.h"
  42. #include "gnunet_time_lib.h"
  43. #define GNUNET_MESSENGER_SERVICE_NAME "messenger"
  44. /**
  45. * Opaque handle to the messenger
  46. */
  47. struct GNUNET_MESSENGER_Handle;
  48. /**
  49. * Opaque handle to a room
  50. */
  51. struct GNUNET_MESSENGER_Room;
  52. /**
  53. * Opaque handle to a contact
  54. */
  55. struct GNUNET_MESSENGER_Contact;
  56. /**
  57. * Enum for the different supported kinds of messages
  58. */
  59. enum GNUNET_MESSENGER_MessageKind
  60. {
  61. /**
  62. * The info kind. The message contains a #GNUNET_MESSENGER_MessageInfo body.
  63. */
  64. GNUNET_MESSENGER_KIND_INFO = 1,
  65. /**
  66. * The join kind. The message contains a #GNUNET_MESSENGER_MessageJoin body.
  67. */
  68. GNUNET_MESSENGER_KIND_JOIN = 2,
  69. /**
  70. * The leave kind. The message contains a #GNUNET_MESSENGER_MessageLeave body.
  71. */
  72. GNUNET_MESSENGER_KIND_LEAVE = 3,
  73. /**
  74. * The name kind. The message contains a #GNUNET_MESSENGER_MessageName body.
  75. */
  76. GNUNET_MESSENGER_KIND_NAME = 4,
  77. /**
  78. * The key kind. The message contains a #GNUNET_MESSENGER_MessageKey body.
  79. */
  80. GNUNET_MESSENGER_KIND_KEY = 5,
  81. /**
  82. * The peer kind. The message contains a #GNUNET_MESSENGER_MessagePeer body.
  83. */
  84. GNUNET_MESSENGER_KIND_PEER = 6,
  85. /**
  86. * The id kind. The message contains a #GNUNET_MESSENGER_MessageId body.
  87. */
  88. GNUNET_MESSENGER_KIND_ID = 7,
  89. /**
  90. * The miss kind. The message contains a #GNUNET_MESSENGER_MessageMiss body.
  91. */
  92. GNUNET_MESSENGER_KIND_MISS = 8,
  93. /**
  94. * The merge kind. The message contains a #GNUNET_MESSENGER_MessageMerge body.
  95. */
  96. GNUNET_MESSENGER_KIND_MERGE = 9,
  97. /**
  98. * The request kind. The message contains a #GNUNET_MESSENGER_MessageRequest body.
  99. */
  100. GNUNET_MESSENGER_KIND_REQUEST = 10,
  101. /**
  102. * The invite kind. The message contains a #GNUNET_MESSENGER_MessageInvite body.
  103. */
  104. GNUNET_MESSENGER_KIND_INVITE = 11,
  105. /**
  106. * The text kind. The message contains a #GNUNET_MESSENGER_MessageText body.
  107. */
  108. GNUNET_MESSENGER_KIND_TEXT = 12,
  109. /**
  110. * The file kind. The message contains a #GNUNET_MESSENGER_MessageFile body.
  111. */
  112. GNUNET_MESSENGER_KIND_FILE = 13,
  113. /**
  114. * The private kind. The message contains a #GNUNET_MESSENGER_MessagePrivate body.
  115. */
  116. GNUNET_MESSENGER_KIND_PRIVATE = 14,
  117. /**
  118. * The unknown kind. The message contains an unknown body.
  119. */
  120. GNUNET_MESSENGER_KIND_UNKNOWN = 0
  121. };
  122. /**
  123. * Get the name of a message <i>kind</i>.
  124. *
  125. * @param kind Kind of a message
  126. * @return Name of that kind
  127. */
  128. const char*
  129. GNUNET_MESSENGER_name_of_kind (enum GNUNET_MESSENGER_MessageKind kind);
  130. /**
  131. * The header of a #GNUNET_MESSENGER_Message.
  132. */
  133. struct GNUNET_MESSENGER_MessageHeader
  134. {
  135. /**
  136. * The signature of the senders private key.
  137. */
  138. struct GNUNET_IDENTITY_Signature signature;
  139. /**
  140. * The timestamp of the message.
  141. */
  142. struct GNUNET_TIME_AbsoluteNBO timestamp;
  143. /**
  144. * The senders id inside of the room the message was sent in.
  145. */
  146. struct GNUNET_ShortHashCode sender_id;
  147. /**
  148. * The hash of the previous message from the senders perspective.
  149. */
  150. struct GNUNET_HashCode previous;
  151. /**
  152. * The kind of the message.
  153. */
  154. enum GNUNET_MESSENGER_MessageKind kind;
  155. };
  156. /**
  157. * An info message body.
  158. */
  159. struct GNUNET_MESSENGER_MessageInfo
  160. {
  161. /**
  162. * The senders key to verify its signatures.
  163. */
  164. struct GNUNET_IDENTITY_PublicKey host_key;
  165. /**
  166. * The new unique id for the receiver in a room.
  167. */
  168. struct GNUNET_ShortHashCode unique_id;
  169. };
  170. /**
  171. * A join message body.
  172. */
  173. struct GNUNET_MESSENGER_MessageJoin
  174. {
  175. /**
  176. * The senders public key to verify its signatures.
  177. */
  178. struct GNUNET_IDENTITY_PublicKey key;
  179. };
  180. /**
  181. * A leave message body.
  182. */
  183. struct GNUNET_MESSENGER_MessageLeave
  184. {
  185. };
  186. /**
  187. * A name message body.
  188. */
  189. struct GNUNET_MESSENGER_MessageName
  190. {
  191. /**
  192. * The new name which replaces the current senders name.
  193. */
  194. char *name;
  195. };
  196. /**
  197. * A key message body.
  198. */
  199. struct GNUNET_MESSENGER_MessageKey
  200. {
  201. /**
  202. * The new public key which replaces the current senders public key.
  203. */
  204. struct GNUNET_IDENTITY_PublicKey key;
  205. };
  206. /**
  207. * A peer message body.
  208. */
  209. struct GNUNET_MESSENGER_MessagePeer
  210. {
  211. /**
  212. * The peer identity of the sender opening a room.
  213. */
  214. struct GNUNET_PeerIdentity peer;
  215. };
  216. /**
  217. * An id message body.
  218. */
  219. struct GNUNET_MESSENGER_MessageId
  220. {
  221. /**
  222. * The new id which will replace the senders id in a room.
  223. */
  224. struct GNUNET_ShortHashCode id;
  225. };
  226. /**
  227. * A miss message body.
  228. */
  229. struct GNUNET_MESSENGER_MessageMiss
  230. {
  231. /**
  232. * The peer identity of a disconnected door to a room.
  233. */
  234. struct GNUNET_PeerIdentity peer;
  235. };
  236. /**
  237. * A merge message body.
  238. */
  239. struct GNUNET_MESSENGER_MessageMerge
  240. {
  241. /**
  242. * The hash of a second previous message.
  243. */
  244. struct GNUNET_HashCode previous;
  245. };
  246. /**
  247. * A request message body.
  248. */
  249. struct GNUNET_MESSENGER_MessageRequest
  250. {
  251. /**
  252. * The hash of the requested message.
  253. */
  254. struct GNUNET_HashCode hash;
  255. };
  256. /**
  257. * An invite message body.
  258. */
  259. struct GNUNET_MESSENGER_MessageInvite
  260. {
  261. /**
  262. * The peer identity of an open door to a room.
  263. */
  264. struct GNUNET_PeerIdentity door;
  265. /**
  266. * The hash identifying the port of the room.
  267. */
  268. struct GNUNET_HashCode key;
  269. };
  270. /**
  271. * A text message body.
  272. */
  273. struct GNUNET_MESSENGER_MessageText
  274. {
  275. /**
  276. * The containing text.
  277. */
  278. char *text;
  279. };
  280. /**
  281. * A file message body.
  282. */
  283. struct GNUNET_MESSENGER_MessageFile
  284. {
  285. /**
  286. * The symmetric key to decrypt the file.
  287. */
  288. struct GNUNET_CRYPTO_SymmetricSessionKey key;
  289. /**
  290. * The hash of the original file.
  291. */
  292. struct GNUNET_HashCode hash;
  293. /**
  294. * The name of the original file.
  295. */
  296. char name[NAME_MAX];
  297. /**
  298. * The uri of the encrypted file.
  299. */
  300. char *uri;
  301. };
  302. /**
  303. * A private message body.
  304. */
  305. struct GNUNET_MESSENGER_MessagePrivate
  306. {
  307. /**
  308. * The ECDH key to decrypt the message.
  309. */
  310. struct GNUNET_CRYPTO_EcdhePublicKey key;
  311. /**
  312. * The length of the encrypted message.
  313. */
  314. uint16_t length;
  315. /**
  316. * The data of the encrypted message.
  317. */
  318. char *data;
  319. };
  320. /**
  321. * The unified body of a #GNUNET_MESSENGER_Message.
  322. */
  323. struct GNUNET_MESSENGER_MessageBody
  324. {
  325. union
  326. {
  327. struct GNUNET_MESSENGER_MessageInfo info;
  328. struct GNUNET_MESSENGER_MessageJoin join;
  329. struct GNUNET_MESSENGER_MessageLeave leave;
  330. struct GNUNET_MESSENGER_MessageName name;
  331. struct GNUNET_MESSENGER_MessageKey key;
  332. struct GNUNET_MESSENGER_MessagePeer peer;
  333. struct GNUNET_MESSENGER_MessageId id;
  334. struct GNUNET_MESSENGER_MessageMiss miss;
  335. struct GNUNET_MESSENGER_MessageMerge merge;
  336. struct GNUNET_MESSENGER_MessageRequest request;
  337. struct GNUNET_MESSENGER_MessageInvite invite;
  338. struct GNUNET_MESSENGER_MessageText text;
  339. struct GNUNET_MESSENGER_MessageFile file;
  340. struct GNUNET_MESSENGER_MessagePrivate private;
  341. };
  342. };
  343. /**
  344. * Struct to a message
  345. */
  346. struct GNUNET_MESSENGER_Message
  347. {
  348. /**
  349. * Header.
  350. */
  351. struct GNUNET_MESSENGER_MessageHeader header;
  352. /**
  353. * Body
  354. */
  355. struct GNUNET_MESSENGER_MessageBody body;
  356. };
  357. /**
  358. * Method called whenever the EGO of a <i>handle</i> changes or if the first connection fails
  359. * to load a valid EGO and the anonymous key pair will be used instead.
  360. *
  361. * @param cls Closure from <i>GNUNET_MESSENGER_connect</i>
  362. * @param handle Messenger handle
  363. */
  364. typedef void
  365. (*GNUNET_MESSENGER_IdentityCallback) (void *cls, struct GNUNET_MESSENGER_Handle *handle);
  366. /**
  367. * Method called whenever a message is sent or received from a <i>room</i>.
  368. *
  369. * @param cls Closure from <i>GNUNET_MESSENGER_connect</i>
  370. * @param room Room handle
  371. * @param message Newly received or sent message
  372. * @param hash Hash identifying the message
  373. */
  374. typedef void
  375. (*GNUNET_MESSENGER_MessageCallback) (void *cls, const struct GNUNET_MESSENGER_Room *room,
  376. const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash);
  377. /**
  378. * Set up a handle for the messenger related functions and connects to all necessary services. It will look up the ego
  379. * key identified by its <i>name</i> and use it for signing all messages from the handle.
  380. *
  381. * @param cfg Configuration to use
  382. * @param name Name to look up an ego or NULL to stay anonymous
  383. * @param identity_callback Function called when the EGO of the handle changes
  384. * @param identity_cls Closure for the <i>identity_callback</i> handler
  385. * @param msg_callback Function called when a new message is sent or received
  386. * @param msg_cls Closure for the <i>msg_callback</i> handler
  387. * @return Messenger handle to use, NULL on error
  388. */
  389. struct GNUNET_MESSENGER_Handle*
  390. GNUNET_MESSENGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *name,
  391. GNUNET_MESSENGER_IdentityCallback identity_callback, void *identity_cls,
  392. GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls);
  393. /**
  394. * Update a handle of the messenger to use a different ego key and replace the old one with a newly generated one. All
  395. * participated rooms get informed about the key renewal. The handle requires a set name for this function to work and
  396. * it needs to be unused by other egos.
  397. *
  398. * Keep in mind that this will fully delete the old ego key (if any is used) even if any other service wants to use it
  399. * as default.
  400. *
  401. * @param handle Messenger handle to use
  402. * @return GNUNET_OK on success, GNUNET_SYSERR on failure
  403. */
  404. int
  405. GNUNET_MESSENGER_update (struct GNUNET_MESSENGER_Handle *handle);
  406. /**
  407. * Disconnect all of the messengers used services and clears up its used memory.
  408. *
  409. * @param handle Messenger handle to use
  410. */
  411. void
  412. GNUNET_MESSENGER_disconnect (struct GNUNET_MESSENGER_Handle *handle);
  413. /**
  414. * Get the name (if specified, otherwise NULL) used by the messenger.
  415. *
  416. * @param handle Messenger handle to use
  417. * @return Name used by the messenger or NULL
  418. */
  419. const char*
  420. GNUNET_MESSENGER_get_name (const struct GNUNET_MESSENGER_Handle *handle);
  421. /**
  422. * Set the name for the messenger. This will rename the currently used ego and move all stored files related to the current
  423. * name to its new directory. If anything fails during this process the function returns GNUNET_NO and the name for
  424. * the messenger won't change as specified.
  425. *
  426. * @param handle Messenger handle to use
  427. * @param name Name for the messenger to change to
  428. * @return GNUNET_YES on success, GNUNET_NO on failure and GNUNET_SYSERR if <i>handle</i> is NULL
  429. */
  430. int
  431. GNUNET_MESSENGER_set_name (struct GNUNET_MESSENGER_Handle *handle, const char *name);
  432. /**
  433. * Get the public key used by the messenger.
  434. *
  435. * @param handle Messenger handle to use
  436. * @return Used ego's public key
  437. */
  438. const struct GNUNET_IDENTITY_PublicKey*
  439. GNUNET_MESSENGER_get_key (const struct GNUNET_MESSENGER_Handle *handle);
  440. /**
  441. * Open a room to send and receive messages. The room will use the specified <i>key</i> as port for the underlying cadet
  442. * service. Opening a room results in opening the port for incoming connections as possible <b>door</b>.
  443. *
  444. * Notice that there can only be one room related to a specific <i>key</i>. So trying to open two rooms with the same
  445. * <i>key</i> will result in opening the room once but returning the handle both times because the room stays open.
  446. *
  447. * You can also open a room after entering it through a <b>door</b> using <i>GNUNET_MESSENGER_entry_room(...)</i>. This
  448. * will notify all entered <b>doors</b> to list you as new <b>door</b>.
  449. *
  450. * ( All <b>doors</b> form a ring structured network to shorten the latency sending and receiving messages. )
  451. *
  452. * @param handle Messenger handle to use
  453. * @param key Hash identifying the port
  454. * @return Room handle, NULL on error
  455. */
  456. struct GNUNET_MESSENGER_Room*
  457. GNUNET_MESSENGER_open_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key);
  458. /**
  459. * Enter a room to send and receive messages through a <b>door</b> opened using <i>GNUNET_MESSENGER_open_room(...)</i>.
  460. *
  461. * Notice that there can only be one room related to a specific <i>key</i>. So trying to enter two rooms with the same
  462. * <i>key</i> will result in entering the room once but returning the handle both times because the room stays entered.
  463. * You can however enter a room through multiple <b>doors</b> in parallel which results in connecting both ends. But
  464. * entering the room through the same <b>door</b> won't have any effect after the first time.
  465. *
  466. * You can also enter a room through a <b>door</b> after opening it using <i>GNUNET_MESSENGER_open_room(...)</i>. But the
  467. * <b>door</b> may not be your own peer identity.
  468. *
  469. * ( All <b>doors</b> form a ring structured network to shorten the latency sending and receiving messages. )
  470. *
  471. * @param handle Messenger handle to use
  472. * @param door Peer identity of an open <b>door</b>
  473. * @param key Hash identifying the port
  474. * @return Room handle, NULL on error
  475. */
  476. struct GNUNET_MESSENGER_Room*
  477. GNUNET_MESSENGER_entry_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_PeerIdentity *door,
  478. const struct GNUNET_HashCode *key);
  479. /**
  480. * Close a room which was entered, opened or both in various order and variety. Closing a room will destroy all connections
  481. * from your peer to another and the other way around.
  482. *
  483. * ( After a member closes a <b>door</b>, all members entered through that specific <b>door</b> have to use another one
  484. * or open the room on their own. )
  485. *
  486. * @param room Room handle
  487. */
  488. void
  489. GNUNET_MESSENGER_close_room (struct GNUNET_MESSENGER_Room *room);
  490. /**
  491. * Get the contact of a member in a <i>room</i> identified by their <i>id</i>.
  492. *
  493. * Notice that contacts are independent of rooms but will be removed if all rooms containing these contacts get closed.
  494. *
  495. * @param room Room handle
  496. * @param id Hash identifying a member
  497. * @return Contact handle, NULL if <i>id</i> is not in use
  498. */
  499. struct GNUNET_MESSENGER_Contact*
  500. GNUNET_MESSENGER_get_member (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_ShortHashCode *id);
  501. /**
  502. * Get the name used by the <i>contact</i>.
  503. *
  504. * @param contact Contact handle
  505. * @return Name of <i>contact</i> or NULL
  506. */
  507. const char*
  508. GNUNET_MESSENGER_contact_get_name (const struct GNUNET_MESSENGER_Contact *contact);
  509. /**
  510. * Get the public key used by the <i>contact</i>.
  511. *
  512. * @param contact Contact handle
  513. * @return Public key of the ego used by <i>contact</i>
  514. */
  515. const struct GNUNET_IDENTITY_PublicKey*
  516. GNUNET_MESSENGER_contact_get_key (const struct GNUNET_MESSENGER_Contact *contact);
  517. /**
  518. * Send a <i>message</i> into a </i>room</i>. If you opened the <i>room</i> all entered members will receive the
  519. * <i>message</i>. If you entered the <i>room</i> through a <b>door</b> all so entered <b>doors</b> will receive the
  520. * <i>message</i> as well. All members receiving the <i>message</i> will also propagate this <i>message</i> recursively
  521. * as long as the <i>message</i> is unknown to them.
  522. *
  523. * Notice that all messages sent and received are also stored and can be propagated to new members entering the room.
  524. *
  525. * @param room Room handle
  526. * @param message New message to send
  527. */
  528. void
  529. GNUNET_MESSENGER_send_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message);
  530. /**
  531. * Get the message in a <i>room</i> identified by its <i>hash</i>.
  532. *
  533. * @param room Room handle
  534. * @param hash Hash identifying a message
  535. * @return Message struct or NULL if no message with that hash is known
  536. */
  537. const struct GNUNET_MESSENGER_Message*
  538. GNUNET_MESSENGER_get_message (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash);
  539. #if 0 /* keep Emacsens' auto-indent happy */
  540. {
  541. #endif
  542. #ifdef __cplusplus
  543. }
  544. #endif
  545. #endif //GNUNET_MESSENGER_SERVICE_H
  546. /** @} *//* end of group */