circuituse.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuituse_c_id[] = "$Id$";
  7. /**
  8. * \file circuituse.c
  9. * \brief Launch the right sort of circuits, attach streams to them.
  10. **/
  11. #include "or.h"
  12. /** Longest time to wait for a circuit before closing an AP connection */
  13. #define CONN_AP_MAX_ATTACH_DELAY 59
  14. /********* START VARIABLES **********/
  15. extern circuit_t *global_circuitlist; /* from circuitlist.c */
  16. extern int has_fetched_directory; /* from main.c */
  17. /********* END VARIABLES ************/
  18. static void circuit_expire_old_circuits(void);
  19. static void circuit_increment_failure_count(void);
  20. /* Return 1 if <b>circ</b> could be returned by circuit_get_best().
  21. * Else return 0.
  22. */
  23. static int circuit_is_acceptable(circuit_t *circ,
  24. connection_t *conn,
  25. int must_be_open,
  26. uint8_t purpose,
  27. time_t now)
  28. {
  29. routerinfo_t *exitrouter;
  30. tor_assert(circ);
  31. tor_assert(conn);
  32. tor_assert(conn->socks_request);
  33. if (!CIRCUIT_IS_ORIGIN(circ))
  34. return 0; /* this circ doesn't start at us */
  35. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  36. return 0; /* ignore non-open circs */
  37. if (circ->marked_for_close)
  38. return 0;
  39. /* if this circ isn't our purpose, skip. */
  40. if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  41. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  42. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  43. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
  44. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  45. return 0;
  46. } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT && !must_be_open) {
  47. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  48. circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  49. return 0;
  50. } else {
  51. if (purpose != circ->purpose)
  52. return 0;
  53. }
  54. if (purpose == CIRCUIT_PURPOSE_C_GENERAL)
  55. if (circ->timestamp_dirty &&
  56. circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
  57. return 0;
  58. /* decide if this circ is suitable for this conn */
  59. /* for rend circs, circ->cpath->prev is not the last router in the
  60. * circuit, it's the magical extra bob hop. so just check the nickname
  61. * of the one we meant to finish at.
  62. */
  63. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  64. if (!exitrouter) {
  65. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  66. return 0; /* this circuit is screwed and doesn't know it yet */
  67. }
  68. if (!circ->build_state->need_uptime &&
  69. smartlist_string_num_isin(get_options()->LongLivedPorts,
  70. conn->socks_request->port))
  71. return 0;
  72. if (purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  73. if (!connection_ap_can_use_exit(conn, exitrouter)) {
  74. /* can't exit from this router */
  75. return 0;
  76. }
  77. } else { /* not general */
  78. if (rend_cmp_service_ids(conn->rend_query, circ->rend_query)) {
  79. /* this circ is not for this conn */
  80. return 0;
  81. }
  82. }
  83. return 1;
  84. }
  85. /* Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  86. * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
  87. */
  88. static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
  89. {
  90. switch (purpose) {
  91. case CIRCUIT_PURPOSE_C_GENERAL:
  92. /* if it's used but less dirty it's best;
  93. * else if it's more recently created it's best
  94. */
  95. if (b->timestamp_dirty) {
  96. if (a->timestamp_dirty &&
  97. a->timestamp_dirty > b->timestamp_dirty)
  98. return 1;
  99. } else {
  100. if (a->timestamp_dirty ||
  101. b->build_state->is_internal ||
  102. a->timestamp_created > b->timestamp_created)
  103. return 1;
  104. }
  105. break;
  106. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  107. /* the closer it is to ack_wait the better it is */
  108. if (a->purpose > b->purpose)
  109. return 1;
  110. break;
  111. case CIRCUIT_PURPOSE_C_REND_JOINED:
  112. /* the closer it is to rend_joined the better it is */
  113. if (a->purpose > b->purpose)
  114. return 1;
  115. break;
  116. }
  117. return 0;
  118. }
  119. /** Find the best circ that conn can use, preferably one which is
  120. * dirty. Circ must not be too old.
  121. *
  122. * Conn must be defined.
  123. *
  124. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  125. *
  126. * circ_purpose specifies what sort of circuit we must have.
  127. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  128. *
  129. * If it's REND_JOINED and must_be_open==0, then return the closest
  130. * rendezvous-purposed circuit that you can find.
  131. *
  132. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  133. * closest introduce-purposed circuit that you can find.
  134. */
  135. static circuit_t *
  136. circuit_get_best(connection_t *conn, int must_be_open, uint8_t purpose)
  137. {
  138. circuit_t *circ, *best=NULL;
  139. time_t now = time(NULL);
  140. tor_assert(conn);
  141. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  142. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  143. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  144. for (circ=global_circuitlist;circ;circ = circ->next) {
  145. if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
  146. continue;
  147. /* now this is an acceptable circ to hand back. but that doesn't
  148. * mean it's the *best* circ to hand back. try to decide.
  149. */
  150. if (!best || circuit_is_better(circ,best,purpose))
  151. best = circ;
  152. }
  153. return best;
  154. }
  155. /** If we find a circuit that isn't open yet and was born this many
  156. * seconds ago, then assume something went wrong, and cull it.
  157. */
  158. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 30
  159. /** Close all circuits that start at us, aren't open, and were born
  160. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago.
  161. */
  162. void circuit_expire_building(time_t now) {
  163. circuit_t *victim, *circ = global_circuitlist;
  164. while (circ) {
  165. victim = circ;
  166. circ = circ->next;
  167. if (!CIRCUIT_IS_ORIGIN(victim))
  168. continue; /* didn't originate here */
  169. if (victim->marked_for_close)
  170. continue; /* don't mess with marked circs */
  171. if (victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
  172. continue; /* it's young still, don't mess with it */
  173. #if 0
  174. /* some debug logs, to help track bugs */
  175. if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  176. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  177. if (!victim->timestamp_dirty)
  178. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). (clean).",
  179. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  180. victim->purpose, victim->build_state->chosen_exit_name,
  181. victim->n_circ_id);
  182. else
  183. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). %d secs since dirty.",
  184. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  185. victim->purpose, victim->build_state->chosen_exit_name,
  186. victim->n_circ_id,
  187. (int)(now - victim->timestamp_dirty));
  188. }
  189. #endif
  190. /* if circ is !open, or if it's open but purpose is a non-finished
  191. * intro or rend, then mark it for close */
  192. if (victim->state != CIRCUIT_STATE_OPEN ||
  193. victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  194. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  195. victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  196. /* it's a rend_ready circ, but it's already picked a query */
  197. (victim->purpose == CIRCUIT_PURPOSE_C_REND_READY &&
  198. victim->rend_query[0]) ||
  199. /* c_rend_ready circs measure age since timestamp_dirty,
  200. * because that's set when they switch purposes
  201. */
  202. /* rend and intro circs become dirty each time they
  203. * make an introduction attempt. so timestamp_dirty
  204. * will reflect the time since the last attempt.
  205. */
  206. ((victim->purpose == CIRCUIT_PURPOSE_C_REND_READY ||
  207. victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED ||
  208. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
  209. victim->timestamp_dirty + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)) {
  210. if (victim->n_conn)
  211. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
  212. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  213. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  214. else
  215. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s, purpose %d)", victim->n_circ_id,
  216. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  217. circuit_log_path(LOG_INFO,victim);
  218. circuit_mark_for_close(victim);
  219. }
  220. }
  221. }
  222. /** Remove any elements in <b>needed_ports</b> that are handled by an
  223. * open or in-progress circuit.
  224. */
  225. void
  226. circuit_remove_handled_ports(smartlist_t *needed_ports) {
  227. int i;
  228. uint16_t *port;
  229. for (i = 0; i < smartlist_len(needed_ports); ++i) {
  230. port = smartlist_get(needed_ports, i);
  231. tor_assert(*port);
  232. if (circuit_stream_is_being_handled(NULL, *port, 2)) {
  233. // log_fn(LOG_DEBUG,"Port %d is already being handled; removing.", port);
  234. smartlist_del(needed_ports, i--);
  235. tor_free(port);
  236. } else {
  237. log_fn(LOG_DEBUG,"Port %d is not handled.", *port);
  238. }
  239. }
  240. }
  241. /** Return 1 if at least <b>min</b> general-purpose circuits will have
  242. * an acceptable exit node for conn if conn is defined, else for "*:port".
  243. * Else return 0.
  244. */
  245. int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min) {
  246. circuit_t *circ;
  247. routerinfo_t *exitrouter;
  248. int num=0;
  249. time_t now = time(NULL);
  250. int need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts,
  251. conn ? conn->socks_request->port : port);
  252. for (circ=global_circuitlist;circ;circ = circ->next) {
  253. if (CIRCUIT_IS_ORIGIN(circ) &&
  254. !circ->marked_for_close &&
  255. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  256. (!circ->timestamp_dirty ||
  257. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness < now)) {
  258. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  259. if (exitrouter &&
  260. (!need_uptime || circ->build_state->need_uptime)) {
  261. int ok;
  262. if (conn) {
  263. ok = connection_ap_can_use_exit(conn, exitrouter);
  264. } else {
  265. addr_policy_result_t r =
  266. router_compare_addr_to_addr_policy(0, port, exitrouter->exit_policy);
  267. ok = r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED;
  268. }
  269. if (ok) {
  270. if (++num >= min)
  271. return 1;
  272. }
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. /** Don't keep more than 10 unused open circuits around. */
  279. #define MAX_UNUSED_OPEN_CIRCUITS 10
  280. /** Figure out how many circuits we have open that are clean. Make
  281. * sure it's enough for all the upcoming behaviors we predict we'll have.
  282. * But if we have too many, close the not-so-useful ones.
  283. */
  284. static void
  285. circuit_predict_and_launch_new(void)
  286. {
  287. circuit_t *circ;
  288. int num=0, num_internal=0, num_uptime_internal=0;
  289. int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
  290. int port_needs_uptime=0, port_needs_capacity=1;
  291. int need_ports, need_hidserv;
  292. time_t now = time(NULL);
  293. /* check if we know of a port that's been requested recently
  294. * and no circuit is currently available that can handle it. */
  295. need_ports = !circuit_all_predicted_ports_handled(now, &port_needs_uptime,
  296. &port_needs_capacity);
  297. need_hidserv = rep_hist_get_predicted_hidserv(now, &hidserv_needs_uptime,
  298. &hidserv_needs_capacity);
  299. for (circ=global_circuitlist;circ;circ = circ->next) {
  300. if (!CIRCUIT_IS_ORIGIN(circ))
  301. continue;
  302. if (circ->marked_for_close)
  303. continue; /* don't mess with marked circs */
  304. if (circ->timestamp_dirty)
  305. continue; /* only count clean circs */
  306. if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL)
  307. continue; /* only pay attention to general-purpose circs */
  308. num++;
  309. if (circ->build_state->is_internal)
  310. num_internal++;
  311. if (circ->build_state->need_uptime && circ->build_state->is_internal)
  312. num_uptime_internal++;
  313. }
  314. if (num < MAX_UNUSED_OPEN_CIRCUITS) {
  315. /* perhaps we want another */
  316. if (need_ports) {
  317. log_fn(LOG_INFO,"Have %d clean circs (%d internal), need another exit circ.",
  318. num, num_internal);
  319. circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL,
  320. port_needs_uptime, port_needs_capacity, 0);
  321. } else if (need_hidserv &&
  322. ((num_uptime_internal<2 && hidserv_needs_uptime) ||
  323. num_internal<2)) {
  324. log_fn(LOG_INFO,"Have %d clean circs (%d uptime-internal, %d internal),"
  325. " need another hidserv circ.", num, num_uptime_internal, num_internal);
  326. circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL,
  327. hidserv_needs_uptime, hidserv_needs_capacity, 1);
  328. }
  329. }
  330. }
  331. /** Build a new test circuit every 5 minutes */
  332. #define TESTING_CIRCUIT_INTERVAL 300
  333. /** This function is called once a second. Its job is to make sure
  334. * all services we offer have enough circuits available. Some
  335. * services just want enough circuits for current tasks, whereas
  336. * others want a minimum set of idle circuits hanging around.
  337. */
  338. void circuit_build_needed_circs(time_t now) {
  339. static long time_to_new_circuit = 0;
  340. /* launch a new circ for any pending streams that need one */
  341. connection_ap_attach_pending();
  342. /* make sure any hidden services have enough intro points */
  343. if (has_fetched_directory)
  344. rend_services_introduce();
  345. if (time_to_new_circuit < now) {
  346. circuit_reset_failure_count(1);
  347. time_to_new_circuit = now + get_options()->NewCircuitPeriod;
  348. if (proxy_mode(get_options()))
  349. addressmap_clean(now);
  350. circuit_expire_old_circuits();
  351. #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
  352. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  353. if (get_options()->RunTesting &&
  354. circ &&
  355. circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
  356. log_fn(LOG_INFO,"Creating a new testing circuit.");
  357. circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL, NULL, 0, 0, 0);
  358. }
  359. #endif
  360. }
  361. circuit_predict_and_launch_new();
  362. }
  363. /** If the stream <b>conn</b> is a member of any of the linked
  364. * lists of <b>circ</b>, then remove it from the list.
  365. */
  366. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  367. connection_t *prevconn;
  368. tor_assert(circ);
  369. tor_assert(conn);
  370. conn->cpath_layer = NULL; /* make sure we don't keep a stale pointer */
  371. conn->on_circuit = NULL;
  372. if (conn == circ->p_streams) {
  373. circ->p_streams = conn->next_stream;
  374. return;
  375. }
  376. if (conn == circ->n_streams) {
  377. circ->n_streams = conn->next_stream;
  378. return;
  379. }
  380. if (conn == circ->resolving_streams) {
  381. circ->resolving_streams = conn->next_stream;
  382. return;
  383. }
  384. for (prevconn = circ->p_streams;
  385. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  386. prevconn = prevconn->next_stream)
  387. ;
  388. if (prevconn && prevconn->next_stream) {
  389. prevconn->next_stream = conn->next_stream;
  390. return;
  391. }
  392. for (prevconn = circ->n_streams;
  393. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  394. prevconn = prevconn->next_stream)
  395. ;
  396. if (prevconn && prevconn->next_stream) {
  397. prevconn->next_stream = conn->next_stream;
  398. return;
  399. }
  400. for (prevconn = circ->resolving_streams;
  401. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  402. prevconn = prevconn->next_stream)
  403. ;
  404. if (prevconn && prevconn->next_stream) {
  405. prevconn->next_stream = conn->next_stream;
  406. return;
  407. }
  408. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  409. tor_assert(0); /* should never get here */
  410. }
  411. /** Notify the global circuit list that <b>conn</b> is about to be
  412. * removed and then freed.
  413. *
  414. * If it's an OR conn, then mark-for-close all the circuits that use
  415. * that conn.
  416. *
  417. * If it's an edge conn, then detach it from its circ, so we don't
  418. * try to reference it later.
  419. */
  420. void circuit_about_to_close_connection(connection_t *conn) {
  421. /* currently, we assume it's too late to flush conn's buf here.
  422. * down the road, maybe we'll consider that eof doesn't mean can't-write
  423. */
  424. circuit_t *circ;
  425. switch (conn->type) {
  426. case CONN_TYPE_OR:
  427. /* Inform any pending (not attached) circs that they should give up. */
  428. circuit_n_conn_done(conn, 0);
  429. /* Now close all the attached circuits on it. */
  430. while ((circ = circuit_get_by_conn(conn))) {
  431. if (circ->n_conn == conn)
  432. /* it's closing in front of us */
  433. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  434. if (circ->p_conn == conn)
  435. /* it's closing behind us */
  436. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  437. circuit_mark_for_close(circ);
  438. }
  439. return;
  440. case CONN_TYPE_AP:
  441. case CONN_TYPE_EXIT:
  442. /* It's an edge conn. Need to remove it from the linked list of
  443. * conn's for this circuit. Confirm that 'end' relay command has
  444. * been sent. But don't kill the circuit.
  445. */
  446. circ = circuit_get_by_edge_conn(conn);
  447. if (!circ)
  448. return;
  449. circuit_detach_stream(circ, conn);
  450. } /* end switch */
  451. }
  452. /** Find each circuit that has been dirty for too long, and has
  453. * no streams on it: mark it for close.
  454. */
  455. static void
  456. circuit_expire_old_circuits(void)
  457. {
  458. circuit_t *circ;
  459. time_t now = time(NULL);
  460. for (circ = global_circuitlist; circ; circ = circ->next) {
  461. if (circ->marked_for_close)
  462. continue;
  463. /* If the circuit has been dirty for too long, and there are no streams
  464. * on it, mark it for close.
  465. */
  466. if (circ->timestamp_dirty &&
  467. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness < now &&
  468. CIRCUIT_IS_ORIGIN(circ) &&
  469. !circ->p_streams /* nothing attached */ ) {
  470. log_fn(LOG_DEBUG,"Closing n_circ_id %d (dirty %d secs ago, purp %d)",circ->n_circ_id,
  471. (int)(now - circ->timestamp_dirty), circ->purpose);
  472. /* (only general and purpose_c circs can get dirty) */
  473. tor_assert(!circ->n_streams);
  474. tor_assert(circ->purpose <= CIRCUIT_PURPOSE_C_REND_JOINED);
  475. circuit_mark_for_close(circ);
  476. } else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
  477. circ->state == CIRCUIT_STATE_OPEN &&
  478. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  479. #define CIRCUIT_UNUSED_CIRC_TIMEOUT 3600 /* an hour */
  480. if (circ->timestamp_created + CIRCUIT_UNUSED_CIRC_TIMEOUT < now) {
  481. log_fn(LOG_DEBUG,"Closing circuit that has been unused for %d seconds.",
  482. (int)(now - circ->timestamp_created));
  483. circuit_mark_for_close(circ);
  484. }
  485. }
  486. }
  487. }
  488. /** A testing circuit has completed. Take whatever stats we want. */
  489. static void
  490. circuit_testing_opened(circuit_t *circ) {
  491. /* For now, we only use testing circuits to see if our ORPort is
  492. reachable. But we remember reachability in onionskin_answer(),
  493. so there's no need to record anything here. Just close the circ. */
  494. circuit_mark_for_close(circ);
  495. }
  496. /** A testing circuit has failed to build. Take whatever stats we want. */
  497. static void
  498. circuit_testing_failed(circuit_t *circ, int at_last_hop) {
  499. #if 0
  500. routerinfo_t *me = router_get_my_routerinfo();
  501. if (!at_last_hop)
  502. circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, me, 0, 1, 1);
  503. else
  504. #endif
  505. log_fn(LOG_INFO,"Our testing circuit (to see if your ORPort is reachable) has failed. I'll try again later.");
  506. }
  507. /** The circuit <b>circ</b> has just become open. Take the next
  508. * step: for rendezvous circuits, we pass circ to the appropriate
  509. * function in rendclient or rendservice. For general circuits, we
  510. * call connection_ap_attach_pending, which looks for pending streams
  511. * that could use circ.
  512. */
  513. void circuit_has_opened(circuit_t *circ) {
  514. control_event_circuit_status(circ, CIRC_EVENT_BUILT);
  515. switch (circ->purpose) {
  516. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  517. rend_client_rendcirc_has_opened(circ);
  518. connection_ap_attach_pending();
  519. break;
  520. case CIRCUIT_PURPOSE_C_INTRODUCING:
  521. rend_client_introcirc_has_opened(circ);
  522. break;
  523. case CIRCUIT_PURPOSE_C_GENERAL:
  524. /* Tell any AP connections that have been waiting for a new
  525. * circuit that one is ready. */
  526. connection_ap_attach_pending();
  527. break;
  528. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  529. /* at Bob, waiting for introductions */
  530. rend_service_intro_has_opened(circ);
  531. break;
  532. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  533. /* at Bob, connecting to rend point */
  534. rend_service_rendezvous_has_opened(circ);
  535. break;
  536. case CIRCUIT_PURPOSE_TESTING:
  537. circuit_testing_opened(circ);
  538. break;
  539. default:
  540. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  541. tor_assert(0);
  542. }
  543. }
  544. /*~ Called whenever a circuit could not be successfully built.
  545. */
  546. void circuit_build_failed(circuit_t *circ) {
  547. /* we should examine circ and see if it failed because of
  548. * the last hop or an earlier hop. then use this info below.
  549. */
  550. int failed_at_last_hop = 0;
  551. /* If the last hop isn't open, and the second-to-last is, we failed
  552. * at the last hop. */
  553. if (circ->cpath &&
  554. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  555. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  556. failed_at_last_hop = 1;
  557. }
  558. switch (circ->purpose) {
  559. case CIRCUIT_PURPOSE_C_GENERAL:
  560. if (circ->state != CIRCUIT_STATE_OPEN) {
  561. /* If we never built the circuit, note it as a failure. */
  562. /* Note that we can't just check circ->cpath here, because if
  563. * circuit-building failed immediately, it won't be set yet. */
  564. circuit_increment_failure_count();
  565. }
  566. break;
  567. case CIRCUIT_PURPOSE_TESTING:
  568. circuit_testing_failed(circ, failed_at_last_hop);
  569. break;
  570. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  571. /* at Bob, waiting for introductions */
  572. if (circ->state != CIRCUIT_STATE_OPEN) {
  573. circuit_increment_failure_count();
  574. }
  575. /* no need to care here, because bob will rebuild intro
  576. * points periodically. */
  577. break;
  578. case CIRCUIT_PURPOSE_C_INTRODUCING:
  579. /* at Alice, connecting to intro point */
  580. /* Don't increment failure count, since Bob may have picked
  581. * the introduction point maliciously */
  582. /* Alice will pick a new intro point when this one dies, if
  583. * the stream in question still cares. No need to act here. */
  584. break;
  585. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  586. /* at Alice, waiting for Bob */
  587. if (circ->state != CIRCUIT_STATE_OPEN) {
  588. circuit_increment_failure_count();
  589. }
  590. /* Alice will pick a new rend point when this one dies, if
  591. * the stream in question still cares. No need to act here. */
  592. break;
  593. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  594. /* at Bob, connecting to rend point */
  595. /* Don't increment failure count, since Alice may have picked
  596. * the rendezvous point maliciously */
  597. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s (%s hop failed).",
  598. failed_at_last_hop?"last":"non-last",
  599. circ->build_state->chosen_exit_name);
  600. rend_service_relaunch_rendezvous(circ);
  601. break;
  602. default:
  603. /* Other cases are impossible, since this function is only called with
  604. * unbuilt circuits. */
  605. tor_assert(0);
  606. }
  607. }
  608. /** Number of consecutive failures so far; should only be touched by
  609. * circuit_launch_new and circuit_*_failure_count.
  610. */
  611. static int n_circuit_failures = 0;
  612. static int did_circs_fail_last_period = 0;
  613. /** Don't retry launching a new circuit if we try this many times with no
  614. * success. */
  615. #define MAX_CIRCUIT_FAILURES 5
  616. /** Launch a new circuit based on our arguments. */
  617. circuit_t *
  618. circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
  619. int need_uptime, int need_capacity, int internal)
  620. {
  621. circuit_t *circ;
  622. if (!has_fetched_directory) {
  623. log_fn(LOG_DEBUG,"Haven't fetched directory yet; canceling circuit launch.");
  624. return NULL;
  625. }
  626. if (purpose != CIRCUIT_PURPOSE_C_GENERAL &&
  627. purpose != CIRCUIT_PURPOSE_TESTING) {
  628. /* see if there are appropriate circs available to cannibalize. */
  629. if ((circ = circuit_get_clean_open(CIRCUIT_PURPOSE_C_GENERAL, need_uptime,
  630. need_capacity, internal))) {
  631. log_fn(LOG_INFO,"Cannibalizing circ '%s' for purpose %d",
  632. circ->build_state->chosen_exit_name, purpose);
  633. circ->purpose = purpose;
  634. /* reset the birth date of this circ, else expire_building
  635. * will see it and think it's been trying to build since it
  636. * began. */
  637. circ->timestamp_created = time(NULL);
  638. switch (purpose) {
  639. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  640. /* it's ready right now */
  641. /* XXX should we call control_event_circuit_status() here? */
  642. rend_client_rendcirc_has_opened(circ);
  643. break;
  644. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  645. /* it's ready right now */
  646. rend_service_intro_has_opened(circ);
  647. break;
  648. case CIRCUIT_PURPOSE_C_INTRODUCING:
  649. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  650. /* need to add a new hop */
  651. tor_assert(exit);
  652. if (circuit_extend_to_new_exit(circ, exit) < 0)
  653. return NULL;
  654. break;
  655. default:
  656. log_fn(LOG_WARN,"Bug: unexpected purpose %d when cannibalizing a general circ.",
  657. purpose);
  658. #ifdef TOR_FRAGILE
  659. tor_assert(0);
  660. #endif
  661. return NULL;
  662. }
  663. return circ;
  664. }
  665. }
  666. if (did_circs_fail_last_period &&
  667. n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  668. /* too many failed circs in a row. don't try. */
  669. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  670. return NULL;
  671. }
  672. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  673. return circuit_establish_circuit(purpose, exit,
  674. need_uptime, need_capacity, internal);
  675. }
  676. /** Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  677. circuit_t *
  678. circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
  679. int need_uptime, int need_capacity, int internal)
  680. {
  681. routerinfo_t *router = NULL;
  682. if (exit_nickname) {
  683. router = router_get_by_nickname(exit_nickname);
  684. if (!router) {
  685. log_fn(LOG_WARN, "No such OR as '%s'", exit_nickname);
  686. return NULL;
  687. }
  688. }
  689. return circuit_launch_by_router(purpose, router,
  690. need_uptime, need_capacity, internal);
  691. }
  692. /** Record another failure at opening a general circuit. When we have
  693. * too many, we'll stop trying for the remainder of this minute.
  694. */
  695. static void circuit_increment_failure_count(void) {
  696. ++n_circuit_failures;
  697. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  698. }
  699. /** Reset the failure count for opening general circuits. This means
  700. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  701. * stopping again.
  702. */
  703. void circuit_reset_failure_count(int timeout) {
  704. if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
  705. did_circs_fail_last_period = 1;
  706. else
  707. did_circs_fail_last_period = 0;
  708. n_circuit_failures = 0;
  709. }
  710. /** Find an open circ that we're happy with: return 1. If there isn't
  711. * one, and there isn't one on the way, launch one and return 0. If it
  712. * will never work, return -1.
  713. *
  714. * Write the found or in-progress or launched circ into *circp.
  715. */
  716. static int
  717. circuit_get_open_circ_or_launch(connection_t *conn,
  718. uint8_t desired_circuit_purpose,
  719. circuit_t **circp) {
  720. circuit_t *circ;
  721. int is_resolve;
  722. int need_uptime;
  723. tor_assert(conn);
  724. tor_assert(circp);
  725. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  726. is_resolve = conn->socks_request->command == SOCKS_COMMAND_RESOLVE;
  727. circ = circuit_get_best(conn, 1, desired_circuit_purpose);
  728. if (circ) {
  729. *circp = circ;
  730. return 1; /* we're happy */
  731. }
  732. if (!has_fetched_directory) {
  733. if (!connection_get_by_type(CONN_TYPE_DIR)) {
  734. log_fn(LOG_NOTICE,"Application request when we're believed to be offline. Optimistically trying again.");
  735. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
  736. }
  737. /* the stream will be dealt with when has_fetched_directory becomes
  738. * 1, or when all directory attempts fail and directory_all_unreachable()
  739. * kills it.
  740. */
  741. return 0;
  742. }
  743. need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts,
  744. conn->socks_request->port);
  745. /* Do we need to check exit policy? */
  746. if (!is_resolve && !connection_edge_is_rendezvous_stream(conn)) {
  747. struct in_addr in;
  748. uint32_t addr = 0;
  749. if (tor_inet_aton(conn->socks_request->address, &in))
  750. addr = ntohl(in.s_addr);
  751. if (router_exit_policy_all_routers_reject(addr, conn->socks_request->port,
  752. need_uptime)) {
  753. log_fn(LOG_NOTICE,"No Tor server exists that allows exit to %s:%d. Rejecting.",
  754. conn->socks_request->address, conn->socks_request->port);
  755. return -1;
  756. }
  757. }
  758. /* is one already on the way? */
  759. circ = circuit_get_best(conn, 0, desired_circuit_purpose);
  760. if (!circ) {
  761. char *exitname=NULL;
  762. uint8_t new_circ_purpose;
  763. int is_internal;
  764. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  765. /* need to pick an intro point */
  766. try_an_intro_point:
  767. exitname = rend_client_get_random_intro(conn->rend_query);
  768. if (!exitname) {
  769. log_fn(LOG_INFO,"No intro points for '%s': refetching service descriptor.",
  770. conn->rend_query);
  771. rend_client_refetch_renddesc(conn->rend_query);
  772. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  773. return 0;
  774. }
  775. if (!router_get_by_nickname(exitname)) {
  776. log_fn(LOG_NOTICE,"Advertised intro point '%s' is not recognized for '%s'. Skipping over.",
  777. exitname, conn->rend_query);
  778. rend_client_remove_intro_point(exitname, conn->rend_query);
  779. tor_free(exitname);
  780. goto try_an_intro_point;
  781. }
  782. log_fn(LOG_INFO,"Chose %s as intro point for %s.", exitname, conn->rend_query);
  783. }
  784. /* If we have specified a particular exit node for our
  785. * connection, then be sure to open a circuit to that exit node.
  786. */
  787. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  788. if (conn->chosen_exit_name) {
  789. exitname = tor_strdup(conn->chosen_exit_name);
  790. if (!router_get_by_nickname(exitname)) {
  791. log_fn(LOG_NOTICE,"Requested exit point '%s' is not known. Closing.", exitname);
  792. tor_free(exitname);
  793. return -1;
  794. }
  795. }
  796. }
  797. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  798. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  799. else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  800. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  801. else
  802. new_circ_purpose = desired_circuit_purpose;
  803. is_internal = (new_circ_purpose != CIRCUIT_PURPOSE_C_GENERAL || is_resolve);
  804. circ = circuit_launch_by_nickname(new_circ_purpose, exitname, need_uptime,
  805. 1, is_internal);
  806. tor_free(exitname);
  807. if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL) {
  808. /* help predict this next time */
  809. rep_hist_note_used_hidserv(time(NULL), need_uptime, 1);
  810. if (circ) {
  811. /* write the service_id into circ */
  812. strlcpy(circ->rend_query, conn->rend_query, sizeof(circ->rend_query));
  813. }
  814. }
  815. }
  816. if (!circ)
  817. log_fn(LOG_INFO,"No safe circuit (purpose %d) ready for edge connection; delaying.",
  818. desired_circuit_purpose);
  819. *circp = circ;
  820. return 0;
  821. }
  822. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  823. * p_streams. Also set apconn's cpath_layer to the last hop in
  824. * circ's cpath.
  825. */
  826. static void link_apconn_to_circ(connection_t *apconn, circuit_t *circ) {
  827. /* add it into the linked list of streams on this circuit */
  828. log_fn(LOG_DEBUG,"attaching new conn to circ. n_circ_id %d.", circ->n_circ_id);
  829. apconn->timestamp_lastread = time(NULL); /* reset it, so we can measure circ timeouts */
  830. apconn->next_stream = circ->p_streams;
  831. apconn->on_circuit = circ;
  832. /* assert_connection_ok(conn, time(NULL)); */
  833. circ->p_streams = apconn;
  834. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  835. tor_assert(circ->cpath);
  836. tor_assert(circ->cpath->prev);
  837. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  838. apconn->cpath_layer = circ->cpath->prev;
  839. }
  840. /** If an exit wasn't specifically chosen, save the history for future
  841. * use */
  842. static void
  843. consider_recording_trackhost(connection_t *conn, circuit_t *circ) {
  844. int found_needle = 0;
  845. char *str;
  846. or_options_t *options = get_options();
  847. size_t len;
  848. char *new_address;
  849. /* Search the addressmap for this conn's destination. */
  850. /* If he's not in the address map.. */
  851. if (!options->TrackHostExits ||
  852. addressmap_already_mapped(conn->socks_request->address))
  853. return; /* nothing to track, or already mapped */
  854. SMARTLIST_FOREACH(options->TrackHostExits, const char *, cp, {
  855. if (cp[0] == '.') { /* match end */
  856. /* XXX strstr is probably really bad here */
  857. if ((str = strstr(conn->socks_request->address, &cp[1]))) {
  858. if (str == conn->socks_request->address
  859. || strcmp(str, &cp[1]) == 0) {
  860. found_needle = 1;
  861. }
  862. }
  863. } else if (strcmp(cp, conn->socks_request->address) == 0) {
  864. found_needle = 1;
  865. }
  866. });
  867. if (!found_needle)
  868. return;
  869. /* Add this exit/hostname pair to the addressmap. */
  870. len = strlen(conn->socks_request->address) + 1 /* '.' */ +
  871. strlen(circ->build_state->chosen_exit_name) + 1 /* '.' */ +
  872. strlen("exit") + 1 /* '\0' */;
  873. new_address = tor_malloc(len);
  874. tor_snprintf(new_address, len, "%s.%s.exit",
  875. conn->socks_request->address,
  876. circ->build_state->chosen_exit_name);
  877. addressmap_register(conn->socks_request->address, new_address,
  878. time(NULL) + options->TrackHostExitsExpire);
  879. }
  880. /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and
  881. * send a begin or resolve cell as appropriate. Return values are as
  882. * for connection_ap_handshake_attach_circuit. */
  883. int
  884. connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
  885. circuit_t *circ)
  886. {
  887. tor_assert(conn);
  888. tor_assert(conn->type == CONN_TYPE_AP);
  889. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT ||
  890. conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
  891. tor_assert(conn->socks_request);
  892. tor_assert(circ);
  893. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  894. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  895. if (!circ->timestamp_dirty)
  896. circ->timestamp_dirty = time(NULL);
  897. link_apconn_to_circ(conn, circ);
  898. tor_assert(conn->socks_request);
  899. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
  900. consider_recording_trackhost(conn, circ);
  901. if (connection_ap_handshake_send_begin(conn, circ)<0)
  902. return -1;
  903. } else {
  904. if (connection_ap_handshake_send_resolve(conn, circ)<0)
  905. return -1;
  906. }
  907. return 1;
  908. }
  909. /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  910. * we don't find one: if conn cannot be handled by any known nodes,
  911. * warn and return -1 (conn needs to die);
  912. * else launch new circuit (if necessary) and return 0.
  913. * Otherwise, associate conn with a safe live circuit, do the
  914. * right next step, and return 1.
  915. */
  916. int connection_ap_handshake_attach_circuit(connection_t *conn) {
  917. int retval;
  918. int conn_age;
  919. tor_assert(conn);
  920. tor_assert(conn->type == CONN_TYPE_AP);
  921. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  922. tor_assert(conn->socks_request);
  923. conn_age = time(NULL) - conn->timestamp_created;
  924. if (conn_age > CONN_AP_MAX_ATTACH_DELAY) {
  925. log_fn(LOG_NOTICE,"Giving up on unattached conn (%d sec old).", conn_age);
  926. return -1;
  927. }
  928. if (!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
  929. circuit_t *circ=NULL;
  930. if (conn->chosen_exit_name) {
  931. routerinfo_t *router = router_get_by_nickname(conn->chosen_exit_name);
  932. if (!router) {
  933. log_fn(LOG_WARN,"Requested exit point '%s' is not known. Closing.",
  934. conn->chosen_exit_name);
  935. return -1;
  936. }
  937. if (!connection_ap_can_use_exit(conn, router)) {
  938. log_fn(LOG_WARN, "Requested exit point '%s' would refuse request. Closing.",
  939. conn->chosen_exit_name);
  940. return -1;
  941. }
  942. }
  943. /* find the circuit that we should use, if there is one. */
  944. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  945. if (retval < 1)
  946. return retval;
  947. log_fn(LOG_DEBUG,"Attaching apconn to circ %d (stream %d sec old).",
  948. circ->n_circ_id, conn_age);
  949. /* here, print the circ's path. so people can figure out which circs are sucking. */
  950. circuit_log_path(LOG_INFO,circ);
  951. /* We have found a suitable circuit for our conn. Hurray. */
  952. return connection_ap_handshake_attach_chosen_circuit(conn, circ);
  953. } else { /* we're a rendezvous conn */
  954. circuit_t *rendcirc=NULL, *introcirc=NULL;
  955. tor_assert(!conn->cpath_layer);
  956. /* start by finding a rendezvous circuit for us */
  957. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  958. if (retval < 0) return -1; /* failed */
  959. if (retval > 0) {
  960. tor_assert(rendcirc);
  961. /* one is already established, attach */
  962. log_fn(LOG_INFO,"rend joined circ %d already here. attaching. (stream %d sec old)",
  963. rendcirc->n_circ_id, conn_age);
  964. /* Mark rendezvous circuits as 'newly dirty' every time you use
  965. * them, since the process of rebuilding a rendezvous circ is so
  966. * expensive. There is a tradeoffs between linkability and
  967. * feasibility, at this point.
  968. */
  969. rendcirc->timestamp_dirty = time(NULL);
  970. link_apconn_to_circ(conn, rendcirc);
  971. if (connection_ap_handshake_send_begin(conn, rendcirc) < 0)
  972. return 0; /* already marked, let them fade away */
  973. return 1;
  974. }
  975. if (rendcirc && rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  976. log_fn(LOG_INFO,"pending-join circ %d already here, with intro ack. Stalling. (stream %d sec old)", rendcirc->n_circ_id, conn_age);
  977. return 0;
  978. }
  979. /* it's on its way. find an intro circ. */
  980. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  981. if (retval < 0) return -1; /* failed */
  982. if (retval > 0) {
  983. /* one has already sent the intro. keep waiting. */
  984. tor_assert(introcirc);
  985. log_fn(LOG_INFO,"Intro circ %d present and awaiting ack (rend %d). Stalling. (stream %d sec old)",
  986. introcirc->n_circ_id, rendcirc ? rendcirc->n_circ_id : 0, conn_age);
  987. return 0;
  988. }
  989. /* now rendcirc and introcirc are each either undefined or not finished */
  990. if (rendcirc && introcirc && rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  991. log_fn(LOG_INFO,"ready rend circ %d already here (no intro-ack yet on intro %d). (stream %d sec old)",
  992. rendcirc->n_circ_id, introcirc->n_circ_id, conn_age);
  993. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  994. if (introcirc->state == CIRCUIT_STATE_OPEN) {
  995. log_fn(LOG_INFO,"found open intro circ %d (rend %d); sending introduction. (stream %d sec old)",
  996. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  997. if (rend_client_send_introduction(introcirc, rendcirc) < 0) {
  998. return -1;
  999. }
  1000. rendcirc->timestamp_dirty = time(NULL);
  1001. introcirc->timestamp_dirty = time(NULL);
  1002. assert_circuit_ok(rendcirc);
  1003. assert_circuit_ok(introcirc);
  1004. return 0;
  1005. }
  1006. }
  1007. log_fn(LOG_INFO,"Intro (%d) and rend (%d) circs are not both ready. Stalling conn. (%d sec old)",
  1008. introcirc ? introcirc->n_circ_id : 0,
  1009. rendcirc ? rendcirc->n_circ_id : 0, conn_age);
  1010. return 0;
  1011. }
  1012. }