1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
8 * \brief Master header file for Tor-specific functionality.
16 #define WIN32_WINNT 0x400
17 #define _WIN32_WINNT 0x400
18 #define WIN32_LEAN_AND_MEAN
39 #include "../common/torint.h"
40 #include "../common/fakepoll.h"
41 #ifdef HAVE_INTTYPES_H
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
47 #ifdef HAVE_SYS_LIMITS_H
48 #include <sys/limits.h>
50 #ifdef HAVE_MACHINE_LIMITS_H
52 /* FreeBSD has a bug where it complains that this file is obsolete,
53 and I should migrate to using sys/limits. It complains even when
55 #include <machine/limits.h>
58 #ifdef HAVE_SYS_TYPES_H
59 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
61 #ifdef HAVE_SYS_WAIT_H
64 #ifdef HAVE_SYS_FCNTL_H
65 #include <sys/fcntl.h>
70 #ifdef HAVE_SYS_IOCTL_H
71 #include <sys/ioctl.h>
73 #ifdef HAVE_SYS_SOCKET_H
74 #include <sys/socket.h>
76 #ifdef HAVE_SYS_TIME_H
79 #ifdef HAVE_SYS_STAT_H
82 #ifdef HAVE_NETINET_IN_H
83 #include <netinet/in.h>
85 #ifdef HAVE_ARPA_INET_H
86 #include <arpa/inet.h>
101 #include <winsock2.h>
102 #include <ws2tcpip.h>
103 #elif defined(_MSC_VER)
112 #define snprintf _snprintf
115 #include "../common/crypto.h"
116 #include "../common/tortls.h"
117 #include "../common/log.h"
118 #include "../common/util.h"
120 /** Upper bound on maximum simultaneous connections; can be lowered by
122 #define MAXCONNECTIONS 10000
124 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
125 #define MAX_NICKNAME_LEN 19
126 /* Hex digest plus dollar sign. */
127 #define MAX_HEX_NICKNAME_LEN HEX_DIGEST_LEN+1
128 #define MAX_DIR_SIZE 500000
131 /** How long do we keep DNS cache entries before purging them? */
132 #define MAX_DNS_ENTRY_AGE (150*60)
134 #define MAX_DNS_ENTRY_AGE (15*60)
137 /** How often do we rotate onion keys? */
138 #define MIN_ONION_KEY_LIFETIME (12*60*60)
139 /** How often do we rotate TLS contexts? */
140 #define MAX_SSL_KEY_LIFETIME (120*60)
142 /** How old do we allow a router to get before removing it, either
143 * from the descriptor list (for dirservers) or the router list (for others)?
145 #define ROUTER_MAX_AGE (60*60*24)
147 #define CIRC_ID_TYPE_LOWER 0
148 #define CIRC_ID_TYPE_HIGHER 1
150 #define _CONN_TYPE_MIN 3
151 /** Type for sockets listening for OR connections. */
152 #define CONN_TYPE_OR_LISTENER 3
153 /** Type for OR-to-OR or OP-to-OR connections. */
154 #define CONN_TYPE_OR 4
155 /** Type for connections from final OR to chosen destination. */
156 #define CONN_TYPE_EXIT 5
157 /** Type for sockets listening for SOCKS connections. */
158 #define CONN_TYPE_AP_LISTENER 6
159 /** Type for SOCKS connections to OP. */
160 #define CONN_TYPE_AP 7
161 /** Type for sockets listening for HTTP connections to the directory server. */
162 #define CONN_TYPE_DIR_LISTENER 8
163 /** Type for HTTP connections to the directory server. */
164 #define CONN_TYPE_DIR 9
165 /** Type for connections to local dnsworker processes. */
166 #define CONN_TYPE_DNSWORKER 10
167 /** Type for connections to local cpuworker processes. */
168 #define CONN_TYPE_CPUWORKER 11
169 #define _CONN_TYPE_MAX 11
171 /** State for any listener connection. */
172 #define LISTENER_STATE_READY 0
174 #define _DNSWORKER_STATE_MIN 1
175 /** State for a connection to a dnsworker process that's idle. */
176 #define DNSWORKER_STATE_IDLE 1
177 /** State for a connection to a dnsworker process that's resolving a hostname. */
178 #define DNSWORKER_STATE_BUSY 2
179 #define _DNSWORKER_STATE_MAX 2
181 #define _CPUWORKER_STATE_MIN 1
182 /** State for a connection to a cpuworker process that's idle. */
183 #define CPUWORKER_STATE_IDLE 1
184 /** State for a connection to a cpuworker process that's processing a
186 #define CPUWORKER_STATE_BUSY_ONION 2
187 #define _CPUWORKER_STATE_MAX 2
189 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
191 #define _OR_CONN_STATE_MIN 1
192 /** State for a connection to an OR: waiting for connect() to finish. */
193 #define OR_CONN_STATE_CONNECTING 1
194 /** State for a connection to an OR: SSL is handshaking, not done yet. */
195 #define OR_CONN_STATE_HANDSHAKING 2
196 /** State for a connection to an OR: Ready to send/receive cells. */
197 #define OR_CONN_STATE_OPEN 3
198 #define _OR_CONN_STATE_MAX 3
200 #define _EXIT_CONN_STATE_MIN 1
201 /** State for an exit connection: waiting for response from dns farm. */
202 #define EXIT_CONN_STATE_RESOLVING 1
203 /** State for an exit connection: waiting for connect() to finish. */
204 #define EXIT_CONN_STATE_CONNECTING 2
205 /** State for an exit connection: open and ready to transmit data. */
206 #define EXIT_CONN_STATE_OPEN 3
207 /** State for an exit connection: waiting to be removed. */
208 #define EXIT_CONN_STATE_RESOLVEFAILED 4
209 #define _EXIT_CONN_STATE_MAX 4
211 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
212 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
215 /* the AP state values must be disjoint from the EXIT state values */
216 #define _AP_CONN_STATE_MIN 5
217 /** State for a SOCKS connection: waiting for SOCKS request. */
218 #define AP_CONN_STATE_SOCKS_WAIT 5
219 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
220 * rendezvous rescriptor. */
221 #define AP_CONN_STATE_RENDDESC_WAIT 6
222 /** State for a SOCKS connection: waiting for a completed circuit. */
223 #define AP_CONN_STATE_CIRCUIT_WAIT 7
224 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
225 #define AP_CONN_STATE_CONNECT_WAIT 8
226 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
227 #define AP_CONN_STATE_RESOLVE_WAIT 9
228 /** State for a SOCKS connection: ready to send and receive. */
229 #define AP_CONN_STATE_OPEN 10
230 #define _AP_CONN_STATE_MAX 10
232 #define _DIR_CONN_STATE_MIN 1
233 /** State for connection to directory server: waiting for connect(). */
234 #define DIR_CONN_STATE_CONNECTING 1
235 /** State for connection to directory server: sending HTTP request. */
236 #define DIR_CONN_STATE_CLIENT_SENDING 2
237 /** State for connection to directory server: reading HTTP response. */
238 #define DIR_CONN_STATE_CLIENT_READING 3
239 /** State for connection at directory server: waiting for HTTP request. */
240 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
241 /** State for connection at directory server: sending HTTP response. */
242 #define DIR_CONN_STATE_SERVER_WRITING 5
243 #define _DIR_CONN_STATE_MAX 5
245 #define _DIR_PURPOSE_MIN 1
246 /** Purpose for connection to directory server: download a directory. */
247 #define DIR_PURPOSE_FETCH_DIR 1
248 /** Purpose for connection to directory server: download just the list
249 * of running routers. */
250 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
251 /** Purpose for connection to directory server: download a rendezvous
253 #define DIR_PURPOSE_FETCH_RENDDESC 3
254 /** Purpose for connection to directory server: set after a rendezvous
255 * descriptor is downloaded. */
256 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
257 /** Purpose for connection to directory server: upload a server descriptor. */
258 #define DIR_PURPOSE_UPLOAD_DIR 5
259 /** Purpose for connection to directory server: upload a rendezvous
261 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
262 /** Purpose for connection at a directory server. */
263 #define DIR_PURPOSE_SERVER 7
264 #define _DIR_PURPOSE_MAX 7
266 #define _EXIT_PURPOSE_MIN 1
267 #define EXIT_PURPOSE_CONNECT 1
268 #define EXIT_PURPOSE_RESOLVE 2
269 #define _EXIT_PURPOSE_MAX 2
271 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
272 #define CIRCUIT_STATE_BUILDING 0
273 /** Circuit state: Waiting to process the onionskin. */
274 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
275 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
276 #define CIRCUIT_STATE_OR_WAIT 2
277 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
278 #define CIRCUIT_STATE_OPEN 3
280 #define _CIRCUIT_PURPOSE_MIN 1
282 /* these circuits were initiated elsewhere */
283 #define _CIRCUIT_PURPOSE_OR_MIN 1
284 /** OR-side circuit purpose: normal circuit, at OR. */
285 #define CIRCUIT_PURPOSE_OR 1
286 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
287 #define CIRCUIT_PURPOSE_INTRO_POINT 2
288 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
289 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
290 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
291 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
292 #define _CIRCUIT_PURPOSE_OR_MAX 4
294 /* these circuits originate at this node */
296 /* here's how circ client-side purposes work:
297 * normal circuits are C_GENERAL.
298 * circuits that are c_introducing are either on their way to
299 * becoming open, or they are open and waiting for a
300 * suitable rendcirc before they send the intro.
301 * circuits that are c_introduce_ack_wait have sent the intro,
302 * but haven't gotten a response yet.
303 * circuits that are c_establish_rend are either on their way
304 * to becoming open, or they are open and have sent the
305 * establish_rendezvous cell but haven't received an ack.
306 * circuits that are c_rend_ready are open and have received a
307 * rend ack, but haven't heard from bob yet. if they have a
308 * buildstate->pending_final_cpath then they're expecting a
309 * cell from bob, else they're not.
310 * circuits that are c_rend_ready_intro_acked are open, and
311 * some intro circ has sent its intro and received an ack.
312 * circuits that are c_rend_joined are open, have heard from
313 * bob, and are talking to him.
315 /** Client-side circuit purpose: Normal circuit, with cpath. */
316 #define CIRCUIT_PURPOSE_C_GENERAL 5
317 /** Client-side circuit purpose: at Alice, connecting to intro point. */
318 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
319 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
320 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
321 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
322 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
323 /** Client-side circuit purpose: at Alice, waiting for ack. */
324 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
325 /** Client-side circuit purpose: at Alice, waiting for Bob. */
326 #define CIRCUIT_PURPOSE_C_REND_READY 10
327 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
328 * has been acknowledged. */
329 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
330 /** Client-side circuit purpose: at Alice, rendezvous established. */
331 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
333 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
334 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
335 /** Hidden-service-side circuit purpose: at Bob, successfully established
337 #define CIRCUIT_PURPOSE_S_INTRO 14
338 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
339 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
340 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
341 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
342 #define _CIRCUIT_PURPOSE_MAX 16
344 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
345 * that this OP has originated. */
346 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
347 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
349 #define RELAY_COMMAND_BEGIN 1
350 #define RELAY_COMMAND_DATA 2
351 #define RELAY_COMMAND_END 3
352 #define RELAY_COMMAND_CONNECTED 4
353 #define RELAY_COMMAND_SENDME 5
354 #define RELAY_COMMAND_EXTEND 6
355 #define RELAY_COMMAND_EXTENDED 7
356 #define RELAY_COMMAND_TRUNCATE 8
357 #define RELAY_COMMAND_TRUNCATED 9
358 #define RELAY_COMMAND_DROP 10
359 #define RELAY_COMMAND_RESOLVE 11
360 #define RELAY_COMMAND_RESOLVED 12
362 #define RELAY_COMMAND_ESTABLISH_INTRO 32
363 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
364 #define RELAY_COMMAND_INTRODUCE1 34
365 #define RELAY_COMMAND_INTRODUCE2 35
366 #define RELAY_COMMAND_RENDEZVOUS1 36
367 #define RELAY_COMMAND_RENDEZVOUS2 37
368 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
369 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
370 #define RELAY_COMMAND_INTRODUCE_ACK 40
372 #define _MIN_END_STREAM_REASON 1
373 #define END_STREAM_REASON_MISC 1
374 #define END_STREAM_REASON_RESOLVEFAILED 2
375 #define END_STREAM_REASON_CONNECTFAILED 3
376 #define END_STREAM_REASON_EXITPOLICY 4
377 #define END_STREAM_REASON_DESTROY 5
378 #define END_STREAM_REASON_DONE 6
379 #define END_STREAM_REASON_TIMEOUT 7
380 #define _MAX_END_STREAM_REASON 7
382 #define RESOLVED_TYPE_IPV4 4
383 #define RESOLVED_TYPE_IPV6 6
384 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
385 #define RESOLVED_TYPE_ERROR 0xF1
387 /** Length of 'y' portion of 'y.onion' URL. */
388 #define REND_SERVICE_ID_LEN 16
390 #define CELL_DIRECTION_IN 1
391 #define CELL_DIRECTION_OUT 2
394 #define CIRCWINDOW_START 10000
395 #define CIRCWINDOW_INCREMENT 1000
396 #define STREAMWINDOW_START 5000
397 #define STREAMWINDOW_INCREMENT 500
399 #define CIRCWINDOW_START 1000
400 #define CIRCWINDOW_INCREMENT 100
401 #define STREAMWINDOW_START 500
402 #define STREAMWINDOW_INCREMENT 50
406 #define CELL_PADDING 0
407 #define CELL_CREATE 1
408 #define CELL_CREATED 2
410 #define CELL_DESTROY 4
412 /* people behind fascist firewalls use only these ports */
413 #define REQUIRED_FIREWALL_DIRPORT 80
414 #define REQUIRED_FIREWALL_ORPORT 443
416 /* legal characters in a nickname */
417 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
419 #define SOCKS4_NETWORK_LEN 8
423 * Relay command [1 byte]
424 * Recognized [2 bytes]
425 * Stream ID [2 bytes]
426 * Partial SHA-1 [4 bytes]
428 * Relay payload [498 bytes]
431 #define CELL_PAYLOAD_SIZE 509
432 #define CELL_NETWORK_SIZE 512
434 #define RELAY_HEADER_SIZE (1+2+2+4+2)
435 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
437 /** Parsed onion routing cell. All communication from OP-to-OR, or from
438 * OR-to-OR, is via cells. */
440 uint16_t circ_id
; /**< Circuit which received the cell. */
441 unsigned char command
; /**< Type of the cell: one of PADDING, CREATE, RELAY,
443 unsigned char payload
[CELL_PAYLOAD_SIZE
]; /**< Cell body. */
446 /** Beginning of a RELAY cell payload. */
448 uint8_t command
; /**< The end-to-end relay command. */
449 uint16_t recognized
; /**< Used to tell whether cell is for us. */
450 uint16_t stream_id
; /**< Which stream is this cell associated with? */
451 char integrity
[4]; /**< Used to tell whether cell is corrupted. */
452 uint16_t length
; /**< How long is the payload body? */
455 typedef struct buf_t buf_t
;
456 typedef struct socks_request_t socks_request_t
;
458 #define CONNECTION_MAGIC 0x7C3C304Eu
459 /** Description of a connection to another host or process, and associated
461 struct connection_t
{
462 uint32_t magic
; /**< For memory debugging: must equal CONNECTION_MAGIC. */
464 uint8_t type
; /**< What kind of connection is this? */
465 uint8_t state
; /**< Current state of this connection. */
466 uint8_t purpose
; /**< Only used for DIR types currently. */
467 uint8_t wants_to_read
; /**< Boolean: should we start reading again once
468 * the bandwidth throttler allows it?
470 uint8_t wants_to_write
; /**< Boolean: should we start writing again once
471 * the bandwidth throttler allows reads?
473 int s
; /**< Our socket; -1 if this connection is closed. */
474 int poll_index
; /**< Index of this conn into the poll_array. */
475 int marked_for_close
; /**< Boolean: should we close this conn on the next
476 * iteration of the main loop?
478 char *marked_for_close_file
; /**< For debugging: in which file were we marked
480 int hold_open_until_flushed
; /**< Despite this connection's being marked
481 * for close, do we flush it before closing it?
484 buf_t
*inbuf
; /**< Buffer holding data read over this connection. */
485 int inbuf_reached_eof
; /**< Boolean: did read() return 0 on this conn? */
486 time_t timestamp_lastread
; /**< When was the last time poll() said we could read? */
488 buf_t
*outbuf
; /**< Buffer holding data to write over this connection. */
489 int outbuf_flushlen
; /**< How much data should we try to flush from the
491 time_t timestamp_lastwritten
; /**< When was the last time poll() said we could write? */
493 time_t timestamp_created
; /**< When was this connection_t created? */
495 uint32_t addr
; /**< IP of the other side of the connection; used to identify
496 * routers, along with port. */
497 uint16_t port
; /**< If non-zero, port on the other end
498 * of the connection. */
499 char *address
; /**< FQDN (or IP) of the guy on the other end.
500 * strdup into this, because free_connection frees it.
502 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for the other side's
504 char identity_digest
[DIGEST_LEN
]; /**< Hash of identity_pkey */
505 char *nickname
; /**< Nickname of OR on other side (if any). */
507 /* Used only by OR connections: */
508 tor_tls
*tls
; /**< TLS connection state (OR only.) */
509 uint16_t next_circ_id
; /**< Which circ_id do we try to use next on
510 * this connection? This is always in the
511 * range 0..1<<15-1. (OR only.)*/
513 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
514 int bandwidth
; /**< Connection bandwidth. (OPEN ORs only.) */
515 int receiver_bucket
; /**< When this hits 0, stop receiving. Every second we
516 * add 'bandwidth' to this, capping it at 10*bandwidth.
520 /* Used only by DIR and AP connections: */
521 char rend_query
[REND_SERVICE_ID_LEN
+1]; /**< What rendezvous service are we
522 * querying for? (DIR/AP only) */
524 /* Used only by edge connections: */
526 struct connection_t
*next_stream
; /**< Points to the next stream at this
527 * edge, if any (Edge only). */
528 struct crypt_path_t
*cpath_layer
; /**< A pointer to which node in the circ
529 * this conn exits at. (Edge only.) */
530 int package_window
; /**< How many more relay cells can i send into the
531 * circuit? (Edge only.) */
532 int deliver_window
; /**< How many more relay cells can end at me? (Edge
535 int done_sending
; /**< For half-open connections; not used currently. */
536 int done_receiving
; /**< For half-open connections; not used currently. */
537 char has_sent_end
; /**< For debugging: set once we've set the stream end,
538 and check in circuit_about_to_close_connection(). */
539 char num_retries
; /**< How many times have we re-tried beginning this stream? (Edge only) */
541 /* Used only by AP connections */
542 socks_request_t
*socks_request
; /**< SOCKS structure describing request (AP
546 typedef struct connection_t connection_t
;
548 #define EXIT_POLICY_ACCEPT 1
549 #define EXIT_POLICY_REJECT 2
551 /** A linked list of exit policy rules */
552 struct exit_policy_t
{
553 char policy_type
; /**< One of EXIT_POLICY_ACCEPT or EXIT_POLICY_REJECT. */
554 char *string
; /**< String representation of this rule. */
555 uint32_t addr
; /**< Base address to accept or reject. */
556 uint32_t msk
; /**< Accept/reject all addresses <b>a</b> such that a & msk ==
557 * <b>addr</b> & msk . */
558 uint16_t prt_min
; /**< Lowest port number to accept/reject. */
559 uint16_t prt_max
; /**< Highest port number to accept/reject. */
561 struct exit_policy_t
*next
; /**< Next rule in list. */
564 /** Information about another onion router in the network. */
566 char *address
; /**< Location of OR: either a hostname or an IP address. */
567 char *nickname
; /**< Human-readable OR name. */
569 uint32_t addr
; /**< IPv4 address of OR, in host order. */
570 uint16_t or_port
; /**< Port for OR-to-OR and OP-to-OR connections. */
571 uint16_t socks_port
; /**< Port for SOCKS connections. */
572 uint16_t dir_port
; /**< Port for HTTP directory connections. */
574 time_t published_on
; /**< When was the information in this routerinfo_t
577 crypto_pk_env_t
*onion_pkey
; /**< Public RSA key for onions. */
578 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for signing. */
579 char identity_digest
[DIGEST_LEN
]; /**< Digest of identity key */
581 char *platform
; /**< What software/operating system is this OR using? */
584 uint32_t bandwidthrate
; /**< How many bytes does this OR add to its token
585 * bucket per second? */
586 uint32_t bandwidthburst
; /**< How large is this OR's token bucket? */
587 /** How many bytes/s is this router known to handle? */
588 uint32_t bandwidthcapacity
;
589 struct exit_policy_t
*exit_policy
; /**< What streams will this OR permit
591 long uptime
; /**< How many seconds the router claims to have been up */
593 int is_running
; /**< As far as we know, is this OR currently running? */
594 time_t status_set_at
; /**< When did we last update is_running? */
595 int is_verified
; /**< Has a trusted dirserver validated this OR? */
596 int is_trusted_dir
; /**< Do we trust this OR as a directory server? */
600 /** Contents of a directory of onion routers. */
602 /** List of routerinfo_t */
603 smartlist_t
*routers
;
604 /** Which versions of tor are recommended by this directory? */
605 char *software_versions
;
606 /** When was the most recent directory that contributed to this list
610 time_t running_routers_updated_on
;
611 /** Which router is claimed to have signed it? */
612 char *signing_router
;
615 /** Contents of a running-routers list */
616 typedef struct running_routers_t
{
617 time_t published_on
; /**< When was the list marked as published? */
618 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
619 smartlist_t
*running_routers
;
622 /** Holds accounting information for a single step in the layered encryption
623 * performed by a circuit. Used only at the client edge of a circuit. */
624 struct crypt_path_t
{
626 /* crypto environments */
627 /** Encryption key and counter for cells heading towards the OR at this
629 crypto_cipher_env_t
*f_crypto
;
630 /** Encryption key and counter for cells heading back from the OR at this
632 crypto_cipher_env_t
*b_crypto
;
634 /** Digest state for cells heading towards the OR at this step. */
635 crypto_digest_env_t
*f_digest
; /* for integrity checking */
636 /** Digest state for cells heading away from the OR at this step. */
637 crypto_digest_env_t
*b_digest
;
639 /** Current state of Diffie-Hellman key negotiation with the OR at this
641 crypto_dh_env_t
*handshake_state
;
642 /** Negotiated key material shared with the OR at this step. */
643 char handshake_digest
[DIGEST_LEN
];/* KH in tor-spec.txt */
645 /** IP4 address of the OR at this step. */
647 /** Port of the OR at this step. */
649 /** Identity key digest of the OR at this step. */
650 char identity_digest
[DIGEST_LEN
];
652 /** Is the circuit built to this step? Must be one of:
653 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
654 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
655 * and not received an EXTENDED/CREATED)
656 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
658 #define CPATH_STATE_CLOSED 0
659 #define CPATH_STATE_AWAITING_KEYS 1
660 #define CPATH_STATE_OPEN 2
661 struct crypt_path_t
*next
; /**< Link to next crypt_path_t in the circuit.
662 * (The list is circular, so the last node
663 * links to the first.) */
664 struct crypt_path_t
*prev
; /**< Link to previous crypt_path_t in the
667 int package_window
; /**< How many bytes are we allowed to originate ending
669 int deliver_window
; /**< How many bytes are we willing to deliver originating
673 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
675 #define DH_KEY_LEN DH_BYTES
676 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
679 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
680 #define REND_COOKIE_LEN DIGEST_LEN
682 typedef struct crypt_path_t crypt_path_t
;
684 /** Information used to build a circuit. */
686 /** Intended length of the final circuit. */
687 int desired_path_len
;
688 /** Nickname of planned exit node. */
689 char *chosen_exit_name
;
690 /** Identity of planned exit node. */
691 char chosen_exit_digest
[DIGEST_LEN
];
692 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
693 struct crypt_path_t
*pending_final_cpath
;
694 /** How many times has building a circuit for this task failed? */
696 } cpath_build_state_t
;
699 #define CIRCUIT_MAGIC 0x35315243u
700 /** Struct for a path (circuit) through the onion routing network. */
702 uint32_t magic
; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
704 int marked_for_close
; /**< Should we close this circuit at the end of the
706 char *marked_for_close_file
; /**< For debugging: in which file was this
707 * circuit marked for close? */
709 /** The IPv4 address of the OR that is next in this circuit. */
711 /** The port for the OR that is next in this circuit. */
713 /** The OR connection that is previous in this circuit. */
714 connection_t
*p_conn
;
715 /** The OR connection that is next in this circuit. */
716 connection_t
*n_conn
;
717 /** The identity hash of n_conn. */
718 char n_conn_id_digest
[DIGEST_LEN
];
719 /** Linked list of AP streams associated with this circuit. */
720 connection_t
*p_streams
;
721 /** Linked list of Exit streams associated with this circuit. */
722 connection_t
*n_streams
;
723 /** Linked list of Exit streams associated with this circuit that are
724 * still being resolved. */
725 connection_t
*resolving_streams
;
726 /** The next stream_id that will be tried when we're attempting to
727 * construct a new AP stream originating at this circuit. */
728 uint16_t next_stream_id
;
729 /** How many relay data cells can we package (read from edge streams)
730 * on this circuit before we receive a circuit-level sendme cell asking
733 /** How many relay data cells will we deliver (write to edge streams)
734 * on this circuit? When deliver_window gets low, we send some
735 * circuit-level sendme cells to indicate that we're willing to accept
739 /** The circuit_id used in the previous (backward) hop of this circuit. */
741 /** The circuit_id used in the next (forward) hop of this circuit. */
744 /** The cipher used by intermediate hops for cells heading toward the
746 crypto_cipher_env_t
*p_crypto
;
747 /** The cipher used by intermediate hops for cells heading away from
749 crypto_cipher_env_t
*n_crypto
;
751 /** The integrity-checking digest used by intermediate hops, for
752 * cells packaged here and heading towards the OP.
754 crypto_digest_env_t
*p_digest
;
755 /** The integrity-checking digest used by intermediate hops, for
756 * cells packaged at the OP and arriving here.
758 crypto_digest_env_t
*n_digest
;
760 /** Build state for this circuit. It includes the intended path
761 * length, the chosen exit router, rendezvous information, etc.
763 cpath_build_state_t
*build_state
;
764 /** The doubly-linked list of crypt_path_t entries, one per hop,
765 * for this circuit. This includes ciphers for each hop,
766 * integrity-checking digests for each hop, and package/delivery
767 * windows for each hop.
769 * The cpath field is defined only when we are the circuit's origin.
773 /** For storage while passing to cpuworker, or while n_conn is pending. */
774 char onionskin
[ONIONSKIN_CHALLENGE_LEN
];
776 char handshake_digest
[DIGEST_LEN
]; /**< Stores KH for intermediate hops. */
778 time_t timestamp_created
; /**< When was this circuit created? */
779 time_t timestamp_dirty
; /**< When the circuit was first used, or 0 if the
780 * circuit is clean. */
782 uint8_t state
; /**< Current status of this circuit. */
783 uint8_t purpose
; /**< Why are we creating this circuit? */
786 * The rend_query field holds y portion of y.onion (nul-terminated)
787 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
788 * for a hidden service, or is S_*.
790 char rend_query
[REND_SERVICE_ID_LEN
+1];
792 /** The rend_pk_digest field holds a hash of location-hidden service's
793 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
795 char rend_pk_digest
[DIGEST_LEN
];
797 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
798 * C_ESTABLISH_REND. Filled with zeroes otherwise.
800 char rend_cookie
[REND_COOKIE_LEN
];
802 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
803 * is not marked for close. */
804 struct circuit_t
*rend_splice
;
806 struct circuit_t
*next
; /**< Next circuit in linked list. */
809 typedef struct circuit_t circuit_t
;
811 #define ALLOW_UNVERIFIED_ENTRY 1
812 #define ALLOW_UNVERIFIED_EXIT 2
813 #define ALLOW_UNVERIFIED_MIDDLE 4
814 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
815 #define ALLOW_UNVERIFIED_INTRODUCTION 16
817 /** Configuration options for a Tor process */
819 struct config_line_t
*LogOptions
; /**< List of configuration lines
822 char *DebugLogFile
; /**< Where to send verbose log messages. */
823 char *DataDirectory
; /**< OR only: where to store long-term data. */
824 char *RouterFile
; /**< Where to find starting list of ORs. */
825 char *Nickname
; /**< OR only: nickname of this onion router. */
826 char *Address
; /**< OR only: configured address for this onion router. */
827 char *PidFile
; /**< Where to store PID of Tor process. */
829 char *ExitNodes
; /**< Comma-separated list of nicknames of ORs to consider
831 char *EntryNodes
; /**< Comma-separated list of nicknames of ORs to consider
832 * as entry points. */
833 int StrictExitNodes
; /**< Boolean: When none of our ExitNodes are up, do we
834 * stop building circuits? */
835 int StrictEntryNodes
; /**< Boolean: When none of our EntryNodes are up, do we
836 * stop building circuits? */
837 char *ExcludeNodes
; /**< Comma-separated list of nicknames of ORs not to
838 * use in circuits. */
840 char *RendNodes
; /**< Comma-separated list of nicknames used as introduction
842 char *RendExcludeNodes
; /**< Comma-separated list of nicknames not to use
843 * as introduction points. */
845 smartlist_t
*AllowUnverifiedNodes
; /**< List of "entry", "middle", "exit" */
846 int _AllowUnverified
; /**< Bitmask; derived from AllowUnverifiedNodes; */
847 struct config_line_t
*ExitPolicy
; /**< Lists of exit policy components. */
848 struct config_line_t
*SocksPolicy
; /**< Lists of socks policy components */
849 /** Addresses to bind for listening for SOCKS connections. */
850 struct config_line_t
*SocksBindAddress
;
851 /** Addresses to bind for listening for OR connections. */
852 struct config_line_t
*ORBindAddress
;
853 /** Addresses to bind for listening for directory connections. */
854 struct config_line_t
*DirBindAddress
;
855 /** Local address to bind outbound sockets */
856 char *OutboundBindAddress
;
857 char *RecommendedVersions
; /**< Directory server only: which versions of
858 * Tor should we tell users to run? */
859 char *User
; /**< Name of user to run Tor as. */
860 char *Group
; /**< Name of group to run Tor as. */
861 double PathlenCoinWeight
; /**< Parameter used to configure average path
862 * length (alpha in geometric distribution). */
863 int ORPort
; /**< Port to listen on for OR connections. */
864 int SocksPort
; /**< Port to listen on for SOCKS connections. */
865 int DirPort
; /**< Port to listen on for directory connections. */
866 int AuthoritativeDir
; /**< Boolean: is this an authoritative directory? */
867 int ClientOnly
; /**< Boolean: should we never evolve into a server role? */
868 int MaxConn
; /**< Maximum number of simultaneous connections. */
869 int TrafficShaping
; /**< Unused. */
870 int LinkPadding
; /**< Unused. */
871 int IgnoreVersion
; /**< If true, run no matter what versions of Tor the
872 * directory recommends. */
873 int RunAsDaemon
; /**< If true, run in the background. (Unix only) */
874 int FascistFirewall
; /**< Whether to prefer ORs reachable on open ports. */
875 smartlist_t
*FirewallPorts
; /** Which ports our firewall allows. */
876 int DirFetchPostPeriod
; /**< How often do we fetch new directories
877 * and post server descriptros to the directory
879 int KeepalivePeriod
; /**< How often do we send padding cells to keep
880 * connections alive? */
881 int MaxOnionsPending
; /**< How many circuit CREATE requests do we allow
882 * to wait simultaneously before we start dropping
884 int NewCircuitPeriod
; /**< How long do we use a circuit before building
886 int BandwidthRate
; /**< How much bandwidth, on average, are we willing to
887 * use in a second? */
888 int BandwidthBurst
; /**< How much bandwidth, at maximum, are we willing to
889 * use in a second? */
890 int NumCpus
; /**< How many CPUs should we try to use? */
891 int RunTesting
; /**< If true, create testing circuits to measure how well the
892 * other ORs are running. */
893 struct config_line_t
*TrustedDirs
; /**< List of fingerprints of keys that are
894 allowed to sign directories. */
895 struct config_line_t
*RendConfigLines
; /**< List of configuration lines
896 * for rendezvous services. */
897 char *ContactInfo
; /** Contact info to be published in the directory */
900 /* XXX are these good enough defaults? */
901 #define MAX_SOCKS_REPLY_LEN 1024
902 #define MAX_SOCKS_ADDR_LEN 256
903 #define SOCKS_COMMAND_CONNECT 0x01
904 #define SOCKS_COMMAND_RESOLVE 0xF0
905 /** State of a SOCKS request from a user to an OP */
906 struct socks_request_t
{
907 char socks_version
; /**< Which version of SOCKS did the client use? */
908 int command
; /**< What has the user requested? One of CONNECT or RESOLVE. */
909 int replylen
; /**< Length of <b>reply</b>. */
910 char reply
[MAX_SOCKS_REPLY_LEN
]; /**< Write an entry into this string if
911 * we want to specify our own socks reply,
912 * rather than using the default socks4 or
913 * socks5 socks reply. We use this for the
914 * two-stage socks5 handshake.
916 int has_finished
; /**< Has the SOCKS handshake finished? */
917 char address
[MAX_SOCKS_ADDR_LEN
]; /**< What address did the client ask to connect to? */
918 uint16_t port
; /**< What port did the client ask to connect to? */
921 /* all the function prototypes go here */
923 /********************************* buffers.c ***************************/
926 buf_t
*buf_new_with_capacity(size_t size
);
927 void buf_free(buf_t
*buf
);
928 void buf_clear(buf_t
*buf
);
930 size_t buf_datalen(const buf_t
*buf
);
931 size_t buf_capacity(const buf_t
*buf
);
932 const char *_buf_peek_raw_buffer(const buf_t
*buf
);
934 int read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
);
935 int read_to_buf_tls(tor_tls
*tls
, size_t at_most
, buf_t
*buf
);
937 int flush_buf(int s
, buf_t
*buf
, int *buf_flushlen
);
938 int flush_buf_tls(tor_tls
*tls
, buf_t
*buf
, int *buf_flushlen
);
940 int write_to_buf(const char *string
, int string_len
, buf_t
*buf
);
941 int fetch_from_buf(char *string
, size_t string_len
, buf_t
*buf
);
942 int fetch_from_buf_http(buf_t
*buf
,
943 char **headers_out
, int max_headerlen
,
944 char **body_out
, int *body_used
, int max_bodylen
);
945 int fetch_from_buf_socks(buf_t
*buf
, socks_request_t
*req
);
947 void assert_buf_ok(buf_t
*buf
);
949 /********************************* circuitbuild.c **********************/
951 void circuit_log_path(int severity
, circuit_t
*circ
);
952 void circuit_rep_hist_note_result(circuit_t
*circ
);
953 void circuit_dump_by_conn(connection_t
*conn
, int severity
);
954 circuit_t
*circuit_establish_circuit(uint8_t purpose
,
955 const char *exit_digest
);
956 void circuit_n_conn_done(connection_t
*or_conn
, int success
);
957 int circuit_send_next_onion_skin(circuit_t
*circ
);
958 int circuit_extend(cell_t
*cell
, circuit_t
*circ
);
959 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
, int reverse
);
960 int circuit_finish_handshake(circuit_t
*circ
, char *reply
);
961 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
962 int onionskin_answer(circuit_t
*circ
, unsigned char *payload
, unsigned char *keys
);
963 void onion_append_to_cpath(crypt_path_t
**head_ptr
, crypt_path_t
*new_hop
);
965 /********************************* circuitlist.c ***********************/
967 extern char *circuit_state_to_string
[];
968 void circuit_close_all_marked(void);
969 circuit_t
*circuit_new(uint16_t p_circ_id
, connection_t
*p_conn
);
970 void circuit_free_cpath_node(crypt_path_t
*victim
);
971 circuit_t
*circuit_get_by_circ_id_conn(uint16_t circ_id
, connection_t
*conn
);
972 circuit_t
*circuit_get_by_conn(connection_t
*conn
);
973 circuit_t
*circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
);
974 circuit_t
*circuit_get_next_by_pk_and_purpose(circuit_t
*start
,
975 const char *digest
, uint8_t purpose
);
976 circuit_t
*circuit_get_rendezvous(const char *cookie
);
977 int circuit_count_building(uint8_t purpose
);
978 circuit_t
*circuit_get_youngest_clean_open(uint8_t purpose
);
979 int _circuit_mark_for_close(circuit_t
*circ
);
981 #define circuit_mark_for_close(c) \
983 if (_circuit_mark_for_close(c)<0) { \
984 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
985 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
987 c->marked_for_close_file = __FILE__; \
988 c->marked_for_close = __LINE__; \
992 void assert_cpath_layer_ok(const crypt_path_t
*cp
);
993 void assert_circuit_ok(const circuit_t
*c
);
995 /********************************* circuituse.c ************************/
997 void circuit_expire_building(time_t now
);
998 int circuit_stream_is_being_handled(connection_t
*conn
);
999 void circuit_build_needed_circs(time_t now
);
1000 void circuit_detach_stream(circuit_t
*circ
, connection_t
*conn
);
1001 void circuit_about_to_close_connection(connection_t
*conn
);
1002 void circuit_has_opened(circuit_t
*circ
);
1003 void circuit_build_failed(circuit_t
*circ
);
1004 circuit_t
*circuit_launch_by_nickname(uint8_t purpose
, const char *exit_nickname
);
1005 circuit_t
*circuit_launch_by_identity(uint8_t purpose
, const char *exit_digest
);
1006 void circuit_reset_failure_count(int timeout
);
1007 int connection_ap_handshake_attach_circuit(connection_t
*conn
);
1009 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
,int reverse
);
1010 int circuit_finish_handshake(circuit_t
*circ
, char *reply
);
1011 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
1013 void assert_cpath_layer_ok(const crypt_path_t
*c
);
1014 void assert_circuit_ok(const circuit_t
*c
);
1016 /********************************* command.c ***************************/
1018 void command_process_cell(cell_t
*cell
, connection_t
*conn
);
1020 extern unsigned long stats_n_padding_cells_processed
;
1021 extern unsigned long stats_n_create_cells_processed
;
1022 extern unsigned long stats_n_created_cells_processed
;
1023 extern unsigned long stats_n_relay_cells_processed
;
1024 extern unsigned long stats_n_destroy_cells_processed
;
1026 /********************************* config.c ***************************/
1028 struct config_line_t
{
1031 struct config_line_t
*next
;
1034 int config_assign_default_dirservers(void);
1035 int resolve_my_address(const char *address
, uint32_t *addr
);
1036 int getconfig(int argc
, char **argv
, or_options_t
*options
);
1037 int config_init_logs(or_options_t
*options
);
1038 void config_parse_exit_policy(struct config_line_t
*cfg
,
1039 struct exit_policy_t
**dest
);
1040 void exit_policy_free(struct exit_policy_t
*p
);
1041 const char *get_data_directory(or_options_t
*options
);
1043 /********************************* connection.c ***************************/
1045 #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
1046 "Unknown" : conn_type_to_string[(t)])
1048 extern char *conn_type_to_string
[];
1049 extern char *conn_state_to_string
[][_CONN_TYPE_MAX
+1];
1051 connection_t
*connection_new(int type
);
1052 void connection_free(connection_t
*conn
);
1053 void connection_free_all(void);
1054 void connection_about_to_close_connection(connection_t
*conn
);
1055 void connection_close_immediate(connection_t
*conn
);
1056 int _connection_mark_for_close(connection_t
*conn
);
1058 #define connection_mark_for_close(c) \
1060 if (_connection_mark_for_close(c)<0) { \
1061 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
1062 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
1064 c->marked_for_close_file = __FILE__; \
1065 c->marked_for_close = __LINE__; \
1069 void connection_expire_held_open(void);
1071 int connection_connect(connection_t
*conn
, char *address
, uint32_t addr
, uint16_t port
);
1072 int retry_all_listeners(void);
1074 void connection_bucket_init(void);
1075 void connection_bucket_refill(struct timeval
*now
);
1077 int connection_handle_read(connection_t
*conn
);
1079 int connection_fetch_from_buf(char *string
, int len
, connection_t
*conn
);
1081 int connection_wants_to_flush(connection_t
*conn
);
1082 int connection_outbuf_too_full(connection_t
*conn
);
1083 int connection_handle_write(connection_t
*conn
);
1084 void connection_write_to_buf(const char *string
, int len
, connection_t
*conn
);
1086 connection_t
*connection_exact_get_by_addr_port(uint32_t addr
, uint16_t port
);
1087 connection_t
*connection_get_by_identity_digest(const char *digest
, int type
);
1089 connection_t
*connection_get_by_type(int type
);
1090 connection_t
*connection_get_by_type_state(int type
, int state
);
1091 connection_t
*connection_get_by_type_state_lastwritten(int type
, int state
);
1092 connection_t
*connection_get_by_type_rendquery(int type
, const char *rendquery
);
1094 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1095 #define connection_has_pending_tls_data(conn) \
1096 ((conn)->type == CONN_TYPE_OR && \
1097 (conn)->state == OR_CONN_STATE_OPEN && \
1098 tor_tls_get_pending_bytes((conn)->tls))
1099 int connection_is_listener(connection_t
*conn
);
1100 int connection_state_is_open(connection_t
*conn
);
1101 int connection_state_is_connecting(connection_t
*conn
);
1103 int connection_send_destroy(uint16_t circ_id
, connection_t
*conn
);
1105 void assert_connection_ok(connection_t
*conn
, time_t now
);
1107 /********************************* connection_edge.c ***************************/
1109 int connection_edge_process_inbuf(connection_t
*conn
);
1110 int connection_edge_destroy(uint16_t circ_id
, connection_t
*conn
);
1111 int connection_edge_end(connection_t
*conn
, char reason
, crypt_path_t
*cpath_layer
);
1112 int connection_edge_finished_flushing(connection_t
*conn
);
1113 int connection_edge_finished_connecting(connection_t
*conn
);
1115 int connection_ap_handshake_send_begin(connection_t
*ap_conn
, circuit_t
*circ
);
1116 int connection_ap_handshake_send_resolve(connection_t
*ap_conn
, circuit_t
*circ
);
1118 int connection_ap_make_bridge(char *address
, uint16_t port
);
1119 void connection_ap_handshake_socks_reply(connection_t
*conn
, char *reply
,
1120 int replylen
, int success
);
1121 void connection_ap_handshake_socks_resolved(connection_t
*conn
,
1124 const char *answer
);
1126 int connection_exit_begin_conn(cell_t
*cell
, circuit_t
*circ
);
1127 int connection_exit_begin_resolve(cell_t
*cell
, circuit_t
*circ
);
1128 void connection_exit_connect(connection_t
*conn
);
1129 int connection_edge_is_rendezvous_stream(connection_t
*conn
);
1130 int connection_ap_can_use_exit(connection_t
*conn
, routerinfo_t
*exit
);
1131 void connection_ap_expire_beginning(void);
1132 void connection_ap_attach_pending(void);
1134 int socks_policy_permits_address(uint32_t addr
);
1136 void client_dns_init(void);
1137 uint32_t client_dns_lookup_entry(const char *address
);
1138 int client_dns_incr_failures(const char *address
);
1139 void client_dns_set_entry(const char *address
, uint32_t val
);
1140 void client_dns_clean(void);
1142 /********************************* connection_or.c ***************************/
1144 int connection_or_process_inbuf(connection_t
*conn
);
1145 int connection_or_finished_flushing(connection_t
*conn
);
1146 int connection_or_finished_connecting(connection_t
*conn
);
1148 connection_t
*connection_or_connect(uint32_t addr
, uint16_t port
,
1149 const char *id_digest
);
1151 int connection_tls_start_handshake(connection_t
*conn
, int receiving
);
1152 int connection_tls_continue_handshake(connection_t
*conn
);
1154 void connection_or_write_cell_to_buf(const cell_t
*cell
, connection_t
*conn
);
1156 /********************************* cpuworker.c *****************************/
1158 void cpu_init(void);
1159 void cpuworkers_rotate(void);
1160 int connection_cpu_finished_flushing(connection_t
*conn
);
1161 int connection_cpu_process_inbuf(connection_t
*conn
);
1162 int assign_to_cpuworker(connection_t
*cpuworker
, unsigned char question_type
,
1165 /********************************* directory.c ***************************/
1167 void directory_post_to_dirservers(uint8_t purpose
, const char *payload
,
1169 void directory_get_from_dirserver(uint8_t purpose
, const char *payload
,
1171 int connection_dir_process_inbuf(connection_t
*conn
);
1172 int connection_dir_finished_flushing(connection_t
*conn
);
1173 int connection_dir_finished_connecting(connection_t
*conn
);
1175 /********************************* dirserv.c ***************************/
1177 int dirserv_add_own_fingerprint(const char *nickname
, crypto_pk_env_t
*pk
);
1178 int dirserv_parse_fingerprint_file(const char *fname
);
1179 int dirserv_router_fingerprint_is_known(const routerinfo_t
*router
);
1180 void dirserv_free_fingerprint_list();
1181 int dirserv_add_descriptor(const char **desc
);
1182 int dirserv_load_from_directory_string(const char *dir
);
1183 void dirserv_free_descriptors();
1184 void dirserv_remove_old_servers(int age
);
1185 int dirserv_dump_directory_to_string(char *s
, unsigned int maxlen
,
1186 crypto_pk_env_t
*private_key
);
1187 void directory_set_dirty(void);
1188 size_t dirserv_get_directory(const char **cp
);
1189 size_t dirserv_get_runningrouters(const char **rr
);
1190 void dirserv_set_cached_directory(const char *directory
, time_t when
);
1192 /********************************* dns.c ***************************/
1194 void dns_init(void);
1195 int connection_dns_finished_flushing(connection_t
*conn
);
1196 int connection_dns_process_inbuf(connection_t
*conn
);
1197 void dnsworkers_rotate(void);
1198 void connection_dns_remove(connection_t
*conn
);
1199 void assert_connection_edge_not_dns_pending(connection_t
*conn
);
1200 void assert_all_pending_dns_resolves_ok(void);
1201 void dns_cancel_pending_resolve(char *question
);
1202 int dns_resolve(connection_t
*exitconn
);
1204 /********************************* main.c ***************************/
1206 int connection_add(connection_t
*conn
);
1207 int connection_remove(connection_t
*conn
);
1208 int connection_in_array(connection_t
*conn
);
1210 void get_connection_array(connection_t
***array
, int *n
);
1212 void connection_watch_events(connection_t
*conn
, short events
);
1213 int connection_is_reading(connection_t
*conn
);
1214 void connection_stop_reading(connection_t
*conn
);
1215 void connection_start_reading(connection_t
*conn
);
1217 int connection_is_writing(connection_t
*conn
);
1218 void connection_stop_writing(connection_t
*conn
);
1219 void connection_start_writing(connection_t
*conn
);
1221 void directory_has_arrived(void);
1222 int authdir_mode(void);
1223 int clique_mode(void);
1224 int server_mode(void);
1225 int advertised_server_mode(void);
1226 int proxy_mode(void);
1228 void handle_signals(int is_parent
);
1229 void tor_cleanup(void);
1231 /********************************* onion.c ***************************/
1233 int onion_pending_add(circuit_t
*circ
);
1234 circuit_t
*onion_next_task(void);
1235 void onion_pending_remove(circuit_t
*circ
);
1237 int onion_skin_create(crypto_pk_env_t
*router_key
,
1238 crypto_dh_env_t
**handshake_state_out
,
1239 char *onion_skin_out
);
1241 int onion_skin_server_handshake(char *onion_skin
,
1242 crypto_pk_env_t
*private_key
,
1243 crypto_pk_env_t
*prev_private_key
,
1244 char *handshake_reply_out
,
1248 int onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
1249 char *handshake_reply
,
1253 /********************************* relay.c ***************************/
1255 extern unsigned long stats_n_relay_cells_relayed
;
1256 extern unsigned long stats_n_relay_cells_delivered
;
1258 int circuit_receive_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1259 int cell_direction
);
1261 void relay_header_pack(char *dest
, const relay_header_t
*src
);
1262 void relay_header_unpack(relay_header_t
*dest
, const char *src
);
1263 int connection_edge_send_command(connection_t
*fromconn
, circuit_t
*circ
,
1264 int relay_command
, const char *payload
,
1265 int payload_len
, crypt_path_t
*cpath_layer
);
1266 int connection_edge_package_raw_inbuf(connection_t
*conn
);
1267 void connection_edge_consider_sending_sendme(connection_t
*conn
);
1269 extern uint64_t stats_n_data_cells_packaged
;
1270 extern uint64_t stats_n_data_bytes_packaged
;
1271 extern uint64_t stats_n_data_cells_received
;
1272 extern uint64_t stats_n_data_bytes_received
;
1274 /********************************* rephist.c ***************************/
1276 void rep_hist_init(void);
1277 void rep_hist_note_connect_failed(const char* nickname
, time_t when
);
1278 void rep_hist_note_connect_succeeded(const char* nickname
, time_t when
);
1279 void rep_hist_note_disconnect(const char* nickname
, time_t when
);
1280 void rep_hist_note_connection_died(const char* nickname
, time_t when
);
1281 void rep_hist_note_extend_succeeded(const char *from_name
,
1282 const char *to_name
);
1283 void rep_hist_note_extend_failed(const char *from_name
, const char *to_name
);
1284 void rep_hist_dump_stats(time_t now
, int severity
);
1285 void rep_hist_note_bytes_read(int num_bytes
, time_t when
);
1286 void rep_hist_note_bytes_written(int num_bytes
, time_t when
);
1287 int rep_hist_bandwidth_assess(void);
1288 char *rep_hist_get_bandwidth_lines(void);
1290 /********************************* rendclient.c ***************************/
1292 void rend_client_introcirc_has_opened(circuit_t
*circ
);
1293 void rend_client_rendcirc_has_opened(circuit_t
*circ
);
1294 int rend_client_introduction_acked(circuit_t
*circ
, const char *request
, int request_len
);
1295 void rend_client_refetch_renddesc(const char *query
);
1296 int rend_client_remove_intro_point(char *failed_intro
, const char *query
);
1297 int rend_client_rendezvous_acked(circuit_t
*circ
, const char *request
, int request_len
);
1298 int rend_client_receive_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1299 void rend_client_desc_fetched(char *query
, int success
);
1301 char *rend_client_get_random_intro(char *query
);
1302 int rend_parse_rendezvous_address(char *address
);
1304 int rend_client_send_introduction(circuit_t
*introcirc
, circuit_t
*rendcirc
);
1306 /********************************* rendcommon.c ***************************/
1308 typedef struct rend_service_descriptor_t
{
1309 crypto_pk_env_t
*pk
;
1312 char **intro_points
;
1313 } rend_service_descriptor_t
;
1315 int rend_cmp_service_ids(const char *one
, const char *two
);
1317 void rend_process_relay_cell(circuit_t
*circ
, int command
, int length
,
1318 const char *payload
);
1320 void rend_service_descriptor_free(rend_service_descriptor_t
*desc
);
1321 int rend_encode_service_descriptor(rend_service_descriptor_t
*desc
,
1322 crypto_pk_env_t
*key
,
1325 rend_service_descriptor_t
*rend_parse_service_descriptor(const char *str
, int len
);
1326 int rend_get_service_id(crypto_pk_env_t
*pk
, char *out
);
1328 typedef struct rend_cache_entry_t
{
1329 int len
; /* Length of desc */
1330 time_t received
; /* When did we get the descriptor? */
1331 char *desc
; /* Service descriptor */
1332 rend_service_descriptor_t
*parsed
; /* Parsed value of 'desc' */
1333 } rend_cache_entry_t
;
1335 void rend_cache_init(void);
1336 void rend_cache_clean(void);
1337 int rend_valid_service_id(const char *query
);
1338 int rend_cache_lookup_desc(const char *query
, const char **desc
, int *desc_len
);
1339 int rend_cache_lookup_entry(const char *query
, rend_cache_entry_t
**entry_out
);
1340 int rend_cache_store(const char *desc
, int desc_len
);
1342 /********************************* rendservice.c ***************************/
1344 int rend_config_services(or_options_t
*options
);
1345 int rend_service_load_keys(void);
1346 void rend_services_init(void);
1347 void rend_services_introduce(void);
1348 void rend_services_upload(int force
);
1350 void rend_service_intro_has_opened(circuit_t
*circuit
);
1351 int rend_service_intro_established(circuit_t
*circuit
, const char *request
, int request_len
);
1352 void rend_service_rendezvous_has_opened(circuit_t
*circuit
);
1353 int rend_service_introduce(circuit_t
*circuit
, const char *request
, int request_len
);
1354 void rend_service_relaunch_rendezvous(circuit_t
*oldcirc
);
1355 int rend_service_set_connection_addr_port(connection_t
*conn
, circuit_t
*circ
);
1356 void rend_service_dump_stats(int severity
);
1358 /********************************* rendmid.c *******************************/
1359 int rend_mid_establish_intro(circuit_t
*circ
, const char *request
, int request_len
);
1360 int rend_mid_introduce(circuit_t
*circ
, const char *request
, int request_len
);
1361 int rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1362 int rend_mid_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1364 /********************************* router.c ***************************/
1366 void set_onion_key(crypto_pk_env_t
*k
);
1367 crypto_pk_env_t
*get_onion_key(void);
1368 crypto_pk_env_t
*get_previous_onion_key(void);
1369 time_t get_onion_key_set_at(void);
1370 void set_identity_key(crypto_pk_env_t
*k
);
1371 crypto_pk_env_t
*get_identity_key(void);
1372 void dup_onion_keys(crypto_pk_env_t
**key
, crypto_pk_env_t
**last
);
1373 int init_keys(void);
1374 crypto_pk_env_t
*init_key_from_file(const char *fname
);
1375 void rotate_onion_key(void);
1376 void router_set_bandwidth_capacity(int bw
);
1377 int router_get_bandwidth_capacity(void);
1379 void router_retry_connections(void);
1380 int router_is_clique_mode(routerinfo_t
*router
);
1381 void router_upload_dir_desc_to_dirservers(void);
1382 int router_compare_to_my_exit_policy(connection_t
*conn
);
1383 routerinfo_t
*router_get_my_routerinfo(void);
1384 const char *router_get_my_descriptor(void);
1385 int router_is_me(routerinfo_t
*router
);
1386 int router_rebuild_descriptor(void);
1387 int router_dump_router_to_string(char *s
, int maxlen
, routerinfo_t
*router
,
1388 crypto_pk_env_t
*ident_key
);
1389 int is_legal_nickname(const char *s
);
1390 int is_legal_nickname_or_hexdigest(const char *s
);
1392 /********************************* routerlist.c ***************************/
1394 routerinfo_t
*router_pick_directory_server(int requireauth
, int requireothers
);
1395 int all_directory_servers_down(void);
1397 void add_nickname_list_to_smartlist(struct smartlist_t
*sl
, const char *list
);
1398 routerinfo_t
*routerlist_find_my_routerinfo(void);
1399 int router_nickname_matches(routerinfo_t
*router
, const char *nickname
);
1400 routerinfo_t
*routerlist_sl_choose_by_bandwidth(smartlist_t
*sl
);
1401 routerinfo_t
*router_choose_random_node(char *preferred
, char *excluded
,
1402 struct smartlist_t
*excludedsmartlist
,
1403 int preferuptime
, int preferbandwidth
,
1404 int allow_unverified
, int strict
);
1405 routerinfo_t
*router_get_by_addr_port(uint32_t addr
, uint16_t port
);
1406 routerinfo_t
*router_get_by_nickname(const char *nickname
);
1407 routerinfo_t
*router_get_by_hexdigest(const char *hexdigest
);
1408 routerinfo_t
*router_get_by_digest(const char *digest
);
1409 int router_digest_is_trusted_dir(const char *digest
);
1410 void router_get_routerlist(routerlist_t
**prouterlist
);
1411 void routerlist_free(routerlist_t
*routerlist
);
1412 void routerlist_clear_trusted_directories(void);
1413 void routerinfo_free(routerinfo_t
*router
);
1414 routerinfo_t
*routerinfo_copy(const routerinfo_t
*router
);
1415 void router_mark_as_down(const char *digest
);
1416 void routerlist_remove_old_routers(int age
);
1417 int router_load_routerlist_from_file(char *routerfile
, int trusted
);
1418 int router_load_routerlist_from_string(const char *s
, int trusted
);
1419 int router_load_routerlist_from_directory(const char *s
,crypto_pk_env_t
*pkey
);
1420 int router_compare_addr_to_exit_policy(uint32_t addr
, uint16_t port
,
1421 struct exit_policy_t
*policy
);
1422 #define ADDR_POLICY_ACCEPTED 0
1423 #define ADDR_POLICY_REJECTED -1
1424 #define ADDR_POLICY_UNKNOWN 1
1425 int router_exit_policy_all_routers_reject(uint32_t addr
, uint16_t port
);
1426 int router_exit_policy_rejects_all(routerinfo_t
*router
);
1427 void running_routers_free(running_routers_t
*rr
);
1428 void routerlist_update_from_runningrouters(routerlist_t
*list
,
1429 running_routers_t
*rr
);
1430 int router_update_status_from_smartlist(routerinfo_t
*r
,
1432 smartlist_t
*running_list
);
1434 /********************************* routerparse.c ************************/
1436 typedef struct tor_version_t
{
1440 enum { VER_PRE
=0, VER_RC
=1, VER_RELEASE
=2 } status
;
1442 enum { IS_CVS
=0, IS_NOT_CVS
=1} cvs
;
1445 int router_get_router_hash(const char *s
, char *digest
);
1446 int router_get_dir_hash(const char *s
, char *digest
);
1447 int router_get_runningrouters_hash(const char *s
, char *digest
);
1448 int router_parse_list_from_string(const char **s
,
1449 routerlist_t
**dest
,
1450 smartlist_t
*good_nickname_list
,
1452 int router_parse_routerlist_from_directory(const char *s
,
1453 routerlist_t
**dest
,
1454 crypto_pk_env_t
*pkey
);
1455 running_routers_t
*router_parse_runningrouters(const char *str
);
1456 routerinfo_t
*router_parse_entry_from_string(const char *s
, const char *end
);
1457 int router_add_exit_policy_from_string(routerinfo_t
*router
, const char *s
);
1458 struct exit_policy_t
*router_parse_exit_policy_from_string(const char *s
);
1459 int check_software_version_against_directory(const char *directory
,
1461 int tor_version_parse(const char *s
, tor_version_t
*out
);
1462 int tor_version_compare(tor_version_t
*a
, tor_version_t
*b
);
1463 int tor_version_compare_to_mine(const char *s
);
1470 indent-tabs-mode:nil