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
19 /* Number of fds that select will accept; default is 64. */
20 #define FD_SETSIZE 512
41 #include "../common/torint.h"
42 #include "../common/fakepoll.h"
43 #ifdef HAVE_SYS_LIMITS_H
44 #include <sys/limits.h>
46 #ifdef HAVE_MACHINE_LIMITS_H
47 #include <machine/limits.h>
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
52 #ifdef HAVE_SYS_WAIT_H
55 #ifdef HAVE_SYS_FCNTL_H
56 #include <sys/fcntl.h>
61 #ifdef HAVE_SYS_IOCTL_H
62 #include <sys/ioctl.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_SYS_TIME_H
70 #ifdef HAVE_SYS_STAT_H
73 #ifdef HAVE_NETINET_IN_H
74 #include <netinet/in.h>
76 #ifdef HAVE_ARPA_INET_H
77 #include <arpa/inet.h>
94 #elif defined(_MSC_VER)
103 #define snprintf _snprintf
106 #include "../common/crypto.h"
107 #include "../common/tortls.h"
108 #include "../common/log.h"
109 #include "../common/util.h"
111 /** Upper bound on maximum simultaneous connections; can be lowered by
113 #define MAXCONNECTIONS 1000
115 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
116 #define MAX_NICKNAME_LEN 19
117 #define MAX_DIR_SIZE 500000
120 /** How long do we keep DNS cache entries before purging them? */
121 #define MAX_DNS_ENTRY_AGE (150*60)
123 #define MAX_DNS_ENTRY_AGE (15*60)
126 /** How often do we rotate onion keys? */
127 #define MIN_ONION_KEY_LIFETIME (120*60)
128 /** How often do we rotate TLS contexts? */
129 #define MAX_SSL_KEY_LIFETIME (120*60)
131 #define CIRC_ID_TYPE_LOWER 0
132 #define CIRC_ID_TYPE_HIGHER 1
134 #define _CONN_TYPE_MIN 3
135 /** Type for sockets listening for OR connections. */
136 #define CONN_TYPE_OR_LISTENER 3
137 /** Type for OR-to-OR or OP-to-OR connections. */
138 #define CONN_TYPE_OR 4
139 /** Type for connections from final OR to chosen destination. */
140 #define CONN_TYPE_EXIT 5
141 /** Type for sockets listening for SOCKS connections. */
142 #define CONN_TYPE_AP_LISTENER 6
143 /** Type for SOCKS connections to OP. */
144 #define CONN_TYPE_AP 7
145 /** Type for sockets listening for HTTP connections to the directory server. */
146 #define CONN_TYPE_DIR_LISTENER 8
147 /** Type for HTTP connections to the directory server. */
148 #define CONN_TYPE_DIR 9
149 /** Type for connections to local dnsworker processes. */
150 #define CONN_TYPE_DNSWORKER 10
151 /** Type for connections to local cpuworker processes. */
152 #define CONN_TYPE_CPUWORKER 11
153 #define _CONN_TYPE_MAX 11
155 /** State for any listener connection. */
156 #define LISTENER_STATE_READY 0
158 #define _DNSWORKER_STATE_MIN 1
159 /** State for a connection to a dnsworker process that's idle. */
160 #define DNSWORKER_STATE_IDLE 1
161 /** State for a connection to a dnsworker process that's resolving a hostname. */
162 #define DNSWORKER_STATE_BUSY 2
163 #define _DNSWORKER_STATE_MAX 2
165 #define _CPUWORKER_STATE_MIN 1
166 /** State for a connection to a cpuworker process that's idle. */
167 #define CPUWORKER_STATE_IDLE 1
168 /** State for a connection to a cpuworker process that's processing a
170 #define CPUWORKER_STATE_BUSY_ONION 2
171 #define _CPUWORKER_STATE_MAX 2
173 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
175 #define _OR_CONN_STATE_MIN 1
176 /** State for a connection to an OR: waiting for connect() to finish. */
177 #define OR_CONN_STATE_CONNECTING 1
178 /** State for a connection to an OR: SSL is handshaking, not done yet. */
179 #define OR_CONN_STATE_HANDSHAKING 2
180 /** State for a connection to an OR: Ready to send/receive cells. */
181 #define OR_CONN_STATE_OPEN 3
182 #define _OR_CONN_STATE_MAX 3
184 #define _EXIT_CONN_STATE_MIN 1
185 /** State for an exit connection: waiting for response from dns farm. */
186 #define EXIT_CONN_STATE_RESOLVING 1
187 /** State for an exit connection: waiting for connect() to finish. */
188 #define EXIT_CONN_STATE_CONNECTING 2
189 /** State for an exit connection: open and ready to transmit data. */
190 #define EXIT_CONN_STATE_OPEN 3
191 /** State for an exit connection: waiting to be removed. */
192 #define EXIT_CONN_STATE_RESOLVEFAILED 4
193 #define _EXIT_CONN_STATE_MAX 4
195 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
196 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
199 /* the AP state values must be disjoint from the EXIT state values */
200 #define _AP_CONN_STATE_MIN 5
201 /** State for a SOCKS connection: waiting for SOCKS request. */
202 #define AP_CONN_STATE_SOCKS_WAIT 5
203 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
204 * rendezvous rescriptor. */
205 #define AP_CONN_STATE_RENDDESC_WAIT 6
206 /** State for a SOCKS connection: waiting for a completed circuit. */
207 #define AP_CONN_STATE_CIRCUIT_WAIT 7
208 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
209 #define AP_CONN_STATE_CONNECT_WAIT 8
210 /** State for a SOCKS connection: ready to send and receive. */
211 #define AP_CONN_STATE_OPEN 9
212 #define _AP_CONN_STATE_MAX 9
214 #define _DIR_CONN_STATE_MIN 1
215 /** State for connection to directory server: waiting for connect(). */
216 #define DIR_CONN_STATE_CONNECTING 1
217 /** State for connection to directory server: sending HTTP request. */
218 #define DIR_CONN_STATE_CLIENT_SENDING 2
219 /** State for connection to directory server: reading HTTP response. */
220 #define DIR_CONN_STATE_CLIENT_READING 3
221 /** State for connection at directory server: waiting for HTTP request. */
222 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
223 /** State for connection at directory server: sending HTTP response. */
224 #define DIR_CONN_STATE_SERVER_WRITING 5
225 #define _DIR_CONN_STATE_MAX 5
227 #define _DIR_PURPOSE_MIN 1
228 /** Purpose for connection to directory server: download a directory. */
229 #define DIR_PURPOSE_FETCH_DIR 1
230 /** Purpose for connection to directory server: download a rendezvous
232 #define DIR_PURPOSE_FETCH_RENDDESC 2
233 /** Purpose for connection to directory server: set after a rendezvous
234 * descriptor is downloaded. */
235 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 3
236 /** Purpose for connection to directory server: upload a server descriptor. */
237 #define DIR_PURPOSE_UPLOAD_DIR 4
238 /** Purpose for connection to directory server: upload a rendezvous
240 #define DIR_PURPOSE_UPLOAD_RENDDESC 5
241 /** Purpose for connection at a directory server. */
242 #define DIR_PURPOSE_SERVER 6
243 #define _DIR_PURPOSE_MAX 6
245 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
246 #define CIRCUIT_STATE_BUILDING 0
247 /** Circuit state: Waiting to process the onionskin. */
248 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
249 /** Circuit state: I'm the OP, my firsthop is still connecting. */
250 #define CIRCUIT_STATE_OR_WAIT 2
251 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
252 #define CIRCUIT_STATE_OPEN 3
254 #define _CIRCUIT_PURPOSE_MIN 1
256 /* these circuits were initiated elsewhere */
257 #define _CIRCUIT_PURPOSE_OR_MIN 1
258 /** OR-side circuit purpose: normal circuit, at OR. */
259 #define CIRCUIT_PURPOSE_OR 1
260 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
261 #define CIRCUIT_PURPOSE_INTRO_POINT 2
262 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
263 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
264 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
265 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
266 #define _CIRCUIT_PURPOSE_OR_MAX 4
268 /* these circuits originate at this node */
270 /* here's how circ client-side purposes work:
271 * normal circuits are C_GENERAL.
272 * circuits that are c_introducing are either on their way to
273 * becoming open, or they are open and waiting for a
274 * suitable rendcirc before they send the intro.
275 * circuits that are c_introduce_ack_wait have sent the intro,
276 * but haven't gotten a response yet.
277 * circuits that are c_establish_rend are either on their way
278 * to becoming open, or they are open and have sent the
279 * establish_rendezvous cell but haven't received an ack.
280 * circuits that are c_rend_ready are open and have received a
281 * rend ack, but haven't heard from bob yet. if they have a
282 * buildstate->pending_final_cpath then they're expecting a
283 * cell from bob, else they're not.
284 * circuits that are c_rend_ready_intro_acked are open, and
285 * some intro circ has sent its intro and received an ack.
286 * circuits that are c_rend_joined are open, have heard from
287 * bob, and are talking to him.
289 /** Client-side circuit purpose: Normal circuit, with cpath. */
290 #define CIRCUIT_PURPOSE_C_GENERAL 5
291 /** Client-side circuit purpose: at Alice, connecting to intro point. */
292 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
293 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
294 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
295 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
296 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
297 /** Client-side circuit purpose: at Alice, waiting for ack. */
298 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
299 /** Client-side circuit purpose: at Alice, waiting for Bob. */
300 #define CIRCUIT_PURPOSE_C_REND_READY 10
301 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
302 * has been acknowledged. */
303 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
304 /** Client-side circuit purpose: at Alice, rendezvous established. */
305 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
307 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
308 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
309 /** Hidden-service-side circuit purpose: at Bob, successfully established
311 #define CIRCUIT_PURPOSE_S_INTRO 14
312 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
313 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
314 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
315 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
316 #define _CIRCUIT_PURPOSE_MAX 16
318 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
319 * that this OP has originated. */
320 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
321 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
323 #define RELAY_COMMAND_BEGIN 1
324 #define RELAY_COMMAND_DATA 2
325 #define RELAY_COMMAND_END 3
326 #define RELAY_COMMAND_CONNECTED 4
327 #define RELAY_COMMAND_SENDME 5
328 #define RELAY_COMMAND_EXTEND 6
329 #define RELAY_COMMAND_EXTENDED 7
330 #define RELAY_COMMAND_TRUNCATE 8
331 #define RELAY_COMMAND_TRUNCATED 9
332 #define RELAY_COMMAND_DROP 10
333 #define RELAY_COMMAND_RESOLVE 11
334 #define RELAY_COMMAND_RESOLVED 12
336 #define RELAY_COMMAND_ESTABLISH_INTRO 32
337 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
338 #define RELAY_COMMAND_INTRODUCE1 34
339 #define RELAY_COMMAND_INTRODUCE2 35
340 #define RELAY_COMMAND_RENDEZVOUS1 36
341 #define RELAY_COMMAND_RENDEZVOUS2 37
342 /* DOCDOC Spec these next two. */
343 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
344 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
345 #define RELAY_COMMAND_INTRODUCE_ACK 40
347 #define _MIN_END_STREAM_REASON 1
348 #define END_STREAM_REASON_MISC 1
349 #define END_STREAM_REASON_RESOLVEFAILED 2
350 #define END_STREAM_REASON_CONNECTFAILED 3
351 #define END_STREAM_REASON_EXITPOLICY 4
352 #define END_STREAM_REASON_DESTROY 5
353 #define END_STREAM_REASON_DONE 6
354 #define END_STREAM_REASON_TIMEOUT 7
355 #define _MAX_END_STREAM_REASON 7
357 /** Length of 'y' portion of 'y.onion' URL. */
358 #define REND_SERVICE_ID_LEN 16
360 /* Reasons used by connection_mark_for_close */
361 #define CLOSE_REASON_UNUSED_OR_CONN 100
363 #define CELL_DIRECTION_IN 1
364 #define CELL_DIRECTION_OUT 2
367 #define CIRCWINDOW_START 10000
368 #define CIRCWINDOW_INCREMENT 1000
369 #define STREAMWINDOW_START 5000
370 #define STREAMWINDOW_INCREMENT 500
372 #define CIRCWINDOW_START 1000
373 #define CIRCWINDOW_INCREMENT 100
374 #define STREAMWINDOW_START 500
375 #define STREAMWINDOW_INCREMENT 50
379 #define CELL_PADDING 0
380 #define CELL_CREATE 1
381 #define CELL_CREATED 2
383 #define CELL_DESTROY 4
385 /* legal characters in a nickname */
386 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
388 #define SOCKS4_NETWORK_LEN 8
392 * Relay command [1 byte]
393 * Recognized [2 bytes]
394 * Stream ID [2 bytes]
395 * Partial SHA-1 [4 bytes]
397 * Relay payload [498 bytes]
400 #define CELL_PAYLOAD_SIZE 509
401 #define CELL_NETWORK_SIZE 512
403 #define RELAY_HEADER_SIZE (1+2+2+4+2)
404 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
406 /** Parsed onion routing cell. All communication from OP-to-OR, or from
407 * OR-to-OR, is via cells. */
409 uint16_t circ_id
; /**< Circuit which received the cell. */
410 unsigned char command
; /**< Type of the cell: one of PADDING, CREATE, RELAY,
412 unsigned char payload
[CELL_PAYLOAD_SIZE
]; /**< Cell body. */
415 /** Beginning of a RELAY cell payload. */
417 uint8_t command
; /**< The end-to-end relay command. */
418 uint16_t recognized
; /**< Used to tell whether cell is for us. */
419 uint16_t stream_id
; /**< Which stream is this cell associated with? */
420 char integrity
[4]; /**< Used to tell whether cell is corrupted. */
421 uint16_t length
; /**< How long is the payload body? */
424 typedef struct buf_t buf_t
;
425 typedef struct socks_request_t socks_request_t
;
427 #define CONNECTION_MAGIC 0x7C3C304Eu
428 /** Description of a connection to another host or process, and associated
430 struct connection_t
{
431 uint32_t magic
; /**< For memory debugging: must equal CONNECTION_MAGIC. */
433 uint8_t type
; /**< What kind of connection is this? */
434 uint8_t state
; /**< Current state of this connection. */
435 uint8_t purpose
; /**< Only used for DIR types currently. */
436 uint8_t wants_to_read
; /**< Boolean: should we start reading again once
437 * the bandwidth throttler allows it?
439 uint8_t wants_to_write
; /**< Boolean: should we start writing again once
440 * the bandwidth throttler allows reads?
442 int s
; /**< Our socket; -1 if this connection is closed. */
443 int poll_index
; /**< Index of this conn into the poll_array. */
444 int marked_for_close
; /**< Boolean: should we close this conn on the next
445 * iteration of the main loop?
447 char *marked_for_close_file
; /**< For debugging: in which file were we marked
449 int hold_open_until_flushed
; /**< Despite this connection's being marked
450 * for close, do we flush it before closing it?
453 buf_t
*inbuf
; /**< Buffer holding data read over this connection. */
454 int inbuf_reached_eof
; /**< Boolean: did read() return 0 on this conn? */
455 time_t timestamp_lastread
; /**< When was the last time poll() said we could read? */
457 buf_t
*outbuf
; /**< Buffer holding data to write over this connection. */
458 int outbuf_flushlen
; /**< How much data should we try to flush from the
460 time_t timestamp_lastwritten
; /**< When was the last time poll() said we could write? */
462 time_t timestamp_created
; /**< When was this connection_t created? */
464 uint32_t addr
; /**< IP of the other side of the connection; used to identify
465 * routers, along with port. */
466 uint16_t port
; /**< If non-zero, port on the other end
467 * of the connection. */
468 char *address
; /**< FQDN (or IP) of the guy on the other end.
469 * strdup into this, because free_connection frees it.
471 crypto_pk_env_t
*identity_pkey
; /**> Public RSA key for the other side's
473 char *nickname
; /**< Nickname of OR on other side (if any). */
475 /* Used only by OR connections: */
476 tor_tls
*tls
; /**< TLS connection state (OR only.) */
477 uint16_t next_circ_id
; /**< Which circ_id do we try to use next on
478 * this connection? This is always in the
479 * range 0..1<<15-1. (OR only.)*/
481 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
482 int bandwidth
; /**< Connection bandwidth. (OPEN ORs only.) */
483 int receiver_bucket
; /**< When this hits 0, stop receiving. Every second we
484 * add 'bandwidth' to this, capping it at 10*bandwidth.
488 /* Used only by DIR and AP connections: */
489 char rend_query
[REND_SERVICE_ID_LEN
+1]; /**< What rendezvous service are we
490 * querying for? (DIR/AP only) */
492 /* Used only by edge connections: */
494 struct connection_t
*next_stream
; /**< Points to the next stream at this
495 * edge, if any (Edge only). */
496 struct crypt_path_t
*cpath_layer
; /**< A pointer to which node in the circ
497 * this conn exits at. (Edge only.) */
498 int package_window
; /**< How many more relay cells can i send into the
499 * circuit? (Edge only.) */
500 int deliver_window
; /**< How many more relay cells can end at me? (Edge
503 int done_sending
; /**< For half-open connections; not used currently. */
504 int done_receiving
; /**< For half-open connections; not used currently. */
505 char has_sent_end
; /**< For debugging: set once we've set the stream end,
506 and check in circuit_about_to_close_connection(). */
507 char num_retries
; /**< How many times have we re-tried beginning this stream? (Edge only) */
509 /* Used only by AP connections */
510 socks_request_t
*socks_request
; /**< SOCKS structure describing request (AP
514 typedef struct connection_t connection_t
;
516 #define EXIT_POLICY_ACCEPT 1
517 #define EXIT_POLICY_REJECT 2
519 /** A linked list of exit policy rules */
520 struct exit_policy_t
{
521 char policy_type
; /**< One of EXIT_POLICY_ACCEPT or EXIT_POLICY_REJECT. */
522 char *string
; /**< String representation of this rule. */
523 uint32_t addr
; /**< Base address to accept or reject. */
524 uint32_t msk
; /**< Accept/reject all addresses <b>a</b> such that a & msk ==
525 * <b>addr</b> & msk . */
526 uint16_t prt_min
; /**< Lowest port number to accept/reject. */
527 uint16_t prt_max
; /**< Highest port number to accept/reject. */
529 struct exit_policy_t
*next
; /**< Next rule in list. */
532 /** Information about another onion router in the network. */
534 char *address
; /**< Location of OR: either a hostname or an IP address. */
535 char *nickname
; /**< Human-readable OR name. */
537 uint32_t addr
; /**< IPv4 address of OR, in host order. */
538 uint16_t or_port
; /**< Port for OR-to-OR and OP-to-OR connections. */
539 uint16_t socks_port
; /**< Port for SOCKS connections. */
540 uint16_t dir_port
; /**< Port for HTTP directory connections. */
542 time_t published_on
; /**< When was the information in this routerinfo_t
545 crypto_pk_env_t
*onion_pkey
; /**< Public RSA key for onions. */
546 crypto_pk_env_t
*identity_pkey
; /**< Public RSA key for signing. */
548 int is_running
; /**< As far as we know, is this OR currently running? */
550 char *platform
; /**< What software/operating system is this OR using? */
553 uint32_t bandwidthrate
; /**< How many bytes does this OR add to its token
554 * bucket per second? */
555 uint32_t bandwidthburst
; /**< How large is this OR's token bucket? */
556 struct exit_policy_t
*exit_policy
; /**< What streams will this OR permit
560 #define MAX_ROUTERS_IN_DIR 1024
561 /** Contents of a directory of onion routers. */
563 /** List of routerinfo_t */
564 smartlist_t
*routers
;
565 /** Which versions of tor are recommended by this directory? */
566 char *software_versions
;
567 /** When was this directory published? */
571 /** Holds accounting information for a single step in the layered encryption
572 * performed by a circuit. Used only at the client edge of a circuit. */
573 struct crypt_path_t
{
575 /* crypto environments */
576 /** Encryption key and counter for cells heading towards the OR at this
578 crypto_cipher_env_t
*f_crypto
;
579 /** Encryption key and counter for cells heading back from the OR at this
581 crypto_cipher_env_t
*b_crypto
;
583 /** Digest state for cells heading towards the OR at this step. */
584 crypto_digest_env_t
*f_digest
; /* for integrity checking */
585 /** Digest state for cells heading away from the OR at this step. */
586 crypto_digest_env_t
*b_digest
;
588 /** Current state of Diffie-Hellman key negotiation with the OR at this
590 crypto_dh_env_t
*handshake_state
;
591 /** Negotiated key material shared with the OR at this step. */
592 char handshake_digest
[DIGEST_LEN
];/* KH in tor-spec.txt */
594 /** IP4 address of the OR at this step. */
596 /** Port of the OR at this step. */
599 /** Is the circuit built to this step? Must be one of:
600 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
601 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
602 * and not received an EXTENDED/CREATED)
603 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
605 #define CPATH_STATE_CLOSED 0
606 #define CPATH_STATE_AWAITING_KEYS 1
607 #define CPATH_STATE_OPEN 2
608 struct crypt_path_t
*next
; /**< Link to next crypt_path_t in the circuit.
609 * (The list is circular, so the last node
610 * links to the first.) */
611 struct crypt_path_t
*prev
; /**< Link to previous crypt_path_t in the
614 int package_window
; /**< How many bytes are we allowed to originate ending
616 int deliver_window
; /**< How many bytes are we willing to deliver originating
620 #define DH_KEY_LEN DH_BYTES
621 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
624 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
625 #define REND_COOKIE_LEN DIGEST_LEN
627 typedef struct crypt_path_t crypt_path_t
;
629 /** Information used to build a circuit. */
631 /** Intended length of the final circuit. */
632 int desired_path_len
;
633 /** Nickname of planned exit node. */
635 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
636 struct crypt_path_t
*pending_final_cpath
;
637 /** How many times has building a circuit for this task failed? */
639 } cpath_build_state_t
;
642 #define CIRCUIT_MAGIC 0x35315243u
643 /** Struct for a path (circuit) through the onion routing network. */
645 uint32_t magic
; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
647 int marked_for_close
; /**< Should we close this circuit at the end of the
649 char *marked_for_close_file
; /**< For debugging: in which file was this
650 * circuit marked for close? */
652 /** The IPv4 address of the OR that is next in this circuit. */
654 /** The port for the OR that is next in this circuit. */
656 /** The OR connection that is previous in this circuit. */
657 connection_t
*p_conn
;
658 /** The OR connection that is next in this circuit. */
659 connection_t
*n_conn
;
660 /** Linked list of AP streams associated with this circuit. */
661 connection_t
*p_streams
;
662 /** Linked list of Exit streams associated with this circuit. */
663 connection_t
*n_streams
;
664 /** Linked list of Exit streams associated with this circuit that are
665 * still being resolved. */
666 connection_t
*resolving_streams
;
667 /** The next stream_id that will be tried when we're attempting to
668 * construct a new AP stream originating at this circuit. */
669 uint16_t next_stream_id
;
670 /** How many relay data cells can we package (read from edge streams)
671 * on this circuit before we receive a circuit-level sendme cell asking
674 /** How many relay data cells will we deliver (write to edge streams)
675 * on this circuit? When deliver_window gets low, we send some
676 * circuit-level sendme cells to indicate that we're willing to accept
680 /** The circuit_id used in the previous (backward) hop of this circuit. */
682 /** The circuit_id used in the next (forward) hop of this circuit. */
685 /** The cipher used by intermediate hops for cells heading toward the
687 crypto_cipher_env_t
*p_crypto
;
688 /** The cipher used by intermediate hops for cells heading away from
690 crypto_cipher_env_t
*n_crypto
;
692 /** The integrity-checking digest used by intermediate hops, for
693 * cells packaged here and heading towards the OP.
695 crypto_digest_env_t
*p_digest
;
696 /** The integrity-checking digest used by intermediate hops, for
697 * cells packaged at the OP and arriving here.
699 crypto_digest_env_t
*n_digest
;
701 /** Build state for this circuit. It includes the intended path
702 * length, the chosen exit router, rendezvous information, etc.
704 cpath_build_state_t
*build_state
;
705 /** The doubly-linked list of crypt_path_t entries, one per hop,
706 * for this circuit. This includes ciphers for each hop,
707 * integrity-checking digests for each hop, and package/delivery
708 * windows for each hop.
710 * The cpath field is defined only when we are the circuit's origin.
714 char onionskin
[ONIONSKIN_CHALLENGE_LEN
]; /**< For storage while onionskin
716 char handshake_digest
[DIGEST_LEN
]; /**< Stores KH for intermediate hops. */
718 time_t timestamp_created
; /**< When was this circuit created? */
719 time_t timestamp_dirty
; /**< When the circuit was first used, or 0 if the
720 * circuit is clean. */
722 uint8_t state
; /**< Current status of this circuit. */
723 uint8_t purpose
; /**< Why are we creating this circuit? */
726 * The rend_query field holds y portion of y.onion (nul-terminated)
727 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
728 * for a hidden service, or is S_*.
730 char rend_query
[REND_SERVICE_ID_LEN
+1];
732 /** The rend_pk_digest field holds a hash of location-hidden service's
733 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
735 char rend_pk_digest
[DIGEST_LEN
];
737 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
738 * C_ESTABLISH_REND. Filled with zeroes otherwise.
740 char rend_cookie
[REND_COOKIE_LEN
];
742 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
743 * is not marked for close. */
744 struct circuit_t
*rend_splice
;
746 struct circuit_t
*next
; /**< Next circuit in linked list. */
749 typedef struct circuit_t circuit_t
;
751 /** Configuration options for a Tor process */
753 char *LogLevel
; /**< Verbosity of log: minimal level of messages to report. */
754 char *LogFile
; /**< Where to send normal log messages. */
755 char *DebugLogFile
; /**< Where to send verbose log messages. */
756 char *DataDirectory
; /**< OR only: where to store long-term data. */
757 char *RouterFile
; /**< Where to find starting list of ORs. */
758 char *Nickname
; /**< OR only: nickname of this onion router. */
759 char *Address
; /**< OR only: configured address for this onion router. */
760 char *PidFile
; /**< Where to store PID of Tor process. */
762 char *ExitNodes
; /**< Comma-separated list of nicknames of ORs to consider
764 char *EntryNodes
; /**< Comma-separated list of nicknames of ORs to consider
765 * as entry points. */
766 char *ExcludeNodes
; /**< Comma-separated list of nicknames of ORs not to
767 * use in circuits. */
769 char *RendNodes
; /**< Comma-separated list of nicknames used as introduction
771 char *RendExcludeNodes
; /**< Comma-separated list of nicknames not to use
772 * as introduction points. */
774 char *ExitPolicy
; /**< Comma-separated list of exit policy components. */
775 char *SocksBindAddress
; /**< Address to bind for listening for SOCKS
777 char *ORBindAddress
; /**< Address to bind for listening for OR
779 char *DirBindAddress
; /**< Address to bind for listening for directory
781 char *RecommendedVersions
; /**< Directory server only: which versions of
782 * Tor should we tell users to run? */
783 char *User
; /**< Name of user to run Tor as. */
784 char *Group
; /**< Name of group to run Tor as. */
785 double PathlenCoinWeight
; /**< Parameter used to configure average path
786 * length (alpha in geometric distribution). */
787 int ORPort
; /**< Port to listen on for OR connections. */
788 int SocksPort
; /**< Port to listen on for SOCKS connections. */
789 int DirPort
; /**< Port to listen on for directory connections. */
790 int MaxConn
; /**< Maximum number of simultaneous connections. */
791 int TrafficShaping
; /**< Unused. */
792 int LinkPadding
; /**< Unused. */
793 int IgnoreVersion
; /**< If true, run no matter what versions of Tor the
794 * directory recommends. */
795 int RunAsDaemon
; /**< If true, run in the background. (Unix only) */
796 int DirFetchPostPeriod
; /**< How often do we fetch new directories
797 * and post server descriptros to the directory
799 int KeepalivePeriod
; /**< How often do we send padding cells to keep
800 * connections alive? */
801 int MaxOnionsPending
; /**< How many circuit CREATE requests do we allow
802 * to wait simultaneously before we start dropping
804 int NewCircuitPeriod
; /**< How long do we use a circuit before building
806 int BandwidthRate
; /**< How much bandwidth, on average, are we willing to
807 * use in a second? */
808 int BandwidthBurst
; /**< How much bandwidth, at maximum, are we willing to
809 * use in a second? */
810 int NumCpus
; /**< How many CPUs should we try to use? */
811 int loglevel
; /**< How verbose should we be? Log messages less severe than
812 * this will be ignored. */
813 int RunTesting
; /**< If true, create testing circuits to measure how well the
814 * other ORs are running. */
815 struct config_line_t
*RendConfigLines
; /**< List of configuration lines
816 * for rendezvous services. */
819 /* XXX are these good enough defaults? */
820 #define MAX_SOCKS_REPLY_LEN 1024
821 #define MAX_SOCKS_ADDR_LEN 256
822 /** State of a SOCKS request from a user to an OP */
823 struct socks_request_t
{
824 char socks_version
; /**< Which version of SOCKS did the client use? */
825 int replylen
; /**< Length of <b>reply</b>. */
826 char reply
[MAX_SOCKS_REPLY_LEN
]; /**< Write an entry into this string if
827 * we want to specify our own socks reply,
828 * rather than using the default socks4 or
829 * socks5 socks reply. We use this for the
830 * two-stage socks5 handshake.
832 int has_finished
; /**< Has the SOCKS handshake finished? */
833 char address
[MAX_SOCKS_ADDR_LEN
]; /**< What address did the client ask to connect to? */
834 uint16_t port
; /**< What port did the client ask to connect to? */
837 /* all the function prototypes go here */
839 /********************************* buffers.c ***************************/
842 buf_t
*buf_new_with_capacity(size_t size
);
843 void buf_free(buf_t
*buf
);
844 void buf_clear(buf_t
*buf
);
846 size_t buf_datalen(const buf_t
*buf
);
847 size_t buf_capacity(const buf_t
*buf
);
848 const char *_buf_peek_raw_buffer(const buf_t
*buf
);
850 int read_to_buf(int s
, size_t at_most
, buf_t
*buf
, int *reached_eof
);
851 int read_to_buf_tls(tor_tls
*tls
, size_t at_most
, buf_t
*buf
);
853 int flush_buf(int s
, buf_t
*buf
, int *buf_flushlen
);
854 int flush_buf_tls(tor_tls
*tls
, buf_t
*buf
, int *buf_flushlen
);
856 int write_to_buf(const char *string
, int string_len
, buf_t
*buf
);
857 int fetch_from_buf(char *string
, size_t string_len
, buf_t
*buf
);
858 int fetch_from_buf_http(buf_t
*buf
,
859 char **headers_out
, int max_headerlen
,
860 char **body_out
, int *body_used
, int max_bodylen
);
861 int fetch_from_buf_socks(buf_t
*buf
, socks_request_t
*req
);
863 void assert_buf_ok(buf_t
*buf
);
865 /********************************* circuit.c ***************************/
867 extern char *circuit_state_to_string
[];
868 circuit_t
*circuit_new(uint16_t p_circ_id
, connection_t
*p_conn
);
869 void circuit_close_all_marked(void);
870 void circuit_free_cpath(crypt_path_t
*cpath
);
871 int _circuit_mark_for_close(circuit_t
*circ
);
873 #define circuit_mark_for_close(c) \
875 if (_circuit_mark_for_close(c)<0) { \
876 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
877 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
879 c->marked_for_close_file = __FILE__; \
880 c->marked_for_close = __LINE__; \
885 circuit_t
*circuit_get_by_circ_id_conn(uint16_t circ_id
, connection_t
*conn
);
886 circuit_t
*circuit_get_by_conn(connection_t
*conn
);
887 circuit_t
*circuit_get_best(connection_t
*conn
,
888 int must_be_open
, uint8_t purpose
);
889 circuit_t
*circuit_get_by_rend_query_and_purpose(const char *rend_query
, uint8_t purpose
);
890 circuit_t
*circuit_get_next_by_pk_and_purpose(circuit_t
*circuit
,
891 const char *servid
, uint8_t purpose
);
892 circuit_t
*circuit_get_rendezvous(const char *cookie
);
894 void circuit_expire_building(time_t now
);
895 int circuit_count_building(uint8_t purpose
);
896 int circuit_stream_is_being_handled(connection_t
*conn
);
897 void circuit_build_needed_circs(time_t now
);
899 void circuit_resume_edge_reading(circuit_t
*circ
, crypt_path_t
*layer_hint
);
900 int circuit_consider_stop_edge_reading(circuit_t
*circ
, crypt_path_t
*layer_hint
);
901 void circuit_consider_sending_sendme(circuit_t
*circ
, crypt_path_t
*layer_hint
);
903 void circuit_detach_stream(circuit_t
*circ
, connection_t
*conn
);
904 void circuit_about_to_close_connection(connection_t
*conn
);
906 void circuit_log_path(int severity
, circuit_t
*circ
);
907 void circuit_dump_by_conn(connection_t
*conn
, int severity
);
909 void circuit_expire_unused_circuits(void);
910 circuit_t
*circuit_launch_new(uint8_t purpose
, const char *exit_nickname
);
911 void circuit_increment_failure_count(void);
912 void circuit_reset_failure_count(void);
913 void circuit_n_conn_open(connection_t
*or_conn
);
914 int circuit_send_next_onion_skin(circuit_t
*circ
);
915 int circuit_extend(cell_t
*cell
, circuit_t
*circ
);
916 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
917 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
,int reverse
);
918 int circuit_finish_handshake(circuit_t
*circ
, char *reply
);
919 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
921 void assert_cpath_ok(const crypt_path_t
*c
);
922 void assert_cpath_layer_ok(const crypt_path_t
*c
);
923 void assert_circuit_ok(const circuit_t
*c
);
925 /********************************* command.c ***************************/
927 void command_process_cell(cell_t
*cell
, connection_t
*conn
);
929 extern unsigned long stats_n_padding_cells_processed
;
930 extern unsigned long stats_n_create_cells_processed
;
931 extern unsigned long stats_n_created_cells_processed
;
932 extern unsigned long stats_n_relay_cells_processed
;
933 extern unsigned long stats_n_destroy_cells_processed
;
935 /********************************* config.c ***************************/
937 struct config_line_t
{
940 struct config_line_t
*next
;
943 int config_assign_default_dirservers(void);
944 int getconfig(int argc
, char **argv
, or_options_t
*options
);
946 /********************************* connection.c ***************************/
948 #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
949 "Unknown" : conn_type_to_string[(t)])
951 extern char *conn_type_to_string
[];
953 connection_t
*connection_new(int type
);
954 void connection_free(connection_t
*conn
);
955 void connection_free_all(void);
956 void connection_about_to_close_connection(connection_t
*conn
);
957 void connection_close_immediate(connection_t
*conn
);
958 int _connection_mark_for_close(connection_t
*conn
, char reason
);
960 #define connection_mark_for_close(c,r) \
962 if (_connection_mark_for_close(c,r)<0) { \
963 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
964 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
966 c->marked_for_close_file = __FILE__; \
967 c->marked_for_close = __LINE__; \
971 void connection_expire_held_open(void);
973 int connection_create_listener(char *bindaddress
, uint16_t bindport
, int type
);
975 int connection_connect(connection_t
*conn
, char *address
, uint32_t addr
, uint16_t port
);
976 int retry_all_connections(void);
978 void connection_bucket_init(void);
979 void connection_bucket_refill(struct timeval
*now
);
981 int connection_handle_read(connection_t
*conn
);
983 int connection_fetch_from_buf(char *string
, int len
, connection_t
*conn
);
985 int connection_wants_to_flush(connection_t
*conn
);
986 int connection_outbuf_too_full(connection_t
*conn
);
987 int connection_handle_write(connection_t
*conn
);
988 void connection_write_to_buf(const char *string
, int len
, connection_t
*conn
);
990 connection_t
*connection_twin_get_by_addr_port(uint32_t addr
, uint16_t port
);
991 connection_t
*connection_exact_get_by_addr_port(uint32_t addr
, uint16_t port
);
993 connection_t
*connection_get_by_type(int type
);
994 connection_t
*connection_get_by_type_state(int type
, int state
);
995 connection_t
*connection_get_by_type_state_lastwritten(int type
, int state
);
996 connection_t
*connection_get_by_type_rendquery(int type
, const char *rendquery
);
998 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
999 #define connection_has_pending_tls_data(conn) \
1000 ((conn)->type == CONN_TYPE_OR && \
1001 (conn)->state == OR_CONN_STATE_OPEN && \
1002 tor_tls_get_pending_bytes((conn)->tls))
1003 int connection_is_listener(connection_t
*conn
);
1004 int connection_state_is_open(connection_t
*conn
);
1005 int connection_state_is_connecting(connection_t
*conn
);
1007 int connection_send_destroy(uint16_t circ_id
, connection_t
*conn
);
1009 void assert_connection_ok(connection_t
*conn
, time_t now
);
1011 /********************************* connection_edge.c ***************************/
1013 void relay_header_pack(char *dest
, const relay_header_t
*src
);
1014 void relay_header_unpack(relay_header_t
*dest
, const char *src
);
1015 int connection_edge_process_inbuf(connection_t
*conn
);
1016 int connection_edge_destroy(uint16_t circ_id
, connection_t
*conn
);
1017 int connection_edge_end(connection_t
*conn
, char reason
, crypt_path_t
*cpath_layer
);
1018 int connection_edge_send_command(connection_t
*fromconn
, circuit_t
*circ
,
1019 int relay_command
, const char *payload
,
1020 int payload_len
, crypt_path_t
*cpath_layer
);
1021 int connection_edge_process_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1023 crypt_path_t
*layer_hint
);
1024 int connection_edge_finished_flushing(connection_t
*conn
);
1025 int connection_edge_finished_connecting(connection_t
*conn
);
1027 int connection_edge_package_raw_inbuf(connection_t
*conn
);
1029 int connection_ap_handshake_attach_circuit(connection_t
*conn
);
1030 int connection_ap_handshake_send_begin(connection_t
*ap_conn
, circuit_t
*circ
);
1032 int connection_ap_make_bridge(char *address
, uint16_t port
);
1034 void connection_ap_handshake_socks_reply(connection_t
*conn
, char *reply
,
1035 int replylen
, char success
);
1037 void connection_exit_connect(connection_t
*conn
);
1038 int connection_edge_is_rendezvous_stream(connection_t
*conn
);
1039 int connection_ap_can_use_exit(connection_t
*conn
, routerinfo_t
*exit
);
1040 void connection_ap_expire_beginning(void);
1041 void connection_ap_attach_pending(void);
1043 extern uint64_t stats_n_data_cells_packaged
;
1044 extern uint64_t stats_n_data_bytes_packaged
;
1045 extern uint64_t stats_n_data_cells_received
;
1046 extern uint64_t stats_n_data_bytes_received
;
1048 void client_dns_init(void);
1049 void client_dns_clean(void);
1051 /********************************* connection_or.c ***************************/
1053 int connection_or_process_inbuf(connection_t
*conn
);
1054 int connection_or_finished_flushing(connection_t
*conn
);
1055 int connection_or_finished_connecting(connection_t
*conn
);
1057 connection_t
*connection_or_connect(routerinfo_t
*router
);
1059 int connection_tls_start_handshake(connection_t
*conn
, int receiving
);
1060 int connection_tls_continue_handshake(connection_t
*conn
);
1062 void connection_or_write_cell_to_buf(const cell_t
*cell
, connection_t
*conn
);
1064 /********************************* cpuworker.c *****************************/
1066 void cpu_init(void);
1067 void cpuworkers_rotate(void);
1068 int connection_cpu_finished_flushing(connection_t
*conn
);
1069 int connection_cpu_process_inbuf(connection_t
*conn
);
1070 int assign_to_cpuworker(connection_t
*cpuworker
, unsigned char question_type
,
1073 /********************************* directory.c ***************************/
1075 void directory_initiate_command(routerinfo_t
*router
, int purpose
,
1076 const char *payload
, int payload_len
);
1077 int connection_dir_process_inbuf(connection_t
*conn
);
1078 int connection_dir_finished_flushing(connection_t
*conn
);
1079 int connection_dir_finished_connecting(connection_t
*conn
);
1081 /********************************* dirserv.c ***************************/
1082 int dirserv_add_own_fingerprint(const char *nickname
, crypto_pk_env_t
*pk
);
1083 int dirserv_parse_fingerprint_file(const char *fname
);
1084 int dirserv_router_fingerprint_is_known(const routerinfo_t
*router
);
1085 void dirserv_free_fingerprint_list();
1086 int dirserv_add_descriptor(const char **desc
);
1087 int dirserv_init_from_directory_string(const char *dir
);
1088 void dirserv_free_descriptors();
1089 int dirserv_dump_directory_to_string(char *s
, unsigned int maxlen
,
1090 crypto_pk_env_t
*private_key
);
1091 void directory_set_dirty(void);
1092 size_t dirserv_get_directory(const char **cp
);
1093 void dirserv_remove_old_servers(void);
1095 /********************************* dns.c ***************************/
1097 void dns_init(void);
1098 int connection_dns_finished_flushing(connection_t
*conn
);
1099 int connection_dns_process_inbuf(connection_t
*conn
);
1100 void connection_dns_remove(connection_t
*conn
);
1101 void assert_connection_edge_not_dns_pending(connection_t
*conn
);
1102 void assert_all_pending_dns_resolves_ok(void);
1103 void dns_cancel_pending_resolve(char *question
);
1104 int dns_resolve(connection_t
*exitconn
);
1106 /********************************* main.c ***************************/
1108 int connection_add(connection_t
*conn
);
1109 int connection_remove(connection_t
*conn
);
1111 void get_connection_array(connection_t
***array
, int *n
);
1113 void connection_watch_events(connection_t
*conn
, short events
);
1114 int connection_is_reading(connection_t
*conn
);
1115 void connection_stop_reading(connection_t
*conn
);
1116 void connection_start_reading(connection_t
*conn
);
1118 int connection_is_writing(connection_t
*conn
);
1119 void connection_stop_writing(connection_t
*conn
);
1120 void connection_start_writing(connection_t
*conn
);
1122 void directory_has_arrived(void);
1124 int main(int argc
, char *argv
[]);
1126 /********************************* onion.c ***************************/
1128 int decide_circ_id_type(char *local_nick
, char *remote_nick
);
1130 int onion_pending_add(circuit_t
*circ
);
1131 circuit_t
*onion_next_task(void);
1132 void onion_pending_remove(circuit_t
*circ
);
1134 int onionskin_answer(circuit_t
*circ
, unsigned char *payload
, unsigned char *keys
);
1137 void onion_append_to_cpath(crypt_path_t
**head_ptr
, crypt_path_t
*new_hop
);
1138 int onion_extend_cpath(crypt_path_t
**head_ptr
, cpath_build_state_t
*state
,
1139 routerinfo_t
**router_out
);
1141 int onion_skin_create(crypto_pk_env_t
*router_key
,
1142 crypto_dh_env_t
**handshake_state_out
,
1143 char *onion_skin_out
);
1145 int onion_skin_server_handshake(char *onion_skin
,
1146 crypto_pk_env_t
*private_key
,
1147 crypto_pk_env_t
*prev_private_key
,
1148 char *handshake_reply_out
,
1152 int onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
1153 char *handshake_reply
,
1157 cpath_build_state_t
*onion_new_cpath_build_state(uint8_t purpose
,
1158 const char *exit_nickname
);
1160 /********************************* relay.c ***************************/
1162 extern unsigned long stats_n_relay_cells_relayed
;
1163 extern unsigned long stats_n_relay_cells_delivered
;
1165 int circuit_receive_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1166 int cell_direction
);
1167 int circuit_package_relay_cell(cell_t
*cell
, circuit_t
*circ
,
1168 int cell_direction
, crypt_path_t
*layer_hint
);
1170 /********************************* rephist.c ***************************/
1172 void rep_hist_init(void);
1173 void rep_hist_note_connect_failed(const char* nickname
, time_t when
);
1174 void rep_hist_note_connect_succeeded(const char* nickname
, time_t when
);
1175 void rep_hist_note_disconnect(const char* nickname
, time_t when
);
1176 void rep_hist_note_connection_died(const char* nickname
, time_t when
);
1177 void rep_hist_note_extend_succeeded(const char *from_name
,
1178 const char *to_name
);
1179 void rep_hist_note_extend_failed(const char *from_name
, const char *to_name
);
1180 void rep_hist_dump_stats(time_t now
, int severity
);
1182 /********************************* rendclient.c ***************************/
1184 void rend_client_introcirc_is_open(circuit_t
*circ
);
1185 void rend_client_rendcirc_is_open(circuit_t
*circ
);
1186 int rend_client_introduction_acked(circuit_t
*circ
, const char *request
, int request_len
);
1187 void rend_client_refetch_renddesc(const char *query
);
1188 int rend_client_remove_intro_point(char *failed_intro
, const char *query
);
1189 int rend_client_rendezvous_acked(circuit_t
*circ
, const char *request
, int request_len
);
1190 int rend_client_receive_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1191 void rend_client_desc_fetched(char *query
, int success
);
1193 char *rend_client_get_random_intro(char *query
);
1194 int rend_parse_rendezvous_address(char *address
);
1196 int rend_client_send_establish_rendezvous(circuit_t
*circ
);
1197 int rend_client_send_introduction(circuit_t
*introcirc
, circuit_t
*rendcirc
);
1199 /********************************* rendcommon.c ***************************/
1201 typedef struct rend_service_descriptor_t
{
1202 crypto_pk_env_t
*pk
;
1205 char **intro_points
;
1206 } rend_service_descriptor_t
;
1208 int rend_cmp_service_ids(const char *one
, const char *two
);
1210 void rend_process_relay_cell(circuit_t
*circ
, int command
, int length
,
1211 const char *payload
);
1213 void rend_service_descriptor_free(rend_service_descriptor_t
*desc
);
1214 int rend_encode_service_descriptor(rend_service_descriptor_t
*desc
,
1215 crypto_pk_env_t
*key
,
1218 rend_service_descriptor_t
*rend_parse_service_descriptor(const char *str
, int len
);
1219 int rend_get_service_id(crypto_pk_env_t
*pk
, char *out
);
1221 typedef struct rend_cache_entry_t
{
1222 int len
; /* Length of desc */
1223 time_t received
; /* When did we get the descriptor? */
1224 char *desc
; /* Service descriptor */
1225 rend_service_descriptor_t
*parsed
; /* Parsed value of 'desc' */
1226 } rend_cache_entry_t
;
1228 void rend_cache_init(void);
1229 void rend_cache_clean(void);
1230 int rend_valid_service_id(const char *query
);
1231 int rend_cache_lookup_desc(const char *query
, const char **desc
, int *desc_len
);
1232 int rend_cache_lookup_entry(const char *query
, rend_cache_entry_t
**entry_out
);
1233 int rend_cache_store(const char *desc
, int desc_len
);
1235 /********************************* rendservice.c ***************************/
1237 int rend_config_services(or_options_t
*options
);
1238 int rend_service_load_keys(void);
1239 void rend_services_init(void);
1240 void rend_services_introduce(void);
1241 void rend_services_upload(int force
);
1243 void rend_service_intro_is_ready(circuit_t
*circuit
);
1244 int rend_service_intro_established(circuit_t
*circuit
, const char *request
, int request_len
);
1245 void rend_service_rendezvous_is_ready(circuit_t
*circuit
);
1246 int rend_service_introduce(circuit_t
*circuit
, const char *request
, int request_len
);
1247 void rend_service_relaunch_rendezvous(circuit_t
*oldcirc
);
1248 int rend_service_set_connection_addr_port(connection_t
*conn
, circuit_t
*circ
);
1249 void rend_service_dump_stats(int severity
);
1251 /********************************* rendmid.c *******************************/
1252 int rend_mid_establish_intro(circuit_t
*circ
, const char *request
, int request_len
);
1253 int rend_mid_introduce(circuit_t
*circ
, const char *request
, int request_len
);
1254 int rend_mid_establish_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1255 int rend_mid_rendezvous(circuit_t
*circ
, const char *request
, int request_len
);
1257 /********************************* router.c ***************************/
1259 void set_onion_key(crypto_pk_env_t
*k
);
1260 crypto_pk_env_t
*get_onion_key(void);
1261 crypto_pk_env_t
*get_previous_onion_key(void);
1262 time_t get_onion_key_set_at(void);
1263 void set_identity_key(crypto_pk_env_t
*k
);
1264 crypto_pk_env_t
*get_identity_key(void);
1265 int init_keys(void);
1266 crypto_pk_env_t
*init_key_from_file(const char *fname
);
1267 void rotate_onion_key(void);
1269 void router_retry_connections(void);
1270 void router_upload_dir_desc_to_dirservers(void);
1271 void router_post_to_dirservers(uint8_t purpose
, const char *payload
, int payload_len
);
1272 int router_compare_to_my_exit_policy(connection_t
*conn
);
1273 routerinfo_t
*router_get_my_routerinfo(void);
1274 const char *router_get_my_descriptor(void);
1275 int router_is_me(routerinfo_t
*router
);
1276 int router_rebuild_descriptor(void);
1277 int router_dump_router_to_string(char *s
, int maxlen
, routerinfo_t
*router
,
1278 crypto_pk_env_t
*ident_key
);
1280 /********************************* routerlist.c ***************************/
1282 routerinfo_t
*router_pick_directory_server(void);
1284 routerinfo_t
*router_choose_random_node(routerlist_t
*dir
,
1285 char *preferred
, char *excluded
,
1286 struct smartlist_t
*excludedsmartlist
);
1287 routerinfo_t
*router_get_by_addr_port(uint32_t addr
, uint16_t port
);
1288 routerinfo_t
*router_get_by_nickname(char *nickname
);
1289 void router_get_routerlist(routerlist_t
**prouterlist
);
1290 void routerlist_free(routerlist_t
*routerlist
);
1291 void routerinfo_free(routerinfo_t
*router
);
1292 routerinfo_t
*routerinfo_copy(const routerinfo_t
*router
);
1293 void router_mark_as_down(char *nickname
);
1294 int router_set_routerlist_from_file(char *routerfile
);
1295 int router_set_routerlist_from_string(const char *s
);
1296 int router_set_routerlist_from_directory(const char *s
, crypto_pk_env_t
*pkey
);
1297 int router_compare_addr_to_exit_policy(uint32_t addr
, uint16_t port
,
1298 struct exit_policy_t
*policy
);
1299 #define ADDR_POLICY_ACCEPTED 0
1300 #define ADDR_POLICY_REJECTED -1
1301 #define ADDR_POLICY_UNKNOWN 1
1302 int router_exit_policy_all_routers_reject(uint32_t addr
, uint16_t port
);
1303 int router_exit_policy_rejects_all(routerinfo_t
*router
);
1305 /********************************* routerparse.c ************************/
1307 int router_get_router_hash(const char *s
, char *digest
);
1308 int router_get_dir_hash(const char *s
, char *digest
);
1309 int router_parse_list_from_string(const char **s
,
1310 routerlist_t
**dest
,
1311 int n_good_nicknames
,
1312 const char **good_nickname_lst
);
1313 int router_parse_routerlist_from_directory(const char *s
,
1314 routerlist_t
**dest
,
1315 crypto_pk_env_t
*pkey
);
1316 routerinfo_t
*router_parse_entry_from_string(const char *s
, const char *end
);
1317 int router_add_exit_policy_from_string(routerinfo_t
*router
, const char *s
);
1324 indent-tabs-mode:nil