connection.c 39 KB

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