rendservice.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char rendservice_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file rendservice.c
  8. * \brief The hidden-service side of rendezvous functionality.
  9. **/
  10. #include "or.h"
  11. static origin_circuit_t *find_intro_circuit(routerinfo_t *router,
  12. const char *pk_digest);
  13. /** Represents the mapping from a virtual port of a rendezvous service to
  14. * a real port on some IP.
  15. */
  16. typedef struct rend_service_port_config_t {
  17. uint16_t virtual_port;
  18. uint16_t real_port;
  19. uint32_t real_addr;
  20. } rend_service_port_config_t;
  21. /** Try to maintain this many intro points per service if possible. */
  22. #define NUM_INTRO_POINTS 3
  23. /** If we can't build our intro circuits, don't retry for this long. */
  24. #define INTRO_CIRC_RETRY_PERIOD (60*5)
  25. /** Don't try to build more than this many circuits before giving up
  26. * for a while.*/
  27. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  28. /** How many times will a hidden service operator attempt to connect to
  29. * a requested rendezvous point before giving up? */
  30. #define MAX_REND_FAILURES 30
  31. /** How many seconds should we spend trying to connect to a requested
  32. * rendezvous point before giving up? */
  33. #define MAX_REND_TIMEOUT 30
  34. /** Represents a single hidden service running at this OP. */
  35. typedef struct rend_service_t {
  36. /** Fields specified in config file */
  37. char *directory; /**< where in the filesystem it stores it */
  38. smartlist_t *ports; /**< List of rend_service_port_config_t */
  39. char *intro_prefer_nodes; /**< comma-separated list of nicknames */
  40. char *intro_exclude_nodes; /**< comma-separated list of nicknames */
  41. /* Other fields */
  42. crypto_pk_env_t *private_key;
  43. char service_id[REND_SERVICE_ID_LEN+1];
  44. char pk_digest[DIGEST_LEN];
  45. smartlist_t *intro_nodes; /**< list of hexdigests for intro points we have,
  46. * or are trying to establish. */
  47. time_t intro_period_started;
  48. int n_intro_circuits_launched; /**< count of intro circuits we have
  49. * established in this period. */
  50. rend_service_descriptor_t *desc;
  51. time_t desc_is_dirty;
  52. time_t next_upload_time;
  53. } rend_service_t;
  54. /** A list of rend_service_t's for services run on this OP.
  55. */
  56. static smartlist_t *rend_service_list = NULL;
  57. /** Return the number of rendezvous services we have configured. */
  58. int
  59. num_rend_services(void)
  60. {
  61. if (!rend_service_list)
  62. return 0;
  63. return smartlist_len(rend_service_list);
  64. }
  65. /** Release the storage held by <b>service</b>.
  66. */
  67. static void
  68. rend_service_free(rend_service_t *service)
  69. {
  70. if (!service) return;
  71. tor_free(service->directory);
  72. SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
  73. smartlist_free(service->ports);
  74. if (service->private_key)
  75. crypto_free_pk_env(service->private_key);
  76. tor_free(service->intro_prefer_nodes);
  77. tor_free(service->intro_exclude_nodes);
  78. SMARTLIST_FOREACH(service->intro_nodes, void*, p, tor_free(p));
  79. smartlist_free(service->intro_nodes);
  80. if (service->desc)
  81. rend_service_descriptor_free(service->desc);
  82. tor_free(service);
  83. }
  84. /** Release all the storage held in rend_service_list.
  85. */
  86. void
  87. rend_service_free_all(void)
  88. {
  89. if (!rend_service_list) {
  90. return;
  91. }
  92. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
  93. rend_service_free(ptr));
  94. smartlist_free(rend_service_list);
  95. rend_service_list = NULL;
  96. }
  97. /** Validate <b>service</b> and add it to rend_service_list if possible.
  98. */
  99. static void
  100. add_service(rend_service_t *service)
  101. {
  102. int i;
  103. rend_service_port_config_t *p;
  104. struct in_addr addr;
  105. if (!service->intro_prefer_nodes)
  106. service->intro_prefer_nodes = tor_strdup("");
  107. if (!service->intro_exclude_nodes)
  108. service->intro_exclude_nodes = tor_strdup("");
  109. if (!smartlist_len(service->ports)) {
  110. log_warn(LD_CONFIG, "Hidden service with no ports configured; ignoring.");
  111. rend_service_free(service);
  112. } else {
  113. smartlist_set_capacity(service->ports, -1);
  114. smartlist_add(rend_service_list, service);
  115. log_debug(LD_REND,"Configuring service with directory \"%s\"",
  116. service->directory);
  117. for (i = 0; i < smartlist_len(service->ports); ++i) {
  118. char addrbuf[INET_NTOA_BUF_LEN];
  119. p = smartlist_get(service->ports, i);
  120. addr.s_addr = htonl(p->real_addr);
  121. tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
  122. log_debug(LD_REND,"Service maps port %d to %s:%d",
  123. p->virtual_port, addrbuf, p->real_port);
  124. }
  125. }
  126. }
  127. /** Parses a real-port to virtual-port mapping and returns a new
  128. * rend_service_port_config_t.
  129. *
  130. * The format is: VirtualPort (IP|RealPort|IP:RealPort)?
  131. *
  132. * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
  133. */
  134. static rend_service_port_config_t *
  135. parse_port_config(const char *string)
  136. {
  137. smartlist_t *sl;
  138. int virtport;
  139. int realport;
  140. uint16_t p;
  141. uint32_t addr;
  142. const char *addrport;
  143. rend_service_port_config_t *result = NULL;
  144. sl = smartlist_create();
  145. smartlist_split_string(sl, string, " ",
  146. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  147. if (smartlist_len(sl) < 1 || smartlist_len(sl) > 2) {
  148. log_warn(LD_CONFIG, "Bad syntax in hidden service port configuration.");
  149. goto err;
  150. }
  151. virtport = atoi(smartlist_get(sl,0));
  152. if (virtport < 1 || virtport > 65535) {
  153. log_warn(LD_CONFIG, "Missing or invalid port in hidden service port "
  154. "configuration.");
  155. goto err;
  156. }
  157. if (smartlist_len(sl) == 1) {
  158. /* No addr:port part; use default. */
  159. realport = virtport;
  160. addr = 0x7F000001u; /* 127.0.0.1 */
  161. } else {
  162. addrport = smartlist_get(sl,1);
  163. if (strchr(addrport, ':') || strchr(addrport, '.')) {
  164. if (parse_addr_port(LOG_WARN, addrport, NULL, &addr, &p)<0) {
  165. log_warn(LD_CONFIG,"Unparseable address in hidden service port "
  166. "configuration.");
  167. goto err;
  168. }
  169. realport = p?p:virtport;
  170. } else {
  171. /* No addr:port, no addr -- must be port. */
  172. realport = atoi(addrport);
  173. if (realport < 1 || realport > 65535)
  174. goto err;
  175. addr = 0x7F000001u; /* Default to 127.0.0.1 */
  176. }
  177. }
  178. result = tor_malloc(sizeof(rend_service_port_config_t));
  179. result->virtual_port = virtport;
  180. result->real_port = realport;
  181. result->real_addr = addr;
  182. err:
  183. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  184. smartlist_free(sl);
  185. return result;
  186. }
  187. /** Set up rend_service_list, based on the values of HiddenServiceDir and
  188. * HiddenServicePort in <b>options</b>. Return 0 on success and -1 on
  189. * failure. (If <b>validate_only</b> is set, parse, warn and return as
  190. * normal, but don't actually change the configured services.)
  191. */
  192. int
  193. rend_config_services(or_options_t *options, int validate_only)
  194. {
  195. config_line_t *line;
  196. rend_service_t *service = NULL;
  197. rend_service_port_config_t *portcfg;
  198. if (!validate_only) {
  199. rend_service_free_all();
  200. rend_service_list = smartlist_create();
  201. }
  202. for (line = options->RendConfigLines; line; line = line->next) {
  203. if (!strcasecmp(line->key, "HiddenServiceDir")) {
  204. if (service) {
  205. if (validate_only)
  206. rend_service_free(service);
  207. else
  208. add_service(service);
  209. }
  210. service = tor_malloc_zero(sizeof(rend_service_t));
  211. service->directory = tor_strdup(line->value);
  212. service->ports = smartlist_create();
  213. service->intro_nodes = smartlist_create();
  214. service->intro_period_started = time(NULL);
  215. continue;
  216. }
  217. if (!service) {
  218. log_warn(LD_CONFIG, "%s with no preceding HiddenServiceDir directive",
  219. line->key);
  220. rend_service_free(service);
  221. return -1;
  222. }
  223. if (!strcasecmp(line->key, "HiddenServicePort")) {
  224. portcfg = parse_port_config(line->value);
  225. if (!portcfg) {
  226. rend_service_free(service);
  227. return -1;
  228. }
  229. smartlist_add(service->ports, portcfg);
  230. } else if (!strcasecmp(line->key, "HiddenServiceNodes")) {
  231. if (service->intro_prefer_nodes) {
  232. log_warn(LD_CONFIG,
  233. "Got multiple HiddenServiceNodes lines for a single "
  234. "service.");
  235. return -1;
  236. }
  237. service->intro_prefer_nodes = tor_strdup(line->value);
  238. } else {
  239. tor_assert(!strcasecmp(line->key, "HiddenServiceExcludeNodes"));
  240. if (service->intro_exclude_nodes) {
  241. log_warn(LD_CONFIG,
  242. "Got multiple HiddenServiceExcludedNodes lines for "
  243. "a single service.");
  244. return -1;
  245. }
  246. service->intro_exclude_nodes = tor_strdup(line->value);
  247. }
  248. }
  249. if (service) {
  250. if (validate_only)
  251. rend_service_free(service);
  252. else
  253. add_service(service);
  254. }
  255. return 0;
  256. }
  257. /** Replace the old value of <b>service</b>-\>desc with one that reflects
  258. * the other fields in service.
  259. */
  260. static void
  261. rend_service_update_descriptor(rend_service_t *service)
  262. {
  263. rend_service_descriptor_t *d;
  264. origin_circuit_t *circ;
  265. int i,n;
  266. routerinfo_t *router;
  267. if (service->desc) {
  268. rend_service_descriptor_free(service->desc);
  269. service->desc = NULL;
  270. }
  271. d = service->desc = tor_malloc(sizeof(rend_service_descriptor_t));
  272. d->pk = crypto_pk_dup_key(service->private_key);
  273. d->timestamp = time(NULL);
  274. d->version = 1;
  275. n = smartlist_len(service->intro_nodes);
  276. d->n_intro_points = 0;
  277. d->intro_points = tor_malloc_zero(sizeof(char*)*n);
  278. d->intro_point_extend_info = tor_malloc_zero(sizeof(extend_info_t*)*n);
  279. d->protocols = (1<<2) | (1<<0); /* We support protocol 2 and protocol 0. */
  280. for (i=0; i < n; ++i) {
  281. router = router_get_by_nickname(smartlist_get(service->intro_nodes, i),1);
  282. if (!router) {
  283. log_info(LD_REND,"Router '%s' not found for intro point %d. Skipping.",
  284. safe_str((char*)smartlist_get(service->intro_nodes, i)), i);
  285. continue;
  286. }
  287. circ = find_intro_circuit(router, service->pk_digest);
  288. if (circ && circ->_base.purpose == CIRCUIT_PURPOSE_S_INTRO) {
  289. /* We have an entirely established intro circuit. */
  290. d->intro_points[d->n_intro_points] = tor_strdup(router->nickname);
  291. d->intro_point_extend_info[d->n_intro_points] =
  292. extend_info_from_router(router);
  293. d->n_intro_points++;
  294. }
  295. }
  296. }
  297. /** Load and/or generate private keys for all hidden services. Return 0 on
  298. * success, -1 on failure.
  299. */
  300. int
  301. rend_service_load_keys(void)
  302. {
  303. int i;
  304. rend_service_t *s;
  305. char fname[512];
  306. char buf[128];
  307. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  308. s = smartlist_get(rend_service_list,i);
  309. if (s->private_key)
  310. continue;
  311. log_info(LD_REND, "Loading hidden-service keys from \"%s\"",
  312. s->directory);
  313. /* Check/create directory */
  314. if (check_private_dir(s->directory, CPD_CREATE) < 0)
  315. return -1;
  316. /* Load key */
  317. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  318. strlcat(fname,"/private_key",sizeof(fname)) >= sizeof(fname)) {
  319. log_warn(LD_CONFIG, "Directory name too long to store key file: \"%s\".",
  320. s->directory);
  321. return -1;
  322. }
  323. s->private_key = init_key_from_file(fname);
  324. if (!s->private_key)
  325. return -1;
  326. /* Create service file */
  327. if (rend_get_service_id(s->private_key, s->service_id)<0) {
  328. log_warn(LD_BUG, "Internal error: couldn't encode service ID.");
  329. return -1;
  330. }
  331. if (crypto_pk_get_digest(s->private_key, s->pk_digest)<0) {
  332. log_warn(LD_BUG, "Bug: Couldn't compute hash of public key.");
  333. return -1;
  334. }
  335. if (strlcpy(fname,s->directory,sizeof(fname)) >= sizeof(fname) ||
  336. strlcat(fname,"/hostname",sizeof(fname)) >= sizeof(fname)) {
  337. log_warn(LD_CONFIG, "Directory name too long to store hostname file:"
  338. " \"%s\".", s->directory);
  339. return -1;
  340. }
  341. tor_snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
  342. if (write_str_to_file(fname,buf,0)<0)
  343. return -1;
  344. }
  345. return 0;
  346. }
  347. /** Return the service whose public key has a digest of <b>digest</b>. Return
  348. * NULL if no such service exists.
  349. */
  350. static rend_service_t *
  351. rend_service_get_by_pk_digest(const char* digest)
  352. {
  353. SMARTLIST_FOREACH(rend_service_list, rend_service_t*, s,
  354. if (!memcmp(s->pk_digest,digest,DIGEST_LEN)) return s);
  355. return NULL;
  356. }
  357. /** Return 1 if any virtual port in <b>service</b> wants a circuit
  358. * to have good uptime. Else return 0.
  359. */
  360. static int
  361. rend_service_requires_uptime(rend_service_t *service)
  362. {
  363. int i;
  364. rend_service_port_config_t *p;
  365. for (i=0; i < smartlist_len(service->ports); ++i) {
  366. p = smartlist_get(service->ports, i);
  367. if (smartlist_string_num_isin(get_options()->LongLivedPorts,
  368. p->virtual_port))
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. /******
  374. * Handle cells
  375. ******/
  376. /** Respond to an INTRODUCE2 cell by launching a circuit to the chosen
  377. * rendezvous point.
  378. */
  379. int
  380. rend_service_introduce(origin_circuit_t *circuit, const char *request,
  381. size_t request_len)
  382. {
  383. char *ptr, *r_cookie;
  384. extend_info_t *extend_info = NULL;
  385. char buf[RELAY_PAYLOAD_SIZE];
  386. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN]; /* Holds KH, Df, Db, Kf, Kb */
  387. rend_service_t *service;
  388. int r, i;
  389. size_t len, keylen;
  390. crypto_dh_env_t *dh = NULL;
  391. origin_circuit_t *launched = NULL;
  392. crypt_path_t *cpath = NULL;
  393. char serviceid[REND_SERVICE_ID_LEN+1];
  394. char hexcookie[9];
  395. int circ_needs_uptime;
  396. int reason = END_CIRC_REASON_TORPROTOCOL;
  397. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  398. circuit->rend_pk_digest,10);
  399. log_info(LD_REND, "Received INTRODUCE2 cell for service %s on circ %d.",
  400. escaped(serviceid), circuit->_base.n_circ_id);
  401. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_INTRO) {
  402. log_warn(LD_PROTOCOL,
  403. "Got an INTRODUCE2 over a non-introduction circuit %d.",
  404. circuit->_base.n_circ_id);
  405. return -1;
  406. }
  407. /* min key length plus digest length plus nickname length */
  408. if (request_len < DIGEST_LEN+REND_COOKIE_LEN+(MAX_NICKNAME_LEN+1)+
  409. DH_KEY_LEN+42) {
  410. log_warn(LD_PROTOCOL, "Got a truncated INTRODUCE2 cell on circ %d.",
  411. circuit->_base.n_circ_id);
  412. return -1;
  413. }
  414. /* first DIGEST_LEN bytes of request is service pk digest */
  415. service = rend_service_get_by_pk_digest(request);
  416. if (!service) {
  417. log_warn(LD_REND, "Got an INTRODUCE2 cell for an unrecognized service %s.",
  418. escaped(serviceid));
  419. return -1;
  420. }
  421. if (memcmp(circuit->rend_pk_digest, request, DIGEST_LEN)) {
  422. base32_encode(serviceid, REND_SERVICE_ID_LEN+1, request, 10);
  423. log_warn(LD_REND, "Got an INTRODUCE2 cell for the wrong service (%s).",
  424. escaped(serviceid));
  425. return -1;
  426. }
  427. keylen = crypto_pk_keysize(service->private_key);
  428. if (request_len < keylen+DIGEST_LEN) {
  429. log_warn(LD_PROTOCOL,
  430. "PK-encrypted portion of INTRODUCE2 cell was truncated.");
  431. return -1;
  432. }
  433. /* Next N bytes is encrypted with service key */
  434. note_crypto_pk_op(REND_SERVER);
  435. r = crypto_pk_private_hybrid_decrypt(
  436. service->private_key,buf,request+DIGEST_LEN,request_len-DIGEST_LEN,
  437. PK_PKCS1_OAEP_PADDING,1);
  438. if (r<0) {
  439. log_warn(LD_PROTOCOL, "Couldn't decrypt INTRODUCE2 cell.");
  440. return -1;
  441. }
  442. len = r;
  443. if (*buf == 2) {
  444. /* Version 2 INTRODUCE2 cell. */
  445. int klen;
  446. extend_info = tor_malloc_zero(sizeof(extend_info_t));
  447. extend_info->addr = ntohl(get_uint32(buf+1));
  448. extend_info->port = ntohs(get_uint16(buf+5));
  449. memcpy(extend_info->identity_digest, buf+7, DIGEST_LEN);
  450. extend_info->nickname[0] = '$';
  451. base16_encode(extend_info->nickname+1, sizeof(extend_info->nickname)-1,
  452. extend_info->identity_digest, DIGEST_LEN);
  453. klen = ntohs(get_uint16(buf+7+DIGEST_LEN));
  454. if ((int)len != 7+DIGEST_LEN+2+klen+20+128) {
  455. log_warn(LD_PROTOCOL, "Bad length %u for version 2 INTRODUCE2 cell.",
  456. (int)len);
  457. reason = END_CIRC_REASON_TORPROTOCOL;
  458. goto err;
  459. }
  460. extend_info->onion_key = crypto_pk_asn1_decode(buf+7+DIGEST_LEN+2, klen);
  461. if (!extend_info->onion_key) {
  462. log_warn(LD_PROTOCOL,
  463. "Error decoding onion key in version 2 INTRODUCE2 cell.");
  464. reason = END_CIRC_REASON_TORPROTOCOL;
  465. goto err;
  466. }
  467. ptr = buf+7+DIGEST_LEN+2+klen;
  468. len -= 7+DIGEST_LEN+2+klen;
  469. } else {
  470. char *rp_nickname;
  471. size_t nickname_field_len;
  472. routerinfo_t *router;
  473. int version;
  474. if (*buf == 1) {
  475. rp_nickname = buf+1;
  476. nickname_field_len = MAX_HEX_NICKNAME_LEN+1;
  477. version = 1;
  478. } else {
  479. nickname_field_len = MAX_NICKNAME_LEN+1;
  480. rp_nickname = buf;
  481. version = 0;
  482. }
  483. ptr=memchr(rp_nickname,0,nickname_field_len);
  484. if (!ptr || ptr == rp_nickname) {
  485. log_warn(LD_PROTOCOL,
  486. "Couldn't find a nul-padded nickname in INTRODUCE2 cell.");
  487. return -1;
  488. }
  489. if ((version == 0 && !is_legal_nickname(rp_nickname)) ||
  490. (version == 1 && !is_legal_nickname_or_hexdigest(rp_nickname))) {
  491. log_warn(LD_PROTOCOL, "Bad nickname in INTRODUCE2 cell.");
  492. return -1;
  493. }
  494. /* Okay, now we know that a nickname is at the start of the buffer. */
  495. ptr = rp_nickname+nickname_field_len;
  496. len -= nickname_field_len;
  497. len -= rp_nickname - buf; /* also remove header space used by version, if
  498. * any */
  499. router = router_get_by_nickname(rp_nickname, 0);
  500. if (!router) {
  501. log_info(LD_REND, "Couldn't find router %s named in rendezvous cell.",
  502. escaped_safe_str(rp_nickname));
  503. /* XXXX Add a no-such-router reason? */
  504. reason = END_CIRC_REASON_TORPROTOCOL;
  505. goto err;
  506. }
  507. extend_info = extend_info_from_router(router);
  508. }
  509. if (len != REND_COOKIE_LEN+DH_KEY_LEN) {
  510. log_warn(LD_PROTOCOL, "Bad length %u for INTRODUCE2 cell.", (int)len);
  511. reason = END_CIRC_REASON_TORPROTOCOL;
  512. return -1;
  513. }
  514. r_cookie = ptr;
  515. base16_encode(hexcookie,9,r_cookie,4);
  516. /* Try DH handshake... */
  517. dh = crypto_dh_new();
  518. if (!dh || crypto_dh_generate_public(dh)<0) {
  519. log_warn(LD_BUG,"Internal error: couldn't build DH state "
  520. "or generate public key.");
  521. reason = END_CIRC_REASON_INTERNAL;
  522. goto err;
  523. }
  524. if (crypto_dh_compute_secret(dh, ptr+REND_COOKIE_LEN, DH_KEY_LEN, keys,
  525. DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  526. log_warn(LD_BUG, "Internal error: couldn't complete DH handshake");
  527. reason = END_CIRC_REASON_INTERNAL;
  528. goto err;
  529. }
  530. circ_needs_uptime = rend_service_requires_uptime(service);
  531. /* help predict this next time */
  532. rep_hist_note_used_internal(time(NULL), circ_needs_uptime, 1);
  533. /* Launch a circuit to alice's chosen rendezvous point.
  534. */
  535. for (i=0;i<MAX_REND_FAILURES;i++) {
  536. launched = circuit_launch_by_extend_info(
  537. CIRCUIT_PURPOSE_S_CONNECT_REND, 0, extend_info,
  538. circ_needs_uptime, 1, 1);
  539. if (launched)
  540. break;
  541. }
  542. if (!launched) { /* give up */
  543. log_warn(LD_REND, "Giving up launching first hop of circuit to rendezvous "
  544. "point %s for service %s.",
  545. escaped_safe_str(extend_info->nickname), serviceid);
  546. reason = END_CIRC_REASON_CONNECTFAILED;
  547. goto err;
  548. }
  549. log_info(LD_REND,
  550. "Accepted intro; launching circuit to %s "
  551. "(cookie %s) for service %s.",
  552. escaped_safe_str(extend_info->nickname), hexcookie, serviceid);
  553. tor_assert(launched->build_state);
  554. /* Fill in the circuit's state. */
  555. memcpy(launched->rend_pk_digest, circuit->rend_pk_digest,
  556. DIGEST_LEN);
  557. memcpy(launched->rend_cookie, r_cookie, REND_COOKIE_LEN);
  558. strlcpy(launched->rend_query, service->service_id,
  559. sizeof(launched->rend_query));
  560. launched->build_state->pending_final_cpath = cpath =
  561. tor_malloc_zero(sizeof(crypt_path_t));
  562. cpath->magic = CRYPT_PATH_MAGIC;
  563. launched->build_state->expiry_time = time(NULL) + MAX_REND_TIMEOUT;
  564. cpath->dh_handshake_state = dh;
  565. dh = NULL;
  566. if (circuit_init_cpath_crypto(cpath,keys+DIGEST_LEN,1)<0)
  567. goto err;
  568. memcpy(cpath->handshake_digest, keys, DIGEST_LEN);
  569. if (extend_info) extend_info_free(extend_info);
  570. return 0;
  571. err:
  572. if (dh) crypto_dh_free(dh);
  573. if (launched)
  574. circuit_mark_for_close(TO_CIRCUIT(launched), reason);
  575. if (extend_info) extend_info_free(extend_info);
  576. return -1;
  577. }
  578. /** Called when we fail building a rendezvous circuit at some point other
  579. * than the last hop: launches a new circuit to the same rendezvous point.
  580. */
  581. void
  582. rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc)
  583. {
  584. origin_circuit_t *newcirc;
  585. cpath_build_state_t *newstate, *oldstate;
  586. tor_assert(oldcirc->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  587. if (!oldcirc->build_state ||
  588. oldcirc->build_state->failure_count > MAX_REND_FAILURES ||
  589. oldcirc->build_state->expiry_time < time(NULL)) {
  590. log_info(LD_REND,
  591. "Attempt to build circuit to %s for rendezvous has failed "
  592. "too many times or expired; giving up.",
  593. oldcirc->build_state ?
  594. oldcirc->build_state->chosen_exit->nickname : "*unknown*");
  595. return;
  596. }
  597. oldstate = oldcirc->build_state;
  598. tor_assert(oldstate);
  599. if (oldstate->pending_final_cpath == NULL) {
  600. log_info(LD_REND,"Skipping relaunch of circ that failed on its first hop. "
  601. "Initiator will retry.");
  602. return;
  603. }
  604. log_info(LD_REND,"Reattempting rendezvous circuit to '%s'",
  605. oldstate->chosen_exit->nickname);
  606. newcirc = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, 0,
  607. oldstate->chosen_exit, 0, 1, 1);
  608. if (!newcirc) {
  609. log_warn(LD_REND,"Couldn't relaunch rendezvous circuit to '%s'.",
  610. oldstate->chosen_exit->nickname);
  611. return;
  612. }
  613. newstate = newcirc->build_state;
  614. tor_assert(newstate);
  615. newstate->failure_count = oldstate->failure_count+1;
  616. newstate->expiry_time = oldstate->expiry_time;
  617. newstate->pending_final_cpath = oldstate->pending_final_cpath;
  618. oldstate->pending_final_cpath = NULL;
  619. memcpy(newcirc->rend_query, oldcirc->rend_query, REND_SERVICE_ID_LEN+1);
  620. memcpy(newcirc->rend_pk_digest, oldcirc->rend_pk_digest,
  621. DIGEST_LEN);
  622. memcpy(newcirc->rend_cookie, oldcirc->rend_cookie,
  623. REND_COOKIE_LEN);
  624. }
  625. /** Launch a circuit to serve as an introduction point for the service
  626. * <b>service</b> at the introduction point <b>nickname</b>
  627. */
  628. static int
  629. rend_service_launch_establish_intro(rend_service_t *service,
  630. const char *nickname)
  631. {
  632. origin_circuit_t *launched;
  633. log_info(LD_REND,
  634. "Launching circuit to introduction point %s for service %s",
  635. escaped_safe_str(nickname), service->service_id);
  636. rep_hist_note_used_internal(time(NULL), 1, 0);
  637. ++service->n_intro_circuits_launched;
  638. launched = circuit_launch_by_nickname(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, 0,
  639. nickname, 1, 0, 1);
  640. if (!launched) {
  641. log_info(LD_REND,
  642. "Can't launch circuit to establish introduction at %s.",
  643. escaped_safe_str(nickname));
  644. return -1;
  645. }
  646. strlcpy(launched->rend_query, service->service_id,
  647. sizeof(launched->rend_query));
  648. memcpy(launched->rend_pk_digest, service->pk_digest, DIGEST_LEN);
  649. if (launched->_base.state == CIRCUIT_STATE_OPEN)
  650. rend_service_intro_has_opened(launched);
  651. return 0;
  652. }
  653. /** Called when we're done building a circuit to an introduction point:
  654. * sends a RELAY_ESTABLISH_INTRO cell.
  655. */
  656. void
  657. rend_service_intro_has_opened(origin_circuit_t *circuit)
  658. {
  659. rend_service_t *service;
  660. size_t len;
  661. int r;
  662. char buf[RELAY_PAYLOAD_SIZE];
  663. char auth[DIGEST_LEN + 9];
  664. char serviceid[REND_SERVICE_ID_LEN+1];
  665. int reason = END_CIRC_REASON_TORPROTOCOL;
  666. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
  667. tor_assert(circuit->cpath);
  668. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  669. circuit->rend_pk_digest,10);
  670. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  671. if (!service) {
  672. log_warn(LD_REND, "Unrecognized service ID %s on introduction circuit %d.",
  673. serviceid, circuit->_base.n_circ_id);
  674. reason = END_CIRC_REASON_NOSUCHSERVICE;
  675. goto err;
  676. }
  677. log_info(LD_REND,
  678. "Established circuit %d as introduction point for service %s",
  679. circuit->_base.n_circ_id, serviceid);
  680. /* Build the payload for a RELAY_ESTABLISH_INTRO cell. */
  681. len = crypto_pk_asn1_encode(service->private_key, buf+2,
  682. RELAY_PAYLOAD_SIZE-2);
  683. set_uint16(buf, htons((uint16_t)len));
  684. len += 2;
  685. memcpy(auth, circuit->cpath->prev->handshake_digest, DIGEST_LEN);
  686. memcpy(auth+DIGEST_LEN, "INTRODUCE", 9);
  687. if (crypto_digest(buf+len, auth, DIGEST_LEN+9))
  688. goto err;
  689. len += 20;
  690. note_crypto_pk_op(REND_SERVER);
  691. r = crypto_pk_private_sign_digest(service->private_key, buf+len, buf, len);
  692. if (r<0) {
  693. log_warn(LD_BUG, "Internal error: couldn't sign introduction request.");
  694. reason = END_CIRC_REASON_INTERNAL;
  695. goto err;
  696. }
  697. len += r;
  698. if (connection_edge_send_command(NULL, TO_CIRCUIT(circuit),
  699. RELAY_COMMAND_ESTABLISH_INTRO,
  700. buf, len, circuit->cpath->prev)<0) {
  701. log_info(LD_GENERAL,
  702. "Couldn't send introduction request for service %s on circuit %d",
  703. serviceid, circuit->_base.n_circ_id);
  704. reason = END_CIRC_REASON_INTERNAL;
  705. goto err;
  706. }
  707. return;
  708. err:
  709. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  710. }
  711. /** Called when we get an INTRO_ESTABLISHED cell; mark the circuit as a
  712. * live introduction point, and note that the service descriptor is
  713. * now out-of-date.*/
  714. int
  715. rend_service_intro_established(origin_circuit_t *circuit, const char *request,
  716. size_t request_len)
  717. {
  718. rend_service_t *service;
  719. (void) request;
  720. (void) request_len;
  721. if (circuit->_base.purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  722. log_warn(LD_PROTOCOL,
  723. "received INTRO_ESTABLISHED cell on non-intro circuit.");
  724. goto err;
  725. }
  726. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  727. if (!service) {
  728. log_warn(LD_REND, "Unknown service on introduction circuit %d.",
  729. circuit->_base.n_circ_id);
  730. goto err;
  731. }
  732. service->desc_is_dirty = time(NULL);
  733. circuit->_base.purpose = CIRCUIT_PURPOSE_S_INTRO;
  734. return 0;
  735. err:
  736. circuit_mark_for_close(TO_CIRCUIT(circuit), END_CIRC_REASON_TORPROTOCOL);
  737. return -1;
  738. }
  739. /** Called once a circuit to a rendezvous point is established: sends a
  740. * RELAY_COMMAND_RENDEZVOUS1 cell.
  741. */
  742. void
  743. rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
  744. {
  745. rend_service_t *service;
  746. char buf[RELAY_PAYLOAD_SIZE];
  747. crypt_path_t *hop;
  748. char serviceid[REND_SERVICE_ID_LEN+1];
  749. char hexcookie[9];
  750. int reason;
  751. tor_assert(circuit->_base.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  752. tor_assert(circuit->cpath);
  753. tor_assert(circuit->build_state);
  754. hop = circuit->build_state->pending_final_cpath;
  755. tor_assert(hop);
  756. base16_encode(hexcookie,9,circuit->rend_cookie,4);
  757. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  758. circuit->rend_pk_digest,10);
  759. log_info(LD_REND,
  760. "Done building circuit %d to rendezvous with "
  761. "cookie %s for service %s",
  762. circuit->_base.n_circ_id, hexcookie, serviceid);
  763. service = rend_service_get_by_pk_digest(circuit->rend_pk_digest);
  764. if (!service) {
  765. log_warn(LD_GENERAL, "Internal error: unrecognized service ID on "
  766. "introduction circuit.");
  767. reason = END_CIRC_REASON_INTERNAL;
  768. goto err;
  769. }
  770. /* All we need to do is send a RELAY_RENDEZVOUS1 cell... */
  771. memcpy(buf, circuit->rend_cookie, REND_COOKIE_LEN);
  772. if (crypto_dh_get_public(hop->dh_handshake_state,
  773. buf+REND_COOKIE_LEN, DH_KEY_LEN)<0) {
  774. log_warn(LD_GENERAL,"Couldn't get DH public key.");
  775. reason = END_CIRC_REASON_INTERNAL;
  776. goto err;
  777. }
  778. memcpy(buf+REND_COOKIE_LEN+DH_KEY_LEN, hop->handshake_digest,
  779. DIGEST_LEN);
  780. /* Send the cell */
  781. if (connection_edge_send_command(NULL, TO_CIRCUIT(circuit),
  782. RELAY_COMMAND_RENDEZVOUS1,
  783. buf, REND_COOKIE_LEN+DH_KEY_LEN+DIGEST_LEN,
  784. circuit->cpath->prev)<0) {
  785. log_warn(LD_GENERAL, "Couldn't send RENDEZVOUS1 cell.");
  786. reason = END_CIRC_REASON_INTERNAL;
  787. goto err;
  788. }
  789. crypto_dh_free(hop->dh_handshake_state);
  790. hop->dh_handshake_state = NULL;
  791. /* Append the cpath entry. */
  792. hop->state = CPATH_STATE_OPEN;
  793. /* set the windows to default. these are the windows
  794. * that bob thinks alice has.
  795. */
  796. hop->package_window = CIRCWINDOW_START;
  797. hop->deliver_window = CIRCWINDOW_START;
  798. onion_append_to_cpath(&circuit->cpath, hop);
  799. circuit->build_state->pending_final_cpath = NULL; /* prevent double-free */
  800. /* Change the circuit purpose. */
  801. circuit->_base.purpose = CIRCUIT_PURPOSE_S_REND_JOINED;
  802. return;
  803. err:
  804. circuit_mark_for_close(TO_CIRCUIT(circuit), reason);
  805. }
  806. /*
  807. * Manage introduction points
  808. */
  809. /** Return the (possibly non-open) introduction circuit ending at
  810. * <b>router</b> for the service whose public key is <b>pk_digest</b>. Return
  811. * NULL if no such service is found.
  812. */
  813. static origin_circuit_t *
  814. find_intro_circuit(routerinfo_t *router, const char *pk_digest)
  815. {
  816. origin_circuit_t *circ = NULL;
  817. tor_assert(router);
  818. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  819. CIRCUIT_PURPOSE_S_INTRO))) {
  820. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  821. router->nickname)) {
  822. return circ;
  823. }
  824. }
  825. circ = NULL;
  826. while ((circ = circuit_get_next_by_pk_and_purpose(circ,pk_digest,
  827. CIRCUIT_PURPOSE_S_ESTABLISH_INTRO))) {
  828. if (!strcasecmp(circ->build_state->chosen_exit->nickname,
  829. router->nickname)) {
  830. return circ;
  831. }
  832. }
  833. return NULL;
  834. }
  835. /** Encode and sign an up-to-date service descriptor for <b>service</b>,
  836. * and upload it to all the dirservers.
  837. */
  838. static void
  839. upload_service_descriptor(rend_service_t *service, int version)
  840. {
  841. char *desc;
  842. size_t desc_len;
  843. /* Update the descriptor. */
  844. rend_service_update_descriptor(service);
  845. if (rend_encode_service_descriptor(service->desc,
  846. version,
  847. service->private_key,
  848. &desc, &desc_len)<0) {
  849. log_warn(LD_BUG, "Internal error: couldn't encode service descriptor; "
  850. "not uploading.");
  851. return;
  852. }
  853. /* Post it to the dirservers */
  854. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_RENDDESC, desc, desc_len);
  855. tor_free(desc);
  856. service->desc_is_dirty = 0;
  857. }
  858. /** For every service, check how many intro points it currently has, and:
  859. * - Pick new intro points as necessary.
  860. * - Launch circuits to any new intro points.
  861. */
  862. void
  863. rend_services_introduce(void)
  864. {
  865. int i,j,r;
  866. routerinfo_t *router;
  867. rend_service_t *service;
  868. char *intro;
  869. int changed, prev_intro_nodes;
  870. smartlist_t *intro_routers, *exclude_routers;
  871. time_t now;
  872. intro_routers = smartlist_create();
  873. exclude_routers = smartlist_create();
  874. now = time(NULL);
  875. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  876. smartlist_clear(intro_routers);
  877. service = smartlist_get(rend_service_list, i);
  878. tor_assert(service);
  879. changed = 0;
  880. if (now > service->intro_period_started+INTRO_CIRC_RETRY_PERIOD) {
  881. /* One period has elapsed; we can try building circuits again. */
  882. service->intro_period_started = now;
  883. service->n_intro_circuits_launched = 0;
  884. } else if (service->n_intro_circuits_launched >=
  885. MAX_INTRO_CIRCS_PER_PERIOD) {
  886. /* We have failed too many times in this period; wait for the next
  887. * one before we try again. */
  888. continue;
  889. }
  890. /* Find out which introduction points we have in progress for this
  891. service. */
  892. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  893. intro = smartlist_get(service->intro_nodes, j);
  894. router = router_get_by_nickname(intro, 0);
  895. if (!router || !find_intro_circuit(router,service->pk_digest)) {
  896. log_info(LD_REND,"Giving up on %s as intro point for %s.",
  897. intro, service->service_id);
  898. tor_free(intro);
  899. smartlist_del(service->intro_nodes,j--);
  900. changed = 1;
  901. service->desc_is_dirty = now;
  902. }
  903. smartlist_add(intro_routers, router);
  904. }
  905. /* We have enough intro points, and the intro points we thought we had were
  906. * all connected.
  907. */
  908. if (!changed && smartlist_len(service->intro_nodes) >= NUM_INTRO_POINTS) {
  909. /* We have all our intro points! Start a fresh period and reset the
  910. * circuit count. */
  911. service->intro_period_started = now;
  912. service->n_intro_circuits_launched = 0;
  913. continue;
  914. }
  915. /* Remember how many introduction circuits we started with. */
  916. prev_intro_nodes = smartlist_len(service->intro_nodes);
  917. smartlist_add_all(exclude_routers, intro_routers);
  918. /* The directory is now here. Pick three ORs as intro points. */
  919. for (j=prev_intro_nodes; j < NUM_INTRO_POINTS; ++j) {
  920. char *hex_digest;
  921. router = router_choose_random_node(service->intro_prefer_nodes,
  922. service->intro_exclude_nodes, exclude_routers, 1, 0, 0,
  923. get_options()->_AllowInvalid & ALLOW_INVALID_INTRODUCTION,
  924. 0, 0);
  925. if (!router) {
  926. log_warn(LD_REND,
  927. "Could only establish %d introduction points for %s.",
  928. smartlist_len(service->intro_nodes), service->service_id);
  929. break;
  930. }
  931. changed = 1;
  932. hex_digest = tor_malloc_zero(HEX_DIGEST_LEN+2);
  933. hex_digest[0] = '$';
  934. base16_encode(hex_digest+1, HEX_DIGEST_LEN+1,
  935. router->cache_info.identity_digest,
  936. DIGEST_LEN);
  937. smartlist_add(intro_routers, router);
  938. smartlist_add(exclude_routers, router);
  939. smartlist_add(service->intro_nodes, hex_digest);
  940. log_info(LD_REND, "Picked router %s as an intro point for %s.",
  941. router->nickname, service->service_id);
  942. }
  943. /* Reset exclude_routers, for the next time around the loop. */
  944. smartlist_clear(exclude_routers);
  945. /* If there's no need to launch new circuits, stop here. */
  946. if (!changed)
  947. continue;
  948. /* Establish new introduction points. */
  949. for (j=prev_intro_nodes; j < smartlist_len(service->intro_nodes); ++j) {
  950. intro = smartlist_get(service->intro_nodes, j);
  951. r = rend_service_launch_establish_intro(service, intro);
  952. if (r<0) {
  953. log_warn(LD_REND, "Error launching circuit to node %s for service %s.",
  954. intro, service->service_id);
  955. }
  956. }
  957. }
  958. smartlist_free(intro_routers);
  959. smartlist_free(exclude_routers);
  960. }
  961. /** Regenerate and upload rendezvous service descriptors for all
  962. * services, if necessary. If the descriptor has been dirty enough
  963. * for long enough, definitely upload; else only upload when the
  964. * periodic timeout has expired.
  965. *
  966. * For the first upload, pick a random time between now and two periods
  967. * from now, and pick it independently for each service.
  968. */
  969. void
  970. rend_consider_services_upload(time_t now)
  971. {
  972. int i;
  973. rend_service_t *service;
  974. int rendpostperiod = get_options()->RendPostPeriod;
  975. if (!get_options()->PublishHidServDescriptors)
  976. return;
  977. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  978. service = smartlist_get(rend_service_list, i);
  979. if (!service->next_upload_time) { /* never been uploaded yet */
  980. service->next_upload_time =
  981. now + crypto_rand_int(2*rendpostperiod);
  982. }
  983. if (service->next_upload_time < now ||
  984. (service->desc_is_dirty &&
  985. service->desc_is_dirty < now-5)) {
  986. /* if it's time, or if the directory servers have a wrong service
  987. * descriptor and ours has been stable for 5 seconds, upload a
  988. * new one of each format. */
  989. upload_service_descriptor(service, 0);
  990. service->next_upload_time = now + rendpostperiod;
  991. }
  992. }
  993. }
  994. /** Log the status of introduction points for all rendezvous services
  995. * at log severity <b>severity</b>.
  996. */
  997. void
  998. rend_service_dump_stats(int severity)
  999. {
  1000. int i,j;
  1001. routerinfo_t *router;
  1002. rend_service_t *service;
  1003. const char *nickname, *safe_name;
  1004. char nn_buf[MAX_VERBOSE_NICKNAME_LEN];
  1005. origin_circuit_t *circ;
  1006. for (i=0; i < smartlist_len(rend_service_list); ++i) {
  1007. service = smartlist_get(rend_service_list, i);
  1008. log(severity, LD_GENERAL, "Service configured in \"%s\":",
  1009. service->directory);
  1010. for (j=0; j < smartlist_len(service->intro_nodes); ++j) {
  1011. nickname = smartlist_get(service->intro_nodes, j);
  1012. router = router_get_by_nickname(nickname,1);
  1013. if (router) {
  1014. router_get_verbose_nickname(nn_buf, router);
  1015. nickname = nn_buf;
  1016. }
  1017. safe_name = safe_str(nickname);
  1018. if (!router) {
  1019. log(severity, LD_GENERAL,
  1020. " Intro point %d at %s: unrecognized router", j, safe_name);
  1021. continue;
  1022. }
  1023. circ = find_intro_circuit(router, service->pk_digest);
  1024. if (!circ) {
  1025. log(severity, LD_GENERAL, " Intro point %d at %s: no circuit",
  1026. j, safe_name);
  1027. continue;
  1028. }
  1029. log(severity, LD_GENERAL, " Intro point %d at %s: circuit is %s",
  1030. j, safe_name, circuit_state_to_string(circ->_base.state));
  1031. }
  1032. }
  1033. }
  1034. /** Given <b>conn</b>, a rendezvous exit stream, look up the hidden service for
  1035. * 'circ', and look up the port and address based on conn-\>port.
  1036. * Assign the actual conn-\>addr and conn-\>port. Return -1 if failure,
  1037. * or 0 for success.
  1038. */
  1039. int
  1040. rend_service_set_connection_addr_port(edge_connection_t *conn,
  1041. origin_circuit_t *circ)
  1042. {
  1043. rend_service_t *service;
  1044. int i;
  1045. rend_service_port_config_t *p;
  1046. char serviceid[REND_SERVICE_ID_LEN+1];
  1047. tor_assert(circ->_base.purpose == CIRCUIT_PURPOSE_S_REND_JOINED);
  1048. log_debug(LD_REND,"beginning to hunt for addr/port");
  1049. base32_encode(serviceid, REND_SERVICE_ID_LEN+1,
  1050. circ->rend_pk_digest,10);
  1051. service = rend_service_get_by_pk_digest(circ->rend_pk_digest);
  1052. if (!service) {
  1053. log_warn(LD_REND, "Couldn't find any service associated with pk %s on "
  1054. "rendezvous circuit %d; closing.",
  1055. serviceid, circ->_base.n_circ_id);
  1056. return -1;
  1057. }
  1058. for (i = 0; i < smartlist_len(service->ports); ++i) {
  1059. p = smartlist_get(service->ports, i);
  1060. if (conn->_base.port == p->virtual_port) {
  1061. conn->_base.addr = p->real_addr;
  1062. conn->_base.port = p->real_port;
  1063. return 0;
  1064. }
  1065. }
  1066. log_info(LD_REND, "No virtual port mapping exists for port %d on service %s",
  1067. conn->_base.port,serviceid);
  1068. return -1;
  1069. }