connection.c 26 KB

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