connection.c 24 KB

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