connection_edge.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. #include "tree.h"
  6. extern or_options_t options; /* command-line and config-file options */
  7. extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
  8. static int connection_ap_handshake_process_socks(connection_t *conn);
  9. static int connection_ap_handshake_attach_circuit(connection_t *conn);
  10. static int connection_ap_handshake_attach_circuit_helper(connection_t *conn);
  11. static void connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  12. static int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  13. static void connection_edge_consider_sending_sendme(connection_t *conn);
  14. static uint32_t client_dns_lookup_entry(const char *address);
  15. static void client_dns_set_entry(const char *address, uint32_t val);
  16. void relay_header_pack(char *dest, const relay_header_t *src) {
  17. uint16_t tmp;
  18. /* we have to do slow memcpy's here, because we screwed up
  19. * and made our cell payload not word-aligned. we should fix
  20. * this someday.
  21. */
  22. *(uint8_t*)(dest) = src->command;
  23. tmp = htons(src->recognized);
  24. memcpy(dest+1, &tmp, 2);
  25. tmp = htons(src->stream_id);
  26. memcpy(dest+3, &tmp, 2);
  27. memcpy(dest+5, src->integrity, 4);
  28. tmp = htons(src->length);
  29. memcpy(dest+9, &tmp, 2);
  30. #if 0
  31. *(uint8_t*)(dest) = src->command;
  32. *(uint16_t*)(dest+1) = htons(src->recognized);
  33. *(uint16_t*)(dest+3) = htons(src->stream_id);
  34. memcpy(dest+5, src->integrity, 4);
  35. *(uint16_t*)(dest+9) = htons(src->length);
  36. #endif
  37. }
  38. void relay_header_unpack(relay_header_t *dest, const char *src) {
  39. dest->command = *(uint8_t*)(src);
  40. memcpy(&dest->recognized, src+1, 2);
  41. dest->recognized = ntohs(dest->recognized);
  42. memcpy(&dest->stream_id, src+3, 2);
  43. dest->stream_id = ntohs(dest->stream_id);
  44. memcpy(dest->integrity, src+5, 4);
  45. memcpy(&dest->length, src+9, 2);
  46. dest->length = ntohs(dest->length);
  47. #if 0
  48. dest->command = *(uint8_t*)(src);
  49. dest->recognized = ntohs(*(uint16_t*)(src+1));
  50. dest->stream_id = ntohs(*(uint16_t*)(src+3));
  51. memcpy(dest->integrity, src+5, 4);
  52. dest->length = ntohs(*(uint16_t*)(src+9));
  53. #endif
  54. }
  55. int connection_edge_process_inbuf(connection_t *conn) {
  56. assert(conn);
  57. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  58. if(conn->inbuf_reached_eof) {
  59. #ifdef HALF_OPEN
  60. /* eof reached; we're done reading, but we might want to write more. */
  61. conn->done_receiving = 1;
  62. shutdown(conn->s, 0); /* XXX check return, refactor NM */
  63. if (conn->done_sending) {
  64. connection_mark_for_close(conn, END_STREAM_REASON_DONE);
  65. } else {
  66. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_END,
  67. NULL, 0, conn->cpath_layer);
  68. }
  69. return 0;
  70. #else
  71. /* eof reached, kill it. */
  72. log_fn(LOG_INFO,"conn (fd %d) reached eof. Closing.", conn->s);
  73. connection_mark_for_close(conn, END_STREAM_REASON_DONE);
  74. conn->hold_open_until_flushed = 1; /* just because we shouldn't read
  75. doesn't mean we shouldn't write */
  76. return 0;
  77. #endif
  78. }
  79. switch(conn->state) {
  80. case AP_CONN_STATE_SOCKS_WAIT:
  81. if(connection_ap_handshake_process_socks(conn) < 0) {
  82. connection_mark_for_close(conn, END_STREAM_REASON_MISC);
  83. conn->hold_open_until_flushed = 1;
  84. return -1;
  85. }
  86. return 0;
  87. case AP_CONN_STATE_OPEN:
  88. case EXIT_CONN_STATE_OPEN:
  89. if(conn->package_window <= 0) {
  90. log_fn(LOG_WARN,"called with package_window %d. Tell Roger.", conn->package_window);
  91. return 0;
  92. }
  93. if(connection_edge_package_raw_inbuf(conn) < 0) {
  94. connection_mark_for_close(conn, END_STREAM_REASON_MISC);
  95. return -1;
  96. }
  97. return 0;
  98. case EXIT_CONN_STATE_CONNECTING:
  99. log_fn(LOG_INFO,"text from server while in 'connecting' state at exit. Leaving it on buffer.");
  100. return 0;
  101. }
  102. log_fn(LOG_WARN,"Got unexpected state %d. Closing.",conn->state);
  103. return -1;
  104. }
  105. int connection_edge_destroy(uint16_t circ_id, connection_t *conn) {
  106. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  107. if(conn->marked_for_close)
  108. return 0; /* already marked; probably got an 'end' */
  109. log_fn(LOG_INFO,"CircID %d: At an edge. Marking connection for close.",
  110. circ_id);
  111. conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
  112. connection_mark_for_close(conn, END_STREAM_REASON_DESTROY);
  113. conn->hold_open_until_flushed = 1;
  114. return 0;
  115. }
  116. static char *connection_edge_end_reason(char *payload, uint16_t length) {
  117. if(length < 1) {
  118. log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
  119. return "MALFORMED";
  120. }
  121. if(*payload < _MIN_END_STREAM_REASON || *payload > _MAX_END_STREAM_REASON) {
  122. log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",*payload);
  123. return "MALFORMED";
  124. }
  125. switch(*payload) {
  126. case END_STREAM_REASON_MISC: return "misc error";
  127. case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
  128. case END_STREAM_REASON_CONNECTFAILED: return "connect failed";
  129. case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
  130. case END_STREAM_REASON_DESTROY: return "destroyed";
  131. case END_STREAM_REASON_DONE: return "closed normally";
  132. case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
  133. }
  134. assert(0);
  135. return "";
  136. }
  137. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer) {
  138. char payload[5];
  139. int payload_len=1;
  140. circuit_t *circ;
  141. if(conn->has_sent_end) {
  142. log_fn(LOG_WARN,"It appears I've already sent the end. Are you calling me twice?");
  143. return -1;
  144. }
  145. payload[0] = reason;
  146. if(reason == END_STREAM_REASON_EXITPOLICY) {
  147. uint32_t tmp = htonl(conn->addr);
  148. memcpy(payload+1, &tmp, 4);
  149. // *(uint32_t *)(payload+1) = htonl(conn->addr);
  150. payload_len += 4;
  151. }
  152. circ = circuit_get_by_conn(conn);
  153. if(circ && !circ->marked_for_close) {
  154. log_fn(LOG_DEBUG,"Marking conn (fd %d) and sending end.",conn->s);
  155. connection_edge_send_command(conn, circ, RELAY_COMMAND_END,
  156. payload, payload_len, cpath_layer);
  157. } else {
  158. log_fn(LOG_DEBUG,"Marking conn (fd %d); no circ to send end.",conn->s);
  159. }
  160. conn->has_sent_end = 1;
  161. return 0;
  162. }
  163. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
  164. void *payload, int payload_len, crypt_path_t *cpath_layer) {
  165. cell_t cell;
  166. relay_header_t rh;
  167. int cell_direction;
  168. if(!circ) {
  169. log_fn(LOG_WARN,"no circ. Closing conn.");
  170. assert(fromconn);
  171. connection_mark_for_close(fromconn, 0);
  172. return -1;
  173. }
  174. memset(&cell, 0, sizeof(cell_t));
  175. cell.command = CELL_RELAY;
  176. // if(fromconn && fromconn->type == CONN_TYPE_AP) {
  177. if(cpath_layer) {
  178. cell.circ_id = circ->n_circ_id;
  179. cell_direction = CELL_DIRECTION_OUT;
  180. } else {
  181. cell.circ_id = circ->p_circ_id;
  182. cell_direction = CELL_DIRECTION_IN;
  183. }
  184. memset(&rh, 0, sizeof(rh));
  185. rh.command = relay_command;
  186. if(fromconn)
  187. rh.stream_id = fromconn->stream_id; /* else it's 0 */
  188. rh.length = payload_len;
  189. relay_header_pack(cell.payload, &rh);
  190. if(payload_len)
  191. memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
  192. log_fn(LOG_DEBUG,"delivering %d cell %s.", relay_command,
  193. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  194. if(circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
  195. log_fn(LOG_WARN,"circuit_package_relay_cell failed. Closing.");
  196. circuit_mark_for_close(circ);
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. /* an incoming relay cell has arrived. return -1 if you want to tear down the
  202. * circuit, else 0. */
  203. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  204. int edge_type, crypt_path_t *layer_hint) {
  205. static int num_seen=0;
  206. uint32_t addr;
  207. relay_header_t rh;
  208. assert(cell && circ);
  209. relay_header_unpack(&rh, cell->payload);
  210. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  211. num_seen++;
  212. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  213. /* either conn is NULL, in which case we've got a control cell, or else
  214. * conn points to the recognized stream. */
  215. if(conn && conn->state != AP_CONN_STATE_OPEN && conn->state != EXIT_CONN_STATE_OPEN) {
  216. if(rh.command == RELAY_COMMAND_END) {
  217. log_fn(LOG_INFO,"Edge got end (%s) before we're connected. Marking for close.",
  218. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length));
  219. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  220. connection_mark_for_close(conn, 0);
  221. /* XXX This is where we should check if reason is EXITPOLICY, and reattach */
  222. /* XXX here we should send a socks reject back if necessary, and hold
  223. * open til flushed */
  224. return 0;
  225. }
  226. if(conn->type == CONN_TYPE_AP && rh.command == RELAY_COMMAND_CONNECTED) {
  227. if(conn->state != AP_CONN_STATE_CONNECT_WAIT) {
  228. log_fn(LOG_WARN,"Got 'connected' while not in state connect_wait. Dropping.");
  229. return 0;
  230. }
  231. // log_fn(LOG_INFO,"Connected! Notifying application.");
  232. conn->state = AP_CONN_STATE_OPEN;
  233. if (rh.length >= 4) {
  234. memcpy(&addr, cell->payload + RELAY_HEADER_SIZE, 4);
  235. addr = ntohl(addr);
  236. // addr = ntohl(*(uint32_t*)(cell->payload + RELAY_HEADER_SIZE));
  237. client_dns_set_entry(conn->socks_request->address, addr);
  238. }
  239. log_fn(LOG_INFO,"'connected' received after %d seconds.",
  240. (int)(time(NULL) - conn->timestamp_lastread));
  241. circuit_log_path(LOG_INFO,circ);
  242. connection_ap_handshake_socks_reply(conn, NULL, 0, 1);
  243. conn->socks_request->has_finished = 1;
  244. return 0;
  245. } else {
  246. log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
  247. rh.command, conn->state, conn_state_to_string[conn->type][conn->state]);
  248. connection_mark_for_close(conn, END_STREAM_REASON_MISC);
  249. return -1;
  250. }
  251. }
  252. switch(rh.command) {
  253. case RELAY_COMMAND_DROP:
  254. log_fn(LOG_INFO,"Got a relay-level padding cell. Dropping.");
  255. return 0;
  256. case RELAY_COMMAND_BEGIN:
  257. if(edge_type == EDGE_AP) {
  258. log_fn(LOG_WARN,"relay begin request unsupported at AP. Dropping.");
  259. return 0;
  260. }
  261. if(conn) {
  262. log_fn(LOG_WARN,"begin cell for known stream. Dropping.");
  263. return 0;
  264. }
  265. connection_exit_begin_conn(cell, circ);
  266. return 0;
  267. case RELAY_COMMAND_DATA:
  268. ++stats_n_data_cells_received;
  269. if((edge_type == EDGE_AP && --layer_hint->deliver_window < 0) ||
  270. (edge_type == EDGE_EXIT && --circ->deliver_window < 0)) {
  271. log_fn(LOG_WARN,"(relay data) circ deliver_window below 0. Killing.");
  272. connection_mark_for_close(conn, END_STREAM_REASON_MISC);
  273. return -1;
  274. }
  275. log_fn(LOG_DEBUG,"circ deliver_window now %d.", edge_type == EDGE_AP ?
  276. layer_hint->deliver_window : circ->deliver_window);
  277. circuit_consider_sending_sendme(circ, edge_type, layer_hint);
  278. if(!conn) {
  279. log_fn(LOG_INFO,"data cell dropped, unknown stream.");
  280. return 0;
  281. }
  282. if(--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  283. log_fn(LOG_WARN,"(relay data) conn deliver_window below 0. Killing.");
  284. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  285. }
  286. stats_n_data_bytes_received += rh.length;
  287. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  288. rh.length, conn);
  289. connection_edge_consider_sending_sendme(conn);
  290. return 0;
  291. case RELAY_COMMAND_END:
  292. if(!conn) {
  293. log_fn(LOG_INFO,"end cell (%s) dropped, unknown stream.",
  294. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length));
  295. return 0;
  296. }
  297. if(rh.length >= 5 &&
  298. *(cell->payload+RELAY_HEADER_SIZE) == END_STREAM_REASON_EXITPOLICY) {
  299. /* XXX this will never be reached, since we're in 'connect_wait' state
  300. * but this is only reached when we're in 'open' state. Put it higher. */
  301. /* No need to close the connection. We'll hold it open while
  302. * we try a new exit node.
  303. * cell->payload+RELAY_HEADER_SIZE+1 holds the destination addr.
  304. */
  305. memcpy(&addr, cell->payload+RELAY_HEADER_SIZE+1, 4);
  306. addr = ntohl(addr);
  307. // addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE+1));
  308. client_dns_set_entry(conn->socks_request->address, addr);
  309. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  310. if(connection_ap_handshake_attach_circuit(conn) >= 0)
  311. return 0;
  312. /* else, conn will get closed below */
  313. }
  314. /* XXX add to this log_fn the exit node's nickname? */
  315. log_fn(LOG_INFO,"end cell (%s) for stream %d. Removing stream.",
  316. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length),
  317. conn->stream_id);
  318. #ifdef HALF_OPEN
  319. conn->done_sending = 1;
  320. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  321. if (conn->done_receiving) {
  322. /* We just *got* an end; no reason to send one. */
  323. conn->has_sent_end = 1;
  324. connection_mark_for_close(conn, 0);
  325. conn->hold_open_until_flushed = 1;
  326. }
  327. #else
  328. /* We just *got* an end; no reason to send one. */
  329. conn->has_sent_end = 1;
  330. connection_mark_for_close(conn, 0);
  331. conn->hold_open_until_flushed = 1;
  332. #endif
  333. return 0;
  334. case RELAY_COMMAND_EXTEND:
  335. if(conn) {
  336. log_fn(LOG_WARN,"'extend' for non-zero stream. Dropping.");
  337. return 0;
  338. }
  339. return circuit_extend(cell, circ);
  340. case RELAY_COMMAND_EXTENDED:
  341. if(edge_type == EDGE_EXIT) {
  342. log_fn(LOG_WARN,"'extended' unsupported at exit. Dropping.");
  343. return 0;
  344. }
  345. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  346. if(circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  347. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  348. return -1;
  349. }
  350. if (circuit_send_next_onion_skin(circ)<0) {
  351. log_fn(LOG_INFO,"circuit_send_next_onion_skin() failed.");
  352. return -1;
  353. }
  354. return 0;
  355. case RELAY_COMMAND_TRUNCATE:
  356. if(edge_type == EDGE_AP) {
  357. log_fn(LOG_WARN,"'truncate' unsupported at AP. Dropping.");
  358. return 0;
  359. }
  360. if(circ->n_conn) {
  361. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  362. circ->n_conn = NULL;
  363. }
  364. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  365. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  366. NULL, 0, NULL);
  367. return 0;
  368. case RELAY_COMMAND_TRUNCATED:
  369. if(edge_type == EDGE_EXIT) {
  370. log_fn(LOG_WARN,"'truncated' unsupported at exit. Dropping.");
  371. return 0;
  372. }
  373. circuit_truncated(circ, layer_hint);
  374. return 0;
  375. case RELAY_COMMAND_CONNECTED:
  376. if(conn) {
  377. log_fn(LOG_WARN,"'connected' unsupported while open. Closing conn.");
  378. return -1;
  379. }
  380. log_fn(LOG_INFO,"'connected' received, no conn attached anymore. Ignoring.");
  381. return 0;
  382. case RELAY_COMMAND_SENDME:
  383. if(!conn) {
  384. if(edge_type == EDGE_AP) {
  385. assert(layer_hint);
  386. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  387. log_fn(LOG_DEBUG,"circ-level sendme at AP, packagewindow %d.",
  388. layer_hint->package_window);
  389. circuit_resume_edge_reading(circ, EDGE_AP, layer_hint);
  390. } else {
  391. assert(!layer_hint);
  392. circ->package_window += CIRCWINDOW_INCREMENT;
  393. log_fn(LOG_DEBUG,"circ-level sendme at exit, packagewindow %d.",
  394. circ->package_window);
  395. circuit_resume_edge_reading(circ, EDGE_EXIT, layer_hint);
  396. }
  397. return 0;
  398. }
  399. conn->package_window += STREAMWINDOW_INCREMENT;
  400. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  401. connection_start_reading(conn);
  402. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  403. return 0;
  404. }
  405. log_fn(LOG_WARN,"unknown relay command %d.",rh.command);
  406. return -1;
  407. }
  408. int connection_edge_finished_flushing(connection_t *conn) {
  409. unsigned char connected_payload[4];
  410. int e, len=sizeof(e);
  411. assert(conn);
  412. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  413. switch(conn->state) {
  414. case EXIT_CONN_STATE_CONNECTING:
  415. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  416. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  417. /* yuck. kill it. */
  418. log_fn(LOG_DEBUG,"in-progress exit connect failed. Removing.");
  419. connection_mark_for_close(conn, END_STREAM_REASON_CONNECTFAILED);
  420. return -1;
  421. } else {
  422. log_fn(LOG_DEBUG,"in-progress exit connect still waiting.");
  423. return 0; /* no change, see if next time is better */
  424. }
  425. }
  426. /* the connect has finished. */
  427. log_fn(LOG_INFO,"Exit connection to %s:%u established.",
  428. conn->address,conn->port);
  429. conn->state = EXIT_CONN_STATE_OPEN;
  430. connection_watch_events(conn, POLLIN); /* stop writing, continue reading */
  431. if(connection_wants_to_flush(conn)) /* in case there are any queued relay cells */
  432. connection_start_writing(conn);
  433. /* deliver a 'connected' relay cell back through the circuit. */
  434. *(uint32_t*)connected_payload = htonl(conn->addr);
  435. if(connection_edge_send_command(conn, circuit_get_by_conn(conn),
  436. RELAY_COMMAND_CONNECTED, connected_payload, 4, NULL) < 0)
  437. return 0; /* circuit is closed, don't continue */
  438. assert(conn->package_window > 0);
  439. return connection_edge_process_inbuf(conn); /* in case the server has written anything */
  440. case AP_CONN_STATE_OPEN:
  441. case EXIT_CONN_STATE_OPEN:
  442. connection_stop_writing(conn);
  443. connection_edge_consider_sending_sendme(conn);
  444. return 0;
  445. case AP_CONN_STATE_SOCKS_WAIT:
  446. case AP_CONN_STATE_CIRCUIT_WAIT:
  447. case AP_CONN_STATE_CONNECT_WAIT:
  448. connection_stop_writing(conn);
  449. return 0;
  450. default:
  451. log_fn(LOG_WARN,"BUG: called in unexpected state: %d", conn->state);
  452. return -1;
  453. }
  454. return 0;
  455. }
  456. uint64_t stats_n_data_cells_packaged = 0;
  457. uint64_t stats_n_data_bytes_packaged = 0;
  458. uint64_t stats_n_data_cells_received = 0;
  459. uint64_t stats_n_data_bytes_received = 0;
  460. int connection_edge_package_raw_inbuf(connection_t *conn) {
  461. int amount_to_process, length;
  462. char payload[CELL_PAYLOAD_SIZE];
  463. circuit_t *circ;
  464. assert(conn);
  465. assert(!connection_speaks_cells(conn));
  466. repeat_connection_edge_package_raw_inbuf:
  467. circ = circuit_get_by_conn(conn);
  468. if(!circ) {
  469. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  470. return -1;
  471. }
  472. if(circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer))
  473. return 0;
  474. if(conn->package_window <= 0) {
  475. log_fn(LOG_WARN,"called with package_window %d. Tell Roger.", conn->package_window);
  476. connection_stop_reading(conn);
  477. return 0;
  478. }
  479. amount_to_process = buf_datalen(conn->inbuf);
  480. if(!amount_to_process)
  481. return 0;
  482. if(amount_to_process > RELAY_PAYLOAD_SIZE) {
  483. length = RELAY_PAYLOAD_SIZE;
  484. } else {
  485. length = amount_to_process;
  486. }
  487. stats_n_data_bytes_packaged += length;
  488. stats_n_data_cells_packaged += 1;
  489. connection_fetch_from_buf(payload, length, conn);
  490. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).", conn->s, length,
  491. (int)buf_datalen(conn->inbuf));
  492. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
  493. payload, length, conn->cpath_layer) < 0)
  494. return 0; /* circuit is closed, don't continue */
  495. if(conn->type == CONN_TYPE_EXIT) {
  496. assert(circ->package_window > 0);
  497. circ->package_window--;
  498. } else { /* we're an AP */
  499. assert(conn->type == CONN_TYPE_AP);
  500. assert(conn->cpath_layer->package_window > 0);
  501. conn->cpath_layer->package_window--;
  502. }
  503. if(--conn->package_window <= 0) { /* is it 0 after decrement? */
  504. connection_stop_reading(conn);
  505. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  506. circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer);
  507. return 0; /* don't process the inbuf any more */
  508. }
  509. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  510. /* handle more if there's more, or return 0 if there isn't */
  511. goto repeat_connection_edge_package_raw_inbuf;
  512. }
  513. #define MAX_STREAM_RETRIES 4
  514. void connection_ap_expire_beginning(void) {
  515. connection_t **carray;
  516. connection_t *conn;
  517. circuit_t *circ;
  518. int n, i;
  519. time_t now = time(NULL);
  520. get_connection_array(&carray, &n);
  521. for (i = 0; i < n; ++i) {
  522. conn = carray[i];
  523. if (conn->type != CONN_TYPE_AP ||
  524. conn->state != AP_CONN_STATE_CONNECT_WAIT)
  525. continue;
  526. if (now - conn->timestamp_lastread < 15)
  527. continue;
  528. conn->num_retries++;
  529. circ = circuit_get_by_conn(conn);
  530. if(conn->num_retries >= MAX_STREAM_RETRIES) {
  531. log_fn(LOG_WARN,"Stream is %d seconds late. Giving up.",
  532. 15*conn->num_retries);
  533. circuit_log_path(LOG_WARN, circ);
  534. connection_mark_for_close(conn,END_STREAM_REASON_TIMEOUT);
  535. } else {
  536. log_fn(LOG_WARN,"Stream is %d seconds late. Retrying.",
  537. (int)(now - conn->timestamp_lastread));
  538. circuit_log_path(LOG_WARN, circ);
  539. /* send an end down the circuit */
  540. connection_edge_end(conn, END_STREAM_REASON_TIMEOUT, conn->cpath_layer);
  541. /* un-mark it as ending, since we're going to reuse it */
  542. conn->has_sent_end = 0;
  543. /* move it back into 'pending' state. */
  544. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  545. circuit_detach_stream(circ, conn);
  546. /* kludge to make us not try this circuit again, yet to allow
  547. * current streams on it to survive if they can: make it
  548. * unattractive to use for new streams */
  549. assert(circ->timestamp_dirty);
  550. circ->timestamp_dirty -= options.NewCircuitPeriod;
  551. /* give our stream another 15 seconds to try */
  552. conn->timestamp_lastread += 15;
  553. if(connection_ap_handshake_attach_circuit(conn)<0) {
  554. /* it will never work */
  555. /* Don't need to send end -- we're not connected */
  556. connection_mark_for_close(conn, 0);
  557. }
  558. } /* end if max_retries */
  559. } /* end for */
  560. }
  561. /* Tell any APs that are waiting for a new circuit that one is available */
  562. void connection_ap_attach_pending(void)
  563. {
  564. connection_t **carray;
  565. connection_t *conn;
  566. int n, i;
  567. get_connection_array(&carray, &n);
  568. for (i = 0; i < n; ++i) {
  569. conn = carray[i];
  570. if (conn->type != CONN_TYPE_AP ||
  571. conn->state != AP_CONN_STATE_CIRCUIT_WAIT)
  572. continue;
  573. if(connection_ap_handshake_attach_circuit(conn) < 0) {
  574. /* -1 means it will never work */
  575. /* Don't send end; there is no 'other side' yet */
  576. connection_mark_for_close(conn,0);
  577. }
  578. }
  579. }
  580. static void connection_edge_consider_sending_sendme(connection_t *conn) {
  581. circuit_t *circ;
  582. if(connection_outbuf_too_full(conn))
  583. return;
  584. circ = circuit_get_by_conn(conn);
  585. if(!circ) {
  586. /* this can legitimately happen if the destroy has already
  587. * arrived and torn down the circuit */
  588. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  589. return;
  590. }
  591. while(conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  592. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", conn->outbuf_flushlen);
  593. conn->deliver_window += STREAMWINDOW_INCREMENT;
  594. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
  595. NULL, 0, conn->cpath_layer) < 0) {
  596. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  597. return; /* the circuit's closed, don't continue */
  598. }
  599. }
  600. }
  601. static int connection_ap_handshake_process_socks(connection_t *conn) {
  602. socks_request_t *socks;
  603. int sockshere;
  604. assert(conn);
  605. assert(conn->type == CONN_TYPE_AP);
  606. assert(conn->state == AP_CONN_STATE_SOCKS_WAIT);
  607. assert(conn->socks_request);
  608. socks = conn->socks_request;
  609. log_fn(LOG_DEBUG,"entered.");
  610. sockshere = fetch_from_buf_socks(conn->inbuf, socks);
  611. if(sockshere == -1 || sockshere == 0) {
  612. if(socks->replylen) { /* we should send reply back */
  613. log_fn(LOG_DEBUG,"reply is already set for us. Using it.");
  614. connection_ap_handshake_socks_reply(conn, socks->reply, socks->replylen, 0);
  615. } else if(sockshere == -1) { /* send normal reject */
  616. log_fn(LOG_WARN,"Fetching socks handshake failed. Closing.");
  617. connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
  618. } else {
  619. log_fn(LOG_DEBUG,"socks handshake not all here yet.");
  620. }
  621. if (sockshere == -1)
  622. conn->socks_request->has_finished = 1;
  623. return sockshere;
  624. } /* else socks handshake is done, continue processing */
  625. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  626. return connection_ap_handshake_attach_circuit(conn);
  627. }
  628. static int connection_ap_handshake_attach_circuit(connection_t *conn) {
  629. /* try attaching. launch new circuit if needed.
  630. * return -1 if conn needs to die, else 0. */
  631. switch(connection_ap_handshake_attach_circuit_helper(conn)) {
  632. case -1: /* it will never work */
  633. return -1;
  634. case 0: /* no useful circuits available */
  635. if(!circuit_get_newest(conn, 0)) /* is one already on the way? */
  636. circuit_launch_new();
  637. return 0;
  638. default: /* case 1, it succeeded, great */
  639. return 0;
  640. }
  641. }
  642. /* Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  643. * we don't find one: if conn cannot be handled by any known nodes,
  644. * warn and return -1; else tell conn to stop reading and return 0.
  645. * Otherwise, associate conn with a safe live circuit, start
  646. * sending a BEGIN cell down the circuit, and return 1.
  647. */
  648. static int connection_ap_handshake_attach_circuit_helper(connection_t *conn) {
  649. circuit_t *circ;
  650. uint32_t addr;
  651. assert(conn);
  652. assert(conn->type == CONN_TYPE_AP);
  653. assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  654. assert(conn->socks_request);
  655. /* find the circuit that we should use, if there is one. */
  656. circ = circuit_get_newest(conn, 1);
  657. if(!circ) {
  658. log_fn(LOG_INFO,"No safe circuit ready for edge connection; delaying.");
  659. addr = client_dns_lookup_entry(conn->socks_request->address);
  660. if(router_exit_policy_all_routers_reject(addr, conn->socks_request->port)) {
  661. log_fn(LOG_WARN,"No Tor server exists that allows exit to %s:%d. Rejecting.",
  662. conn->socks_request->address, conn->socks_request->port);
  663. return -1;
  664. }
  665. connection_stop_reading(conn); /* don't read until the connected cell arrives */
  666. return 0;
  667. }
  668. connection_start_reading(conn);
  669. /* here, print the circ's path. so people can figure out which circs are sucking. */
  670. circuit_log_path(LOG_INFO,circ);
  671. if(!circ->timestamp_dirty)
  672. circ->timestamp_dirty = time(NULL);
  673. /* add it into the linked list of streams on this circuit */
  674. log_fn(LOG_DEBUG,"attaching new conn to circ. n_circ_id %d.", circ->n_circ_id);
  675. conn->next_stream = circ->p_streams;
  676. /* assert_connection_ok(conn, time(NULL)); */
  677. circ->p_streams = conn;
  678. assert(circ->cpath && circ->cpath->prev);
  679. assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  680. conn->cpath_layer = circ->cpath->prev;
  681. connection_ap_handshake_send_begin(conn, circ);
  682. return 1;
  683. }
  684. /* Iterate over the two bytes of stream_id until we get one that is not
  685. * already in use. Return 0 if can't get a unique stream_id.
  686. */
  687. static uint16_t get_unique_stream_id_by_circ(circuit_t *circ) {
  688. connection_t *tmpconn;
  689. uint16_t test_stream_id;
  690. uint32_t attempts=0;
  691. again:
  692. test_stream_id = circ->next_stream_id++;
  693. if(++attempts > 1<<16) {
  694. /* Make sure we don't loop forever if all stream_id's are used. */
  695. log_fn(LOG_WARN,"No unused stream IDs. Failing.");
  696. return 0;
  697. }
  698. if (test_stream_id == 0)
  699. goto again;
  700. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  701. if(tmpconn->stream_id == test_stream_id)
  702. goto again;
  703. return test_stream_id;
  704. }
  705. /* deliver the destaddr:destport in a relay cell */
  706. static void connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ)
  707. {
  708. char payload[CELL_PAYLOAD_SIZE];
  709. int payload_len;
  710. struct in_addr in;
  711. const char *string_addr;
  712. assert(ap_conn->type == CONN_TYPE_AP);
  713. assert(ap_conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  714. assert(ap_conn->socks_request);
  715. ap_conn->stream_id = get_unique_stream_id_by_circ(circ);
  716. if (ap_conn->stream_id==0) {
  717. /* Don't send end: there is no 'other side' yet */
  718. connection_mark_for_close(ap_conn, 0);
  719. return;
  720. }
  721. in.s_addr = htonl(client_dns_lookup_entry(ap_conn->socks_request->address));
  722. string_addr = in.s_addr ? inet_ntoa(in) : NULL;
  723. snprintf(payload,RELAY_PAYLOAD_SIZE,
  724. "%s:%d",
  725. string_addr ? string_addr : ap_conn->socks_request->address,
  726. ap_conn->socks_request->port);
  727. payload_len = strlen(payload)+1;
  728. log_fn(LOG_DEBUG,"Sending relay cell to begin stream %d.",ap_conn->stream_id);
  729. if(connection_edge_send_command(ap_conn, circ, RELAY_COMMAND_BEGIN,
  730. payload, payload_len, ap_conn->cpath_layer) < 0)
  731. return; /* circuit is closed, don't continue */
  732. ap_conn->package_window = STREAMWINDOW_START;
  733. ap_conn->deliver_window = STREAMWINDOW_START;
  734. ap_conn->state = AP_CONN_STATE_CONNECT_WAIT;
  735. /* XXX Right now, we rely on the socks client not to send us any data
  736. * XXX until we've sent back a socks reply. (If it does, we could wind
  737. * XXX up packaging that data and sending it to the exit, then later having
  738. * XXX the exit refuse us.)
  739. */
  740. log_fn(LOG_INFO,"Address/port sent, ap socket %d, n_circ_id %d",ap_conn->s,circ->n_circ_id);
  741. return;
  742. }
  743. void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  744. int replylen, char success) {
  745. char buf[256];
  746. if(replylen) { /* we already have a reply in mind */
  747. connection_write_to_buf(reply, replylen, conn);
  748. return;
  749. }
  750. assert(conn->socks_request);
  751. if(conn->socks_request->socks_version == 4) {
  752. memset(buf,0,SOCKS4_NETWORK_LEN);
  753. #define SOCKS4_GRANTED 90
  754. #define SOCKS4_REJECT 91
  755. buf[1] = (success ? SOCKS4_GRANTED : SOCKS4_REJECT);
  756. /* leave version, destport, destip zero */
  757. connection_write_to_buf(buf, SOCKS4_NETWORK_LEN, conn);
  758. }
  759. if(conn->socks_request->socks_version == 5) {
  760. buf[0] = 5; /* version 5 */
  761. #define SOCKS5_SUCCESS 0
  762. #define SOCKS5_GENERIC_ERROR 1
  763. buf[1] = success ? SOCKS5_SUCCESS : SOCKS5_GENERIC_ERROR;
  764. buf[2] = 0;
  765. buf[3] = 1; /* ipv4 addr */
  766. memset(buf+4,0,6); /* Set external addr/port to 0.
  767. The spec doesn't seem to say what to do here. -RD */
  768. connection_write_to_buf(buf,10,conn);
  769. }
  770. /* if socks_version isn't 4 or 5, don't send anything */
  771. return;
  772. }
  773. static int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
  774. connection_t *n_stream;
  775. relay_header_t rh;
  776. char *colon;
  777. relay_header_unpack(&rh, cell->payload);
  778. /* XXX currently we don't send an end cell back if we drop the
  779. * begin because it's malformed.
  780. */
  781. if(!memchr(cell->payload+RELAY_HEADER_SIZE, 0, rh.length)) {
  782. log_fn(LOG_WARN,"relay begin cell has no \\0. Dropping.");
  783. return 0;
  784. }
  785. colon = strchr(cell->payload+RELAY_HEADER_SIZE, ':');
  786. if(!colon) {
  787. log_fn(LOG_WARN,"relay begin cell has no colon. Dropping.");
  788. return 0;
  789. }
  790. *colon = 0;
  791. if(!atoi(colon+1)) { /* bad port */
  792. log_fn(LOG_WARN,"relay begin cell has invalid port. Dropping.");
  793. return 0;
  794. }
  795. log_fn(LOG_DEBUG,"Creating new exit connection.");
  796. n_stream = connection_new(CONN_TYPE_EXIT);
  797. n_stream->stream_id = rh.stream_id;
  798. n_stream->address = tor_strdup(cell->payload + RELAY_HEADER_SIZE);
  799. n_stream->port = atoi(colon+1);
  800. n_stream->state = EXIT_CONN_STATE_RESOLVING;
  801. /* leave n_stream->s at -1, because it's not yet valid */
  802. n_stream->package_window = STREAMWINDOW_START;
  803. n_stream->deliver_window = STREAMWINDOW_START;
  804. if(connection_add(n_stream) < 0) { /* no space, forget it */
  805. log_fn(LOG_WARN,"connection_add failed. Dropping.");
  806. connection_free(n_stream);
  807. return 0;
  808. }
  809. /* add it into the linked list of streams on this circuit */
  810. n_stream->next_stream = circ->n_streams;
  811. circ->n_streams = n_stream;
  812. /* send it off to the gethostbyname farm */
  813. switch(dns_resolve(n_stream)) {
  814. case 1: /* resolve worked */
  815. connection_exit_connect(n_stream);
  816. return 0;
  817. case -1: /* resolve failed */
  818. log_fn(LOG_INFO,"Resolve failed (%s).", n_stream->address);
  819. /* Set the state so that we don't try to remove n_stream from a DNS
  820. * pending list. */
  821. n_stream->state = EXIT_CONN_STATE_RESOLVEFAILED;
  822. connection_mark_for_close(n_stream, END_STREAM_REASON_RESOLVEFAILED);
  823. break;
  824. case 0: /* resolve added to pending list */
  825. ;
  826. }
  827. return 0;
  828. }
  829. void connection_exit_connect(connection_t *conn) {
  830. unsigned char connected_payload[4];
  831. if(router_compare_to_my_exit_policy(conn) == ADDR_POLICY_REJECTED) {
  832. log_fn(LOG_INFO,"%s:%d failed exit policy. Closing.", conn->address, conn->port);
  833. connection_mark_for_close(conn, END_STREAM_REASON_EXITPOLICY);
  834. return;
  835. }
  836. switch(connection_connect(conn, conn->address, conn->addr, conn->port)) {
  837. case -1:
  838. connection_mark_for_close(conn, END_STREAM_REASON_CONNECTFAILED);
  839. return;
  840. case 0:
  841. connection_set_poll_socket(conn);
  842. conn->state = EXIT_CONN_STATE_CONNECTING;
  843. connection_watch_events(conn, POLLOUT | POLLIN | POLLERR);
  844. /* writable indicates finish, readable indicates broken link,
  845. error indicates broken link in windowsland. */
  846. return;
  847. /* case 1: fall through */
  848. }
  849. connection_set_poll_socket(conn);
  850. conn->state = EXIT_CONN_STATE_OPEN;
  851. if(connection_wants_to_flush(conn)) { /* in case there are any queued data cells */
  852. log_fn(LOG_WARN,"tell roger: newly connected conn had data waiting!");
  853. // connection_start_writing(conn);
  854. }
  855. // connection_process_inbuf(conn);
  856. connection_watch_events(conn, POLLIN);
  857. /* also, deliver a 'connected' cell back through the circuit. */
  858. *(uint32_t*)connected_payload = htonl(conn->addr);
  859. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED,
  860. connected_payload, 4, NULL);
  861. }
  862. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit)
  863. {
  864. uint32_t addr;
  865. assert(conn);
  866. assert(conn->type == CONN_TYPE_AP);
  867. assert(conn->socks_request);
  868. log_fn(LOG_DEBUG,"considering nickname %s, for address %s / port %d:",
  869. exit->nickname, conn->socks_request->address,
  870. conn->socks_request->port);
  871. addr = client_dns_lookup_entry(conn->socks_request->address);
  872. return router_compare_addr_to_exit_policy(addr,
  873. conn->socks_request->port, exit->exit_policy);
  874. }
  875. /* ***** Client DNS code ***** */
  876. /* XXX Perhaps this should get merged with the dns.c code somehow. */
  877. /* XXX But we can't just merge them, because then nodes that act as
  878. * both OR and OP could be attacked: people could rig the dns cache
  879. * by answering funny things to stream begin requests, and later
  880. * other clients would reuse those funny addr's. Hm.
  881. */
  882. struct client_dns_entry {
  883. uint32_t addr;
  884. time_t expires;
  885. };
  886. static int client_dns_size = 0;
  887. static strmap_t *client_dns_map = NULL;
  888. void client_dns_init(void) {
  889. client_dns_map = strmap_new();
  890. client_dns_size = 0;
  891. }
  892. static uint32_t client_dns_lookup_entry(const char *address)
  893. {
  894. struct client_dns_entry *ent;
  895. struct in_addr in;
  896. time_t now;
  897. assert(address);
  898. if (tor_inet_aton(address, &in)) {
  899. log_fn(LOG_DEBUG, "Using static address %s (%08lX)", address,
  900. (unsigned long)ntohl(in.s_addr));
  901. return ntohl(in.s_addr);
  902. }
  903. ent = strmap_get_lc(client_dns_map,address);
  904. if (!ent) {
  905. log_fn(LOG_DEBUG, "No entry found for address %s", address);
  906. return 0;
  907. } else {
  908. now = time(NULL);
  909. if (ent->expires < now) {
  910. log_fn(LOG_DEBUG, "Expired entry found for address %s", address);
  911. strmap_remove_lc(client_dns_map,address);
  912. tor_free(ent);
  913. --client_dns_size;
  914. return 0;
  915. }
  916. in.s_addr = htonl(ent->addr);
  917. log_fn(LOG_DEBUG, "Found cached entry for address %s: %s", address,
  918. inet_ntoa(in));
  919. return ent->addr;
  920. }
  921. }
  922. static void client_dns_set_entry(const char *address, uint32_t val)
  923. {
  924. struct client_dns_entry *ent;
  925. struct in_addr in;
  926. time_t now;
  927. assert(address);
  928. assert(val);
  929. if (tor_inet_aton(address, &in))
  930. return;
  931. now = time(NULL);
  932. ent = strmap_get_lc(client_dns_map, address);
  933. if (ent) {
  934. in.s_addr = htonl(val);
  935. log_fn(LOG_DEBUG, "Updating entry for address %s: %s", address,
  936. inet_ntoa(in));
  937. ent->addr = val;
  938. ent->expires = now+MAX_DNS_ENTRY_AGE;
  939. } else {
  940. in.s_addr = htonl(val);
  941. log_fn(LOG_DEBUG, "Caching result for address %s: %s", address,
  942. inet_ntoa(in));
  943. ent = tor_malloc(sizeof(struct client_dns_entry));
  944. ent->addr = val;
  945. ent->expires = now+MAX_DNS_ENTRY_AGE;
  946. strmap_set_lc(client_dns_map, address, ent);
  947. ++client_dns_size;
  948. }
  949. }
  950. static void* _remove_if_expired(const char *addr,
  951. struct client_dns_entry *ent,
  952. time_t *nowp)
  953. {
  954. if (ent->expires < *nowp) {
  955. --client_dns_size;
  956. tor_free(ent);
  957. return NULL;
  958. } else {
  959. return ent;
  960. }
  961. }
  962. void client_dns_clean(void)
  963. {
  964. time_t now;
  965. if(!client_dns_size)
  966. return;
  967. now = time(NULL);
  968. strmap_foreach(client_dns_map, (strmap_foreach_fn)_remove_if_expired, &now);
  969. }
  970. /*
  971. Local Variables:
  972. mode:c
  973. indent-tabs-mode:nil
  974. c-basic-offset:2
  975. End:
  976. */