connection.c 24 KB

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