connection.c 24 KB

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