connection.c 26 KB

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