connection.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. extern or_options_t options; /* command-line and config-file options */
  7. extern int global_read_bucket;
  8. char *conn_type_to_string[] = {
  9. "", /* 0 */
  10. "OP listener", /* 1 */
  11. "OP", /* 2 */
  12. "OR listener", /* 3 */
  13. "OR", /* 4 */
  14. "Exit", /* 5 */
  15. "App listener",/* 6 */
  16. "App", /* 7 */
  17. "Dir listener",/* 8 */
  18. "Dir", /* 9 */
  19. "DNS worker", /* 10 */
  20. "CPU worker", /* 11 */
  21. };
  22. char *conn_state_to_string[][_CONN_TYPE_MAX+1] = {
  23. { NULL }, /* no type associated with 0 */
  24. { NULL }, /* op listener, obsolete */
  25. { NULL }, /* op, obsolete */
  26. { "ready" }, /* or listener, 0 */
  27. { "", /* OR, 0 */
  28. "connect()ing", /* 1 */
  29. "handshaking", /* 2 */
  30. "open" }, /* 3 */
  31. { "", /* exit, 0 */
  32. "waiting for dest info", /* 1 */
  33. "connecting", /* 2 */
  34. "open" }, /* 3 */
  35. { "ready" }, /* app listener, 0 */
  36. { "", /* 0 */
  37. "", /* 1 */
  38. "", /* 2 */
  39. "", /* 3 */
  40. "awaiting dest info", /* app, 4 */
  41. "waiting for safe circuit", /* 5 */
  42. "waiting for connected", /* 6 */
  43. "open" }, /* 7 */
  44. { "ready" }, /* dir listener, 0 */
  45. { "", /* dir, 0 */
  46. "connecting (fetch)", /* 1 */
  47. "connecting (upload)", /* 2 */
  48. "client sending fetch", /* 3 */
  49. "client sending upload", /* 4 */
  50. "client reading fetch", /* 5 */
  51. "client reading upload", /* 6 */
  52. "awaiting command", /* 7 */
  53. "writing" }, /* 8 */
  54. { "", /* dns worker, 0 */
  55. "idle", /* 1 */
  56. "busy" }, /* 2 */
  57. { "", /* cpu worker, 0 */
  58. "idle", /* 1 */
  59. "busy with onion", /* 2 */
  60. "busy with handshake" }, /* 3 */
  61. };
  62. /********* END VARIABLES ************/
  63. static int connection_init_accepted_conn(connection_t *conn);
  64. static int connection_handle_listener_read(connection_t *conn, int new_type);
  65. /**************************************************************/
  66. connection_t *connection_new(int type) {
  67. connection_t *conn;
  68. time_t now = time(NULL);
  69. conn = tor_malloc_zero(sizeof(connection_t));
  70. conn->magic = CONNECTION_MAGIC;
  71. conn->s = -1; /* give it a default of 'not used' */
  72. conn->type = type;
  73. if(!connection_is_listener(conn)) { /* listeners never use their buf */
  74. conn->inbuf = buf_new();
  75. conn->outbuf = buf_new();
  76. }
  77. if (type == CONN_TYPE_AP) {
  78. conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
  79. }
  80. conn->next_circ_id = crypto_pseudo_rand_int(1<<15);
  81. conn->timestamp_created = now;
  82. conn->timestamp_lastread = now;
  83. conn->timestamp_lastwritten = now;
  84. return conn;
  85. }
  86. void connection_free(connection_t *conn) {
  87. assert(conn);
  88. assert(conn->magic == CONNECTION_MAGIC);
  89. if(!connection_is_listener(conn)) {
  90. buf_free(conn->inbuf);
  91. buf_free(conn->outbuf);
  92. }
  93. tor_free(conn->address);
  94. if(connection_speaks_cells(conn)) {
  95. directory_set_dirty();
  96. if (conn->tls)
  97. tor_tls_free(conn->tls);
  98. }
  99. if (conn->onion_pkey)
  100. crypto_free_pk_env(conn->onion_pkey);
  101. if (conn->link_pkey)
  102. crypto_free_pk_env(conn->link_pkey);
  103. if (conn->identity_pkey)
  104. crypto_free_pk_env(conn->identity_pkey);
  105. tor_free(conn->nickname);
  106. tor_free(conn->socks_request);
  107. if(conn->s >= 0) {
  108. log_fn(LOG_INFO,"closing fd %d.",conn->s);
  109. close(conn->s);
  110. }
  111. memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */
  112. free(conn);
  113. }
  114. void connection_free_all(void) {
  115. int i, n;
  116. connection_t **carray;
  117. get_connection_array(&carray,&n);
  118. for(i=0;i<n;i++)
  119. connection_free(carray[i]);
  120. }
  121. int connection_create_listener(char *bindaddress, uint16_t bindport, int type) {
  122. struct sockaddr_in bindaddr; /* where to bind */
  123. struct hostent *rent;
  124. connection_t *conn;
  125. int s; /* the socket we're going to make */
  126. int one=1;
  127. memset(&bindaddr,0,sizeof(struct sockaddr_in));
  128. bindaddr.sin_family = AF_INET;
  129. bindaddr.sin_port = htons(bindport);
  130. rent = gethostbyname(bindaddress);
  131. if (!rent) {
  132. log_fn(LOG_WARN,"Can't resolve BindAddress %s",bindaddress);
  133. return -1;
  134. }
  135. if(rent->h_length != 4)
  136. return -1; /* XXX complain */
  137. memcpy(&(bindaddr.sin_addr.s_addr),rent->h_addr,rent->h_length);
  138. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  139. if (s < 0) {
  140. log_fn(LOG_WARN,"Socket creation failed.");
  141. return -1;
  142. }
  143. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  144. if(bind(s,(struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) {
  145. log_fn(LOG_WARN,"Could not bind to port %u: %s",bindport,strerror(errno));
  146. return -1;
  147. }
  148. if(listen(s,SOMAXCONN) < 0) {
  149. log_fn(LOG_WARN,"Could not listen on port %u: %s",bindport,strerror(errno));
  150. return -1;
  151. }
  152. set_socket_nonblocking(s);
  153. conn = connection_new(type);
  154. conn->s = s;
  155. if(connection_add(conn) < 0) { /* no space, forget it */
  156. log_fn(LOG_WARN,"connection_add failed. Giving up.");
  157. connection_free(conn);
  158. return -1;
  159. }
  160. log_fn(LOG_DEBUG,"%s listening on port %u.",conn_type_to_string[type], bindport);
  161. conn->state = LISTENER_STATE_READY;
  162. connection_start_reading(conn);
  163. return 0;
  164. }
  165. static int connection_handle_listener_read(connection_t *conn, int new_type) {
  166. int news; /* the new socket */
  167. connection_t *newconn;
  168. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  169. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  170. #ifdef MS_WINDOWS
  171. int e;
  172. #endif
  173. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  174. if (news == -1) { /* accept() error */
  175. if(ERRNO_EAGAIN(errno)) {
  176. #ifdef MS_WINDOWS
  177. e = correct_socket_errno(conn->s);
  178. if (ERRNO_EAGAIN(e))
  179. return 0;
  180. #else
  181. return 0; /* he hung up before we could accept(). that's fine. */
  182. #endif
  183. }
  184. /* else there was a real error. */
  185. log_fn(LOG_WARN,"accept() failed. Closing listener.");
  186. return -1;
  187. }
  188. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  189. set_socket_nonblocking(news);
  190. newconn = connection_new(new_type);
  191. newconn->s = news;
  192. newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  193. newconn->addr = ntohl(remote.sin_addr.s_addr);
  194. newconn->port = ntohs(remote.sin_port);
  195. if(connection_add(newconn) < 0) { /* no space, forget it */
  196. connection_free(newconn);
  197. return 0; /* no need to tear down the parent */
  198. }
  199. if(connection_init_accepted_conn(newconn) < 0) {
  200. newconn->marked_for_close = 1;
  201. return 0;
  202. }
  203. return 0;
  204. }
  205. static int connection_init_accepted_conn(connection_t *conn) {
  206. connection_start_reading(conn);
  207. switch(conn->type) {
  208. case CONN_TYPE_OR:
  209. return connection_tls_start_handshake(conn, 1);
  210. case CONN_TYPE_AP:
  211. conn->state = AP_CONN_STATE_SOCKS_WAIT;
  212. break;
  213. case CONN_TYPE_DIR:
  214. conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
  215. break;
  216. }
  217. return 0;
  218. }
  219. /* take conn, make a nonblocking socket; try to connect to
  220. * addr:port (they arrive in *host order*). If fail, return -1. Else
  221. * assign s to conn->s: if connected return 1, if eagain return 0.
  222. * address is used to make the logs useful.
  223. */
  224. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port) {
  225. int s;
  226. struct sockaddr_in dest_addr;
  227. s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  228. if (s < 0) {
  229. log_fn(LOG_WARN,"Error creating network socket.");
  230. return -1;
  231. }
  232. set_socket_nonblocking(s);
  233. memset(&dest_addr,0,sizeof(dest_addr));
  234. dest_addr.sin_family = AF_INET;
  235. dest_addr.sin_port = htons(port);
  236. dest_addr.sin_addr.s_addr = htonl(addr);
  237. log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);
  238. if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
  239. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  240. /* yuck. kill it. */
  241. log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,strerror(errno));
  242. close(s);
  243. return -1;
  244. } else {
  245. /* it's in progress. set state appropriately and return. */
  246. conn->s = s;
  247. log_fn(LOG_DEBUG,"connect in progress, socket %d.",s);
  248. return 0;
  249. }
  250. }
  251. /* it succeeded. we're connected. */
  252. log_fn(LOG_INFO,"Connection to %s:%u established.",address,port);
  253. conn->s = s;
  254. return 1;
  255. }
  256. static void listener_close_if_present(int type) {
  257. connection_t *conn = connection_get_by_type(type);
  258. if (conn) {
  259. close(conn->s);
  260. conn->s = -1;
  261. conn->marked_for_close = 1;
  262. }
  263. }
  264. /* start all connections that should be up but aren't */
  265. int retry_all_connections(void) {
  266. if(options.ORPort) {
  267. router_retry_connections();
  268. }
  269. if(options.ORPort) {
  270. listener_close_if_present(CONN_TYPE_OR_LISTENER);
  271. if(connection_create_listener(options.ORBindAddress, options.ORPort,
  272. CONN_TYPE_OR_LISTENER) < 0)
  273. return -1;
  274. }
  275. if(options.DirPort) {
  276. listener_close_if_present(CONN_TYPE_DIR_LISTENER);
  277. if(connection_create_listener(options.DirBindAddress, options.DirPort,
  278. CONN_TYPE_DIR_LISTENER) < 0)
  279. return -1;
  280. }
  281. if(options.SocksPort) {
  282. listener_close_if_present(CONN_TYPE_AP_LISTENER);
  283. if(connection_create_listener(options.SocksBindAddress, options.SocksPort,
  284. CONN_TYPE_AP_LISTENER) < 0)
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. int connection_handle_read(connection_t *conn) {
  290. conn->timestamp_lastread = time(NULL);
  291. switch(conn->type) {
  292. case CONN_TYPE_OR_LISTENER:
  293. return connection_handle_listener_read(conn, CONN_TYPE_OR);
  294. case CONN_TYPE_AP_LISTENER:
  295. return connection_handle_listener_read(conn, CONN_TYPE_AP);
  296. case CONN_TYPE_DIR_LISTENER:
  297. return connection_handle_listener_read(conn, CONN_TYPE_DIR);
  298. }
  299. if(connection_read_to_buf(conn) < 0) {
  300. if(conn->type == CONN_TYPE_DIR &&
  301. (conn->state == DIR_CONN_STATE_CONNECTING_FETCH ||
  302. conn->state == DIR_CONN_STATE_CONNECTING_UPLOAD)) {
  303. /* it's a directory server and connecting failed: forget about this router */
  304. /* XXX I suspect pollerr may make Windows not get to this point. :( */
  305. router_mark_as_down(conn->nickname);
  306. }
  307. return -1;
  308. }
  309. if(connection_process_inbuf(conn) < 0) {
  310. // log_fn(LOG_DEBUG,"connection_process_inbuf returned -1.");
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. /* return -1 if we want to break conn, else return 0 */
  316. int connection_read_to_buf(connection_t *conn) {
  317. int result;
  318. int at_most;
  319. if(options.LinkPadding) {
  320. at_most = global_read_bucket;
  321. } else {
  322. /* do a rudimentary round-robin so one connection can't hog a thickpipe */
  323. if(connection_speaks_cells(conn)) {
  324. at_most = 32*(CELL_NETWORK_SIZE);
  325. } else {
  326. at_most = 32*(RELAY_PAYLOAD_SIZE);
  327. }
  328. if(at_most > global_read_bucket)
  329. at_most = global_read_bucket;
  330. }
  331. if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
  332. if(conn->state == OR_CONN_STATE_HANDSHAKING)
  333. return connection_tls_continue_handshake(conn);
  334. /* else open, or closing */
  335. if(at_most > conn->receiver_bucket)
  336. at_most = conn->receiver_bucket;
  337. result = read_to_buf_tls(conn->tls, at_most, conn->inbuf);
  338. switch(result) {
  339. case TOR_TLS_ERROR:
  340. case TOR_TLS_CLOSE:
  341. log_fn(LOG_INFO,"tls error. breaking.");
  342. return -1; /* XXX deal with close better */
  343. case TOR_TLS_WANTWRITE:
  344. connection_start_writing(conn);
  345. return 0;
  346. case TOR_TLS_WANTREAD: /* we're already reading */
  347. case TOR_TLS_DONE: /* no data read, so nothing to process */
  348. return 0;
  349. }
  350. } else {
  351. result = read_to_buf(conn->s, at_most, conn->inbuf,
  352. &conn->inbuf_reached_eof);
  353. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  354. if(result < 0)
  355. return -1;
  356. }
  357. global_read_bucket -= result; assert(global_read_bucket >= 0);
  358. if(global_read_bucket == 0) {
  359. log_fn(LOG_DEBUG,"global bucket exhausted. Pausing.");
  360. conn->wants_to_read = 1;
  361. connection_stop_reading(conn);
  362. return 0;
  363. }
  364. if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
  365. conn->receiver_bucket -= result; assert(conn->receiver_bucket >= 0);
  366. if(conn->receiver_bucket == 0) {
  367. log_fn(LOG_DEBUG,"receiver bucket exhausted. Pausing.");
  368. conn->wants_to_read = 1;
  369. connection_stop_reading(conn);
  370. return 0;
  371. }
  372. }
  373. return 0;
  374. }
  375. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  376. return fetch_from_buf(string, len, conn->inbuf);
  377. }
  378. int connection_find_on_inbuf(char *string, int len, connection_t *conn) {
  379. return find_on_inbuf(string, len, conn->inbuf);
  380. }
  381. int connection_wants_to_flush(connection_t *conn) {
  382. return conn->outbuf_flushlen;
  383. }
  384. int connection_outbuf_too_full(connection_t *conn) {
  385. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  386. }
  387. /* return -1 if you want to break the conn, else return 0 */
  388. int connection_handle_write(connection_t *conn) {
  389. if(connection_is_listener(conn)) {
  390. log_fn(LOG_WARN,"Got a listener socket. Can't happen!");
  391. return -1;
  392. }
  393. conn->timestamp_lastwritten = time(NULL);
  394. if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
  395. if(conn->state == OR_CONN_STATE_HANDSHAKING) {
  396. connection_stop_writing(conn);
  397. return connection_tls_continue_handshake(conn);
  398. }
  399. /* else open, or closing */
  400. switch(flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen)) {
  401. case TOR_TLS_ERROR:
  402. case TOR_TLS_CLOSE:
  403. log_fn(LOG_INFO,"tls error. breaking.");
  404. return -1; /* XXX deal with close better */
  405. case TOR_TLS_WANTWRITE:
  406. log_fn(LOG_DEBUG,"wanted write.");
  407. /* we're already writing */
  408. return 0;
  409. case TOR_TLS_WANTREAD:
  410. /* Make sure to avoid a loop if the receive buckets are empty. */
  411. log_fn(LOG_DEBUG,"wanted read.");
  412. if(!connection_is_reading(conn)) {
  413. connection_stop_writing(conn);
  414. conn->wants_to_write = 1;
  415. /* we'll start reading again when the next second arrives,
  416. * and then also start writing again.
  417. */
  418. }
  419. /* else no problem, we're already reading */
  420. return 0;
  421. /* case TOR_TLS_DONE:
  422. * for TOR_TLS_DONE, fall through to check if the flushlen
  423. * is empty, so we can stop writing.
  424. */
  425. }
  426. } else {
  427. if(flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen) < 0)
  428. return -1;
  429. /* conns in CONNECTING state will fall through... */
  430. }
  431. if(!connection_wants_to_flush(conn)) /* it's done flushing */
  432. if(connection_finished_flushing(conn) < 0) /* ...and get handled here. */
  433. return -1;
  434. return 0;
  435. }
  436. void connection_write_to_buf(const char *string, int len, connection_t *conn) {
  437. if(!len || conn->marked_for_close)
  438. return;
  439. if(write_to_buf(string, len, conn->outbuf) < 0) {
  440. log_fn(LOG_WARN,"write_to_buf failed. Closing connection (fd %d).", conn->s);
  441. conn->marked_for_close = 1;
  442. return;
  443. }
  444. connection_start_writing(conn);
  445. #define MIN_TLS_FLUSHLEN 15872
  446. /* openssl tls record size is 16383, this is close. The goal here is to
  447. * push data out as soon as we know there's enough for a tls record, so
  448. * during periods of high load we won't read the entire megabyte from
  449. * input before pushing any data out. */
  450. if(connection_speaks_cells(conn) &&
  451. conn->outbuf_flushlen < MIN_TLS_FLUSHLEN &&
  452. conn->outbuf_flushlen+len >= MIN_TLS_FLUSHLEN) {
  453. len -= (MIN_TLS_FLUSHLEN - conn->outbuf_flushlen);
  454. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  455. if(connection_handle_write(conn) < 0) {
  456. conn->marked_for_close = 1;
  457. log_fn(LOG_WARN,"flushing failed.");
  458. }
  459. }
  460. if(len > 0) { /* if there's any left over */
  461. conn->outbuf_flushlen += len;
  462. connection_start_writing(conn);
  463. /* because connection_handle_write() above might have stopped writing */
  464. }
  465. }
  466. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
  467. int i, n;
  468. connection_t *conn;
  469. connection_t **carray;
  470. get_connection_array(&carray,&n);
  471. for(i=0;i<n;i++) {
  472. conn = carray[i];
  473. if(conn->addr == addr && conn->port == port && !conn->marked_for_close)
  474. return conn;
  475. }
  476. return NULL;
  477. }
  478. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port) {
  479. /* Find a connection to the router described by addr and port,
  480. * or alternately any router which knows its key.
  481. * This connection *must* be in 'open' state.
  482. * If not, return NULL.
  483. */
  484. int i, n;
  485. connection_t *conn;
  486. routerinfo_t *router;
  487. connection_t **carray;
  488. /* first check if it's there exactly */
  489. conn = connection_exact_get_by_addr_port(addr,port);
  490. if(conn && connection_state_is_open(conn)) {
  491. log(LOG_INFO,"connection_twin_get_by_addr_port(): Found exact match.");
  492. return conn;
  493. }
  494. /* now check if any of the other open connections are a twin for this one */
  495. router = router_get_by_addr_port(addr,port);
  496. if(!router)
  497. return NULL;
  498. get_connection_array(&carray,&n);
  499. for(i=0;i<n;i++) {
  500. conn = carray[i];
  501. assert(conn);
  502. if(connection_state_is_open(conn) &&
  503. !crypto_pk_cmp_keys(conn->onion_pkey, router->onion_pkey)) {
  504. log(LOG_INFO,"connection_twin_get_by_addr_port(): Found twin (%s).",conn->address);
  505. return conn;
  506. }
  507. }
  508. return NULL;
  509. }
  510. connection_t *connection_get_by_type(int type) {
  511. int i, n;
  512. connection_t *conn;
  513. connection_t **carray;
  514. get_connection_array(&carray,&n);
  515. for(i=0;i<n;i++) {
  516. conn = carray[i];
  517. if(conn->type == type && !conn->marked_for_close)
  518. return conn;
  519. }
  520. return NULL;
  521. }
  522. connection_t *connection_get_by_type_state(int type, int state) {
  523. int i, n;
  524. connection_t *conn;
  525. connection_t **carray;
  526. get_connection_array(&carray,&n);
  527. for(i=0;i<n;i++) {
  528. conn = carray[i];
  529. if(conn->type == type && conn->state == state && !conn->marked_for_close)
  530. return conn;
  531. }
  532. return NULL;
  533. }
  534. connection_t *connection_get_by_type_state_lastwritten(int type, int state) {
  535. int i, n;
  536. connection_t *conn, *best=NULL;
  537. connection_t **carray;
  538. get_connection_array(&carray,&n);
  539. for(i=0;i<n;i++) {
  540. conn = carray[i];
  541. if(conn->type == type && conn->state == state && !conn->marked_for_close)
  542. if(!best || conn->timestamp_lastwritten < best->timestamp_lastwritten)
  543. best = conn;
  544. }
  545. return best;
  546. }
  547. int connection_receiver_bucket_should_increase(connection_t *conn) {
  548. assert(conn);
  549. if(!connection_speaks_cells(conn))
  550. return 0; /* edge connections don't use receiver_buckets */
  551. if(conn->state != OR_CONN_STATE_OPEN)
  552. return 0; /* only open connections play the rate limiting game */
  553. assert(conn->bandwidth > 0);
  554. if(conn->receiver_bucket > 9*conn->bandwidth)
  555. return 0;
  556. return 1;
  557. }
  558. int connection_is_listener(connection_t *conn) {
  559. if(conn->type == CONN_TYPE_OR_LISTENER ||
  560. conn->type == CONN_TYPE_AP_LISTENER ||
  561. conn->type == CONN_TYPE_DIR_LISTENER)
  562. return 1;
  563. return 0;
  564. }
  565. int connection_state_is_open(connection_t *conn) {
  566. assert(conn);
  567. if(conn->marked_for_close)
  568. return 0;
  569. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  570. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  571. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  572. return 1;
  573. return 0;
  574. }
  575. int connection_send_destroy(uint16_t circ_id, connection_t *conn) {
  576. cell_t cell;
  577. assert(conn);
  578. if(!connection_speaks_cells(conn)) {
  579. log_fn(LOG_INFO,"CircID %d: At an edge. Marking connection for close.",
  580. circ_id);
  581. if(conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_RESOLVING) {
  582. log_fn(LOG_INFO,"...and informing resolver we don't want the answer anymore.");
  583. dns_cancel_pending_resolve(conn->address, conn);
  584. }
  585. if(connection_edge_end(conn, END_STREAM_REASON_DESTROY, conn->cpath_layer) < 0)
  586. log_fn(LOG_WARN,"1: I called connection_edge_end redundantly.");
  587. /* if they already sent a destroy, they know. XXX can just close? */
  588. return 0;
  589. }
  590. memset(&cell, 0, sizeof(cell_t));
  591. cell.circ_id = circ_id;
  592. cell.command = CELL_DESTROY;
  593. log_fn(LOG_INFO,"Sending destroy (circID %d).", circ_id);
  594. connection_or_write_cell_to_buf(&cell, conn);
  595. return 0;
  596. }
  597. int connection_process_inbuf(connection_t *conn) {
  598. assert(conn);
  599. switch(conn->type) {
  600. case CONN_TYPE_OR:
  601. return connection_or_process_inbuf(conn);
  602. case CONN_TYPE_EXIT:
  603. case CONN_TYPE_AP:
  604. return connection_edge_process_inbuf(conn);
  605. case CONN_TYPE_DIR:
  606. return connection_dir_process_inbuf(conn);
  607. case CONN_TYPE_DNSWORKER:
  608. return connection_dns_process_inbuf(conn);
  609. case CONN_TYPE_CPUWORKER:
  610. return connection_cpu_process_inbuf(conn);
  611. default:
  612. log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
  613. return -1;
  614. }
  615. }
  616. int connection_finished_flushing(connection_t *conn) {
  617. assert(conn);
  618. // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
  619. switch(conn->type) {
  620. case CONN_TYPE_OR:
  621. return connection_or_finished_flushing(conn);
  622. case CONN_TYPE_AP:
  623. case CONN_TYPE_EXIT:
  624. return connection_edge_finished_flushing(conn);
  625. case CONN_TYPE_DIR:
  626. return connection_dir_finished_flushing(conn);
  627. case CONN_TYPE_DNSWORKER:
  628. return connection_dns_finished_flushing(conn);
  629. case CONN_TYPE_CPUWORKER:
  630. return connection_cpu_finished_flushing(conn);
  631. default:
  632. log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
  633. return -1;
  634. }
  635. }
  636. void assert_connection_ok(connection_t *conn, time_t now)
  637. {
  638. assert(conn);
  639. assert(conn->magic == CONNECTION_MAGIC);
  640. return;
  641. assert(conn->type >= _CONN_TYPE_MIN);
  642. assert(conn->type <= _CONN_TYPE_MAX);
  643. if(conn->outbuf_flushlen > 0) {
  644. assert(connection_is_writing(conn) || conn->wants_to_write);
  645. }
  646. /* XXX check: wants_to_read, wants_to_write, s, poll_index,
  647. * marked_for_close. */
  648. /* buffers */
  649. if (!connection_is_listener(conn)) {
  650. assert(conn->inbuf);
  651. assert(conn->outbuf);
  652. }
  653. assert(!now || conn->timestamp_lastread <= now);
  654. assert(!now || conn->timestamp_lastwritten <= now);
  655. assert(conn->timestamp_created <= conn->timestamp_lastread);
  656. assert(conn->timestamp_created <= conn->timestamp_lastwritten);
  657. /* XXX Fix this; no longer so.*/
  658. #if 0
  659. if(conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR)
  660. assert(!conn->pkey);
  661. /* pkey is set if we're a dir client, or if we're an OR in state OPEN
  662. * connected to another OR.
  663. */
  664. #endif
  665. if (conn->type != CONN_TYPE_OR) {
  666. assert(!conn->tls);
  667. } else {
  668. if(conn->state == OR_CONN_STATE_OPEN) {
  669. assert(conn->bandwidth > 0);
  670. assert(conn->receiver_bucket >= 0);
  671. // assert(conn->receiver_bucket <= 10*conn->bandwidth);
  672. }
  673. assert(conn->addr && conn->port);
  674. assert(conn->address);
  675. if (conn->state != OR_CONN_STATE_CONNECTING)
  676. assert(conn->tls);
  677. }
  678. if (conn->type != CONN_TYPE_EXIT && conn->type != CONN_TYPE_AP) {
  679. assert(!conn->stream_id);
  680. assert(!conn->next_stream);
  681. assert(!conn->cpath_layer);
  682. assert(!conn->package_window);
  683. assert(!conn->deliver_window);
  684. assert(!conn->done_sending);
  685. assert(!conn->done_receiving);
  686. } else {
  687. if(conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN)
  688. assert(conn->cpath_layer);
  689. if(conn->cpath_layer)
  690. assert_cpath_layer_ok(conn->cpath_layer);
  691. /* XXX unchecked: package window, deliver window. */
  692. }
  693. if (conn->type != CONN_TYPE_AP) {
  694. assert(!conn->socks_request);
  695. }
  696. switch(conn->type)
  697. {
  698. case CONN_TYPE_OR_LISTENER:
  699. case CONN_TYPE_AP_LISTENER:
  700. case CONN_TYPE_DIR_LISTENER:
  701. assert(conn->state == LISTENER_STATE_READY);
  702. break;
  703. case CONN_TYPE_OR:
  704. assert(conn->state >= _OR_CONN_STATE_MIN &&
  705. conn->state <= _OR_CONN_STATE_MAX);
  706. break;
  707. case CONN_TYPE_EXIT:
  708. assert(conn->state >= _EXIT_CONN_STATE_MIN &&
  709. conn->state <= _EXIT_CONN_STATE_MAX);
  710. break;
  711. case CONN_TYPE_AP:
  712. assert(conn->state >= _AP_CONN_STATE_MIN &&
  713. conn->state <= _AP_CONN_STATE_MAX);
  714. assert(conn->socks_request);
  715. break;
  716. case CONN_TYPE_DIR:
  717. assert(conn->state >= _DIR_CONN_STATE_MIN &&
  718. conn->state <= _DIR_CONN_STATE_MAX);
  719. break;
  720. case CONN_TYPE_DNSWORKER:
  721. assert(conn->state == DNSWORKER_STATE_IDLE ||
  722. conn->state == DNSWORKER_STATE_BUSY);
  723. break;
  724. case CONN_TYPE_CPUWORKER:
  725. assert(conn->state >= _CPUWORKER_STATE_MIN &&
  726. conn->state <= _CPUWORKER_STATE_MAX);
  727. break;
  728. default:
  729. assert(0);
  730. }
  731. }
  732. /*
  733. Local Variables:
  734. mode:c
  735. indent-tabs-mode:nil
  736. c-basic-offset:2
  737. End:
  738. */