connection.c 23 KB

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