rendclient.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char rendclient_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file rendclient.c
  9. * \brief Client code to access location-hidden services.
  10. **/
  11. #include "or.h"
  12. /** Called when we've established a circuit to an introduction point:
  13. * send the introduction request. */
  14. void
  15. rend_client_introcirc_has_opened(origin_circuit_t *circ)
  16. {
  17. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  18. tor_assert(circ->cpath);
  19. log_info(LD_REND,"introcirc is open");
  20. connection_ap_attach_pending();
  21. }
  22. /** Send the establish-rendezvous cell along a rendezvous circuit. if
  23. * it fails, mark the circ for close and return -1. else return 0.
  24. */
  25. static int
  26. rend_client_send_establish_rendezvous(origin_circuit_t *circ)
  27. {
  28. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  29. log_info(LD_REND, "Sending an ESTABLISH_RENDEZVOUS cell");
  30. if (crypto_rand(circ->rend_cookie, REND_COOKIE_LEN) < 0) {
  31. log_warn(LD_BUG, "Internal error: Couldn't produce random cookie.");
  32. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  33. return -1;
  34. }
  35. if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  36. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  37. circ->rend_cookie, REND_COOKIE_LEN,
  38. circ->cpath->prev)<0) {
  39. /* circ is already marked for close */
  40. log_warn(LD_GENERAL, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. /** Called when we're trying to connect an ap conn; sends an INTRODUCE1 cell
  46. * down introcirc if possible.
  47. */
  48. int
  49. rend_client_send_introduction(origin_circuit_t *introcirc,
  50. origin_circuit_t *rendcirc)
  51. {
  52. size_t payload_len;
  53. int r;
  54. char payload[RELAY_PAYLOAD_SIZE];
  55. char tmp[RELAY_PAYLOAD_SIZE];
  56. rend_cache_entry_t *entry;
  57. crypt_path_t *cpath;
  58. off_t dh_offset;
  59. crypto_pk_env_t *intro_key; /* either Bob's public key or an intro key. */
  60. tor_assert(introcirc->_base.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  61. tor_assert(rendcirc->_base.purpose == CIRCUIT_PURPOSE_C_REND_READY);
  62. tor_assert(!rend_cmp_service_ids(introcirc->rend_query,
  63. rendcirc->rend_query));
  64. if (rend_cache_lookup_entry(introcirc->rend_query, -1, &entry) < 1) {
  65. log_warn(LD_REND,
  66. "query %s didn't have valid rend desc in cache. Failing.",
  67. escaped_safe_str(introcirc->rend_query));
  68. goto err;
  69. }
  70. /* first 20 bytes of payload are the hash of bob's pk */
  71. if (entry->parsed->version == 0) { /* unversioned descriptor */
  72. intro_key = entry->parsed->pk;
  73. } else { /* versioned descriptor */
  74. char hex_digest[HEX_DIGEST_LEN+2];
  75. hex_digest[0] = '$';
  76. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  77. introcirc->build_state->chosen_exit->identity_digest,
  78. DIGEST_LEN);
  79. intro_key = strmap_get(entry->parsed->intro_keys, hex_digest);
  80. if (!intro_key) {
  81. log_warn(LD_BUG, "Internal error: could not find intro key.");
  82. goto err;
  83. }
  84. }
  85. if (crypto_pk_get_digest(intro_key, payload)<0) {
  86. log_warn(LD_BUG, "Internal error: couldn't hash public key.");
  87. goto err;
  88. }
  89. /* Initialize the pending_final_cpath and start the DH handshake. */
  90. cpath = rendcirc->build_state->pending_final_cpath;
  91. if (!cpath) {
  92. cpath = rendcirc->build_state->pending_final_cpath =
  93. tor_malloc_zero(sizeof(crypt_path_t));
  94. cpath->magic = CRYPT_PATH_MAGIC;
  95. if (!(cpath->dh_handshake_state = crypto_dh_new())) {
  96. log_warn(LD_BUG, "Internal error: couldn't allocate DH.");
  97. goto err;
  98. }
  99. if (crypto_dh_generate_public(cpath->dh_handshake_state)<0) {
  100. log_warn(LD_BUG, "Internal error: couldn't generate g^x.");
  101. goto err;
  102. }
  103. }
  104. /* write the remaining items into tmp */
  105. if (entry->parsed->protocols & (1<<2)) {
  106. /* version 2 format */
  107. extend_info_t *extend_info = rendcirc->build_state->chosen_exit;
  108. int klen;
  109. tmp[0] = 2; /* version 2 of the cell format */
  110. /* nul pads */
  111. set_uint32(tmp+1, htonl(extend_info->addr));
  112. set_uint16(tmp+5, htons(extend_info->port));
  113. memcpy(tmp+7, extend_info->identity_digest, DIGEST_LEN);
  114. klen = crypto_pk_asn1_encode(extend_info->onion_key, tmp+7+DIGEST_LEN+2,
  115. sizeof(tmp)-(7+DIGEST_LEN+2));
  116. set_uint16(tmp+7+DIGEST_LEN, htons(klen));
  117. memcpy(tmp+7+DIGEST_LEN+2+klen, rendcirc->rend_cookie,
  118. REND_COOKIE_LEN);
  119. dh_offset = 7+DIGEST_LEN+2+klen+REND_COOKIE_LEN;
  120. } else {
  121. /* Version 0. */
  122. strncpy(tmp, rendcirc->build_state->chosen_exit->nickname,
  123. (MAX_NICKNAME_LEN+1)); /* nul pads */
  124. memcpy(tmp+MAX_NICKNAME_LEN+1, rendcirc->rend_cookie,
  125. REND_COOKIE_LEN);
  126. dh_offset = MAX_NICKNAME_LEN+1+REND_COOKIE_LEN;
  127. }
  128. if (crypto_dh_get_public(cpath->dh_handshake_state, tmp+dh_offset,
  129. DH_KEY_LEN)<0) {
  130. log_warn(LD_BUG, "Internal error: couldn't extract g^x.");
  131. goto err;
  132. }
  133. note_crypto_pk_op(REND_CLIENT);
  134. /*XXX maybe give crypto_pk_public_hybrid_encrypt a max_len arg,
  135. * to avoid buffer overflows? */
  136. r = crypto_pk_public_hybrid_encrypt(intro_key, payload+DIGEST_LEN,
  137. tmp,
  138. (int)(dh_offset+DH_KEY_LEN),
  139. PK_PKCS1_OAEP_PADDING, 0);
  140. if (r<0) {
  141. log_warn(LD_BUG,"Internal error: hybrid pk encrypt failed.");
  142. goto err;
  143. }
  144. payload_len = DIGEST_LEN + r;
  145. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE); /* we overran something */
  146. log_info(LD_REND, "Sending an INTRODUCE1 cell");
  147. if (relay_send_command_from_edge(0, TO_CIRCUIT(introcirc),
  148. RELAY_COMMAND_INTRODUCE1,
  149. payload, payload_len,
  150. introcirc->cpath->prev)<0) {
  151. /* introcirc is already marked for close. leave rendcirc alone. */
  152. log_warn(LD_BUG, "Couldn't send INTRODUCE1 cell");
  153. return -1;
  154. }
  155. /* Now, we wait for an ACK or NAK on this circuit. */
  156. introcirc->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
  157. return 0;
  158. err:
  159. circuit_mark_for_close(TO_CIRCUIT(introcirc), END_CIRC_REASON_INTERNAL);
  160. circuit_mark_for_close(TO_CIRCUIT(rendcirc), END_CIRC_REASON_INTERNAL);
  161. return -1;
  162. }
  163. /** Called when a rendezvous circuit is open; sends a establish
  164. * rendezvous circuit as appropriate. */
  165. void
  166. rend_client_rendcirc_has_opened(origin_circuit_t *circ)
  167. {
  168. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  169. log_info(LD_REND,"rendcirc is open");
  170. /* generate a rendezvous cookie, store it in circ */
  171. if (rend_client_send_establish_rendezvous(circ) < 0) {
  172. return;
  173. }
  174. }
  175. /** Called when get an ACK or a NAK for a REND_INTRODUCE1 cell.
  176. */
  177. int
  178. rend_client_introduction_acked(origin_circuit_t *circ,
  179. const char *request, size_t request_len)
  180. {
  181. origin_circuit_t *rendcirc;
  182. (void) request; // XXXX Use this.
  183. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  184. log_warn(LD_PROTOCOL,
  185. "Received REND_INTRODUCE_ACK on unexpected circuit %d.",
  186. circ->_base.n_circ_id);
  187. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  188. return -1;
  189. }
  190. tor_assert(circ->build_state->chosen_exit);
  191. if (request_len == 0) {
  192. /* It's an ACK; the introduction point relayed our introduction request. */
  193. /* Locate the rend circ which is waiting to hear about this ack,
  194. * and tell it.
  195. */
  196. log_info(LD_REND,"Received ack. Telling rend circ...");
  197. rendcirc = circuit_get_by_rend_query_and_purpose(
  198. circ->rend_query, CIRCUIT_PURPOSE_C_REND_READY);
  199. if (rendcirc) { /* remember the ack */
  200. rendcirc->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  201. } else {
  202. log_info(LD_REND,"...Found no rend circ. Dropping on the floor.");
  203. }
  204. /* close the circuit: we won't need it anymore. */
  205. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACKED;
  206. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  207. } else {
  208. /* It's a NAK; the introduction point didn't relay our request. */
  209. circ->_base.purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  210. /* Remove this intro point from the set of viable introduction
  211. * points. If any remain, extend to a new one and try again.
  212. * If none remain, refetch the service descriptor.
  213. */
  214. if (rend_client_remove_intro_point(circ->build_state->chosen_exit,
  215. circ->rend_query) > 0) {
  216. /* There are introduction points left. Re-extend the circuit to
  217. * another intro point and try again. */
  218. extend_info_t *extend_info;
  219. int result;
  220. extend_info = rend_client_get_random_intro(circ->rend_query);
  221. if (!extend_info) {
  222. log_warn(LD_REND, "No introduction points left for %s. Closing.",
  223. escaped_safe_str(circ->rend_query));
  224. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  225. return -1;
  226. }
  227. log_info(LD_REND,
  228. "Got nack for %s from %s. Re-extending circ %d, "
  229. "this time to %s.",
  230. escaped_safe_str(circ->rend_query),
  231. circ->build_state->chosen_exit->nickname, circ->_base.n_circ_id,
  232. extend_info->nickname);
  233. result = circuit_extend_to_new_exit(circ, extend_info);
  234. extend_info_free(extend_info);
  235. return result;
  236. }
  237. }
  238. return 0;
  239. }
  240. /** If we are not currently fetching a rendezvous service descriptor
  241. * for the service ID <b>query</b>, start a directory connection to fetch a
  242. * new one.
  243. */
  244. void
  245. rend_client_refetch_renddesc(const char *query)
  246. {
  247. if (!get_options()->FetchHidServDescriptors)
  248. return;
  249. log_info(LD_REND, "Fetching rendezvous descriptor for service %s", query);
  250. if (connection_get_by_type_state_rendquery(CONN_TYPE_DIR, 0, query)) {
  251. log_info(LD_REND,"Would fetch a new renddesc here (for %s), but one is "
  252. "already in progress.", escaped_safe_str(query));
  253. } else {
  254. /* not one already; initiate a dir rend desc lookup */
  255. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RENDDESC,
  256. ROUTER_PURPOSE_GENERAL, query, 1);
  257. }
  258. }
  259. /** Start a connection to a hidden service directory to fetch a v2
  260. * rendezvous service descriptor for the base32-encoded service ID
  261. * <b>query</b>.
  262. */
  263. void
  264. rend_client_refetch_v2_renddesc(const char *query)
  265. {
  266. char descriptor_id[DIGEST_LEN];
  267. int replica;
  268. tor_assert(query);
  269. tor_assert(strlen(query) == REND_SERVICE_ID_LEN_BASE32);
  270. /* Are we configured to fetch descriptors? */
  271. if (!get_options()->FetchHidServDescriptors) {
  272. log_warn(LD_REND, "We received an onion address for a v2 rendezvous "
  273. "service descriptor, but are not fetching service descriptors.");
  274. return;
  275. }
  276. log_debug(LD_REND, "Fetching v2 rendezvous descriptor for service %s",
  277. query);
  278. replica = crypto_rand_int(REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  279. if (rend_compute_v2_desc_id(descriptor_id, query, NULL, time(NULL),
  280. replica) < 0) {
  281. log_warn(LD_REND, "Internal error: Computing v2 rendezvous "
  282. "descriptor ID did not succeed.");
  283. return;
  284. }
  285. directory_get_from_hs_dir(descriptor_id, query);
  286. return;
  287. }
  288. /** Remove failed_intro from ent. If ent now has no intro points, or
  289. * service is unrecognized, then launch a new renddesc fetch.
  290. *
  291. * Return -1 if error, 0 if no intro points remain or service
  292. * unrecognized, 1 if recognized and some intro points remain.
  293. */
  294. int
  295. rend_client_remove_intro_point(extend_info_t *failed_intro, const char *query)
  296. {
  297. int i, r;
  298. rend_cache_entry_t *ent;
  299. connection_t *conn;
  300. r = rend_cache_lookup_entry(query, -1, &ent);
  301. if (r<0) {
  302. log_warn(LD_BUG, "Malformed service ID %s.", escaped_safe_str(query));
  303. return -1;
  304. }
  305. if (r==0) {
  306. log_info(LD_REND, "Unknown service %s. Re-fetching descriptor.",
  307. escaped_safe_str(query));
  308. /* Fetch both, v0 and v2 rend descriptors in parallel. Use whichever
  309. * arrives first. */
  310. rend_client_refetch_v2_renddesc(query);
  311. rend_client_refetch_renddesc(query);
  312. return 0;
  313. }
  314. if (ent->parsed->intro_point_extend_info) {
  315. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  316. if (!memcmp(failed_intro->identity_digest,
  317. ent->parsed->intro_point_extend_info[i]->identity_digest,
  318. DIGEST_LEN)) {
  319. tor_assert(!strcmp(ent->parsed->intro_points[i],
  320. ent->parsed->intro_point_extend_info[i]->nickname));
  321. tor_free(ent->parsed->intro_points[i]);
  322. extend_info_free(ent->parsed->intro_point_extend_info[i]);
  323. --ent->parsed->n_intro_points;
  324. ent->parsed->intro_points[i] =
  325. ent->parsed->intro_points[ent->parsed->n_intro_points];
  326. ent->parsed->intro_point_extend_info[i] =
  327. ent->parsed->intro_point_extend_info[ent->parsed->n_intro_points];
  328. break;
  329. }
  330. }
  331. } else {
  332. for (i=0; i < ent->parsed->n_intro_points; ++i) {
  333. if (!strcasecmp(ent->parsed->intro_points[i], failed_intro->nickname)) {
  334. tor_free(ent->parsed->intro_points[i]);
  335. ent->parsed->intro_points[i] =
  336. ent->parsed->intro_points[--ent->parsed->n_intro_points];
  337. break;
  338. }
  339. }
  340. }
  341. if (!ent->parsed->n_intro_points) {
  342. log_info(LD_REND,
  343. "No more intro points remain for %s. Re-fetching descriptor.",
  344. escaped_safe_str(query));
  345. /* Fetch both, v0 and v2 rend descriptors in parallel. Use whichever
  346. * arrives first. */
  347. rend_client_refetch_v2_renddesc(query);
  348. rend_client_refetch_renddesc(query);
  349. /* move all pending streams back to renddesc_wait */
  350. while ((conn = connection_get_by_type_state_rendquery(CONN_TYPE_AP,
  351. AP_CONN_STATE_CIRCUIT_WAIT, query))) {
  352. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  353. }
  354. return 0;
  355. }
  356. log_info(LD_REND,"%d options left for %s.",
  357. ent->parsed->n_intro_points, escaped_safe_str(query));
  358. return 1;
  359. }
  360. /** Called when we receive a RENDEZVOUS_ESTABLISHED cell; changes the state of
  361. * the circuit to C_REND_READY.
  362. */
  363. int
  364. rend_client_rendezvous_acked(origin_circuit_t *circ, const char *request,
  365. size_t request_len)
  366. {
  367. (void) request;
  368. (void) request_len;
  369. /* we just got an ack for our establish-rendezvous. switch purposes. */
  370. if (circ->_base.purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  371. log_warn(LD_PROTOCOL,"Got a rendezvous ack when we weren't expecting one. "
  372. "Closing circ.");
  373. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  374. return -1;
  375. }
  376. log_info(LD_REND,"Got rendezvous ack. This circuit is now ready for "
  377. "rendezvous.");
  378. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_READY;
  379. return 0;
  380. }
  381. /** Bob sent us a rendezvous cell; join the circuits. */
  382. int
  383. rend_client_receive_rendezvous(origin_circuit_t *circ, const char *request,
  384. size_t request_len)
  385. {
  386. crypt_path_t *hop;
  387. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  388. if ((circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  389. circ->_base.purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)
  390. || !circ->build_state->pending_final_cpath) {
  391. log_warn(LD_PROTOCOL,"Got rendezvous2 cell from hidden service, but not "
  392. "expecting it. Closing.");
  393. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  394. return -1;
  395. }
  396. if (request_len != DH_KEY_LEN+DIGEST_LEN) {
  397. log_warn(LD_PROTOCOL,"Incorrect length (%d) on RENDEZVOUS2 cell.",
  398. (int)request_len);
  399. goto err;
  400. }
  401. log_info(LD_REND,"Got RENDEZVOUS2 cell from hidden service.");
  402. /* first DH_KEY_LEN bytes are g^y from bob. Finish the dh handshake...*/
  403. tor_assert(circ->build_state);
  404. tor_assert(circ->build_state->pending_final_cpath);
  405. hop = circ->build_state->pending_final_cpath;
  406. tor_assert(hop->dh_handshake_state);
  407. if (crypto_dh_compute_secret(hop->dh_handshake_state, request, DH_KEY_LEN,
  408. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  409. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  410. goto err;
  411. }
  412. /* ... and set up cpath. */
  413. if (circuit_init_cpath_crypto(hop, keys+DIGEST_LEN, 0)<0)
  414. goto err;
  415. /* Check whether the digest is right... */
  416. if (memcmp(keys, request+DH_KEY_LEN, DIGEST_LEN)) {
  417. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  418. goto err;
  419. }
  420. crypto_dh_free(hop->dh_handshake_state);
  421. hop->dh_handshake_state = NULL;
  422. /* All is well. Extend the circuit. */
  423. circ->_base.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  424. hop->state = CPATH_STATE_OPEN;
  425. /* set the windows to default. these are the windows
  426. * that alice thinks bob has.
  427. */
  428. hop->package_window = CIRCWINDOW_START;
  429. hop->deliver_window = CIRCWINDOW_START;
  430. onion_append_to_cpath(&circ->cpath, hop);
  431. circ->build_state->pending_final_cpath = NULL; /* prevent double-free */
  432. return 0;
  433. err:
  434. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  435. return -1;
  436. }
  437. /** Find all the apconns in state AP_CONN_STATE_RENDDESC_WAIT that
  438. * are waiting on query. If there's a working cache entry here
  439. * with at least one intro point, move them to the next state;
  440. * else fail them.
  441. */
  442. void
  443. rend_client_desc_here(const char *query)
  444. {
  445. edge_connection_t *conn;
  446. rend_cache_entry_t *entry;
  447. time_t now = time(NULL);
  448. smartlist_t *conns = get_connection_array();
  449. SMARTLIST_FOREACH(conns, connection_t *, _conn,
  450. {
  451. if (_conn->type != CONN_TYPE_AP ||
  452. _conn->state != AP_CONN_STATE_RENDDESC_WAIT ||
  453. _conn->marked_for_close)
  454. continue;
  455. conn = TO_EDGE_CONN(_conn);
  456. if (rend_cmp_service_ids(query, conn->rend_query))
  457. continue;
  458. assert_connection_ok(TO_CONN(conn), now);
  459. if (rend_cache_lookup_entry(conn->rend_query, -1, &entry) == 1 &&
  460. entry->parsed->n_intro_points > 0) {
  461. /* either this fetch worked, or it failed but there was a
  462. * valid entry from before which we should reuse */
  463. log_info(LD_REND,"Rend desc is usable. Launching circuits.");
  464. conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT;
  465. /* restart their timeout values, so they get a fair shake at
  466. * connecting to the hidden service. */
  467. conn->_base.timestamp_created = now;
  468. conn->_base.timestamp_lastread = now;
  469. conn->_base.timestamp_lastwritten = now;
  470. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  471. /* it will never work */
  472. log_warn(LD_REND,"Rendezvous attempt failed. Closing.");
  473. connection_mark_unattached_ap(conn, END_STREAM_REASON_CANT_ATTACH);
  474. }
  475. } else { /* 404, or fetch didn't get that far */
  476. log_notice(LD_REND,"Closing stream for '%s.onion': hidden service is "
  477. "unavailable (try again later).", safe_str(query));
  478. connection_mark_unattached_ap(conn, END_STREAM_REASON_RESOLVEFAILED);
  479. }
  480. });
  481. }
  482. /** Return a newly allocated extend_info_t* for a randomly chosen introduction
  483. * point for the named hidden service. Return NULL if all introduction points
  484. * have been tried and failed.
  485. */
  486. extend_info_t *
  487. rend_client_get_random_intro(const char *query)
  488. {
  489. int i;
  490. rend_cache_entry_t *entry;
  491. if (rend_cache_lookup_entry(query, -1, &entry) < 1) {
  492. log_warn(LD_REND,
  493. "Query '%s' didn't have valid rend desc in cache. Failing.",
  494. safe_str(query));
  495. return NULL;
  496. }
  497. again:
  498. if (!entry->parsed->n_intro_points)
  499. return NULL;
  500. i = crypto_rand_int(entry->parsed->n_intro_points);
  501. if (entry->parsed->intro_point_extend_info) {
  502. return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
  503. } else {
  504. /* add the intro point nicknames */
  505. char *choice = entry->parsed->intro_points[i];
  506. routerinfo_t *router = router_get_by_nickname(choice, 0);
  507. if (!router) {
  508. log_info(LD_REND, "Unknown router with nickname '%s'; trying another.",
  509. choice);
  510. tor_free(choice);
  511. entry->parsed->intro_points[i] =
  512. entry->parsed->intro_points[--entry->parsed->n_intro_points];
  513. goto again;
  514. }
  515. return extend_info_from_router(router);
  516. }
  517. }