2
0

gnunet-service-cadet_connection.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-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/gnunet-service-cadet_connection.c
  18. * @brief management of CORE-level end-to-end connections; establishes
  19. * end-to-end routes and transmits messages along the route
  20. * @author Bartlomiej Polot
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet-service-cadet_connection.h"
  25. #include "gnunet-service-cadet_channel.h"
  26. #include "gnunet-service-cadet_paths.h"
  27. #include "gnunet-service-cadet_tunnels.h"
  28. #include "gnunet_cadet_service.h"
  29. #include "gnunet_statistics_service.h"
  30. #include "cadet_protocol.h"
  31. #define LOG(level, ...) GNUNET_log_from(level,"cadet-con",__VA_ARGS__)
  32. /**
  33. * How long do we wait initially before retransmitting the KX?
  34. * TODO: replace by 2 RTT if/once we have connection-level RTT data!
  35. */
  36. #define INITIAL_CONNECTION_CREATE_RETRY_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200)
  37. /**
  38. * All the states a connection can be in.
  39. */
  40. enum CadetConnectionState
  41. {
  42. /**
  43. * Uninitialized status, we have not yet even gotten the message queue.
  44. */
  45. CADET_CONNECTION_NEW,
  46. /**
  47. * Connection create message in queue, awaiting transmission by CORE.
  48. */
  49. CADET_CONNECTION_SENDING_CREATE,
  50. /**
  51. * Connection create message sent, waiting for ACK.
  52. */
  53. CADET_CONNECTION_SENT,
  54. /**
  55. * We are an inbound connection, and received a CREATE. Need to
  56. * send an CREATE_ACK back.
  57. */
  58. CADET_CONNECTION_CREATE_RECEIVED,
  59. /**
  60. * Connection confirmed, ready to carry traffic.
  61. */
  62. CADET_CONNECTION_READY
  63. };
  64. /**
  65. * Low-level connection to a destination.
  66. */
  67. struct CadetConnection
  68. {
  69. /**
  70. * ID of the connection.
  71. */
  72. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  73. /**
  74. * To which peer does this connection go?
  75. */
  76. struct CadetPeer *destination;
  77. /**
  78. * Which tunnel is using this connection?
  79. */
  80. struct CadetTConnection *ct;
  81. /**
  82. * Path we are using to our destination.
  83. */
  84. struct CadetPeerPath *path;
  85. /**
  86. * Pending message, NULL if we are ready to transmit.
  87. */
  88. struct GNUNET_MQ_Envelope *env;
  89. /**
  90. * Handle for calling #GCP_request_mq_cancel() once we are finished.
  91. */
  92. struct GCP_MessageQueueManager *mq_man;
  93. /**
  94. * Task for connection maintenance.
  95. */
  96. struct GNUNET_SCHEDULER_Task *task;
  97. /**
  98. * Queue entry for keepalive messages.
  99. */
  100. struct CadetTunnelQueueEntry *keepalive_qe;
  101. /**
  102. * Function to call once we are ready to transmit.
  103. */
  104. GCC_ReadyCallback ready_cb;
  105. /**
  106. * Closure for @e ready_cb.
  107. */
  108. void *ready_cb_cls;
  109. /**
  110. * How long do we wait before we try again with a CREATE message?
  111. */
  112. struct GNUNET_TIME_Relative retry_delay;
  113. /**
  114. * Earliest time for re-trying CREATE
  115. */
  116. struct GNUNET_TIME_Absolute create_at;
  117. /**
  118. * Earliest time for re-trying CREATE_ACK
  119. */
  120. struct GNUNET_TIME_Absolute create_ack_at;
  121. /**
  122. * Performance metrics for this connection.
  123. */
  124. struct CadetConnectionMetrics metrics;
  125. /**
  126. * State of the connection.
  127. */
  128. enum CadetConnectionState state;
  129. /**
  130. * Options for the route, control buffering.
  131. */
  132. enum GNUNET_CADET_ChannelOption options;
  133. /**
  134. * How many latency observations did we make for this connection?
  135. */
  136. unsigned int latency_datapoints;
  137. /**
  138. * Offset of our @e destination in @e path.
  139. */
  140. unsigned int off;
  141. /**
  142. * Are we ready to transmit via @e mq_man right now?
  143. */
  144. int mqm_ready;
  145. };
  146. /**
  147. * Lookup a connection by its identifier.
  148. *
  149. * @param cid identifier to resolve
  150. * @return NULL if connection was not found
  151. */
  152. struct CadetConnection *
  153. GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
  154. {
  155. return GNUNET_CONTAINER_multishortmap_get (connections,
  156. &cid->connection_of_tunnel);
  157. }
  158. /**
  159. * Update the connection state. Also triggers the necessary
  160. * MQM notifications.
  161. *
  162. * @param cc connection to update the state for
  163. * @param new_state new state for @a cc
  164. * @param new_mqm_ready new `mqm_ready` state for @a cc
  165. */
  166. static void
  167. update_state (struct CadetConnection *cc,
  168. enum CadetConnectionState new_state,
  169. int new_mqm_ready)
  170. {
  171. int old_ready;
  172. int new_ready;
  173. if ( (new_state == cc->state) &&
  174. (new_mqm_ready == cc->mqm_ready) )
  175. return; /* no change, nothing to do */
  176. old_ready = ( (CADET_CONNECTION_READY == cc->state) &&
  177. (GNUNET_YES == cc->mqm_ready) );
  178. new_ready = ( (CADET_CONNECTION_READY == new_state) &&
  179. (GNUNET_YES == new_mqm_ready) );
  180. cc->state = new_state;
  181. cc->mqm_ready = new_mqm_ready;
  182. if (old_ready != new_ready)
  183. cc->ready_cb (cc->ready_cb_cls,
  184. new_ready);
  185. }
  186. /**
  187. * Destroy a connection, part of the internal implementation. Called
  188. * only from #GCC_destroy_from_core() or #GCC_destroy_from_tunnel().
  189. *
  190. * @param cc connection to destroy
  191. */
  192. static void
  193. GCC_destroy (struct CadetConnection *cc)
  194. {
  195. LOG (GNUNET_ERROR_TYPE_DEBUG,
  196. "Destroying %s\n",
  197. GCC_2s (cc));
  198. if (NULL != cc->mq_man)
  199. {
  200. GCP_request_mq_cancel (cc->mq_man,
  201. NULL);
  202. cc->mq_man = NULL;
  203. }
  204. if (NULL != cc->task)
  205. {
  206. GNUNET_SCHEDULER_cancel (cc->task);
  207. cc->task = NULL;
  208. }
  209. if (NULL != cc->keepalive_qe)
  210. {
  211. GCT_send_cancel (cc->keepalive_qe);
  212. cc->keepalive_qe = NULL;
  213. }
  214. GCPP_del_connection (cc->path,
  215. cc->off,
  216. cc);
  217. for (unsigned int i=0;i<cc->off;i++)
  218. GCP_remove_connection (GCPP_get_peer_at_offset (cc->path,
  219. i),
  220. cc);
  221. GNUNET_assert (GNUNET_YES ==
  222. GNUNET_CONTAINER_multishortmap_remove (connections,
  223. &GCC_get_id (cc)->connection_of_tunnel,
  224. cc));
  225. GNUNET_free (cc);
  226. }
  227. /**
  228. * Destroy a connection, called when the CORE layer is already done
  229. * (i.e. has received a BROKEN message), but if we still have to
  230. * communicate the destruction of the connection to the tunnel (if one
  231. * exists).
  232. *
  233. * @param cc connection to destroy
  234. */
  235. void
  236. GCC_destroy_without_core (struct CadetConnection *cc)
  237. {
  238. if (NULL != cc->ct)
  239. {
  240. GCT_connection_lost (cc->ct);
  241. cc->ct = NULL;
  242. }
  243. GCC_destroy (cc);
  244. }
  245. /**
  246. * Destroy a connection, called if the tunnel association with the
  247. * connection was already broken, but we still need to notify the CORE
  248. * layer about the breakage.
  249. *
  250. * @param cc connection to destroy
  251. */
  252. void
  253. GCC_destroy_without_tunnel (struct CadetConnection *cc)
  254. {
  255. cc->ct = NULL;
  256. if ( (CADET_CONNECTION_SENDING_CREATE != cc->state) &&
  257. (NULL != cc->mq_man) )
  258. {
  259. struct GNUNET_MQ_Envelope *env;
  260. struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
  261. /* Need to notify next hop that we are down. */
  262. env = GNUNET_MQ_msg (destroy_msg,
  263. GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
  264. destroy_msg->cid = cc->cid;
  265. GCP_request_mq_cancel (cc->mq_man,
  266. env);
  267. cc->mq_man = NULL;
  268. }
  269. GCC_destroy (cc);
  270. }
  271. /**
  272. * Return the tunnel associated with this connection.
  273. *
  274. * @param cc connection to query
  275. * @return corresponding entry in the tunnel's connection list
  276. */
  277. struct CadetTConnection *
  278. GCC_get_ct (struct CadetConnection *cc)
  279. {
  280. return cc->ct;
  281. }
  282. /**
  283. * Obtain performance @a metrics from @a cc.
  284. *
  285. * @param cc connection to query
  286. * @return the metrics
  287. */
  288. const struct CadetConnectionMetrics *
  289. GCC_get_metrics (struct CadetConnection *cc)
  290. {
  291. return &cc->metrics;
  292. }
  293. /**
  294. * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
  295. * tunnel to prevent it from timing out.
  296. *
  297. * @param cls the `struct CadetConnection` to keep alive.
  298. */
  299. static void
  300. send_keepalive (void *cls);
  301. /**
  302. * Keepalive was transmitted. Remember this, and possibly
  303. * schedule the next one.
  304. *
  305. * @param cls the `struct CadetConnection` to keep alive.
  306. * @param cid identifier of the connection within the tunnel, NULL
  307. * if transmission failed
  308. */
  309. static void
  310. keepalive_done (void *cls,
  311. const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
  312. {
  313. struct CadetConnection *cc = cls;
  314. cc->keepalive_qe = NULL;
  315. if ( (GNUNET_YES == cc->mqm_ready) &&
  316. (NULL == cc->task) )
  317. cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
  318. &send_keepalive,
  319. cc);
  320. }
  321. /**
  322. * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
  323. * tunnel to prevent it from timing out.
  324. *
  325. * @param cls the `struct CadetConnection` to keep alive.
  326. */
  327. static void
  328. send_keepalive (void *cls)
  329. {
  330. struct CadetConnection *cc = cls;
  331. struct GNUNET_MessageHeader msg;
  332. cc->task = NULL;
  333. if (CADET_TUNNEL_KEY_OK != GCT_get_estate (cc->ct->t))
  334. {
  335. /* Tunnel not yet ready, wait with keepalives... */
  336. cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
  337. &send_keepalive,
  338. cc);
  339. return;
  340. }
  341. GNUNET_assert (NULL != cc->ct);
  342. GNUNET_assert (GNUNET_YES == cc->mqm_ready);
  343. GNUNET_assert (NULL == cc->keepalive_qe);
  344. LOG (GNUNET_ERROR_TYPE_INFO,
  345. "Sending KEEPALIVE on behalf of %s via %s\n",
  346. GCC_2s (cc),
  347. GCT_2s (cc->ct->t));
  348. GNUNET_STATISTICS_update (stats,
  349. "# keepalives sent",
  350. 1,
  351. GNUNET_NO);
  352. msg.size = htons (sizeof (msg));
  353. msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
  354. cc->keepalive_qe
  355. = GCT_send (cc->ct->t,
  356. &msg,
  357. &keepalive_done,
  358. cc);
  359. }
  360. /**
  361. * We sent a message for which we expect to receive an ACK via
  362. * the connection identified by @a cti.
  363. *
  364. * @param cid connection identifier where we expect an ACK
  365. */
  366. void
  367. GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
  368. {
  369. struct CadetConnection *cc;
  370. cc = GCC_lookup (cid);
  371. if (NULL == cc)
  372. return; /* whopise, connection alredy down? */
  373. cc->metrics.num_acked_transmissions++;
  374. }
  375. /**
  376. * We observed an ACK for a message that was originally sent via
  377. * the connection identified by @a cti.
  378. *
  379. * @param cti connection identifier where we got an ACK for a message
  380. * that was originally sent via this connection (the ACK
  381. * may have gotten back to us via a different connection).
  382. */
  383. void
  384. GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
  385. {
  386. struct CadetConnection *cc;
  387. cc = GCC_lookup (cid);
  388. if (NULL == cc)
  389. return; /* whopise, connection alredy down? */
  390. cc->metrics.num_successes++;
  391. }
  392. /**
  393. * We observed some the given @a latency on the connection
  394. * identified by @a cti. (The same connection was taken
  395. * in both directions.)
  396. *
  397. * @param cid connection identifier where we measured latency
  398. * @param latency the observed latency
  399. */
  400. void
  401. GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
  402. struct GNUNET_TIME_Relative latency)
  403. {
  404. struct CadetConnection *cc;
  405. double weight;
  406. double result;
  407. cc = GCC_lookup (cid);
  408. if (NULL == cc)
  409. return; /* whopise, connection alredy down? */
  410. GNUNET_STATISTICS_update (stats,
  411. "# latencies observed",
  412. 1,
  413. GNUNET_NO);
  414. cc->latency_datapoints++;
  415. if (cc->latency_datapoints >= 7)
  416. weight = 7.0;
  417. else
  418. weight = cc->latency_datapoints;
  419. /* Compute weighted average, giving at MOST weight 7 to the
  420. existing values, or less if that value is based on fewer than 7
  421. measurements. */
  422. result = (weight * cc->metrics.aged_latency.rel_value_us) + 1.0 * latency.rel_value_us;
  423. result /= (weight + 1.0);
  424. cc->metrics.aged_latency.rel_value_us = (uint64_t) result;
  425. }
  426. /**
  427. * A #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK was received for
  428. * this connection, implying that the end-to-end connection is up.
  429. * Process it.
  430. *
  431. * @param cc the connection that got the ACK.
  432. */
  433. void
  434. GCC_handle_connection_create_ack (struct CadetConnection *cc)
  435. {
  436. LOG (GNUNET_ERROR_TYPE_DEBUG,
  437. "Received CADET_CONNECTION_CREATE_ACK for %s in state %d (%s)\n",
  438. GCC_2s (cc),
  439. cc->state,
  440. (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
  441. if (CADET_CONNECTION_READY == cc->state)
  442. return; /* Duplicate ACK, ignore */
  443. if (NULL != cc->task)
  444. {
  445. GNUNET_SCHEDULER_cancel (cc->task);
  446. cc->task = NULL;
  447. }
  448. cc->metrics.age = GNUNET_TIME_absolute_get ();
  449. update_state (cc,
  450. CADET_CONNECTION_READY,
  451. cc->mqm_ready);
  452. if ( (NULL == cc->keepalive_qe) &&
  453. (GNUNET_YES == cc->mqm_ready) &&
  454. (NULL == cc->task) )
  455. cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
  456. &send_keepalive,
  457. cc);
  458. }
  459. /**
  460. * Handle KX message.
  461. *
  462. * @param cc connection that received encrypted message
  463. * @param msg the key exchange message
  464. */
  465. void
  466. GCC_handle_kx (struct CadetConnection *cc,
  467. const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
  468. {
  469. LOG (GNUNET_ERROR_TYPE_DEBUG,
  470. "Received KX message with ephermal %s on CC %s in state %d\n",
  471. GNUNET_e2s (&msg->ephemeral_key),
  472. GNUNET_sh2s (&cc->cid.connection_of_tunnel),
  473. cc->state);
  474. if (CADET_CONNECTION_SENT == cc->state)
  475. {
  476. /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
  477. clearly something is working, so pretend we got an ACK. */
  478. LOG (GNUNET_ERROR_TYPE_DEBUG,
  479. "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
  480. GCC_2s (cc));
  481. GCC_handle_connection_create_ack (cc);
  482. }
  483. GCT_handle_kx (cc->ct,
  484. msg);
  485. }
  486. /**
  487. * Handle KX_AUTH message.
  488. *
  489. * @param cc connection that received encrypted message
  490. * @param msg the key exchange message
  491. */
  492. void
  493. GCC_handle_kx_auth (struct CadetConnection *cc,
  494. const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
  495. {
  496. LOG (GNUNET_ERROR_TYPE_DEBUG,
  497. "Received KX AUTH message with ephermal %s on CC %s in state %d\n",
  498. GNUNET_e2s (&msg->kx.ephemeral_key),
  499. GNUNET_sh2s (&cc->cid.connection_of_tunnel),
  500. cc->state);
  501. if (CADET_CONNECTION_SENT == cc->state)
  502. {
  503. /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
  504. clearly something is working, so pretend we got an ACK. */
  505. LOG (GNUNET_ERROR_TYPE_DEBUG,
  506. "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
  507. GCC_2s (cc));
  508. GCC_handle_connection_create_ack (cc);
  509. }
  510. GCT_handle_kx_auth (cc->ct,
  511. msg);
  512. }
  513. /**
  514. * Handle encrypted message.
  515. *
  516. * @param cc connection that received encrypted message
  517. * @param msg the encrypted message to decrypt
  518. */
  519. void
  520. GCC_handle_encrypted (struct CadetConnection *cc,
  521. const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
  522. {
  523. if (CADET_CONNECTION_SENT == cc->state)
  524. {
  525. /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
  526. clearly something is working, so pretend we got an ACK. */
  527. LOG (GNUNET_ERROR_TYPE_DEBUG,
  528. "Faking connection ACK for %s due to ENCRYPTED payload\n",
  529. GCC_2s (cc));
  530. GCC_handle_connection_create_ack (cc);
  531. }
  532. cc->metrics.last_use = GNUNET_TIME_absolute_get ();
  533. GCT_handle_encrypted (cc->ct,
  534. msg);
  535. }
  536. /**
  537. * Send a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE message to the
  538. * first hop.
  539. *
  540. * @param cls the `struct CadetConnection` to initiate
  541. */
  542. static void
  543. send_create (void *cls)
  544. {
  545. struct CadetConnection *cc = cls;
  546. struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
  547. struct GNUNET_PeerIdentity *pids;
  548. struct GNUNET_MQ_Envelope *env;
  549. cc->task = NULL;
  550. GNUNET_assert (GNUNET_YES == cc->mqm_ready);
  551. env = GNUNET_MQ_msg_extra (create_msg,
  552. (2 + cc->off) * sizeof (struct GNUNET_PeerIdentity),
  553. GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
  554. create_msg->options = htonl ((uint32_t) cc->options);
  555. create_msg->cid = cc->cid;
  556. pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
  557. pids[0] = my_full_id;
  558. for (unsigned int i=0;i<=cc->off;i++)
  559. pids[i + 1] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
  560. i));
  561. LOG (GNUNET_ERROR_TYPE_DEBUG,
  562. "Sending CADET_CONNECTION_CREATE message for %s with %u hops\n",
  563. GCC_2s (cc),
  564. cc->off + 2);
  565. cc->env = env;
  566. cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
  567. cc->create_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay);
  568. update_state (cc,
  569. CADET_CONNECTION_SENT,
  570. GNUNET_NO);
  571. GCP_send (cc->mq_man,
  572. env);
  573. }
  574. /**
  575. * Send a CREATE_ACK message towards the origin.
  576. *
  577. * @param cls the `struct CadetConnection` to initiate
  578. */
  579. static void
  580. send_create_ack (void *cls)
  581. {
  582. struct CadetConnection *cc = cls;
  583. struct GNUNET_CADET_ConnectionCreateAckMessage *ack_msg;
  584. struct GNUNET_MQ_Envelope *env;
  585. cc->task = NULL;
  586. LOG (GNUNET_ERROR_TYPE_DEBUG,
  587. "Sending CONNECTION_CREATE_ACK message for %s\n",
  588. GCC_2s (cc));
  589. GNUNET_assert (GNUNET_YES == cc->mqm_ready);
  590. env = GNUNET_MQ_msg (ack_msg,
  591. GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK);
  592. ack_msg->cid = cc->cid;
  593. cc->env = env;
  594. cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
  595. cc->create_ack_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay);
  596. if (CADET_CONNECTION_CREATE_RECEIVED == cc->state)
  597. update_state (cc,
  598. CADET_CONNECTION_READY,
  599. GNUNET_NO);
  600. if (CADET_CONNECTION_READY == cc->state)
  601. cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
  602. &send_keepalive,
  603. cc);
  604. GCP_send (cc->mq_man,
  605. env);
  606. }
  607. /**
  608. * We got a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE for a
  609. * connection that we already have. Either our ACK got lost
  610. * or something is fishy. Consider retransmitting the ACK.
  611. *
  612. * @param cc connection that got the duplicate CREATE
  613. */
  614. void
  615. GCC_handle_duplicate_create (struct CadetConnection *cc)
  616. {
  617. if (GNUNET_YES == cc->mqm_ready)
  618. {
  619. LOG (GNUNET_ERROR_TYPE_DEBUG,
  620. "Got duplicate CREATE for %s, scheduling another ACK (%s)\n",
  621. GCC_2s (cc),
  622. (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
  623. /* Revert back to the state of having only received the 'CREATE',
  624. and immediately proceed to send the CREATE_ACK. */
  625. update_state (cc,
  626. CADET_CONNECTION_CREATE_RECEIVED,
  627. cc->mqm_ready);
  628. if (NULL != cc->task)
  629. GNUNET_SCHEDULER_cancel (cc->task);
  630. cc->task = GNUNET_SCHEDULER_add_at (cc->create_ack_at,
  631. &send_create_ack,
  632. cc);
  633. }
  634. else
  635. {
  636. /* We are currently sending something else back, which
  637. can only be an ACK or payload, either of which would
  638. do. So actually no need to do anything. */
  639. LOG (GNUNET_ERROR_TYPE_DEBUG,
  640. "Got duplicate CREATE for %s. MQ is busy, not queueing another ACK\n",
  641. GCC_2s (cc));
  642. }
  643. }
  644. /**
  645. * There has been a change in the message queue existence for our
  646. * peer at the first hop. Adjust accordingly.
  647. *
  648. * @param cls the `struct CadetConnection`
  649. * @param available #GNUNET_YES if sending is now possible,
  650. * #GNUNET_NO if sending is no longer possible
  651. * #GNUNET_SYSERR if sending is no longer possible
  652. * and the last envelope was discarded
  653. */
  654. static void
  655. manage_first_hop_mq (void *cls,
  656. int available)
  657. {
  658. struct CadetConnection *cc = cls;
  659. if (GNUNET_YES != available)
  660. {
  661. /* Connection is down, for now... */
  662. LOG (GNUNET_ERROR_TYPE_DEBUG,
  663. "Core MQ for %s went down\n",
  664. GCC_2s (cc));
  665. update_state (cc,
  666. CADET_CONNECTION_NEW,
  667. GNUNET_NO);
  668. cc->retry_delay = INITIAL_CONNECTION_CREATE_RETRY_DELAY;
  669. if (NULL != cc->task)
  670. {
  671. GNUNET_SCHEDULER_cancel (cc->task);
  672. cc->task = NULL;
  673. }
  674. return;
  675. }
  676. update_state (cc,
  677. cc->state,
  678. GNUNET_YES);
  679. LOG (GNUNET_ERROR_TYPE_DEBUG,
  680. "Core MQ for %s became available in state %d\n",
  681. GCC_2s (cc),
  682. cc->state);
  683. switch (cc->state)
  684. {
  685. case CADET_CONNECTION_NEW:
  686. /* Transmit immediately */
  687. cc->task = GNUNET_SCHEDULER_add_at (cc->create_at,
  688. &send_create,
  689. cc);
  690. break;
  691. case CADET_CONNECTION_SENDING_CREATE:
  692. /* Should not be possible to be called in this state. */
  693. GNUNET_assert (0);
  694. break;
  695. case CADET_CONNECTION_SENT:
  696. /* Retry a bit later... */
  697. cc->task = GNUNET_SCHEDULER_add_at (cc->create_at,
  698. &send_create,
  699. cc);
  700. break;
  701. case CADET_CONNECTION_CREATE_RECEIVED:
  702. /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
  703. cc->metrics.age = GNUNET_TIME_absolute_get ();
  704. cc->task = GNUNET_SCHEDULER_add_at (cc->create_ack_at,
  705. &send_create_ack,
  706. cc);
  707. break;
  708. case CADET_CONNECTION_READY:
  709. if ( (NULL == cc->keepalive_qe) &&
  710. (GNUNET_YES == cc->mqm_ready) &&
  711. (NULL == cc->task) )
  712. {
  713. LOG (GNUNET_ERROR_TYPE_DEBUG,
  714. "Scheduling keepalive for %s in %s\n",
  715. GCC_2s (cc),
  716. GNUNET_STRINGS_relative_time_to_string (keepalive_period,
  717. GNUNET_YES));
  718. cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
  719. &send_keepalive,
  720. cc);
  721. }
  722. break;
  723. }
  724. }
  725. /**
  726. * Create a connection to @a destination via @a path and notify @a cb
  727. * whenever we are ready for more data. Shared logic independent of
  728. * who is initiating the connection.
  729. *
  730. * @param destination where to go
  731. * @param path which path to take (may not be the full path)
  732. * @param off offset of @a destination on @a path
  733. * @param options options for the connection
  734. * @param ct which tunnel uses this connection
  735. * @param init_state initial state for the connection
  736. * @param ready_cb function to call when ready to transmit
  737. * @param ready_cb_cls closure for @a cb
  738. * @return handle to the connection
  739. */
  740. static struct CadetConnection *
  741. connection_create (struct CadetPeer *destination,
  742. struct CadetPeerPath *path,
  743. unsigned int off,
  744. enum GNUNET_CADET_ChannelOption options,
  745. struct CadetTConnection *ct,
  746. const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
  747. enum CadetConnectionState init_state,
  748. GCC_ReadyCallback ready_cb,
  749. void *ready_cb_cls)
  750. {
  751. struct CadetConnection *cc;
  752. struct CadetPeer *first_hop;
  753. cc = GNUNET_new (struct CadetConnection);
  754. cc->options = options;
  755. cc->state = init_state;
  756. cc->ct = ct;
  757. cc->cid = *cid;
  758. cc->retry_delay = GNUNET_TIME_relative_multiply (INITIAL_CONNECTION_CREATE_RETRY_DELAY,
  759. off);
  760. GNUNET_assert (GNUNET_OK ==
  761. GNUNET_CONTAINER_multishortmap_put (connections,
  762. &GCC_get_id (cc)->connection_of_tunnel,
  763. cc,
  764. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  765. cc->ready_cb = ready_cb;
  766. cc->ready_cb_cls = ready_cb_cls;
  767. cc->path = path;
  768. cc->off = off;
  769. LOG (GNUNET_ERROR_TYPE_DEBUG,
  770. "Creating %s using path %s (offset: %u)\n",
  771. GCC_2s (cc),
  772. GCPP_2s (path),
  773. off);
  774. GCPP_add_connection (path,
  775. off,
  776. cc);
  777. for (unsigned int i=0;i<off;i++)
  778. GCP_add_connection (GCPP_get_peer_at_offset (path,
  779. i),
  780. cc);
  781. first_hop = GCPP_get_peer_at_offset (path,
  782. 0);
  783. cc->mq_man = GCP_request_mq (first_hop,
  784. &manage_first_hop_mq,
  785. cc);
  786. return cc;
  787. }
  788. /**
  789. * Create a connection to @a destination via @a path and
  790. * notify @a cb whenever we are ready for more data. This
  791. * is an inbound tunnel, so we must use the existing @a cid
  792. *
  793. * @param destination where to go
  794. * @param path which path to take (may not be the full path)
  795. * @param options options for the connection
  796. * @param ct which tunnel uses this connection
  797. * @param ready_cb function to call when ready to transmit
  798. * @param ready_cb_cls closure for @a cb
  799. * @return handle to the connection, NULL if we already have
  800. * a connection that takes precedence on @a path
  801. */
  802. struct CadetConnection *
  803. GCC_create_inbound (struct CadetPeer *destination,
  804. struct CadetPeerPath *path,
  805. enum GNUNET_CADET_ChannelOption options,
  806. struct CadetTConnection *ct,
  807. const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
  808. GCC_ReadyCallback ready_cb,
  809. void *ready_cb_cls)
  810. {
  811. struct CadetConnection *cc;
  812. unsigned int off;
  813. off = GCPP_find_peer (path,
  814. destination);
  815. GNUNET_assert (UINT_MAX != off);
  816. cc = GCPP_get_connection (path,
  817. destination,
  818. off);
  819. if (NULL != cc)
  820. {
  821. int cmp;
  822. cmp = memcmp (cid,
  823. &cc->cid,
  824. sizeof (*cid));
  825. if (0 == cmp)
  826. {
  827. /* Two peers picked the SAME random connection identifier at the
  828. same time for the same path? Must be malicious. Drop
  829. connection (existing and inbound), even if it is the only
  830. one. */
  831. GNUNET_break_op (0);
  832. GCT_connection_lost (cc->ct);
  833. GCC_destroy_without_tunnel (cc);
  834. return NULL;
  835. }
  836. if (0 < cmp)
  837. {
  838. /* drop existing */
  839. LOG (GNUNET_ERROR_TYPE_DEBUG,
  840. "Got two connections on %s, dropping my existing %s\n",
  841. GCPP_2s (path),
  842. GCC_2s (cc));
  843. GCT_connection_lost (cc->ct);
  844. GCC_destroy_without_tunnel (cc);
  845. }
  846. else
  847. {
  848. /* keep existing */
  849. LOG (GNUNET_ERROR_TYPE_DEBUG,
  850. "Got two connections on %s, keeping my existing %s\n",
  851. GCPP_2s (path),
  852. GCC_2s (cc));
  853. return NULL;
  854. }
  855. }
  856. return connection_create (destination,
  857. path,
  858. off,
  859. options,
  860. ct,
  861. cid,
  862. CADET_CONNECTION_CREATE_RECEIVED,
  863. ready_cb,
  864. ready_cb_cls);
  865. }
  866. /**
  867. * Create a connection to @a destination via @a path and
  868. * notify @a cb whenever we are ready for more data.
  869. *
  870. * @param destination where to go
  871. * @param path which path to take (may not be the full path)
  872. * @param off offset of @a destination on @a path
  873. * @param options options for the connection
  874. * @param ct tunnel that uses the connection
  875. * @param ready_cb function to call when ready to transmit
  876. * @param ready_cb_cls closure for @a cb
  877. * @return handle to the connection
  878. */
  879. struct CadetConnection *
  880. GCC_create (struct CadetPeer *destination,
  881. struct CadetPeerPath *path,
  882. unsigned int off,
  883. enum GNUNET_CADET_ChannelOption options,
  884. struct CadetTConnection *ct,
  885. GCC_ReadyCallback ready_cb,
  886. void *ready_cb_cls)
  887. {
  888. struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
  889. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
  890. &cid,
  891. sizeof (cid));
  892. return connection_create (destination,
  893. path,
  894. off,
  895. options,
  896. ct,
  897. &cid,
  898. CADET_CONNECTION_NEW,
  899. ready_cb,
  900. ready_cb_cls);
  901. }
  902. /**
  903. * Transmit message @a msg via connection @a cc. Must only be called
  904. * (once) after the connection has signalled that it is ready via the
  905. * `ready_cb`. Clients can also use #GCC_is_ready() to check if the
  906. * connection is right now ready for transmission.
  907. *
  908. * @param cc connection identification
  909. * @param env envelope with message to transmit; must NOT
  910. * yet have a #GNUNET_MQ_notify_sent() callback attached to it
  911. */
  912. void
  913. GCC_transmit (struct CadetConnection *cc,
  914. struct GNUNET_MQ_Envelope *env)
  915. {
  916. LOG (GNUNET_ERROR_TYPE_DEBUG,
  917. "Scheduling message for transmission on %s\n",
  918. GCC_2s (cc));
  919. GNUNET_assert (GNUNET_YES == cc->mqm_ready);
  920. GNUNET_assert (CADET_CONNECTION_READY == cc->state);
  921. cc->metrics.last_use = GNUNET_TIME_absolute_get ();
  922. cc->mqm_ready = GNUNET_NO;
  923. if (NULL != cc->task)
  924. {
  925. GNUNET_SCHEDULER_cancel (cc->task);
  926. cc->task = NULL;
  927. }
  928. GCP_send (cc->mq_man,
  929. env);
  930. }
  931. /**
  932. * Obtain the path used by this connection.
  933. *
  934. * @param cc connection
  935. * @param off[out] set to the length of the path we use
  936. * @return path to @a cc
  937. */
  938. struct CadetPeerPath *
  939. GCC_get_path (struct CadetConnection *cc,
  940. unsigned int *off)
  941. {
  942. *off = cc->off;
  943. return cc->path;
  944. }
  945. /**
  946. * Obtain unique ID for the connection.
  947. *
  948. * @param cc connection.
  949. * @return unique number of the connection
  950. */
  951. const struct GNUNET_CADET_ConnectionTunnelIdentifier *
  952. GCC_get_id (struct CadetConnection *cc)
  953. {
  954. return &cc->cid;
  955. }
  956. /**
  957. * Get a (static) string for a connection.
  958. *
  959. * @param cc Connection.
  960. */
  961. const char *
  962. GCC_2s (const struct CadetConnection *cc)
  963. {
  964. static char buf[128];
  965. if (NULL == cc)
  966. return "Connection(NULL)";
  967. if (NULL != cc->ct)
  968. {
  969. GNUNET_snprintf (buf,
  970. sizeof (buf),
  971. "Connection %s (%s)",
  972. GNUNET_sh2s (&cc->cid.connection_of_tunnel),
  973. GCT_2s (cc->ct->t));
  974. return buf;
  975. }
  976. GNUNET_snprintf (buf,
  977. sizeof (buf),
  978. "Connection %s",
  979. GNUNET_sh2s (&cc->cid.connection_of_tunnel));
  980. return buf;
  981. }
  982. #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
  983. /**
  984. * Log connection info.
  985. *
  986. * @param cc connection
  987. * @param level Debug level to use.
  988. */
  989. void
  990. GCC_debug (struct CadetConnection *cc,
  991. enum GNUNET_ErrorType level)
  992. {
  993. #if !defined(GNUNET_CULL_LOGGING)
  994. int do_log;
  995. do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
  996. "cadet-con",
  997. __FILE__, __FUNCTION__, __LINE__);
  998. if (0 == do_log)
  999. return;
  1000. if (NULL == cc)
  1001. {
  1002. LOG2 (level,
  1003. "Connection (NULL)\n");
  1004. return;
  1005. }
  1006. LOG2 (level,
  1007. "%s to %s via path %s in state %d is %s\n",
  1008. GCC_2s (cc),
  1009. GCP_2s (cc->destination),
  1010. GCPP_2s (cc->path),
  1011. cc->state,
  1012. (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy");
  1013. #endif
  1014. }
  1015. /* end of gnunet-service-cadet_connection.c */