Forward port changelog
[tor.git] / src / or / or.h
blob39cb83c11267b2e3515a7ecc0531cb411adb8f74
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004 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 #ifdef MS_WINDOWS
101 #if (_MSC_VER <= 1300)
102 #include <winsock.h>
103 #else
104 #include <winsock2.h>
105 #include <ws2tcpip.h>
106 #endif
107 #endif
109 #ifdef MS_WINDOWS
110 #include <io.h>
111 #include <process.h>
112 #include <direct.h>
113 #include <windows.h>
114 #define snprintf _snprintf
115 #endif
117 #ifdef HAVE_EVENT_H
118 #include <event.h>
119 #else
120 #error "Tor requires libevent to build."
121 #endif
123 #include "../common/crypto.h"
124 #include "../common/tortls.h"
125 #include "../common/log.h"
126 #include "../common/compat.h"
127 #include "../common/container.h"
128 #include "../common/util.h"
129 #include "../common/torgzip.h"
131 /** Define this if you want Tor to crash when any problem comes up,
132 * so you can get a coredump and track things down. */
133 #undef TOR_FRAGILE
135 /** Upper bound on maximum simultaneous connections; can be lowered by
136 * config file. */
137 #define MAXCONNECTIONS 15000
139 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
140 #define MAX_NICKNAME_LEN 19
141 /* Hex digest plus dollar sign. */
142 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
143 #define MAX_DIR_SIZE 500000
145 #ifdef TOR_PERF
146 /** How long do we keep DNS cache entries before purging them? */
147 #define MAX_DNS_ENTRY_AGE (150*60)
148 #else
149 #define MAX_DNS_ENTRY_AGE (15*60)
150 #endif
152 /** How often do we rotate onion keys? */
153 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
154 /** How often do we rotate TLS contexts? */
155 #define MAX_SSL_KEY_LIFETIME (120*60)
157 /** How old do we allow a router to get before removing it, either
158 * from the descriptor list (for dirservers) or the router list (for others)?
159 * In seconds. */
160 #define ROUTER_MAX_AGE (60*60*24)
162 typedef enum {
163 CIRC_ID_TYPE_LOWER=0,
164 CIRC_ID_TYPE_HIGHER=1
165 } circ_id_type_t;
167 #define _CONN_TYPE_MIN 3
168 /** Type for sockets listening for OR connections. */
169 #define CONN_TYPE_OR_LISTENER 3
170 /** Type for OR-to-OR or OP-to-OR connections. */
171 #define CONN_TYPE_OR 4
172 /** Type for connections from final OR to chosen destination. */
173 #define CONN_TYPE_EXIT 5
174 /** Type for sockets listening for SOCKS connections. */
175 #define CONN_TYPE_AP_LISTENER 6
176 /** Type for SOCKS connections to OP. */
177 #define CONN_TYPE_AP 7
178 /** Type for sockets listening for HTTP connections to the directory server. */
179 #define CONN_TYPE_DIR_LISTENER 8
180 /** Type for HTTP connections to the directory server. */
181 #define CONN_TYPE_DIR 9
182 /** Type for connections to local dnsworker processes. */
183 #define CONN_TYPE_DNSWORKER 10
184 /** Type for connections to local cpuworker processes. */
185 #define CONN_TYPE_CPUWORKER 11
186 /** Type for listenting for connections from user interface process */
187 #define CONN_TYPE_CONTROL_LISTENER 12
188 /** Type for connections from user interface process */
189 #define CONN_TYPE_CONTROL 13
190 #define _CONN_TYPE_MAX 13
192 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
194 /** State for any listener connection. */
195 #define LISTENER_STATE_READY 0
197 #define _DNSWORKER_STATE_MIN 1
198 /** State for a connection to a dnsworker process that's idle. */
199 #define DNSWORKER_STATE_IDLE 1
200 /** State for a connection to a dnsworker process that's resolving a hostname. */
201 #define DNSWORKER_STATE_BUSY 2
202 #define _DNSWORKER_STATE_MAX 2
204 #define _CPUWORKER_STATE_MIN 1
205 /** State for a connection to a cpuworker process that's idle. */
206 #define CPUWORKER_STATE_IDLE 1
207 /** State for a connection to a cpuworker process that's processing a
208 * handshake. */
209 #define CPUWORKER_STATE_BUSY_ONION 2
210 #define _CPUWORKER_STATE_MAX 2
212 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
214 #define _OR_CONN_STATE_MIN 1
215 /** State for a connection to an OR: waiting for connect() to finish. */
216 #define OR_CONN_STATE_CONNECTING 1
217 /** State for a connection to an OR: SSL is handshaking, not done yet. */
218 #define OR_CONN_STATE_HANDSHAKING 2
219 /** State for a connection to an OR: Ready to send/receive cells. */
220 #define OR_CONN_STATE_OPEN 3
221 #define _OR_CONN_STATE_MAX 3
223 #define _EXIT_CONN_STATE_MIN 1
224 /** State for an exit connection: waiting for response from dns farm. */
225 #define EXIT_CONN_STATE_RESOLVING 1
226 /** State for an exit connection: waiting for connect() to finish. */
227 #define EXIT_CONN_STATE_CONNECTING 2
228 /** State for an exit connection: open and ready to transmit data. */
229 #define EXIT_CONN_STATE_OPEN 3
230 /** State for an exit connection: waiting to be removed. */
231 #define EXIT_CONN_STATE_RESOLVEFAILED 4
232 #define _EXIT_CONN_STATE_MAX 4
233 #if 0
234 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
235 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
236 #endif
238 /* the AP state values must be disjoint from the EXIT state values */
239 #define _AP_CONN_STATE_MIN 5
240 /** State for a SOCKS connection: waiting for SOCKS request. */
241 #define AP_CONN_STATE_SOCKS_WAIT 5
242 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
243 * rendezvous rescriptor. */
244 #define AP_CONN_STATE_RENDDESC_WAIT 6
245 /** State for a SOCKS connection: waiting for a completed circuit. */
246 #define AP_CONN_STATE_CIRCUIT_WAIT 7
247 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
248 #define AP_CONN_STATE_CONNECT_WAIT 8
249 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
250 #define AP_CONN_STATE_RESOLVE_WAIT 9
251 /** State for a SOCKS connection: ready to send and receive. */
252 #define AP_CONN_STATE_OPEN 10
253 #define _AP_CONN_STATE_MAX 10
255 #define _DIR_CONN_STATE_MIN 1
256 /** State for connection to directory server: waiting for connect(). */
257 #define DIR_CONN_STATE_CONNECTING 1
258 /** State for connection to directory server: sending HTTP request. */
259 #define DIR_CONN_STATE_CLIENT_SENDING 2
260 /** State for connection to directory server: reading HTTP response. */
261 #define DIR_CONN_STATE_CLIENT_READING 3
262 /** State for connection at directory server: waiting for HTTP request. */
263 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
264 /** State for connection at directory server: sending HTTP response. */
265 #define DIR_CONN_STATE_SERVER_WRITING 5
266 #define _DIR_CONN_STATE_MAX 5
268 #define _CONTROL_CONN_STATE_MIN 1
269 #define CONTROL_CONN_STATE_OPEN 1
270 #define CONTROL_CONN_STATE_NEEDAUTH 2
271 #define _CONTROL_CONN_STATE_MAX 2
273 #define _DIR_PURPOSE_MIN 1
274 /** Purpose for connection to directory server: download a directory. */
275 #define DIR_PURPOSE_FETCH_DIR 1
276 /** Purpose for connection to directory server: download just the list
277 * of running routers. */
278 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
279 /** Purpose for connection to directory server: download a rendezvous
280 * descriptor. */
281 #define DIR_PURPOSE_FETCH_RENDDESC 3
282 /** Purpose for connection to directory server: set after a rendezvous
283 * descriptor is downloaded. */
284 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
285 /** Purpose for connection to directory server: upload a server descriptor. */
286 #define DIR_PURPOSE_UPLOAD_DIR 5
287 /** Purpose for connection to directory server: upload a rendezvous
288 * descriptor. */
289 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
290 /** Purpose for connection at a directory server. */
291 #define DIR_PURPOSE_SERVER 7
292 #define _DIR_PURPOSE_MAX 7
294 #define _EXIT_PURPOSE_MIN 1
295 #define EXIT_PURPOSE_CONNECT 1
296 #define EXIT_PURPOSE_RESOLVE 2
297 #define _EXIT_PURPOSE_MAX 2
299 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
300 #define CIRCUIT_STATE_BUILDING 0
301 /** Circuit state: Waiting to process the onionskin. */
302 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
303 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
304 #define CIRCUIT_STATE_OR_WAIT 2
305 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
306 #define CIRCUIT_STATE_OPEN 3
308 #define _CIRCUIT_PURPOSE_MIN 1
310 /* these circuits were initiated elsewhere */
311 #define _CIRCUIT_PURPOSE_OR_MIN 1
312 /** OR-side circuit purpose: normal circuit, at OR. */
313 #define CIRCUIT_PURPOSE_OR 1
314 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
315 #define CIRCUIT_PURPOSE_INTRO_POINT 2
316 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
317 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
318 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
319 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
320 #define _CIRCUIT_PURPOSE_OR_MAX 4
322 /* these circuits originate at this node */
324 /* here's how circ client-side purposes work:
325 * normal circuits are C_GENERAL.
326 * circuits that are c_introducing are either on their way to
327 * becoming open, or they are open and waiting for a
328 * suitable rendcirc before they send the intro.
329 * circuits that are c_introduce_ack_wait have sent the intro,
330 * but haven't gotten a response yet.
331 * circuits that are c_establish_rend are either on their way
332 * to becoming open, or they are open and have sent the
333 * establish_rendezvous cell but haven't received an ack.
334 * circuits that are c_rend_ready are open and have received a
335 * rend ack, but haven't heard from bob yet. if they have a
336 * buildstate->pending_final_cpath then they're expecting a
337 * cell from bob, else they're not.
338 * circuits that are c_rend_ready_intro_acked are open, and
339 * some intro circ has sent its intro and received an ack.
340 * circuits that are c_rend_joined are open, have heard from
341 * bob, and are talking to him.
343 /** Client-side circuit purpose: Normal circuit, with cpath. */
344 #define CIRCUIT_PURPOSE_C_GENERAL 5
345 /** Client-side circuit purpose: at Alice, connecting to intro point. */
346 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
347 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
348 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
349 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
350 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
351 /** Client-side circuit purpose: at Alice, waiting for ack. */
352 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
353 /** Client-side circuit purpose: at Alice, waiting for Bob. */
354 #define CIRCUIT_PURPOSE_C_REND_READY 10
355 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
356 * has been acknowledged. */
357 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
358 /** Client-side circuit purpose: at Alice, rendezvous established. */
359 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
361 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
362 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
363 /** Hidden-service-side circuit purpose: at Bob, successfully established
364 * intro. */
365 #define CIRCUIT_PURPOSE_S_INTRO 14
366 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
367 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
368 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
369 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
370 #define _CIRCUIT_PURPOSE_MAX 16
372 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
373 * that this OP has originated. */
374 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
375 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
377 #define RELAY_COMMAND_BEGIN 1
378 #define RELAY_COMMAND_DATA 2
379 #define RELAY_COMMAND_END 3
380 #define RELAY_COMMAND_CONNECTED 4
381 #define RELAY_COMMAND_SENDME 5
382 #define RELAY_COMMAND_EXTEND 6
383 #define RELAY_COMMAND_EXTENDED 7
384 #define RELAY_COMMAND_TRUNCATE 8
385 #define RELAY_COMMAND_TRUNCATED 9
386 #define RELAY_COMMAND_DROP 10
387 #define RELAY_COMMAND_RESOLVE 11
388 #define RELAY_COMMAND_RESOLVED 12
390 #define RELAY_COMMAND_ESTABLISH_INTRO 32
391 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
392 #define RELAY_COMMAND_INTRODUCE1 34
393 #define RELAY_COMMAND_INTRODUCE2 35
394 #define RELAY_COMMAND_RENDEZVOUS1 36
395 #define RELAY_COMMAND_RENDEZVOUS2 37
396 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
397 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
398 #define RELAY_COMMAND_INTRODUCE_ACK 40
400 #define _MIN_END_STREAM_REASON 1
401 #define END_STREAM_REASON_MISC 1
402 #define END_STREAM_REASON_RESOLVEFAILED 2
403 #define END_STREAM_REASON_CONNECTFAILED 3
404 #define END_STREAM_REASON_EXITPOLICY 4
405 #define END_STREAM_REASON_DESTROY 5
406 #define END_STREAM_REASON_DONE 6
407 #define END_STREAM_REASON_TIMEOUT 7
408 #define END_STREAM_REASON_RESOURCELIMIT 8
409 #define _MAX_END_STREAM_REASON 8
411 #define RESOLVED_TYPE_IPV4 4
412 #define RESOLVED_TYPE_IPV6 6
413 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
414 #define RESOLVED_TYPE_ERROR 0xF1
416 /** Length of 'y' portion of 'y.onion' URL. */
417 #define REND_SERVICE_ID_LEN 16
419 #define CELL_DIRECTION_IN 1
420 #define CELL_DIRECTION_OUT 2
422 #ifdef TOR_PERF
423 #define CIRCWINDOW_START 10000
424 #define CIRCWINDOW_INCREMENT 1000
425 #define STREAMWINDOW_START 5000
426 #define STREAMWINDOW_INCREMENT 500
427 #else
428 #define CIRCWINDOW_START 1000
429 #define CIRCWINDOW_INCREMENT 100
430 #define STREAMWINDOW_START 500
431 #define STREAMWINDOW_INCREMENT 50
432 #endif
434 /* cell commands */
435 #define CELL_PADDING 0
436 #define CELL_CREATE 1
437 #define CELL_CREATED 2
438 #define CELL_RELAY 3
439 #define CELL_DESTROY 4
441 /* people behind fascist firewalls use only these ports */
442 #define REQUIRED_FIREWALL_DIRPORT 80
443 #define REQUIRED_FIREWALL_ORPORT 443
445 /* legal characters in a nickname */
446 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
448 /** Name to use in client TLS certificates if no nickname is given.*/
449 #define DEFAULT_CLIENT_NICKNAME "client"
451 #define SOCKS4_NETWORK_LEN 8
454 * Relay payload:
455 * Relay command [1 byte]
456 * Recognized [2 bytes]
457 * Stream ID [2 bytes]
458 * Partial SHA-1 [4 bytes]
459 * Length [2 bytes]
460 * Relay payload [498 bytes]
463 #define CELL_PAYLOAD_SIZE 509
464 #define CELL_NETWORK_SIZE 512
466 #define RELAY_HEADER_SIZE (1+2+2+4+2)
467 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
469 /** Parsed onion routing cell. All communication from OP-to-OR, or from
470 * OR-to-OR, is via cells. */
471 typedef struct {
472 uint16_t circ_id; /**< Circuit which received the cell. */
473 unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
474 * or DESTROY. */
475 unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
476 } cell_t;
478 /** Beginning of a RELAY cell payload. */
479 typedef struct {
480 uint8_t command; /**< The end-to-end relay command. */
481 uint16_t recognized; /**< Used to tell whether cell is for us. */
482 uint16_t stream_id; /**< Which stream is this cell associated with? */
483 char integrity[4]; /**< Used to tell whether cell is corrupted. */
484 uint16_t length; /**< How long is the payload body? */
485 } relay_header_t;
487 typedef struct buf_t buf_t;
488 typedef struct socks_request_t socks_request_t;
490 #define CONNECTION_MAGIC 0x7C3C304Eu
491 /** Description of a connection to another host or process, and associated
492 * data. */
493 struct connection_t {
494 uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
496 uint8_t type; /**< What kind of connection is this? */
497 uint8_t state; /**< Current state of this connection. */
498 uint8_t purpose; /**< Only used for DIR types currently. */
499 uint8_t wants_to_read; /**< Boolean: should we start reading again once
500 * the bandwidth throttler allows it?
502 uint8_t wants_to_write; /**< Boolean: should we start writing again once
503 * the bandwidth throttler allows reads?
505 int s; /**< Our socket; -1 if this connection is closed. */
506 int poll_index; /* XXXX rename. */
507 struct event *read_event; /**< libevent event structure. */
508 struct event *write_event; /**< libevent event structure. */
509 int marked_for_close; /**< Boolean: should we close this conn on the next
510 * iteration of the main loop?
512 const char *marked_for_close_file; /**< For debugging: in which file were
513 * we marked for close? */
514 int hold_open_until_flushed; /**< Despite this connection's being marked
515 * for close, do we flush it before closing it?
518 buf_t *inbuf; /**< Buffer holding data read over this connection. */
519 int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
520 time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
522 buf_t *outbuf; /**< Buffer holding data to write over this connection. */
523 size_t outbuf_flushlen; /**< How much data should we try to flush from the
524 * outbuf? */
525 time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
527 time_t timestamp_created; /**< When was this connection_t created? */
529 uint32_t addr; /**< IP of the other side of the connection; used to identify
530 * routers, along with port. */
531 uint16_t port; /**< If non-zero, port on the other end
532 * of the connection. */
533 char *address; /**< FQDN (or IP) of the guy on the other end.
534 * strdup into this, because free_connection frees it.
536 crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
537 * signing key. */
538 char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
539 char *nickname; /**< Nickname of OR on other side (if any). */
541 /** Nickname of planned exit node -- to be used with .exit support. */
542 char *chosen_exit_name;
544 /* Used only by OR connections: */
545 tor_tls *tls; /**< TLS connection state (OR only.) */
546 uint16_t next_circ_id; /**< Which circ_id do we try to use next on
547 * this connection? This is always in the
548 * range 0..1<<15-1. (OR only.)*/
550 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
551 int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
552 int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
553 * add 'bandwidth' to this, capping it at 10*bandwidth.
554 * (OPEN ORs only)
556 circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
557 * connection, which half of the space should
558 * we use? */
560 /* Used only by DIR and AP connections: */
561 char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
562 * querying for? (DIR/AP only) */
564 /* Used only by edge connections: */
565 size_t stream_size; /**< Used for debugging. */
566 uint16_t stream_id;
567 struct connection_t *next_stream; /**< Points to the next stream at this
568 * edge, if any (Edge only). */
569 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
570 * this conn exits at. (Edge only.) */
571 int package_window; /**< How many more relay cells can i send into the
572 * circuit? (Edge only.) */
573 int deliver_window; /**< How many more relay cells can end at me? (Edge
574 * only.) */
576 int done_sending; /**< For half-open connections; not used currently. */
577 int done_receiving; /**< For half-open connections; not used currently. */
578 char has_sent_end; /**< For debugging: set once we've set the stream end,
579 and check in circuit_about_to_close_connection(). */
581 /* Used only by AP connections */
582 socks_request_t *socks_request; /**< SOCKS structure describing request (AP
583 * only.) */
585 /* Used only by control connections */
586 uint32_t event_mask;
589 typedef struct connection_t connection_t;
591 #define ADDR_POLICY_ACCEPT 1
592 #define ADDR_POLICY_REJECT 2
594 /** A linked list of policy rules */
595 typedef struct addr_policy_t {
596 char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
597 char *string; /**< String representation of this rule. */
598 uint32_t addr; /**< Base address to accept or reject. */
599 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
600 * a &amp; msk == <b>addr</b> &amp; msk . */
601 uint16_t prt_min; /**< Lowest port number to accept/reject. */
602 uint16_t prt_max; /**< Highest port number to accept/reject. */
604 struct addr_policy_t *next; /**< Next rule in list. */
605 } addr_policy_t;
607 /** Information about another onion router in the network. */
608 typedef struct {
609 char *address; /**< Location of OR: either a hostname or an IP address. */
610 char *nickname; /**< Human-readable OR name. */
612 uint32_t addr; /**< IPv4 address of OR, in host order. */
613 uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
614 uint16_t dir_port; /**< Port for HTTP directory connections. */
616 time_t published_on; /**< When was the information in this routerinfo_t
617 * published? */
619 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
620 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
621 char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
623 char *platform; /**< What software/operating system is this OR using? */
625 /* link info */
626 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
627 * bucket per second? */
628 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
629 /** How many bytes/s is this router known to handle? */
630 uint32_t bandwidthcapacity;
631 addr_policy_t *exit_policy; /**< What streams will this OR permit
632 * to exit? */
633 long uptime; /**< How many seconds the router claims to have been up */
634 /* local info */
635 int is_running; /**< As far as we know, is this OR currently running? */
636 time_t status_set_at; /**< When did we last update is_running? */
637 int is_verified; /**< Has a trusted dirserver validated this OR? */
639 smartlist_t *declared_family; /**< Nicknames of router which this router
640 * claims are its family. */
641 } routerinfo_t;
643 /** Contents of a directory of onion routers. */
644 typedef struct {
645 /** List of routerinfo_t */
646 smartlist_t *routers;
647 /** Which versions of tor are recommended by this directory? */
648 char *software_versions;
649 /** When was the most recent directory that contributed to this list
650 * published?
652 time_t published_on;
653 time_t running_routers_updated_on;
654 /** Which router is claimed to have signed it? */
655 char *signing_router;
656 } routerlist_t;
658 /** Contents of a running-routers list */
659 typedef struct running_routers_t {
660 time_t published_on; /**< When was the list marked as published? */
661 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
662 smartlist_t *running_routers;
663 int is_running_routers_format; /**< Are we using the old entry format? */
664 } running_routers_t;
666 /** Holds accounting information for a single step in the layered encryption
667 * performed by a circuit. Used only at the client edge of a circuit. */
668 struct crypt_path_t {
670 /* crypto environments */
671 /** Encryption key and counter for cells heading towards the OR at this
672 * step. */
673 crypto_cipher_env_t *f_crypto;
674 /** Encryption key and counter for cells heading back from the OR at this
675 * step. */
676 crypto_cipher_env_t *b_crypto;
678 /** Digest state for cells heading towards the OR at this step. */
679 crypto_digest_env_t *f_digest; /* for integrity checking */
680 /** Digest state for cells heading away from the OR at this step. */
681 crypto_digest_env_t *b_digest;
683 /** Current state of Diffie-Hellman key negotiation with the OR at this
684 * step. */
685 crypto_dh_env_t *handshake_state;
686 /** Negotiated key material shared with the OR at this step. */
687 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
689 /** IP4 address of the OR at this step. */
690 uint32_t addr;
691 /** Port of the OR at this step. */
692 uint16_t port;
693 /** Identity key digest of the OR at this step. */
694 char identity_digest[DIGEST_LEN];
696 /** Is the circuit built to this step? Must be one of:
697 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
698 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
699 * and not received an EXTENDED/CREATED)
700 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
701 uint8_t state;
702 #define CPATH_STATE_CLOSED 0
703 #define CPATH_STATE_AWAITING_KEYS 1
704 #define CPATH_STATE_OPEN 2
705 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
706 * (The list is circular, so the last node
707 * links to the first.) */
708 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
709 * circuit. */
711 int package_window; /**< How many bytes are we allowed to originate ending
712 * at this step? */
713 int deliver_window; /**< How many bytes are we willing to deliver originating
714 * at this step? */
717 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
719 #define DH_KEY_LEN DH_BYTES
720 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
721 CIPHER_KEY_LEN+\
722 DH_KEY_LEN)
723 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
724 #define REND_COOKIE_LEN DIGEST_LEN
726 typedef struct crypt_path_t crypt_path_t;
728 /** Information used to build a circuit. */
729 typedef struct {
730 /** Intended length of the final circuit. */
731 int desired_path_len;
732 /** Nickname of planned exit node. */
733 char *chosen_exit_name;
734 /** Identity of planned exit node. */
735 char chosen_exit_digest[DIGEST_LEN];
736 /** Whether every node in the circ must have adequate uptime. */
737 int need_uptime;
738 /** Whether every node in the circ must have adequate capacity. */
739 int need_capacity;
740 /** Whether the last hop was picked with exiting in mind. */
741 int is_internal;
742 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
743 struct crypt_path_t *pending_final_cpath;
744 /** How many times has building a circuit for this task failed? */
745 int failure_count;
746 } cpath_build_state_t;
748 #define CIRCUIT_MAGIC 0x35315243u
749 /** Struct for a path (circuit) through the onion routing network. */
750 struct circuit_t {
751 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
753 int marked_for_close; /**< Should we close this circuit at the end of the
754 * main loop? */
755 const char *marked_for_close_file; /**< For debugging: in which file was this
756 * circuit marked for close? */
758 /** The IPv4 address of the OR that is next in this circuit. */
759 uint32_t n_addr;
760 /** The port for the OR that is next in this circuit. */
761 uint16_t n_port;
762 /** The OR connection that is previous in this circuit. */
763 connection_t *p_conn;
764 /** The OR connection that is next in this circuit. */
765 connection_t *n_conn;
766 /** The identity hash of n_conn. */
767 char n_conn_id_digest[DIGEST_LEN];
768 /** Linked list of AP streams associated with this circuit. */
769 connection_t *p_streams;
770 /** Linked list of Exit streams associated with this circuit. */
771 connection_t *n_streams;
772 /** Linked list of Exit streams associated with this circuit that are
773 * still being resolved. */
774 connection_t *resolving_streams;
775 /** The next stream_id that will be tried when we're attempting to
776 * construct a new AP stream originating at this circuit. */
777 uint16_t next_stream_id;
778 /** How many relay data cells can we package (read from edge streams)
779 * on this circuit before we receive a circuit-level sendme cell asking
780 * for more? */
781 int package_window;
782 /** How many relay data cells will we deliver (write to edge streams)
783 * on this circuit? When deliver_window gets low, we send some
784 * circuit-level sendme cells to indicate that we're willing to accept
785 * more. */
786 int deliver_window;
788 /** The circuit_id used in the previous (backward) hop of this circuit. */
789 uint16_t p_circ_id;
790 /** The circuit_id used in the next (forward) hop of this circuit. */
791 uint16_t n_circ_id;
793 /** The cipher used by intermediate hops for cells heading toward the
794 * OP. */
795 crypto_cipher_env_t *p_crypto;
796 /** The cipher used by intermediate hops for cells heading away from
797 * the OP. */
798 crypto_cipher_env_t *n_crypto;
800 /** The integrity-checking digest used by intermediate hops, for
801 * cells packaged here and heading towards the OP.
803 crypto_digest_env_t *p_digest;
804 /** The integrity-checking digest used by intermediate hops, for
805 * cells packaged at the OP and arriving here.
807 crypto_digest_env_t *n_digest;
809 /** Build state for this circuit. It includes the intended path
810 * length, the chosen exit router, rendezvous information, etc.
812 cpath_build_state_t *build_state;
813 /** The doubly-linked list of crypt_path_t entries, one per hop,
814 * for this circuit. This includes ciphers for each hop,
815 * integrity-checking digests for each hop, and package/delivery
816 * windows for each hop.
818 * The cpath field is defined only when we are the circuit's origin.
820 crypt_path_t *cpath;
822 /** For storage while passing to cpuworker, or while n_conn is pending. */
823 char onionskin[ONIONSKIN_CHALLENGE_LEN];
825 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
827 time_t timestamp_created; /**< When was this circuit created? */
828 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
829 * circuit is clean. */
831 uint8_t state; /**< Current status of this circuit. */
832 uint8_t purpose; /**< Why are we creating this circuit? */
835 * The rend_query field holds y portion of y.onion (nul-terminated)
836 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
837 * for a hidden service, or is S_*.
839 char rend_query[REND_SERVICE_ID_LEN+1];
841 /** The rend_pk_digest field holds a hash of location-hidden service's
842 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
844 char rend_pk_digest[DIGEST_LEN];
846 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
847 * C_ESTABLISH_REND. Filled with zeroes otherwise.
849 char rend_cookie[REND_COOKIE_LEN];
851 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
852 * is not marked for close. */
853 struct circuit_t *rend_splice;
855 /** Quasi-global identifier for this circuit; used for control.c */
856 /* XXXX009 NM This can get re-used after 2**32 circuits. */
857 uint32_t global_identifier;
859 struct circuit_t *next; /**< Next circuit in linked list. */
862 typedef struct circuit_t circuit_t;
864 #define ALLOW_UNVERIFIED_ENTRY 1
865 #define ALLOW_UNVERIFIED_EXIT 2
866 #define ALLOW_UNVERIFIED_MIDDLE 4
867 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
868 #define ALLOW_UNVERIFIED_INTRODUCTION 16
870 typedef struct exit_redirect_t {
871 uint32_t addr;
872 uint32_t mask;
873 uint16_t port_min;
874 uint16_t port_max;
876 int is_redirect;
877 uint32_t addr_dest;
878 uint16_t port_dest;
879 } exit_redirect_t;
881 /** Configuration options for a Tor process */
882 typedef struct {
883 /** What should the tor process actually do? */
884 enum {
885 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
886 } command;
887 const char *command_arg; /**< Argument for command-line option. */
889 struct config_line_t *OldLogOptions; /**< List of configuration lines
890 * for logfiles, old style. */
892 struct config_line_t *Logs; /**< New-style list of configuration lines
893 * for logs */
895 char *DebugLogFile; /**< Where to send verbose log messages. */
896 char *DataDirectory; /**< OR only: where to store long-term data. */
897 char *Nickname; /**< OR only: nickname of this onion router. */
898 char *Address; /**< OR only: configured address for this onion router. */
899 char *PidFile; /**< Where to store PID of Tor process. */
901 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
902 * as exits. */
903 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
904 * as entry points. */
905 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
906 * stop building circuits? */
907 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
908 * stop building circuits? */
909 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
910 * use in circuits. */
912 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
913 * points. */
914 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
915 * as introduction points. */
917 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
918 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
919 struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
920 struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
921 struct config_line_t *DirPolicy; /**< Lists of dir policy components */
922 /** Addresses to bind for listening for SOCKS connections. */
923 struct config_line_t *SocksBindAddress;
924 /** Addresses to bind for listening for OR connections. */
925 struct config_line_t *ORBindAddress;
926 /** Addresses to bind for listening for directory connections. */
927 struct config_line_t *DirBindAddress;
928 /** Local address to bind outbound sockets */
929 char *OutboundBindAddress;
930 /** Directory server only: which versions of
931 * Tor should we tell users to run? */
932 struct config_line_t *RecommendedVersions;
933 /** Whether dirservers refuse router descriptors with private IPs. */
934 int DirAllowPrivateAddresses;
935 char *User; /**< Name of user to run Tor as. */
936 char *Group; /**< Name of group to run Tor as. */
937 double PathlenCoinWeight; /**< Parameter used to configure average path
938 * length (alpha in geometric distribution). */
939 int ORPort; /**< Port to listen on for OR connections. */
940 int SocksPort; /**< Port to listen on for SOCKS connections. */
941 int ControlPort; /**< Port to listen on for control connections. */
942 int DirPort; /**< Port to listen on for directory connections. */
943 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
944 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
945 int MaxConn; /**< Maximum number of simultaneous connections. */
946 int IgnoreVersion; /**< If true, run no matter what versions of Tor the
947 * directory recommends. */
948 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
949 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
950 smartlist_t *FirewallPorts; /**< Which ports our firewall allows (strings). */
951 /** Application ports that require all nodes in circ to have sufficient uptime. */
952 smartlist_t *LongLivedPorts;
953 int DirFetchPeriod; /**< How often do we fetch new directories? */
954 int DirPostPeriod; /**< How often do we post our server descriptor to the
955 * authoritative directory servers? */
956 int RendPostPeriod; /**< How often do we post each rendezvous service
957 * descriptor? Remember to publish them independently. */
958 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
959 int KeepalivePeriod; /**< How often do we send padding cells to keep
960 * connections alive? */
961 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
962 * to wait simultaneously before we start dropping
963 * them? */
964 int NewCircuitPeriod; /**< How long do we use a circuit before building
965 * a new one? */
966 int MaxCircuitDirtiness; /**< Never use circs that were first used more than
967 this interval ago. */
968 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
969 * use in a second? */
970 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
971 * use in a second? */
972 int NumCpus; /**< How many CPUs should we try to use? */
973 int RunTesting; /**< If true, create testing circuits to measure how well the
974 * other ORs are running. */
975 struct config_line_t *RendConfigLines; /**< List of configuration lines
976 * for rendezvous services. */
977 char *ContactInfo; /**< Contact info to be published in the directory */
979 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
980 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
981 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
983 struct config_line_t *DirServers; /**< List of configuration lines
984 * for directory servers. */
985 char *MyFamily; /**< Declared family for this OR. */
986 struct config_line_t *NodeFamilies; /**< List of config lines for
987 * node families */
988 struct config_line_t *RedirectExit; /**< List of config lines for simple
989 * addr/port redirection */
990 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
991 int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
992 * interval starts */
993 char *AccountingStart; /** How long is the accounting interval, and when
994 * does it start? */
995 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
996 * interval before hibernation? 0 for "never
997 * hibernate." */
998 int _AccountingMaxKB; /**< How many KB do we allow per accounting
999 * interval before hibernation? 0 for "never
1000 * hibernate." (Based on a deprecated option)*/
1002 char *HashedControlPassword; /**< Base64-encoded hash of a password for
1003 * the control system. */
1004 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
1005 * the control system? */
1006 } or_options_t;
1008 #define MAX_SOCKS_REPLY_LEN 1024
1009 #define MAX_SOCKS_ADDR_LEN 256
1010 #define SOCKS_COMMAND_CONNECT 0x01
1011 #define SOCKS_COMMAND_RESOLVE 0xF0
1012 /** State of a SOCKS request from a user to an OP */
1013 struct socks_request_t {
1014 char socks_version; /**< Which version of SOCKS did the client use? */
1015 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
1016 size_t replylen; /**< Length of <b>reply</b>. */
1017 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
1018 * we want to specify our own socks reply,
1019 * rather than using the default socks4 or
1020 * socks5 socks reply. We use this for the
1021 * two-stage socks5 handshake.
1023 int has_finished; /**< Has the SOCKS handshake finished? */
1024 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
1025 uint16_t port; /**< What port did the client ask to connect to? */
1028 /* all the function prototypes go here */
1030 /********************************* buffers.c ***************************/
1032 buf_t *buf_new(void);
1033 buf_t *buf_new_with_capacity(size_t size);
1034 void buf_free(buf_t *buf);
1035 void buf_clear(buf_t *buf);
1037 size_t buf_datalen(const buf_t *buf);
1038 size_t buf_capacity(const buf_t *buf);
1039 const char *_buf_peek_raw_buffer(const buf_t *buf);
1041 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1042 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
1044 int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
1045 int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
1047 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1048 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1049 int fetch_from_buf_http(buf_t *buf,
1050 char **headers_out, size_t max_headerlen,
1051 char **body_out, size_t *body_used, size_t max_bodylen);
1052 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
1053 int fetch_from_buf_control(buf_t *buf, uint16_t *len_out, uint16_t *type_out,
1054 char **body_out);
1056 void assert_buf_ok(buf_t *buf);
1058 /********************************* circuitbuild.c **********************/
1060 char *circuit_list_path(circuit_t *circ, int verbose);
1061 void circuit_log_path(int severity, circuit_t *circ);
1062 void circuit_rep_hist_note_result(circuit_t *circ);
1063 void circuit_dump_by_conn(connection_t *conn, int severity);
1064 circuit_t *circuit_establish_circuit(uint8_t purpose, const char *exit_digest,
1065 int need_uptime, int need_capacity, int internal);
1066 void circuit_n_conn_done(connection_t *or_conn, int status);
1067 int circuit_send_next_onion_skin(circuit_t *circ);
1068 int circuit_extend(cell_t *cell, circuit_t *circ);
1069 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
1070 int circuit_finish_handshake(circuit_t *circ, char *reply);
1071 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1072 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
1073 int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
1074 int *need_capacity);
1076 int circuit_append_new_hop(circuit_t *circ, char *nickname, const char *exit_digest);
1077 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1079 /********************************* circuitlist.c ***********************/
1081 extern const char *circuit_state_to_string[];
1082 void circuit_close_all_marked(void);
1083 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1084 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
1085 circuit_t *circuit_get_by_conn(connection_t *conn);
1086 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
1087 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1088 const char *digest, uint8_t purpose);
1089 circuit_t *circuit_get_rendezvous(const char *cookie);
1090 circuit_t *circuit_get_clean_open(uint8_t purpose, int need_uptime,
1091 int need_capacity, int internal);
1092 int _circuit_mark_for_close(circuit_t *circ);
1094 #define circuit_mark_for_close(c) \
1095 do { \
1096 if (_circuit_mark_for_close(c)<0) { \
1097 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
1098 _SHORT_FILE_,__LINE__, \
1099 c->marked_for_close_file,c->marked_for_close); \
1100 } else { \
1101 c->marked_for_close_file = _SHORT_FILE_; \
1102 c->marked_for_close = __LINE__; \
1104 } while (0)
1106 void assert_cpath_layer_ok(const crypt_path_t *cp);
1107 void assert_circuit_ok(const circuit_t *c);
1109 /********************************* circuituse.c ************************/
1111 void circuit_expire_building(time_t now);
1112 void circuit_remove_handled_ports(smartlist_t *needed_ports);
1113 int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
1114 void circuit_build_needed_circs(time_t now);
1115 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1116 void circuit_about_to_close_connection(connection_t *conn);
1117 void circuit_has_opened(circuit_t *circ);
1118 void circuit_build_failed(circuit_t *circ);
1119 circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
1120 int need_uptime, int need_capacity, int is_internal);
1121 circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest,
1122 int need_uptime, int need_capacity, int is_internal);
1123 void circuit_reset_failure_count(int timeout);
1124 int connection_ap_handshake_attach_circuit(connection_t *conn);
1126 /********************************* command.c ***************************/
1128 void command_process_cell(cell_t *cell, connection_t *conn);
1130 extern unsigned long stats_n_padding_cells_processed;
1131 extern unsigned long stats_n_create_cells_processed;
1132 extern unsigned long stats_n_created_cells_processed;
1133 extern unsigned long stats_n_relay_cells_processed;
1134 extern unsigned long stats_n_destroy_cells_processed;
1136 /********************************* config.c ***************************/
1138 struct config_line_t {
1139 char *key;
1140 char *value;
1141 struct config_line_t *next;
1144 or_options_t *get_options(void);
1145 void set_options(or_options_t *new_val);
1146 int options_act(void);
1148 int config_get_lines(char *string, struct config_line_t **result);
1149 void config_free_lines(struct config_line_t *front);
1150 int config_trial_assign(struct config_line_t *list, int reset);
1151 int resolve_my_address(const char *address, uint32_t *addr);
1152 void options_init(or_options_t *options);
1153 int init_from_config(int argc, char **argv);
1154 int config_init_logs(or_options_t *options, int validate_only);
1155 int config_parse_addr_policy(struct config_line_t *cfg,
1156 addr_policy_t **dest);
1157 void addr_policy_free(addr_policy_t *p);
1158 int config_option_is_recognized(const char *key);
1159 struct config_line_t *config_get_assigned_option(or_options_t *options,
1160 const char *key);
1161 char *config_dump_options(or_options_t *options, int minimal);
1162 int save_current_config(void);
1164 /********************************* connection.c ***************************/
1166 #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
1167 "Unknown" : conn_type_to_string[(t)])
1169 extern const char *conn_type_to_string[];
1170 extern const char *conn_state_to_string[][_CONN_TYPE_MAX+1];
1172 connection_t *connection_new(int type);
1173 void connection_free(connection_t *conn);
1174 void connection_free_all(void);
1175 void connection_about_to_close_connection(connection_t *conn);
1176 void connection_close_immediate(connection_t *conn);
1177 int _connection_mark_for_close(connection_t *conn);
1179 #define connection_mark_for_close(c) \
1180 do { \
1181 if (_connection_mark_for_close(c)<0) { \
1182 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
1183 _SHORT_FILE_,__LINE__, \
1184 c->marked_for_close_file,c->marked_for_close); \
1185 } else { \
1186 c->marked_for_close_file = _SHORT_FILE_; \
1187 c->marked_for_close = __LINE__; \
1189 } while (0)
1191 void connection_expire_held_open(void);
1193 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
1194 int retry_all_listeners(int force);
1196 void connection_bucket_init(void);
1197 void connection_bucket_refill(struct timeval *now);
1199 int connection_handle_read(connection_t *conn);
1201 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1203 int connection_wants_to_flush(connection_t *conn);
1204 int connection_outbuf_too_full(connection_t *conn);
1205 int connection_handle_write(connection_t *conn);
1206 void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
1208 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
1209 connection_t *connection_get_by_identity_digest(const char *digest, int type);
1211 connection_t *connection_get_by_type(int type);
1212 connection_t *connection_get_by_type_state(int type, int state);
1213 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1214 connection_t *connection_get_by_type_state_rendquery(int type, int state, const char *rendquery);
1216 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1217 #define connection_has_pending_tls_data(conn) \
1218 ((conn)->type == CONN_TYPE_OR && \
1219 (conn)->state == OR_CONN_STATE_OPEN && \
1220 tor_tls_get_pending_bytes((conn)->tls))
1221 int connection_is_listener(connection_t *conn);
1222 int connection_state_is_open(connection_t *conn);
1223 int connection_state_is_connecting(connection_t *conn);
1225 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
1227 void assert_connection_ok(connection_t *conn, time_t now);
1228 int connection_or_nonopen_was_started_here(connection_t *conn);
1230 /********************************* connection_edge.c ***************************/
1232 int connection_edge_reached_eof(connection_t *conn);
1233 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1234 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1235 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
1236 int connection_edge_finished_flushing(connection_t *conn);
1237 int connection_edge_finished_connecting(connection_t *conn);
1239 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1240 int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
1242 int connection_ap_make_bridge(char *address, uint16_t port);
1243 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1244 size_t replylen, int status);
1245 void connection_ap_handshake_socks_resolved(connection_t *conn,
1246 int answer_type,
1247 size_t answer_len,
1248 const char *answer);
1250 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1251 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1252 void connection_exit_connect(connection_t *conn);
1253 int connection_edge_is_rendezvous_stream(connection_t *conn);
1254 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1255 void connection_ap_expire_beginning(void);
1256 void connection_ap_attach_pending(void);
1258 void parse_socks_policy(void);
1259 int socks_policy_permits_address(uint32_t addr);
1261 void client_dns_init(void);
1262 uint32_t client_dns_lookup_entry(const char *address);
1263 int client_dns_incr_failures(const char *address);
1264 void client_dns_set_entry(const char *address, uint32_t val);
1265 void client_dns_clean(void);
1266 void set_exit_redirects(smartlist_t *lst);
1267 typedef enum hostname_type_t {
1268 NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME
1269 } hostname_type_t;
1270 hostname_type_t parse_extended_hostname(char *address);
1272 /********************************* connection_or.c ***************************/
1274 int connection_or_reached_eof(connection_t *conn);
1275 int connection_or_process_inbuf(connection_t *conn);
1276 int connection_or_finished_flushing(connection_t *conn);
1277 int connection_or_finished_connecting(connection_t *conn);
1279 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1280 const char *id_digest);
1282 int connection_tls_start_handshake(connection_t *conn, int receiving);
1283 int connection_tls_continue_handshake(connection_t *conn);
1285 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1286 void connection_or_update_nickname(connection_t *conn);
1288 /********************************* control.c ***************************/
1290 typedef enum circuit_status_event_t {
1291 CIRC_EVENT_LAUNCHED = 0,
1292 CIRC_EVENT_BUILT = 1,
1293 CIRC_EVENT_EXTENDED = 2,
1294 CIRC_EVENT_FAILED = 3,
1295 CIRC_EVENT_CLOSED = 4,
1296 } circuit_status_event_t;
1298 typedef enum stream_status_event_t {
1299 STREAM_EVENT_SENT_CONNECT = 0,
1300 STREAM_EVENT_SENT_RESOLVE = 1,
1301 STREAM_EVENT_SUCCEEDED = 2,
1302 STREAM_EVENT_FAILED = 3,
1303 STREAM_EVENT_CLOSED = 4
1304 } stream_status_event_t;
1306 typedef enum or_conn_status_event_t {
1307 OR_CONN_EVENT_LAUNCHED = 0,
1308 OR_CONN_EVENT_CONNECTED = 1,
1309 OR_CONN_EVENT_FAILED = 2,
1310 OR_CONN_EVENT_CLOSED = 3,
1311 } or_conn_status_event_t;
1313 int connection_control_finished_flushing(connection_t *conn);
1314 int connection_control_reached_eof(connection_t *conn);
1315 int connection_control_process_inbuf(connection_t *conn);
1317 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1318 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1319 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1320 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1321 void control_event_logmsg(int severity, const char *msg);
1323 int init_cookie_authentication(int enabled);
1324 int decode_hashed_password(char *buf, const char *hashed);
1326 /********************************* cpuworker.c *****************************/
1328 void cpu_init(void);
1329 void cpuworkers_rotate(void);
1330 int connection_cpu_finished_flushing(connection_t *conn);
1331 int connection_cpu_reached_eof(connection_t *conn);
1332 int connection_cpu_process_inbuf(connection_t *conn);
1333 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
1334 void *task);
1336 /********************************* directory.c ***************************/
1338 int dir_policy_permits_address(uint32_t addr);
1339 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1340 size_t payload_len);
1341 void directory_get_from_dirserver(uint8_t purpose, const char *resource,
1342 int retry_if_no_servers);
1343 int connection_dir_reached_eof(connection_t *conn);
1344 int connection_dir_process_inbuf(connection_t *conn);
1345 int connection_dir_finished_flushing(connection_t *conn);
1346 int connection_dir_finished_connecting(connection_t *conn);
1347 void connection_dir_connect_failed(connection_t *conn);
1348 void parse_dir_policy(void);
1350 /********************************* dirserv.c ***************************/
1352 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1353 int dirserv_parse_fingerprint_file(const char *fname);
1354 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
1355 void dirserv_free_fingerprint_list(void);
1356 const char *dirserv_get_nickname_by_digest(const char *digest);
1357 int dirserv_add_descriptor(const char **desc, const char **msg);
1358 int dirserv_load_from_directory_string(const char *dir);
1359 void dirserv_free_descriptors(void);
1360 void dirserv_remove_old_servers(int age);
1361 int dirserv_dump_directory_to_string(char **dir_out,
1362 crypto_pk_env_t *private_key);
1363 void directory_set_dirty(void);
1364 size_t dirserv_get_directory(const char **cp, int compress);
1365 size_t dirserv_get_runningrouters(const char **rr, int compress);
1366 void dirserv_set_cached_directory(const char *directory, time_t when,
1367 int is_running_routers);
1369 /********************************* dns.c ***************************/
1371 void dns_init(void);
1372 int connection_dns_finished_flushing(connection_t *conn);
1373 int connection_dns_reached_eof(connection_t *conn);
1374 int connection_dns_process_inbuf(connection_t *conn);
1375 void dnsworkers_rotate(void);
1376 void connection_dns_remove(connection_t *conn);
1377 void assert_connection_edge_not_dns_pending(connection_t *conn);
1378 void assert_all_pending_dns_resolves_ok(void);
1379 void dns_cancel_pending_resolve(char *question);
1380 int dns_resolve(connection_t *exitconn);
1382 /********************************* hibernate.c **********************/
1384 int accounting_parse_options(or_options_t *options, int validate_only);
1385 int accounting_is_enabled(or_options_t *options);
1386 void configure_accounting(time_t now);
1387 void accounting_run_housekeeping(time_t now);
1388 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1389 int accounting_record_bandwidth_usage(time_t now);
1390 void hibernate_begin_shutdown(void);
1391 int we_are_hibernating(void);
1392 void consider_hibernation(time_t now);
1394 /********************************* main.c ***************************/
1396 int connection_add(connection_t *conn);
1397 int connection_remove(connection_t *conn);
1398 int connection_in_array(connection_t *conn);
1399 void add_connection_to_closeable_list(connection_t *conn);
1400 int connection_is_on_closeable_list(connection_t *conn);
1402 void get_connection_array(connection_t ***array, int *n);
1404 void connection_watch_events(connection_t *conn, short events);
1405 int connection_is_reading(connection_t *conn);
1406 void connection_stop_reading(connection_t *conn);
1407 void connection_start_reading(connection_t *conn);
1409 int connection_is_writing(connection_t *conn);
1410 void connection_stop_writing(connection_t *conn);
1411 void connection_start_writing(connection_t *conn);
1413 void directory_all_unreachable(time_t now);
1414 void directory_has_arrived(time_t now);
1416 int authdir_mode(or_options_t *options);
1417 int clique_mode(or_options_t *options);
1418 int server_mode(or_options_t *options);
1419 int advertised_server_mode(void);
1420 int proxy_mode(or_options_t *options);
1422 int control_signal_act(int the_signal);
1423 void handle_signals(int is_parent);
1424 void tor_cleanup(void);
1426 int tor_main(int argc, char *argv[]);
1428 /********************************* onion.c ***************************/
1430 int onion_pending_add(circuit_t *circ);
1431 circuit_t *onion_next_task(void);
1432 void onion_pending_remove(circuit_t *circ);
1434 int onion_skin_create(crypto_pk_env_t *router_key,
1435 crypto_dh_env_t **handshake_state_out,
1436 char *onion_skin_out);
1438 int onion_skin_server_handshake(char *onion_skin,
1439 crypto_pk_env_t *private_key,
1440 crypto_pk_env_t *prev_private_key,
1441 char *handshake_reply_out,
1442 char *key_out,
1443 size_t key_out_len);
1445 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
1446 char *handshake_reply,
1447 char *key_out,
1448 size_t key_out_len);
1450 /********************************* relay.c ***************************/
1452 extern unsigned long stats_n_relay_cells_relayed;
1453 extern unsigned long stats_n_relay_cells_delivered;
1455 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
1456 int cell_direction);
1458 void relay_header_pack(char *dest, const relay_header_t *src);
1459 void relay_header_unpack(relay_header_t *dest, const char *src);
1460 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
1461 int relay_command, const char *payload,
1462 size_t payload_len, crypt_path_t *cpath_layer);
1463 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
1464 void connection_edge_consider_sending_sendme(connection_t *conn);
1466 extern uint64_t stats_n_data_cells_packaged;
1467 extern uint64_t stats_n_data_bytes_packaged;
1468 extern uint64_t stats_n_data_cells_received;
1469 extern uint64_t stats_n_data_bytes_received;
1471 /********************************* rephist.c ***************************/
1473 void rep_hist_init(void);
1474 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1475 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1476 void rep_hist_note_disconnect(const char* nickname, time_t when);
1477 void rep_hist_note_connection_died(const char* nickname, time_t when);
1478 void rep_hist_note_extend_succeeded(const char *from_name,
1479 const char *to_name);
1480 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1481 void rep_hist_dump_stats(time_t now, int severity);
1482 void rep_hist_note_bytes_read(int num_bytes, time_t when);
1483 void rep_hist_note_bytes_written(int num_bytes, time_t when);
1484 int rep_hist_bandwidth_assess(void);
1485 char *rep_hist_get_bandwidth_lines(void);
1486 void rep_history_clean(time_t before);
1488 void rep_hist_note_used_port(uint16_t port, time_t now);
1489 smartlist_t *rep_hist_get_predicted_ports(time_t now);
1490 void rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity);
1491 int rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity);
1492 void rep_hist_note_used_resolve(time_t now);
1493 int rep_hist_get_predicted_resolve(time_t now);
1495 /********************************* rendclient.c ***************************/
1497 void rend_client_introcirc_has_opened(circuit_t *circ);
1498 void rend_client_rendcirc_has_opened(circuit_t *circ);
1499 int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
1500 void rend_client_refetch_renddesc(const char *query);
1501 int rend_client_remove_intro_point(char *failed_intro, const char *query);
1502 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
1503 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1504 void rend_client_desc_here(char *query);
1506 char *rend_client_get_random_intro(char *query);
1508 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1510 /********************************* rendcommon.c ***************************/
1512 typedef struct rend_service_descriptor_t {
1513 crypto_pk_env_t *pk;
1514 time_t timestamp;
1515 int n_intro_points;
1516 char **intro_points;
1517 } rend_service_descriptor_t;
1519 int rend_cmp_service_ids(const char *one, const char *two);
1521 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
1522 const char *payload);
1524 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1525 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1526 crypto_pk_env_t *key,
1527 char **str_out,
1528 size_t *len_out);
1529 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
1530 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1532 typedef struct rend_cache_entry_t {
1533 size_t len; /* Length of desc */
1534 time_t received; /* When did we get the descriptor? */
1535 char *desc; /* Service descriptor */
1536 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1537 } rend_cache_entry_t;
1539 void rend_cache_init(void);
1540 void rend_cache_clean(void);
1541 int rend_valid_service_id(const char *query);
1542 int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
1543 int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
1544 int rend_cache_store(const char *desc, size_t desc_len);
1546 /********************************* rendservice.c ***************************/
1548 int rend_config_services(or_options_t *options, int validate_only);
1549 int rend_service_load_keys(void);
1550 void rend_services_init(void);
1551 void rend_services_introduce(void);
1552 void rend_consider_services_upload(time_t now);
1554 void rend_service_intro_has_opened(circuit_t *circuit);
1555 int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
1556 void rend_service_rendezvous_has_opened(circuit_t *circuit);
1557 int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
1558 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1559 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1560 void rend_service_dump_stats(int severity);
1562 /********************************* rendmid.c *******************************/
1563 int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
1564 int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
1565 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1566 int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1568 /********************************* router.c ***************************/
1570 void set_onion_key(crypto_pk_env_t *k);
1571 crypto_pk_env_t *get_onion_key(void);
1572 crypto_pk_env_t *get_previous_onion_key(void);
1573 time_t get_onion_key_set_at(void);
1574 void set_identity_key(crypto_pk_env_t *k);
1575 crypto_pk_env_t *get_identity_key(void);
1576 int identity_key_is_set(void);
1577 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
1578 int init_keys(void);
1579 crypto_pk_env_t *init_key_from_file(const char *fname);
1580 void rotate_onion_key(void);
1581 void router_set_bandwidth_capacity(int bw);
1582 int router_get_bandwidth_capacity(void);
1584 void router_retry_connections(void);
1585 int router_is_clique_mode(routerinfo_t *router);
1586 void router_upload_dir_desc_to_dirservers(int force);
1587 void mark_my_descriptor_dirty(void);
1588 int router_compare_to_my_exit_policy(connection_t *conn);
1589 routerinfo_t *router_get_my_routerinfo(void);
1590 const char *router_get_my_descriptor(void);
1591 int router_is_me(routerinfo_t *router);
1592 int router_rebuild_descriptor(int force);
1593 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
1594 crypto_pk_env_t *ident_key);
1595 int is_legal_nickname(const char *s);
1596 int is_legal_nickname_or_hexdigest(const char *s);
1598 /********************************* routerlist.c ***************************/
1600 typedef struct trusted_dir_server_t {
1601 char *address;
1602 uint32_t addr;
1603 uint16_t dir_port;
1604 char digest[DIGEST_LEN];
1605 int is_running;
1606 } trusted_dir_server_t;
1608 int router_reload_router_list(void);
1609 void router_get_trusted_dir_servers(smartlist_t **outp);
1610 routerinfo_t *router_pick_directory_server(int requireothers,
1611 int fascistfirewall,
1612 int for_running_routers,
1613 int retry_if_no_servers);
1614 trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
1615 int fascistfirewall,
1616 int retry_if_no_servers);
1617 int all_trusted_directory_servers_down(void);
1618 struct smartlist_t;
1619 void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
1620 void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
1621 int router_nickname_is_in_list(routerinfo_t *router, const char *list);
1622 routerinfo_t *routerlist_find_my_routerinfo(void);
1623 int router_nickname_matches(routerinfo_t *router, const char *nickname);
1624 int exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
1625 int warn);
1627 /** How many seconds a router must be up before we'll use it for
1628 * reliability-critical node positions.
1630 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
1631 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
1633 int router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity);
1634 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
1635 routerinfo_t *router_choose_random_node(const char *preferred,
1636 const char *excluded,
1637 struct smartlist_t *excludedsmartlist,
1638 int need_uptime, int need_bandwidth,
1639 int allow_unverified, int strict);
1640 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
1641 routerinfo_t *router_get_by_nickname(const char *nickname);
1642 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
1643 routerinfo_t *router_get_by_digest(const char *digest);
1644 int router_digest_is_trusted_dir(const char *digest);
1645 void router_get_routerlist(routerlist_t **prouterlist);
1646 time_t routerlist_get_published_time(void);
1647 void routerlist_free(routerlist_t *routerlist);
1648 void routerinfo_free(routerinfo_t *router);
1649 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
1650 void router_mark_as_down(const char *digest);
1651 void routerlist_remove_old_routers(int age);
1652 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
1653 int dir_is_recent, int dir_is_cached);
1654 int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
1655 addr_policy_t *policy);
1656 #define ADDR_POLICY_ACCEPTED 0
1657 #define ADDR_POLICY_REJECTED -1
1658 #define ADDR_POLICY_UNKNOWN 1
1659 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
1660 int need_uptime);
1662 int router_exit_policy_rejects_all(routerinfo_t *router);
1663 void running_routers_free(running_routers_t *rr);
1664 void routerlist_update_from_runningrouters(routerlist_t *list,
1665 running_routers_t *rr);
1666 int routers_update_status_from_entry(smartlist_t *routers,
1667 time_t list_time,
1668 const char *s,
1669 int rr_format);
1670 int router_update_status_from_smartlist(routerinfo_t *r,
1671 time_t list_time,
1672 smartlist_t *running_list,
1673 int rr_format);
1674 void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
1675 void clear_trusted_dir_servers(void);
1677 /********************************* routerparse.c ************************/
1679 #define MAX_STATUS_TAG_LEN 32
1680 /** Structure to hold parsed Tor versions. This is a little messier
1681 * than we would like it to be, because we changed version schemes with 0.1.0.
1683 * Before 0.1.0, versions were of the format:
1684 * MAJOR.MINOR.MICRO(status(PATCHLEVEL))?(-cvs)?
1685 * where MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, status is one of
1686 * "pre" (for an alpha release), "rc" (for a release candidate), or "." for a
1687 * release. As a special case, "a.b.c" was equivalent to "a.b.c.0". We
1688 * compare the elements in order (major, minor, micro, status, patchlevel,
1689 * cvs), with "cvs" preceding non-cvs.
1691 * We would start each development branch with a final version in mind: say,
1692 * "0.0.8". Our first pre-release would be "0.0.8pre1", followed by (for
1693 * example) "0.0.8pre2-cvs", "0.0.8pre2", "0.0.8pre3-cvs", "0.0.8rc1",
1694 * "0.0.8rc2-cvs", and "0.0.8rc2". Finally, we'd release 0.0.8. The stable
1695 * CVS branch would then be versioned "0.0.8.1-cvs", and any eventual bugfix
1696 * release would be "0.0.8.1".
1698 * After 0.1.0, versions are of the format:
1699 * MAJOR.MINOR.MICRO(.PATCHLEVEL([-.]status_tag)?)?
1700 * As before, MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, with an absent
1701 * number equivalent to 0. All versions _should_ be distinguishable purely by
1702 * those four numbers; the status tag is purely informational. If we *do*
1703 * encounter two versions that differ only by status tag, we compare them
1704 * lexically.
1706 * Now, we start each development branch with (say) 0.1.1.1-alpha.
1707 * The patchlevel increments consistently as the status tag changes,
1708 * for example, as in: 0.1.1.2-alpha, 0.1.1.3-alpha, 0.1.1.4-rc
1709 * 0.1.1.5-rc, Eventually, we release 0.1.1.6. The next patch release
1710 * is 0.1.1.7.
1712 * Between these releases, CVS is versioned with a -cvs tag: after
1713 * 0.1.1.1-alpha comes 0.1.1.1-alpha-cvs, and so on.
1715 typedef struct tor_version_t {
1716 int major;
1717 int minor;
1718 int micro;
1719 /** Release status. For version in the post-0.1 format, this is always
1720 * VER_RELEASE. */
1721 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
1722 int patchlevel;
1723 /** CVS status. For version in the post-0.1 format, this is always
1724 * IS_NOT_CVS */
1725 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
1726 char status_tag[MAX_STATUS_TAG_LEN];
1727 } tor_version_t;
1729 int router_get_router_hash(const char *s, char *digest);
1730 int router_get_dir_hash(const char *s, char *digest);
1731 int router_get_runningrouters_hash(const char *s, char *digest);
1732 int router_parse_list_from_string(const char **s,
1733 routerlist_t **dest,
1734 smartlist_t *good_nickname_list,
1735 int rr_format,
1736 time_t published);
1737 int router_parse_routerlist_from_directory(const char *s,
1738 routerlist_t **dest,
1739 crypto_pk_env_t *pkey,
1740 int check_version,
1741 int write_to_cache);
1742 running_routers_t *router_parse_runningrouters(const char *str,
1743 int write_to_cache);
1744 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
1745 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
1746 addr_policy_t *router_parse_addr_policy_from_string(const char *s);
1747 int check_software_version_against_directory(const char *directory,
1748 int ignoreversion);
1749 int tor_version_parse(const char *s, tor_version_t *out);
1750 int tor_version_as_new_as(const char *platform, const char *cutoff);
1751 int tor_version_compare(tor_version_t *a, tor_version_t *b);
1752 void assert_addr_policy_ok(addr_policy_t *t);
1754 #endif