directory.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char directory_c_id[] = "$Id$";
  6. #include "or.h"
  7. /**
  8. * \file directory.c
  9. * \brief Implement directory HTTP protocol.
  10. **/
  11. /* In-points to directory.c:
  12. *
  13. * - directory_post_to_dirservers(), called from
  14. * router_upload_dir_desc_to_dirservers() in router.c
  15. * upload_service_descriptor() in rendservice.c
  16. * - directory_get_from_dirserver(), called from
  17. * rend_client_refetch_renddesc() in rendclient.c
  18. * run_scheduled_events() in main.c
  19. * do_hup() in main.c
  20. * - connection_dir_process_inbuf(), called from
  21. * connection_process_inbuf() in connection.c
  22. * - connection_dir_finished_flushing(), called from
  23. * connection_finished_flushing() in connection.c
  24. * - connection_dir_finished_connecting(), called from
  25. * connection_finished_connecting() in connection.c
  26. */
  27. static void
  28. directory_initiate_command_trusted_dir(trusted_dir_server_t *dirserv,
  29. uint8_t purpose, int private_connection,
  30. const char *resource,
  31. const char *payload, size_t payload_len);
  32. static void
  33. directory_initiate_command(const char *address, uint32_t addr, uint16_t port,
  34. const char *platform,
  35. const char *digest, uint8_t purpose,
  36. int private_connection, const char *resource,
  37. const char *payload, size_t payload_len);
  38. static void
  39. directory_send_command(connection_t *conn, const char *platform,
  40. int purpose, const char *resource,
  41. const char *payload, size_t payload_len);
  42. static int directory_handle_command(connection_t *conn);
  43. static int body_is_plausible(const char *body, size_t body_len, int purpose);
  44. static int purpose_is_private(uint8_t purpose);
  45. /********* START VARIABLES **********/
  46. static addr_policy_t *dir_policy = NULL;
  47. #if 0 /* commented out for now, since for now what clients send is
  48. different from what servers want to receive */
  49. /** URL for publishing rendezvous descriptors. */
  50. char rend_publish_string[] = "/tor/rendezvous/publish";
  51. /** Prefix for downloading rendezvous descriptors. */
  52. char rend_fetch_url[] = "/tor/rendezvous/";
  53. #endif
  54. #define ALLOW_DIRECTORY_TIME_SKEW 30*60
  55. /********* END VARIABLES ************/
  56. /** Parse get_options()->DirPolicy, and put the processed version in
  57. * &dir_policy. Ignore port specifiers.
  58. */
  59. void
  60. parse_dir_policy(void)
  61. {
  62. addr_policy_t *n;
  63. if (dir_policy) {
  64. addr_policy_free(dir_policy);
  65. dir_policy = NULL;
  66. }
  67. config_parse_addr_policy(get_options()->DirPolicy, &dir_policy);
  68. /* ports aren't used. */
  69. for (n=dir_policy; n; n = n->next) {
  70. n->prt_min = 1;
  71. n->prt_max = 65535;
  72. }
  73. }
  74. void
  75. free_dir_policy(void)
  76. {
  77. addr_policy_free(dir_policy);
  78. dir_policy = NULL;
  79. }
  80. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  81. * based on <b>dir_policy</b>. Else return 0.
  82. */
  83. int dir_policy_permits_address(uint32_t addr)
  84. {
  85. int a;
  86. if (!dir_policy) /* 'no dir policy' means 'accept' */
  87. return 1;
  88. a = router_compare_addr_to_addr_policy(addr, 1, dir_policy);
  89. if (a==ADDR_POLICY_REJECTED)
  90. return 0;
  91. else if (a==ADDR_POLICY_ACCEPTED)
  92. return 1;
  93. log_fn(LOG_WARN, "Bug: got unexpected 'maybe' answer from dir policy");
  94. return 0;
  95. }
  96. static int
  97. purpose_is_private(uint8_t purpose) {
  98. if (purpose == DIR_PURPOSE_FETCH_DIR ||
  99. purpose == DIR_PURPOSE_UPLOAD_DIR ||
  100. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST)
  101. return 0;
  102. return 1;
  103. }
  104. /** Start a connection to every known directory server, using
  105. * connection purpose 'purpose' and uploading the payload 'payload'
  106. * (length 'payload_len'). The purpose should be one of
  107. * 'DIR_PURPOSE_UPLOAD_DIR' or 'DIR_PURPOSE_UPLOAD_RENDDESC'.
  108. */
  109. void
  110. directory_post_to_dirservers(uint8_t purpose, const char *payload,
  111. size_t payload_len)
  112. {
  113. smartlist_t *dirservers;
  114. router_get_trusted_dir_servers(&dirservers);
  115. tor_assert(dirservers);
  116. /* This tries dirservers which we believe to be down, but ultimately, that's
  117. * harmless, and we may as well err on the side of getting things uploaded.
  118. */
  119. SMARTLIST_FOREACH(dirservers, trusted_dir_server_t *, ds,
  120. {
  121. /* Pay attention to fascistfirewall when we're uploading a
  122. * router descriptor, but not when uploading a service
  123. * descriptor -- those use Tor. */
  124. if (get_options()->FascistFirewall && purpose == DIR_PURPOSE_UPLOAD_DIR &&
  125. !get_options()->HttpProxy) {
  126. if (!smartlist_string_num_isin(get_options()->FirewallPorts, ds->dir_port))
  127. continue;
  128. }
  129. directory_initiate_command_trusted_dir(ds, purpose, purpose_is_private(purpose),
  130. NULL, payload, payload_len);
  131. });
  132. }
  133. /** Start a connection to a random running directory server, using
  134. * connection purpose 'purpose' requesting 'resource'. The purpose
  135. * should be one of 'DIR_PURPOSE_FETCH_DIR',
  136. * 'DIR_PURPOSE_FETCH_RENDDESC', 'DIR_PURPOSE_FETCH_RUNNING_LIST.'
  137. * If <b>retry_if_no_servers</b>, then if all the possible servers seem
  138. * down, mark them up and try again.
  139. */
  140. void
  141. directory_get_from_dirserver(uint8_t purpose, const char *resource,
  142. int retry_if_no_servers)
  143. {
  144. routerinfo_t *r = NULL;
  145. trusted_dir_server_t *ds = NULL;
  146. int fascistfirewall = get_options()->FascistFirewall;
  147. int directconn = purpose == DIR_PURPOSE_FETCH_DIR ||
  148. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST;
  149. int fetch_fresh_first = advertised_server_mode();
  150. int priv = purpose_is_private(purpose);
  151. if (directconn) {
  152. if (fetch_fresh_first) {
  153. /* only ask authdirservers, and don't ask myself */
  154. ds = router_pick_trusteddirserver(1, fascistfirewall,
  155. retry_if_no_servers);
  156. }
  157. if (!ds) {
  158. /* anybody with a non-zero dirport will do */
  159. r = router_pick_directory_server(1, fascistfirewall,
  160. purpose==DIR_PURPOSE_FETCH_RUNNING_LIST,
  161. retry_if_no_servers);
  162. if (!r) {
  163. log_fn(LOG_INFO, "No router found for %s; falling back to dirserver list",
  164. purpose == DIR_PURPOSE_FETCH_RUNNING_LIST
  165. ? "status list" : "directory");
  166. ds = router_pick_trusteddirserver(1, fascistfirewall,
  167. retry_if_no_servers);
  168. }
  169. }
  170. } else { // (purpose == DIR_PURPOSE_FETCH_RENDDESC)
  171. /* only ask authdirservers, any of them will do */
  172. /* Never use fascistfirewall; we're going via Tor. */
  173. ds = router_pick_trusteddirserver(0, 0, retry_if_no_servers);
  174. }
  175. if (r)
  176. directory_initiate_command_router(r, purpose, priv, resource, NULL, 0);
  177. else if (ds)
  178. directory_initiate_command_trusted_dir(ds, purpose, priv, resource, NULL, 0);
  179. else {
  180. log_fn(LOG_NOTICE,"No running dirservers known. Not trying. (purpose %d)",
  181. purpose);
  182. if (directconn) {
  183. /* remember we tried them all and failed. */
  184. directory_all_unreachable(time(NULL));
  185. }
  186. }
  187. }
  188. /** Launch a new connection to the directory server <b>router</b> to upload or
  189. * download a service or rendezvous descriptor. <b>purpose</b> determines what
  190. * kind of directory connection we're launching, and must be one of
  191. * DIR_PURPOSE_{FETCH|UPLOAD}_{DIR|RENDDESC}.
  192. *
  193. * When uploading, <b>payload</b> and <b>payload_len</b> determine the content
  194. * of the HTTP post. Otherwise, <b>payload</b> should be NULL.
  195. *
  196. * When fetching a rendezvous descriptor, <b>resource</b> is the service ID we
  197. * want to fetch.
  198. */
  199. void
  200. directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
  201. int private_connection, const char *resource,
  202. const char *payload, size_t payload_len)
  203. {
  204. directory_initiate_command(router->address, router->addr, router->dir_port,
  205. router->platform, router->identity_digest,
  206. purpose, private_connection, resource,
  207. payload, payload_len);
  208. }
  209. /** As directory_initiate_command_router, but send the command to a trusted
  210. * directory server <b>dirserv</b>. **/
  211. static void
  212. directory_initiate_command_trusted_dir(trusted_dir_server_t *dirserv,
  213. uint8_t purpose, int private_connection,
  214. const char *resource,
  215. const char *payload, size_t payload_len)
  216. {
  217. directory_initiate_command(dirserv->address, dirserv->addr,dirserv->dir_port,
  218. NULL, dirserv->digest, purpose, private_connection, resource,
  219. payload, payload_len);
  220. }
  221. /** Called when we are unable to complete our connection to a
  222. * directory server: Mark the router as down and try again if possible.
  223. */
  224. void
  225. connection_dir_connect_failed(connection_t *conn)
  226. {
  227. router_mark_as_down(conn->identity_digest); /* don't try him again */
  228. if (conn->purpose == DIR_PURPOSE_FETCH_DIR ||
  229. conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  230. log_fn(LOG_INFO, "Giving up on directory server at '%s'; retrying",
  231. conn->address);
  232. directory_get_from_dirserver(conn->purpose, NULL,
  233. 0 /* don't retry_if_no_servers */);
  234. }
  235. }
  236. /** Helper for directory_initiate_command_(router|trusted_dir): send the
  237. * command to a server whose address is <b>address</b>, whose IP is
  238. * <b>addr</b>, whose directory port is <b>dir_port</b>, whose tor version is
  239. * <b>platform</b>, and whose identity key digest is <b>digest</b>. The
  240. * <b>platform</b> argument is optional; the others are required. */
  241. static void
  242. directory_initiate_command(const char *address, uint32_t addr,
  243. uint16_t dir_port, const char *platform,
  244. const char *digest, uint8_t purpose,
  245. int private_connection, const char *resource,
  246. const char *payload, size_t payload_len)
  247. {
  248. connection_t *conn;
  249. tor_assert(address);
  250. tor_assert(addr);
  251. tor_assert(dir_port);
  252. tor_assert(digest);
  253. switch (purpose) {
  254. case DIR_PURPOSE_FETCH_DIR:
  255. log_fn(LOG_DEBUG,"initiating directory fetch");
  256. break;
  257. case DIR_PURPOSE_FETCH_RENDDESC:
  258. log_fn(LOG_DEBUG,"initiating hidden-service descriptor fetch");
  259. break;
  260. case DIR_PURPOSE_UPLOAD_DIR:
  261. log_fn(LOG_DEBUG,"initiating server descriptor upload");
  262. break;
  263. case DIR_PURPOSE_UPLOAD_RENDDESC:
  264. log_fn(LOG_DEBUG,"initiating hidden-service descriptor upload");
  265. break;
  266. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  267. log_fn(LOG_DEBUG,"initiating running-routers fetch");
  268. break;
  269. default:
  270. log_fn(LOG_ERR, "Unrecognized directory connection purpose.");
  271. tor_assert(0);
  272. }
  273. conn = connection_new(CONN_TYPE_DIR);
  274. /* set up conn so it's got all the data we need to remember */
  275. conn->addr = addr;
  276. conn->port = dir_port;
  277. conn->address = tor_strdup(address);
  278. memcpy(conn->identity_digest, digest, DIGEST_LEN);
  279. conn->purpose = purpose;
  280. /* give it an initial state */
  281. conn->state = DIR_CONN_STATE_CONNECTING;
  282. if (!private_connection) {
  283. /* then we want to connect directly */
  284. if (get_options()->HttpProxy) {
  285. addr = get_options()->HttpProxyAddr;
  286. dir_port = get_options()->HttpProxyPort;
  287. }
  288. switch (connection_connect(conn, conn->address, addr, dir_port)) {
  289. case -1:
  290. connection_dir_connect_failed(conn);
  291. connection_free(conn);
  292. return;
  293. case 1:
  294. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  295. /* fall through */
  296. case 0:
  297. /* queue the command on the outbuf */
  298. directory_send_command(conn, platform, purpose, resource,
  299. payload, payload_len);
  300. connection_watch_events(conn, EV_READ | EV_WRITE);
  301. /* writable indicates finish, readable indicates broken link,
  302. error indicates broken link in windowsland. */
  303. }
  304. } else { /* we want to connect via tor */
  305. /* make an AP connection
  306. * populate it and add it at the right state
  307. * socketpair and hook up both sides
  308. */
  309. conn->s = connection_ap_make_bridge(conn->address, conn->port);
  310. if (conn->s < 0) {
  311. log_fn(LOG_WARN,"Making AP bridge to dirserver failed.");
  312. connection_mark_for_close(conn);
  313. return;
  314. }
  315. conn->state = DIR_CONN_STATE_CLIENT_SENDING;
  316. connection_add(conn);
  317. /* queue the command on the outbuf */
  318. directory_send_command(conn, platform, purpose, resource,
  319. payload, payload_len);
  320. connection_watch_events(conn, EV_READ | EV_WRITE);
  321. }
  322. }
  323. /** Queue an appropriate HTTP command on conn-\>outbuf. The other args
  324. * are as in directory_initiate_command.
  325. */
  326. static void
  327. directory_send_command(connection_t *conn, const char *platform,
  328. int purpose, const char *resource,
  329. const char *payload, size_t payload_len) {
  330. char tmp[8192];
  331. char proxystring[128];
  332. char hoststring[128];
  333. char url[128];
  334. int use_newer = 0;
  335. const char *httpcommand = NULL;
  336. tor_assert(conn);
  337. tor_assert(conn->type == CONN_TYPE_DIR);
  338. /* If we don't know the platform, assume it's up-to-date. */
  339. use_newer = platform ? tor_version_as_new_as(platform, "0.0.9pre1"):1;
  340. if (conn->port == 80) {
  341. strlcpy(hoststring, conn->address, sizeof(hoststring));
  342. } else {
  343. tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",conn->address, conn->port);
  344. }
  345. if (get_options()->HttpProxy) {
  346. tor_snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
  347. } else {
  348. proxystring[0] = 0;
  349. }
  350. switch (purpose) {
  351. case DIR_PURPOSE_FETCH_DIR:
  352. tor_assert(!resource);
  353. tor_assert(!payload);
  354. log_fn(LOG_DEBUG, "Asking for %scompressed directory from server running %s",
  355. use_newer?"":"un", platform?platform:"<unknown version>");
  356. httpcommand = "GET";
  357. strlcpy(url, use_newer ? "/tor/dir.z" : "/", sizeof(url));
  358. break;
  359. case DIR_PURPOSE_FETCH_RUNNING_LIST:
  360. tor_assert(!resource);
  361. tor_assert(!payload);
  362. httpcommand = "GET";
  363. strlcpy(url, use_newer ? "/tor/running-routers" : "/running-routers", sizeof(url));
  364. break;
  365. case DIR_PURPOSE_UPLOAD_DIR:
  366. tor_assert(!resource);
  367. tor_assert(payload);
  368. httpcommand = "POST";
  369. strlcpy(url, use_newer ? "/tor/" : "/", sizeof(url));
  370. break;
  371. case DIR_PURPOSE_FETCH_RENDDESC:
  372. tor_assert(resource);
  373. tor_assert(!payload);
  374. /* this must be true or we wouldn't be doing the lookup */
  375. tor_assert(strlen(resource) <= REND_SERVICE_ID_LEN);
  376. /* This breaks the function abstraction. */
  377. strlcpy(conn->rend_query, resource, sizeof(conn->rend_query));
  378. httpcommand = "GET";
  379. tor_snprintf(url, sizeof(url), "%s/rendezvous/%s", use_newer ? "/tor" : "", resource);
  380. break;
  381. case DIR_PURPOSE_UPLOAD_RENDDESC:
  382. tor_assert(!resource);
  383. tor_assert(payload);
  384. httpcommand = "POST";
  385. tor_snprintf(url, sizeof(url), "%s/rendezvous/publish", use_newer ? "/tor" : "");
  386. break;
  387. }
  388. tor_snprintf(tmp, sizeof(tmp), "%s %s%s HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s\r\n\r\n",
  389. httpcommand,
  390. proxystring,
  391. url,
  392. payload ? (unsigned long)payload_len : 0,
  393. hoststring);
  394. connection_write_to_buf(tmp, strlen(tmp), conn);
  395. if (payload) {
  396. /* then send the payload afterwards too */
  397. connection_write_to_buf(payload, payload_len, conn);
  398. }
  399. }
  400. /** Parse an HTTP request string <b>headers</b> of the form
  401. * "\%s [http[s]://]\%s HTTP/1..."
  402. * If it's well-formed, strdup the second \%s into *<b>url</b>, and
  403. * null-terminate it. If the url doesn't start with "/tor/", rewrite it
  404. * so it does. Return 0.
  405. * Otherwise, return -1.
  406. */
  407. static int
  408. parse_http_url(char *headers, char **url)
  409. {
  410. char *s, *start, *tmp;
  411. s = (char *)eat_whitespace_no_nl(headers);
  412. if (!*s) return -1;
  413. s = (char *)find_whitespace(s); /* get past GET/POST */
  414. if (!*s) return -1;
  415. s = (char *)eat_whitespace_no_nl(s);
  416. if (!*s) return -1;
  417. start = s; /* this is it, assuming it's valid */
  418. s = (char *)find_whitespace(start);
  419. if (!*s) return -1;
  420. /* tolerate the http[s] proxy style of putting the hostname in the url */
  421. if (s-start >= 4 && !strcmpstart(start,"http")) {
  422. tmp = start + 4;
  423. if (*tmp == 's')
  424. tmp++;
  425. if (s-tmp >= 3 && !strcmpstart(tmp,"://")) {
  426. tmp = strchr(tmp+3, '/');
  427. if (tmp && tmp < s) {
  428. log_fn(LOG_DEBUG,"Skipping over 'http[s]://hostname' string");
  429. start = tmp;
  430. }
  431. }
  432. }
  433. if (s-start < 5 || strcmpstart(start,"/tor/")) { /* need to rewrite it */
  434. *url = tor_malloc(s - start + 5);
  435. strlcpy(*url,"/tor", s-start+5);
  436. strlcat((*url)+4, start, s-start+1);
  437. } else {
  438. *url = tor_strndup(start, s-start);
  439. }
  440. return 0;
  441. }
  442. /** Parse an HTTP response string <b>headers</b> of the form
  443. * "HTTP/1.\%d \%d\%s\r\n...".
  444. *
  445. * If it's well-formed, assign the status code to *<b>code</b> and
  446. * return 0. Otherwise, return -1.
  447. *
  448. * On success: If <b>date</b> is provided, set *date to the Date
  449. * header in the http headers, or 0 if no such header is found. If
  450. * <b>compression</b> is provided, set *<b>compression</b> to the
  451. * compression method given in the Content-Encoding header, or 0 if no
  452. * such header is found, or -1 if the value of the header is not
  453. * recognized. If <b>reason</b> is provided, strdup the reason string
  454. * into it.
  455. */
  456. int
  457. parse_http_response(const char *headers, int *code, time_t *date,
  458. int *compression, char **reason)
  459. {
  460. int n1, n2;
  461. char datestr[RFC1123_TIME_LEN+1];
  462. smartlist_t *parsed_headers;
  463. tor_assert(headers);
  464. tor_assert(code);
  465. while (TOR_ISSPACE(*headers)) headers++; /* tolerate leading whitespace */
  466. if (sscanf(headers, "HTTP/1.%d %d", &n1, &n2) < 2 ||
  467. (n1 != 0 && n1 != 1) ||
  468. (n2 < 100 || n2 >= 600)) {
  469. log_fn(LOG_WARN,"Failed to parse header '%s'",headers);
  470. return -1;
  471. }
  472. *code = n2;
  473. parsed_headers = smartlist_create();
  474. smartlist_split_string(parsed_headers, headers, "\n",
  475. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  476. if (reason) {
  477. smartlist_t *status_line_elements = smartlist_create();
  478. tor_assert(smartlist_len(parsed_headers));
  479. smartlist_split_string(status_line_elements,
  480. smartlist_get(parsed_headers, 0),
  481. " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  482. tor_assert(smartlist_len(status_line_elements) <= 3);
  483. if (smartlist_len(status_line_elements) == 3) {
  484. *reason = smartlist_get(status_line_elements, 2);
  485. smartlist_set(status_line_elements, 2, NULL); /* Prevent free */
  486. }
  487. SMARTLIST_FOREACH(status_line_elements, char *, cp, tor_free(cp));
  488. smartlist_free(status_line_elements);
  489. }
  490. if (date) {
  491. *date = 0;
  492. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  493. if (!strcmpstart(s, "Date: ")) {
  494. strlcpy(datestr, s+6, sizeof(datestr));
  495. /* This will do nothing on failure, so we don't need to check
  496. the result. We shouldn't warn, since there are many other valid
  497. date formats besides the one we use. */
  498. parse_rfc1123_time(datestr, date);
  499. break;
  500. });
  501. }
  502. if (compression) {
  503. const char *enc = NULL;
  504. SMARTLIST_FOREACH(parsed_headers, const char *, s,
  505. if (!strcmpstart(s, "Content-Encoding: ")) {
  506. enc = s+18; break;
  507. });
  508. if (!enc || !strcmp(enc, "identity")) {
  509. *compression = 0;
  510. } else if (!strcmp(enc, "deflate") || !strcmp(enc, "x-deflate")) {
  511. *compression = ZLIB_METHOD;
  512. } else if (!strcmp(enc, "gzip") || !strcmp(enc, "x-gzip")) {
  513. *compression = GZIP_METHOD;
  514. } else {
  515. log_fn(LOG_INFO, "Unrecognized content encoding: '%s'. Trying to deal.", enc);
  516. *compression = -1;
  517. }
  518. }
  519. SMARTLIST_FOREACH(parsed_headers, char *, s, tor_free(s));
  520. smartlist_free(parsed_headers);
  521. return 0;
  522. }
  523. /** Return true iff <b>body</b> doesn't start with a plausible router or
  524. * running-list or directory opening. This is a sign of possible compression.
  525. **/
  526. static int
  527. body_is_plausible(const char *body, size_t len, int purpose)
  528. {
  529. int i;
  530. if (len == 0)
  531. return 1; /* empty bodies don't need decompression */
  532. if (len < 32)
  533. return 0;
  534. if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
  535. if (!strcmpstart(body,"router") ||
  536. !strcmpstart(body,"signed-directory") ||
  537. !strcmpstart(body,"network-status") ||
  538. !strcmpstart(body,"running-routers"))
  539. return 1;
  540. for (i=0;i<32;++i) {
  541. if (!TOR_ISPRINT(body[i]) && !TOR_ISSPACE(body[i]))
  542. return 0;
  543. }
  544. return 1;
  545. } else {
  546. return 1;
  547. }
  548. }
  549. /** We are a client, and we've finished reading the server's
  550. * response. Parse and it and act appropriately.
  551. *
  552. * Return -1 if an error has occurred, or 0 normally. The caller
  553. * will take care of marking the connection for close.
  554. */
  555. static int
  556. connection_dir_client_reached_eof(connection_t *conn)
  557. {
  558. char *body;
  559. char *headers;
  560. char *reason = NULL;
  561. size_t body_len=0;
  562. int status_code;
  563. time_t now, date_header=0;
  564. int delta;
  565. int compression;
  566. int plausible;
  567. int skewed=0;
  568. switch (fetch_from_buf_http(conn->inbuf,
  569. &headers, MAX_HEADERS_SIZE,
  570. &body, &body_len, MAX_DIR_SIZE)) {
  571. case -1: /* overflow */
  572. log_fn(LOG_WARN,"'fetch' response too large (server '%s'). Failing.", conn->address);
  573. return -1;
  574. case 0:
  575. log_fn(LOG_INFO,"'fetch' response not all here, but we're at eof. Closing.");
  576. return -1;
  577. /* case 1, fall through */
  578. }
  579. if (parse_http_response(headers, &status_code, &date_header,
  580. &compression, &reason) < 0) {
  581. log_fn(LOG_WARN,"Unparseable headers (server '%s'). Closing.", conn->address);
  582. tor_free(body); tor_free(headers);
  583. return -1;
  584. }
  585. if (!reason) reason = tor_strdup("[no reason given]");
  586. log_fn(LOG_DEBUG,
  587. "Received response from directory servers '%s': %d \"%s\"",
  588. conn->address, status_code, reason);
  589. if (date_header > 0) {
  590. now = time(NULL);
  591. delta = now-date_header;
  592. if (abs(delta)>ALLOW_DIRECTORY_TIME_SKEW) {
  593. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  594. log_fn((router && router->is_verified) ? LOG_WARN : LOG_INFO,
  595. "Received directory with skewed time (server '%s'): we are %d minutes %s, or the directory is %d minutes %s.",
  596. conn->address,
  597. abs(delta)/60, delta>0 ? "ahead" : "behind",
  598. abs(delta)/60, delta>0 ? "behind" : "ahead");
  599. skewed = 1; /* don't check the recommended-versions line */
  600. } else {
  601. log_fn(LOG_INFO, "Time on received directory is within tolerance; we are %d seconds skewed. (That's okay.)", delta);
  602. }
  603. }
  604. plausible = body_is_plausible(body, body_len, conn->purpose);
  605. if (compression || !plausible) {
  606. char *new_body = NULL;
  607. size_t new_len = 0;
  608. int guessed = detect_compression_method(body, body_len);
  609. if (compression <= 0 || guessed != compression) {
  610. /* Tell the user if we don't believe what we're told about compression.*/
  611. const char *description1, *description2;
  612. if (compression == ZLIB_METHOD)
  613. description1 = "as deflated";
  614. else if (compression == GZIP_METHOD)
  615. description1 = "as gzipped";
  616. else if (compression == 0)
  617. description1 = "as uncompressed";
  618. else
  619. description1 = "with an unknown Content-Encoding";
  620. if (guessed == ZLIB_METHOD)
  621. description2 = "deflated";
  622. else if (guessed == GZIP_METHOD)
  623. description2 = "gzipped";
  624. else if (!plausible)
  625. description2 = "confusing binary junk";
  626. else
  627. description2 = "uncompressed";
  628. log_fn(LOG_INFO, "HTTP body from server '%s' was labeled %s, "
  629. "but it seems to be %s.%s",
  630. conn->address, description1, description2,
  631. (compression>0 && guessed>0)?" Trying both.":"");
  632. }
  633. /* Try declared compression first if we can. */
  634. if (compression > 0)
  635. tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression);
  636. /* Okay, if that didn't work, and we think that it was compressed
  637. * differently, try that. */
  638. if (!new_body && guessed > 0 && compression != guessed)
  639. tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed);
  640. /* If we're pretty sure that we have a compressed directory, and
  641. * we didn't manage to uncompress it, then warn and bail. */
  642. if (!plausible && !new_body) {
  643. log_fn(LOG_WARN, "Unable to decompress HTTP body (server '%s').", conn->address);
  644. tor_free(body); tor_free(headers); tor_free(reason);
  645. return -1;
  646. }
  647. if (new_body) {
  648. tor_free(body);
  649. body = new_body;
  650. body_len = new_len;
  651. }
  652. }
  653. if (conn->purpose == DIR_PURPOSE_FETCH_DIR) {
  654. /* fetch/process the directory to learn about new routers. */
  655. log_fn(LOG_INFO,"Received directory (size %d) from server '%s'",
  656. (int)body_len, conn->address);
  657. if (status_code == 503 || body_len == 0) {
  658. log_fn(LOG_INFO,"Empty directory; status %d (\"%s\") Ignoring.",
  659. status_code, reason);
  660. tor_free(body); tor_free(headers); tor_free(reason);
  661. return 0;
  662. }
  663. if (status_code != 200) {
  664. log_fn(LOG_WARN,"Received http status code %d (\"%s\") from server '%s'. Failing.",
  665. status_code, reason, conn->address);
  666. tor_free(body); tor_free(headers); tor_free(reason);
  667. return -1;
  668. }
  669. if (router_load_routerlist_from_directory(body, NULL, !skewed, 0) < 0) {
  670. log_fn(LOG_NOTICE,"I failed to parse the directory I fetched from %s:%d. Ignoring.", conn->address, conn->port);
  671. } else {
  672. log_fn(LOG_INFO,"updated routers.");
  673. }
  674. /* do things we've been waiting to do */
  675. directory_has_arrived(time(NULL), conn->identity_digest);
  676. }
  677. if (conn->purpose == DIR_PURPOSE_FETCH_RUNNING_LIST) {
  678. running_routers_t *rrs;
  679. routerlist_t *rl;
  680. /* just update our list of running routers, if this list is new info */
  681. log_fn(LOG_INFO,"Received running-routers list (size %d)", (int)body_len);
  682. if (status_code != 200) {
  683. log_fn(LOG_WARN,"Received http status code %d (\"%s\") from server '%s'. Failing.",
  684. status_code, reason, conn->address);
  685. tor_free(body); tor_free(headers); tor_free(reason);
  686. return -1;
  687. }
  688. if (!(rrs = router_parse_runningrouters(body, 1))) {
  689. log_fn(LOG_WARN, "Can't parse runningrouters list (server '%s')", conn->address);
  690. tor_free(body); tor_free(headers); tor_free(reason);
  691. return -1;
  692. }
  693. router_get_routerlist(&rl);
  694. if (rl) {
  695. routerlist_set_runningrouters(rl,rrs);
  696. } else {
  697. running_routers_free(rrs);
  698. }
  699. }
  700. if (conn->purpose == DIR_PURPOSE_UPLOAD_DIR) {
  701. switch (status_code) {
  702. case 200:
  703. log_fn(LOG_INFO,"eof (status 200) after uploading server descriptor: finished.");
  704. break;
  705. case 400:
  706. log_fn(LOG_WARN,"http status 400 (\"%s\") response from dirserver '%s'. Malformed server descriptor?", reason, conn->address);
  707. break;
  708. case 403:
  709. log_fn(LOG_WARN,"http status 403 (\"%s\") response from dirserver '%s'. Is your clock skewed? Have you mailed us your key fingerprint? Are you using the right key? Are you using a private IP address? See http://tor.eff.org/doc/tor-doc.html#server.", reason, conn->address);
  710. break;
  711. default:
  712. log_fn(LOG_WARN,"http status %d (\"%s\") reason unexpected (server '%s').", status_code, reason, conn->address);
  713. break;
  714. }
  715. }
  716. if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC) {
  717. log_fn(LOG_INFO,"Received rendezvous descriptor (size %d, status %d (\"%s\"))",
  718. (int)body_len, status_code, reason);
  719. switch (status_code) {
  720. case 200:
  721. if (rend_cache_store(body, body_len) < 0) {
  722. log_fn(LOG_WARN,"Failed to store rendezvous descriptor.");
  723. /* alice's ap_stream will notice when connection_mark_for_close
  724. * cleans it up */
  725. } else {
  726. /* success. notify pending connections about this. */
  727. conn->purpose = DIR_PURPOSE_HAS_FETCHED_RENDDESC;
  728. rend_client_desc_here(conn->rend_query);
  729. }
  730. break;
  731. case 404:
  732. /* not there. pending connections will be notified when
  733. * connection_mark_for_close cleans it up. */
  734. break;
  735. case 400:
  736. log_fn(LOG_WARN,"http status 400 (\"%s\"). Dirserver didn't like our rendezvous query?", reason);
  737. break;
  738. default:
  739. log_fn(LOG_WARN,"http status %d (\"%s\") response unexpected (server '%s').", status_code, reason, conn->address);
  740. break;
  741. }
  742. }
  743. if (conn->purpose == DIR_PURPOSE_UPLOAD_RENDDESC) {
  744. switch (status_code) {
  745. case 200:
  746. log_fn(LOG_INFO,"Uploading rendezvous descriptor: finished with status 200 (\"%s\")", reason);
  747. break;
  748. case 400:
  749. log_fn(LOG_WARN,"http status 400 (\"%s\") response from dirserver. Malformed rendezvous descriptor?", reason);
  750. break;
  751. default:
  752. log_fn(LOG_WARN,"http status %d (\"%s\") response unexpected (server '%s').", status_code, reason, conn->address);
  753. break;
  754. }
  755. }
  756. tor_free(body); tor_free(headers); tor_free(reason);
  757. return 0;
  758. }
  759. int connection_dir_reached_eof(connection_t *conn) {
  760. int retval;
  761. if (conn->state != DIR_CONN_STATE_CLIENT_READING) {
  762. log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
  763. connection_close_immediate(conn); /* it was an error; give up on flushing */
  764. connection_mark_for_close(conn);
  765. return -1;
  766. }
  767. retval = connection_dir_client_reached_eof(conn);
  768. connection_mark_for_close(conn);
  769. return retval;
  770. }
  771. /** Read handler for directory connections. (That's connections <em>to</em>
  772. * directory servers and connections <em>at</em> directory servers.)
  773. */
  774. int connection_dir_process_inbuf(connection_t *conn) {
  775. tor_assert(conn);
  776. tor_assert(conn->type == CONN_TYPE_DIR);
  777. /* Directory clients write, then read data until they receive EOF;
  778. * directory servers read data until they get an HTTP command, then
  779. * write their response (when it's finished flushing, they mark for
  780. * close).
  781. */
  782. /* If we're on the dirserver side, look for a command. */
  783. if (conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT) {
  784. if (directory_handle_command(conn) < 0) {
  785. connection_mark_for_close(conn);
  786. return -1;
  787. }
  788. return 0;
  789. }
  790. /* XXX for READ states, might want to make sure inbuf isn't too big */
  791. log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
  792. return 0;
  793. }
  794. /** Create an http response for the client <b>conn</b> out of
  795. * <b>status</b> and <b>reason_phrase</b>. Write it to <b>conn</b>.
  796. */
  797. static void
  798. write_http_status_line(connection_t *conn, int status,
  799. const char *reason_phrase)
  800. {
  801. char buf[256];
  802. if (tor_snprintf(buf, sizeof(buf), "HTTP/1.0 %d %s\r\n\r\n",
  803. status, reason_phrase) < 0) {
  804. log_fn(LOG_WARN,"Bug: status line too long.");
  805. return;
  806. }
  807. connection_write_to_buf(buf, strlen(buf), conn);
  808. }
  809. /** Helper function: called when a dirserver gets a complete HTTP GET
  810. * request. Look for a request for a directory or for a rendezvous
  811. * service descriptor. On finding one, write a response into
  812. * conn-\>outbuf. If the request is unrecognized, send a 400.
  813. * Always return 0. */
  814. static int
  815. directory_handle_command_get(connection_t *conn, char *headers,
  816. char *body, size_t body_len)
  817. {
  818. size_t dlen;
  819. const char *cp;
  820. char *url;
  821. char tmp[8192];
  822. char date[RFC1123_TIME_LEN+1];
  823. log_fn(LOG_DEBUG,"Received GET command.");
  824. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  825. if (parse_http_url(headers, &url) < 0) {
  826. write_http_status_line(conn, 400, "Bad request");
  827. return 0;
  828. }
  829. log_fn(LOG_INFO,"rewritten url as '%s'.", url);
  830. if (!strcmp(url,"/tor/") || !strcmp(url,"/tor/dir.z")) { /* directory fetch */
  831. int deflated = !strcmp(url,"/tor/dir.z");
  832. dlen = dirserv_get_directory(&cp, deflated);
  833. tor_free(url);
  834. if (dlen == 0) {
  835. log_fn(LOG_NOTICE,"Client asked for the mirrored directory, but we don't have a good one yet. Sending 503 Dir not available.");
  836. write_http_status_line(conn, 503, "Directory unavailable");
  837. /* try to get a new one now */
  838. if (!connection_get_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_DIR))
  839. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
  840. return 0;
  841. }
  842. log_fn(LOG_DEBUG,"Dumping %sdirectory to client.",
  843. deflated?"deflated ":"");
  844. format_rfc1123_time(date, time(NULL));
  845. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\nContent-Encoding: %s\r\n\r\n",
  846. date,
  847. (int)dlen,
  848. deflated?"deflate":"identity");
  849. connection_write_to_buf(tmp, strlen(tmp), conn);
  850. connection_write_to_buf(cp, dlen, conn);
  851. return 0;
  852. }
  853. if (!strcmp(url,"/tor/running-routers") ||
  854. !strcmp(url,"/tor/running-routers.z")) { /* running-routers fetch */
  855. int deflated = !strcmp(url,"/tor/dir.z");
  856. tor_free(url);
  857. dlen = dirserv_get_runningrouters(&cp, deflated);
  858. if (!dlen) { /* we failed to create/cache cp */
  859. write_http_status_line(conn, 503, "Directory unavailable");
  860. /* try to get a new one now */
  861. if (!connection_get_by_type_purpose(CONN_TYPE_DIR,
  862. DIR_PURPOSE_FETCH_RUNNING_LIST))
  863. directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
  864. return 0;
  865. }
  866. format_rfc1123_time(date, time(NULL));
  867. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: text/plain\r\nContent-Encoding: %s\r\n\r\n",
  868. date,
  869. (int)dlen,
  870. deflated?"deflate":"identity");
  871. connection_write_to_buf(tmp, strlen(tmp), conn);
  872. connection_write_to_buf(cp, strlen(cp), conn);
  873. return 0;
  874. }
  875. if (!strcmpstart(url,"/tor/rendezvous/")) {
  876. /* rendezvous descriptor fetch */
  877. const char *descp;
  878. size_t desc_len;
  879. if (!authdir_mode(get_options())) {
  880. /* We don't hand out rend descs. In fact, it could be a security
  881. * risk, since rend_cache_lookup_desc() below would provide it
  882. * if we're gone to the site recently, and 404 if we haven't.
  883. *
  884. * Reject. */
  885. write_http_status_line(conn, 400, "Nonauthoritative directory does not not store rendezvous descriptors.");
  886. tor_free(url);
  887. return 0;
  888. }
  889. switch (rend_cache_lookup_desc(url+strlen("/tor/rendezvous/"), &descp, &desc_len)) {
  890. case 1: /* valid */
  891. format_rfc1123_time(date, time(NULL));
  892. tor_snprintf(tmp, sizeof(tmp), "HTTP/1.0 200 OK\r\nDate: %s\r\nContent-Length: %d\r\nContent-Type: application/octet-stream\r\n\r\n",
  893. date,
  894. (int)desc_len); /* can't include descp here, because it's got nuls */
  895. connection_write_to_buf(tmp, strlen(tmp), conn);
  896. connection_write_to_buf(descp, desc_len, conn);
  897. break;
  898. case 0: /* well-formed but not present */
  899. write_http_status_line(conn, 404, "Not found");
  900. break;
  901. case -1: /* not well-formed */
  902. write_http_status_line(conn, 400, "Bad request");
  903. break;
  904. }
  905. tor_free(url);
  906. return 0;
  907. }
  908. /* we didn't recognize the url */
  909. write_http_status_line(conn, 404, "Not found");
  910. tor_free(url);
  911. return 0;
  912. }
  913. /** Helper function: called when a dirserver gets a complete HTTP POST
  914. * request. Look for an uploaded server descriptor or rendezvous
  915. * service descriptor. On finding one, process it and write a
  916. * response into conn-\>outbuf. If the request is unrecognized, send a
  917. * 400. Always return 0. */
  918. static int
  919. directory_handle_command_post(connection_t *conn, char *headers,
  920. char *body, size_t body_len)
  921. {
  922. const char *cp;
  923. char *url;
  924. log_fn(LOG_DEBUG,"Received POST command.");
  925. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  926. if (!authdir_mode(get_options())) {
  927. /* we just provide cached directories; we don't want to
  928. * receive anything. */
  929. write_http_status_line(conn, 400, "Nonauthoritative directory does not not store server descriptors.");
  930. return 0;
  931. }
  932. if (parse_http_url(headers, &url) < 0) {
  933. write_http_status_line(conn, 400, "Bad request");
  934. return 0;
  935. }
  936. log_fn(LOG_INFO,"rewritten url as '%s'.", url);
  937. if (!strcmp(url,"/tor/")) { /* server descriptor post */
  938. const char *msg;
  939. cp = body;
  940. switch (dirserv_add_descriptor(&cp, &msg)) {
  941. case -2:
  942. case -1:
  943. /* malformed descriptor, or something wrong */
  944. write_http_status_line(conn, 400, msg?msg:"Malformed or unacceptable server descriptor");
  945. break;
  946. case 0:
  947. /* descriptor was well-formed but server has not been approved */
  948. write_http_status_line(conn, 200, msg?msg:"Unverified server descriptor accepted");
  949. break;
  950. case 1:
  951. dirserv_get_directory(&cp, 0); /* rebuild and write to disk */
  952. write_http_status_line(conn, 200, msg?msg:"Verified server descriptor accepted");
  953. break;
  954. }
  955. tor_free(url);
  956. return 0;
  957. }
  958. if (!strcmpstart(url,"/tor/rendezvous/publish")) {
  959. /* rendezvous descriptor post */
  960. if (rend_cache_store(body, body_len) < 0)
  961. write_http_status_line(conn, 400, "Invalid service descriptor rejected");
  962. else
  963. write_http_status_line(conn, 200, "Service descriptor stored");
  964. tor_free(url);
  965. return 0;
  966. }
  967. /* we didn't recognize the url */
  968. write_http_status_line(conn, 404, "Not found");
  969. tor_free(url);
  970. return 0;
  971. }
  972. /** Called when a dirserver receives data on a directory connection;
  973. * looks for an HTTP request. If the request is complete, remove it
  974. * from the inbuf, try to process it; otherwise, leave it on the
  975. * buffer. Return a 0 on success, or -1 on error.
  976. */
  977. static int directory_handle_command(connection_t *conn) {
  978. char *headers=NULL, *body=NULL;
  979. size_t body_len=0;
  980. int r;
  981. tor_assert(conn);
  982. tor_assert(conn->type == CONN_TYPE_DIR);
  983. switch (fetch_from_buf_http(conn->inbuf,
  984. &headers, MAX_HEADERS_SIZE,
  985. &body, &body_len, MAX_BODY_SIZE)) {
  986. case -1: /* overflow */
  987. log_fn(LOG_WARN,"Invalid input from address '%s'. Closing.", conn->address);
  988. return -1;
  989. case 0:
  990. log_fn(LOG_DEBUG,"command not all here yet.");
  991. return 0;
  992. /* case 1, fall through */
  993. }
  994. log_fn(LOG_DEBUG,"headers '%s', body '%s'.", headers, body);
  995. if (!strncasecmp(headers,"GET",3))
  996. r = directory_handle_command_get(conn, headers, body, body_len);
  997. else if (!strncasecmp(headers,"POST",4))
  998. r = directory_handle_command_post(conn, headers, body, body_len);
  999. else {
  1000. log_fn(LOG_WARN,"Got headers '%s' with unknown command. Closing.", headers);
  1001. r = -1;
  1002. }
  1003. tor_free(headers); tor_free(body);
  1004. return r;
  1005. }
  1006. /** Write handler for directory connections; called when all data has
  1007. * been flushed. Close the connection or wait for a response as
  1008. * appropriate.
  1009. */
  1010. int connection_dir_finished_flushing(connection_t *conn) {
  1011. tor_assert(conn);
  1012. tor_assert(conn->type == CONN_TYPE_DIR);
  1013. switch (conn->state) {
  1014. case DIR_CONN_STATE_CLIENT_SENDING:
  1015. log_fn(LOG_DEBUG,"client finished sending command.");
  1016. conn->state = DIR_CONN_STATE_CLIENT_READING;
  1017. connection_stop_writing(conn);
  1018. return 0;
  1019. case DIR_CONN_STATE_SERVER_WRITING:
  1020. log_fn(LOG_INFO,"Finished writing server response. Closing.");
  1021. connection_mark_for_close(conn);
  1022. return 0;
  1023. default:
  1024. log_fn(LOG_WARN,"Bug: called in unexpected state %d.", conn->state);
  1025. #ifdef TOR_FRAGILE
  1026. tor_assert(0);
  1027. #endif
  1028. return -1;
  1029. }
  1030. return 0;
  1031. }
  1032. /** Connected handler for directory connections: begin sending data to the
  1033. * server */
  1034. int connection_dir_finished_connecting(connection_t *conn)
  1035. {
  1036. tor_assert(conn);
  1037. tor_assert(conn->type == CONN_TYPE_DIR);
  1038. tor_assert(conn->state == DIR_CONN_STATE_CONNECTING);
  1039. log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
  1040. conn->address,conn->port);
  1041. conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
  1042. return 0;
  1043. }