cadet_api.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011, 2017 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. * @file cadet/cadet_api.c
  18. * @brief cadet api: client implementation of cadet service
  19. * @author Bartlomiej Polot
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_cadet_service.h"
  26. #include "cadet.h"
  27. #include "cadet_protocol.h"
  28. #define LOG(kind, ...) GNUNET_log_from (kind, "cadet-api", __VA_ARGS__)
  29. /**
  30. * Opaque handle to the service.
  31. */
  32. struct GNUNET_CADET_Handle
  33. {
  34. /**
  35. * Message queue.
  36. */
  37. struct GNUNET_MQ_Handle *mq;
  38. /**
  39. * Ports open.
  40. */
  41. struct GNUNET_CONTAINER_MultiHashMap *ports;
  42. /**
  43. * Channels open.
  44. */
  45. struct GNUNET_CONTAINER_MultiHashMap32 *channels;
  46. /**
  47. * child of the next channel to create (to avoid reusing IDs often)
  48. */
  49. struct GNUNET_CADET_ClientChannelNumber next_ccn;
  50. /**
  51. * Configuration given by the client, in case of reconnection
  52. */
  53. const struct GNUNET_CONFIGURATION_Handle *cfg;
  54. /**
  55. * Task for trying to reconnect.
  56. */
  57. struct GNUNET_SCHEDULER_Task *reconnect_task;
  58. /**
  59. * Time to the next reconnect in case one reconnect fails
  60. */
  61. struct GNUNET_TIME_Relative reconnect_time;
  62. };
  63. /**
  64. * Opaque handle to a channel.
  65. */
  66. struct GNUNET_CADET_Channel
  67. {
  68. /**
  69. * Other end of the channel.
  70. */
  71. struct GNUNET_PeerIdentity peer;
  72. /**
  73. * Handle to the cadet this channel belongs to
  74. */
  75. struct GNUNET_CADET_Handle *cadet;
  76. /**
  77. * Channel's port, if incoming.
  78. */
  79. struct GNUNET_CADET_Port *incoming_port;
  80. /**
  81. * Any data the caller wants to put in here, used for the
  82. * various callbacks (@e disconnects, @e window_changes, handlers).
  83. */
  84. void *ctx;
  85. /**
  86. * Message Queue for the channel (which we are implementing).
  87. */
  88. struct GNUNET_MQ_Handle *mq;
  89. /**
  90. * Task to allow mq to send more traffic.
  91. */
  92. struct GNUNET_SCHEDULER_Task *mq_cont;
  93. /**
  94. * Pending envelope with a message to be transmitted to the
  95. * service as soon as we are allowed to. Should only be
  96. * non-NULL if @e allow_send is 0.
  97. */
  98. struct GNUNET_MQ_Envelope *pending_env;
  99. /**
  100. * Window change handler.
  101. */
  102. GNUNET_CADET_WindowSizeEventHandler window_changes;
  103. /**
  104. * Disconnect handler.
  105. */
  106. GNUNET_CADET_DisconnectEventHandler disconnects;
  107. /**
  108. * Local ID of the channel, #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI bit is set if outbound.
  109. */
  110. struct GNUNET_CADET_ClientChannelNumber ccn;
  111. /**
  112. * How many messages are we allowed to send to the service right now?
  113. */
  114. unsigned int allow_send;
  115. };
  116. /**
  117. * Opaque handle to a port.
  118. */
  119. struct GNUNET_CADET_Port
  120. {
  121. /**
  122. * Port "number"
  123. */
  124. struct GNUNET_HashCode id;
  125. /**
  126. * Handle to the CADET session this port belongs to.
  127. */
  128. struct GNUNET_CADET_Handle *cadet;
  129. /**
  130. * Closure for @a handler.
  131. */
  132. void *cls;
  133. /**
  134. * Handler for incoming channels on this port
  135. */
  136. GNUNET_CADET_ConnectEventHandler connects;
  137. /**
  138. * Closure for @ref connects
  139. */
  140. void *connects_cls;
  141. /**
  142. * Window size change handler.
  143. */
  144. GNUNET_CADET_WindowSizeEventHandler window_changes;
  145. /**
  146. * Handler called when an incoming channel is destroyed.
  147. */
  148. GNUNET_CADET_DisconnectEventHandler disconnects;
  149. /**
  150. * Payload handlers for incoming channels.
  151. */
  152. struct GNUNET_MQ_MessageHandler *handlers;
  153. };
  154. /**
  155. * Find the Port struct for a hash.
  156. *
  157. * @param h CADET handle.
  158. * @param hash HashCode for the port number.
  159. * @return The port handle if known, NULL otherwise.
  160. */
  161. static struct GNUNET_CADET_Port *
  162. find_port (const struct GNUNET_CADET_Handle *h,
  163. const struct GNUNET_HashCode *hash)
  164. {
  165. return GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
  166. }
  167. /**
  168. * Get the channel handler for the channel specified by id from the given handle
  169. *
  170. * @param h Cadet handle
  171. * @param ccn ID of the wanted channel
  172. * @return handle to the required channel or NULL if not found
  173. */
  174. static struct GNUNET_CADET_Channel *
  175. find_channel (struct GNUNET_CADET_Handle *h,
  176. struct GNUNET_CADET_ClientChannelNumber ccn)
  177. {
  178. return GNUNET_CONTAINER_multihashmap32_get (h->channels,
  179. ntohl (ccn.channel_of_client));
  180. }
  181. /**
  182. * Create a new channel and insert it in the channel list of the cadet handle
  183. *
  184. * @param h Cadet handle
  185. * @param ccnp pointer to desired ccn of the channel, NULL to assign one automatically.
  186. * @return Handle to the created channel.
  187. */
  188. static struct GNUNET_CADET_Channel *
  189. create_channel (struct GNUNET_CADET_Handle *h,
  190. const struct GNUNET_CADET_ClientChannelNumber *ccnp)
  191. {
  192. struct GNUNET_CADET_Channel *ch;
  193. struct GNUNET_CADET_ClientChannelNumber ccn;
  194. ch = GNUNET_new (struct GNUNET_CADET_Channel);
  195. ch->cadet = h;
  196. if (NULL == ccnp)
  197. {
  198. while (NULL != find_channel (h, h->next_ccn))
  199. h->next_ccn.channel_of_client =
  200. htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI
  201. | (1 + ntohl (h->next_ccn.channel_of_client)));
  202. ccn = h->next_ccn;
  203. }
  204. else
  205. {
  206. ccn = *ccnp;
  207. }
  208. ch->ccn = ccn;
  209. GNUNET_assert (GNUNET_OK ==
  210. GNUNET_CONTAINER_multihashmap32_put (
  211. h->channels,
  212. ntohl (ch->ccn.channel_of_client),
  213. ch,
  214. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  215. return ch;
  216. }
  217. /**
  218. * Destroy the specified channel.
  219. * - Destroys all peers, calling the disconnect callback on each if needed
  220. * - Cancels all outgoing traffic for that channel, calling respective notifys
  221. * - Calls cleaner if channel was inbound
  222. * - Frees all memory used
  223. *
  224. * @param ch Pointer to the channel.
  225. * @param call_cleaner Whether to call the cleaner handler.
  226. */
  227. static void
  228. destroy_channel (struct GNUNET_CADET_Channel *ch)
  229. {
  230. struct GNUNET_CADET_Handle *h = ch->cadet;
  231. LOG (GNUNET_ERROR_TYPE_DEBUG,
  232. "Destroying channel %X of %p\n",
  233. htonl (ch->ccn.channel_of_client),
  234. h);
  235. GNUNET_assert (
  236. GNUNET_YES ==
  237. GNUNET_CONTAINER_multihashmap32_remove (h->channels,
  238. ntohl (ch->ccn.channel_of_client),
  239. ch));
  240. if (NULL != ch->mq_cont)
  241. {
  242. GNUNET_SCHEDULER_cancel (ch->mq_cont);
  243. ch->mq_cont = NULL;
  244. }
  245. /* signal channel destruction */
  246. if (NULL != ch->disconnects)
  247. ch->disconnects (ch->ctx, ch);
  248. if (NULL != ch->pending_env)
  249. GNUNET_MQ_discard (ch->pending_env);
  250. GNUNET_MQ_destroy (ch->mq);
  251. GNUNET_free (ch);
  252. }
  253. /**
  254. * Reconnect to the service, retransmit all infomation to try to restore the
  255. * original state.
  256. *
  257. * @param h handle to the cadet
  258. */
  259. static void
  260. reconnect (struct GNUNET_CADET_Handle *h);
  261. /**
  262. * Function called during #reconnect_cbk() to (re)open
  263. * all ports that are still open.
  264. *
  265. * @param cls the `struct GNUNET_CADET_Handle`
  266. * @param id port ID
  267. * @param value a `struct GNUNET_CADET_Channel` to open
  268. * @return #GNUNET_OK (continue to iterate)
  269. */
  270. static int
  271. open_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value)
  272. {
  273. struct GNUNET_CADET_Handle *h = cls;
  274. struct GNUNET_CADET_Port *port = value;
  275. struct GNUNET_CADET_PortMessage *msg;
  276. struct GNUNET_MQ_Envelope *env;
  277. (void) id;
  278. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
  279. msg->port = port->id;
  280. GNUNET_MQ_send (h->mq, env);
  281. return GNUNET_OK;
  282. }
  283. /**
  284. * Reconnect callback: tries to reconnect again after a failer previous
  285. * reconnecttion
  286. *
  287. * @param cls closure (cadet handle)
  288. */
  289. static void
  290. reconnect_cbk (void *cls)
  291. {
  292. struct GNUNET_CADET_Handle *h = cls;
  293. h->reconnect_task = NULL;
  294. h->reconnect_time = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
  295. reconnect (h);
  296. GNUNET_CONTAINER_multihashmap_iterate (h->ports, &open_port_cb, h);
  297. }
  298. /**
  299. * Notify the application about a change in the window size (if needed).
  300. *
  301. * @param ch Channel to notify about.
  302. */
  303. static void
  304. notify_window_size (struct GNUNET_CADET_Channel *ch)
  305. {
  306. if (NULL != ch->window_changes)
  307. ch->window_changes (ch->ctx,
  308. ch, /* FIXME: remove 'ch'? */
  309. ch->allow_send);
  310. }
  311. /**
  312. * Transmit the next message from our queue.
  313. *
  314. * @param cls Closure (channel whose mq to activate).
  315. */
  316. static void
  317. cadet_mq_send_now (void *cls)
  318. {
  319. struct GNUNET_CADET_Channel *ch = cls;
  320. struct GNUNET_MQ_Envelope *env = ch->pending_env;
  321. ch->mq_cont = NULL;
  322. if (0 == ch->allow_send)
  323. {
  324. /* how did we get here? */
  325. GNUNET_break (0);
  326. return;
  327. }
  328. if (NULL == env)
  329. {
  330. /* how did we get here? */
  331. GNUNET_break (0);
  332. return;
  333. }
  334. ch->allow_send--;
  335. ch->pending_env = NULL;
  336. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  337. "Sending message on channel %s to CADET, new window size is %u\n",
  338. GNUNET_i2s (&ch->peer),
  339. ch->allow_send);
  340. GNUNET_MQ_send (ch->cadet->mq, env);
  341. GNUNET_MQ_impl_send_continue (ch->mq);
  342. }
  343. /**
  344. * Implement sending functionality of a message queue for
  345. * us sending messages to a peer.
  346. *
  347. * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
  348. * in order to label the message with the channel ID and send the
  349. * encapsulated message to the service.
  350. *
  351. * @param mq the message queue
  352. * @param msg the message to send
  353. * @param impl_state state of the implementation
  354. */
  355. static void
  356. cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
  357. const struct GNUNET_MessageHeader *msg,
  358. void *impl_state)
  359. {
  360. struct GNUNET_CADET_Channel *ch = impl_state;
  361. struct GNUNET_CADET_Handle *h = ch->cadet;
  362. uint16_t msize;
  363. struct GNUNET_MQ_Envelope *orig_env;
  364. struct GNUNET_MQ_Envelope *env;
  365. struct GNUNET_CADET_LocalData *cadet_msg;
  366. enum GNUNET_MQ_PriorityPreferences pp;
  367. if (NULL == h->mq)
  368. {
  369. /* We're currently reconnecting, pretend this worked */
  370. GNUNET_MQ_impl_send_continue (mq);
  371. return;
  372. }
  373. orig_env = GNUNET_MQ_get_current_envelope (mq);
  374. pp = GNUNET_MQ_env_get_options (orig_env);
  375. /* check message size for sanity */
  376. msize = ntohs (msg->size);
  377. if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
  378. {
  379. GNUNET_break (0);
  380. GNUNET_MQ_impl_send_continue (mq);
  381. return;
  382. }
  383. env = GNUNET_MQ_msg_nested_mh (cadet_msg,
  384. GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
  385. msg);
  386. cadet_msg->ccn = ch->ccn;
  387. cadet_msg->pp = htonl ((uint32_t) pp);
  388. GNUNET_assert (NULL == ch->pending_env);
  389. ch->pending_env = env;
  390. if (0 < ch->allow_send)
  391. ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now, ch);
  392. }
  393. /**
  394. * Handle destruction of a message queue. Implementations must not
  395. * free @a mq, but should take care of @a impl_state.
  396. *
  397. * @param mq the message queue to destroy
  398. * @param impl_state state of the implementation
  399. */
  400. static void
  401. cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
  402. {
  403. struct GNUNET_CADET_Channel *ch = impl_state;
  404. GNUNET_assert (mq == ch->mq);
  405. ch->mq = NULL;
  406. }
  407. /**
  408. * We had an error processing a message we forwarded from a peer to
  409. * the CADET service. We should just complain about it but otherwise
  410. * continue processing.
  411. *
  412. * @param cls closure with our `struct GNUNET_CADET_Channel`
  413. * @param error error code
  414. */
  415. static void
  416. cadet_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  417. {
  418. struct GNUNET_CADET_Channel *ch = cls;
  419. if (GNUNET_MQ_ERROR_NO_MATCH == error)
  420. {
  421. /* Got a message we did not understand, still try to continue! */
  422. GNUNET_break_op (0);
  423. GNUNET_CADET_receive_done (ch);
  424. }
  425. else
  426. {
  427. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  428. "MQ error in communication with CADET: %d\n",
  429. error);
  430. if (NULL != ch->disconnects)
  431. ch->disconnects (ch->ctx, ch);
  432. GNUNET_CADET_channel_destroy (ch);
  433. }
  434. }
  435. /**
  436. * Implementation function that cancels the currently sent message.
  437. * Should basically undo whatever #mq_send_impl() did.
  438. *
  439. * @param mq message queue
  440. * @param impl_state state specific to the implementation
  441. */
  442. static void
  443. cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
  444. {
  445. struct GNUNET_CADET_Channel *ch = impl_state;
  446. (void) mq;
  447. GNUNET_assert (NULL != ch->pending_env);
  448. GNUNET_MQ_discard (ch->pending_env);
  449. ch->pending_env = NULL;
  450. if (NULL != ch->mq_cont)
  451. {
  452. GNUNET_SCHEDULER_cancel (ch->mq_cont);
  453. ch->mq_cont = NULL;
  454. }
  455. }
  456. /**
  457. * Process the new channel notification and add it to the channels in the handle
  458. *
  459. * @param h The cadet handle
  460. * @param msg A message with the details of the new incoming channel
  461. */
  462. static void
  463. handle_channel_created (
  464. void *cls,
  465. const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
  466. {
  467. struct GNUNET_CADET_Handle *h = cls;
  468. struct GNUNET_CADET_Channel *ch;
  469. struct GNUNET_CADET_Port *port;
  470. const struct GNUNET_HashCode *port_number;
  471. struct GNUNET_CADET_ClientChannelNumber ccn;
  472. ccn = msg->ccn;
  473. port_number = &msg->port;
  474. if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
  475. {
  476. GNUNET_break (0);
  477. return;
  478. }
  479. port = find_port (h, port_number);
  480. if (NULL == port)
  481. {
  482. /* We could have closed the port but the service didn't know about it yet
  483. * This is not an error.
  484. */
  485. struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
  486. struct GNUNET_MQ_Envelope *env;
  487. LOG (GNUNET_ERROR_TYPE_DEBUG,
  488. "No handler for incoming channel %X (on port %s, recently closed?)\n",
  489. ntohl (ccn.channel_of_client),
  490. GNUNET_h2s (port_number));
  491. env =
  492. GNUNET_MQ_msg (d_msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
  493. d_msg->ccn = msg->ccn;
  494. GNUNET_MQ_send (h->mq, env);
  495. return;
  496. }
  497. ch = create_channel (h, &ccn);
  498. ch->peer = msg->peer;
  499. ch->incoming_port = port;
  500. LOG (GNUNET_ERROR_TYPE_DEBUG,
  501. "Creating incoming channel %X [%s] %p\n",
  502. ntohl (ccn.channel_of_client),
  503. GNUNET_h2s (port_number),
  504. ch);
  505. GNUNET_assert (NULL != port->connects);
  506. ch->window_changes = port->window_changes;
  507. ch->disconnects = port->disconnects;
  508. ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
  509. &cadet_mq_destroy_impl,
  510. &cadet_mq_cancel_impl,
  511. ch,
  512. port->handlers,
  513. &cadet_mq_error_handler,
  514. ch);
  515. ch->ctx = port->connects (port->cls, ch, &msg->peer);
  516. GNUNET_MQ_set_handlers_closure (ch->mq, ch->ctx);
  517. }
  518. /**
  519. * Process the channel destroy notification and free associated resources
  520. *
  521. * @param h The cadet handle
  522. * @param msg A message with the details of the channel being destroyed
  523. */
  524. static void
  525. handle_channel_destroy (
  526. void *cls,
  527. const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
  528. {
  529. struct GNUNET_CADET_Handle *h = cls;
  530. struct GNUNET_CADET_Channel *ch;
  531. ch = find_channel (h, msg->ccn);
  532. if (NULL == ch)
  533. {
  534. LOG (GNUNET_ERROR_TYPE_DEBUG,
  535. "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
  536. ntohl (msg->ccn.channel_of_client));
  537. return;
  538. }
  539. LOG (GNUNET_ERROR_TYPE_DEBUG,
  540. "Received channel destroy for channel %X from CADET service\n",
  541. ntohl (msg->ccn.channel_of_client));
  542. destroy_channel (ch);
  543. }
  544. /**
  545. * Check that message received from CADET service is well-formed.
  546. *
  547. * @param cls the `struct GNUNET_CADET_Handle`
  548. * @param message the message we got
  549. * @return #GNUNET_OK if the message is well-formed,
  550. * #GNUNET_SYSERR otherwise
  551. */
  552. static int
  553. check_local_data (void *cls, const struct GNUNET_CADET_LocalData *message)
  554. {
  555. uint16_t size;
  556. (void) cls;
  557. size = ntohs (message->header.size);
  558. if (sizeof(*message) + sizeof(struct GNUNET_MessageHeader) > size)
  559. {
  560. GNUNET_break (0);
  561. return GNUNET_SYSERR;
  562. }
  563. return GNUNET_OK;
  564. }
  565. /**
  566. * Process the incoming data packets, call appropriate handlers.
  567. *
  568. * @param h The cadet handle
  569. * @param message A message encapsulating the data
  570. */
  571. static void
  572. handle_local_data (void *cls, const struct GNUNET_CADET_LocalData *message)
  573. {
  574. struct GNUNET_CADET_Handle *h = cls;
  575. const struct GNUNET_MessageHeader *payload;
  576. struct GNUNET_CADET_Channel *ch;
  577. uint16_t type;
  578. int fwd;
  579. ch = find_channel (h, message->ccn);
  580. if (NULL == ch)
  581. {
  582. LOG (GNUNET_ERROR_TYPE_DEBUG,
  583. "Unknown channel %X for incoming data (recently closed?)\n",
  584. ntohl (message->ccn.channel_of_client));
  585. return;
  586. }
  587. payload = (const struct GNUNET_MessageHeader *) &message[1];
  588. type = ntohs (payload->type);
  589. fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
  590. LOG (GNUNET_ERROR_TYPE_DEBUG,
  591. "Got a %s data on channel %s [%X] of type %u\n",
  592. fwd ? "FWD" : "BWD",
  593. GNUNET_i2s (&ch->peer),
  594. ntohl (message->ccn.channel_of_client),
  595. type);
  596. GNUNET_MQ_inject_message (ch->mq, payload);
  597. }
  598. /**
  599. * Process a local ACK message, enabling the client to send
  600. * more data to the service.
  601. *
  602. * @param h Cadet handle.
  603. * @param message Message itself.
  604. */
  605. static void
  606. handle_local_ack (void *cls, const struct GNUNET_CADET_LocalAck *message)
  607. {
  608. struct GNUNET_CADET_Handle *h = cls;
  609. struct GNUNET_CADET_Channel *ch;
  610. ch = find_channel (h, message->ccn);
  611. if (NULL == ch)
  612. {
  613. LOG (GNUNET_ERROR_TYPE_DEBUG,
  614. "ACK on unknown channel %X\n",
  615. ntohl (message->ccn.channel_of_client));
  616. return;
  617. }
  618. ch->allow_send++;
  619. LOG (GNUNET_ERROR_TYPE_DEBUG,
  620. "Got an ACK on mq channel %X (peer %s); new window size is %u!\n",
  621. ntohl (ch->ccn.channel_of_client),
  622. GNUNET_i2s (&ch->peer),
  623. ch->allow_send);
  624. if (NULL == ch->pending_env)
  625. {
  626. LOG (GNUNET_ERROR_TYPE_DEBUG,
  627. "Got an ACK on mq channel %X, allow send now %u!\n",
  628. ntohl (ch->ccn.channel_of_client),
  629. ch->allow_send);
  630. notify_window_size (ch);
  631. return;
  632. }
  633. if (NULL != ch->mq_cont)
  634. return; /* already working on it! */
  635. ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now, ch);
  636. }
  637. /**
  638. * Function called during #GNUNET_CADET_disconnect() to destroy
  639. * all channels that are still open.
  640. *
  641. * @param cls the `struct GNUNET_CADET_Handle`
  642. * @param cid chanenl ID
  643. * @param value a `struct GNUNET_CADET_Channel` to destroy
  644. * @return #GNUNET_OK (continue to iterate)
  645. */
  646. static int
  647. destroy_channel_cb (void *cls, uint32_t cid, void *value)
  648. {
  649. /* struct GNUNET_CADET_Handle *handle = cls; */
  650. struct GNUNET_CADET_Channel *ch = value;
  651. (void) cls;
  652. (void) cid;
  653. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  654. "Destroying channel due to GNUNET_CADET_disconnect()\n");
  655. destroy_channel (ch);
  656. return GNUNET_OK;
  657. }
  658. /**
  659. * Generic error handler, called with the appropriate error code and
  660. * the same closure specified at the creation of the message queue.
  661. * Not every message queue implementation supports an error handler.
  662. *
  663. * @param cls closure, a `struct GNUNET_CORE_Handle *`
  664. * @param error error code
  665. */
  666. static void
  667. handle_mq_error (void *cls, enum GNUNET_MQ_Error error)
  668. {
  669. struct GNUNET_CADET_Handle *h = cls;
  670. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
  671. GNUNET_CONTAINER_multihashmap32_iterate (h->channels, &destroy_channel_cb, h);
  672. GNUNET_MQ_destroy (h->mq);
  673. h->mq = NULL;
  674. GNUNET_assert (NULL == h->reconnect_task);
  675. h->reconnect_task =
  676. GNUNET_SCHEDULER_add_delayed (h->reconnect_time, &reconnect_cbk, h);
  677. }
  678. /**
  679. * Reconnect to the service, retransmit all infomation to try to restore the
  680. * original state.
  681. *
  682. * @param h handle to the cadet
  683. */
  684. static void
  685. reconnect (struct GNUNET_CADET_Handle *h)
  686. {
  687. struct GNUNET_MQ_MessageHandler handlers[] =
  688. { GNUNET_MQ_hd_fixed_size (channel_created,
  689. GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
  690. struct GNUNET_CADET_LocalChannelCreateMessage,
  691. h),
  692. GNUNET_MQ_hd_fixed_size (channel_destroy,
  693. GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
  694. struct GNUNET_CADET_LocalChannelDestroyMessage,
  695. h),
  696. GNUNET_MQ_hd_var_size (local_data,
  697. GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
  698. struct GNUNET_CADET_LocalData,
  699. h),
  700. GNUNET_MQ_hd_fixed_size (local_ack,
  701. GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
  702. struct GNUNET_CADET_LocalAck,
  703. h),
  704. GNUNET_MQ_handler_end () };
  705. GNUNET_assert (NULL == h->mq);
  706. h->mq =
  707. GNUNET_CLIENT_connect (h->cfg, "cadet", handlers, &handle_mq_error, h);
  708. }
  709. /**
  710. * Function called during #GNUNET_CADET_disconnect() to destroy
  711. * all ports that are still open.
  712. *
  713. * @param cls the `struct GNUNET_CADET_Handle`
  714. * @param id port ID
  715. * @param value a `struct GNUNET_CADET_Channel` to destroy
  716. * @return #GNUNET_OK (continue to iterate)
  717. */
  718. static int
  719. destroy_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value)
  720. {
  721. /* struct GNUNET_CADET_Handle *handle = cls; */
  722. struct GNUNET_CADET_Port *port = value;
  723. (void) cls;
  724. (void) id;
  725. /* This is a warning, the app should have cleanly closed all open ports */
  726. GNUNET_break (0);
  727. GNUNET_CADET_close_port (port);
  728. return GNUNET_OK;
  729. }
  730. /**
  731. * Disconnect from the cadet service. All channels will be destroyed. All channel
  732. * disconnect callbacks will be called on any still connected peers, notifying
  733. * about their disconnection. The registered inbound channel cleaner will be
  734. * called should any inbound channels still exist.
  735. *
  736. * @param handle connection to cadet to disconnect
  737. */
  738. void
  739. GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
  740. {
  741. GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
  742. &destroy_port_cb,
  743. handle);
  744. GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
  745. handle->ports = NULL;
  746. GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
  747. &destroy_channel_cb,
  748. handle);
  749. GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
  750. handle->channels = NULL;
  751. if (NULL != handle->mq)
  752. {
  753. GNUNET_MQ_destroy (handle->mq);
  754. handle->mq = NULL;
  755. }
  756. if (NULL != handle->reconnect_task)
  757. {
  758. GNUNET_SCHEDULER_cancel (handle->reconnect_task);
  759. handle->reconnect_task = NULL;
  760. }
  761. GNUNET_free (handle);
  762. }
  763. /**
  764. * Close a port opened with @a GNUNET_CADET_open_port().
  765. * The @a new_channel callback will no longer be called.
  766. *
  767. * @param p Port handle.
  768. */
  769. void
  770. GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
  771. {
  772. GNUNET_assert (
  773. GNUNET_YES ==
  774. GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, &p->id, p));
  775. if (NULL != p->cadet->mq)
  776. {
  777. struct GNUNET_CADET_PortMessage *msg;
  778. struct GNUNET_MQ_Envelope *env;
  779. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
  780. msg->port = p->id;
  781. GNUNET_MQ_send (p->cadet->mq, env);
  782. }
  783. GNUNET_free_non_null (p->handlers);
  784. GNUNET_free (p);
  785. }
  786. /**
  787. * Destroy an existing channel.
  788. *
  789. * The existing end callback for the channel will NOT be called.
  790. * Any pending outgoing messages will be sent but no incoming messages will be
  791. * accepted and no data callbacks will be called.
  792. *
  793. * @param channel Channel handle, becomes invalid after this call.
  794. */
  795. void
  796. GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
  797. {
  798. struct GNUNET_CADET_Handle *h = channel->cadet;
  799. struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
  800. struct GNUNET_MQ_Envelope *env;
  801. if (NULL != h->mq)
  802. {
  803. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
  804. msg->ccn = channel->ccn;
  805. GNUNET_MQ_send (h->mq, env);
  806. }
  807. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  808. "Destroying channel due to GNUNET_CADET_channel_destroy()\n");
  809. channel->disconnects = NULL;
  810. destroy_channel (channel);
  811. }
  812. /**
  813. * Get information about a channel.
  814. *
  815. * @param channel Channel handle.
  816. * @param option Query (GNUNET_CADET_OPTION_*).
  817. * @param ... dependant on option, currently not used
  818. *
  819. * @return Union with an answer to the query.
  820. */
  821. const union GNUNET_CADET_ChannelInfo *
  822. GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
  823. enum GNUNET_CADET_ChannelInfoOption option,
  824. ...)
  825. {
  826. switch (option)
  827. {
  828. case GNUNET_CADET_OPTION_PEER:
  829. return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
  830. default:
  831. GNUNET_break (0);
  832. return NULL;
  833. }
  834. }
  835. /**
  836. * Send an ack on the channel to confirm the processing of a message.
  837. *
  838. * @param ch Channel on which to send the ACK.
  839. */
  840. void
  841. GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
  842. {
  843. struct GNUNET_CADET_LocalAck *msg;
  844. struct GNUNET_MQ_Envelope *env;
  845. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
  846. LOG (GNUNET_ERROR_TYPE_DEBUG,
  847. "Sending ACK on channel %X\n",
  848. ntohl (channel->ccn.channel_of_client));
  849. msg->ccn = channel->ccn;
  850. GNUNET_MQ_send (channel->cadet->mq, env);
  851. }
  852. /**
  853. * Connect to the MQ-based cadet service.
  854. *
  855. * @param cfg Configuration to use.
  856. *
  857. * @return Handle to the cadet service NULL on error.
  858. */
  859. struct GNUNET_CADET_Handle *
  860. GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  861. {
  862. struct GNUNET_CADET_Handle *h;
  863. LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
  864. h = GNUNET_new (struct GNUNET_CADET_Handle);
  865. h->cfg = cfg;
  866. h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
  867. h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
  868. reconnect (h);
  869. if (NULL == h->mq)
  870. {
  871. GNUNET_break (0);
  872. GNUNET_CADET_disconnect (h);
  873. return NULL;
  874. }
  875. h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
  876. return h;
  877. }
  878. /**
  879. * Function to return link to AGPL source upon request.
  880. *
  881. * @param cls closure with the identification of the client
  882. * @param msg AGPL request
  883. */
  884. static void
  885. return_agpl (void *cls, const struct GNUNET_MessageHeader *msg)
  886. {
  887. struct GNUNET_SERVICE_Client *client = cls;
  888. struct GNUNET_MQ_Handle *mq;
  889. struct GNUNET_MQ_Envelope *env;
  890. struct GNUNET_MessageHeader *res;
  891. size_t slen;
  892. const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
  893. (void) msg;
  894. slen = strlen (pd->agpl_url) + 1;
  895. env = GNUNET_MQ_msg_extra (res, GNUNET_MESSAGE_TYPE_RESPONSE_AGPL, slen);
  896. memcpy (&res[1], GNUNET_AGPL_URL, slen);
  897. mq = GNUNET_SERVICE_client_get_mq (client);
  898. GNUNET_MQ_send (mq, env);
  899. GNUNET_SERVICE_client_continue (client);
  900. }
  901. /**
  902. * Open a port to receive incomming MQ-based channels.
  903. *
  904. * @param h CADET handle.
  905. * @param port Hash identifying the port.
  906. * @param connects Function called when an incoming channel is connected.
  907. * @param connects_cls Closure for the @a connects handler.
  908. * @param window_changes Function called when the transmit window size changes.
  909. * @param disconnects Function called when a channel is disconnected.
  910. * @param handlers Callbacks for messages we care about, NULL-terminated.
  911. * @return Port handle, NULL if port is in use
  912. */
  913. struct GNUNET_CADET_Port *
  914. GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
  915. const struct GNUNET_HashCode *port,
  916. GNUNET_CADET_ConnectEventHandler connects,
  917. void *connects_cls,
  918. GNUNET_CADET_WindowSizeEventHandler window_changes,
  919. GNUNET_CADET_DisconnectEventHandler disconnects,
  920. const struct GNUNET_MQ_MessageHandler *handlers)
  921. {
  922. struct GNUNET_CADET_Port *p;
  923. const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
  924. GNUNET_assert (NULL != connects);
  925. GNUNET_assert (NULL != disconnects);
  926. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  927. "Listening to CADET port %s\n",
  928. GNUNET_h2s (port));
  929. p = GNUNET_new (struct GNUNET_CADET_Port);
  930. p->cadet = h;
  931. p->id = *port;
  932. if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
  933. h->ports,
  934. &p->id,
  935. p,
  936. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  937. {
  938. GNUNET_free (p);
  939. return NULL;
  940. }
  941. p->connects = connects;
  942. p->cls = connects_cls;
  943. p->window_changes = window_changes;
  944. p->disconnects = disconnects;
  945. p->handlers = (NULL == pd->agpl_url)
  946. ? GNUNET_MQ_copy_handlers (handlers)
  947. : GNUNET_MQ_copy_handlers2 (handlers, &return_agpl, NULL);
  948. GNUNET_assert (GNUNET_OK == open_port_cb (h, &p->id, p));
  949. return p;
  950. }
  951. /**
  952. * Create a new channel towards a remote peer.
  953. *
  954. * If the destination peer closes the channel after accepting it,
  955. * @a disconnects will be called for this channel (unless
  956. * #GNUNET_CADET_channel_destroy() was called on this end first).
  957. *
  958. * @param h CADET handle.
  959. * @param channel_cls Closure for the channel. It's given to:
  960. * - The disconnect handler @a disconnects
  961. * - Each message type callback in @a handlers
  962. * @param destination Peer identity the channel should go to.
  963. * @param port Identification of the destination port.
  964. * @param window_changes Function called when the transmit window size changes.
  965. * @param disconnects Function called when the channel is disconnected.
  966. * @param handlers Callbacks for messages we care about, NULL-terminated.
  967. * @return Handle to the channel.
  968. */
  969. struct GNUNET_CADET_Channel *
  970. GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
  971. void *channel_cls,
  972. const struct GNUNET_PeerIdentity *destination,
  973. const struct GNUNET_HashCode *port,
  974. GNUNET_CADET_WindowSizeEventHandler window_changes,
  975. GNUNET_CADET_DisconnectEventHandler disconnects,
  976. const struct GNUNET_MQ_MessageHandler *handlers)
  977. {
  978. struct GNUNET_CADET_Channel *ch;
  979. struct GNUNET_CADET_LocalChannelCreateMessage *msg;
  980. struct GNUNET_MQ_Envelope *env;
  981. GNUNET_assert (NULL != disconnects);
  982. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  983. "Creating channel to peer %s at port %s\n",
  984. GNUNET_i2s (destination),
  985. GNUNET_h2s (port));
  986. ch = create_channel (h, NULL);
  987. ch->ctx = channel_cls;
  988. ch->peer = *destination;
  989. ch->window_changes = window_changes;
  990. ch->disconnects = disconnects;
  991. /* Create MQ for channel */
  992. ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
  993. &cadet_mq_destroy_impl,
  994. &cadet_mq_cancel_impl,
  995. ch,
  996. handlers,
  997. &cadet_mq_error_handler,
  998. ch);
  999. GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
  1000. /* Request channel creation to service */
  1001. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
  1002. msg->ccn = ch->ccn;
  1003. msg->port = *port;
  1004. msg->peer = *destination;
  1005. GNUNET_MQ_send (h->mq, env);
  1006. return ch;
  1007. }
  1008. /**
  1009. * Obtain the message queue for a connected peer.
  1010. *
  1011. * @param channel The channel handle from which to get the MQ.
  1012. *
  1013. * @return NULL if @a channel is not yet connected.
  1014. */
  1015. struct GNUNET_MQ_Handle *
  1016. GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
  1017. {
  1018. return channel->mq;
  1019. }
  1020. /* end of cadet_api.c */