transport_api2_core.c 25 KB

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