connection.c 23 KB

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