circuituse.c 40 KB

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