1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
9 * \brief Master header file for Tor-specific functionality.
14 #define OR_H_ID "$Id$"
18 #define WIN32_WINNT 0x400
19 #define _WIN32_WINNT 0x400
20 #define WIN32_LEAN_AND_MEAN
41 #include "../common/torint.h"
42 #ifdef HAVE_INTTYPES_H
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
48 #ifdef HAVE_SYS_LIMITS_H
49 #include <sys/limits.h>
51 #ifdef HAVE_MACHINE_LIMITS_H
53 /* FreeBSD has a bug where it complains that this file is obsolete,
54 and I should migrate to using sys/limits. It complains even when
56 #include <machine/limits.h>
59 #ifdef HAVE_SYS_TYPES_H
60 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
62 #ifdef HAVE_SYS_WAIT_H
65 #ifdef HAVE_SYS_FCNTL_H
66 #include <sys/fcntl.h>
71 #ifdef HAVE_SYS_IOCTL_H
72 #include <sys/ioctl.h>
74 #ifdef HAVE_SYS_SOCKET_H
75 #include <sys/socket.h>
77 #ifdef HAVE_SYS_TIME_H
80 #ifdef HAVE_SYS_STAT_H
83 #ifdef HAVE_NETINET_IN_H
84 #include <netinet/in.h>
86 #ifdef HAVE_ARPA_INET_H
87 #include <arpa/inet.h>
99 /** Upper bound on maximum simultaneous connections; can be lowered by
101 #define MAXCONNECTIONS 15000
104 /* No, we don't need to redefine FD_SETSIZE before including winsock:
105 * we use libevent now, and libevent handles the select() stuff. Yes,
106 * some documents imply that we need to redefine anyway if we're using
107 * select() anywhere in our application or in anything it links to: these
108 * documents are either the holy texts of a cargo cult of network
109 * programmers, or more likely a simplification of what's going on for
110 * people who haven't read winsock[2].c for themselves.
112 #if (_MSC_VER <= 1300)
115 #include <winsock2.h>
116 #include <ws2tcpip.h>
125 #define snprintf _snprintf
131 #error "Tor requires libevent to build."
134 #include "../common/crypto.h"
135 #include "../common/tortls.h"
136 #include "../common/log.h"
137 #include "../common/compat.h"
138 #include "../common/container.h"
139 #include "../common/util.h"
140 #include "../common/torgzip.h"
142 /* These signals are defined to help control_signal_act work.
143 * XXXX Move into compat.h ?
161 #if (SIZEOF_CELL_T != 0)
162 /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
163 * that our stuff always calls cell_t something different. */
164 #define cell_t tor_cell_t
167 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
168 #define MAX_NICKNAME_LEN 19
169 /* Hex digest plus dollar sign. */
170 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
171 /** Maximum size, in bytes, for resized buffers. */
172 #define MAX_BUF_SIZE ((1<<24)-1)
173 #define MAX_DIR_SIZE MAX_BUF_SIZE
175 /* For http parsing */
176 #define MAX_HEADERS_SIZE 50000
177 #define MAX_BODY_SIZE 500000
180 /** How long do we keep DNS cache entries before purging them? */
181 #define MAX_DNS_ENTRY_AGE (150*60)
183 #define MAX_DNS_ENTRY_AGE (30*60)
186 /** How often do we rotate onion keys? */
187 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
188 /** How often do we rotate TLS contexts? */
189 #define MAX_SSL_KEY_LIFETIME (120*60)
191 /** How old do we allow a router to get before removing it, either
192 * from the descriptor list (for dirservers) or the router list (for others)?
194 #define ROUTER_MAX_AGE (60*60*24)
197 CIRC_ID_TYPE_LOWER
=0,
198 CIRC_ID_TYPE_HIGHER
=1
201 #define _CONN_TYPE_MIN 3
202 /** Type for sockets listening for OR connections. */
203 #define CONN_TYPE_OR_LISTENER 3
204 /** A bidirectional TLS connection transmitting a sequence of cells.
205 * May be from an OR to an OR, or from an OP to an OR. */
206 #define CONN_TYPE_OR 4
207 /** A TCP connection from an onion router to a stream's destination. */
208 #define CONN_TYPE_EXIT 5
209 /** Type for sockets listening for SOCKS connections. */
210 #define CONN_TYPE_AP_LISTENER 6
211 /** A SOCKS proxy connection from the user application to the onion
213 #define CONN_TYPE_AP 7
214 /** Type for sockets listening for HTTP connections to the directory server. */
215 #define CONN_TYPE_DIR_LISTENER 8
216 /** Type for HTTP connections to the directory server. */
217 #define CONN_TYPE_DIR 9
218 /** Connection from the main process to a DNS worker process. */
219 #define CONN_TYPE_DNSWORKER 10
220 /** Connection from the main process to a CPU worker process. */
221 #define CONN_TYPE_CPUWORKER 11
222 /** Type for listenting for connections from user interface process */
223 #define CONN_TYPE_CONTROL_LISTENER 12
224 /** Type for connections from user interface process */
225 #define CONN_TYPE_CONTROL 13
226 #define _CONN_TYPE_MAX 13
228 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
230 /** State for any listener connection. */
231 #define LISTENER_STATE_READY 0
233 #define _DNSWORKER_STATE_MIN 1
234 /** State for a connection to a dnsworker process that's idle. */
235 #define DNSWORKER_STATE_IDLE 1
236 /** State for a connection to a dnsworker process that's resolving a hostname. */
237 #define DNSWORKER_STATE_BUSY 2
238 #define _DNSWORKER_STATE_MAX 2
240 #define _CPUWORKER_STATE_MIN 1
241 /** State for a connection to a cpuworker process that's idle. */
242 #define CPUWORKER_STATE_IDLE 1
243 /** State for a connection to a cpuworker process that's processing a
245 #define CPUWORKER_STATE_BUSY_ONION 2
246 #define _CPUWORKER_STATE_MAX 2
248 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
250 #define _OR_CONN_STATE_MIN 1
251 /** State for a connection to an OR: waiting for connect() to finish. */
252 #define OR_CONN_STATE_CONNECTING 1
253 /** State for a connection to an OR: waiting for proxy command to flush. */
254 #define OR_CONN_STATE_PROXY_FLUSHING 2
255 /** State for a connection to an OR: waiting for proxy response. */
256 #define OR_CONN_STATE_PROXY_READING 3
257 /** State for a connection to an OR: SSL is handshaking, not done yet. */
258 #define OR_CONN_STATE_HANDSHAKING 4
259 /** State for a connection to an OR: Ready to send/receive cells. */
260 #define OR_CONN_STATE_OPEN 5
261 #define _OR_CONN_STATE_MAX 5
263 #define _EXIT_CONN_STATE_MIN 1
264 /** State for an exit connection: waiting for response from dns farm. */
265 #define EXIT_CONN_STATE_RESOLVING 1
266 /** State for an exit connection: waiting for connect() to finish. */
267 #define EXIT_CONN_STATE_CONNECTING 2
268 /** State for an exit connection: open and ready to transmit data. */
269 #define EXIT_CONN_STATE_OPEN 3
270 /** State for an exit connection: waiting to be removed. */
271 #define EXIT_CONN_STATE_RESOLVEFAILED 4
272 #define _EXIT_CONN_STATE_MAX 4
274 /* the AP state values must be disjoint from the EXIT state values */
275 #define _AP_CONN_STATE_MIN 5
276 /** State for a SOCKS connection: waiting for SOCKS request. */
277 #define AP_CONN_STATE_SOCKS_WAIT 5
278 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
279 * rendezvous rescriptor. */
280 #define AP_CONN_STATE_RENDDESC_WAIT 6
281 /** The controller will attach this connection to a circuit; it isn't our
283 #define AP_CONN_STATE_CONTROLLER_WAIT 7
284 /** State for a SOCKS connection: waiting for a completed circuit. */
285 #define AP_CONN_STATE_CIRCUIT_WAIT 8
286 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
287 #define AP_CONN_STATE_CONNECT_WAIT 9
288 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
289 #define AP_CONN_STATE_RESOLVE_WAIT 10
290 /** State for a SOCKS connection: ready to send and receive. */
291 #define AP_CONN_STATE_OPEN 11
292 #define _AP_CONN_STATE_MAX 11
294 #define _DIR_CONN_STATE_MIN 1
295 /** State for connection to directory server: waiting for connect(). */
296 #define DIR_CONN_STATE_CONNECTING 1
297 /** State for connection to directory server: sending HTTP request. */
298 #define DIR_CONN_STATE_CLIENT_SENDING 2
299 /** State for connection to directory server: reading HTTP response. */
300 #define DIR_CONN_STATE_CLIENT_READING 3
301 /** State for connection at directory server: waiting for HTTP request. */
302 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
303 /** State for connection at directory server: sending HTTP response. */
304 #define DIR_CONN_STATE_SERVER_WRITING 5
305 #define _DIR_CONN_STATE_MAX 5
307 #define _CONTROL_CONN_STATE_MIN 1
308 #define CONTROL_CONN_STATE_OPEN_V0 1
309 #define CONTROL_CONN_STATE_OPEN_V1 2
310 #define CONTROL_CONN_STATE_NEEDAUTH_V0 3
311 #define CONTROL_CONN_STATE_NEEDAUTH_V1 4
312 #define _CONTROL_CONN_STATE_MAX 4
314 #define _DIR_PURPOSE_MIN 1
315 /** Purpose for connection to directory server: download a directory. */
316 #define DIR_PURPOSE_FETCH_DIR 1
317 /** Purpose for connection to directory server: download just the list
318 * of running routers. */
319 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
320 /** Purpose for connection to directory server: download a rendezvous
322 #define DIR_PURPOSE_FETCH_RENDDESC 3
323 /** Purpose for connection to directory server: set after a rendezvous
324 * descriptor is downloaded. */
325 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
326 /** Purpose for connection to directory server: upload a server descriptor. */
327 #define DIR_PURPOSE_UPLOAD_DIR 5
328 /** Purpose for connection to directory server: upload a rendezvous
330 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
331 /** Purpose for connection at a directory server. */
332 #define DIR_PURPOSE_SERVER 7
333 #define _DIR_PURPOSE_MAX 7
335 #define _EXIT_PURPOSE_MIN 1
336 #define EXIT_PURPOSE_CONNECT 1
337 #define EXIT_PURPOSE_RESOLVE 2
338 #define _EXIT_PURPOSE_MAX 2
340 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
341 #define CIRCUIT_STATE_BUILDING 0
342 /** Circuit state: Waiting to process the onionskin. */
343 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
344 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
345 #define CIRCUIT_STATE_OR_WAIT 2
346 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
347 #define CIRCUIT_STATE_OPEN 3
349 #define _CIRCUIT_PURPOSE_MIN 1
351 /* these circuits were initiated elsewhere */
352 #define _CIRCUIT_PURPOSE_OR_MIN 1
353 /** OR-side circuit purpose: normal circuit, at OR. */
354 #define CIRCUIT_PURPOSE_OR 1
355 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
356 #define CIRCUIT_PURPOSE_INTRO_POINT 2
357 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
358 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
359 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
360 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
361 #define _CIRCUIT_PURPOSE_OR_MAX 4
363 /* these circuits originate at this node */
365 /* here's how circ client-side purposes work:
366 * normal circuits are C_GENERAL.
367 * circuits that are c_introducing are either on their way to
368 * becoming open, or they are open and waiting for a
369 * suitable rendcirc before they send the intro.
370 * circuits that are c_introduce_ack_wait have sent the intro,
371 * but haven't gotten a response yet.
372 * circuits that are c_establish_rend are either on their way
373 * to becoming open, or they are open and have sent the
374 * establish_rendezvous cell but haven't received an ack.
375 * circuits that are c_rend_ready are open and have received a
376 * rend ack, but haven't heard from bob yet. if they have a
377 * buildstate->pending_final_cpath then they're expecting a
378 * cell from bob, else they're not.
379 * circuits that are c_rend_ready_intro_acked are open, and
380 * some intro circ has sent its intro and received an ack.
381 * circuits that are c_rend_joined are open, have heard from
382 * bob, and are talking to him.
384 /** Client-side circuit purpose: Normal circuit, with cpath. */
385 #define CIRCUIT_PURPOSE_C_GENERAL 5
386 /** Client-side circuit purpose: at Alice, connecting to intro point. */
387 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
388 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
389 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
390 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
391 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
392 /** Client-side circuit purpose: at Alice, waiting for ack. */
393 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
394 /** Client-side circuit purpose: at Alice, waiting for Bob. */
395 #define CIRCUIT_PURPOSE_C_REND_READY 10
396 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
397 * has been acknowledged. */
398 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
399 /** Client-side circuit purpose: at Alice, rendezvous established. */
400 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
402 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
403 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
404 /** Hidden-service-side circuit purpose: at Bob, successfully established
406 #define CIRCUIT_PURPOSE_S_INTRO 14
407 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
408 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
409 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
410 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
411 /** A testing circuit; not meant to be used for actual traffic. */
412 #define CIRCUIT_PURPOSE_TESTING 17
413 #define _CIRCUIT_PURPOSE_MAX 17
415 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
416 * that this OP has originated. */
417 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
418 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
420 #define RELAY_COMMAND_BEGIN 1
421 #define RELAY_COMMAND_DATA 2
422 #define RELAY_COMMAND_END 3
423 #define RELAY_COMMAND_CONNECTED 4
424 #define RELAY_COMMAND_SENDME 5
425 #define RELAY_COMMAND_EXTEND 6
426 #define RELAY_COMMAND_EXTENDED 7
427 #define RELAY_COMMAND_TRUNCATE 8
428 #define RELAY_COMMAND_TRUNCATED 9
429 #define RELAY_COMMAND_DROP 10
430 #define RELAY_COMMAND_RESOLVE 11
431 #define RELAY_COMMAND_RESOLVED 12
433 #define RELAY_COMMAND_ESTABLISH_INTRO 32
434 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
435 #define RELAY_COMMAND_INTRODUCE1 34
436 #define RELAY_COMMAND_INTRODUCE2 35
437 #define RELAY_COMMAND_RENDEZVOUS1 36
438 #define RELAY_COMMAND_RENDEZVOUS2 37
439 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
440 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
441 #define RELAY_COMMAND_INTRODUCE_ACK 40
443 #define END_STREAM_REASON_MISC 1
444 #define END_STREAM_REASON_RESOLVEFAILED 2
445 #define END_STREAM_REASON_CONNECTREFUSED 3
446 #define END_STREAM_REASON_EXITPOLICY 4
447 #define END_STREAM_REASON_DESTROY 5
448 #define END_STREAM_REASON_DONE 6
449 #define END_STREAM_REASON_TIMEOUT 7
450 /* 8 is unallocated. */
451 #define END_STREAM_REASON_HIBERNATING 9
452 #define END_STREAM_REASON_INTERNAL 10
453 #define END_STREAM_REASON_RESOURCELIMIT 11
454 #define END_STREAM_REASON_CONNRESET 12
455 #define END_STREAM_REASON_TORPROTOCOL 13
457 /* These high-numbered end reasons are not part of the official spec,
458 * and are not intended to be put in relay end cells. They are here
459 * to be more informative when sending back socks replies to the
461 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
462 #define END_STREAM_REASON_CANT_ATTACH 257
463 #define END_STREAM_REASON_NET_UNREACHABLE 258
465 #define RESOLVED_TYPE_HOSTNAME 0
466 #define RESOLVED_TYPE_IPV4 4
467 #define RESOLVED_TYPE_IPV6 6
468 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
469 #define RESOLVED_TYPE_ERROR 0xF1
471 /** Length of 'y' portion of 'y.onion' URL. */
472 #define REND_SERVICE_ID_LEN 16
474 #define CELL_DIRECTION_IN 1
475 #define CELL_DIRECTION_OUT 2
478 #define CIRCWINDOW_START 10000
479 #define CIRCWINDOW_INCREMENT 1000
480 #define STREAMWINDOW_START 5000
481 #define STREAMWINDOW_INCREMENT 500
483 #define CIRCWINDOW_START 1000
484 #define CIRCWINDOW_INCREMENT 100
485 #define STREAMWINDOW_START 500
486 #define STREAMWINDOW_INCREMENT 50
490 #define CELL_PADDING 0
491 #define CELL_CREATE 1
492 #define CELL_CREATED 2
494 #define CELL_DESTROY 4
495 #define CELL_CREATE_FAST 5
496 #define CELL_CREATED_FAST 6
498 /* people behind fascist firewalls use only these ports */
499 #define REQUIRED_FIREWALL_DIRPORT 80
500 #define REQUIRED_FIREWALL_ORPORT 443
502 /* legal characters in a nickname */
503 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
505 /** Name to use in client TLS certificates if no nickname is given.*/
506 #define DEFAULT_CLIENT_NICKNAME "client"
508 #define SOCKS4_NETWORK_LEN 8
511 SOCKS5_SUCCEEDED
= 0x00,
512 SOCKS5_GENERAL_ERROR
= 0x01,
513 SOCKS5_NOT_ALLOWED
= 0x02,
514 SOCKS5_NET_UNREACHABLE
= 0x03,
515 SOCKS5_HOST_UNREACHABLE
= 0x04,
516 SOCKS5_CONNECTION_REFUSED
= 0x05,
517 SOCKS5_TTL_EXPIRED
= 0x06,
518 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
519 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
520 } socks5_reply_status_t
;
524 * Relay command [1 byte]
525 * Recognized [2 bytes]
526 * Stream ID [2 bytes]
527 * Partial SHA-1 [4 bytes]
529 * Relay payload [498 bytes]
532 #define CELL_PAYLOAD_SIZE 509
533 #define CELL_NETWORK_SIZE 512
535 #define RELAY_HEADER_SIZE (1+2+2+4+2)
536 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
538 /** Parsed onion routing cell. All communication from OP-to-OR, or from
539 * OR-to-OR, is via cells. */
541 uint16_t circ_id
; /**< Circuit which received the cell. */
542 uint8_t command
; /**< Type of the cell: one of PADDING, CREATE, RELAY,
544 char payload
[CELL_PAYLOAD_SIZE
]; /**< Cell body. */
547 /** Beginning of a RELAY cell payload. */
549 uint8_t command
; /**< The end-to-end relay command. */
550 uint16_t recognized
; /**< Used to tell whether cell is for us. */
551 uint16_t stream_id
; /**< Which stream is this cell associated with? */
552 char integrity
[4]; /**< Used to tell whether cell is corrupted. */
553 uint16_t length
; /**< How long is the payload body? */
556 typedef struct buf_t buf_t
;
557 typedef struct socks_request_t socks_request_t
;
559 #define CONNECTION_MAGIC 0x7C3C304Eu
561 /** Description of a connection to another host or process, and associated
564 * A connection is named based on what it's connected to -- an "OR
565 * connection" has an onion router on the other end, an "OP connection"
566 * (nearly obsolete) has an onion proxy on the other end, an "exit
567 * connection" has a website or other server on the other end, and an
568 * "AP connection" has an application proxy (and thus a user) on the
571 * Every connection has a type and a state. Connections never change
572 * their type, but can go through many state changes in their lifetime.
574 * Every connection has two associated input and output buffers.
575 * Listeners don't use them. For non-listener connections, incoming
576 * data is appended to conn->inbuf, and outgoing data is taken from
577 * conn->outbuf. Connections differ primarily in the functions called
578 * to fill and drain these buffers.
580 struct connection_t
{
581 uint32_t magic
; /**< For memory debugging: must equal CONNECTION_MAGIC. */
583 uint8_t type
; /**< What kind of connection is this? */
584 uint8_t state
; /**< Current state of this connection. */
585 uint8_t purpose
; /**< Only used for DIR types currently. */
586 uint8_t wants_to_read
; /**< Boolean: should we start reading again once
587 * the bandwidth throttler allows it?
589 uint8_t wants_to_write
; /**< Boolean: should we start writing again once
590 * the bandwidth throttler allows reads?
592 int s
; /**< Our socket; -1 if this connection is closed. */
593 int poll_index
; /* XXXX rename. */
594 struct event
*read_event
; /**< libevent event structure. */
595 struct event
*write_event
; /**< libevent event structure. */
596 int marked_for_close
; /**< Boolean: should we close this conn on the next
597 * iteration of the main loop?
599 const char *marked_for_close_file
; /**< For debugging: in which file were
600 * we marked for close? */
601 int hold_open_until_flushed
; /**< Despite this connection's being marked
602 * for close, do we flush it before closing it?
605 buf_t
*inbuf
; /**< Buffer holding data read over this connection. */
606 int inbuf_reached_eof
; /**< Boolean: did read() return 0 on this conn? */
607 time_t timestamp_lastread
; /**< When was the last time poll() said we could read? */
609 buf_t
*outbuf
; /**< Buffer holding data to write over this connection. */
610 size_t outbuf_flushlen
; /**< How much data should we try to flush from the
612 time_t timestamp_lastwritten
; /**< When was the last time poll() said we could write? */
614 time_t timestamp_created
; /**< When was this connection_t created? */
615 time_t timestamp_lastempty
; /**< When was the outbuf last completely empty?*/
617 uint32_t addr
; /**< IP of the other side of the connection; used to identify
618 * routers, along with port. */
619 uint16_t port
; /**< If non-zero, port on the other end
620 * of the connection. */
621 char *address
; /**< FQDN (or IP) of the guy on the other end.
622 * strdup into this, because free_connection frees it.
624 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for the other side's
626 char identity_digest
[DIGEST_LEN
]; /**< Hash of identity_pkey */
627 char *nickname
; /**< Nickname of OR on other side (if any). */
629 /** Nickname of planned exit node -- to be used with .exit support. */
630 char *chosen_exit_name
;
632 /* Used only by OR connections: */
633 tor_tls
*tls
; /**< TLS connection state (OR only.) */
634 uint16_t next_circ_id
; /**< Which circ_id do we try to use next on
635 * this connection? This is always in the
636 * range 0..1<<15-1. (OR only.)*/
638 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
639 int bandwidth
; /**< Connection bandwidth. (OPEN ORs only.) */
640 int receiver_bucket
; /**< When this hits 0, stop receiving. Every second we
641 * add 'bandwidth' to this, capping it at 10*bandwidth.
644 circ_id_type_t circ_id_type
; /**< When we send CREATE cells along this
645 * connection, which half of the space should
648 /* Used only by DIR and AP connections: */
649 char rend_query
[REND_SERVICE_ID_LEN
+1]; /**< What rendezvous service are we
650 * querying for? (DIR/AP only) */
652 /* Used only by edge connections: */
654 struct connection_t
*next_stream
; /**< Points to the next stream at this
655 * edge, if any (Edge only). */
656 struct crypt_path_t
*cpath_layer
; /**< A pointer to which node in the circ
657 * this conn exits at. (Edge only.) */
658 int package_window
; /**< How many more relay cells can i send into the
659 * circuit? (Edge only.) */
660 int deliver_window
; /**< How many more relay cells can end at me? (Edge
663 int done_sending
; /**< For half-open connections; not used currently. */
664 int done_receiving
; /**< For half-open connections; not used currently. */
665 char has_sent_end
; /**< For debugging: set once we've set the stream end,
666 and check in circuit_about_to_close_connection(). */
667 struct circuit_t
*on_circuit
; /**< The circuit (if any) that this edge
668 * connection is using. */
670 /* Used only by AP connections */
671 socks_request_t
*socks_request
; /**< SOCKS structure describing request (AP
674 /** Quasi-global identifier for this connection; used for control.c */
675 /* XXXX NM This can get re-used after 2**32 circuits. */
676 uint32_t global_identifier
;
678 /* Used only by control connections */
680 uint32_t incoming_cmd_len
;
681 uint32_t incoming_cmd_cur_len
;
684 /* Used only by control v0 connections */
685 uint16_t incoming_cmd_type
;
688 typedef struct connection_t connection_t
;
690 #define ADDR_POLICY_ACCEPT 1
691 #define ADDR_POLICY_REJECT 2
693 /** A linked list of policy rules */
694 typedef struct addr_policy_t
{
695 char policy_type
; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
696 char *string
; /**< String representation of this rule. */
697 uint32_t addr
; /**< Base address to accept or reject. */
698 uint32_t msk
; /**< Accept/reject all addresses <b>a</b> such that
699 * a & msk == <b>addr</b> & msk . */
700 uint16_t prt_min
; /**< Lowest port number to accept/reject. */
701 uint16_t prt_max
; /**< Highest port number to accept/reject. */
703 struct addr_policy_t
*next
; /**< Next rule in list. */
706 /** Information about another onion router in the network. */
708 char *signed_descriptor
; /**< The original signed descriptor for this router*/
710 char *address
; /**< Location of OR: either a hostname or an IP address. */
711 char *nickname
; /**< Human-readable OR name. */
713 uint32_t addr
; /**< IPv4 address of OR, in host order. */
714 uint16_t or_port
; /**< Port for OR-to-OR and OP-to-OR connections. */
715 uint16_t dir_port
; /**< Port for HTTP directory connections. */
717 time_t published_on
; /**< When was the information in this routerinfo_t
720 crypto_pk_env_t
*onion_pkey
; /**< Public RSA key for onions. */
721 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for signing. */
722 char identity_digest
[DIGEST_LEN
]; /**< Digest of identity key */
724 char *platform
; /**< What software/operating system is this OR using? */
727 uint32_t bandwidthrate
; /**< How many bytes does this OR add to its token
728 * bucket per second? */
729 uint32_t bandwidthburst
; /**< How large is this OR's token bucket? */
730 /** How many bytes/s is this router known to handle? */
731 uint32_t bandwidthcapacity
;
732 addr_policy_t
*exit_policy
; /**< What streams will this OR permit
734 long uptime
; /**< How many seconds the router claims to have been up */
736 int is_running
; /**< As far as we know, is this OR currently running? */
737 time_t status_set_at
; /**< When did we last update is_running? */
738 int is_verified
; /**< Has a trusted dirserver validated this OR? */
740 smartlist_t
*declared_family
; /**< Nicknames of router which this router
741 * claims are its family. */
743 char *contact_info
; /**< Declared contact info for this router. */
746 /** Contents of a running-routers list */
747 typedef struct running_routers_t
{
748 time_t published_on
; /**< When was the list marked as published? */
749 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
750 smartlist_t
*running_routers
;
753 /** Contents of a directory of onion routers. */
755 /** List of routerinfo_t */
756 smartlist_t
*routers
;
757 /** Which versions of tor are recommended by this directory? */
758 char *software_versions
;
759 /** When was the most recent directory that contributed to this list
763 time_t running_routers_updated_on
;
764 /** What is the most recently received running_routers structure? */
765 running_routers_t
*running_routers
;
766 /** Which router is claimed to have signed it? */
767 char *signing_router
;
770 /** Information on router used when extending a circuit. (We don't need a
771 * full routerinfo_t to extend: we only need addr:port:keyid to build an OR
772 * connection, and onion_key to create the onionskin.) */
773 typedef struct extend_info_t
{
774 char nickname
[MAX_HEX_NICKNAME_LEN
+1]; /**< This router's nickname for display. */
775 char identity_digest
[DIGEST_LEN
]; /**< Hash of this router's identity key. */
776 uint32_t addr
; /**< IP address in host order. */
777 uint16_t port
; /**< OR port. */
778 crypto_pk_env_t
*onion_key
; /**< Current onionskin key. */
781 #define CRYPT_PATH_MAGIC 0x70127012u
783 /** Holds accounting information for a single step in the layered encryption
784 * performed by a circuit. Used only at the client edge of a circuit. */
785 typedef struct crypt_path_t
{
788 /* crypto environments */
789 /** Encryption key and counter for cells heading towards the OR at this
791 crypto_cipher_env_t
*f_crypto
;
792 /** Encryption key and counter for cells heading back from the OR at this
794 crypto_cipher_env_t
*b_crypto
;
796 /** Digest state for cells heading towards the OR at this step. */
797 crypto_digest_env_t
*f_digest
; /* for integrity checking */
798 /** Digest state for cells heading away from the OR at this step. */
799 crypto_digest_env_t
*b_digest
;
801 /** Current state of Diffie-Hellman key negotiation with the OR at this
803 crypto_dh_env_t
*dh_handshake_state
;
804 char fast_handshake_state
[DIGEST_LEN
];
805 /** Negotiated key material shared with the OR at this step. */
806 char handshake_digest
[DIGEST_LEN
];/* KH in tor-spec.txt */
808 /** Information to extend to the OR at this step. */
809 extend_info_t
*extend_info
;
811 /** Is the circuit built to this step? Must be one of:
812 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
813 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
814 * and not received an EXTENDED/CREATED)
815 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
817 #define CPATH_STATE_CLOSED 0
818 #define CPATH_STATE_AWAITING_KEYS 1
819 #define CPATH_STATE_OPEN 2
820 struct crypt_path_t
*next
; /**< Link to next crypt_path_t in the circuit.
821 * (The list is circular, so the last node
822 * links to the first.) */
823 struct crypt_path_t
*prev
; /**< Link to previous crypt_path_t in the
826 int package_window
; /**< How many bytes are we allowed to originate ending
828 int deliver_window
; /**< How many bytes are we willing to deliver originating
832 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
834 #define DH_KEY_LEN DH_BYTES
835 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
838 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
839 #define REND_COOKIE_LEN DIGEST_LEN
841 /** Information used to build a circuit. */
843 /** Intended length of the final circuit. */
844 int desired_path_len
;
845 /** How to extend to the planned exit node. */
846 extend_info_t
*chosen_exit
;
847 /** Whether every node in the circ must have adequate uptime. */
849 /** Whether every node in the circ must have adequate capacity. */
851 /** Whether the last hop was picked with exiting in mind. */
853 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
854 crypt_path_t
*pending_final_cpath
;
855 /** How many times has building a circuit for this task failed? */
857 /** At what time should we give up on this task? */
859 } cpath_build_state_t
;
861 #define CIRCUIT_MAGIC 0x35315243u
864 * A circuit is a path over the onion routing
865 * network. Applications can connect to one end of the circuit, and can
866 * create exit connections at the other end of the circuit. AP and exit
867 * connections have only one circuit associated with them (and thus these
868 * connection types are closed when the circuit is closed), whereas
869 * OR connections multiplex many circuits at once, and stay standing even
870 * when there are no circuits running over them.
872 * A circuit_t structure fills two roles. First, a circuit_t links two
873 * connections together: either an edge connection and an OR connection,
874 * or two OR connections. (When joined to an OR connection, a circuit_t
875 * affects only cells sent to a particular circID on that connection. When
876 * joined to an edge connection, a circuit_t affects all data.)
878 * Second, a circuit_t holds the cipher keys and state for sending data
879 * along a given circuit. At the OP, it has a sequence of ciphers, each
880 * of which is shared with a single OR along the circuit. Separate
881 * ciphers are used for data going "forward" (away from the OP) and
882 * "backward" (towards the OP). At the OR, a circuit has only two stream
883 * ciphers: one for data going forward, and one for data going backward.
886 uint32_t magic
; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
888 int marked_for_close
; /**< Should we close this circuit at the end of the
890 const char *marked_for_close_file
; /**< For debugging: in which file was this
891 * circuit marked for close? */
893 /** The IPv4 address of the OR that is next in this circuit. */
895 /** The port for the OR that is next in this circuit. */
897 /** The OR connection that is previous in this circuit. */
898 connection_t
*p_conn
;
899 /** The OR connection that is next in this circuit. */
900 connection_t
*n_conn
;
901 /** The identity hash of n_conn. */
902 char n_conn_id_digest
[DIGEST_LEN
];
903 /** Linked list of AP streams associated with this circuit. */
904 connection_t
*p_streams
;
905 /** Linked list of Exit streams associated with this circuit. */
906 connection_t
*n_streams
;
907 /** Linked list of Exit streams associated with this circuit that are
908 * still being resolved. */
909 connection_t
*resolving_streams
;
910 /** The next stream_id that will be tried when we're attempting to
911 * construct a new AP stream originating at this circuit. */
912 uint16_t next_stream_id
;
913 /** How many relay data cells can we package (read from edge streams)
914 * on this circuit before we receive a circuit-level sendme cell asking
917 /** How many relay data cells will we deliver (write to edge streams)
918 * on this circuit? When deliver_window gets low, we send some
919 * circuit-level sendme cells to indicate that we're willing to accept
923 /** The circuit_id used in the previous (backward) hop of this circuit. */
925 /** The circuit_id used in the next (forward) hop of this circuit. */
928 /** The cipher used by intermediate hops for cells heading toward the
930 crypto_cipher_env_t
*p_crypto
;
931 /** The cipher used by intermediate hops for cells heading away from
933 crypto_cipher_env_t
*n_crypto
;
935 /** The integrity-checking digest used by intermediate hops, for
936 * cells packaged here and heading towards the OP.
938 crypto_digest_env_t
*p_digest
;
939 /** The integrity-checking digest used by intermediate hops, for
940 * cells packaged at the OP and arriving here.
942 crypto_digest_env_t
*n_digest
;
944 /** Build state for this circuit. It includes the intended path
945 * length, the chosen exit router, rendezvous information, etc.
947 cpath_build_state_t
*build_state
;
948 /** The doubly-linked list of crypt_path_t entries, one per hop,
949 * for this circuit. This includes ciphers for each hop,
950 * integrity-checking digests for each hop, and package/delivery
951 * windows for each hop.
953 * The cpath field is defined only when we are the circuit's origin.
957 /** For storage while passing to cpuworker, or while n_conn is pending. */
958 char onionskin
[ONIONSKIN_CHALLENGE_LEN
];
960 char handshake_digest
[DIGEST_LEN
]; /**< Stores KH for intermediate hops. */
962 time_t timestamp_created
; /**< When was this circuit created? */
963 time_t timestamp_dirty
; /**< When the circuit was first used, or 0 if the
964 * circuit is clean. */
966 uint8_t state
; /**< Current status of this circuit. */
967 uint8_t purpose
; /**< Why are we creating this circuit? */
970 * The rend_query field holds y portion of y.onion (nul-terminated)
971 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
972 * for a hidden service, or is S_*.
974 char rend_query
[REND_SERVICE_ID_LEN
+1];
976 /** The rend_pk_digest field holds a hash of location-hidden service's
977 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
979 char rend_pk_digest
[DIGEST_LEN
];
981 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
982 * C_ESTABLISH_REND. Filled with zeroes otherwise.
984 char rend_cookie
[REND_COOKIE_LEN
];
986 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
987 * is not marked for close. */
988 struct circuit_t
*rend_splice
;
990 /** Quasi-global identifier for this circuit; used for control.c */
991 /* XXXX NM This can get re-used after 2**32 circuits. */
992 uint32_t global_identifier
;
994 struct circuit_t
*next
; /**< Next circuit in linked list. */
997 typedef struct circuit_t circuit_t
;
999 #define ALLOW_UNVERIFIED_ENTRY 1
1000 #define ALLOW_UNVERIFIED_EXIT 2
1001 #define ALLOW_UNVERIFIED_MIDDLE 4
1002 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
1003 #define ALLOW_UNVERIFIED_INTRODUCTION 16
1005 typedef struct exit_redirect_t
{
1016 typedef struct config_line_t
{
1019 struct config_line_t
*next
;
1022 /** Configuration options for a Tor process */
1026 /** What should the tor process actually do? */
1028 CMD_RUN_TOR
=0, CMD_LIST_FINGERPRINT
, CMD_HASH_PASSWORD
,
1031 const char *command_arg
; /**< Argument for command-line option. */
1033 config_line_t
*OldLogOptions
; /**< List of configuration lines
1034 * for logfiles, old style. */
1036 config_line_t
*Logs
; /**< New-style list of configuration lines
1039 char *DebugLogFile
; /**< Where to send verbose log messages. */
1040 char *DataDirectory
; /**< OR only: where to store long-term data. */
1041 char *Nickname
; /**< OR only: nickname of this onion router. */
1042 char *Address
; /**< OR only: configured address for this onion router. */
1043 char *PidFile
; /**< Where to store PID of Tor process. */
1045 char *ExitNodes
; /**< Comma-separated list of nicknames of ORs to consider
1047 char *EntryNodes
; /**< Comma-separated list of nicknames of ORs to consider
1048 * as entry points. */
1049 int StrictExitNodes
; /**< Boolean: When none of our ExitNodes are up, do we
1050 * stop building circuits? */
1051 int StrictEntryNodes
; /**< Boolean: When none of our EntryNodes are up, do we
1052 * stop building circuits? */
1053 char *ExcludeNodes
; /**< Comma-separated list of nicknames of ORs not to
1054 * use in circuits. */
1056 char *RendNodes
; /**< Comma-separated list of nicknames used as introduction
1058 char *RendExcludeNodes
; /**< Comma-separated list of nicknames not to use
1059 * as introduction points. */
1061 smartlist_t
*AllowUnverifiedNodes
; /**< List of "entry", "middle", "exit" */
1062 int _AllowUnverified
; /**< Bitmask; derived from AllowUnverifiedNodes; */
1063 config_line_t
*ExitPolicy
; /**< Lists of exit policy components. */
1064 config_line_t
*SocksPolicy
; /**< Lists of socks policy components */
1065 config_line_t
*DirPolicy
; /**< Lists of dir policy components */
1066 /** Addresses to bind for listening for SOCKS connections. */
1067 config_line_t
*SocksBindAddress
;
1068 /** Addresses to bind for listening for OR connections. */
1069 config_line_t
*ORBindAddress
;
1070 /** Addresses to bind for listening for directory connections. */
1071 config_line_t
*DirBindAddress
;
1072 /** Local address to bind outbound sockets */
1073 char *OutboundBindAddress
;
1074 /** Directory server only: which versions of
1075 * Tor should we tell users to run? */
1076 config_line_t
*RecommendedVersions
;
1077 /** Whether dirservers refuse router descriptors with private IPs. */
1078 int DirAllowPrivateAddresses
;
1079 char *User
; /**< Name of user to run Tor as. */
1080 char *Group
; /**< Name of group to run Tor as. */
1081 double PathlenCoinWeight
; /**< Parameter used to configure average path
1082 * length (alpha in geometric distribution). */
1083 int ORPort
; /**< Port to listen on for OR connections. */
1084 int SocksPort
; /**< Port to listen on for SOCKS connections. */
1085 int ControlPort
; /**< Port to listen on for control connections. */
1086 int DirPort
; /**< Port to listen on for directory connections. */
1087 int AuthoritativeDir
; /**< Boolean: is this an authoritative directory? */
1088 int ClientOnly
; /**< Boolean: should we never evolve into a server role? */
1089 int NoPublish
; /**< Boolean: should we never publish a descriptor? */
1090 int ConnLimit
; /**< Requested maximum number of simultaneous connections. */
1091 int _ConnLimit
; /**< Actual maximum number of simultaneous connections. */
1092 int IgnoreVersion
; /**< If true, run no matter what versions of Tor the
1093 * directory recommends. */
1094 int RunAsDaemon
; /**< If true, run in the background. (Unix only) */
1095 int FascistFirewall
; /**< Whether to prefer ORs reachable on open ports. */
1096 smartlist_t
*FirewallPorts
; /**< Which ports our firewall allows (strings). */
1097 smartlist_t
*FirewallIPs
; /**< Which IPs our firewall allows (strings). */
1098 /** Application ports that require all nodes in circ to have sufficient uptime. */
1099 smartlist_t
*LongLivedPorts
;
1100 /** Should we try to reuse the same exit node for a given host */
1101 smartlist_t
*TrackHostExits
;
1102 int TrackHostExitsExpire
; /**< Number of seconds until we expire an addressmap */
1103 config_line_t
*AddressMap
; /**< List of address map directives. */
1104 int DirFetchPeriod
; /**< How often do we fetch new directories? */
1105 int DirPostPeriod
; /**< How often do we post our server descriptor to the
1106 * authoritative directory servers? */
1107 int RendPostPeriod
; /**< How often do we post each rendezvous service
1108 * descriptor? Remember to publish them independently. */
1109 int StatusFetchPeriod
; /**< How often do we fetch running-routers lists? */
1110 int KeepalivePeriod
; /**< How often do we send padding cells to keep
1111 * connections alive? */
1112 int MaxOnionsPending
; /**< How many circuit CREATE requests do we allow
1113 * to wait simultaneously before we start dropping
1115 int NewCircuitPeriod
; /**< How long do we use a circuit before building
1117 int MaxCircuitDirtiness
; /**< Never use circs that were first used more than
1118 this interval ago. */
1119 uint64_t BandwidthRate
; /**< How much bandwidth, on average, are we willing to
1120 * use in a second? */
1121 uint64_t BandwidthBurst
; /**< How much bandwidth, at maximum, are we willing to
1122 * use in a second? */
1123 uint64_t MaxAdvertisedBandwidth
; /**< How much bandwidth are we willing to
1124 * tell people we have? */
1125 int NumCpus
; /**< How many CPUs should we try to use? */
1126 int RunTesting
; /**< If true, create testing circuits to measure how well the
1127 * other ORs are running. */
1128 config_line_t
*RendConfigLines
; /**< List of configuration lines
1129 * for rendezvous services. */
1130 char *ContactInfo
; /**< Contact info to be published in the directory */
1132 char *HttpProxy
; /**< hostname[:port] to use as http proxy, if any */
1133 uint32_t HttpProxyAddr
; /**< Parsed IPv4 addr for http proxy, if any */
1134 uint16_t HttpProxyPort
; /**< Parsed port for http proxy, if any */
1135 char *HttpProxyAuthenticator
; /**< username:password string, if any */
1137 char *HttpsProxy
; /**< hostname[:port] to use as https proxy, if any */
1138 uint32_t HttpsProxyAddr
; /**< Parsed IPv4 addr for https proxy, if any */
1139 uint16_t HttpsProxyPort
; /**< Parsed port for https proxy, if any */
1140 char *HttpsProxyAuthenticator
; /**< username:password string, if any */
1142 config_line_t
*DirServers
; /**< List of configuration lines
1143 * for directory servers. */
1144 char *MyFamily
; /**< Declared family for this OR. */
1145 config_line_t
*NodeFamilies
; /**< List of config lines for
1147 config_line_t
*RedirectExit
; /**< List of config lines for simple
1148 * addr/port redirection */
1149 smartlist_t
*RedirectExitList
; /**< List of exit_redirect_t */
1150 int _MonthlyAccountingStart
; /**< Deprecated: day of month when accounting
1151 * interval starts */
1152 char *AccountingStart
; /**< How long is the accounting interval, and when
1154 uint64_t AccountingMax
; /**< How many bytes do we allow per accounting
1155 * interval before hibernation? 0 for "never
1157 int _AccountingMaxKB
; /**< How many KB do we allow per accounting
1158 * interval before hibernation? 0 for "never
1159 * hibernate." (Based on a deprecated option)*/
1161 char *HashedControlPassword
; /**< Base64-encoded hash of a password for
1162 * the control system. */
1163 int CookieAuthentication
; /**< Boolean: do we enable cookie-based auth for
1164 * the control system? */
1165 int LeaveStreamsUnattached
; /**< Boolean: Does Tor attach new streams to
1166 * circuits itself (0), or does it expect a controller
1168 int ShutdownWaitLength
; /**< When we get a SIGINT and we're a server, how
1169 * long do we wait before exiting? */
1170 int SafeLogging
; /**< Boolean: are we allowed to log sensitive strings
1171 * such as addresses (0), or do we scrub them first (1)? */
1172 int HardwareAccel
; /**< Boolean: Should we enable OpenSSL hardware
1173 * acceleration where available? */
1174 int UseHelperNodes
; /**< Boolean: Do we try to enter from a smallish number
1175 * of fixed nodes? */
1176 int NumHelperNodes
; /**< How many helper nodes do we try to establish? */
1177 int RephistTrackTime
; /**< How many seconds do we keep rephist info? */
1180 /** Persistent state for an onion router, as saved to disk. */
1185 /* XXXX These options aren't actually attached to anything yet. */
1187 time_t AccountingIntervalStart
;
1188 uint64_t AccountingBytesReadInInterval
;
1189 uint64_t AccountingBytesWrittenInInterval
;
1190 int AccountingSecondsActive
;
1191 uint64_t AccountingExpectedUsage
;
1193 config_line_t
*HelperNodes
;
1196 #define MAX_SOCKS_REPLY_LEN 1024
1197 #define MAX_SOCKS_ADDR_LEN 256
1198 #define SOCKS_COMMAND_CONNECT 0x01
1199 #define SOCKS_COMMAND_RESOLVE 0xF0
1200 /** State of a SOCKS request from a user to an OP */
1201 struct socks_request_t
{
1202 char socks_version
; /**< Which version of SOCKS did the client use? */
1203 int command
; /**< What has the user requested? One of CONNECT or RESOLVE. */
1204 size_t replylen
; /**< Length of <b>reply</b>. */
1205 char reply
[MAX_SOCKS_REPLY_LEN
]; /**< Write an entry into this string if
1206 * we want to specify our own socks reply,
1207 * rather than using the default socks4 or
1208 * socks5 socks reply. We use this for the
1209 * two-stage socks5 handshake.
1211 int has_finished
; /**< Has the SOCKS handshake finished? */
1212 char address
[MAX_SOCKS_ADDR_LEN
]; /**< What address did the client ask to connect to? */
1213 uint16_t port
; /**< What port did the client ask to connect to? */
1216 /* all the function prototypes go here */
1218 /********************************* buffers.c ***************************/
1220 buf_t
*buf_new(void);
1221 buf_t
*buf_new_with_capacity(size_t size
);
1222 void buf_free(buf_t
*buf
);
1223 void buf_clear(buf_t
*buf
);
1224 void buf_shrink(buf_t
*buf
);
1226 size_t buf_datalen(const buf_t
*buf
);
1227 size_t buf_capacity(const buf_t
*buf
);
1228 const char *_buf_peek_raw_buffer(const buf_t
*buf
);
1230 int read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
);
1231 int read_to_buf_tls(tor_tls
*tls
, size_t at_most
, buf_t
*buf
);
1233 int flush_buf(int s
, buf_t
*buf
, size_t *buf_flushlen
);
1234 int flush_buf_tls(tor_tls
*tls
, buf_t
*buf
, size_t *buf_flushlen
);
1236 int write_to_buf(const char *string
, size_t string_len
, buf_t
*buf
);
1237 int fetch_from_buf(char *string
, size_t string_len
, buf_t
*buf
);
1238 int fetch_from_buf_http(buf_t
*buf
,
1239 char **headers_out
, size_t max_headerlen
,
1240 char **body_out
, size_t *body_used
, size_t max_bodylen
);
1241 int fetch_from_buf_socks(buf_t
*buf
, socks_request_t
*req
);
1242 int fetch_from_buf_control0(buf_t
*buf
, uint32_t *len_out
, uint16_t *type_out
,
1243 char **body_out
, int check_for_v1
);
1244 int fetch_from_buf_line(buf_t
*buf
, char *data_out
, size_t *data_len
);
1246 void assert_buf_ok(buf_t
*buf
);
1248 /********************************* circuitbuild.c **********************/
1250 char *circuit_list_path(circuit_t
*circ
, int verbose
);
1251 void circuit_log_path(int severity
, circuit_t
*circ
);
1252 void circuit_rep_hist_note_result(circuit_t
*circ
);
1253 void circuit_dump_by_conn(connection_t
*conn
, int severity
);
1254 circuit_t
*circuit_init(uint8_t purpose
, int need_uptime
,
1255 int need_capacity
, int internal
);
1256 circuit_t
*circuit_establish_circuit(uint8_t purpose
, extend_info_t
*exit
,
1257 int need_uptime
, int need_capacity
, int internal
);
1258 int circuit_handle_first_hop(circuit_t
*circ
);
1259 void circuit_n_conn_done(connection_t
*or_conn
, int status
);
1260 int circuit_send_next_onion_skin(circuit_t
*circ
);
1261 void circuit_note_clock_jumped(int seconds_elapsed
);
1262 int circuit_extend(cell_t
*cell
, circuit_t
*circ
);
1263 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
, int reverse
);
1264 int circuit_finish_handshake(circuit_t
*circ
, uint8_t cell_type
, char *reply
);
1265 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
1266 int onionskin_answer(circuit_t
*circ
, uint8_t cell_type
, char *payload
, char *keys
);
1267 int circuit_all_predicted_ports_handled(time_t now
, int *need_uptime
,
1268 int *need_capacity
);
1270 int circuit_append_new_exit(circuit_t
*circ
, extend_info_t
*info
);
1271 int circuit_extend_to_new_exit(circuit_t
*circ
, extend_info_t
*info
);
1272 void onion_append_to_cpath(crypt_path_t
**head_ptr
, crypt_path_t
*new_hop
);
1273 extend_info_t
*extend_info_from_router(routerinfo_t
*r
);
1274 extend_info_t
*extend_info_dup(extend_info_t
*info
);
1275 void extend_info_free(extend_info_t
*info
);
1276 routerinfo_t
*build_state_get_exit_router(cpath_build_state_t
*state
);
1277 const char *build_state_get_exit_nickname(cpath_build_state_t
*state
);
1279 void helper_node_set_status(const char *digest
, int succeeded
);
1280 void helper_nodes_set_status_from_directory(void);
1281 int helper_nodes_update_state(or_state_t
*state
);
1282 int helper_nodes_parse_state(or_state_t
*state
, int set
, const char **err
);
1283 int helper_nodes_getinfo_helper(const char *question
, char **answer
);
1285 /********************************* circuitlist.c ***********************/
1287 circuit_t
* _circuit_get_global_list(void);
1288 const char *circuit_state_to_string(int state
);
1289 enum which_conn_changed_t
{ P_CONN_CHANGED
=1, N_CONN_CHANGED
=0 };
1290 void circuit_set_circid_orconn(circuit_t
*circ
, uint16_t id
,
1292 enum which_conn_changed_t which
);
1293 void circuit_close_all_marked(void);
1294 circuit_t
*circuit_new(uint16_t p_circ_id
, connection_t
*p_conn
);
1295 circuit_t
*circuit_get_by_circid_orconn(uint16_t circ_id
, connection_t
*conn
);
1296 circuit_t
*circuit_get_by_edge_conn(connection_t
*conn
);
1297 circuit_t
*circuit_get_by_conn(connection_t
*conn
);
1298 circuit_t
*circuit_get_by_global_id(uint32_t id
);
1299 circuit_t
*circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
);
1300 circuit_t
*circuit_get_next_by_pk_and_purpose(circuit_t
*start
,
1301 const char *digest
, uint8_t purpose
);
1302 circuit_t
*circuit_get_rendezvous(const char *cookie
);
1303 circuit_t
*circuit_get_clean_open(uint8_t purpose
, int need_uptime
,
1304 int need_capacity
, int internal
);
1305 void circuit_mark_all_unused_circs(void);
1306 void _circuit_mark_for_close(circuit_t
*circ
, int line
, const char *file
);
1308 #define circuit_mark_for_close(c) \
1309 _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
1311 void assert_cpath_layer_ok(const crypt_path_t
*cp
);
1312 void assert_circuit_ok(const circuit_t
*c
);
1313 void circuit_free_all(void);
1315 /********************************* circuituse.c ************************/
1317 void circuit_expire_building(time_t now
);
1318 void circuit_remove_handled_ports(smartlist_t
*needed_ports
);
1319 int circuit_stream_is_being_handled(connection_t
*conn
, uint16_t port
, int min
);
1320 void circuit_build_needed_circs(time_t now
);
1321 void circuit_detach_stream(circuit_t
*circ
, connection_t
*conn
);
1322 void circuit_about_to_close_connection(connection_t
*conn
);
1323 void circuit_has_opened(circuit_t
*circ
);
1324 void circuit_build_failed(circuit_t
*circ
);
1325 circuit_t
*circuit_launch_by_nickname(uint8_t purpose
, const char *exit_nickname
,
1326 int need_uptime
, int need_capacity
, int is_internal
);
1327 circuit_t
*circuit_launch_by_extend_info(uint8_t purpose
,
1328 extend_info_t
*info
,
1329 int need_uptime
, int need_capacity
, int is_internal
);
1330 circuit_t
*circuit_launch_by_router(uint8_t purpose
, routerinfo_t
*exit
,
1331 int need_uptime
, int need_capacity
, int is_internal
);
1332 void circuit_reset_failure_count(int timeout
);
1333 int connection_ap_handshake_attach_chosen_circuit(connection_t
*conn
,
1335 int connection_ap_handshake_attach_circuit(connection_t
*conn
);
1337 /********************************* command.c ***************************/
1339 void command_process_cell(cell_t
*cell
, connection_t
*conn
);
1341 extern unsigned long stats_n_padding_cells_processed
;
1342 extern unsigned long stats_n_create_cells_processed
;
1343 extern unsigned long stats_n_created_cells_processed
;
1344 extern unsigned long stats_n_relay_cells_processed
;
1345 extern unsigned long stats_n_destroy_cells_processed
;
1347 /********************************* config.c ***************************/
1349 or_options_t
*get_options(void);
1350 void set_options(or_options_t
*new_val
);
1351 int options_act(void);
1352 void config_free_all(void);
1353 const char *safe_str(const char *address
);
1355 int config_get_lines(char *string
, config_line_t
**result
);
1356 void config_free_lines(config_line_t
*front
);
1357 int options_trial_assign(config_line_t
*list
, int reset
);
1358 int resolve_my_address(or_options_t
*options
, uint32_t *addr
);
1359 void options_init(or_options_t
*options
);
1360 int options_init_from_torrc(int argc
, char **argv
);
1361 int options_init_logs(or_options_t
*options
, int validate_only
);
1362 int config_parse_addr_policy(config_line_t
*cfg
,
1363 addr_policy_t
**dest
);
1364 void options_append_default_exit_policy(addr_policy_t
**policy
);
1365 void addr_policy_free(addr_policy_t
*p
);
1366 int option_is_recognized(const char *key
);
1367 const char *option_get_canonical_name(const char *key
);
1368 config_line_t
*option_get_assignment(or_options_t
*options
,
1370 char *options_dump(or_options_t
*options
, int minimal
);
1371 int options_save_current(void);
1373 or_state_t
*get_or_state(void);
1374 int or_state_load(void);
1375 int or_state_save(void);
1377 int config_getinfo_helper(const char *question
, char **answer
);
1379 int fascist_firewall_allows_address(or_options_t
*options
, uint32_t addr
,
1382 /********************************* connection.c ***************************/
1384 const char *conn_type_to_string(int type
);
1385 const char *conn_state_to_string(int type
, int state
);
1387 connection_t
*connection_new(int type
);
1388 void connection_unregister(connection_t
*conn
);
1389 void connection_free(connection_t
*conn
);
1390 void connection_free_all(void);
1391 void connection_about_to_close_connection(connection_t
*conn
);
1392 void connection_close_immediate(connection_t
*conn
);
1393 void _connection_mark_for_close(connection_t
*conn
,int line
, const char *file
);
1395 #define connection_mark_for_close(c) \
1396 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1398 void connection_expire_held_open(void);
1400 int connection_connect(connection_t
*conn
, char *address
, uint32_t addr
, uint16_t port
);
1401 int retry_all_listeners(int force
);
1403 void connection_bucket_init(void);
1404 void connection_bucket_refill(struct timeval
*now
);
1406 int connection_handle_read(connection_t
*conn
);
1408 int connection_fetch_from_buf(char *string
, size_t len
, connection_t
*conn
);
1410 int connection_wants_to_flush(connection_t
*conn
);
1411 int connection_outbuf_too_full(connection_t
*conn
);
1412 int connection_handle_write(connection_t
*conn
);
1413 void _connection_controller_force_write(connection_t
*conn
);
1414 void connection_write_to_buf(const char *string
, size_t len
, connection_t
*conn
);
1416 connection_t
*connection_or_exact_get_by_addr_port(uint32_t addr
, uint16_t port
);
1417 connection_t
*connection_get_by_identity_digest(const char *digest
, int type
);
1418 connection_t
*connection_get_by_global_id(uint32_t id
);
1420 connection_t
*connection_get_by_type(int type
);
1421 connection_t
*connection_get_by_type_state(int type
, int state
);
1422 connection_t
*connection_get_by_type_state_lastwritten(int type
, int state
);
1423 connection_t
*connection_get_by_type_state_rendquery(int type
, int state
, const char *rendquery
);
1425 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1426 #define connection_has_pending_tls_data(conn) \
1427 ((conn)->type == CONN_TYPE_OR && \
1428 (conn)->state == OR_CONN_STATE_OPEN && \
1429 tor_tls_get_pending_bytes((conn)->tls))
1430 int connection_is_listener(connection_t
*conn
);
1431 int connection_state_is_open(connection_t
*conn
);
1432 int connection_state_is_connecting(connection_t
*conn
);
1434 int connection_send_destroy(uint16_t circ_id
, connection_t
*conn
);
1435 char *alloc_http_authenticator(const char *authenticator
);
1437 void assert_connection_ok(connection_t
*conn
, time_t now
);
1438 int connection_or_nonopen_was_started_here(connection_t
*conn
);
1440 /********************************* connection_edge.c ***************************/
1442 #define connection_mark_unattached_ap(conn, endreason) \
1443 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1445 void _connection_mark_unattached_ap(connection_t
*conn
, int endreason
,
1446 int line
, const char *file
);
1447 int connection_edge_reached_eof(connection_t
*conn
);
1448 int connection_edge_process_inbuf(connection_t
*conn
, int package_partial
);
1449 int connection_edge_destroy(uint16_t circ_id
, connection_t
*conn
);
1450 int connection_edge_end(connection_t
*conn
, char reason
, crypt_path_t
*cpath_layer
);
1451 int connection_edge_end_errno(connection_t
*conn
, crypt_path_t
*cpath_layer
);
1452 int connection_edge_finished_flushing(connection_t
*conn
);
1453 int connection_edge_finished_connecting(connection_t
*conn
);
1455 int connection_ap_handshake_send_begin(connection_t
*ap_conn
, circuit_t
*circ
);
1456 int connection_ap_handshake_send_resolve(connection_t
*ap_conn
, circuit_t
*circ
);
1458 int connection_ap_make_bridge(char *address
, uint16_t port
);
1459 void connection_ap_handshake_socks_reply(connection_t
*conn
, char *reply
,
1461 socks5_reply_status_t status
);
1462 void connection_ap_handshake_socks_resolved(connection_t
*conn
,
1465 const char *answer
);
1467 int connection_exit_begin_conn(cell_t
*cell
, circuit_t
*circ
);
1468 int connection_exit_begin_resolve(cell_t
*cell
, circuit_t
*circ
);
1469 void connection_exit_connect(connection_t
*conn
);
1470 int connection_edge_is_rendezvous_stream(connection_t
*conn
);
1471 int connection_ap_can_use_exit(connection_t
*conn
, routerinfo_t
*exit
);
1472 void connection_ap_expire_beginning(void);
1473 void connection_ap_attach_pending(void);
1474 int connection_ap_detach_retriable(connection_t
*conn
, circuit_t
*circ
);
1476 void addressmap_init(void);
1477 void addressmap_clean(time_t now
);
1478 void addressmap_clear_configured(void);
1479 void addressmap_clear_transient(void);
1480 void addressmap_free_all(void);
1481 void addressmap_rewrite(char *address
, size_t maxlen
);
1482 int addressmap_already_mapped(const char *address
);
1483 void addressmap_register(const char *address
, char *new_address
, time_t expires
);
1484 int client_dns_incr_failures(const char *address
);
1485 void client_dns_set_addressmap(const char *address
, uint32_t val
, const char *exitname
);
1486 const char *addressmap_register_virtual_address(int type
, char *new_address
);
1487 void addressmap_get_mappings(smartlist_t
*sl
, time_t min_expires
, time_t max_expires
);
1489 void parse_socks_policy(void);
1490 void free_socks_policy(void);
1491 int socks_policy_permits_address(uint32_t addr
);
1493 void set_exit_redirects(smartlist_t
*lst
);
1494 typedef enum hostname_type_t
{
1495 NORMAL_HOSTNAME
, ONION_HOSTNAME
, EXIT_HOSTNAME
, BAD_HOSTNAME
1497 hostname_type_t
parse_extended_hostname(char *address
);
1499 /********************************* connection_or.c ***************************/
1501 int connection_or_reached_eof(connection_t
*conn
);
1502 int connection_or_process_inbuf(connection_t
*conn
);
1503 int connection_or_finished_flushing(connection_t
*conn
);
1504 int connection_or_finished_connecting(connection_t
*conn
);
1506 connection_t
*connection_or_connect(uint32_t addr
, uint16_t port
,
1507 const char *id_digest
);
1509 int connection_tls_start_handshake(connection_t
*conn
, int receiving
);
1510 int connection_tls_continue_handshake(connection_t
*conn
);
1512 void connection_or_write_cell_to_buf(const cell_t
*cell
, connection_t
*conn
);
1513 void connection_or_update_nickname(connection_t
*conn
);
1515 /********************************* control.c ***************************/
1517 typedef enum circuit_status_event_t
{
1518 CIRC_EVENT_LAUNCHED
= 0,
1519 CIRC_EVENT_BUILT
= 1,
1520 CIRC_EVENT_EXTENDED
= 2,
1521 CIRC_EVENT_FAILED
= 3,
1522 CIRC_EVENT_CLOSED
= 4,
1523 } circuit_status_event_t
;
1525 typedef enum stream_status_event_t
{
1526 STREAM_EVENT_SENT_CONNECT
= 0,
1527 STREAM_EVENT_SENT_RESOLVE
= 1,
1528 STREAM_EVENT_SUCCEEDED
= 2,
1529 STREAM_EVENT_FAILED
= 3,
1530 STREAM_EVENT_CLOSED
= 4,
1531 STREAM_EVENT_NEW
= 5,
1532 STREAM_EVENT_NEW_RESOLVE
= 6,
1533 STREAM_EVENT_FAILED_RETRIABLE
= 7
1534 } stream_status_event_t
;
1536 typedef enum or_conn_status_event_t
{
1537 OR_CONN_EVENT_LAUNCHED
= 0,
1538 OR_CONN_EVENT_CONNECTED
= 1,
1539 OR_CONN_EVENT_FAILED
= 2,
1540 OR_CONN_EVENT_CLOSED
= 3,
1541 } or_conn_status_event_t
;
1543 void control_update_global_event_mask(void);
1544 void control_adjust_event_log_severity(void);
1545 void disable_control_logging(void);
1546 void enable_control_logging(void);
1548 #define CONN_LOG_PROTECT(conn, stmt) \
1550 int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
1551 if (_log_conn_is_control) \
1552 disable_control_logging(); \
1553 do {stmt;} while (0); \
1554 if (_log_conn_is_control) \
1555 enable_control_logging(); \
1558 #define LOG_FN_CONN(conn, args) \
1559 CONN_LOG_PROTECT(conn, log_fn args)
1561 int connection_control_finished_flushing(connection_t
*conn
);
1562 int connection_control_reached_eof(connection_t
*conn
);
1563 int connection_control_process_inbuf(connection_t
*conn
);
1565 int control_event_circuit_status(circuit_t
*circ
, circuit_status_event_t e
);
1566 int control_event_stream_status(connection_t
*conn
, stream_status_event_t e
);
1567 int control_event_or_conn_status(connection_t
*conn
, or_conn_status_event_t e
);
1568 int control_event_bandwidth_used(uint32_t n_read
, uint32_t n_written
);
1569 void control_event_logmsg(int severity
, const char *msg
);
1570 int control_event_descriptors_changed(smartlist_t
*routers
);
1571 int control_event_address_mapped(const char *from
, const char *to
,time_t expires
);
1573 int init_cookie_authentication(int enabled
);
1574 int decode_hashed_password(char *buf
, const char *hashed
);
1576 /********************************* cpuworker.c *****************************/
1578 void cpu_init(void);
1579 void cpuworkers_rotate(void);
1580 int connection_cpu_finished_flushing(connection_t
*conn
);
1581 int connection_cpu_reached_eof(connection_t
*conn
);
1582 int connection_cpu_process_inbuf(connection_t
*conn
);
1583 int assign_to_cpuworker(connection_t
*cpuworker
, uint8_t question_type
,
1586 /********************************* directory.c ***************************/
1588 int dir_policy_permits_address(uint32_t addr
);
1589 void directory_post_to_dirservers(uint8_t purpose
, const char *payload
,
1590 size_t payload_len
);
1591 void directory_get_from_dirserver(uint8_t purpose
, const char *resource
,
1592 int retry_if_no_servers
);
1593 void directory_initiate_command_router(routerinfo_t
*router
, uint8_t purpose
,
1594 int private_connection
, const char *resource
,
1595 const char *payload
, size_t payload_len
);
1597 int parse_http_response(const char *headers
, int *code
, time_t *date
,
1598 int *compression
, char **response
);
1600 int connection_dir_reached_eof(connection_t
*conn
);
1601 int connection_dir_process_inbuf(connection_t
*conn
);
1602 int connection_dir_finished_flushing(connection_t
*conn
);
1603 int connection_dir_finished_connecting(connection_t
*conn
);
1604 void connection_dir_connect_failed(connection_t
*conn
);
1605 void parse_dir_policy(void);
1606 void free_dir_policy(void);
1608 /********************************* dirserv.c ***************************/
1610 int dirserv_add_own_fingerprint(const char *nickname
, crypto_pk_env_t
*pk
);
1611 int dirserv_parse_fingerprint_file(const char *fname
);
1612 int dirserv_router_fingerprint_is_known(const routerinfo_t
*router
);
1613 void dirserv_free_fingerprint_list(void);
1614 const char *dirserv_get_nickname_by_digest(const char *digest
);
1615 int dirserv_add_descriptor(const char **desc
, const char **msg
);
1616 int dirserv_load_from_directory_string(const char *dir
);
1617 void dirserv_free_descriptors(void);
1618 int list_server_status(smartlist_t
*routers
, char **router_status_out
);
1619 void dirserv_remove_old_servers(int age
);
1620 int dirserv_dump_directory_to_string(char **dir_out
,
1621 crypto_pk_env_t
*private_key
);
1622 void directory_set_dirty(void);
1623 size_t dirserv_get_directory(const char **cp
, int compress
);
1624 size_t dirserv_get_runningrouters(const char **rr
, int compress
);
1625 void dirserv_set_cached_directory(const char *directory
, time_t when
,
1626 int is_running_routers
);
1627 void dirserv_orconn_tls_done(const char *address
,
1629 const char *digest_rcvd
,
1630 const char *nickname
,
1632 void dirserv_free_all(void);
1634 /********************************* dns.c ***************************/
1636 void dns_init(void);
1637 void dns_free_all(void);
1638 int connection_dns_finished_flushing(connection_t
*conn
);
1639 int connection_dns_reached_eof(connection_t
*conn
);
1640 int connection_dns_process_inbuf(connection_t
*conn
);
1641 void dnsworkers_rotate(void);
1642 void connection_dns_remove(connection_t
*conn
);
1643 void assert_connection_edge_not_dns_pending(connection_t
*conn
);
1644 void assert_all_pending_dns_resolves_ok(void);
1645 void dns_cancel_pending_resolve(char *question
);
1646 int dns_resolve(connection_t
*exitconn
);
1648 /********************************* hibernate.c **********************/
1650 int accounting_parse_options(or_options_t
*options
, int validate_only
);
1651 int accounting_is_enabled(or_options_t
*options
);
1652 void configure_accounting(time_t now
);
1653 void accounting_run_housekeeping(time_t now
);
1654 void accounting_add_bytes(size_t n_read
, size_t n_written
, int seconds
);
1655 int accounting_record_bandwidth_usage(time_t now
);
1656 void hibernate_begin_shutdown(void);
1657 int we_are_hibernating(void);
1658 void consider_hibernation(time_t now
);
1659 int accounting_getinfo_helper(const char *question
, char **answer
);
1660 void accounting_set_bandwidth_usage_from_state(or_state_t
*state
);
1662 /********************************* main.c ***************************/
1664 int connection_add(connection_t
*conn
);
1665 int connection_remove(connection_t
*conn
);
1666 int connection_in_array(connection_t
*conn
);
1667 void add_connection_to_closeable_list(connection_t
*conn
);
1668 int connection_is_on_closeable_list(connection_t
*conn
);
1670 void get_connection_array(connection_t
***array
, int *n
);
1672 void connection_watch_events(connection_t
*conn
, short events
);
1673 int connection_is_reading(connection_t
*conn
);
1674 void connection_stop_reading(connection_t
*conn
);
1675 void connection_start_reading(connection_t
*conn
);
1677 int connection_is_writing(connection_t
*conn
);
1678 void connection_stop_writing(connection_t
*conn
);
1679 void connection_start_writing(connection_t
*conn
);
1681 void directory_all_unreachable(time_t now
);
1682 void directory_has_arrived(time_t now
, char *identity_digest
);
1684 int control_signal_act(int the_signal
);
1685 void handle_signals(int is_parent
);
1686 void tor_cleanup(void);
1687 void tor_free_all(int postfork
);
1689 int tor_main(int argc
, char *argv
[]);
1691 /********************************* onion.c ***************************/
1693 int onion_pending_add(circuit_t
*circ
);
1694 circuit_t
*onion_next_task(void);
1695 void onion_pending_remove(circuit_t
*circ
);
1697 int onion_skin_create(crypto_pk_env_t
*router_key
,
1698 crypto_dh_env_t
**handshake_state_out
,
1699 char *onion_skin_out
);
1701 int onion_skin_server_handshake(const char *onion_skin
,
1702 crypto_pk_env_t
*private_key
,
1703 crypto_pk_env_t
*prev_private_key
,
1704 char *handshake_reply_out
,
1706 size_t key_out_len
);
1708 int onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
1709 const char *handshake_reply
,
1711 size_t key_out_len
);
1713 int fast_server_handshake(const char *key_in
,
1714 char *handshake_reply_out
,
1716 size_t key_out_len
);
1718 int fast_client_handshake(const char *handshake_state
,
1719 const char *handshake_reply_out
,
1721 size_t key_out_len
);
1723 void clear_pending_onions(void);
1725 /********************************* relay.c ***************************/
1727 extern unsigned long stats_n_relay_cells_relayed
;
1728 extern unsigned long stats_n_relay_cells_delivered
;
1730 int circuit_receive_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1731 int cell_direction
);
1733 void relay_header_pack(char *dest
, const relay_header_t
*src
);
1734 void relay_header_unpack(relay_header_t
*dest
, const char *src
);
1735 int connection_edge_send_command(connection_t
*fromconn
, circuit_t
*circ
,
1736 int relay_command
, const char *payload
,
1737 size_t payload_len
, crypt_path_t
*cpath_layer
);
1738 int connection_edge_package_raw_inbuf(connection_t
*conn
, int package_partial
);
1739 void connection_edge_consider_sending_sendme(connection_t
*conn
);
1740 socks5_reply_status_t
connection_edge_end_reason_socks5_response(int reason
);
1741 int errno_to_end_reason(int e
);
1743 extern uint64_t stats_n_data_cells_packaged
;
1744 extern uint64_t stats_n_data_bytes_packaged
;
1745 extern uint64_t stats_n_data_cells_received
;
1746 extern uint64_t stats_n_data_bytes_received
;
1748 /********************************* rephist.c ***************************/
1750 void rep_hist_init(void);
1751 void rep_hist_note_connect_failed(const char* nickname
, time_t when
);
1752 void rep_hist_note_connect_succeeded(const char* nickname
, time_t when
);
1753 void rep_hist_note_disconnect(const char* nickname
, time_t when
);
1754 void rep_hist_note_connection_died(const char* nickname
, time_t when
);
1755 void rep_hist_note_extend_succeeded(const char *from_name
,
1756 const char *to_name
);
1757 void rep_hist_note_extend_failed(const char *from_name
, const char *to_name
);
1758 void rep_hist_dump_stats(time_t now
, int severity
);
1759 void rep_hist_note_bytes_read(int num_bytes
, time_t when
);
1760 void rep_hist_note_bytes_written(int num_bytes
, time_t when
);
1761 int rep_hist_bandwidth_assess(void);
1762 char *rep_hist_get_bandwidth_lines(void);
1763 void rep_history_clean(time_t before
);
1765 void rep_hist_note_used_port(uint16_t port
, time_t now
);
1766 smartlist_t
*rep_hist_get_predicted_ports(time_t now
);
1767 void rep_hist_note_used_hidserv(time_t now
, int need_uptime
, int need_capacity
);
1768 int rep_hist_get_predicted_hidserv(time_t now
, int *need_uptime
, int *need_capacity
);
1769 void rep_hist_note_used_resolve(time_t now
);
1770 int rep_hist_get_predicted_resolve(time_t now
);
1772 void rep_hist_free_all(void);
1774 /********************************* rendclient.c ***************************/
1776 void rend_client_introcirc_has_opened(circuit_t
*circ
);
1777 void rend_client_rendcirc_has_opened(circuit_t
*circ
);
1778 int rend_client_introduction_acked(circuit_t
*circ
, const char *request
, size_t request_len
);
1779 void rend_client_refetch_renddesc(const char *query
);
1780 int rend_client_remove_intro_point(extend_info_t
*failed_intro
, const char *query
);
1781 int rend_client_rendezvous_acked(circuit_t
*circ
, const char *request
, size_t request_len
);
1782 int rend_client_receive_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1783 void rend_client_desc_here(const char *query
);
1785 extend_info_t
*rend_client_get_random_intro(const char *query
);
1787 int rend_client_send_introduction(circuit_t
*introcirc
, circuit_t
*rendcirc
);
1789 /********************************* rendcommon.c ***************************/
1791 /** Information used to connect to a hidden service. */
1792 typedef struct rend_service_descriptor_t
{
1793 crypto_pk_env_t
*pk
; /**< This service's public key. */
1794 int version
; /**< 0 or 1. */
1795 time_t timestamp
; /**< Time when the descriptor was generated. */
1796 uint16_t protocols
; /**< Bitmask: which rendezvous protocols are supported?
1797 * (We allow bits '0', '1', and '2' to be set.) */
1798 int n_intro_points
; /**< Number of introduction points. */
1799 /** Array of n_intro_points elements for this service's introduction points'
1800 * nicknames. Elements are removed from this array if introduction attempts
1802 char **intro_points
;
1803 /** Array of n_intro_points elements for this service's introduction points'
1804 * extend_infos, or NULL if this descriptor is V0. Elements are removed
1805 * from this array if introduction attempts fail. If this array is present,
1806 * its elements correspond to the elements of intro_points. */
1807 extend_info_t
**intro_point_extend_info
;
1808 } rend_service_descriptor_t
;
1810 int rend_cmp_service_ids(const char *one
, const char *two
);
1812 void rend_process_relay_cell(circuit_t
*circ
, int command
, size_t length
,
1813 const char *payload
);
1815 void rend_service_descriptor_free(rend_service_descriptor_t
*desc
);
1816 int rend_encode_service_descriptor(rend_service_descriptor_t
*desc
,
1818 crypto_pk_env_t
*key
,
1821 rend_service_descriptor_t
*rend_parse_service_descriptor(const char *str
, size_t len
);
1822 int rend_get_service_id(crypto_pk_env_t
*pk
, char *out
);
1824 typedef struct rend_cache_entry_t
{
1825 size_t len
; /* Length of desc */
1826 time_t received
; /* When was the descriptor received? */
1827 char *desc
; /* Service descriptor */
1828 rend_service_descriptor_t
*parsed
; /* Parsed value of 'desc' */
1829 } rend_cache_entry_t
;
1831 void rend_cache_init(void);
1832 void rend_cache_clean(void);
1833 void rend_cache_free_all(void);
1834 int rend_valid_service_id(const char *query
);
1835 int rend_cache_lookup_desc(const char *query
, int version
, const char **desc
, size_t *desc_len
);
1836 int rend_cache_lookup_entry(const char *query
, int version
, rend_cache_entry_t
**entry_out
);
1837 int rend_cache_store(const char *desc
, size_t desc_len
);
1839 /********************************* rendservice.c ***************************/
1841 int rend_config_services(or_options_t
*options
, int validate_only
);
1842 int rend_service_load_keys(void);
1843 void rend_services_init(void);
1844 void rend_services_introduce(void);
1845 void rend_consider_services_upload(time_t now
);
1847 void rend_service_intro_has_opened(circuit_t
*circuit
);
1848 int rend_service_intro_established(circuit_t
*circuit
, const char *request
, size_t request_len
);
1849 void rend_service_rendezvous_has_opened(circuit_t
*circuit
);
1850 int rend_service_introduce(circuit_t
*circuit
, const char *request
, size_t request_len
);
1851 void rend_service_relaunch_rendezvous(circuit_t
*oldcirc
);
1852 int rend_service_set_connection_addr_port(connection_t
*conn
, circuit_t
*circ
);
1853 void rend_service_dump_stats(int severity
);
1854 void rend_service_free_all(void);
1856 /********************************* rendmid.c *******************************/
1857 int rend_mid_establish_intro(circuit_t
*circ
, const char *request
, size_t request_len
);
1858 int rend_mid_introduce(circuit_t
*circ
, const char *request
, size_t request_len
);
1859 int rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1860 int rend_mid_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1862 /********************************* router.c ***************************/
1864 ADDR_POLICY_ACCEPTED
=0,
1865 ADDR_POLICY_REJECTED
=-1,
1866 ADDR_POLICY_PROBABLY_ACCEPTED
=1,
1867 ADDR_POLICY_PROBABLY_REJECTED
=2
1868 } addr_policy_result_t
;
1870 void set_onion_key(crypto_pk_env_t
*k
);
1871 crypto_pk_env_t
*get_onion_key(void);
1872 crypto_pk_env_t
*get_previous_onion_key(void);
1873 time_t get_onion_key_set_at(void);
1874 void set_identity_key(crypto_pk_env_t
*k
);
1875 crypto_pk_env_t
*get_identity_key(void);
1876 int identity_key_is_set(void);
1877 void dup_onion_keys(crypto_pk_env_t
**key
, crypto_pk_env_t
**last
);
1878 void rotate_onion_key(void);
1879 crypto_pk_env_t
*init_key_from_file(const char *fname
);
1880 int init_keys(void);
1882 int check_whether_orport_reachable(void);
1883 int check_whether_dirport_reachable(void);
1884 void consider_testing_reachability(void);
1885 void router_orport_found_reachable(void);
1886 void router_dirport_found_reachable(void);
1887 void server_has_changed_ip(void);
1888 void consider_publishable_server(time_t now
, int force
);
1890 int authdir_mode(or_options_t
*options
);
1891 int clique_mode(or_options_t
*options
);
1892 int server_mode(or_options_t
*options
);
1893 int advertised_server_mode(void);
1894 int proxy_mode(or_options_t
*options
);
1896 void router_retry_connections(void);
1897 int router_is_clique_mode(routerinfo_t
*router
);
1898 void router_upload_dir_desc_to_dirservers(int force
);
1899 void mark_my_descriptor_dirty(void);
1900 int router_compare_to_my_exit_policy(connection_t
*conn
);
1901 routerinfo_t
*router_get_my_routerinfo(void);
1902 const char *router_get_my_descriptor(void);
1903 int router_digest_is_me(const char *digest
);
1904 int router_is_me(routerinfo_t
*router
);
1905 int router_rebuild_descriptor(int force
);
1906 int router_dump_router_to_string(char *s
, size_t maxlen
, routerinfo_t
*router
,
1907 crypto_pk_env_t
*ident_key
);
1908 int is_legal_nickname(const char *s
);
1909 int is_legal_nickname_or_hexdigest(const char *s
);
1910 void router_free_all_keys(void);
1912 /********************************* routerlist.c ***************************/
1914 typedef struct trusted_dir_server_t
{
1918 char digest
[DIGEST_LEN
];
1920 } trusted_dir_server_t
;
1922 int router_reload_router_list(void);
1923 void router_get_trusted_dir_servers(smartlist_t
**outp
);
1924 routerinfo_t
*router_pick_directory_server(int requireother
,
1925 int fascistfirewall
,
1926 int for_running_routers
,
1927 int retry_if_no_servers
);
1928 trusted_dir_server_t
*router_pick_trusteddirserver(int requireother
,
1929 int fascistfirewall
,
1930 int retry_if_no_servers
);
1931 int all_trusted_directory_servers_down(void);
1933 void routerlist_add_family(struct smartlist_t
*sl
, routerinfo_t
*router
);
1934 void add_nickname_list_to_smartlist(struct smartlist_t
*sl
, const char *list
, int warn_if_down
);
1935 routerinfo_t
*routerlist_find_my_routerinfo(void);
1936 int exit_policy_implicitly_allows_local_networks(addr_policy_t
*policy
,
1939 /** How many seconds a router must be up before we'll use it for
1940 * reliability-critical node positions.
1942 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
1943 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
1945 int router_is_unreliable(routerinfo_t
*router
, int need_uptime
, int need_capacity
);
1946 routerinfo_t
*routerlist_sl_choose_by_bandwidth(smartlist_t
*sl
);
1947 routerinfo_t
*router_choose_random_node(const char *preferred
,
1948 const char *excluded
,
1949 struct smartlist_t
*excludedsmartlist
,
1950 int need_uptime
, int need_bandwidth
,
1951 int allow_unverified
, int strict
);
1952 routerinfo_t
*router_get_by_nickname(const char *nickname
);
1953 routerinfo_t
*router_get_by_hexdigest(const char *hexdigest
);
1954 routerinfo_t
*router_get_by_digest(const char *digest
);
1955 int router_digest_is_trusted_dir(const char *digest
);
1956 void router_get_routerlist(routerlist_t
**prouterlist
);
1957 time_t routerlist_get_published_time(void);
1958 void routerlist_free(routerlist_t
*routerlist
);
1959 void routerinfo_free(routerinfo_t
*router
);
1960 void routerlist_free_current(void);
1961 void free_trusted_dir_servers(void);
1962 routerinfo_t
*routerinfo_copy(const routerinfo_t
*router
);
1963 void router_mark_as_down(const char *digest
);
1964 void routerlist_remove_old_routers(int age
);
1965 int router_load_single_router(const char *s
, const char **msg
);
1966 int router_load_routerlist_from_directory(const char *s
,crypto_pk_env_t
*pkey
,
1967 int dir_is_recent
, int dir_is_cached
);
1968 addr_policy_result_t
router_compare_addr_to_addr_policy(uint32_t addr
,
1969 uint16_t port
, addr_policy_t
*policy
);
1971 int router_exit_policy_all_routers_reject(uint32_t addr
, uint16_t port
,
1974 int router_exit_policy_rejects_all(routerinfo_t
*router
);
1975 void running_routers_free(running_routers_t
*rr
);
1976 void routerlist_set_runningrouters(routerlist_t
*list
, running_routers_t
*rr
);
1977 int routers_update_status_from_entry(smartlist_t
*routers
,
1980 int router_update_status_from_smartlist(routerinfo_t
*r
,
1982 smartlist_t
*running_list
);
1983 void add_trusted_dir_server(const char *addr
, uint16_t port
,const char *digest
);
1984 void clear_trusted_dir_servers(void);
1986 /********************************* routerparse.c ************************/
1988 #define MAX_STATUS_TAG_LEN 32
1989 /** Structure to hold parsed Tor versions. This is a little messier
1990 * than we would like it to be, because we changed version schemes with 0.1.0.
1992 * See version-spec.txt for the whole business.
1994 typedef struct tor_version_t
{
1998 /** Release status. For version in the post-0.1 format, this is always
2000 enum { VER_PRE
=0, VER_RC
=1, VER_RELEASE
=2, } status
;
2002 /** CVS status. For version in the post-0.1 format, this is always
2004 enum { IS_CVS
=0, IS_NOT_CVS
=1} cvs
;
2005 char status_tag
[MAX_STATUS_TAG_LEN
];
2008 int router_get_router_hash(const char *s
, char *digest
);
2009 int router_get_dir_hash(const char *s
, char *digest
);
2010 int router_get_runningrouters_hash(const char *s
, char *digest
);
2011 int router_parse_list_from_string(const char **s
,
2012 routerlist_t
**dest
,
2013 smartlist_t
*good_nickname_list
,
2016 int router_parse_routerlist_from_directory(const char *s
,
2017 routerlist_t
**dest
,
2018 crypto_pk_env_t
*pkey
,
2020 int write_to_cache
);
2021 running_routers_t
*router_parse_runningrouters(const char *str
,
2022 int write_to_cache
);
2023 routerinfo_t
*router_parse_entry_from_string(const char *s
, const char *end
);
2024 int router_add_exit_policy_from_string(routerinfo_t
*router
, const char *s
);
2025 addr_policy_t
*router_parse_addr_policy_from_string(const char *s
);
2026 int check_software_version_against_directory(const char *directory
);
2027 int tor_version_parse(const char *s
, tor_version_t
*out
);
2028 int tor_version_as_new_as(const char *platform
, const char *cutoff
);
2029 int tor_version_compare(tor_version_t
*a
, tor_version_t
*b
);
2030 void assert_addr_policy_ok(addr_policy_t
*t
);