add geoff's NoPublish patch
[tor.git] / src / or / or.h
blob232eeb59601aa0370323a67b2fcf70961aa79649
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 */
5 /* $Id$ */
7 /**
8 * \file or.h
10 * \brief Master header file for Tor-specific functionality.
13 #ifndef __OR_H
14 #define __OR_H
15 #define OR_H_ID "$Id$"
17 #include "orconfig.h"
18 #ifdef MS_WINDOWS
19 #define WIN32_WINNT 0x400
20 #define _WIN32_WINNT 0x400
21 #define WIN32_LEAN_AND_MEAN
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <limits.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #ifdef HAVE_SIGNAL_H
34 #include <signal.h>
35 #endif
36 #ifdef HAVE_NETDB_H
37 #include <netdb.h>
38 #endif
39 #ifdef HAVE_CTYPE_H
40 #include <ctype.h>
41 #endif
42 #include "../common/torint.h"
43 #ifdef HAVE_INTTYPES_H
44 #include <inttypes.h>
45 #endif
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
48 #endif
49 #ifdef HAVE_SYS_LIMITS_H
50 #include <sys/limits.h>
51 #endif
52 #ifdef HAVE_MACHINE_LIMITS_H
53 #ifndef __FreeBSD__
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
56 I include both. */
57 #include <machine/limits.h>
58 #endif
59 #endif
60 #ifdef HAVE_SYS_TYPES_H
61 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
62 #endif
63 #ifdef HAVE_SYS_WAIT_H
64 #include <sys/wait.h>
65 #endif
66 #ifdef HAVE_SYS_FCNTL_H
67 #include <sys/fcntl.h>
68 #endif
69 #ifdef HAVE_FCNTL_H
70 #include <fcntl.h>
71 #endif
72 #ifdef HAVE_SYS_IOCTL_H
73 #include <sys/ioctl.h>
74 #endif
75 #ifdef HAVE_SYS_SOCKET_H
76 #include <sys/socket.h>
77 #endif
78 #ifdef HAVE_SYS_TIME_H
79 #include <sys/time.h>
80 #endif
81 #ifdef HAVE_SYS_STAT_H
82 #include <sys/stat.h>
83 #endif
84 #ifdef HAVE_NETINET_IN_H
85 #include <netinet/in.h>
86 #endif
87 #ifdef HAVE_ARPA_INET_H
88 #include <arpa/inet.h>
89 #endif
90 #ifdef HAVE_ERRNO_H
91 #include <errno.h>
92 #endif
93 #ifdef HAVE_ASSERT_H
94 #include <assert.h>
95 #endif
96 #ifdef HAVE_TIME_H
97 #include <time.h>
98 #endif
100 /** Upper bound on maximum simultaneous connections; can be lowered by
101 * config file. */
102 #define MAXCONNECTIONS 15000
104 #ifdef MS_WINDOWS
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)
114 #include <winsock.h>
115 #else
116 #include <winsock2.h>
117 #include <ws2tcpip.h>
118 #endif
119 #endif
121 #ifdef MS_WINDOWS
122 #include <io.h>
123 #include <process.h>
124 #include <direct.h>
125 #include <windows.h>
126 #define snprintf _snprintf
127 #endif
129 #ifdef HAVE_EVENT_H
130 #include <event.h>
131 #else
132 #error "Tor requires libevent to build."
133 #endif
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
147 #endif
149 /** Define this if you want Tor to crash when any problem comes up,
150 * so you can get a coredump and track things down. */
151 #undef TOR_FRAGILE
153 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
154 #define MAX_NICKNAME_LEN 19
155 /* Hex digest plus dollar sign. */
156 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
157 /** Maximum size, in bytes, for resized buffers. */
158 #define MAX_BUF_SIZE ((1<<24)-1)
159 #define MAX_DIR_SIZE MAX_BUF_SIZE
161 /* For http parsing */
162 #define MAX_HEADERS_SIZE 50000
163 #define MAX_BODY_SIZE 500000
165 #ifdef TOR_PERF
166 /** How long do we keep DNS cache entries before purging them? */
167 #define MAX_DNS_ENTRY_AGE (150*60)
168 #else
169 #define MAX_DNS_ENTRY_AGE (15*60)
170 #endif
172 /** How often do we rotate onion keys? */
173 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
174 /** How often do we rotate TLS contexts? */
175 #define MAX_SSL_KEY_LIFETIME (120*60)
177 /** How old do we allow a router to get before removing it, either
178 * from the descriptor list (for dirservers) or the router list (for others)?
179 * In seconds. */
180 #define ROUTER_MAX_AGE (60*60*24)
182 typedef enum {
183 CIRC_ID_TYPE_LOWER=0,
184 CIRC_ID_TYPE_HIGHER=1
185 } circ_id_type_t;
187 #define _CONN_TYPE_MIN 3
188 /** Type for sockets listening for OR connections. */
189 #define CONN_TYPE_OR_LISTENER 3
190 /** Type for OR-to-OR or OP-to-OR connections. */
191 #define CONN_TYPE_OR 4
192 /** Type for connections from final OR to chosen destination. */
193 #define CONN_TYPE_EXIT 5
194 /** Type for sockets listening for SOCKS connections. */
195 #define CONN_TYPE_AP_LISTENER 6
196 /** Type for SOCKS connections to OP. */
197 #define CONN_TYPE_AP 7
198 /** Type for sockets listening for HTTP connections to the directory server. */
199 #define CONN_TYPE_DIR_LISTENER 8
200 /** Type for HTTP connections to the directory server. */
201 #define CONN_TYPE_DIR 9
202 /** Type for connections to local dnsworker processes. */
203 #define CONN_TYPE_DNSWORKER 10
204 /** Type for connections to local cpuworker processes. */
205 #define CONN_TYPE_CPUWORKER 11
206 /** Type for listenting for connections from user interface process */
207 #define CONN_TYPE_CONTROL_LISTENER 12
208 /** Type for connections from user interface process */
209 #define CONN_TYPE_CONTROL 13
210 #define _CONN_TYPE_MAX 13
212 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
214 /** State for any listener connection. */
215 #define LISTENER_STATE_READY 0
217 #define _DNSWORKER_STATE_MIN 1
218 /** State for a connection to a dnsworker process that's idle. */
219 #define DNSWORKER_STATE_IDLE 1
220 /** State for a connection to a dnsworker process that's resolving a hostname. */
221 #define DNSWORKER_STATE_BUSY 2
222 #define _DNSWORKER_STATE_MAX 2
224 #define _CPUWORKER_STATE_MIN 1
225 /** State for a connection to a cpuworker process that's idle. */
226 #define CPUWORKER_STATE_IDLE 1
227 /** State for a connection to a cpuworker process that's processing a
228 * handshake. */
229 #define CPUWORKER_STATE_BUSY_ONION 2
230 #define _CPUWORKER_STATE_MAX 2
232 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
234 #define _OR_CONN_STATE_MIN 1
235 /** State for a connection to an OR: waiting for connect() to finish. */
236 #define OR_CONN_STATE_CONNECTING 1
237 /** State for a connection to an OR: waiting for proxy command to flush. */
238 #define OR_CONN_STATE_PROXY_FLUSHING 2
239 /** State for a connection to an OR: waiting for proxy response. */
240 #define OR_CONN_STATE_PROXY_READING 3
241 /** State for a connection to an OR: SSL is handshaking, not done yet. */
242 #define OR_CONN_STATE_HANDSHAKING 4
243 /** State for a connection to an OR: Ready to send/receive cells. */
244 #define OR_CONN_STATE_OPEN 5
245 #define _OR_CONN_STATE_MAX 5
247 #define _EXIT_CONN_STATE_MIN 1
248 /** State for an exit connection: waiting for response from dns farm. */
249 #define EXIT_CONN_STATE_RESOLVING 1
250 /** State for an exit connection: waiting for connect() to finish. */
251 #define EXIT_CONN_STATE_CONNECTING 2
252 /** State for an exit connection: open and ready to transmit data. */
253 #define EXIT_CONN_STATE_OPEN 3
254 /** State for an exit connection: waiting to be removed. */
255 #define EXIT_CONN_STATE_RESOLVEFAILED 4
256 #define _EXIT_CONN_STATE_MAX 4
257 #if 0
258 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
259 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
260 #endif
262 /* the AP state values must be disjoint from the EXIT state values */
263 #define _AP_CONN_STATE_MIN 5
264 /** State for a SOCKS connection: waiting for SOCKS request. */
265 #define AP_CONN_STATE_SOCKS_WAIT 5
266 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
267 * rendezvous rescriptor. */
268 #define AP_CONN_STATE_RENDDESC_WAIT 6
269 /** The controller will attach this connection to a circuit; it isn't our
270 * job to do so. */
271 #define AP_CONN_STATE_CONTROLLER_WAIT 7
272 /** State for a SOCKS connection: waiting for a completed circuit. */
273 #define AP_CONN_STATE_CIRCUIT_WAIT 8
274 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
275 #define AP_CONN_STATE_CONNECT_WAIT 9
276 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
277 #define AP_CONN_STATE_RESOLVE_WAIT 10
278 /** State for a SOCKS connection: ready to send and receive. */
279 #define AP_CONN_STATE_OPEN 11
280 #define _AP_CONN_STATE_MAX 11
282 #define _DIR_CONN_STATE_MIN 1
283 /** State for connection to directory server: waiting for connect(). */
284 #define DIR_CONN_STATE_CONNECTING 1
285 /** State for connection to directory server: sending HTTP request. */
286 #define DIR_CONN_STATE_CLIENT_SENDING 2
287 /** State for connection to directory server: reading HTTP response. */
288 #define DIR_CONN_STATE_CLIENT_READING 3
289 /** State for connection at directory server: waiting for HTTP request. */
290 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
291 /** State for connection at directory server: sending HTTP response. */
292 #define DIR_CONN_STATE_SERVER_WRITING 5
293 #define _DIR_CONN_STATE_MAX 5
295 #define _CONTROL_CONN_STATE_MIN 1
296 #define CONTROL_CONN_STATE_OPEN 1
297 #define CONTROL_CONN_STATE_NEEDAUTH 2
298 #define _CONTROL_CONN_STATE_MAX 2
300 #define _DIR_PURPOSE_MIN 1
301 /** Purpose for connection to directory server: download a directory. */
302 #define DIR_PURPOSE_FETCH_DIR 1
303 /** Purpose for connection to directory server: download just the list
304 * of running routers. */
305 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
306 /** Purpose for connection to directory server: download a rendezvous
307 * descriptor. */
308 #define DIR_PURPOSE_FETCH_RENDDESC 3
309 /** Purpose for connection to directory server: set after a rendezvous
310 * descriptor is downloaded. */
311 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
312 /** Purpose for connection to directory server: upload a server descriptor. */
313 #define DIR_PURPOSE_UPLOAD_DIR 5
314 /** Purpose for connection to directory server: upload a rendezvous
315 * descriptor. */
316 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
317 /** Purpose for connection at a directory server. */
318 #define DIR_PURPOSE_SERVER 7
319 #define _DIR_PURPOSE_MAX 7
321 #define _EXIT_PURPOSE_MIN 1
322 #define EXIT_PURPOSE_CONNECT 1
323 #define EXIT_PURPOSE_RESOLVE 2
324 #define _EXIT_PURPOSE_MAX 2
326 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
327 #define CIRCUIT_STATE_BUILDING 0
328 /** Circuit state: Waiting to process the onionskin. */
329 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
330 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
331 #define CIRCUIT_STATE_OR_WAIT 2
332 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
333 #define CIRCUIT_STATE_OPEN 3
335 #define _CIRCUIT_PURPOSE_MIN 1
337 /* these circuits were initiated elsewhere */
338 #define _CIRCUIT_PURPOSE_OR_MIN 1
339 /** OR-side circuit purpose: normal circuit, at OR. */
340 #define CIRCUIT_PURPOSE_OR 1
341 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
342 #define CIRCUIT_PURPOSE_INTRO_POINT 2
343 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
344 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
345 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
346 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
347 #define _CIRCUIT_PURPOSE_OR_MAX 4
349 /* these circuits originate at this node */
351 /* here's how circ client-side purposes work:
352 * normal circuits are C_GENERAL.
353 * circuits that are c_introducing are either on their way to
354 * becoming open, or they are open and waiting for a
355 * suitable rendcirc before they send the intro.
356 * circuits that are c_introduce_ack_wait have sent the intro,
357 * but haven't gotten a response yet.
358 * circuits that are c_establish_rend are either on their way
359 * to becoming open, or they are open and have sent the
360 * establish_rendezvous cell but haven't received an ack.
361 * circuits that are c_rend_ready are open and have received a
362 * rend ack, but haven't heard from bob yet. if they have a
363 * buildstate->pending_final_cpath then they're expecting a
364 * cell from bob, else they're not.
365 * circuits that are c_rend_ready_intro_acked are open, and
366 * some intro circ has sent its intro and received an ack.
367 * circuits that are c_rend_joined are open, have heard from
368 * bob, and are talking to him.
370 /** Client-side circuit purpose: Normal circuit, with cpath. */
371 #define CIRCUIT_PURPOSE_C_GENERAL 5
372 /** Client-side circuit purpose: at Alice, connecting to intro point. */
373 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
374 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
375 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
376 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
377 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
378 /** Client-side circuit purpose: at Alice, waiting for ack. */
379 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
380 /** Client-side circuit purpose: at Alice, waiting for Bob. */
381 #define CIRCUIT_PURPOSE_C_REND_READY 10
382 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
383 * has been acknowledged. */
384 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
385 /** Client-side circuit purpose: at Alice, rendezvous established. */
386 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
388 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
389 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
390 /** Hidden-service-side circuit purpose: at Bob, successfully established
391 * intro. */
392 #define CIRCUIT_PURPOSE_S_INTRO 14
393 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
394 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
395 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
396 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
397 /** A testing circuit; not meant to be used for actual traffic. */
398 #define CIRCUIT_PURPOSE_TESTING 17
399 #define _CIRCUIT_PURPOSE_MAX 17
401 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
402 * that this OP has originated. */
403 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
404 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
406 #define RELAY_COMMAND_BEGIN 1
407 #define RELAY_COMMAND_DATA 2
408 #define RELAY_COMMAND_END 3
409 #define RELAY_COMMAND_CONNECTED 4
410 #define RELAY_COMMAND_SENDME 5
411 #define RELAY_COMMAND_EXTEND 6
412 #define RELAY_COMMAND_EXTENDED 7
413 #define RELAY_COMMAND_TRUNCATE 8
414 #define RELAY_COMMAND_TRUNCATED 9
415 #define RELAY_COMMAND_DROP 10
416 #define RELAY_COMMAND_RESOLVE 11
417 #define RELAY_COMMAND_RESOLVED 12
419 #define RELAY_COMMAND_ESTABLISH_INTRO 32
420 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
421 #define RELAY_COMMAND_INTRODUCE1 34
422 #define RELAY_COMMAND_INTRODUCE2 35
423 #define RELAY_COMMAND_RENDEZVOUS1 36
424 #define RELAY_COMMAND_RENDEZVOUS2 37
425 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
426 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
427 #define RELAY_COMMAND_INTRODUCE_ACK 40
429 #define END_STREAM_REASON_MISC 1
430 #define END_STREAM_REASON_RESOLVEFAILED 2
431 #define END_STREAM_REASON_CONNECTREFUSED 3
432 #define END_STREAM_REASON_EXITPOLICY 4
433 #define END_STREAM_REASON_DESTROY 5
434 #define END_STREAM_REASON_DONE 6
435 #define END_STREAM_REASON_TIMEOUT 7
436 /* 8 is unallocated. */
437 #define END_STREAM_REASON_HIBERNATING 9
438 #define END_STREAM_REASON_INTERNAL 10
439 #define END_STREAM_REASON_RESOURCELIMIT 11
440 #define END_STREAM_REASON_CONNRESET 12
441 #define END_STREAM_REASON_TORPROTOCOL 13
443 /* These high-numbered end reasons are not part of the official spec,
444 * and are not intended to be put in relay end cells. They are here
445 * to be more informative when sending back socks replies to the
446 * application. */
447 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
448 #define END_STREAM_REASON_CANT_ATTACH 257
449 #define END_STREAM_REASON_NET_UNREACHABLE 258
451 #define RESOLVED_TYPE_HOSTNAME 0
452 #define RESOLVED_TYPE_IPV4 4
453 #define RESOLVED_TYPE_IPV6 6
454 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
455 #define RESOLVED_TYPE_ERROR 0xF1
457 /** Length of 'y' portion of 'y.onion' URL. */
458 #define REND_SERVICE_ID_LEN 16
460 #define CELL_DIRECTION_IN 1
461 #define CELL_DIRECTION_OUT 2
463 #ifdef TOR_PERF
464 #define CIRCWINDOW_START 10000
465 #define CIRCWINDOW_INCREMENT 1000
466 #define STREAMWINDOW_START 5000
467 #define STREAMWINDOW_INCREMENT 500
468 #else
469 #define CIRCWINDOW_START 1000
470 #define CIRCWINDOW_INCREMENT 100
471 #define STREAMWINDOW_START 500
472 #define STREAMWINDOW_INCREMENT 50
473 #endif
475 /* cell commands */
476 #define CELL_PADDING 0
477 #define CELL_CREATE 1
478 #define CELL_CREATED 2
479 #define CELL_RELAY 3
480 #define CELL_DESTROY 4
482 /* people behind fascist firewalls use only these ports */
483 #define REQUIRED_FIREWALL_DIRPORT 80
484 #define REQUIRED_FIREWALL_ORPORT 443
486 /* legal characters in a nickname */
487 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
489 /** Name to use in client TLS certificates if no nickname is given.*/
490 #define DEFAULT_CLIENT_NICKNAME "client"
492 #define SOCKS4_NETWORK_LEN 8
494 typedef enum {
495 SOCKS5_SUCCEEDED = 0x00,
496 SOCKS5_GENERAL_ERROR = 0x01,
497 SOCKS5_NOT_ALLOWED = 0x02,
498 SOCKS5_NET_UNREACHABLE = 0x03,
499 SOCKS5_HOST_UNREACHABLE = 0x04,
500 SOCKS5_CONNECTION_REFUSED = 0x05,
501 SOCKS5_TTL_EXPIRED = 0x06,
502 SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
503 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
504 } socks5_reply_status_t;
507 * Relay payload:
508 * Relay command [1 byte]
509 * Recognized [2 bytes]
510 * Stream ID [2 bytes]
511 * Partial SHA-1 [4 bytes]
512 * Length [2 bytes]
513 * Relay payload [498 bytes]
516 #define CELL_PAYLOAD_SIZE 509
517 #define CELL_NETWORK_SIZE 512
519 #define RELAY_HEADER_SIZE (1+2+2+4+2)
520 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
522 /** Parsed onion routing cell. All communication from OP-to-OR, or from
523 * OR-to-OR, is via cells. */
524 typedef struct {
525 uint16_t circ_id; /**< Circuit which received the cell. */
526 unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
527 * or DESTROY. */
528 unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
529 } cell_t;
531 /** Beginning of a RELAY cell payload. */
532 typedef struct {
533 uint8_t command; /**< The end-to-end relay command. */
534 uint16_t recognized; /**< Used to tell whether cell is for us. */
535 uint16_t stream_id; /**< Which stream is this cell associated with? */
536 char integrity[4]; /**< Used to tell whether cell is corrupted. */
537 uint16_t length; /**< How long is the payload body? */
538 } relay_header_t;
540 typedef struct buf_t buf_t;
541 typedef struct socks_request_t socks_request_t;
543 #define CONNECTION_MAGIC 0x7C3C304Eu
544 /** Description of a connection to another host or process, and associated
545 * data. */
546 struct connection_t {
547 uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
549 uint8_t type; /**< What kind of connection is this? */
550 uint8_t state; /**< Current state of this connection. */
551 uint8_t purpose; /**< Only used for DIR types currently. */
552 uint8_t wants_to_read; /**< Boolean: should we start reading again once
553 * the bandwidth throttler allows it?
555 uint8_t wants_to_write; /**< Boolean: should we start writing again once
556 * the bandwidth throttler allows reads?
558 int s; /**< Our socket; -1 if this connection is closed. */
559 int poll_index; /* XXXX rename. */
560 struct event *read_event; /**< libevent event structure. */
561 struct event *write_event; /**< libevent event structure. */
562 int marked_for_close; /**< Boolean: should we close this conn on the next
563 * iteration of the main loop?
565 const char *marked_for_close_file; /**< For debugging: in which file were
566 * we marked for close? */
567 int hold_open_until_flushed; /**< Despite this connection's being marked
568 * for close, do we flush it before closing it?
571 buf_t *inbuf; /**< Buffer holding data read over this connection. */
572 int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
573 time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
575 buf_t *outbuf; /**< Buffer holding data to write over this connection. */
576 size_t outbuf_flushlen; /**< How much data should we try to flush from the
577 * outbuf? */
578 time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
580 time_t timestamp_created; /**< When was this connection_t created? */
581 time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
583 uint32_t addr; /**< IP of the other side of the connection; used to identify
584 * routers, along with port. */
585 uint16_t port; /**< If non-zero, port on the other end
586 * of the connection. */
587 char *address; /**< FQDN (or IP) of the guy on the other end.
588 * strdup into this, because free_connection frees it.
590 crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
591 * signing key. */
592 char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
593 char *nickname; /**< Nickname of OR on other side (if any). */
595 /** Nickname of planned exit node -- to be used with .exit support. */
596 char *chosen_exit_name;
598 /* Used only by OR connections: */
599 tor_tls *tls; /**< TLS connection state (OR only.) */
600 uint16_t next_circ_id; /**< Which circ_id do we try to use next on
601 * this connection? This is always in the
602 * range 0..1<<15-1. (OR only.)*/
604 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
605 int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
606 int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
607 * add 'bandwidth' to this, capping it at 10*bandwidth.
608 * (OPEN ORs only)
610 circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
611 * connection, which half of the space should
612 * we use? */
614 /* Used only by DIR and AP connections: */
615 char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
616 * querying for? (DIR/AP only) */
618 /* Used only by edge connections: */
619 size_t stream_size; /**< Used for debugging. */
620 uint16_t stream_id;
621 struct connection_t *next_stream; /**< Points to the next stream at this
622 * edge, if any (Edge only). */
623 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
624 * this conn exits at. (Edge only.) */
625 int package_window; /**< How many more relay cells can i send into the
626 * circuit? (Edge only.) */
627 int deliver_window; /**< How many more relay cells can end at me? (Edge
628 * only.) */
630 int done_sending; /**< For half-open connections; not used currently. */
631 int done_receiving; /**< For half-open connections; not used currently. */
632 char has_sent_end; /**< For debugging: set once we've set the stream end,
633 and check in circuit_about_to_close_connection(). */
634 struct circuit_t *on_circuit; /**< DOCDOC */
636 /* Used only by AP connections */
637 socks_request_t *socks_request; /**< SOCKS structure describing request (AP
638 * only.) */
640 /** Quasi-global identifier for this connection; used for control.c */
641 /* XXXX NM This can get re-used after 2**32 circuits. */
642 uint32_t global_identifier;
644 /* Used only by control connections */
645 uint32_t event_mask;
648 typedef struct connection_t connection_t;
650 #define ADDR_POLICY_ACCEPT 1
651 #define ADDR_POLICY_REJECT 2
653 /** A linked list of policy rules */
654 typedef struct addr_policy_t {
655 char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
656 char *string; /**< String representation of this rule. */
657 uint32_t addr; /**< Base address to accept or reject. */
658 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
659 * a &amp; msk == <b>addr</b> &amp; msk . */
660 uint16_t prt_min; /**< Lowest port number to accept/reject. */
661 uint16_t prt_max; /**< Highest port number to accept/reject. */
663 struct addr_policy_t *next; /**< Next rule in list. */
664 } addr_policy_t;
666 /** Information about another onion router in the network. */
667 typedef struct {
668 char *signed_descriptor; /**< The original signed descriptor for this router*/
670 char *address; /**< Location of OR: either a hostname or an IP address. */
671 char *nickname; /**< Human-readable OR name. */
673 uint32_t addr; /**< IPv4 address of OR, in host order. */
674 uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
675 uint16_t dir_port; /**< Port for HTTP directory connections. */
677 time_t published_on; /**< When was the information in this routerinfo_t
678 * published? */
680 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
681 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
682 char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
684 char *platform; /**< What software/operating system is this OR using? */
686 /* link info */
687 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
688 * bucket per second? */
689 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
690 /** How many bytes/s is this router known to handle? */
691 uint32_t bandwidthcapacity;
692 addr_policy_t *exit_policy; /**< What streams will this OR permit
693 * to exit? */
694 long uptime; /**< How many seconds the router claims to have been up */
695 /* local info */
696 int is_running; /**< As far as we know, is this OR currently running? */
697 time_t status_set_at; /**< When did we last update is_running? */
698 int is_verified; /**< Has a trusted dirserver validated this OR? */
700 smartlist_t *declared_family; /**< Nicknames of router which this router
701 * claims are its family. */
702 } routerinfo_t;
704 /** Contents of a running-routers list */
705 typedef struct running_routers_t {
706 time_t published_on; /**< When was the list marked as published? */
707 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
708 smartlist_t *running_routers;
709 int is_running_routers_format; /**< Are we using the old entry format? */
710 } running_routers_t;
712 /** Contents of a directory of onion routers. */
713 typedef struct {
714 /** List of routerinfo_t */
715 smartlist_t *routers;
716 /** Which versions of tor are recommended by this directory? */
717 char *software_versions;
718 /** When was the most recent directory that contributed to this list
719 * published?
721 time_t published_on;
722 time_t running_routers_updated_on;
723 /** What is the most recently received running_routers structure? */
724 running_routers_t *running_routers;
725 /** Which router is claimed to have signed it? */
726 char *signing_router;
727 } routerlist_t;
729 #define CRYPT_PATH_MAGIC 0x70127012u
731 /** Holds accounting information for a single step in the layered encryption
732 * performed by a circuit. Used only at the client edge of a circuit. */
733 struct crypt_path_t {
734 uint32_t magic;
736 /* crypto environments */
737 /** Encryption key and counter for cells heading towards the OR at this
738 * step. */
739 crypto_cipher_env_t *f_crypto;
740 /** Encryption key and counter for cells heading back from the OR at this
741 * step. */
742 crypto_cipher_env_t *b_crypto;
744 /** Digest state for cells heading towards the OR at this step. */
745 crypto_digest_env_t *f_digest; /* for integrity checking */
746 /** Digest state for cells heading away from the OR at this step. */
747 crypto_digest_env_t *b_digest;
749 /** Current state of Diffie-Hellman key negotiation with the OR at this
750 * step. */
751 crypto_dh_env_t *handshake_state;
752 /** Negotiated key material shared with the OR at this step. */
753 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
755 /** IP4 address of the OR at this step. */
756 uint32_t addr;
757 /** Port of the OR at this step. */
758 uint16_t port;
759 /** Identity key digest of the OR at this step. */
760 char identity_digest[DIGEST_LEN];
762 /** Is the circuit built to this step? Must be one of:
763 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
764 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
765 * and not received an EXTENDED/CREATED)
766 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
767 uint8_t state;
768 #define CPATH_STATE_CLOSED 0
769 #define CPATH_STATE_AWAITING_KEYS 1
770 #define CPATH_STATE_OPEN 2
771 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
772 * (The list is circular, so the last node
773 * links to the first.) */
774 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
775 * circuit. */
777 int package_window; /**< How many bytes are we allowed to originate ending
778 * at this step? */
779 int deliver_window; /**< How many bytes are we willing to deliver originating
780 * at this step? */
783 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
785 #define DH_KEY_LEN DH_BYTES
786 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
787 CIPHER_KEY_LEN+\
788 DH_KEY_LEN)
789 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
790 #define REND_COOKIE_LEN DIGEST_LEN
792 typedef struct crypt_path_t crypt_path_t;
794 /** Information used to build a circuit. */
795 typedef struct {
796 /** Intended length of the final circuit. */
797 int desired_path_len;
798 /** Nickname of planned exit node. */
799 char *chosen_exit_name;
800 /** Identity of planned exit node. */
801 char chosen_exit_digest[DIGEST_LEN];
802 /** Whether every node in the circ must have adequate uptime. */
803 int need_uptime;
804 /** Whether every node in the circ must have adequate capacity. */
805 int need_capacity;
806 /** Whether the last hop was picked with exiting in mind. */
807 int is_internal;
808 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
809 struct crypt_path_t *pending_final_cpath;
810 /** How many times has building a circuit for this task failed? */
811 int failure_count;
812 /** At what time should we give up on this task? */
813 time_t expiry_time;
814 } cpath_build_state_t;
816 #define CIRCUIT_MAGIC 0x35315243u
817 /** Struct for a path (circuit) through the onion routing network. */
818 struct circuit_t {
819 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
821 int marked_for_close; /**< Should we close this circuit at the end of the
822 * main loop? */
823 const char *marked_for_close_file; /**< For debugging: in which file was this
824 * circuit marked for close? */
826 /** The IPv4 address of the OR that is next in this circuit. */
827 uint32_t n_addr;
828 /** The port for the OR that is next in this circuit. */
829 uint16_t n_port;
830 /** The OR connection that is previous in this circuit. */
831 connection_t *p_conn;
832 /** The OR connection that is next in this circuit. */
833 connection_t *n_conn;
834 /** The identity hash of n_conn. */
835 char n_conn_id_digest[DIGEST_LEN];
836 /** Linked list of AP streams associated with this circuit. */
837 connection_t *p_streams;
838 /** Linked list of Exit streams associated with this circuit. */
839 connection_t *n_streams;
840 /** Linked list of Exit streams associated with this circuit that are
841 * still being resolved. */
842 connection_t *resolving_streams;
843 /** The next stream_id that will be tried when we're attempting to
844 * construct a new AP stream originating at this circuit. */
845 uint16_t next_stream_id;
846 /** How many relay data cells can we package (read from edge streams)
847 * on this circuit before we receive a circuit-level sendme cell asking
848 * for more? */
849 int package_window;
850 /** How many relay data cells will we deliver (write to edge streams)
851 * on this circuit? When deliver_window gets low, we send some
852 * circuit-level sendme cells to indicate that we're willing to accept
853 * more. */
854 int deliver_window;
856 /** The circuit_id used in the previous (backward) hop of this circuit. */
857 uint16_t p_circ_id;
858 /** The circuit_id used in the next (forward) hop of this circuit. */
859 uint16_t n_circ_id;
861 /** The cipher used by intermediate hops for cells heading toward the
862 * OP. */
863 crypto_cipher_env_t *p_crypto;
864 /** The cipher used by intermediate hops for cells heading away from
865 * the OP. */
866 crypto_cipher_env_t *n_crypto;
868 /** The integrity-checking digest used by intermediate hops, for
869 * cells packaged here and heading towards the OP.
871 crypto_digest_env_t *p_digest;
872 /** The integrity-checking digest used by intermediate hops, for
873 * cells packaged at the OP and arriving here.
875 crypto_digest_env_t *n_digest;
877 /** Build state for this circuit. It includes the intended path
878 * length, the chosen exit router, rendezvous information, etc.
880 cpath_build_state_t *build_state;
881 /** The doubly-linked list of crypt_path_t entries, one per hop,
882 * for this circuit. This includes ciphers for each hop,
883 * integrity-checking digests for each hop, and package/delivery
884 * windows for each hop.
886 * The cpath field is defined only when we are the circuit's origin.
888 crypt_path_t *cpath;
890 /** For storage while passing to cpuworker, or while n_conn is pending. */
891 char onionskin[ONIONSKIN_CHALLENGE_LEN];
893 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
895 time_t timestamp_created; /**< When was this circuit created? */
896 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
897 * circuit is clean. */
899 uint8_t state; /**< Current status of this circuit. */
900 uint8_t purpose; /**< Why are we creating this circuit? */
903 * The rend_query field holds y portion of y.onion (nul-terminated)
904 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
905 * for a hidden service, or is S_*.
907 char rend_query[REND_SERVICE_ID_LEN+1];
909 /** The rend_pk_digest field holds a hash of location-hidden service's
910 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
912 char rend_pk_digest[DIGEST_LEN];
914 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
915 * C_ESTABLISH_REND. Filled with zeroes otherwise.
917 char rend_cookie[REND_COOKIE_LEN];
919 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
920 * is not marked for close. */
921 struct circuit_t *rend_splice;
923 /** Quasi-global identifier for this circuit; used for control.c */
924 /* XXXX NM This can get re-used after 2**32 circuits. */
925 uint32_t global_identifier;
927 struct circuit_t *next; /**< Next circuit in linked list. */
930 typedef struct circuit_t circuit_t;
932 #define ALLOW_UNVERIFIED_ENTRY 1
933 #define ALLOW_UNVERIFIED_EXIT 2
934 #define ALLOW_UNVERIFIED_MIDDLE 4
935 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
936 #define ALLOW_UNVERIFIED_INTRODUCTION 16
938 typedef struct exit_redirect_t {
939 uint32_t addr;
940 uint32_t mask;
941 uint16_t port_min;
942 uint16_t port_max;
944 int is_redirect;
945 uint32_t addr_dest;
946 uint16_t port_dest;
947 } exit_redirect_t;
949 /** Configuration options for a Tor process */
950 typedef struct {
951 /** What should the tor process actually do? */
952 enum {
953 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
954 } command;
955 const char *command_arg; /**< Argument for command-line option. */
957 struct config_line_t *OldLogOptions; /**< List of configuration lines
958 * for logfiles, old style. */
960 struct config_line_t *Logs; /**< New-style list of configuration lines
961 * for logs */
963 char *DebugLogFile; /**< Where to send verbose log messages. */
964 char *DataDirectory; /**< OR only: where to store long-term data. */
965 char *Nickname; /**< OR only: nickname of this onion router. */
966 char *Address; /**< OR only: configured address for this onion router. */
967 char *PidFile; /**< Where to store PID of Tor process. */
969 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
970 * as exits. */
971 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
972 * as entry points. */
973 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
974 * stop building circuits? */
975 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
976 * stop building circuits? */
977 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
978 * use in circuits. */
980 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
981 * points. */
982 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
983 * as introduction points. */
985 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
986 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
987 struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
988 struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
989 struct config_line_t *DirPolicy; /**< Lists of dir policy components */
990 /** Addresses to bind for listening for SOCKS connections. */
991 struct config_line_t *SocksBindAddress;
992 /** Addresses to bind for listening for OR connections. */
993 struct config_line_t *ORBindAddress;
994 /** Addresses to bind for listening for directory connections. */
995 struct config_line_t *DirBindAddress;
996 /** Local address to bind outbound sockets */
997 char *OutboundBindAddress;
998 /** Directory server only: which versions of
999 * Tor should we tell users to run? */
1000 struct config_line_t *RecommendedVersions;
1001 /** Whether dirservers refuse router descriptors with private IPs. */
1002 int DirAllowPrivateAddresses;
1003 char *User; /**< Name of user to run Tor as. */
1004 char *Group; /**< Name of group to run Tor as. */
1005 double PathlenCoinWeight; /**< Parameter used to configure average path
1006 * length (alpha in geometric distribution). */
1007 int ORPort; /**< Port to listen on for OR connections. */
1008 int SocksPort; /**< Port to listen on for SOCKS connections. */
1009 int ControlPort; /**< Port to listen on for control connections. */
1010 int DirPort; /**< Port to listen on for directory connections. */
1011 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
1012 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
1013 int NoPublish; /**< Boolean: should we never publish a descriptor? */
1014 int ConnLimit; /**< Requested maximum number of simultaneous connections. */
1015 int _ConnLimit; /**< Actual maximum number of simultaneous connections. */
1016 int IgnoreVersion; /**< If true, run no matter what versions of Tor the
1017 * directory recommends. */
1018 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
1019 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
1020 smartlist_t *FirewallPorts; /**< Which ports our firewall allows (strings). */
1021 /** Application ports that require all nodes in circ to have sufficient uptime. */
1022 smartlist_t *LongLivedPorts;
1023 /** Should we try to reuse the same exit node for a given host */
1024 smartlist_t *TrackHostExits;
1025 int TrackHostExitsExpire; /**< Number of seconds until we expire an addressmap */
1026 struct config_line_t *AddressMap; /**< List of address map directives. */
1027 int DirFetchPeriod; /**< How often do we fetch new directories? */
1028 int DirPostPeriod; /**< How often do we post our server descriptor to the
1029 * authoritative directory servers? */
1030 int RendPostPeriod; /**< How often do we post each rendezvous service
1031 * descriptor? Remember to publish them independently. */
1032 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
1033 int KeepalivePeriod; /**< How often do we send padding cells to keep
1034 * connections alive? */
1035 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
1036 * to wait simultaneously before we start dropping
1037 * them? */
1038 int NewCircuitPeriod; /**< How long do we use a circuit before building
1039 * a new one? */
1040 int MaxCircuitDirtiness; /**< Never use circs that were first used more than
1041 this interval ago. */
1042 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
1043 * use in a second? */
1044 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
1045 * use in a second? */
1046 uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
1047 * tell people we have? */
1048 int NumCpus; /**< How many CPUs should we try to use? */
1049 int RunTesting; /**< If true, create testing circuits to measure how well the
1050 * other ORs are running. */
1051 struct config_line_t *RendConfigLines; /**< List of configuration lines
1052 * for rendezvous services. */
1053 char *ContactInfo; /**< Contact info to be published in the directory */
1055 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
1056 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
1057 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
1059 char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
1060 uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
1061 uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
1063 struct config_line_t *DirServers; /**< List of configuration lines
1064 * for directory servers. */
1065 char *MyFamily; /**< Declared family for this OR. */
1066 struct config_line_t *NodeFamilies; /**< List of config lines for
1067 * node families */
1068 struct config_line_t *RedirectExit; /**< List of config lines for simple
1069 * addr/port redirection */
1070 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
1071 int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
1072 * interval starts */
1073 char *AccountingStart; /** How long is the accounting interval, and when
1074 * does it start? */
1075 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
1076 * interval before hibernation? 0 for "never
1077 * hibernate." */
1078 int _AccountingMaxKB; /**< How many KB do we allow per accounting
1079 * interval before hibernation? 0 for "never
1080 * hibernate." (Based on a deprecated option)*/
1082 char *HashedControlPassword; /**< Base64-encoded hash of a password for
1083 * the control system. */
1084 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
1085 * the control system? */
1086 int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
1087 * circuits itself (0), or does it expect a controller
1088 * to cope? (1) */
1089 int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
1090 * long do we wait before exiting? */
1091 } or_options_t;
1093 #define MAX_SOCKS_REPLY_LEN 1024
1094 #define MAX_SOCKS_ADDR_LEN 256
1095 #define SOCKS_COMMAND_CONNECT 0x01
1096 #define SOCKS_COMMAND_RESOLVE 0xF0
1097 /** State of a SOCKS request from a user to an OP */
1098 struct socks_request_t {
1099 char socks_version; /**< Which version of SOCKS did the client use? */
1100 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
1101 size_t replylen; /**< Length of <b>reply</b>. */
1102 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
1103 * we want to specify our own socks reply,
1104 * rather than using the default socks4 or
1105 * socks5 socks reply. We use this for the
1106 * two-stage socks5 handshake.
1108 int has_finished; /**< Has the SOCKS handshake finished? */
1109 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
1110 uint16_t port; /**< What port did the client ask to connect to? */
1113 /* all the function prototypes go here */
1115 /********************************* buffers.c ***************************/
1117 buf_t *buf_new(void);
1118 buf_t *buf_new_with_capacity(size_t size);
1119 void buf_free(buf_t *buf);
1120 void buf_clear(buf_t *buf);
1122 size_t buf_datalen(const buf_t *buf);
1123 size_t buf_capacity(const buf_t *buf);
1124 const char *_buf_peek_raw_buffer(const buf_t *buf);
1126 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1127 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
1129 int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
1130 int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
1132 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1133 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1134 int fetch_from_buf_http(buf_t *buf,
1135 char **headers_out, size_t max_headerlen,
1136 char **body_out, size_t *body_used, size_t max_bodylen);
1137 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
1138 int fetch_from_buf_control(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
1139 char **body_out);
1141 void assert_buf_ok(buf_t *buf);
1143 /********************************* circuitbuild.c **********************/
1145 char *circuit_list_path(circuit_t *circ, int verbose);
1146 void circuit_log_path(int severity, circuit_t *circ);
1147 void circuit_rep_hist_note_result(circuit_t *circ);
1148 void circuit_dump_by_conn(connection_t *conn, int severity);
1149 circuit_t *circuit_init(uint8_t purpose, int need_uptime,
1150 int need_capacity, int internal);
1151 circuit_t *circuit_establish_circuit(uint8_t purpose, routerinfo_t *exit,
1152 int need_uptime, int need_capacity, int internal);
1153 int circuit_handle_first_hop(circuit_t *circ);
1154 void circuit_n_conn_done(connection_t *or_conn, int status);
1155 int circuit_send_next_onion_skin(circuit_t *circ);
1156 void circuit_note_clock_jumped(int seconds_elapsed);
1157 int circuit_extend(cell_t *cell, circuit_t *circ);
1158 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
1159 int circuit_finish_handshake(circuit_t *circ, char *reply);
1160 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1161 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
1162 int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
1163 int *need_capacity);
1165 int circuit_append_new_exit(circuit_t *circ, routerinfo_t *exit);
1166 int circuit_extend_to_new_exit(circuit_t *circ, routerinfo_t *exit);
1167 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1169 /********************************* circuitlist.c ***********************/
1171 const char *circuit_state_to_string(int state);
1172 enum which_conn_changed_t { P_CONN_CHANGED=1, N_CONN_CHANGED=0 };
1173 void circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
1174 connection_t *conn,
1175 enum which_conn_changed_t which);
1176 void circuit_close_all_marked(void);
1177 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1178 circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn);
1179 circuit_t *circuit_get_by_edge_conn(connection_t *conn);
1180 circuit_t *circuit_get_by_conn(connection_t *conn);
1181 circuit_t *circuit_get_by_global_id(uint32_t id);
1182 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
1183 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1184 const char *digest, uint8_t purpose);
1185 circuit_t *circuit_get_rendezvous(const char *cookie);
1186 circuit_t *circuit_get_clean_open(uint8_t purpose, int need_uptime,
1187 int need_capacity, int internal);
1188 void circuit_mark_all_unused_circs(void);
1189 void _circuit_mark_for_close(circuit_t *circ, int line, const char *file);
1191 #define circuit_mark_for_close(c) \
1192 _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
1194 void assert_cpath_layer_ok(const crypt_path_t *cp);
1195 void assert_circuit_ok(const circuit_t *c);
1196 void circuit_free_all(void);
1198 /********************************* circuituse.c ************************/
1200 void circuit_expire_building(time_t now);
1201 void circuit_remove_handled_ports(smartlist_t *needed_ports);
1202 int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
1203 void circuit_build_needed_circs(time_t now);
1204 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1205 void circuit_about_to_close_connection(connection_t *conn);
1206 void circuit_has_opened(circuit_t *circ);
1207 void circuit_build_failed(circuit_t *circ);
1208 circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
1209 int need_uptime, int need_capacity, int is_internal);
1210 circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
1211 int need_uptime, int need_capacity, int is_internal);
1212 void circuit_reset_failure_count(int timeout);
1213 int connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
1214 circuit_t *circ);
1215 int connection_ap_handshake_attach_circuit(connection_t *conn);
1217 /********************************* command.c ***************************/
1219 void command_process_cell(cell_t *cell, connection_t *conn);
1221 extern unsigned long stats_n_padding_cells_processed;
1222 extern unsigned long stats_n_create_cells_processed;
1223 extern unsigned long stats_n_created_cells_processed;
1224 extern unsigned long stats_n_relay_cells_processed;
1225 extern unsigned long stats_n_destroy_cells_processed;
1227 /********************************* config.c ***************************/
1229 struct config_line_t {
1230 char *key;
1231 char *value;
1232 struct config_line_t *next;
1235 or_options_t *get_options(void);
1236 void set_options(or_options_t *new_val);
1237 int options_act(void);
1238 void config_free_all(void);
1240 int config_get_lines(char *string, struct config_line_t **result);
1241 void config_free_lines(struct config_line_t *front);
1242 int config_trial_assign(struct config_line_t *list, int reset);
1243 int resolve_my_address(const char *address, uint32_t *addr);
1244 void options_init(or_options_t *options);
1245 int init_from_config(int argc, char **argv);
1246 int config_init_logs(or_options_t *options, int validate_only);
1247 int config_parse_addr_policy(struct config_line_t *cfg,
1248 addr_policy_t **dest);
1249 void addr_policy_free(addr_policy_t *p);
1250 int config_option_is_recognized(const char *key);
1251 struct config_line_t *config_get_assigned_option(or_options_t *options,
1252 const char *key);
1253 char *config_dump_options(or_options_t *options, int minimal);
1254 int save_current_config(void);
1256 /********************************* connection.c ***************************/
1258 const char *conn_type_to_string(int type);
1259 const char *conn_state_to_string(int type, int state);
1261 connection_t *connection_new(int type);
1262 void connection_unregister(connection_t *conn);
1263 void connection_free(connection_t *conn);
1264 void connection_free_all(void);
1265 void connection_about_to_close_connection(connection_t *conn);
1266 void connection_close_immediate(connection_t *conn);
1267 void _connection_mark_for_close(connection_t *conn,int line, const char *file);
1269 #define connection_mark_for_close(c) \
1270 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1272 void connection_expire_held_open(void);
1274 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
1275 int retry_all_listeners(int force);
1277 void connection_bucket_init(void);
1278 void connection_bucket_refill(struct timeval *now);
1280 int connection_handle_read(connection_t *conn);
1282 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1284 int connection_wants_to_flush(connection_t *conn);
1285 int connection_outbuf_too_full(connection_t *conn);
1286 int connection_handle_write(connection_t *conn);
1287 void _connection_controller_force_write(connection_t *conn);
1288 void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
1290 connection_t *connection_or_exact_get_by_addr_port(uint32_t addr, uint16_t port);
1291 connection_t *connection_get_by_identity_digest(const char *digest, int type);
1292 connection_t *connection_get_by_global_id(uint32_t id);
1294 connection_t *connection_get_by_type(int type);
1295 connection_t *connection_get_by_type_state(int type, int state);
1296 connection_t *connection_get_by_type_purpose(int type, int purpose);
1297 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1298 connection_t *connection_get_by_type_state_rendquery(int type, int state, const char *rendquery);
1300 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1301 #define connection_has_pending_tls_data(conn) \
1302 ((conn)->type == CONN_TYPE_OR && \
1303 (conn)->state == OR_CONN_STATE_OPEN && \
1304 tor_tls_get_pending_bytes((conn)->tls))
1305 int connection_is_listener(connection_t *conn);
1306 int connection_state_is_open(connection_t *conn);
1307 int connection_state_is_connecting(connection_t *conn);
1309 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
1311 void assert_connection_ok(connection_t *conn, time_t now);
1312 int connection_or_nonopen_was_started_here(connection_t *conn);
1314 /********************************* connection_edge.c ***************************/
1316 #define connection_mark_unattached_ap(conn, endreason) \
1317 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1319 void _connection_mark_unattached_ap(connection_t *conn, int endreason,
1320 int line, const char *file);
1321 int connection_edge_reached_eof(connection_t *conn);
1322 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1323 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1324 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
1325 int connection_edge_end_errno(connection_t *conn, crypt_path_t *cpath_layer);
1326 int connection_edge_finished_flushing(connection_t *conn);
1327 int connection_edge_finished_connecting(connection_t *conn);
1329 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1330 int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
1332 int connection_ap_make_bridge(char *address, uint16_t port);
1333 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1334 size_t replylen,
1335 socks5_reply_status_t status);
1336 void connection_ap_handshake_socks_resolved(connection_t *conn,
1337 int answer_type,
1338 size_t answer_len,
1339 const char *answer);
1341 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1342 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1343 void connection_exit_connect(connection_t *conn);
1344 int connection_edge_is_rendezvous_stream(connection_t *conn);
1345 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1346 void connection_ap_expire_beginning(void);
1347 void connection_ap_attach_pending(void);
1348 int connection_ap_detach_retriable(connection_t *conn, circuit_t *circ);
1350 void addressmap_init(void);
1351 void addressmap_clean(time_t now);
1352 void addressmap_clear_configured(void);
1353 void addressmap_clear_transient(void);
1354 void addressmap_free_all(void);
1355 void addressmap_rewrite(char *address, size_t maxlen);
1356 int addressmap_already_mapped(const char *address);
1357 void addressmap_register(const char *address, char *new_address, time_t expires);
1358 int client_dns_incr_failures(const char *address);
1359 void client_dns_set_addressmap(const char *address, uint32_t val, const char *exitname);
1360 const char *addressmap_register_virtual_address(int type, char *new_address);
1361 void addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires);
1363 void parse_socks_policy(void);
1364 void free_socks_policy(void);
1365 int socks_policy_permits_address(uint32_t addr);
1367 void set_exit_redirects(smartlist_t *lst);
1368 typedef enum hostname_type_t {
1369 NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME
1370 } hostname_type_t;
1371 hostname_type_t parse_extended_hostname(char *address);
1373 /********************************* connection_or.c ***************************/
1375 int connection_or_reached_eof(connection_t *conn);
1376 int connection_or_process_inbuf(connection_t *conn);
1377 int connection_or_finished_flushing(connection_t *conn);
1378 int connection_or_finished_connecting(connection_t *conn);
1380 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1381 const char *id_digest);
1383 int connection_tls_start_handshake(connection_t *conn, int receiving);
1384 int connection_tls_continue_handshake(connection_t *conn);
1386 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1387 void connection_or_update_nickname(connection_t *conn);
1389 /********************************* control.c ***************************/
1391 typedef enum circuit_status_event_t {
1392 CIRC_EVENT_LAUNCHED = 0,
1393 CIRC_EVENT_BUILT = 1,
1394 CIRC_EVENT_EXTENDED = 2,
1395 CIRC_EVENT_FAILED = 3,
1396 CIRC_EVENT_CLOSED = 4,
1397 } circuit_status_event_t;
1399 typedef enum stream_status_event_t {
1400 STREAM_EVENT_SENT_CONNECT = 0,
1401 STREAM_EVENT_SENT_RESOLVE = 1,
1402 STREAM_EVENT_SUCCEEDED = 2,
1403 STREAM_EVENT_FAILED = 3,
1404 STREAM_EVENT_CLOSED = 4,
1405 STREAM_EVENT_NEW = 5,
1406 STREAM_EVENT_NEW_RESOLVE = 6,
1407 STREAM_EVENT_FAILED_RETRIABLE = 7
1408 } stream_status_event_t;
1410 typedef enum or_conn_status_event_t {
1411 OR_CONN_EVENT_LAUNCHED = 0,
1412 OR_CONN_EVENT_CONNECTED = 1,
1413 OR_CONN_EVENT_FAILED = 2,
1414 OR_CONN_EVENT_CLOSED = 3,
1415 } or_conn_status_event_t;
1417 void adjust_event_log_severity(void);
1418 int connection_control_finished_flushing(connection_t *conn);
1419 int connection_control_reached_eof(connection_t *conn);
1420 int connection_control_process_inbuf(connection_t *conn);
1422 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1423 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1424 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1425 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1426 void control_event_logmsg(int severity, const char *msg);
1427 int control_event_descriptors_changed(smartlist_t *routers);
1429 int init_cookie_authentication(int enabled);
1430 int decode_hashed_password(char *buf, const char *hashed);
1432 /********************************* cpuworker.c *****************************/
1434 void cpu_init(void);
1435 void cpuworkers_rotate(void);
1436 int connection_cpu_finished_flushing(connection_t *conn);
1437 int connection_cpu_reached_eof(connection_t *conn);
1438 int connection_cpu_process_inbuf(connection_t *conn);
1439 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
1440 void *task);
1442 /********************************* directory.c ***************************/
1444 int dir_policy_permits_address(uint32_t addr);
1445 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1446 size_t payload_len);
1447 void directory_get_from_dirserver(uint8_t purpose, const char *resource,
1448 int retry_if_no_servers);
1449 void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
1450 int private_connection, const char *resource,
1451 const char *payload, size_t payload_len);
1453 int parse_http_response(const char *headers, int *code, time_t *date,
1454 int *compression, char **response);
1456 int connection_dir_reached_eof(connection_t *conn);
1457 int connection_dir_process_inbuf(connection_t *conn);
1458 int connection_dir_finished_flushing(connection_t *conn);
1459 int connection_dir_finished_connecting(connection_t *conn);
1460 void connection_dir_connect_failed(connection_t *conn);
1461 void parse_dir_policy(void);
1462 void free_dir_policy(void);
1464 /********************************* dirserv.c ***************************/
1466 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1467 int dirserv_parse_fingerprint_file(const char *fname);
1468 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
1469 void dirserv_free_fingerprint_list(void);
1470 const char *dirserv_get_nickname_by_digest(const char *digest);
1471 int dirserv_add_descriptor(const char **desc, const char **msg);
1472 int dirserv_load_from_directory_string(const char *dir);
1473 void dirserv_free_descriptors(void);
1474 int list_server_status(smartlist_t *routers,
1475 char **running_routers_out, char **router_status_out);
1476 void dirserv_remove_old_servers(int age);
1477 int dirserv_dump_directory_to_string(char **dir_out,
1478 crypto_pk_env_t *private_key);
1479 void directory_set_dirty(void);
1480 size_t dirserv_get_directory(const char **cp, int compress);
1481 size_t dirserv_get_runningrouters(const char **rr, int compress);
1482 void dirserv_set_cached_directory(const char *directory, time_t when,
1483 int is_running_routers);
1484 void dirserv_orconn_tls_done(const char *address,
1485 uint16_t or_port,
1486 const char *digest_rcvd,
1487 const char *nickname);
1488 void dirserv_free_all(void);
1490 /********************************* dns.c ***************************/
1492 void dns_init(void);
1493 void dns_free_all(void);
1494 int connection_dns_finished_flushing(connection_t *conn);
1495 int connection_dns_reached_eof(connection_t *conn);
1496 int connection_dns_process_inbuf(connection_t *conn);
1497 void dnsworkers_rotate(void);
1498 void connection_dns_remove(connection_t *conn);
1499 void assert_connection_edge_not_dns_pending(connection_t *conn);
1500 void assert_all_pending_dns_resolves_ok(void);
1501 void dns_cancel_pending_resolve(char *question);
1502 int dns_resolve(connection_t *exitconn);
1504 /********************************* hibernate.c **********************/
1506 int accounting_parse_options(or_options_t *options, int validate_only);
1507 int accounting_is_enabled(or_options_t *options);
1508 void configure_accounting(time_t now);
1509 void accounting_run_housekeeping(time_t now);
1510 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1511 int accounting_record_bandwidth_usage(time_t now);
1512 void hibernate_begin_shutdown(void);
1513 int we_are_hibernating(void);
1514 void consider_hibernation(time_t now);
1516 /********************************* main.c ***************************/
1518 int connection_add(connection_t *conn);
1519 int connection_remove(connection_t *conn);
1520 int connection_in_array(connection_t *conn);
1521 void add_connection_to_closeable_list(connection_t *conn);
1522 int connection_is_on_closeable_list(connection_t *conn);
1524 void get_connection_array(connection_t ***array, int *n);
1526 void connection_watch_events(connection_t *conn, short events);
1527 int connection_is_reading(connection_t *conn);
1528 void connection_stop_reading(connection_t *conn);
1529 void connection_start_reading(connection_t *conn);
1531 int connection_is_writing(connection_t *conn);
1532 void connection_stop_writing(connection_t *conn);
1533 void connection_start_writing(connection_t *conn);
1535 void directory_all_unreachable(time_t now);
1536 void directory_has_arrived(time_t now, char *identity_digest);
1538 int control_signal_act(int the_signal);
1539 void handle_signals(int is_parent);
1540 void tor_cleanup(void);
1542 int tor_main(int argc, char *argv[]);
1544 /********************************* onion.c ***************************/
1546 int onion_pending_add(circuit_t *circ);
1547 circuit_t *onion_next_task(void);
1548 void onion_pending_remove(circuit_t *circ);
1550 int onion_skin_create(crypto_pk_env_t *router_key,
1551 crypto_dh_env_t **handshake_state_out,
1552 char *onion_skin_out);
1554 int onion_skin_server_handshake(char *onion_skin,
1555 crypto_pk_env_t *private_key,
1556 crypto_pk_env_t *prev_private_key,
1557 char *handshake_reply_out,
1558 char *key_out,
1559 size_t key_out_len);
1561 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
1562 char *handshake_reply,
1563 char *key_out,
1564 size_t key_out_len);
1566 void clear_pending_onions(void);
1568 /********************************* relay.c ***************************/
1570 extern unsigned long stats_n_relay_cells_relayed;
1571 extern unsigned long stats_n_relay_cells_delivered;
1573 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
1574 int cell_direction);
1576 void relay_header_pack(char *dest, const relay_header_t *src);
1577 void relay_header_unpack(relay_header_t *dest, const char *src);
1578 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
1579 int relay_command, const char *payload,
1580 size_t payload_len, crypt_path_t *cpath_layer);
1581 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
1582 void connection_edge_consider_sending_sendme(connection_t *conn);
1583 socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
1584 int errno_to_end_reason(int e);
1586 extern uint64_t stats_n_data_cells_packaged;
1587 extern uint64_t stats_n_data_bytes_packaged;
1588 extern uint64_t stats_n_data_cells_received;
1589 extern uint64_t stats_n_data_bytes_received;
1591 /********************************* rephist.c ***************************/
1593 void rep_hist_init(void);
1594 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1595 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1596 void rep_hist_note_disconnect(const char* nickname, time_t when);
1597 void rep_hist_note_connection_died(const char* nickname, time_t when);
1598 void rep_hist_note_extend_succeeded(const char *from_name,
1599 const char *to_name);
1600 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1601 void rep_hist_dump_stats(time_t now, int severity);
1602 void rep_hist_note_bytes_read(int num_bytes, time_t when);
1603 void rep_hist_note_bytes_written(int num_bytes, time_t when);
1604 int rep_hist_bandwidth_assess(void);
1605 char *rep_hist_get_bandwidth_lines(void);
1606 void rep_history_clean(time_t before);
1608 void rep_hist_note_used_port(uint16_t port, time_t now);
1609 smartlist_t *rep_hist_get_predicted_ports(time_t now);
1610 void rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity);
1611 int rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity);
1612 void rep_hist_note_used_resolve(time_t now);
1613 int rep_hist_get_predicted_resolve(time_t now);
1615 void rep_hist_free_all(void);
1617 /********************************* rendclient.c ***************************/
1619 void rend_client_introcirc_has_opened(circuit_t *circ);
1620 void rend_client_rendcirc_has_opened(circuit_t *circ);
1621 int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
1622 void rend_client_refetch_renddesc(const char *query);
1623 int rend_client_remove_intro_point(char *failed_intro, const char *query);
1624 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
1625 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1626 void rend_client_desc_here(char *query);
1628 char *rend_client_get_random_intro(char *query);
1630 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1632 /********************************* rendcommon.c ***************************/
1634 typedef struct rend_service_descriptor_t {
1635 crypto_pk_env_t *pk;
1636 time_t timestamp;
1637 int n_intro_points;
1638 char **intro_points;
1639 } rend_service_descriptor_t;
1641 int rend_cmp_service_ids(const char *one, const char *two);
1643 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
1644 const char *payload);
1646 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1647 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1648 crypto_pk_env_t *key,
1649 char **str_out,
1650 size_t *len_out);
1651 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
1652 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1654 typedef struct rend_cache_entry_t {
1655 size_t len; /* Length of desc */
1656 time_t received; /* When did we get the descriptor? */
1657 char *desc; /* Service descriptor */
1658 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1659 } rend_cache_entry_t;
1661 void rend_cache_init(void);
1662 void rend_cache_clean(void);
1663 void rend_cache_free_all(void);
1664 int rend_valid_service_id(const char *query);
1665 int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
1666 int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
1667 int rend_cache_store(const char *desc, size_t desc_len);
1669 /********************************* rendservice.c ***************************/
1671 int rend_config_services(or_options_t *options, int validate_only);
1672 int rend_service_load_keys(void);
1673 void rend_services_init(void);
1674 void rend_services_introduce(void);
1675 void rend_consider_services_upload(time_t now);
1677 void rend_service_intro_has_opened(circuit_t *circuit);
1678 int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
1679 void rend_service_rendezvous_has_opened(circuit_t *circuit);
1680 int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
1681 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1682 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1683 void rend_service_dump_stats(int severity);
1684 void rend_service_free_all(void);
1686 /********************************* rendmid.c *******************************/
1687 int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
1688 int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
1689 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1690 int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1692 /********************************* router.c ***************************/
1693 typedef enum {
1694 ADDR_POLICY_ACCEPTED=0,
1695 ADDR_POLICY_REJECTED=-1,
1696 ADDR_POLICY_PROBABLY_ACCEPTED=1,
1697 ADDR_POLICY_PROBABLY_REJECTED=2
1698 } addr_policy_result_t;
1700 void set_onion_key(crypto_pk_env_t *k);
1701 crypto_pk_env_t *get_onion_key(void);
1702 crypto_pk_env_t *get_previous_onion_key(void);
1703 time_t get_onion_key_set_at(void);
1704 void set_identity_key(crypto_pk_env_t *k);
1705 crypto_pk_env_t *get_identity_key(void);
1706 int identity_key_is_set(void);
1707 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
1708 void rotate_onion_key(void);
1709 crypto_pk_env_t *init_key_from_file(const char *fname);
1710 int init_keys(void);
1712 int check_whether_orport_reachable(void);
1713 int check_whether_dirport_reachable(void);
1714 void consider_testing_reachability(void);
1715 void router_orport_found_reachable(void);
1716 void router_dirport_found_reachable(void);
1717 void server_has_changed_ip(void);
1718 void consider_publishable_server(time_t now, int force);
1720 int authdir_mode(or_options_t *options);
1721 int clique_mode(or_options_t *options);
1722 int server_mode(or_options_t *options);
1723 int advertised_server_mode(void);
1724 int proxy_mode(or_options_t *options);
1726 void router_retry_connections(void);
1727 int router_is_clique_mode(routerinfo_t *router);
1728 void router_upload_dir_desc_to_dirservers(int force);
1729 void mark_my_descriptor_dirty(void);
1730 int router_compare_to_my_exit_policy(connection_t *conn);
1731 routerinfo_t *router_get_my_routerinfo(void);
1732 const char *router_get_my_descriptor(void);
1733 int router_is_me(routerinfo_t *router);
1734 int router_rebuild_descriptor(int force);
1735 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
1736 crypto_pk_env_t *ident_key);
1737 int is_legal_nickname(const char *s);
1738 int is_legal_nickname_or_hexdigest(const char *s);
1739 void router_free_all_keys(void);
1741 /********************************* routerlist.c ***************************/
1743 typedef struct trusted_dir_server_t {
1744 char *address;
1745 uint32_t addr;
1746 uint16_t dir_port;
1747 char digest[DIGEST_LEN];
1748 int is_running;
1749 } trusted_dir_server_t;
1751 int router_reload_router_list(void);
1752 void router_get_trusted_dir_servers(smartlist_t **outp);
1753 routerinfo_t *router_pick_directory_server(int requireothers,
1754 int fascistfirewall,
1755 int for_running_routers,
1756 int retry_if_no_servers);
1757 trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
1758 int fascistfirewall,
1759 int retry_if_no_servers);
1760 int all_trusted_directory_servers_down(void);
1761 struct smartlist_t;
1762 void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
1763 void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
1764 int router_nickname_is_in_list(routerinfo_t *router, const char *list);
1765 routerinfo_t *routerlist_find_my_routerinfo(void);
1766 int router_nickname_matches(routerinfo_t *router, const char *nickname);
1767 int exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
1768 int warn);
1770 /** How many seconds a router must be up before we'll use it for
1771 * reliability-critical node positions.
1773 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
1774 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
1776 int router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity);
1777 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
1778 routerinfo_t *router_choose_random_node(const char *preferred,
1779 const char *excluded,
1780 struct smartlist_t *excludedsmartlist,
1781 int need_uptime, int need_bandwidth,
1782 int allow_unverified, int strict);
1783 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
1784 routerinfo_t *router_get_by_nickname(const char *nickname);
1785 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
1786 routerinfo_t *router_get_by_digest(const char *digest);
1787 int router_digest_is_trusted_dir(const char *digest);
1788 void router_get_routerlist(routerlist_t **prouterlist);
1789 time_t routerlist_get_published_time(void);
1790 void routerlist_free(routerlist_t *routerlist);
1791 void routerinfo_free(routerinfo_t *router);
1792 void routerlist_free_current(void);
1793 void free_trusted_dir_servers(void);
1794 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
1795 void router_mark_as_down(const char *digest);
1796 void routerlist_remove_old_routers(int age);
1797 int router_load_single_router(const char *s, const char **msg);
1798 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
1799 int dir_is_recent, int dir_is_cached);
1800 addr_policy_result_t router_compare_addr_to_addr_policy(uint32_t addr,
1801 uint16_t port, addr_policy_t *policy);
1803 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
1804 int need_uptime);
1806 int router_exit_policy_rejects_all(routerinfo_t *router);
1807 void running_routers_free(running_routers_t *rr);
1808 void routerlist_set_runningrouters(routerlist_t *list, running_routers_t *rr);
1809 void routerlist_update_from_runningrouters(routerlist_t *list,
1810 running_routers_t *rr);
1811 int routers_update_status_from_entry(smartlist_t *routers,
1812 time_t list_time,
1813 const char *s,
1814 int rr_format);
1815 int router_update_status_from_smartlist(routerinfo_t *r,
1816 time_t list_time,
1817 smartlist_t *running_list,
1818 int rr_format);
1819 void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
1820 void clear_trusted_dir_servers(void);
1822 /********************************* routerparse.c ************************/
1824 #define MAX_STATUS_TAG_LEN 32
1825 /** Structure to hold parsed Tor versions. This is a little messier
1826 * than we would like it to be, because we changed version schemes with 0.1.0.
1828 * See version-spec.txt for the whole business.
1830 typedef struct tor_version_t {
1831 int major;
1832 int minor;
1833 int micro;
1834 /** Release status. For version in the post-0.1 format, this is always
1835 * VER_RELEASE. */
1836 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
1837 int patchlevel;
1838 /** CVS status. For version in the post-0.1 format, this is always
1839 * IS_NOT_CVS */
1840 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
1841 char status_tag[MAX_STATUS_TAG_LEN];
1842 } tor_version_t;
1844 int router_get_router_hash(const char *s, char *digest);
1845 int router_get_dir_hash(const char *s, char *digest);
1846 int router_get_runningrouters_hash(const char *s, char *digest);
1847 int router_parse_list_from_string(const char **s,
1848 routerlist_t **dest,
1849 smartlist_t *good_nickname_list,
1850 int rr_format,
1851 time_t published);
1852 int router_parse_routerlist_from_directory(const char *s,
1853 routerlist_t **dest,
1854 crypto_pk_env_t *pkey,
1855 int check_version,
1856 int write_to_cache);
1857 running_routers_t *router_parse_runningrouters(const char *str,
1858 int write_to_cache);
1859 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
1860 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
1861 addr_policy_t *router_parse_addr_policy_from_string(const char *s);
1862 int check_software_version_against_directory(const char *directory,
1863 int ignoreversion);
1864 int tor_version_parse(const char *s, tor_version_t *out);
1865 int tor_version_as_new_as(const char *platform, const char *cutoff);
1866 int tor_version_compare(tor_version_t *a, tor_version_t *b);
1867 void assert_addr_policy_ok(addr_policy_t *t);
1869 #endif