connection.c 23 KB

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