transport_api_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 2016 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 transport/transport_api_core.c
  18. * @brief library to access the transport service for message exchange
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_transport_service.h"
  28. #include "transport.h"
  29. #define LOG(kind, ...) GNUNET_log_from (kind, "transport-api-core", __VA_ARGS__)
  30. /**
  31. * If we could not send any payload to a peer for this amount of
  32. * time, we print a warning.
  33. */
  34. #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
  35. /**
  36. * How large to start with for the hashmap of neighbours.
  37. */
  38. #define STARTING_NEIGHBOURS_SIZE 16
  39. /**
  40. * Entry in hash table of all of our current (connected) neighbours.
  41. */
  42. struct Neighbour
  43. {
  44. /**
  45. * Overall transport handle.
  46. */
  47. struct GNUNET_TRANSPORT_CoreHandle *h;
  48. /**
  49. * Active message queue for the peer.
  50. */
  51. struct GNUNET_MQ_Handle *mq;
  52. /**
  53. * Envelope with the message we are currently transmitting (or NULL).
  54. */
  55. struct GNUNET_MQ_Envelope *env;
  56. /**
  57. * Closure for @e mq handlers.
  58. */
  59. void *handlers_cls;
  60. /**
  61. * Identity of this neighbour.
  62. */
  63. struct GNUNET_PeerIdentity id;
  64. /**
  65. * Outbound bandwidh tracker.
  66. */
  67. struct GNUNET_BANDWIDTH_Tracker out_tracker;
  68. /**
  69. * Entry in our readiness heap (which is sorted by @e next_ready
  70. * value). NULL if there is no pending transmission request for
  71. * this neighbour or if we're waiting for @e is_ready to become
  72. * true AFTER the @e out_tracker suggested that this peer's quota
  73. * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
  74. * we should immediately go back into the heap).
  75. */
  76. struct GNUNET_CONTAINER_HeapNode *hn;
  77. /**
  78. * Task to trigger MQ when we have enough bandwidth for the
  79. * next transmission.
  80. */
  81. struct GNUNET_SCHEDULER_Task *timeout_task;
  82. /**
  83. * Sending consumed more bytes on wire than payload was announced
  84. * This overhead is added to the delay of next sending operation
  85. */
  86. unsigned long long traffic_overhead;
  87. /**
  88. * Is this peer currently ready to receive a message?
  89. */
  90. int is_ready;
  91. /**
  92. * Size of the message in @e env.
  93. */
  94. uint16_t env_size;
  95. };
  96. /**
  97. * Handle for the transport service (includes all of the
  98. * state for the transport service).
  99. */
  100. struct GNUNET_TRANSPORT_CoreHandle
  101. {
  102. /**
  103. * Closure for the callbacks.
  104. */
  105. void *cls;
  106. /**
  107. * Functions to call for received data (template for
  108. * new message queues).
  109. */
  110. struct GNUNET_MQ_MessageHandler *handlers;
  111. /**
  112. * function to call on connect events
  113. */
  114. GNUNET_TRANSPORT_NotifyConnect nc_cb;
  115. /**
  116. * function to call on disconnect events
  117. */
  118. GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
  119. /**
  120. * function to call on excess bandwidth events
  121. */
  122. GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
  123. /**
  124. * My client connection to the transport service.
  125. */
  126. struct GNUNET_MQ_Handle *mq;
  127. /**
  128. * My configuration.
  129. */
  130. const struct GNUNET_CONFIGURATION_Handle *cfg;
  131. /**
  132. * Hash map of the current connected neighbours of this peer.
  133. * Maps peer identities to `struct Neighbour` entries.
  134. */
  135. struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
  136. /**
  137. * Peer identity as assumed by this process, or all zeros.
  138. */
  139. struct GNUNET_PeerIdentity self;
  140. /**
  141. * ID of the task trying to reconnect to the service.
  142. */
  143. struct GNUNET_SCHEDULER_Task *reconnect_task;
  144. /**
  145. * Delay until we try to reconnect.
  146. */
  147. struct GNUNET_TIME_Relative reconnect_delay;
  148. /**
  149. * Internal counter to check how many more receive OK messages this
  150. * CORE service is allowed to send in total. Just to detect easy
  151. * cases of protocol violations by the CORE implementation.
  152. * NOTE: we may want to make this stronger by counting per peer
  153. * instead of globally.
  154. */
  155. unsigned int rom_pending;
  156. /**
  157. * Should we check that @e self matches what the service thinks?
  158. * (if #GNUNET_NO, then @e self is all zeros!).
  159. */
  160. int check_self;
  161. };
  162. /**
  163. * Function that will schedule the job that will try
  164. * to connect us again to the client.
  165. *
  166. * @param h transport service to reconnect
  167. */
  168. static void
  169. disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
  170. /**
  171. * Get the neighbour list entry for the given peer
  172. *
  173. * @param h our context
  174. * @param peer peer to look up
  175. * @return NULL if no such peer entry exists
  176. */
  177. static struct Neighbour *
  178. neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
  179. const struct GNUNET_PeerIdentity *peer)
  180. {
  181. return GNUNET_CONTAINER_multipeermap_get (h->neighbours, peer);
  182. }
  183. /**
  184. * Function called by the bandwidth tracker if we have excess
  185. * bandwidth.
  186. *
  187. * @param cls the `struct Neighbour` that has excess bandwidth
  188. */
  189. static void
  190. notify_excess_cb (void *cls)
  191. {
  192. struct Neighbour *n = cls;
  193. struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
  194. LOG (GNUNET_ERROR_TYPE_DEBUG,
  195. "Notifying CORE that more bandwidth is available for %s\n",
  196. GNUNET_i2s (&n->id));
  197. if (NULL != h->neb_cb)
  198. h->neb_cb (h->cls, &n->id, n->handlers_cls);
  199. }
  200. /**
  201. * Iterator over hash map entries, for deleting state of a neighbour.
  202. *
  203. * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
  204. * @param key peer identity
  205. * @param value value in the hash map, the neighbour entry to delete
  206. * @return #GNUNET_YES if we should continue to
  207. * iterate,
  208. * #GNUNET_NO if not.
  209. */
  210. static int
  211. neighbour_delete (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  212. {
  213. struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
  214. struct Neighbour *n = value;
  215. LOG (GNUNET_ERROR_TYPE_DEBUG,
  216. "Dropping entry for neighbour `%s'.\n",
  217. GNUNET_i2s (key));
  218. GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
  219. if (NULL != handle->nd_cb)
  220. handle->nd_cb (handle->cls, &n->id, n->handlers_cls);
  221. if (NULL != n->timeout_task)
  222. {
  223. GNUNET_SCHEDULER_cancel (n->timeout_task);
  224. n->timeout_task = NULL;
  225. }
  226. if (NULL != n->env)
  227. {
  228. GNUNET_MQ_send_cancel (n->env);
  229. n->env = NULL;
  230. }
  231. GNUNET_MQ_destroy (n->mq);
  232. GNUNET_assert (NULL == n->mq);
  233. GNUNET_assert (
  234. GNUNET_YES ==
  235. GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key, n));
  236. GNUNET_free (n);
  237. return GNUNET_YES;
  238. }
  239. /**
  240. * Generic error handler, called with the appropriate
  241. * error code and the same closure specified at the creation of
  242. * the message queue.
  243. * Not every message queue implementation supports an error handler.
  244. *
  245. * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
  246. * @param error error code
  247. */
  248. static void
  249. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  250. {
  251. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  252. LOG (GNUNET_ERROR_TYPE_ERROR,
  253. "Error receiving from transport service (%d), disconnecting temporarily.\n",
  254. error);
  255. disconnect_and_schedule_reconnect (h);
  256. }
  257. /**
  258. * Function we use for checking incoming HELLO messages.
  259. *
  260. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  261. * @param msg message received
  262. * @return #GNUNET_OK if message is well-formed
  263. */
  264. static int
  265. check_hello (void *cls, const struct GNUNET_MessageHeader *msg)
  266. {
  267. struct GNUNET_PeerIdentity me;
  268. if (GNUNET_OK !=
  269. GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
  270. {
  271. GNUNET_break (0);
  272. return GNUNET_SYSERR;
  273. }
  274. return GNUNET_OK;
  275. }
  276. /**
  277. * Function we use for handling incoming HELLO messages.
  278. *
  279. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  280. * @param msg message received
  281. */
  282. static void
  283. handle_hello (void *cls, const struct GNUNET_MessageHeader *msg)
  284. {
  285. /* we do not care => FIXME: signal in options to NEVER send HELLOs! */
  286. }
  287. /**
  288. * A message from the handler's message queue to a neighbour was
  289. * transmitted. Now trigger (possibly delayed) notification of the
  290. * neighbour's message queue that we are done and thus ready for
  291. * the next message.
  292. *
  293. * @param cls the `struct Neighbour` where the message was sent
  294. */
  295. static void
  296. notify_send_done_fin (void *cls)
  297. {
  298. struct Neighbour *n = cls;
  299. n->timeout_task = NULL;
  300. n->is_ready = GNUNET_YES;
  301. GNUNET_MQ_impl_send_continue (n->mq);
  302. }
  303. /**
  304. * A message from the handler's message queue to a neighbour was
  305. * transmitted. Now trigger (possibly delayed) notification of the
  306. * neighbour's message queue that we are done and thus ready for
  307. * the next message.
  308. *
  309. * @param cls the `struct Neighbour` where the message was sent
  310. */
  311. static void
  312. notify_send_done (void *cls)
  313. {
  314. struct Neighbour *n = cls;
  315. struct GNUNET_TIME_Relative delay;
  316. n->timeout_task = NULL;
  317. if (NULL != n->env)
  318. {
  319. GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
  320. n->env_size + n->traffic_overhead);
  321. n->env = NULL;
  322. n->traffic_overhead = 0;
  323. }
  324. delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, 128);
  325. if (0 == delay.rel_value_us)
  326. {
  327. n->is_ready = GNUNET_YES;
  328. GNUNET_MQ_impl_send_continue (n->mq);
  329. return;
  330. }
  331. GNUNET_MQ_impl_send_in_flight (n->mq);
  332. /* cannot send even a small message without violating
  333. quota, wait a before allowing MQ to send next message */
  334. n->timeout_task =
  335. GNUNET_SCHEDULER_add_delayed (delay, &notify_send_done_fin, n);
  336. }
  337. /**
  338. * Implement sending functionality of a message queue.
  339. * Called one message at a time. Should send the @a msg
  340. * to the transport service and then notify the queue
  341. * once we are ready for the next one.
  342. *
  343. * @param mq the message queue
  344. * @param msg the message to send
  345. * @param impl_state state of the implementation
  346. */
  347. static void
  348. mq_send_impl (struct GNUNET_MQ_Handle *mq,
  349. const struct GNUNET_MessageHeader *msg,
  350. void *impl_state)
  351. {
  352. struct Neighbour *n = impl_state;
  353. struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
  354. struct OutboundMessage *obm;
  355. uint16_t msize;
  356. GNUNET_assert (GNUNET_YES == n->is_ready);
  357. msize = ntohs (msg->size);
  358. if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*obm))
  359. {
  360. GNUNET_break (0);
  361. GNUNET_MQ_impl_send_continue (mq);
  362. return;
  363. }
  364. GNUNET_assert (NULL == n->env);
  365. n->env =
  366. GNUNET_MQ_msg_nested_mh (obm, GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, msg);
  367. {
  368. struct GNUNET_MQ_Envelope *env;
  369. env = GNUNET_MQ_get_current_envelope (mq);
  370. obm->priority = htonl ((uint32_t) GNUNET_MQ_env_get_options (env));
  371. }
  372. obm->timeout = GNUNET_TIME_relative_hton (
  373. GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
  374. obm->peer = n->id;
  375. GNUNET_assert (NULL == n->timeout_task);
  376. n->is_ready = GNUNET_NO;
  377. n->env_size = ntohs (msg->size);
  378. GNUNET_MQ_notify_sent (n->env, &notify_send_done, n);
  379. GNUNET_MQ_send (h->mq, n->env);
  380. LOG (GNUNET_ERROR_TYPE_DEBUG,
  381. "Queued message of type %u for neighbour `%s'.\n",
  382. ntohs (msg->type),
  383. GNUNET_i2s (&n->id));
  384. }
  385. /**
  386. * Handle destruction of a message queue. Implementations must not
  387. * free @a mq, but should take care of @a impl_state.
  388. *
  389. * @param mq the message queue to destroy
  390. * @param impl_state state of the implementation
  391. */
  392. static void
  393. mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
  394. {
  395. struct Neighbour *n = impl_state;
  396. GNUNET_assert (mq == n->mq);
  397. n->mq = NULL;
  398. }
  399. /**
  400. * Implementation function that cancels the currently sent message.
  401. * Should basically undo whatever #mq_send_impl() did.
  402. *
  403. * @param mq message queue
  404. * @param impl_state state specific to the implementation
  405. */
  406. static void
  407. mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
  408. {
  409. struct Neighbour *n = impl_state;
  410. GNUNET_assert (GNUNET_NO == n->is_ready);
  411. if (NULL != n->env)
  412. {
  413. GNUNET_MQ_send_cancel (n->env);
  414. n->env = NULL;
  415. }
  416. n->is_ready = GNUNET_YES;
  417. }
  418. /**
  419. * We had an error processing a message we forwarded from a peer to
  420. * the CORE service. We should just complain about it but otherwise
  421. * continue processing.
  422. *
  423. * @param cls closure
  424. * @param error error code
  425. */
  426. static void
  427. peer_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  428. {
  429. /* struct Neighbour *n = cls; */
  430. GNUNET_break_op (0);
  431. }
  432. /**
  433. * The outbound quota has changed in a way that may require
  434. * us to reset the timeout. Update the timeout.
  435. *
  436. * @param cls the `struct Neighbour` for which the timeout changed
  437. */
  438. static void
  439. outbound_bw_tracker_update (void *cls)
  440. {
  441. struct Neighbour *n = cls;
  442. struct GNUNET_TIME_Relative delay;
  443. if (NULL == n->timeout_task)
  444. return;
  445. delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, 128);
  446. GNUNET_SCHEDULER_cancel (n->timeout_task);
  447. n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &notify_send_done, n);
  448. }
  449. /**
  450. * Function we use for handling incoming connect messages.
  451. *
  452. * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
  453. * @param cim message received
  454. */
  455. static void
  456. handle_connect (void *cls, const struct ConnectInfoMessage *cim)
  457. {
  458. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  459. struct Neighbour *n;
  460. LOG (GNUNET_ERROR_TYPE_DEBUG,
  461. "Receiving CONNECT message for `%s' with quota %u\n",
  462. GNUNET_i2s (&cim->id),
  463. ntohl (cim->quota_out.value__));
  464. n = neighbour_find (h, &cim->id);
  465. if (NULL != n)
  466. {
  467. GNUNET_break (0); /* FIXME: this assertion seems to fail sometimes!? */
  468. disconnect_and_schedule_reconnect (h);
  469. return;
  470. }
  471. n = GNUNET_new (struct Neighbour);
  472. n->id = cim->id;
  473. n->h = h;
  474. n->is_ready = GNUNET_YES;
  475. n->traffic_overhead = 0;
  476. GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
  477. &outbound_bw_tracker_update,
  478. n,
  479. GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
  480. MAX_BANDWIDTH_CARRY_S,
  481. &notify_excess_cb,
  482. n);
  483. GNUNET_assert (GNUNET_OK ==
  484. GNUNET_CONTAINER_multipeermap_put (
  485. h->neighbours,
  486. &n->id,
  487. n,
  488. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  489. GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
  490. n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
  491. &mq_destroy_impl,
  492. &mq_cancel_impl,
  493. n,
  494. h->handlers,
  495. &peer_mq_error_handler,
  496. n);
  497. if (NULL != h->nc_cb)
  498. {
  499. n->handlers_cls = h->nc_cb (h->cls, &n->id, n->mq);
  500. GNUNET_MQ_set_handlers_closure (n->mq, n->handlers_cls);
  501. }
  502. }
  503. /**
  504. * Function we use for handling incoming disconnect messages.
  505. *
  506. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  507. * @param dim message received
  508. */
  509. static void
  510. handle_disconnect (void *cls, const struct DisconnectInfoMessage *dim)
  511. {
  512. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  513. struct Neighbour *n;
  514. GNUNET_break (ntohl (dim->reserved) == 0);
  515. LOG (GNUNET_ERROR_TYPE_DEBUG,
  516. "Receiving DISCONNECT message for `%s'.\n",
  517. GNUNET_i2s (&dim->peer));
  518. n = neighbour_find (h, &dim->peer);
  519. if (NULL == n)
  520. {
  521. GNUNET_break (0);
  522. disconnect_and_schedule_reconnect (h);
  523. return;
  524. }
  525. GNUNET_assert (GNUNET_YES == neighbour_delete (h, &dim->peer, n));
  526. }
  527. /**
  528. * Function we use for handling incoming send-ok messages.
  529. *
  530. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  531. * @param okm message received
  532. */
  533. static void
  534. handle_send_ok (void *cls, const struct SendOkMessage *okm)
  535. {
  536. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  537. struct Neighbour *n;
  538. uint32_t bytes_msg;
  539. uint32_t bytes_physical;
  540. uint16_t success = ntohs (okm->success);
  541. bytes_msg = ntohs (okm->bytes_msg);
  542. bytes_physical = ntohl (okm->bytes_physical);
  543. LOG (GNUNET_ERROR_TYPE_DEBUG,
  544. "Receiving SEND_OK message, transmission to %s %s.\n",
  545. GNUNET_i2s (&okm->peer),
  546. success == GNUNET_OK ? "succeeded" : "failed");
  547. n = neighbour_find (h, &okm->peer);
  548. if (NULL == n)
  549. {
  550. /* We should never get a 'SEND_OK' for a peer that we are not
  551. connected to */
  552. GNUNET_break (0);
  553. disconnect_and_schedule_reconnect (h);
  554. return;
  555. }
  556. if (bytes_physical > bytes_msg)
  557. {
  558. LOG (GNUNET_ERROR_TYPE_DEBUG,
  559. "Overhead for %u byte message was %u\n",
  560. bytes_msg,
  561. bytes_physical - bytes_msg);
  562. n->traffic_overhead += bytes_physical - bytes_msg;
  563. }
  564. }
  565. /**
  566. * Function we use for checking incoming "inbound" messages.
  567. *
  568. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  569. * @param im message received
  570. */
  571. static int
  572. check_recv (void *cls, const struct InboundMessage *im)
  573. {
  574. const struct GNUNET_MessageHeader *imm;
  575. uint16_t size;
  576. size = ntohs (im->header.size) - sizeof(*im);
  577. if (size < sizeof(struct GNUNET_MessageHeader))
  578. {
  579. GNUNET_break (0);
  580. return GNUNET_SYSERR;
  581. }
  582. imm = (const struct GNUNET_MessageHeader *) &im[1];
  583. if (ntohs (imm->size) != size)
  584. {
  585. GNUNET_break (0);
  586. return GNUNET_SYSERR;
  587. }
  588. return GNUNET_OK;
  589. }
  590. /**
  591. * Function we use for handling incoming messages.
  592. *
  593. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  594. * @param im message received
  595. */
  596. static void
  597. handle_recv (void *cls, const struct InboundMessage *im)
  598. {
  599. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  600. const struct GNUNET_MessageHeader *imm =
  601. (const struct GNUNET_MessageHeader *) &im[1];
  602. struct Neighbour *n;
  603. LOG (GNUNET_ERROR_TYPE_DEBUG,
  604. "Received message of type %u with %u bytes from `%s'.\n",
  605. (unsigned int) ntohs (imm->type),
  606. (unsigned int) ntohs (imm->size),
  607. GNUNET_i2s (&im->peer));
  608. n = neighbour_find (h, &im->peer);
  609. if (NULL == n)
  610. {
  611. GNUNET_break (0);
  612. disconnect_and_schedule_reconnect (h);
  613. return;
  614. }
  615. h->rom_pending++;
  616. GNUNET_MQ_inject_message (n->mq, imm);
  617. }
  618. /**
  619. * Function we use for handling incoming set quota messages.
  620. *
  621. * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
  622. * @param msg message received
  623. */
  624. static void
  625. handle_set_quota (void *cls, const struct QuotaSetMessage *qm)
  626. {
  627. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  628. struct Neighbour *n;
  629. LOG (GNUNET_ERROR_TYPE_DEBUG,
  630. "Receiving SET_QUOTA message for `%s' with quota %u\n",
  631. GNUNET_i2s (&qm->peer),
  632. ntohl (qm->quota.value__));
  633. n = neighbour_find (h, &qm->peer);
  634. if (NULL == n)
  635. {
  636. GNUNET_break (
  637. 0); /* FIXME: julius reports this assertion fails sometimes? */
  638. disconnect_and_schedule_reconnect (h);
  639. return;
  640. }
  641. GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
  642. }
  643. /**
  644. * Try again to connect to transport service.
  645. *
  646. * @param cls the handle to the transport service
  647. */
  648. static void
  649. reconnect (void *cls)
  650. {
  651. struct GNUNET_TRANSPORT_CoreHandle *h = cls;
  652. struct GNUNET_MQ_MessageHandler handlers[] =
  653. { GNUNET_MQ_hd_var_size (hello,
  654. GNUNET_MESSAGE_TYPE_HELLO,
  655. struct GNUNET_MessageHeader,
  656. h),
  657. GNUNET_MQ_hd_fixed_size (connect,
  658. GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
  659. struct ConnectInfoMessage,
  660. h),
  661. GNUNET_MQ_hd_fixed_size (disconnect,
  662. GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
  663. struct DisconnectInfoMessage,
  664. h),
  665. GNUNET_MQ_hd_fixed_size (send_ok,
  666. GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
  667. struct SendOkMessage,
  668. h),
  669. GNUNET_MQ_hd_var_size (recv,
  670. GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
  671. struct InboundMessage,
  672. h),
  673. GNUNET_MQ_hd_fixed_size (set_quota,
  674. GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
  675. struct QuotaSetMessage,
  676. h),
  677. GNUNET_MQ_handler_end () };
  678. struct GNUNET_MQ_Envelope *env;
  679. struct StartMessage *s;
  680. uint32_t options;
  681. h->reconnect_task = NULL;
  682. LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
  683. GNUNET_assert (NULL == h->mq);
  684. h->mq =
  685. GNUNET_CLIENT_connect (h->cfg, "transport", handlers, &mq_error_handler, h);
  686. if (NULL == h->mq)
  687. return;
  688. env = GNUNET_MQ_msg (s, GNUNET_MESSAGE_TYPE_TRANSPORT_START);
  689. options = 0;
  690. if (h->check_self)
  691. options |= 1;
  692. if (NULL != h->handlers)
  693. options |= 2;
  694. s->options = htonl (options);
  695. s->self = h->self;
  696. GNUNET_MQ_send (h->mq, env);
  697. }
  698. /**
  699. * Function that will schedule the job that will try
  700. * to connect us again to the client.
  701. *
  702. * @param h transport service to reconnect
  703. */
  704. static void
  705. disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
  706. {
  707. GNUNET_assert (NULL == h->reconnect_task);
  708. /* Forget about all neighbours that we used to be connected to */
  709. GNUNET_CONTAINER_multipeermap_iterate (h->neighbours, &neighbour_delete, h);
  710. if (NULL != h->mq)
  711. {
  712. GNUNET_MQ_destroy (h->mq);
  713. h->mq = NULL;
  714. }
  715. LOG (GNUNET_ERROR_TYPE_DEBUG,
  716. "Scheduling task to reconnect to transport service in %s.\n",
  717. GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
  718. h->reconnect_task =
  719. GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
  720. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  721. }
  722. /**
  723. * Checks if a given peer is connected to us and get the message queue.
  724. *
  725. * @param handle connection to transport service
  726. * @param peer the peer to check
  727. * @return NULL if disconnected, otherwise message queue for @a peer
  728. */
  729. struct GNUNET_MQ_Handle *
  730. GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
  731. const struct GNUNET_PeerIdentity *peer)
  732. {
  733. struct Neighbour *n;
  734. n = neighbour_find (handle, peer);
  735. if (NULL == n)
  736. return NULL;
  737. return n->mq;
  738. }
  739. /**
  740. * Connect to the transport service. Note that the connection may
  741. * complete (or fail) asynchronously.
  742. *
  743. * @param cfg configuration to use
  744. * @param self our own identity (API should check that it matches
  745. * the identity found by transport), or NULL (no check)
  746. * @param cls closure for the callbacks
  747. * @param rec receive function to call
  748. * @param nc function to call on connect events
  749. * @param nd function to call on disconnect events
  750. * @param neb function to call if we have excess bandwidth to a peer
  751. * @return NULL on error
  752. */
  753. struct GNUNET_TRANSPORT_CoreHandle *
  754. GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  755. const struct GNUNET_PeerIdentity *self,
  756. const struct GNUNET_MQ_MessageHandler *handlers,
  757. void *cls,
  758. GNUNET_TRANSPORT_NotifyConnect nc,
  759. GNUNET_TRANSPORT_NotifyDisconnect nd,
  760. GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
  761. {
  762. struct GNUNET_TRANSPORT_CoreHandle *h;
  763. unsigned int i;
  764. h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
  765. if (NULL != self)
  766. {
  767. h->self = *self;
  768. h->check_self = GNUNET_YES;
  769. }
  770. h->cfg = cfg;
  771. h->cls = cls;
  772. h->nc_cb = nc;
  773. h->nd_cb = nd;
  774. h->neb_cb = neb;
  775. h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
  776. if (NULL != handlers)
  777. {
  778. for (i = 0; NULL != handlers[i].cb; i++)
  779. ;
  780. h->handlers = GNUNET_new_array (i + 1, struct GNUNET_MQ_MessageHandler);
  781. GNUNET_memcpy (h->handlers,
  782. handlers,
  783. i * sizeof(struct GNUNET_MQ_MessageHandler));
  784. }
  785. LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service\n");
  786. reconnect (h);
  787. if (NULL == h->mq)
  788. {
  789. GNUNET_free (h->handlers);
  790. GNUNET_free (h);
  791. return NULL;
  792. }
  793. h->neighbours =
  794. GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES);
  795. return h;
  796. }
  797. /**
  798. * Disconnect from the transport service.
  799. *
  800. * @param handle handle to the service as returned from
  801. * #GNUNET_TRANSPORT_core_connect()
  802. */
  803. void
  804. GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
  805. {
  806. LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
  807. /* this disconnects all neighbours... */
  808. if (NULL == handle->reconnect_task)
  809. disconnect_and_schedule_reconnect (handle);
  810. /* and now we stop trying to connect again... */
  811. if (NULL != handle->reconnect_task)
  812. {
  813. GNUNET_SCHEDULER_cancel (handle->reconnect_task);
  814. handle->reconnect_task = NULL;
  815. }
  816. GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
  817. handle->neighbours = NULL;
  818. GNUNET_free (handle->handlers);
  819. handle->handlers = NULL;
  820. GNUNET_free (handle);
  821. }
  822. /**
  823. * Notification from the CORE service to the TRANSPORT service
  824. * that the CORE service has finished processing a message from
  825. * TRANSPORT (via the @code{handlers} of #GNUNET_TRANSPORT_core_connect())
  826. * and that it is thus now OK for TRANSPORT to send more messages
  827. * for @a pid.
  828. *
  829. * Used to provide flow control, this is our equivalent to
  830. * #GNUNET_SERVICE_client_continue() of an ordinary service.
  831. *
  832. * Note that due to the use of a window, TRANSPORT may send multiple
  833. * messages destined for the same peer even without an intermediate
  834. * call to this function. However, CORE must still call this function
  835. * once per message received, as otherwise eventually the window will
  836. * be full and TRANSPORT will stop providing messages to CORE for @a
  837. * pid.
  838. *
  839. * @param ch core handle
  840. * @param pid which peer was the message from that was fully processed by CORE
  841. */
  842. void
  843. GNUNET_TRANSPORT_core_receive_continue (struct GNUNET_TRANSPORT_CoreHandle *ch,
  844. const struct GNUNET_PeerIdentity *pid)
  845. {
  846. struct RecvOkMessage *rom;
  847. struct GNUNET_MQ_Envelope *env;
  848. GNUNET_assert (ch->rom_pending > 0);
  849. ch->rom_pending--;
  850. env = GNUNET_MQ_msg (rom, GNUNET_MESSAGE_TYPE_TRANSPORT_RECV_OK);
  851. rom->increase_window_delta = htonl (1);
  852. rom->peer = *pid;
  853. GNUNET_MQ_send (ch->mq, env);
  854. }
  855. /* end of transport_api_core.c */