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 */
10 * \brief Master header file for Tor-specific functionality.
15 #define OR_H_ID "$Id$"
19 #define WIN32_WINNT 0x400
20 #define _WIN32_WINNT 0x400
21 #define WIN32_LEAN_AND_MEAN
42 #include "../common/torint.h"
43 #ifdef HAVE_INTTYPES_H
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
49 #ifdef HAVE_SYS_LIMITS_H
50 #include <sys/limits.h>
52 #ifdef HAVE_MACHINE_LIMITS_H
54 /* FreeBSD has a bug where it complains that this file is obsolete,
55 and I should migrate to using sys/limits. It complains even when
57 #include <machine/limits.h>
60 #ifdef HAVE_SYS_TYPES_H
61 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
63 #ifdef HAVE_SYS_WAIT_H
66 #ifdef HAVE_SYS_FCNTL_H
67 #include <sys/fcntl.h>
72 #ifdef HAVE_SYS_IOCTL_H
73 #include <sys/ioctl.h>
75 #ifdef HAVE_SYS_SOCKET_H
76 #include <sys/socket.h>
78 #ifdef HAVE_SYS_TIME_H
81 #ifdef HAVE_SYS_STAT_H
84 #ifdef HAVE_NETINET_IN_H
85 #include <netinet/in.h>
87 #ifdef HAVE_ARPA_INET_H
88 #include <arpa/inet.h>
100 /** Upper bound on maximum simultaneous connections; can be lowered by
102 #define MAXCONNECTIONS 15000
105 /* No, we don't need to redefine FD_SETSIZE before including winsock:
106 * we use libevent now, and libevent handles the select() stuff. Yes,
107 * some documents imply that we need to redefine anyway if we're using
108 * select() anywhere in our application or in anything it links to: these
109 * documents are either the holy texts of a cargo cult of network
110 * programmers, or more likely a simplification of what's going on for
111 * people who haven't read winsock[2].c for themselves.
113 #if (_MSC_VER <= 1300)
116 #include <winsock2.h>
117 #include <ws2tcpip.h>
126 #define snprintf _snprintf
132 #error "Tor requires libevent to build."
135 #include "../common/crypto.h"
136 #include "../common/tortls.h"
137 #include "../common/log.h"
138 #include "../common/compat.h"
139 #include "../common/container.h"
140 #include "../common/util.h"
141 #include "../common/torgzip.h"
143 #if (SIZEOF_CELL_T != 0)
144 /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
145 * that our stuff always calls cell_t something different. */
146 #define cell_t tor_cell_t
149 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
150 #define MAX_NICKNAME_LEN 19
151 /* Hex digest plus dollar sign. */
152 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
153 /** Maximum size, in bytes, for resized buffers. */
154 #define MAX_BUF_SIZE ((1<<24)-1)
155 #define MAX_DIR_SIZE MAX_BUF_SIZE
157 /* For http parsing */
158 #define MAX_HEADERS_SIZE 50000
159 #define MAX_BODY_SIZE 500000
162 /** How long do we keep DNS cache entries before purging them? */
163 #define MAX_DNS_ENTRY_AGE (150*60)
165 #define MAX_DNS_ENTRY_AGE (15*60)
168 /** How often do we rotate onion keys? */
169 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
170 /** How often do we rotate TLS contexts? */
171 #define MAX_SSL_KEY_LIFETIME (120*60)
173 /** How old do we allow a router to get before removing it, either
174 * from the descriptor list (for dirservers) or the router list (for others)?
176 #define ROUTER_MAX_AGE (60*60*24)
179 CIRC_ID_TYPE_LOWER
=0,
180 CIRC_ID_TYPE_HIGHER
=1
183 #define _CONN_TYPE_MIN 3
184 /** Type for sockets listening for OR connections. */
185 #define CONN_TYPE_OR_LISTENER 3
186 /** Type for OR-to-OR or OP-to-OR connections. */
187 #define CONN_TYPE_OR 4
188 /** Type for connections from final OR to chosen destination. */
189 #define CONN_TYPE_EXIT 5
190 /** Type for sockets listening for SOCKS connections. */
191 #define CONN_TYPE_AP_LISTENER 6
192 /** Type for SOCKS connections to OP. */
193 #define CONN_TYPE_AP 7
194 /** Type for sockets listening for HTTP connections to the directory server. */
195 #define CONN_TYPE_DIR_LISTENER 8
196 /** Type for HTTP connections to the directory server. */
197 #define CONN_TYPE_DIR 9
198 /** Type for connections to local dnsworker processes. */
199 #define CONN_TYPE_DNSWORKER 10
200 /** Type for connections to local cpuworker processes. */
201 #define CONN_TYPE_CPUWORKER 11
202 /** Type for listenting for connections from user interface process */
203 #define CONN_TYPE_CONTROL_LISTENER 12
204 /** Type for connections from user interface process */
205 #define CONN_TYPE_CONTROL 13
206 #define _CONN_TYPE_MAX 13
208 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
210 /** State for any listener connection. */
211 #define LISTENER_STATE_READY 0
213 #define _DNSWORKER_STATE_MIN 1
214 /** State for a connection to a dnsworker process that's idle. */
215 #define DNSWORKER_STATE_IDLE 1
216 /** State for a connection to a dnsworker process that's resolving a hostname. */
217 #define DNSWORKER_STATE_BUSY 2
218 #define _DNSWORKER_STATE_MAX 2
220 #define _CPUWORKER_STATE_MIN 1
221 /** State for a connection to a cpuworker process that's idle. */
222 #define CPUWORKER_STATE_IDLE 1
223 /** State for a connection to a cpuworker process that's processing a
225 #define CPUWORKER_STATE_BUSY_ONION 2
226 #define _CPUWORKER_STATE_MAX 2
228 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
230 #define _OR_CONN_STATE_MIN 1
231 /** State for a connection to an OR: waiting for connect() to finish. */
232 #define OR_CONN_STATE_CONNECTING 1
233 /** State for a connection to an OR: waiting for proxy command to flush. */
234 #define OR_CONN_STATE_PROXY_FLUSHING 2
235 /** State for a connection to an OR: waiting for proxy response. */
236 #define OR_CONN_STATE_PROXY_READING 3
237 /** State for a connection to an OR: SSL is handshaking, not done yet. */
238 #define OR_CONN_STATE_HANDSHAKING 4
239 /** State for a connection to an OR: Ready to send/receive cells. */
240 #define OR_CONN_STATE_OPEN 5
241 #define _OR_CONN_STATE_MAX 5
243 #define _EXIT_CONN_STATE_MIN 1
244 /** State for an exit connection: waiting for response from dns farm. */
245 #define EXIT_CONN_STATE_RESOLVING 1
246 /** State for an exit connection: waiting for connect() to finish. */
247 #define EXIT_CONN_STATE_CONNECTING 2
248 /** State for an exit connection: open and ready to transmit data. */
249 #define EXIT_CONN_STATE_OPEN 3
250 /** State for an exit connection: waiting to be removed. */
251 #define EXIT_CONN_STATE_RESOLVEFAILED 4
252 #define _EXIT_CONN_STATE_MAX 4
254 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
255 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
258 /* the AP state values must be disjoint from the EXIT state values */
259 #define _AP_CONN_STATE_MIN 5
260 /** State for a SOCKS connection: waiting for SOCKS request. */
261 #define AP_CONN_STATE_SOCKS_WAIT 5
262 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
263 * rendezvous rescriptor. */
264 #define AP_CONN_STATE_RENDDESC_WAIT 6
265 /** The controller will attach this connection to a circuit; it isn't our
267 #define AP_CONN_STATE_CONTROLLER_WAIT 7
268 /** State for a SOCKS connection: waiting for a completed circuit. */
269 #define AP_CONN_STATE_CIRCUIT_WAIT 8
270 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
271 #define AP_CONN_STATE_CONNECT_WAIT 9
272 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
273 #define AP_CONN_STATE_RESOLVE_WAIT 10
274 /** State for a SOCKS connection: ready to send and receive. */
275 #define AP_CONN_STATE_OPEN 11
276 #define _AP_CONN_STATE_MAX 11
278 #define _DIR_CONN_STATE_MIN 1
279 /** State for connection to directory server: waiting for connect(). */
280 #define DIR_CONN_STATE_CONNECTING 1
281 /** State for connection to directory server: sending HTTP request. */
282 #define DIR_CONN_STATE_CLIENT_SENDING 2
283 /** State for connection to directory server: reading HTTP response. */
284 #define DIR_CONN_STATE_CLIENT_READING 3
285 /** State for connection at directory server: waiting for HTTP request. */
286 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
287 /** State for connection at directory server: sending HTTP response. */
288 #define DIR_CONN_STATE_SERVER_WRITING 5
289 #define _DIR_CONN_STATE_MAX 5
291 #define _CONTROL_CONN_STATE_MIN 1
292 #define CONTROL_CONN_STATE_OPEN 1
293 #define CONTROL_CONN_STATE_NEEDAUTH 2
294 #define _CONTROL_CONN_STATE_MAX 2
296 #define _DIR_PURPOSE_MIN 1
297 /** Purpose for connection to directory server: download a directory. */
298 #define DIR_PURPOSE_FETCH_DIR 1
299 /** Purpose for connection to directory server: download just the list
300 * of running routers. */
301 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
302 /** Purpose for connection to directory server: download a rendezvous
304 #define DIR_PURPOSE_FETCH_RENDDESC 3
305 /** Purpose for connection to directory server: set after a rendezvous
306 * descriptor is downloaded. */
307 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
308 /** Purpose for connection to directory server: upload a server descriptor. */
309 #define DIR_PURPOSE_UPLOAD_DIR 5
310 /** Purpose for connection to directory server: upload a rendezvous
312 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
313 /** Purpose for connection at a directory server. */
314 #define DIR_PURPOSE_SERVER 7
315 #define _DIR_PURPOSE_MAX 7
317 #define _EXIT_PURPOSE_MIN 1
318 #define EXIT_PURPOSE_CONNECT 1
319 #define EXIT_PURPOSE_RESOLVE 2
320 #define _EXIT_PURPOSE_MAX 2
322 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
323 #define CIRCUIT_STATE_BUILDING 0
324 /** Circuit state: Waiting to process the onionskin. */
325 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
326 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
327 #define CIRCUIT_STATE_OR_WAIT 2
328 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
329 #define CIRCUIT_STATE_OPEN 3
331 #define _CIRCUIT_PURPOSE_MIN 1
333 /* these circuits were initiated elsewhere */
334 #define _CIRCUIT_PURPOSE_OR_MIN 1
335 /** OR-side circuit purpose: normal circuit, at OR. */
336 #define CIRCUIT_PURPOSE_OR 1
337 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
338 #define CIRCUIT_PURPOSE_INTRO_POINT 2
339 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
340 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
341 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
342 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
343 #define _CIRCUIT_PURPOSE_OR_MAX 4
345 /* these circuits originate at this node */
347 /* here's how circ client-side purposes work:
348 * normal circuits are C_GENERAL.
349 * circuits that are c_introducing are either on their way to
350 * becoming open, or they are open and waiting for a
351 * suitable rendcirc before they send the intro.
352 * circuits that are c_introduce_ack_wait have sent the intro,
353 * but haven't gotten a response yet.
354 * circuits that are c_establish_rend are either on their way
355 * to becoming open, or they are open and have sent the
356 * establish_rendezvous cell but haven't received an ack.
357 * circuits that are c_rend_ready are open and have received a
358 * rend ack, but haven't heard from bob yet. if they have a
359 * buildstate->pending_final_cpath then they're expecting a
360 * cell from bob, else they're not.
361 * circuits that are c_rend_ready_intro_acked are open, and
362 * some intro circ has sent its intro and received an ack.
363 * circuits that are c_rend_joined are open, have heard from
364 * bob, and are talking to him.
366 /** Client-side circuit purpose: Normal circuit, with cpath. */
367 #define CIRCUIT_PURPOSE_C_GENERAL 5
368 /** Client-side circuit purpose: at Alice, connecting to intro point. */
369 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
370 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
371 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
372 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
373 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
374 /** Client-side circuit purpose: at Alice, waiting for ack. */
375 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
376 /** Client-side circuit purpose: at Alice, waiting for Bob. */
377 #define CIRCUIT_PURPOSE_C_REND_READY 10
378 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
379 * has been acknowledged. */
380 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
381 /** Client-side circuit purpose: at Alice, rendezvous established. */
382 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
384 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
385 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
386 /** Hidden-service-side circuit purpose: at Bob, successfully established
388 #define CIRCUIT_PURPOSE_S_INTRO 14
389 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
390 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
391 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
392 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
393 /** A testing circuit; not meant to be used for actual traffic. */
394 #define CIRCUIT_PURPOSE_TESTING 17
395 #define _CIRCUIT_PURPOSE_MAX 17
397 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
398 * that this OP has originated. */
399 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
400 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
402 #define RELAY_COMMAND_BEGIN 1
403 #define RELAY_COMMAND_DATA 2
404 #define RELAY_COMMAND_END 3
405 #define RELAY_COMMAND_CONNECTED 4
406 #define RELAY_COMMAND_SENDME 5
407 #define RELAY_COMMAND_EXTEND 6
408 #define RELAY_COMMAND_EXTENDED 7
409 #define RELAY_COMMAND_TRUNCATE 8
410 #define RELAY_COMMAND_TRUNCATED 9
411 #define RELAY_COMMAND_DROP 10
412 #define RELAY_COMMAND_RESOLVE 11
413 #define RELAY_COMMAND_RESOLVED 12
415 #define RELAY_COMMAND_ESTABLISH_INTRO 32
416 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
417 #define RELAY_COMMAND_INTRODUCE1 34
418 #define RELAY_COMMAND_INTRODUCE2 35
419 #define RELAY_COMMAND_RENDEZVOUS1 36
420 #define RELAY_COMMAND_RENDEZVOUS2 37
421 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
422 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
423 #define RELAY_COMMAND_INTRODUCE_ACK 40
425 #define END_STREAM_REASON_MISC 1
426 #define END_STREAM_REASON_RESOLVEFAILED 2
427 #define END_STREAM_REASON_CONNECTREFUSED 3
428 #define END_STREAM_REASON_EXITPOLICY 4
429 #define END_STREAM_REASON_DESTROY 5
430 #define END_STREAM_REASON_DONE 6
431 #define END_STREAM_REASON_TIMEOUT 7
432 /* 8 is unallocated. */
433 #define END_STREAM_REASON_HIBERNATING 9
434 #define END_STREAM_REASON_INTERNAL 10
435 #define END_STREAM_REASON_RESOURCELIMIT 11
436 #define END_STREAM_REASON_CONNRESET 12
437 #define END_STREAM_REASON_TORPROTOCOL 13
439 /* These high-numbered end reasons are not part of the official spec,
440 * and are not intended to be put in relay end cells. They are here
441 * to be more informative when sending back socks replies to the
443 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
444 #define END_STREAM_REASON_CANT_ATTACH 257
445 #define END_STREAM_REASON_NET_UNREACHABLE 258
447 #define RESOLVED_TYPE_HOSTNAME 0
448 #define RESOLVED_TYPE_IPV4 4
449 #define RESOLVED_TYPE_IPV6 6
450 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
451 #define RESOLVED_TYPE_ERROR 0xF1
453 /** Length of 'y' portion of 'y.onion' URL. */
454 #define REND_SERVICE_ID_LEN 16
456 #define CELL_DIRECTION_IN 1
457 #define CELL_DIRECTION_OUT 2
460 #define CIRCWINDOW_START 10000
461 #define CIRCWINDOW_INCREMENT 1000
462 #define STREAMWINDOW_START 5000
463 #define STREAMWINDOW_INCREMENT 500
465 #define CIRCWINDOW_START 1000
466 #define CIRCWINDOW_INCREMENT 100
467 #define STREAMWINDOW_START 500
468 #define STREAMWINDOW_INCREMENT 50
472 #define CELL_PADDING 0
473 #define CELL_CREATE 1
474 #define CELL_CREATED 2
476 #define CELL_DESTROY 4
477 #define CELL_CREATE_FAST 5
478 #define CELL_CREATED_FAST 6
480 /* people behind fascist firewalls use only these ports */
481 #define REQUIRED_FIREWALL_DIRPORT 80
482 #define REQUIRED_FIREWALL_ORPORT 443
484 /* legal characters in a nickname */
485 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
487 /** Name to use in client TLS certificates if no nickname is given.*/
488 #define DEFAULT_CLIENT_NICKNAME "client"
490 #define SOCKS4_NETWORK_LEN 8
493 SOCKS5_SUCCEEDED
= 0x00,
494 SOCKS5_GENERAL_ERROR
= 0x01,
495 SOCKS5_NOT_ALLOWED
= 0x02,
496 SOCKS5_NET_UNREACHABLE
= 0x03,
497 SOCKS5_HOST_UNREACHABLE
= 0x04,
498 SOCKS5_CONNECTION_REFUSED
= 0x05,
499 SOCKS5_TTL_EXPIRED
= 0x06,
500 SOCKS5_COMMAND_NOT_SUPPORTED
= 0x07,
501 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
= 0x08,
502 } socks5_reply_status_t
;
506 * Relay command [1 byte]
507 * Recognized [2 bytes]
508 * Stream ID [2 bytes]
509 * Partial SHA-1 [4 bytes]
511 * Relay payload [498 bytes]
514 #define CELL_PAYLOAD_SIZE 509
515 #define CELL_NETWORK_SIZE 512
517 #define RELAY_HEADER_SIZE (1+2+2+4+2)
518 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
520 /** Parsed onion routing cell. All communication from OP-to-OR, or from
521 * OR-to-OR, is via cells. */
523 uint16_t circ_id
; /**< Circuit which received the cell. */
524 unsigned char command
; /**< Type of the cell: one of PADDING, CREATE, RELAY,
526 unsigned char payload
[CELL_PAYLOAD_SIZE
]; /**< Cell body. */
529 /** Beginning of a RELAY cell payload. */
531 uint8_t command
; /**< The end-to-end relay command. */
532 uint16_t recognized
; /**< Used to tell whether cell is for us. */
533 uint16_t stream_id
; /**< Which stream is this cell associated with? */
534 char integrity
[4]; /**< Used to tell whether cell is corrupted. */
535 uint16_t length
; /**< How long is the payload body? */
538 typedef struct buf_t buf_t
;
539 typedef struct socks_request_t socks_request_t
;
541 #define CONNECTION_MAGIC 0x7C3C304Eu
542 /** Description of a connection to another host or process, and associated
544 struct connection_t
{
545 uint32_t magic
; /**< For memory debugging: must equal CONNECTION_MAGIC. */
547 uint8_t type
; /**< What kind of connection is this? */
548 uint8_t state
; /**< Current state of this connection. */
549 uint8_t purpose
; /**< Only used for DIR types currently. */
550 uint8_t wants_to_read
; /**< Boolean: should we start reading again once
551 * the bandwidth throttler allows it?
553 uint8_t wants_to_write
; /**< Boolean: should we start writing again once
554 * the bandwidth throttler allows reads?
556 int s
; /**< Our socket; -1 if this connection is closed. */
557 int poll_index
; /* XXXX rename. */
558 struct event
*read_event
; /**< libevent event structure. */
559 struct event
*write_event
; /**< libevent event structure. */
560 int marked_for_close
; /**< Boolean: should we close this conn on the next
561 * iteration of the main loop?
563 const char *marked_for_close_file
; /**< For debugging: in which file were
564 * we marked for close? */
565 int hold_open_until_flushed
; /**< Despite this connection's being marked
566 * for close, do we flush it before closing it?
569 buf_t
*inbuf
; /**< Buffer holding data read over this connection. */
570 int inbuf_reached_eof
; /**< Boolean: did read() return 0 on this conn? */
571 time_t timestamp_lastread
; /**< When was the last time poll() said we could read? */
573 buf_t
*outbuf
; /**< Buffer holding data to write over this connection. */
574 size_t outbuf_flushlen
; /**< How much data should we try to flush from the
576 time_t timestamp_lastwritten
; /**< When was the last time poll() said we could write? */
578 time_t timestamp_created
; /**< When was this connection_t created? */
579 time_t timestamp_lastempty
; /**< When was the outbuf last completely empty?*/
581 uint32_t addr
; /**< IP of the other side of the connection; used to identify
582 * routers, along with port. */
583 uint16_t port
; /**< If non-zero, port on the other end
584 * of the connection. */
585 char *address
; /**< FQDN (or IP) of the guy on the other end.
586 * strdup into this, because free_connection frees it.
588 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for the other side's
590 char identity_digest
[DIGEST_LEN
]; /**< Hash of identity_pkey */
591 char *nickname
; /**< Nickname of OR on other side (if any). */
593 /** Nickname of planned exit node -- to be used with .exit support. */
594 char *chosen_exit_name
;
596 /* Used only by OR connections: */
597 tor_tls
*tls
; /**< TLS connection state (OR only.) */
598 uint16_t next_circ_id
; /**< Which circ_id do we try to use next on
599 * this connection? This is always in the
600 * range 0..1<<15-1. (OR only.)*/
602 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
603 int bandwidth
; /**< Connection bandwidth. (OPEN ORs only.) */
604 int receiver_bucket
; /**< When this hits 0, stop receiving. Every second we
605 * add 'bandwidth' to this, capping it at 10*bandwidth.
608 circ_id_type_t circ_id_type
; /**< When we send CREATE cells along this
609 * connection, which half of the space should
612 /* Used only by DIR and AP connections: */
613 char rend_query
[REND_SERVICE_ID_LEN
+1]; /**< What rendezvous service are we
614 * querying for? (DIR/AP only) */
616 /* Used only by edge connections: */
618 struct connection_t
*next_stream
; /**< Points to the next stream at this
619 * edge, if any (Edge only). */
620 struct crypt_path_t
*cpath_layer
; /**< A pointer to which node in the circ
621 * this conn exits at. (Edge only.) */
622 int package_window
; /**< How many more relay cells can i send into the
623 * circuit? (Edge only.) */
624 int deliver_window
; /**< How many more relay cells can end at me? (Edge
627 int done_sending
; /**< For half-open connections; not used currently. */
628 int done_receiving
; /**< For half-open connections; not used currently. */
629 char has_sent_end
; /**< For debugging: set once we've set the stream end,
630 and check in circuit_about_to_close_connection(). */
631 struct circuit_t
*on_circuit
; /**< DOCDOC */
633 /* Used only by AP connections */
634 socks_request_t
*socks_request
; /**< SOCKS structure describing request (AP
637 /** Quasi-global identifier for this connection; used for control.c */
638 /* XXXX NM This can get re-used after 2**32 circuits. */
639 uint32_t global_identifier
;
641 /* Used only by control connections */
643 uint16_t incoming_cmd_type
;
644 uint32_t incoming_cmd_len
;
645 uint32_t incoming_cmd_cur_len
;
649 typedef struct connection_t connection_t
;
651 #define ADDR_POLICY_ACCEPT 1
652 #define ADDR_POLICY_REJECT 2
654 /** A linked list of policy rules */
655 typedef struct addr_policy_t
{
656 char policy_type
; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
657 char *string
; /**< String representation of this rule. */
658 uint32_t addr
; /**< Base address to accept or reject. */
659 uint32_t msk
; /**< Accept/reject all addresses <b>a</b> such that
660 * a & msk == <b>addr</b> & msk . */
661 uint16_t prt_min
; /**< Lowest port number to accept/reject. */
662 uint16_t prt_max
; /**< Highest port number to accept/reject. */
664 struct addr_policy_t
*next
; /**< Next rule in list. */
667 /** Information about another onion router in the network. */
669 char *signed_descriptor
; /**< The original signed descriptor for this router*/
671 char *address
; /**< Location of OR: either a hostname or an IP address. */
672 char *nickname
; /**< Human-readable OR name. */
674 uint32_t addr
; /**< IPv4 address of OR, in host order. */
675 uint16_t or_port
; /**< Port for OR-to-OR and OP-to-OR connections. */
676 uint16_t dir_port
; /**< Port for HTTP directory connections. */
678 time_t published_on
; /**< When was the information in this routerinfo_t
681 crypto_pk_env_t
*onion_pkey
; /**< Public RSA key for onions. */
682 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for signing. */
683 char identity_digest
[DIGEST_LEN
]; /**< Digest of identity key */
685 char *platform
; /**< What software/operating system is this OR using? */
688 uint32_t bandwidthrate
; /**< How many bytes does this OR add to its token
689 * bucket per second? */
690 uint32_t bandwidthburst
; /**< How large is this OR's token bucket? */
691 /** How many bytes/s is this router known to handle? */
692 uint32_t bandwidthcapacity
;
693 addr_policy_t
*exit_policy
; /**< What streams will this OR permit
695 long uptime
; /**< How many seconds the router claims to have been up */
697 int is_running
; /**< As far as we know, is this OR currently running? */
698 time_t status_set_at
; /**< When did we last update is_running? */
699 int is_verified
; /**< Has a trusted dirserver validated this OR? */
701 smartlist_t
*declared_family
; /**< Nicknames of router which this router
702 * claims are its family. */
704 char *contact_info
; /**< Declared contact info for this router. */
707 /** Contents of a running-routers list */
708 typedef struct running_routers_t
{
709 time_t published_on
; /**< When was the list marked as published? */
710 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
711 smartlist_t
*running_routers
;
714 /** Contents of a directory of onion routers. */
716 /** List of routerinfo_t */
717 smartlist_t
*routers
;
718 /** Which versions of tor are recommended by this directory? */
719 char *software_versions
;
720 /** When was the most recent directory that contributed to this list
724 time_t running_routers_updated_on
;
725 /** What is the most recently received running_routers structure? */
726 running_routers_t
*running_routers
;
727 /** Which router is claimed to have signed it? */
728 char *signing_router
;
731 #define CRYPT_PATH_MAGIC 0x70127012u
733 /** Holds accounting information for a single step in the layered encryption
734 * performed by a circuit. Used only at the client edge of a circuit. */
735 struct crypt_path_t
{
738 /* crypto environments */
739 /** Encryption key and counter for cells heading towards the OR at this
741 crypto_cipher_env_t
*f_crypto
;
742 /** Encryption key and counter for cells heading back from the OR at this
744 crypto_cipher_env_t
*b_crypto
;
746 /** Digest state for cells heading towards the OR at this step. */
747 crypto_digest_env_t
*f_digest
; /* for integrity checking */
748 /** Digest state for cells heading away from the OR at this step. */
749 crypto_digest_env_t
*b_digest
;
751 /** Current state of Diffie-Hellman key negotiation with the OR at this
753 crypto_dh_env_t
*dh_handshake_state
;
754 char fast_handshake_state
[DIGEST_LEN
];
755 /** Negotiated key material shared with the OR at this step. */
756 char handshake_digest
[DIGEST_LEN
];/* KH in tor-spec.txt */
758 /** IP4 address of the OR at this step. */
760 /** Port of the OR at this step. */
762 /** Identity key digest of the OR at this step. */
763 char identity_digest
[DIGEST_LEN
];
765 /** Is the circuit built to this step? Must be one of:
766 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
767 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
768 * and not received an EXTENDED/CREATED)
769 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
771 #define CPATH_STATE_CLOSED 0
772 #define CPATH_STATE_AWAITING_KEYS 1
773 #define CPATH_STATE_OPEN 2
774 struct crypt_path_t
*next
; /**< Link to next crypt_path_t in the circuit.
775 * (The list is circular, so the last node
776 * links to the first.) */
777 struct crypt_path_t
*prev
; /**< Link to previous crypt_path_t in the
780 int package_window
; /**< How many bytes are we allowed to originate ending
782 int deliver_window
; /**< How many bytes are we willing to deliver originating
786 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
788 #define DH_KEY_LEN DH_BYTES
789 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
792 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
793 #define REND_COOKIE_LEN DIGEST_LEN
795 typedef struct crypt_path_t crypt_path_t
;
797 /** Information used to build a circuit. */
799 /** Intended length of the final circuit. */
800 int desired_path_len
;
801 /** Nickname of planned exit node. */
802 char *chosen_exit_name
;
803 /** Identity of planned exit node. */
804 char chosen_exit_digest
[DIGEST_LEN
];
805 /** Whether every node in the circ must have adequate uptime. */
807 /** Whether every node in the circ must have adequate capacity. */
809 /** Whether the last hop was picked with exiting in mind. */
811 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
812 struct crypt_path_t
*pending_final_cpath
;
813 /** How many times has building a circuit for this task failed? */
815 /** At what time should we give up on this task? */
817 } cpath_build_state_t
;
819 #define CIRCUIT_MAGIC 0x35315243u
820 /** Struct for a path (circuit) through the onion routing network. */
822 uint32_t magic
; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
824 int marked_for_close
; /**< Should we close this circuit at the end of the
826 const char *marked_for_close_file
; /**< For debugging: in which file was this
827 * circuit marked for close? */
829 /** The IPv4 address of the OR that is next in this circuit. */
831 /** The port for the OR that is next in this circuit. */
833 /** The OR connection that is previous in this circuit. */
834 connection_t
*p_conn
;
835 /** The OR connection that is next in this circuit. */
836 connection_t
*n_conn
;
837 /** The identity hash of n_conn. */
838 char n_conn_id_digest
[DIGEST_LEN
];
839 /** Linked list of AP streams associated with this circuit. */
840 connection_t
*p_streams
;
841 /** Linked list of Exit streams associated with this circuit. */
842 connection_t
*n_streams
;
843 /** Linked list of Exit streams associated with this circuit that are
844 * still being resolved. */
845 connection_t
*resolving_streams
;
846 /** The next stream_id that will be tried when we're attempting to
847 * construct a new AP stream originating at this circuit. */
848 uint16_t next_stream_id
;
849 /** How many relay data cells can we package (read from edge streams)
850 * on this circuit before we receive a circuit-level sendme cell asking
853 /** How many relay data cells will we deliver (write to edge streams)
854 * on this circuit? When deliver_window gets low, we send some
855 * circuit-level sendme cells to indicate that we're willing to accept
859 /** The circuit_id used in the previous (backward) hop of this circuit. */
861 /** The circuit_id used in the next (forward) hop of this circuit. */
864 /** The cipher used by intermediate hops for cells heading toward the
866 crypto_cipher_env_t
*p_crypto
;
867 /** The cipher used by intermediate hops for cells heading away from
869 crypto_cipher_env_t
*n_crypto
;
871 /** The integrity-checking digest used by intermediate hops, for
872 * cells packaged here and heading towards the OP.
874 crypto_digest_env_t
*p_digest
;
875 /** The integrity-checking digest used by intermediate hops, for
876 * cells packaged at the OP and arriving here.
878 crypto_digest_env_t
*n_digest
;
880 /** Build state for this circuit. It includes the intended path
881 * length, the chosen exit router, rendezvous information, etc.
883 cpath_build_state_t
*build_state
;
884 /** The doubly-linked list of crypt_path_t entries, one per hop,
885 * for this circuit. This includes ciphers for each hop,
886 * integrity-checking digests for each hop, and package/delivery
887 * windows for each hop.
889 * The cpath field is defined only when we are the circuit's origin.
893 /** For storage while passing to cpuworker, or while n_conn is pending. */
894 char onionskin
[ONIONSKIN_CHALLENGE_LEN
];
896 char handshake_digest
[DIGEST_LEN
]; /**< Stores KH for intermediate hops. */
898 time_t timestamp_created
; /**< When was this circuit created? */
899 time_t timestamp_dirty
; /**< When the circuit was first used, or 0 if the
900 * circuit is clean. */
902 uint8_t state
; /**< Current status of this circuit. */
903 uint8_t purpose
; /**< Why are we creating this circuit? */
906 * The rend_query field holds y portion of y.onion (nul-terminated)
907 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
908 * for a hidden service, or is S_*.
910 char rend_query
[REND_SERVICE_ID_LEN
+1];
912 /** The rend_pk_digest field holds a hash of location-hidden service's
913 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
915 char rend_pk_digest
[DIGEST_LEN
];
917 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
918 * C_ESTABLISH_REND. Filled with zeroes otherwise.
920 char rend_cookie
[REND_COOKIE_LEN
];
922 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
923 * is not marked for close. */
924 struct circuit_t
*rend_splice
;
926 /** Quasi-global identifier for this circuit; used for control.c */
927 /* XXXX NM This can get re-used after 2**32 circuits. */
928 uint32_t global_identifier
;
930 struct circuit_t
*next
; /**< Next circuit in linked list. */
933 typedef struct circuit_t circuit_t
;
935 #define ALLOW_UNVERIFIED_ENTRY 1
936 #define ALLOW_UNVERIFIED_EXIT 2
937 #define ALLOW_UNVERIFIED_MIDDLE 4
938 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
939 #define ALLOW_UNVERIFIED_INTRODUCTION 16
941 typedef struct exit_redirect_t
{
952 /** Configuration options for a Tor process */
954 /** What should the tor process actually do? */
956 CMD_RUN_TOR
=0, CMD_LIST_FINGERPRINT
, CMD_HASH_PASSWORD
,
959 const char *command_arg
; /**< Argument for command-line option. */
961 struct config_line_t
*OldLogOptions
; /**< List of configuration lines
962 * for logfiles, old style. */
964 struct config_line_t
*Logs
; /**< New-style list of configuration lines
967 char *DebugLogFile
; /**< Where to send verbose log messages. */
968 char *DataDirectory
; /**< OR only: where to store long-term data. */
969 char *Nickname
; /**< OR only: nickname of this onion router. */
970 char *Address
; /**< OR only: configured address for this onion router. */
971 char *PidFile
; /**< Where to store PID of Tor process. */
973 char *ExitNodes
; /**< Comma-separated list of nicknames of ORs to consider
975 char *EntryNodes
; /**< Comma-separated list of nicknames of ORs to consider
976 * as entry points. */
977 int StrictExitNodes
; /**< Boolean: When none of our ExitNodes are up, do we
978 * stop building circuits? */
979 int StrictEntryNodes
; /**< Boolean: When none of our EntryNodes are up, do we
980 * stop building circuits? */
981 char *ExcludeNodes
; /**< Comma-separated list of nicknames of ORs not to
982 * use in circuits. */
984 char *RendNodes
; /**< Comma-separated list of nicknames used as introduction
986 char *RendExcludeNodes
; /**< Comma-separated list of nicknames not to use
987 * as introduction points. */
989 smartlist_t
*AllowUnverifiedNodes
; /**< List of "entry", "middle", "exit" */
990 int _AllowUnverified
; /**< Bitmask; derived from AllowUnverifiedNodes; */
991 struct config_line_t
*ExitPolicy
; /**< Lists of exit policy components. */
992 struct config_line_t
*SocksPolicy
; /**< Lists of socks policy components */
993 struct config_line_t
*DirPolicy
; /**< Lists of dir policy components */
994 /** Addresses to bind for listening for SOCKS connections. */
995 struct config_line_t
*SocksBindAddress
;
996 /** Addresses to bind for listening for OR connections. */
997 struct config_line_t
*ORBindAddress
;
998 /** Addresses to bind for listening for directory connections. */
999 struct config_line_t
*DirBindAddress
;
1000 /** Local address to bind outbound sockets */
1001 char *OutboundBindAddress
;
1002 /** Directory server only: which versions of
1003 * Tor should we tell users to run? */
1004 struct config_line_t
*RecommendedVersions
;
1005 /** Whether dirservers refuse router descriptors with private IPs. */
1006 int DirAllowPrivateAddresses
;
1007 char *User
; /**< Name of user to run Tor as. */
1008 char *Group
; /**< Name of group to run Tor as. */
1009 double PathlenCoinWeight
; /**< Parameter used to configure average path
1010 * length (alpha in geometric distribution). */
1011 int ORPort
; /**< Port to listen on for OR connections. */
1012 int SocksPort
; /**< Port to listen on for SOCKS connections. */
1013 int ControlPort
; /**< Port to listen on for control connections. */
1014 int DirPort
; /**< Port to listen on for directory connections. */
1015 int AuthoritativeDir
; /**< Boolean: is this an authoritative directory? */
1016 int ClientOnly
; /**< Boolean: should we never evolve into a server role? */
1017 int NoPublish
; /**< Boolean: should we never publish a descriptor? */
1018 int ConnLimit
; /**< Requested maximum number of simultaneous connections. */
1019 int _ConnLimit
; /**< Actual maximum number of simultaneous connections. */
1020 int IgnoreVersion
; /**< If true, run no matter what versions of Tor the
1021 * directory recommends. */
1022 int RunAsDaemon
; /**< If true, run in the background. (Unix only) */
1023 int FascistFirewall
; /**< Whether to prefer ORs reachable on open ports. */
1024 smartlist_t
*FirewallPorts
; /**< Which ports our firewall allows (strings). */
1025 /** Application ports that require all nodes in circ to have sufficient uptime. */
1026 smartlist_t
*LongLivedPorts
;
1027 /** Should we try to reuse the same exit node for a given host */
1028 smartlist_t
*TrackHostExits
;
1029 int TrackHostExitsExpire
; /**< Number of seconds until we expire an addressmap */
1030 struct config_line_t
*AddressMap
; /**< List of address map directives. */
1031 int DirFetchPeriod
; /**< How often do we fetch new directories? */
1032 int DirPostPeriod
; /**< How often do we post our server descriptor to the
1033 * authoritative directory servers? */
1034 int RendPostPeriod
; /**< How often do we post each rendezvous service
1035 * descriptor? Remember to publish them independently. */
1036 int StatusFetchPeriod
; /**< How often do we fetch running-routers lists? */
1037 int KeepalivePeriod
; /**< How often do we send padding cells to keep
1038 * connections alive? */
1039 int MaxOnionsPending
; /**< How many circuit CREATE requests do we allow
1040 * to wait simultaneously before we start dropping
1042 int NewCircuitPeriod
; /**< How long do we use a circuit before building
1044 int MaxCircuitDirtiness
; /**< Never use circs that were first used more than
1045 this interval ago. */
1046 uint64_t BandwidthRate
; /**< How much bandwidth, on average, are we willing to
1047 * use in a second? */
1048 uint64_t BandwidthBurst
; /**< How much bandwidth, at maximum, are we willing to
1049 * use in a second? */
1050 uint64_t MaxAdvertisedBandwidth
; /**< How much bandwidth are we willing to
1051 * tell people we have? */
1052 int NumCpus
; /**< How many CPUs should we try to use? */
1053 int RunTesting
; /**< If true, create testing circuits to measure how well the
1054 * other ORs are running. */
1055 struct config_line_t
*RendConfigLines
; /**< List of configuration lines
1056 * for rendezvous services. */
1057 char *ContactInfo
; /**< Contact info to be published in the directory */
1059 char *HttpProxy
; /**< hostname[:port] to use as http proxy, if any */
1060 uint32_t HttpProxyAddr
; /**< Parsed IPv4 addr for http proxy, if any */
1061 uint16_t HttpProxyPort
; /**< Parsed port for http proxy, if any */
1063 char *HttpsProxy
; /**< hostname[:port] to use as https proxy, if any */
1064 uint32_t HttpsProxyAddr
; /**< Parsed IPv4 addr for https proxy, if any */
1065 uint16_t HttpsProxyPort
; /**< Parsed port for https proxy, if any */
1066 char *HttpsProxyAuthenticator
; /** username:password string, if any */
1068 struct config_line_t
*DirServers
; /**< List of configuration lines
1069 * for directory servers. */
1070 char *MyFamily
; /**< Declared family for this OR. */
1071 struct config_line_t
*NodeFamilies
; /**< List of config lines for
1073 struct config_line_t
*RedirectExit
; /**< List of config lines for simple
1074 * addr/port redirection */
1075 smartlist_t
*RedirectExitList
; /**< List of exit_redirect_t */
1076 int _MonthlyAccountingStart
; /**< Deprecated: day of month when accounting
1077 * interval starts */
1078 char *AccountingStart
; /** How long is the accounting interval, and when
1080 uint64_t AccountingMax
; /**< How many bytes do we allow per accounting
1081 * interval before hibernation? 0 for "never
1083 int _AccountingMaxKB
; /**< How many KB do we allow per accounting
1084 * interval before hibernation? 0 for "never
1085 * hibernate." (Based on a deprecated option)*/
1087 char *HashedControlPassword
; /**< Base64-encoded hash of a password for
1088 * the control system. */
1089 int CookieAuthentication
; /**< Boolean: do we enable cookie-based auth for
1090 * the control system? */
1091 int LeaveStreamsUnattached
; /**< Boolean: Does Tor attach new streams to
1092 * circuits itself (0), or does it expect a controller
1094 int ShutdownWaitLength
; /**< When we get a SIGINT and we're a server, how
1095 * long do we wait before exiting? */
1096 int SafeLogging
; /**< Boolean: are we allowed to log sensitive strings
1097 * such as addresses (0), or do we scrub them first (1)? */
1100 #define MAX_SOCKS_REPLY_LEN 1024
1101 #define MAX_SOCKS_ADDR_LEN 256
1102 #define SOCKS_COMMAND_CONNECT 0x01
1103 #define SOCKS_COMMAND_RESOLVE 0xF0
1104 /** State of a SOCKS request from a user to an OP */
1105 struct socks_request_t
{
1106 char socks_version
; /**< Which version of SOCKS did the client use? */
1107 int command
; /**< What has the user requested? One of CONNECT or RESOLVE. */
1108 size_t replylen
; /**< Length of <b>reply</b>. */
1109 char reply
[MAX_SOCKS_REPLY_LEN
]; /**< Write an entry into this string if
1110 * we want to specify our own socks reply,
1111 * rather than using the default socks4 or
1112 * socks5 socks reply. We use this for the
1113 * two-stage socks5 handshake.
1115 int has_finished
; /**< Has the SOCKS handshake finished? */
1116 char address
[MAX_SOCKS_ADDR_LEN
]; /**< What address did the client ask to connect to? */
1117 uint16_t port
; /**< What port did the client ask to connect to? */
1120 /* all the function prototypes go here */
1122 /********************************* buffers.c ***************************/
1124 buf_t
*buf_new(void);
1125 buf_t
*buf_new_with_capacity(size_t size
);
1126 void buf_free(buf_t
*buf
);
1127 void buf_clear(buf_t
*buf
);
1128 void buf_shrink(buf_t
*buf
);
1130 size_t buf_datalen(const buf_t
*buf
);
1131 size_t buf_capacity(const buf_t
*buf
);
1132 const char *_buf_peek_raw_buffer(const buf_t
*buf
);
1134 int read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
);
1135 int read_to_buf_tls(tor_tls
*tls
, size_t at_most
, buf_t
*buf
);
1137 int flush_buf(int s
, buf_t
*buf
, size_t *buf_flushlen
);
1138 int flush_buf_tls(tor_tls
*tls
, buf_t
*buf
, size_t *buf_flushlen
);
1140 int write_to_buf(const char *string
, size_t string_len
, buf_t
*buf
);
1141 int fetch_from_buf(char *string
, size_t string_len
, buf_t
*buf
);
1142 int fetch_from_buf_http(buf_t
*buf
,
1143 char **headers_out
, size_t max_headerlen
,
1144 char **body_out
, size_t *body_used
, size_t max_bodylen
);
1145 int fetch_from_buf_socks(buf_t
*buf
, socks_request_t
*req
);
1146 int fetch_from_buf_control(buf_t
*buf
, uint32_t *len_out
, uint16_t *type_out
,
1149 void assert_buf_ok(buf_t
*buf
);
1151 /********************************* circuitbuild.c **********************/
1153 char *circuit_list_path(circuit_t
*circ
, int verbose
);
1154 void circuit_log_path(int severity
, circuit_t
*circ
);
1155 void circuit_rep_hist_note_result(circuit_t
*circ
);
1156 void circuit_dump_by_conn(connection_t
*conn
, int severity
);
1157 circuit_t
*circuit_init(uint8_t purpose
, int need_uptime
,
1158 int need_capacity
, int internal
);
1159 circuit_t
*circuit_establish_circuit(uint8_t purpose
, routerinfo_t
*exit
,
1160 int need_uptime
, int need_capacity
, int internal
);
1161 int circuit_handle_first_hop(circuit_t
*circ
);
1162 void circuit_n_conn_done(connection_t
*or_conn
, int status
);
1163 int circuit_send_next_onion_skin(circuit_t
*circ
);
1164 void circuit_note_clock_jumped(int seconds_elapsed
);
1165 int circuit_extend(cell_t
*cell
, circuit_t
*circ
);
1166 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
, int reverse
);
1167 int circuit_finish_handshake(circuit_t
*circ
, uint8_t cell_type
, char *reply
);
1168 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
1169 int onionskin_answer(circuit_t
*circ
, uint8_t cell_type
, unsigned char *payload
, unsigned char *keys
);
1170 int circuit_all_predicted_ports_handled(time_t now
, int *need_uptime
,
1171 int *need_capacity
);
1173 int circuit_append_new_exit(circuit_t
*circ
, routerinfo_t
*exit
);
1174 int circuit_extend_to_new_exit(circuit_t
*circ
, routerinfo_t
*exit
);
1175 void onion_append_to_cpath(crypt_path_t
**head_ptr
, crypt_path_t
*new_hop
);
1177 /********************************* circuitlist.c ***********************/
1179 const char *circuit_state_to_string(int state
);
1180 enum which_conn_changed_t
{ P_CONN_CHANGED
=1, N_CONN_CHANGED
=0 };
1181 void circuit_set_circid_orconn(circuit_t
*circ
, uint16_t id
,
1183 enum which_conn_changed_t which
);
1184 void circuit_close_all_marked(void);
1185 circuit_t
*circuit_new(uint16_t p_circ_id
, connection_t
*p_conn
);
1186 circuit_t
*circuit_get_by_circid_orconn(uint16_t circ_id
, connection_t
*conn
);
1187 circuit_t
*circuit_get_by_edge_conn(connection_t
*conn
);
1188 circuit_t
*circuit_get_by_conn(connection_t
*conn
);
1189 circuit_t
*circuit_get_by_global_id(uint32_t id
);
1190 circuit_t
*circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
);
1191 circuit_t
*circuit_get_next_by_pk_and_purpose(circuit_t
*start
,
1192 const char *digest
, uint8_t purpose
);
1193 circuit_t
*circuit_get_rendezvous(const char *cookie
);
1194 circuit_t
*circuit_get_clean_open(uint8_t purpose
, int need_uptime
,
1195 int need_capacity
, int internal
);
1196 void circuit_mark_all_unused_circs(void);
1197 void _circuit_mark_for_close(circuit_t
*circ
, int line
, const char *file
);
1199 #define circuit_mark_for_close(c) \
1200 _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
1202 void assert_cpath_layer_ok(const crypt_path_t
*cp
);
1203 void assert_circuit_ok(const circuit_t
*c
);
1204 void circuit_free_all(void);
1206 /********************************* circuituse.c ************************/
1208 void circuit_expire_building(time_t now
);
1209 void circuit_remove_handled_ports(smartlist_t
*needed_ports
);
1210 int circuit_stream_is_being_handled(connection_t
*conn
, uint16_t port
, int min
);
1211 void circuit_build_needed_circs(time_t now
);
1212 void circuit_detach_stream(circuit_t
*circ
, connection_t
*conn
);
1213 void circuit_about_to_close_connection(connection_t
*conn
);
1214 void circuit_has_opened(circuit_t
*circ
);
1215 void circuit_build_failed(circuit_t
*circ
);
1216 circuit_t
*circuit_launch_by_nickname(uint8_t purpose
, const char *exit_nickname
,
1217 int need_uptime
, int need_capacity
, int is_internal
);
1218 circuit_t
*circuit_launch_by_router(uint8_t purpose
, routerinfo_t
*exit
,
1219 int need_uptime
, int need_capacity
, int is_internal
);
1220 void circuit_reset_failure_count(int timeout
);
1221 int connection_ap_handshake_attach_chosen_circuit(connection_t
*conn
,
1223 int connection_ap_handshake_attach_circuit(connection_t
*conn
);
1225 /********************************* command.c ***************************/
1227 void command_process_cell(cell_t
*cell
, connection_t
*conn
);
1229 extern unsigned long stats_n_padding_cells_processed
;
1230 extern unsigned long stats_n_create_cells_processed
;
1231 extern unsigned long stats_n_created_cells_processed
;
1232 extern unsigned long stats_n_relay_cells_processed
;
1233 extern unsigned long stats_n_destroy_cells_processed
;
1235 /********************************* config.c ***************************/
1237 struct config_line_t
{
1240 struct config_line_t
*next
;
1243 or_options_t
*get_options(void);
1244 void set_options(or_options_t
*new_val
);
1245 int options_act(void);
1246 void config_free_all(void);
1247 const char *safe_str(const char *address
);
1249 int config_get_lines(char *string
, struct config_line_t
**result
);
1250 void config_free_lines(struct config_line_t
*front
);
1251 int config_trial_assign(struct config_line_t
*list
, int reset
);
1252 int resolve_my_address(const char *address
, uint32_t *addr
);
1253 void options_init(or_options_t
*options
);
1254 int init_from_config(int argc
, char **argv
);
1255 int config_init_logs(or_options_t
*options
, int validate_only
);
1256 int config_parse_addr_policy(struct config_line_t
*cfg
,
1257 addr_policy_t
**dest
);
1258 void addr_policy_free(addr_policy_t
*p
);
1259 int config_option_is_recognized(const char *key
);
1260 struct config_line_t
*config_get_assigned_option(or_options_t
*options
,
1262 char *config_dump_options(or_options_t
*options
, int minimal
);
1263 int save_current_config(void);
1265 /********************************* connection.c ***************************/
1267 const char *conn_type_to_string(int type
);
1268 const char *conn_state_to_string(int type
, int state
);
1270 connection_t
*connection_new(int type
);
1271 void connection_unregister(connection_t
*conn
);
1272 void connection_free(connection_t
*conn
);
1273 void connection_free_all(void);
1274 void connection_about_to_close_connection(connection_t
*conn
);
1275 void connection_close_immediate(connection_t
*conn
);
1276 void _connection_mark_for_close(connection_t
*conn
,int line
, const char *file
);
1278 #define connection_mark_for_close(c) \
1279 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1281 void connection_expire_held_open(void);
1283 int connection_connect(connection_t
*conn
, char *address
, uint32_t addr
, uint16_t port
);
1284 int retry_all_listeners(int force
);
1286 void connection_bucket_init(void);
1287 void connection_bucket_refill(struct timeval
*now
);
1289 int connection_handle_read(connection_t
*conn
);
1291 int connection_fetch_from_buf(char *string
, size_t len
, connection_t
*conn
);
1293 int connection_wants_to_flush(connection_t
*conn
);
1294 int connection_outbuf_too_full(connection_t
*conn
);
1295 int connection_handle_write(connection_t
*conn
);
1296 void _connection_controller_force_write(connection_t
*conn
);
1297 void connection_write_to_buf(const char *string
, size_t len
, connection_t
*conn
);
1299 connection_t
*connection_or_exact_get_by_addr_port(uint32_t addr
, uint16_t port
);
1300 connection_t
*connection_get_by_identity_digest(const char *digest
, int type
);
1301 connection_t
*connection_get_by_global_id(uint32_t id
);
1303 connection_t
*connection_get_by_type(int type
);
1304 connection_t
*connection_get_by_type_state(int type
, int state
);
1305 connection_t
*connection_get_by_type_purpose(int type
, int purpose
);
1306 connection_t
*connection_get_by_type_state_lastwritten(int type
, int state
);
1307 connection_t
*connection_get_by_type_state_rendquery(int type
, int state
, const char *rendquery
);
1309 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1310 #define connection_has_pending_tls_data(conn) \
1311 ((conn)->type == CONN_TYPE_OR && \
1312 (conn)->state == OR_CONN_STATE_OPEN && \
1313 tor_tls_get_pending_bytes((conn)->tls))
1314 int connection_is_listener(connection_t
*conn
);
1315 int connection_state_is_open(connection_t
*conn
);
1316 int connection_state_is_connecting(connection_t
*conn
);
1318 int connection_send_destroy(uint16_t circ_id
, connection_t
*conn
);
1320 void assert_connection_ok(connection_t
*conn
, time_t now
);
1321 int connection_or_nonopen_was_started_here(connection_t
*conn
);
1323 /********************************* connection_edge.c ***************************/
1325 #define connection_mark_unattached_ap(conn, endreason) \
1326 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1328 void _connection_mark_unattached_ap(connection_t
*conn
, int endreason
,
1329 int line
, const char *file
);
1330 int connection_edge_reached_eof(connection_t
*conn
);
1331 int connection_edge_process_inbuf(connection_t
*conn
, int package_partial
);
1332 int connection_edge_destroy(uint16_t circ_id
, connection_t
*conn
);
1333 int connection_edge_end(connection_t
*conn
, char reason
, crypt_path_t
*cpath_layer
);
1334 int connection_edge_end_errno(connection_t
*conn
, crypt_path_t
*cpath_layer
);
1335 int connection_edge_finished_flushing(connection_t
*conn
);
1336 int connection_edge_finished_connecting(connection_t
*conn
);
1338 int connection_ap_handshake_send_begin(connection_t
*ap_conn
, circuit_t
*circ
);
1339 int connection_ap_handshake_send_resolve(connection_t
*ap_conn
, circuit_t
*circ
);
1341 int connection_ap_make_bridge(char *address
, uint16_t port
);
1342 void connection_ap_handshake_socks_reply(connection_t
*conn
, char *reply
,
1344 socks5_reply_status_t status
);
1345 void connection_ap_handshake_socks_resolved(connection_t
*conn
,
1348 const char *answer
);
1350 int connection_exit_begin_conn(cell_t
*cell
, circuit_t
*circ
);
1351 int connection_exit_begin_resolve(cell_t
*cell
, circuit_t
*circ
);
1352 void connection_exit_connect(connection_t
*conn
);
1353 int connection_edge_is_rendezvous_stream(connection_t
*conn
);
1354 int connection_ap_can_use_exit(connection_t
*conn
, routerinfo_t
*exit
);
1355 void connection_ap_expire_beginning(void);
1356 void connection_ap_attach_pending(void);
1357 int connection_ap_detach_retriable(connection_t
*conn
, circuit_t
*circ
);
1359 void addressmap_init(void);
1360 void addressmap_clean(time_t now
);
1361 void addressmap_clear_configured(void);
1362 void addressmap_clear_transient(void);
1363 void addressmap_free_all(void);
1364 void addressmap_rewrite(char *address
, size_t maxlen
);
1365 int addressmap_already_mapped(const char *address
);
1366 void addressmap_register(const char *address
, char *new_address
, time_t expires
);
1367 int client_dns_incr_failures(const char *address
);
1368 void client_dns_set_addressmap(const char *address
, uint32_t val
, const char *exitname
);
1369 const char *addressmap_register_virtual_address(int type
, char *new_address
);
1370 void addressmap_get_mappings(smartlist_t
*sl
, time_t min_expires
, time_t max_expires
);
1372 void parse_socks_policy(void);
1373 void free_socks_policy(void);
1374 int socks_policy_permits_address(uint32_t addr
);
1376 void set_exit_redirects(smartlist_t
*lst
);
1377 typedef enum hostname_type_t
{
1378 NORMAL_HOSTNAME
, ONION_HOSTNAME
, EXIT_HOSTNAME
1380 hostname_type_t
parse_extended_hostname(char *address
);
1382 /********************************* connection_or.c ***************************/
1384 int connection_or_reached_eof(connection_t
*conn
);
1385 int connection_or_process_inbuf(connection_t
*conn
);
1386 int connection_or_finished_flushing(connection_t
*conn
);
1387 int connection_or_finished_connecting(connection_t
*conn
);
1389 connection_t
*connection_or_connect(uint32_t addr
, uint16_t port
,
1390 const char *id_digest
);
1392 int connection_tls_start_handshake(connection_t
*conn
, int receiving
);
1393 int connection_tls_continue_handshake(connection_t
*conn
);
1395 void connection_or_write_cell_to_buf(const cell_t
*cell
, connection_t
*conn
);
1396 void connection_or_update_nickname(connection_t
*conn
);
1398 /********************************* control.c ***************************/
1400 typedef enum circuit_status_event_t
{
1401 CIRC_EVENT_LAUNCHED
= 0,
1402 CIRC_EVENT_BUILT
= 1,
1403 CIRC_EVENT_EXTENDED
= 2,
1404 CIRC_EVENT_FAILED
= 3,
1405 CIRC_EVENT_CLOSED
= 4,
1406 } circuit_status_event_t
;
1408 typedef enum stream_status_event_t
{
1409 STREAM_EVENT_SENT_CONNECT
= 0,
1410 STREAM_EVENT_SENT_RESOLVE
= 1,
1411 STREAM_EVENT_SUCCEEDED
= 2,
1412 STREAM_EVENT_FAILED
= 3,
1413 STREAM_EVENT_CLOSED
= 4,
1414 STREAM_EVENT_NEW
= 5,
1415 STREAM_EVENT_NEW_RESOLVE
= 6,
1416 STREAM_EVENT_FAILED_RETRIABLE
= 7
1417 } stream_status_event_t
;
1419 typedef enum or_conn_status_event_t
{
1420 OR_CONN_EVENT_LAUNCHED
= 0,
1421 OR_CONN_EVENT_CONNECTED
= 1,
1422 OR_CONN_EVENT_FAILED
= 2,
1423 OR_CONN_EVENT_CLOSED
= 3,
1424 } or_conn_status_event_t
;
1426 void adjust_event_log_severity(void);
1427 int connection_control_finished_flushing(connection_t
*conn
);
1428 int connection_control_reached_eof(connection_t
*conn
);
1429 int connection_control_process_inbuf(connection_t
*conn
);
1431 int control_event_circuit_status(circuit_t
*circ
, circuit_status_event_t e
);
1432 int control_event_stream_status(connection_t
*conn
, stream_status_event_t e
);
1433 int control_event_or_conn_status(connection_t
*conn
, or_conn_status_event_t e
);
1434 int control_event_bandwidth_used(uint32_t n_read
, uint32_t n_written
);
1435 void control_event_logmsg(int severity
, const char *msg
);
1436 int control_event_descriptors_changed(smartlist_t
*routers
);
1438 int init_cookie_authentication(int enabled
);
1439 int decode_hashed_password(char *buf
, const char *hashed
);
1441 /********************************* cpuworker.c *****************************/
1443 void cpu_init(void);
1444 void cpuworkers_rotate(void);
1445 int connection_cpu_finished_flushing(connection_t
*conn
);
1446 int connection_cpu_reached_eof(connection_t
*conn
);
1447 int connection_cpu_process_inbuf(connection_t
*conn
);
1448 int assign_to_cpuworker(connection_t
*cpuworker
, unsigned char question_type
,
1451 /********************************* directory.c ***************************/
1453 int dir_policy_permits_address(uint32_t addr
);
1454 void directory_post_to_dirservers(uint8_t purpose
, const char *payload
,
1455 size_t payload_len
);
1456 void directory_get_from_dirserver(uint8_t purpose
, const char *resource
,
1457 int retry_if_no_servers
);
1458 void directory_initiate_command_router(routerinfo_t
*router
, uint8_t purpose
,
1459 int private_connection
, const char *resource
,
1460 const char *payload
, size_t payload_len
);
1462 int parse_http_response(const char *headers
, int *code
, time_t *date
,
1463 int *compression
, char **response
);
1465 int connection_dir_reached_eof(connection_t
*conn
);
1466 int connection_dir_process_inbuf(connection_t
*conn
);
1467 int connection_dir_finished_flushing(connection_t
*conn
);
1468 int connection_dir_finished_connecting(connection_t
*conn
);
1469 void connection_dir_connect_failed(connection_t
*conn
);
1470 void parse_dir_policy(void);
1471 void free_dir_policy(void);
1473 /********************************* dirserv.c ***************************/
1475 int dirserv_add_own_fingerprint(const char *nickname
, crypto_pk_env_t
*pk
);
1476 int dirserv_parse_fingerprint_file(const char *fname
);
1477 int dirserv_router_fingerprint_is_known(const routerinfo_t
*router
);
1478 void dirserv_free_fingerprint_list(void);
1479 const char *dirserv_get_nickname_by_digest(const char *digest
);
1480 int dirserv_add_descriptor(const char **desc
, const char **msg
);
1481 int dirserv_load_from_directory_string(const char *dir
);
1482 void dirserv_free_descriptors(void);
1483 int list_server_status(smartlist_t
*routers
, char **router_status_out
);
1484 void dirserv_remove_old_servers(int age
);
1485 int dirserv_dump_directory_to_string(char **dir_out
,
1486 crypto_pk_env_t
*private_key
);
1487 void directory_set_dirty(void);
1488 size_t dirserv_get_directory(const char **cp
, int compress
);
1489 size_t dirserv_get_runningrouters(const char **rr
, int compress
);
1490 void dirserv_set_cached_directory(const char *directory
, time_t when
,
1491 int is_running_routers
);
1492 void dirserv_orconn_tls_done(const char *address
,
1494 const char *digest_rcvd
,
1495 const char *nickname
);
1496 void dirserv_free_all(void);
1498 /********************************* dns.c ***************************/
1500 void dns_init(void);
1501 void dns_free_all(void);
1502 int connection_dns_finished_flushing(connection_t
*conn
);
1503 int connection_dns_reached_eof(connection_t
*conn
);
1504 int connection_dns_process_inbuf(connection_t
*conn
);
1505 void dnsworkers_rotate(void);
1506 void connection_dns_remove(connection_t
*conn
);
1507 void assert_connection_edge_not_dns_pending(connection_t
*conn
);
1508 void assert_all_pending_dns_resolves_ok(void);
1509 void dns_cancel_pending_resolve(char *question
);
1510 int dns_resolve(connection_t
*exitconn
);
1512 /********************************* hibernate.c **********************/
1514 int accounting_parse_options(or_options_t
*options
, int validate_only
);
1515 int accounting_is_enabled(or_options_t
*options
);
1516 void configure_accounting(time_t now
);
1517 void accounting_run_housekeeping(time_t now
);
1518 void accounting_add_bytes(size_t n_read
, size_t n_written
, int seconds
);
1519 int accounting_record_bandwidth_usage(time_t now
);
1520 void hibernate_begin_shutdown(void);
1521 int we_are_hibernating(void);
1522 void consider_hibernation(time_t now
);
1524 /********************************* main.c ***************************/
1526 int connection_add(connection_t
*conn
);
1527 int connection_remove(connection_t
*conn
);
1528 int connection_in_array(connection_t
*conn
);
1529 void add_connection_to_closeable_list(connection_t
*conn
);
1530 int connection_is_on_closeable_list(connection_t
*conn
);
1532 void get_connection_array(connection_t
***array
, int *n
);
1534 void connection_watch_events(connection_t
*conn
, short events
);
1535 int connection_is_reading(connection_t
*conn
);
1536 void connection_stop_reading(connection_t
*conn
);
1537 void connection_start_reading(connection_t
*conn
);
1539 int connection_is_writing(connection_t
*conn
);
1540 void connection_stop_writing(connection_t
*conn
);
1541 void connection_start_writing(connection_t
*conn
);
1543 void directory_all_unreachable(time_t now
);
1544 void directory_has_arrived(time_t now
, char *identity_digest
);
1546 int control_signal_act(int the_signal
);
1547 void handle_signals(int is_parent
);
1548 void tor_cleanup(void);
1549 void tor_free_all(int postfork
);
1551 int tor_main(int argc
, char *argv
[]);
1553 /********************************* onion.c ***************************/
1555 int onion_pending_add(circuit_t
*circ
);
1556 circuit_t
*onion_next_task(void);
1557 void onion_pending_remove(circuit_t
*circ
);
1559 int onion_skin_create(crypto_pk_env_t
*router_key
,
1560 crypto_dh_env_t
**handshake_state_out
,
1561 char *onion_skin_out
);
1563 int onion_skin_server_handshake(const char *onion_skin
,
1564 crypto_pk_env_t
*private_key
,
1565 crypto_pk_env_t
*prev_private_key
,
1566 char *handshake_reply_out
,
1568 size_t key_out_len
);
1570 int onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
1571 const char *handshake_reply
,
1573 size_t key_out_len
);
1575 int fast_server_handshake(const char *key_in
,
1576 char *handshake_reply_out
,
1578 size_t key_out_len
);
1580 int fast_client_handshake(const char *handshake_state
,
1581 const char *handshake_reply_out
,
1583 size_t key_out_len
);
1585 void clear_pending_onions(void);
1587 /********************************* relay.c ***************************/
1589 extern unsigned long stats_n_relay_cells_relayed
;
1590 extern unsigned long stats_n_relay_cells_delivered
;
1592 int circuit_receive_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1593 int cell_direction
);
1595 void relay_header_pack(char *dest
, const relay_header_t
*src
);
1596 void relay_header_unpack(relay_header_t
*dest
, const char *src
);
1597 int connection_edge_send_command(connection_t
*fromconn
, circuit_t
*circ
,
1598 int relay_command
, const char *payload
,
1599 size_t payload_len
, crypt_path_t
*cpath_layer
);
1600 int connection_edge_package_raw_inbuf(connection_t
*conn
, int package_partial
);
1601 void connection_edge_consider_sending_sendme(connection_t
*conn
);
1602 socks5_reply_status_t
connection_edge_end_reason_socks5_response(int reason
);
1603 int errno_to_end_reason(int e
);
1605 extern uint64_t stats_n_data_cells_packaged
;
1606 extern uint64_t stats_n_data_bytes_packaged
;
1607 extern uint64_t stats_n_data_cells_received
;
1608 extern uint64_t stats_n_data_bytes_received
;
1610 /********************************* rephist.c ***************************/
1612 void rep_hist_init(void);
1613 void rep_hist_note_connect_failed(const char* nickname
, time_t when
);
1614 void rep_hist_note_connect_succeeded(const char* nickname
, time_t when
);
1615 void rep_hist_note_disconnect(const char* nickname
, time_t when
);
1616 void rep_hist_note_connection_died(const char* nickname
, time_t when
);
1617 void rep_hist_note_extend_succeeded(const char *from_name
,
1618 const char *to_name
);
1619 void rep_hist_note_extend_failed(const char *from_name
, const char *to_name
);
1620 void rep_hist_dump_stats(time_t now
, int severity
);
1621 void rep_hist_note_bytes_read(int num_bytes
, time_t when
);
1622 void rep_hist_note_bytes_written(int num_bytes
, time_t when
);
1623 int rep_hist_bandwidth_assess(void);
1624 char *rep_hist_get_bandwidth_lines(void);
1625 void rep_history_clean(time_t before
);
1627 void rep_hist_note_used_port(uint16_t port
, time_t now
);
1628 smartlist_t
*rep_hist_get_predicted_ports(time_t now
);
1629 void rep_hist_note_used_hidserv(time_t now
, int need_uptime
, int need_capacity
);
1630 int rep_hist_get_predicted_hidserv(time_t now
, int *need_uptime
, int *need_capacity
);
1631 void rep_hist_note_used_resolve(time_t now
);
1632 int rep_hist_get_predicted_resolve(time_t now
);
1634 void rep_hist_free_all(void);
1636 /********************************* rendclient.c ***************************/
1638 void rend_client_introcirc_has_opened(circuit_t
*circ
);
1639 void rend_client_rendcirc_has_opened(circuit_t
*circ
);
1640 int rend_client_introduction_acked(circuit_t
*circ
, const char *request
, size_t request_len
);
1641 void rend_client_refetch_renddesc(const char *query
);
1642 int rend_client_remove_intro_point(char *failed_intro
, const char *query
);
1643 int rend_client_rendezvous_acked(circuit_t
*circ
, const char *request
, size_t request_len
);
1644 int rend_client_receive_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1645 void rend_client_desc_here(char *query
);
1647 char *rend_client_get_random_intro(char *query
);
1649 int rend_client_send_introduction(circuit_t
*introcirc
, circuit_t
*rendcirc
);
1651 /********************************* rendcommon.c ***************************/
1653 typedef struct rend_service_descriptor_t
{
1654 crypto_pk_env_t
*pk
;
1657 char **intro_points
;
1658 } rend_service_descriptor_t
;
1660 int rend_cmp_service_ids(const char *one
, const char *two
);
1662 void rend_process_relay_cell(circuit_t
*circ
, int command
, size_t length
,
1663 const char *payload
);
1665 void rend_service_descriptor_free(rend_service_descriptor_t
*desc
);
1666 int rend_encode_service_descriptor(rend_service_descriptor_t
*desc
,
1667 crypto_pk_env_t
*key
,
1670 rend_service_descriptor_t
*rend_parse_service_descriptor(const char *str
, size_t len
);
1671 int rend_get_service_id(crypto_pk_env_t
*pk
, char *out
);
1673 typedef struct rend_cache_entry_t
{
1674 size_t len
; /* Length of desc */
1675 time_t received
; /* When did we get the descriptor? */
1676 char *desc
; /* Service descriptor */
1677 rend_service_descriptor_t
*parsed
; /* Parsed value of 'desc' */
1678 } rend_cache_entry_t
;
1680 void rend_cache_init(void);
1681 void rend_cache_clean(void);
1682 void rend_cache_free_all(void);
1683 int rend_valid_service_id(const char *query
);
1684 int rend_cache_lookup_desc(const char *query
, const char **desc
, size_t *desc_len
);
1685 int rend_cache_lookup_entry(const char *query
, rend_cache_entry_t
**entry_out
);
1686 int rend_cache_store(const char *desc
, size_t desc_len
);
1688 /********************************* rendservice.c ***************************/
1690 int rend_config_services(or_options_t
*options
, int validate_only
);
1691 int rend_service_load_keys(void);
1692 void rend_services_init(void);
1693 void rend_services_introduce(void);
1694 void rend_consider_services_upload(time_t now
);
1696 void rend_service_intro_has_opened(circuit_t
*circuit
);
1697 int rend_service_intro_established(circuit_t
*circuit
, const char *request
, size_t request_len
);
1698 void rend_service_rendezvous_has_opened(circuit_t
*circuit
);
1699 int rend_service_introduce(circuit_t
*circuit
, const char *request
, size_t request_len
);
1700 void rend_service_relaunch_rendezvous(circuit_t
*oldcirc
);
1701 int rend_service_set_connection_addr_port(connection_t
*conn
, circuit_t
*circ
);
1702 void rend_service_dump_stats(int severity
);
1703 void rend_service_free_all(void);
1705 /********************************* rendmid.c *******************************/
1706 int rend_mid_establish_intro(circuit_t
*circ
, const char *request
, size_t request_len
);
1707 int rend_mid_introduce(circuit_t
*circ
, const char *request
, size_t request_len
);
1708 int rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1709 int rend_mid_rendezvous(circuit_t
*circ
, const char *request
, size_t request_len
);
1711 /********************************* router.c ***************************/
1713 ADDR_POLICY_ACCEPTED
=0,
1714 ADDR_POLICY_REJECTED
=-1,
1715 ADDR_POLICY_PROBABLY_ACCEPTED
=1,
1716 ADDR_POLICY_PROBABLY_REJECTED
=2
1717 } addr_policy_result_t
;
1719 void set_onion_key(crypto_pk_env_t
*k
);
1720 crypto_pk_env_t
*get_onion_key(void);
1721 crypto_pk_env_t
*get_previous_onion_key(void);
1722 time_t get_onion_key_set_at(void);
1723 void set_identity_key(crypto_pk_env_t
*k
);
1724 crypto_pk_env_t
*get_identity_key(void);
1725 int identity_key_is_set(void);
1726 void dup_onion_keys(crypto_pk_env_t
**key
, crypto_pk_env_t
**last
);
1727 void rotate_onion_key(void);
1728 crypto_pk_env_t
*init_key_from_file(const char *fname
);
1729 int init_keys(void);
1731 int check_whether_orport_reachable(void);
1732 int check_whether_dirport_reachable(void);
1733 void consider_testing_reachability(void);
1734 void router_orport_found_reachable(void);
1735 void router_dirport_found_reachable(void);
1736 void server_has_changed_ip(void);
1737 void consider_publishable_server(time_t now
, int force
);
1739 int authdir_mode(or_options_t
*options
);
1740 int clique_mode(or_options_t
*options
);
1741 int server_mode(or_options_t
*options
);
1742 int advertised_server_mode(void);
1743 int proxy_mode(or_options_t
*options
);
1745 void router_retry_connections(void);
1746 int router_is_clique_mode(routerinfo_t
*router
);
1747 void router_upload_dir_desc_to_dirservers(int force
);
1748 void mark_my_descriptor_dirty(void);
1749 int router_compare_to_my_exit_policy(connection_t
*conn
);
1750 routerinfo_t
*router_get_my_routerinfo(void);
1751 const char *router_get_my_descriptor(void);
1752 int router_is_me(routerinfo_t
*router
);
1753 int router_rebuild_descriptor(int force
);
1754 int router_dump_router_to_string(char *s
, size_t maxlen
, routerinfo_t
*router
,
1755 crypto_pk_env_t
*ident_key
);
1756 int is_legal_nickname(const char *s
);
1757 int is_legal_nickname_or_hexdigest(const char *s
);
1758 void router_free_all_keys(void);
1760 /********************************* routerlist.c ***************************/
1762 typedef struct trusted_dir_server_t
{
1766 char digest
[DIGEST_LEN
];
1768 } trusted_dir_server_t
;
1770 int router_reload_router_list(void);
1771 void router_get_trusted_dir_servers(smartlist_t
**outp
);
1772 routerinfo_t
*router_pick_directory_server(int requireothers
,
1773 int fascistfirewall
,
1774 int for_running_routers
,
1775 int retry_if_no_servers
);
1776 trusted_dir_server_t
*router_pick_trusteddirserver(int requireothers
,
1777 int fascistfirewall
,
1778 int retry_if_no_servers
);
1779 int all_trusted_directory_servers_down(void);
1781 void routerlist_add_family(struct smartlist_t
*sl
, routerinfo_t
*router
);
1782 void add_nickname_list_to_smartlist(struct smartlist_t
*sl
, const char *list
, int warn_if_down
);
1783 int router_nickname_is_in_list(routerinfo_t
*router
, const char *list
);
1784 routerinfo_t
*routerlist_find_my_routerinfo(void);
1785 int router_nickname_matches(routerinfo_t
*router
, const char *nickname
);
1786 int exit_policy_implicitly_allows_local_networks(addr_policy_t
*policy
,
1789 /** How many seconds a router must be up before we'll use it for
1790 * reliability-critical node positions.
1792 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
1793 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
1795 int router_is_unreliable(routerinfo_t
*router
, int need_uptime
, int need_capacity
);
1796 routerinfo_t
*routerlist_sl_choose_by_bandwidth(smartlist_t
*sl
);
1797 routerinfo_t
*router_choose_random_node(const char *preferred
,
1798 const char *excluded
,
1799 struct smartlist_t
*excludedsmartlist
,
1800 int need_uptime
, int need_bandwidth
,
1801 int allow_unverified
, int strict
);
1802 routerinfo_t
*router_get_by_addr_port(uint32_t addr
, uint16_t port
);
1803 routerinfo_t
*router_get_by_nickname(const char *nickname
);
1804 routerinfo_t
*router_get_by_hexdigest(const char *hexdigest
);
1805 routerinfo_t
*router_get_by_digest(const char *digest
);
1806 int router_digest_is_trusted_dir(const char *digest
);
1807 void router_get_routerlist(routerlist_t
**prouterlist
);
1808 time_t routerlist_get_published_time(void);
1809 void routerlist_free(routerlist_t
*routerlist
);
1810 void routerinfo_free(routerinfo_t
*router
);
1811 void routerlist_free_current(void);
1812 void free_trusted_dir_servers(void);
1813 routerinfo_t
*routerinfo_copy(const routerinfo_t
*router
);
1814 void router_mark_as_down(const char *digest
);
1815 void routerlist_remove_old_routers(int age
);
1816 int router_load_single_router(const char *s
, const char **msg
);
1817 int router_load_routerlist_from_directory(const char *s
,crypto_pk_env_t
*pkey
,
1818 int dir_is_recent
, int dir_is_cached
);
1819 addr_policy_result_t
router_compare_addr_to_addr_policy(uint32_t addr
,
1820 uint16_t port
, addr_policy_t
*policy
);
1822 int router_exit_policy_all_routers_reject(uint32_t addr
, uint16_t port
,
1825 int router_exit_policy_rejects_all(routerinfo_t
*router
);
1826 void running_routers_free(running_routers_t
*rr
);
1827 void routerlist_set_runningrouters(routerlist_t
*list
, running_routers_t
*rr
);
1828 void routerlist_update_from_runningrouters(routerlist_t
*list
,
1829 running_routers_t
*rr
);
1830 int routers_update_status_from_entry(smartlist_t
*routers
,
1833 int router_update_status_from_smartlist(routerinfo_t
*r
,
1835 smartlist_t
*running_list
);
1836 void add_trusted_dir_server(const char *addr
, uint16_t port
,const char *digest
);
1837 void clear_trusted_dir_servers(void);
1839 /********************************* routerparse.c ************************/
1841 #define MAX_STATUS_TAG_LEN 32
1842 /** Structure to hold parsed Tor versions. This is a little messier
1843 * than we would like it to be, because we changed version schemes with 0.1.0.
1845 * See version-spec.txt for the whole business.
1847 typedef struct tor_version_t
{
1851 /** Release status. For version in the post-0.1 format, this is always
1853 enum { VER_PRE
=0, VER_RC
=1, VER_RELEASE
=2, } status
;
1855 /** CVS status. For version in the post-0.1 format, this is always
1857 enum { IS_CVS
=0, IS_NOT_CVS
=1} cvs
;
1858 char status_tag
[MAX_STATUS_TAG_LEN
];
1861 int router_get_router_hash(const char *s
, char *digest
);
1862 int router_get_dir_hash(const char *s
, char *digest
);
1863 int router_get_runningrouters_hash(const char *s
, char *digest
);
1864 int router_parse_list_from_string(const char **s
,
1865 routerlist_t
**dest
,
1866 smartlist_t
*good_nickname_list
,
1869 int router_parse_routerlist_from_directory(const char *s
,
1870 routerlist_t
**dest
,
1871 crypto_pk_env_t
*pkey
,
1873 int write_to_cache
);
1874 running_routers_t
*router_parse_runningrouters(const char *str
,
1875 int write_to_cache
);
1876 routerinfo_t
*router_parse_entry_from_string(const char *s
, const char *end
);
1877 int router_add_exit_policy_from_string(routerinfo_t
*router
, const char *s
);
1878 addr_policy_t
*router_parse_addr_policy_from_string(const char *s
);
1879 int check_software_version_against_directory(const char *directory
,
1881 int tor_version_parse(const char *s
, tor_version_t
*out
);
1882 int tor_version_as_new_as(const char *platform
, const char *cutoff
);
1883 int tor_version_compare(tor_version_t
*a
, tor_version_t
*b
);
1884 void assert_addr_policy_ok(addr_policy_t
*t
);