connection.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file connection.c
  6. * \brief General high-level functions to handle reading and writing
  7. * on connections.
  8. **/
  9. #include "or.h"
  10. /********* START VARIABLES **********/
  11. extern or_options_t options; /* command-line and config-file options */
  12. /** Array of strings to make conn-\>type human-readable. */
  13. char *conn_type_to_string[] = {
  14. "", /* 0 */
  15. "OP listener", /* 1 */
  16. "OP", /* 2 */
  17. "OR listener", /* 3 */
  18. "OR", /* 4 */
  19. "Exit", /* 5 */
  20. "App listener",/* 6 */
  21. "App", /* 7 */
  22. "Dir listener",/* 8 */
  23. "Dir", /* 9 */
  24. "DNS worker", /* 10 */
  25. "CPU worker", /* 11 */
  26. };
  27. /** Array of string arrays to make {conn-\>type,conn-\>state} human-readable. */
  28. char *conn_state_to_string[][_CONN_TYPE_MAX+1] = {
  29. { NULL }, /* no type associated with 0 */
  30. { NULL }, /* op listener, obsolete */
  31. { NULL }, /* op, obsolete */
  32. { "ready" }, /* or listener, 0 */
  33. { "", /* OR, 0 */
  34. "connect()ing", /* 1 */
  35. "handshaking", /* 2 */
  36. "open" }, /* 3 */
  37. { "", /* exit, 0 */
  38. "waiting for dest info", /* 1 */
  39. "connecting", /* 2 */
  40. "open", /* 3 */
  41. "resolve failed" }, /* 4 */
  42. { "ready" }, /* app listener, 0 */
  43. { "", /* 0 */
  44. "", /* 1 */
  45. "", /* 2 */
  46. "", /* 3 */
  47. "", /* 4 */
  48. "awaiting dest info", /* app, 5 */
  49. "waiting for rendezvous desc", /* 6 */
  50. "waiting for safe circuit", /* 7 */
  51. "waiting for connected", /* 8 */
  52. "open" }, /* 9 */
  53. { "ready" }, /* dir listener, 0 */
  54. { "", /* dir, 0 */
  55. "connecting", /* 1 */
  56. "client sending", /* 2 */
  57. "client reading", /* 3 */
  58. "awaiting command", /* 4 */
  59. "writing" }, /* 5 */
  60. { "", /* dns worker, 0 */
  61. "idle", /* 1 */
  62. "busy" }, /* 2 */
  63. { "", /* cpu worker, 0 */
  64. "idle", /* 1 */
  65. "busy with onion", /* 2 */
  66. "busy with handshake" }, /* 3 */
  67. };
  68. /********* END VARIABLES ************/
  69. static int connection_init_accepted_conn(connection_t *conn);
  70. static int connection_handle_listener_read(connection_t *conn, int new_type);
  71. static int connection_receiver_bucket_should_increase(connection_t *conn);
  72. static int connection_finished_flushing(connection_t *conn);
  73. static int connection_finished_connecting(connection_t *conn);
  74. static int connection_read_to_buf(connection_t *conn);
  75. static int connection_process_inbuf(connection_t *conn);
  76. /**************************************************************/
  77. /** Allocate space for a new connection_t. This function just initializes
  78. * conn; you must call connection_add() to link it into the main array.
  79. *
  80. * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>poll_index to
  81. * -1 to signify they are not yet assigned.
  82. *
  83. * If conn is not a listener type, allocate buffers for it. If it's
  84. * an AP type, allocate space to store the socks_request.
  85. *
  86. * Assign a pseudorandom next_circ_id between 0 and 2**15.
  87. *
  88. * Initialize conn's timestamps to now.
  89. */
  90. connection_t *connection_new(int type) {
  91. connection_t *conn;
  92. time_t now = time(NULL);
  93. conn = tor_malloc_zero(sizeof(connection_t));
  94. conn->magic = CONNECTION_MAGIC;
  95. conn->s = -1; /* give it a default of 'not used' */
  96. conn->poll_index = -1; /* also default to 'not used' */
  97. conn->type = type;
  98. if(!connection_is_listener(conn)) { /* listeners never use their buf */
  99. conn->inbuf = buf_new();
  100. conn->outbuf = buf_new();
  101. }
  102. if (type == CONN_TYPE_AP) {
  103. conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
  104. }
  105. conn->next_circ_id = crypto_pseudo_rand_int(1<<15);
  106. conn->timestamp_created = now;
  107. conn->timestamp_lastread = now;
  108. conn->timestamp_lastwritten = now;
  109. return conn;
  110. }
  111. /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if necessary,
  112. * close its socket if necessary, and mark the directory as dirty if <b>conn</b>
  113. * is an OR or OP connection.
  114. */
  115. void connection_free(connection_t *conn) {
  116. tor_assert(conn);
  117. tor_assert(conn->magic == CONNECTION_MAGIC);
  118. if(!connection_is_listener(conn)) {
  119. buf_free(conn->inbuf);
  120. buf_free(conn->outbuf);
  121. }
  122. tor_free(conn->address);
  123. if(connection_speaks_cells(conn)) {
  124. directory_set_dirty(); /* XXX should only do this for an open OR conn */
  125. if (conn->tls)
  126. tor_tls_free(conn->tls);
  127. }
  128. if (conn->identity_pkey)
  129. crypto_free_pk_env(conn->identity_pkey);
  130. tor_free(conn->nickname);
  131. tor_free(conn->socks_request);
  132. if(conn->s >= 0) {
  133. log_fn(LOG_INFO,"closing fd %d.",conn->s);
  134. tor_close_socket(conn->s);
  135. }
  136. memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */
  137. free(conn);
  138. }
  139. /** Call connection_free() on every connection in our array.
  140. * This is used by cpuworkers and dnsworkers when they fork,
  141. * so they don't keep resources held open (especially sockets).
  142. */
  143. void connection_free_all(void) {
  144. int i, n;
  145. connection_t **carray;
  146. get_connection_array(&carray,&n);
  147. for(i=0;i<n;i++)
  148. connection_free(carray[i]);
  149. }
  150. void connection_about_to_close_connection(connection_t *conn)
  151. {
  152. switch(conn->type) {
  153. case CONN_TYPE_DIR:
  154. if(conn->purpose == DIR_PURPOSE_FETCH_RENDDESC)
  155. rend_client_desc_fetched(conn->rend_query, 0);
  156. break;
  157. case CONN_TYPE_AP:
  158. case CONN_TYPE_EXIT:
  159. if(!conn->has_sent_end) {
  160. log_fn(LOG_WARN,"Edge connection hasn't sent end yet? Bug.");
  161. connection_mark_for_close(conn);
  162. }
  163. break;
  164. }
  165. }
  166. /** Close the underlying socket for <b>conn</b>, so we don't try to
  167. * flush it. Must be used in conjunction with (right before)
  168. * connection_mark_for_close().
  169. */
  170. void connection_close_immediate(connection_t *conn)
  171. {
  172. assert_connection_ok(conn,0);
  173. if (conn->s < 0) {
  174. log_fn(LOG_WARN,"Attempt to close already-closed connection.");
  175. return;
  176. }
  177. if (conn->outbuf_flushlen) {
  178. log_fn(LOG_INFO,"Closing connection (fd %d, type %s, state %d) with data on outbuf.",
  179. conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
  180. }
  181. tor_close_socket(conn->s);
  182. conn->s = -1;
  183. if(!connection_is_listener(conn)) {
  184. buf_clear(conn->outbuf);
  185. conn->outbuf_flushlen = 0;
  186. }
  187. }
  188. /** Mark <b>conn</b> to be closed next time we loop through
  189. * conn_close_if_marked() in main.c. Do any cleanup needed:
  190. * - Directory conns that fail to fetch a rendezvous descriptor need
  191. * to inform pending rendezvous streams.
  192. * - OR conns need to call rep_hist_note_*() to record status.
  193. * - AP conns need to send a socks reject if necessary.
  194. * - Exit conns need to call connection_dns_remove() if necessary.
  195. * - AP and Exit conns need to send an end cell if they can.
  196. * - DNS conns need to fail any resolves that are pending on them.
  197. */
  198. int
  199. _connection_mark_for_close(connection_t *conn)
  200. {
  201. assert_connection_ok(conn,0);
  202. if (conn->marked_for_close) {
  203. log(LOG_WARN, "Double mark-for-close on connection.");
  204. return -1;
  205. }
  206. switch (conn->type)
  207. {
  208. case CONN_TYPE_OR_LISTENER:
  209. case CONN_TYPE_AP_LISTENER:
  210. case CONN_TYPE_DIR_LISTENER:
  211. case CONN_TYPE_CPUWORKER:
  212. case CONN_TYPE_DIR:
  213. /* No special processing needed immediately. */
  214. break;
  215. case CONN_TYPE_OR:
  216. /* Remember why we're closing this connection. */
  217. if (conn->state != OR_CONN_STATE_OPEN) {
  218. /* XXX Nick: this still isn't right, because it might be
  219. * dying even though we didn't initiate the connect. Can
  220. * you look at this more? -RD */
  221. if(conn->nickname)
  222. rep_hist_note_connect_failed(conn->nickname, time(NULL));
  223. } else if (0) { // XXX reason == CLOSE_REASON_UNUSED_OR_CONN) {
  224. rep_hist_note_disconnect(conn->nickname, time(NULL));
  225. } else {
  226. rep_hist_note_connection_died(conn->nickname, time(NULL));
  227. }
  228. break;
  229. case CONN_TYPE_AP:
  230. if (conn->socks_request->has_finished == 0) {
  231. log_fn(LOG_INFO,"Cleaning up AP -- sending socks reject.");
  232. connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
  233. conn->socks_request->has_finished = 1;
  234. conn->hold_open_until_flushed = 1;
  235. }
  236. /* fall through, to do things for both ap and exit */
  237. case CONN_TYPE_EXIT:
  238. if (conn->state == EXIT_CONN_STATE_RESOLVING)
  239. connection_dns_remove(conn);
  240. break;
  241. case CONN_TYPE_DNSWORKER:
  242. if (conn->state == DNSWORKER_STATE_BUSY) {
  243. dns_cancel_pending_resolve(conn->address);
  244. }
  245. break;
  246. default:
  247. log(LOG_ERR, "Unknown connection type %d", conn->type);
  248. ;
  249. }
  250. conn->marked_for_close = 1;
  251. /* in case we're going to be held-open-til-flushed, reset
  252. * the number of seconds since last successful write, so
  253. * we get our whole 15 seconds */
  254. conn->timestamp_lastwritten = time(NULL);
  255. return 0;
  256. }
  257. /** Find each connection that has hold_open_until_flushed set to
  258. * 1 but hasn't written in the past 15 seconds, and set
  259. * hold_open_until_flushed to 0. This means it will get cleaned
  260. * up in the next loop through close_if_marked() in main.c.
  261. */
  262. void connection_expire_held_open(void)
  263. {
  264. connection_t **carray, *conn;
  265. int n, i;
  266. time_t now;
  267. now = time(NULL);
  268. get_connection_array(&carray, &n);
  269. for (i = 0; i < n; ++i) {
  270. conn = carray[i];
  271. /* If we've been holding the connection open, but we haven't written
  272. * for 15 seconds...
  273. */
  274. if (conn->hold_open_until_flushed) {
  275. tor_assert(conn->marked_for_close);
  276. if (now - conn->timestamp_lastwritten >= 15) {
  277. log_fn(LOG_WARN,"Giving up on marked_for_close conn that's been flushing for 15s (fd %d, type %s, state %d).",
  278. conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
  279. conn->hold_open_until_flushed = 0;
  280. }
  281. }
  282. }
  283. }
  284. /** Bind a new non-blocking socket listening to
  285. * <b>bindaddress</b>:<b>bindport</b>, and add this new connection
  286. * (of type <b>type</b>) to the connection array.
  287. *
  288. * If <b>bindaddress</b> includes a port, we bind on that port; otherwise, we
  289. * use bindport.
  290. */
  291. int connection_create_listener(char *bindaddress, uint16_t bindport, int type) {
  292. struct sockaddr_in bindaddr; /* where to bind */
  293. connection_t *conn;
  294. char *hostname, *cp;
  295. int usePort;
  296. int s; /* the socket we're going to make */
  297. int one=1;
  298. cp = strchr(bindaddress, ':');
  299. if (cp) {
  300. hostname = tor_strndup(bindaddress, cp-bindaddress);
  301. usePort = atoi(cp+1);
  302. } else {
  303. hostname = tor_strdup(bindaddress);
  304. usePort = bindport;
  305. }
  306. memset(&bindaddr,0,sizeof(struct sockaddr_in));
  307. bindaddr.sin_family = AF_INET;
  308. bindaddr.sin_port = htons(usePort);
  309. if(tor_lookup_hostname(hostname, &(bindaddr.sin_addr.s_addr)) != 0) {
  310. log_fn(LOG_WARN,"Can't resolve BindAddress %s",hostname);
  311. tor_free(hostname);
  312. return -1;
  313. }
  314. tor_free(hostname);
  315. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  316. if (s < 0) {
  317. log_fn(LOG_WARN,"Socket creation failed.");
  318. return -1;
  319. }
  320. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one));
  321. if(bind(s,(struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) {
  322. log_fn(LOG_WARN,"Could not bind to port %u: %s",usePort,
  323. tor_socket_strerror(tor_socket_errno(s)));
  324. return -1;
  325. }
  326. if(listen(s,SOMAXCONN) < 0) {
  327. log_fn(LOG_WARN,"Could not listen on port %u: %s",usePort,
  328. tor_socket_strerror(tor_socket_errno(s)));
  329. return -1;
  330. }
  331. set_socket_nonblocking(s);
  332. conn = connection_new(type);
  333. conn->s = s;
  334. if(connection_add(conn) < 0) { /* no space, forget it */
  335. log_fn(LOG_WARN,"connection_add failed. Giving up.");
  336. connection_free(conn);
  337. return -1;
  338. }
  339. log_fn(LOG_DEBUG,"%s listening on port %u.",conn_type_to_string[type], usePort);
  340. conn->state = LISTENER_STATE_READY;
  341. connection_start_reading(conn);
  342. return 0;
  343. }
  344. /** The listener connection <b>conn</b> told poll() it wanted to read.
  345. * Call accept() on conn-\>s, and add the new connection if necessary.
  346. */
  347. static int connection_handle_listener_read(connection_t *conn, int new_type) {
  348. int news; /* the new socket */
  349. connection_t *newconn;
  350. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  351. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  352. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  353. if (news == -1) { /* accept() error */
  354. if(ERRNO_IS_EAGAIN(tor_socket_errno(conn->s))) {
  355. return 0; /* he hung up before we could accept(). that's fine. */
  356. }
  357. /* else there was a real error. */
  358. log_fn(LOG_WARN,"accept() failed. Closing listener.");
  359. connection_mark_for_close(conn);
  360. return -1;
  361. }
  362. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  363. set_socket_nonblocking(news);
  364. newconn = connection_new(new_type);
  365. newconn->s = news;
  366. newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  367. newconn->addr = ntohl(remote.sin_addr.s_addr);
  368. newconn->port = ntohs(remote.sin_port);
  369. if(connection_add(newconn) < 0) { /* no space, forget it */
  370. connection_free(newconn);
  371. return 0; /* no need to tear down the parent */
  372. }
  373. if(connection_init_accepted_conn(newconn) < 0) {
  374. connection_mark_for_close(newconn);
  375. return 0;
  376. }
  377. return 0;
  378. }
  379. /** Initialize states for newly accepted connection <b>conn</b>.
  380. * If conn is an OR, start the tls handshake.
  381. */
  382. static int connection_init_accepted_conn(connection_t *conn) {
  383. connection_start_reading(conn);
  384. switch(conn->type) {
  385. case CONN_TYPE_OR:
  386. return connection_tls_start_handshake(conn, 1);
  387. case CONN_TYPE_AP:
  388. conn->state = AP_CONN_STATE_SOCKS_WAIT;
  389. break;
  390. case CONN_TYPE_DIR:
  391. conn->purpose = DIR_PURPOSE_SERVER;
  392. conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
  393. break;
  394. }
  395. return 0;
  396. }
  397. /** Take conn, make a nonblocking socket; try to connect to
  398. * addr:port (they arrive in *host order*). If fail, return -1. Else
  399. * assign s to conn->\s: if connected return 1, if EAGAIN return 0.
  400. *
  401. * address is used to make the logs useful.
  402. *
  403. * On success, add conn to the list of polled connections.
  404. */
  405. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port) {
  406. int s;
  407. struct sockaddr_in dest_addr;
  408. s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  409. if (s < 0) {
  410. log_fn(LOG_WARN,"Error creating network socket.");
  411. return -1;
  412. }
  413. set_socket_nonblocking(s);
  414. memset(&dest_addr,0,sizeof(dest_addr));
  415. dest_addr.sin_family = AF_INET;
  416. dest_addr.sin_port = htons(port);
  417. dest_addr.sin_addr.s_addr = htonl(addr);
  418. log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);
  419. if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
  420. if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
  421. /* yuck. kill it. */
  422. log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
  423. tor_socket_strerror(tor_socket_errno(s)));
  424. tor_close_socket(s);
  425. return -1;
  426. } else {
  427. /* it's in progress. set state appropriately and return. */
  428. conn->s = s;
  429. if(connection_add(conn) < 0) /* no space, forget it */
  430. return -1;
  431. log_fn(LOG_DEBUG,"connect in progress, socket %d.",s);
  432. return 0;
  433. }
  434. }
  435. /* it succeeded. we're connected. */
  436. log_fn(LOG_INFO,"Connection to %s:%u established.",address,port);
  437. conn->s = s;
  438. if(connection_add(conn) < 0) /* no space, forget it */
  439. return -1;
  440. return 1;
  441. }
  442. /** If there exists a listener of type <b>type</b> in the connection
  443. * array, mark it for close.
  444. */
  445. static void listener_close_if_present(int type) {
  446. connection_t *conn;
  447. tor_assert(type == CONN_TYPE_OR_LISTENER ||
  448. type == CONN_TYPE_AP_LISTENER ||
  449. type == CONN_TYPE_DIR_LISTENER);
  450. conn = connection_get_by_type(type);
  451. if (conn) {
  452. connection_close_immediate(conn);
  453. connection_mark_for_close(conn);
  454. }
  455. }
  456. /** Start all connections that should be up but aren't.
  457. * - Connect to all ORs if you're an OR.
  458. * - Relaunch listeners for each port you have open.
  459. */
  460. int retry_all_connections(void) {
  461. if(options.ORPort) {
  462. router_retry_connections();
  463. }
  464. if(options.ORPort) {
  465. listener_close_if_present(CONN_TYPE_OR_LISTENER);
  466. if(connection_create_listener(options.ORBindAddress,
  467. (uint16_t) options.ORPort,
  468. CONN_TYPE_OR_LISTENER) < 0)
  469. return -1;
  470. }
  471. if(options.DirPort) {
  472. listener_close_if_present(CONN_TYPE_DIR_LISTENER);
  473. if(connection_create_listener(options.DirBindAddress,
  474. (uint16_t) options.DirPort,
  475. CONN_TYPE_DIR_LISTENER) < 0)
  476. return -1;
  477. }
  478. if(options.SocksPort) {
  479. listener_close_if_present(CONN_TYPE_AP_LISTENER);
  480. if(connection_create_listener(options.SocksBindAddress,
  481. (uint16_t) options.SocksPort,
  482. CONN_TYPE_AP_LISTENER) < 0)
  483. return -1;
  484. }
  485. return 0;
  486. }
  487. extern int global_read_bucket;
  488. /** How many bytes at most can we read onto this connection? */
  489. int connection_bucket_read_limit(connection_t *conn) {
  490. int at_most;
  491. if(options.LinkPadding) {
  492. at_most = global_read_bucket;
  493. } else {
  494. /* do a rudimentary round-robin so one circuit can't hog a connection */
  495. if(connection_speaks_cells(conn)) {
  496. at_most = 32*(CELL_NETWORK_SIZE);
  497. } else {
  498. at_most = 32*(RELAY_PAYLOAD_SIZE);
  499. }
  500. if(at_most > global_read_bucket)
  501. at_most = global_read_bucket;
  502. }
  503. if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
  504. if(at_most > conn->receiver_bucket)
  505. at_most = conn->receiver_bucket;
  506. return at_most;
  507. }
  508. /** We just read num_read onto conn. Decrement buckets appropriately. */
  509. void connection_bucket_decrement(connection_t *conn, int num_read) {
  510. global_read_bucket -= num_read; tor_assert(global_read_bucket >= 0);
  511. if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
  512. conn->receiver_bucket -= num_read; tor_assert(conn->receiver_bucket >= 0);
  513. }
  514. if(global_read_bucket == 0) {
  515. log_fn(LOG_DEBUG,"global bucket exhausted. Pausing.");
  516. conn->wants_to_read = 1;
  517. connection_stop_reading(conn);
  518. return;
  519. }
  520. if(connection_speaks_cells(conn) &&
  521. conn->state == OR_CONN_STATE_OPEN &&
  522. conn->receiver_bucket == 0) {
  523. log_fn(LOG_DEBUG,"receiver bucket exhausted. Pausing.");
  524. conn->wants_to_read = 1;
  525. connection_stop_reading(conn);
  526. }
  527. }
  528. /** Keep a timeval to know when time has passed enough to refill buckets */
  529. static struct timeval current_time;
  530. /** Initiatialize the global read bucket to options.BandwidthBurst,
  531. * and current_time to the current time. */
  532. void connection_bucket_init(void) {
  533. tor_gettimeofday(&current_time);
  534. global_read_bucket = options.BandwidthBurst; /* start it at max traffic */
  535. }
  536. /** Some time has passed; increment buckets appropriately. */
  537. void connection_bucket_refill(struct timeval *now) {
  538. int i, n;
  539. connection_t *conn;
  540. connection_t **carray;
  541. if(now->tv_sec <= current_time.tv_sec)
  542. return; /* wait until the second has rolled over */
  543. current_time.tv_sec = now->tv_sec; /* update current_time */
  544. /* (ignore usecs for now) */
  545. /* refill the global bucket */
  546. if(global_read_bucket < options.BandwidthBurst) {
  547. global_read_bucket += options.BandwidthRate;
  548. log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
  549. }
  550. /* refill the per-connection buckets */
  551. get_connection_array(&carray,&n);
  552. for(i=0;i<n;i++) {
  553. conn = carray[i];
  554. if(connection_receiver_bucket_should_increase(conn)) {
  555. conn->receiver_bucket += conn->bandwidth;
  556. //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
  557. }
  558. if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
  559. && global_read_bucket > 0 /* and we're allowed to read */
  560. && (!connection_speaks_cells(conn) ||
  561. conn->state != OR_CONN_STATE_OPEN ||
  562. conn->receiver_bucket > 0)) {
  563. /* and either a non-cell conn or a cell conn with non-empty bucket */
  564. log_fn(LOG_DEBUG,"waking up conn (fd %d)",conn->s);
  565. conn->wants_to_read = 0;
  566. connection_start_reading(conn);
  567. if(conn->wants_to_write == 1) {
  568. conn->wants_to_write = 0;
  569. connection_start_writing(conn);
  570. }
  571. }
  572. }
  573. }
  574. /** Is the receiver bucket for connection <b>conn</b> low enough that we
  575. * should add another pile of tokens to it?
  576. */
  577. static int connection_receiver_bucket_should_increase(connection_t *conn) {
  578. tor_assert(conn);
  579. if(!connection_speaks_cells(conn))
  580. return 0; /* edge connections don't use receiver_buckets */
  581. if(conn->state != OR_CONN_STATE_OPEN)
  582. return 0; /* only open connections play the rate limiting game */
  583. tor_assert(conn->bandwidth > 0);
  584. if(conn->receiver_bucket > 9*conn->bandwidth)
  585. return 0;
  586. return 1;
  587. }
  588. /** Read bytes from conn->\s and process them.
  589. *
  590. * This function gets called from conn_read() in main.c, either
  591. * when poll() has declared that conn wants to read, or (for OR conns)
  592. * when there are pending TLS bytes.
  593. *
  594. * It calls connection_read_to_buf() to bring in any new bytes,
  595. * and then calls connection_process_inbuf() to process them.
  596. *
  597. * Mark the connection and return -1 if you want to close it, else
  598. * return 0.
  599. */
  600. int connection_handle_read(connection_t *conn) {
  601. conn->timestamp_lastread = time(NULL);
  602. switch(conn->type) {
  603. case CONN_TYPE_OR_LISTENER:
  604. return connection_handle_listener_read(conn, CONN_TYPE_OR);
  605. case CONN_TYPE_AP_LISTENER:
  606. return connection_handle_listener_read(conn, CONN_TYPE_AP);
  607. case CONN_TYPE_DIR_LISTENER:
  608. return connection_handle_listener_read(conn, CONN_TYPE_DIR);
  609. }
  610. if(connection_read_to_buf(conn) < 0) {
  611. if(conn->type == CONN_TYPE_DIR &&
  612. conn->state == DIR_CONN_STATE_CONNECTING) {
  613. /* it's a directory server and connecting failed: forget about this router */
  614. /* XXX I suspect pollerr may make Windows not get to this point. :( */
  615. router_mark_as_down(conn->nickname);
  616. }
  617. /* There's a read error; kill the connection.*/
  618. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  619. conn->has_sent_end = 1;
  620. connection_mark_for_close(conn);
  621. return -1;
  622. }
  623. if(connection_process_inbuf(conn) < 0) {
  624. // log_fn(LOG_DEBUG,"connection_process_inbuf returned -1.");
  625. return -1;
  626. }
  627. return 0;
  628. }
  629. /** Pull in new bytes from conn-\>s onto conn-\>inbuf, either
  630. * directly or via TLS. Reduce the token buckets by the number of
  631. * bytes read.
  632. *
  633. * Return -1 if we want to break conn, else return 0.
  634. */
  635. static int connection_read_to_buf(connection_t *conn) {
  636. int result;
  637. int at_most;
  638. /* how many bytes are we allowed to read? */
  639. at_most = connection_bucket_read_limit(conn);
  640. if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
  641. if(conn->state == OR_CONN_STATE_HANDSHAKING) {
  642. /* continue handshaking even if global token bucket is empty */
  643. return connection_tls_continue_handshake(conn);
  644. }
  645. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object). at_most %d.",
  646. conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls), at_most);
  647. /* else open, or closing */
  648. result = read_to_buf_tls(conn->tls, at_most, conn->inbuf);
  649. switch(result) {
  650. case TOR_TLS_ERROR:
  651. case TOR_TLS_CLOSE:
  652. log_fn(LOG_INFO,"tls error. breaking (nickname %s, address %s).",
  653. conn->nickname ? conn->nickname : "not set", conn->address);
  654. return -1; /* XXX deal with close better */
  655. case TOR_TLS_WANTWRITE:
  656. connection_start_writing(conn);
  657. return 0;
  658. case TOR_TLS_WANTREAD: /* we're already reading */
  659. case TOR_TLS_DONE: /* no data read, so nothing to process */
  660. result = 0;
  661. break; /* so we call bucket_decrement below */
  662. }
  663. } else {
  664. result = read_to_buf(conn->s, at_most, conn->inbuf,
  665. &conn->inbuf_reached_eof);
  666. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  667. if(result < 0)
  668. return -1;
  669. }
  670. connection_bucket_decrement(conn, result);
  671. return 0;
  672. }
  673. /** A pass-through to fetch_from_buf. */
  674. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  675. return fetch_from_buf(string, len, conn->inbuf);
  676. }
  677. /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
  678. * from its outbuf. */
  679. int connection_wants_to_flush(connection_t *conn) {
  680. return conn->outbuf_flushlen;
  681. }
  682. /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
  683. * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
  684. * connection_edge_consider_sending_sendme().
  685. */
  686. int connection_outbuf_too_full(connection_t *conn) {
  687. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  688. }
  689. /** Try to flush more bytes onto conn-\>s.
  690. *
  691. * This function gets called either from conn_write() in main.c
  692. * when poll() has declared that conn wants to write, or below
  693. * from connection_write_to_buf() when an entire TLS record is ready.
  694. *
  695. * Update conn-\>timestamp_lastwritten to now, and call flush_buf
  696. * or flush_buf_tls appropriately. If it succeeds and there no more
  697. * more bytes on conn->outbuf, then call connection_finished_flushing
  698. * on it too.
  699. *
  700. * Mark the connection and return -1 if you want to close it, else
  701. * return 0.
  702. */
  703. int connection_handle_write(connection_t *conn) {
  704. int e, len=sizeof(e);
  705. tor_assert(!connection_is_listener(conn));
  706. conn->timestamp_lastwritten = time(NULL);
  707. /* Sometimes, "writeable" means "connected". */
  708. if (connection_state_is_connecting(conn)) {
  709. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
  710. /* not yet */
  711. if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(conn->s))) {
  712. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  713. connection_close_immediate(conn);
  714. connection_mark_for_close(conn);
  715. if (conn->nickname)
  716. router_mark_as_down(conn->nickname);
  717. return -1;
  718. } else {
  719. return 0; /* no change, see if next time is better */
  720. }
  721. }
  722. /* The connection is successful. */
  723. return connection_finished_connecting(conn);
  724. }
  725. if (connection_speaks_cells(conn)) {
  726. if (conn->state == OR_CONN_STATE_HANDSHAKING) {
  727. connection_stop_writing(conn);
  728. if(connection_tls_continue_handshake(conn) < 0) {
  729. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  730. connection_mark_for_close(conn);
  731. return -1;
  732. }
  733. return 0;
  734. }
  735. /* else open, or closing */
  736. switch(flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen)) {
  737. case TOR_TLS_ERROR:
  738. case TOR_TLS_CLOSE:
  739. log_fn(LOG_INFO,"tls error. breaking.");
  740. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  741. connection_mark_for_close(conn);
  742. return -1; /* XXX deal with close better */
  743. case TOR_TLS_WANTWRITE:
  744. log_fn(LOG_DEBUG,"wanted write.");
  745. /* we're already writing */
  746. return 0;
  747. case TOR_TLS_WANTREAD:
  748. /* Make sure to avoid a loop if the receive buckets are empty. */
  749. log_fn(LOG_DEBUG,"wanted read.");
  750. if(!connection_is_reading(conn)) {
  751. connection_stop_writing(conn);
  752. conn->wants_to_write = 1;
  753. /* we'll start reading again when the next second arrives,
  754. * and then also start writing again.
  755. */
  756. }
  757. /* else no problem, we're already reading */
  758. return 0;
  759. /* case TOR_TLS_DONE:
  760. * for TOR_TLS_DONE, fall through to check if the flushlen
  761. * is empty, so we can stop writing.
  762. */
  763. }
  764. } else {
  765. if (flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen) < 0) {
  766. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  767. conn->has_sent_end = 1;
  768. connection_mark_for_close(conn);
  769. return -1;
  770. }
  771. }
  772. if(!connection_wants_to_flush(conn)) { /* it's done flushing */
  773. if(connection_finished_flushing(conn) < 0) {
  774. /* already marked */
  775. return -1;
  776. }
  777. }
  778. return 0;
  779. }
  780. /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
  781. * outbuf, and ask it to start writing.
  782. */
  783. void connection_write_to_buf(const char *string, int len, connection_t *conn) {
  784. if(!len || conn->marked_for_close)
  785. return;
  786. if(write_to_buf(string, len, conn->outbuf) < 0) {
  787. if(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT) {
  788. /* if it failed, it means we have our package/delivery windows set
  789. wrong compared to our max outbuf size. close the whole circuit. */
  790. log_fn(LOG_WARN,"write_to_buf failed. Closing circuit (fd %d).", conn->s);
  791. circuit_mark_for_close(circuit_get_by_conn(conn));
  792. } else {
  793. log_fn(LOG_WARN,"write_to_buf failed. Closing connection (fd %d).", conn->s);
  794. connection_mark_for_close(conn);
  795. }
  796. return;
  797. }
  798. connection_start_writing(conn);
  799. conn->outbuf_flushlen += len;
  800. }
  801. /** Return the conn to addr/port that has the most recent
  802. * timestamp_created, or NULL if no such conn exists. */
  803. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
  804. int i, n;
  805. connection_t *conn, *best=NULL;
  806. connection_t **carray;
  807. get_connection_array(&carray,&n);
  808. for(i=0;i<n;i++) {
  809. conn = carray[i];
  810. if(conn->addr == addr && conn->port == port && !conn->marked_for_close &&
  811. (!best || best->timestamp_created < conn->timestamp_created))
  812. best = conn;
  813. }
  814. return best;
  815. }
  816. /** Find a connection to the router described by addr and port,
  817. * or alternately any router with the same identity key.
  818. * This connection <em>must</em> be in an "open" state.
  819. * If not, return NULL.
  820. */
  821. /* XXX this twin thing is busted, now that we're rotating onion
  822. * keys. abandon/patch? */
  823. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port) {
  824. int i, n;
  825. connection_t *conn;
  826. routerinfo_t *router;
  827. connection_t **carray;
  828. /* first check if it's there exactly */
  829. conn = connection_exact_get_by_addr_port(addr,port);
  830. if(conn && connection_state_is_open(conn)) {
  831. log(LOG_DEBUG,"connection_twin_get_by_addr_port(): Found exact match.");
  832. return conn;
  833. }
  834. /* now check if any of the other open connections are a twin for this one */
  835. router = router_get_by_addr_port(addr,port);
  836. if(!router)
  837. return NULL;
  838. get_connection_array(&carray,&n);
  839. for(i=0;i<n;i++) {
  840. conn = carray[i];
  841. tor_assert(conn);
  842. if(connection_state_is_open(conn) &&
  843. !crypto_pk_cmp_keys(conn->identity_pkey, router->identity_pkey)) {
  844. log(LOG_DEBUG,"connection_twin_get_by_addr_port(): Found twin (%s).",conn->address);
  845. return conn;
  846. }
  847. }
  848. return NULL;
  849. }
  850. /** Return a connection of type <b>type</b> that is not marked for
  851. * close.
  852. */
  853. connection_t *connection_get_by_type(int type) {
  854. int i, n;
  855. connection_t *conn;
  856. connection_t **carray;
  857. get_connection_array(&carray,&n);
  858. for(i=0;i<n;i++) {
  859. conn = carray[i];
  860. if(conn->type == type && !conn->marked_for_close)
  861. return conn;
  862. }
  863. return NULL;
  864. }
  865. /** Return a connection of type <b>type</b> that is in state <b>state</b>,
  866. * and that is not marked for close.
  867. */
  868. connection_t *connection_get_by_type_state(int type, int state) {
  869. int i, n;
  870. connection_t *conn;
  871. connection_t **carray;
  872. get_connection_array(&carray,&n);
  873. for(i=0;i<n;i++) {
  874. conn = carray[i];
  875. if(conn->type == type && conn->state == state && !conn->marked_for_close)
  876. return conn;
  877. }
  878. return NULL;
  879. }
  880. /** Return the connection of type <b>type</b> that is in state
  881. * <b>state</b>, that was written to least recently, and that is not
  882. * marked for close.
  883. */
  884. connection_t *connection_get_by_type_state_lastwritten(int type, int state) {
  885. int i, n;
  886. connection_t *conn, *best=NULL;
  887. connection_t **carray;
  888. get_connection_array(&carray,&n);
  889. for(i=0;i<n;i++) {
  890. conn = carray[i];
  891. if(conn->type == type && conn->state == state && !conn->marked_for_close)
  892. if(!best || conn->timestamp_lastwritten < best->timestamp_lastwritten)
  893. best = conn;
  894. }
  895. return best;
  896. }
  897. /** Return a connection of type <b>type</b> that has rendquery equal
  898. * to <b>rendquery</b>, and that is not marked for close.
  899. */
  900. connection_t *connection_get_by_type_rendquery(int type, const char *rendquery) {
  901. int i, n;
  902. connection_t *conn;
  903. connection_t **carray;
  904. get_connection_array(&carray,&n);
  905. for(i=0;i<n;i++) {
  906. conn = carray[i];
  907. if(conn->type == type &&
  908. !conn->marked_for_close &&
  909. !rend_cmp_service_ids(rendquery, conn->rend_query))
  910. return conn;
  911. }
  912. return NULL;
  913. }
  914. /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
  915. int connection_is_listener(connection_t *conn) {
  916. if(conn->type == CONN_TYPE_OR_LISTENER ||
  917. conn->type == CONN_TYPE_AP_LISTENER ||
  918. conn->type == CONN_TYPE_DIR_LISTENER)
  919. return 1;
  920. return 0;
  921. }
  922. /** Return 1 if <b>conn</b> is in state "open" and is not marked
  923. * for close, else return 0.
  924. */
  925. int connection_state_is_open(connection_t *conn) {
  926. tor_assert(conn);
  927. if(conn->marked_for_close)
  928. return 0;
  929. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  930. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  931. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  932. return 1;
  933. return 0;
  934. }
  935. /** Return 1 if conn is in 'connecting' state, else return 0. */
  936. int connection_state_is_connecting(connection_t *conn) {
  937. tor_assert(conn);
  938. if (conn->marked_for_close)
  939. return 0;
  940. switch (conn->type)
  941. {
  942. case CONN_TYPE_OR:
  943. return conn->state == OR_CONN_STATE_CONNECTING;
  944. case CONN_TYPE_EXIT:
  945. return conn->state == EXIT_CONN_STATE_CONNECTING;
  946. case CONN_TYPE_DIR:
  947. return conn->state == DIR_CONN_STATE_CONNECTING;
  948. }
  949. return 0;
  950. }
  951. /** Write a destroy cell with circ ID <b>circ_id</b> onto OR connection
  952. * <b>conn</b>.
  953. *
  954. * Return 0.
  955. */
  956. int connection_send_destroy(uint16_t circ_id, connection_t *conn) {
  957. cell_t cell;
  958. tor_assert(conn);
  959. tor_assert(connection_speaks_cells(conn));
  960. memset(&cell, 0, sizeof(cell_t));
  961. cell.circ_id = circ_id;
  962. cell.command = CELL_DESTROY;
  963. log_fn(LOG_INFO,"Sending destroy (circID %d).", circ_id);
  964. connection_or_write_cell_to_buf(&cell, conn);
  965. return 0;
  966. }
  967. /** Process new bytes that have arrived on conn-\>inbuf.
  968. *
  969. * This function just passes conn to the connection-specific
  970. * connection_*_process_inbuf() function.
  971. */
  972. static int connection_process_inbuf(connection_t *conn) {
  973. tor_assert(conn);
  974. switch(conn->type) {
  975. case CONN_TYPE_OR:
  976. return connection_or_process_inbuf(conn);
  977. case CONN_TYPE_EXIT:
  978. case CONN_TYPE_AP:
  979. return connection_edge_process_inbuf(conn);
  980. case CONN_TYPE_DIR:
  981. return connection_dir_process_inbuf(conn);
  982. case CONN_TYPE_DNSWORKER:
  983. return connection_dns_process_inbuf(conn);
  984. case CONN_TYPE_CPUWORKER:
  985. return connection_cpu_process_inbuf(conn);
  986. default:
  987. log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
  988. return -1;
  989. }
  990. }
  991. /** We just finished flushing bytes from conn-\>outbuf, and there
  992. * are no more bytes remaining.
  993. *
  994. * This function just passes conn to the connection-specific
  995. * connection_*_finished_flushing() function.
  996. */
  997. static int connection_finished_flushing(connection_t *conn) {
  998. tor_assert(conn);
  999. // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
  1000. switch(conn->type) {
  1001. case CONN_TYPE_OR:
  1002. return connection_or_finished_flushing(conn);
  1003. case CONN_TYPE_AP:
  1004. case CONN_TYPE_EXIT:
  1005. return connection_edge_finished_flushing(conn);
  1006. case CONN_TYPE_DIR:
  1007. return connection_dir_finished_flushing(conn);
  1008. case CONN_TYPE_DNSWORKER:
  1009. return connection_dns_finished_flushing(conn);
  1010. case CONN_TYPE_CPUWORKER:
  1011. return connection_cpu_finished_flushing(conn);
  1012. default:
  1013. log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
  1014. return -1;
  1015. }
  1016. }
  1017. /** Called when our attempt to connect() to another server has just
  1018. * succeeded.
  1019. *
  1020. * This function just passes conn to the connection-specific
  1021. * connection_*_finished_connecting() function.
  1022. */
  1023. static int connection_finished_connecting(connection_t *conn)
  1024. {
  1025. tor_assert(conn);
  1026. switch (conn->type)
  1027. {
  1028. case CONN_TYPE_OR:
  1029. return connection_or_finished_connecting(conn);
  1030. case CONN_TYPE_EXIT:
  1031. return connection_edge_finished_connecting(conn);
  1032. case CONN_TYPE_DIR:
  1033. return connection_dir_finished_connecting(conn);
  1034. default:
  1035. tor_assert(0);
  1036. return -1;
  1037. }
  1038. }
  1039. /** Verify that connection <b>conn</b> has all of its invariants
  1040. * correct. Trigger an assert if anything is invalid.
  1041. */
  1042. void assert_connection_ok(connection_t *conn, time_t now)
  1043. {
  1044. tor_assert(conn);
  1045. tor_assert(conn->magic == CONNECTION_MAGIC);
  1046. tor_assert(conn->type >= _CONN_TYPE_MIN);
  1047. tor_assert(conn->type <= _CONN_TYPE_MAX);
  1048. if(conn->outbuf_flushlen > 0) {
  1049. tor_assert(connection_is_writing(conn) || conn->wants_to_write);
  1050. }
  1051. if(conn->hold_open_until_flushed)
  1052. tor_assert(conn->marked_for_close);
  1053. /* XXX check: wants_to_read, wants_to_write, s, poll_index,
  1054. * marked_for_close. */
  1055. /* buffers */
  1056. if (!connection_is_listener(conn)) {
  1057. assert_buf_ok(conn->inbuf);
  1058. assert_buf_ok(conn->outbuf);
  1059. }
  1060. #if 0 /* computers often go back in time; no way to know */
  1061. tor_assert(!now || conn->timestamp_lastread <= now);
  1062. tor_assert(!now || conn->timestamp_lastwritten <= now);
  1063. tor_assert(conn->timestamp_created <= conn->timestamp_lastread);
  1064. tor_assert(conn->timestamp_created <= conn->timestamp_lastwritten);
  1065. #endif
  1066. /* XXX Fix this; no longer so.*/
  1067. #if 0
  1068. if(conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR)
  1069. tor_assert(!conn->pkey);
  1070. /* pkey is set if we're a dir client, or if we're an OR in state OPEN
  1071. * connected to another OR.
  1072. */
  1073. #endif
  1074. if (conn->type != CONN_TYPE_OR) {
  1075. tor_assert(!conn->tls);
  1076. } else {
  1077. if(conn->state == OR_CONN_STATE_OPEN) {
  1078. /* tor_assert(conn->bandwidth > 0); */
  1079. /* the above isn't necessarily true: if we just did a TLS
  1080. * handshake but we didn't recognize the other peer, or it
  1081. * gave a bad cert/etc, then we won't have assigned bandwidth,
  1082. * yet it will be open. -RD
  1083. */
  1084. tor_assert(conn->receiver_bucket >= 0);
  1085. }
  1086. tor_assert(conn->addr && conn->port);
  1087. tor_assert(conn->address);
  1088. if (conn->state != OR_CONN_STATE_CONNECTING)
  1089. tor_assert(conn->tls);
  1090. }
  1091. if (conn->type != CONN_TYPE_EXIT && conn->type != CONN_TYPE_AP) {
  1092. tor_assert(!conn->stream_id);
  1093. tor_assert(!conn->next_stream);
  1094. tor_assert(!conn->cpath_layer);
  1095. tor_assert(!conn->package_window);
  1096. tor_assert(!conn->deliver_window);
  1097. tor_assert(!conn->done_sending);
  1098. tor_assert(!conn->done_receiving);
  1099. } else {
  1100. /* XXX unchecked: package window, deliver window. */
  1101. }
  1102. if (conn->type == CONN_TYPE_AP) {
  1103. tor_assert(conn->socks_request);
  1104. if (conn->state == AP_CONN_STATE_OPEN) {
  1105. tor_assert(conn->socks_request->has_finished);
  1106. tor_assert(conn->cpath_layer);
  1107. assert_cpath_layer_ok(conn->cpath_layer);
  1108. }
  1109. } else {
  1110. tor_assert(!conn->socks_request);
  1111. }
  1112. if(conn->type != CONN_TYPE_DIR) {
  1113. tor_assert(!conn->purpose); /* only used for dir types currently */
  1114. }
  1115. switch(conn->type)
  1116. {
  1117. case CONN_TYPE_OR_LISTENER:
  1118. case CONN_TYPE_AP_LISTENER:
  1119. case CONN_TYPE_DIR_LISTENER:
  1120. tor_assert(conn->state == LISTENER_STATE_READY);
  1121. break;
  1122. case CONN_TYPE_OR:
  1123. tor_assert(conn->state >= _OR_CONN_STATE_MIN &&
  1124. conn->state <= _OR_CONN_STATE_MAX);
  1125. break;
  1126. case CONN_TYPE_EXIT:
  1127. tor_assert(conn->state >= _EXIT_CONN_STATE_MIN &&
  1128. conn->state <= _EXIT_CONN_STATE_MAX);
  1129. break;
  1130. case CONN_TYPE_AP:
  1131. tor_assert(conn->state >= _AP_CONN_STATE_MIN &&
  1132. conn->state <= _AP_CONN_STATE_MAX);
  1133. tor_assert(conn->socks_request);
  1134. break;
  1135. case CONN_TYPE_DIR:
  1136. tor_assert(conn->state >= _DIR_CONN_STATE_MIN &&
  1137. conn->state <= _DIR_CONN_STATE_MAX);
  1138. tor_assert(conn->purpose >= _DIR_PURPOSE_MIN &&
  1139. conn->purpose <= _DIR_PURPOSE_MAX);
  1140. break;
  1141. case CONN_TYPE_DNSWORKER:
  1142. tor_assert(conn->state == DNSWORKER_STATE_IDLE ||
  1143. conn->state == DNSWORKER_STATE_BUSY);
  1144. break;
  1145. case CONN_TYPE_CPUWORKER:
  1146. tor_assert(conn->state >= _CPUWORKER_STATE_MIN &&
  1147. conn->state <= _CPUWORKER_STATE_MAX);
  1148. break;
  1149. default:
  1150. tor_assert(0);
  1151. }
  1152. }
  1153. /*
  1154. Local Variables:
  1155. mode:c
  1156. indent-tabs-mode:nil
  1157. c-basic-offset:2
  1158. End:
  1159. */