connection.c 41 KB

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