Rate-limit warnings related to unrecognized MyFamily elements.
[tor.git] / src / or / or.h
blob254de738b82eee107a972488621ee7a1cd47578e
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
9 * \brief Master header file for Tor-specific functionality.
10 **/
12 #ifndef __OR_H
13 #define __OR_H
14 #define OR_H_ID "$Id$"
16 #include "orconfig.h"
17 #ifdef MS_WINDOWS
18 #define WIN32_WINNT 0x400
19 #define _WIN32_WINNT 0x400
20 #define WIN32_LEAN_AND_MEAN
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #ifdef HAVE_SIGNAL_H
33 #include <signal.h>
34 #endif
35 #ifdef HAVE_NETDB_H
36 #include <netdb.h>
37 #endif
38 #ifdef HAVE_CTYPE_H
39 #include <ctype.h>
40 #endif
41 #include "../common/torint.h"
42 #ifdef HAVE_INTTYPES_H
43 #include <inttypes.h>
44 #endif
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
47 #endif
48 #ifdef HAVE_SYS_LIMITS_H
49 #include <sys/limits.h>
50 #endif
51 #ifdef HAVE_MACHINE_LIMITS_H
52 #ifndef __FreeBSD__
53 /* FreeBSD has a bug where it complains that this file is obsolete,
54 and I should migrate to using sys/limits. It complains even when
55 I include both. */
56 #include <machine/limits.h>
57 #endif
58 #endif
59 #ifdef HAVE_SYS_TYPES_H
60 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
61 #endif
62 #ifdef HAVE_SYS_WAIT_H
63 #include <sys/wait.h>
64 #endif
65 #ifdef HAVE_SYS_FCNTL_H
66 #include <sys/fcntl.h>
67 #endif
68 #ifdef HAVE_FCNTL_H
69 #include <fcntl.h>
70 #endif
71 #ifdef HAVE_SYS_IOCTL_H
72 #include <sys/ioctl.h>
73 #endif
74 #ifdef HAVE_SYS_SOCKET_H
75 #include <sys/socket.h>
76 #endif
77 #ifdef HAVE_SYS_TIME_H
78 #include <sys/time.h>
79 #endif
80 #ifdef HAVE_SYS_STAT_H
81 #include <sys/stat.h>
82 #endif
83 #ifdef HAVE_NETINET_IN_H
84 #include <netinet/in.h>
85 #endif
86 #ifdef HAVE_ARPA_INET_H
87 #include <arpa/inet.h>
88 #endif
89 #ifdef HAVE_ERRNO_H
90 #include <errno.h>
91 #endif
92 #ifdef HAVE_ASSERT_H
93 #include <assert.h>
94 #endif
95 #ifdef HAVE_TIME_H
96 #include <time.h>
97 #endif
99 /** Upper bound on maximum simultaneous connections; can be lowered by
100 * config file. */
101 #define MAXCONNECTIONS 15000
103 #ifdef MS_WINDOWS
104 /* No, we don't need to redefine FD_SETSIZE before including winsock:
105 * we use libevent now, and libevent handles the select() stuff. Yes,
106 * some documents imply that we need to redefine anyway if we're using
107 * select() anywhere in our application or in anything it links to: these
108 * documents are either the holy texts of a cargo cult of network
109 * programmers, or more likely a simplification of what's going on for
110 * people who haven't read winsock[2].c for themselves.
112 #if (_MSC_VER <= 1300)
113 #include <winsock.h>
114 #else
115 #include <winsock2.h>
116 #include <ws2tcpip.h>
117 #endif
118 #endif
120 #ifdef MS_WINDOWS
121 #include <io.h>
122 #include <process.h>
123 #include <direct.h>
124 #include <windows.h>
125 #define snprintf _snprintf
126 #endif
128 #ifdef HAVE_EVENT_H
129 #include <event.h>
130 #else
131 #error "Tor requires libevent to build."
132 #endif
134 #include "../common/crypto.h"
135 #include "../common/tortls.h"
136 #include "../common/log.h"
137 #include "../common/compat.h"
138 #include "../common/container.h"
139 #include "../common/util.h"
140 #include "../common/torgzip.h"
142 /* These signals are defined to help control_signal_act work.
143 * XXXX Move into compat.h ?
145 #ifndef SIGHUP
146 #define SIGHUP 1
147 #endif
148 #ifndef SIGINT
149 #define SIGINT 2
150 #endif
151 #ifndef SIGUSR1
152 #define SIGUSR1 10
153 #endif
154 #ifndef SIGUSR2
155 #define SIGUSR2 12
156 #endif
157 #ifndef SIGTERM
158 #define SIGTERM 15
159 #endif
161 #if (SIZEOF_CELL_T != 0)
162 /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
163 * that our stuff always calls cell_t something different. */
164 #define cell_t tor_cell_t
165 #endif
167 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
168 #define MAX_NICKNAME_LEN 19
169 /* Hex digest plus dollar sign. */
170 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
171 /** Maximum size, in bytes, for resized buffers. */
172 #define MAX_BUF_SIZE ((1<<24)-1)
173 #define MAX_DIR_SIZE MAX_BUF_SIZE
175 /* For http parsing */
176 #define MAX_HEADERS_SIZE 50000
177 #define MAX_BODY_SIZE 500000
179 #ifdef TOR_PERF
180 /** How long do we keep DNS cache entries before purging them? */
181 #define MAX_DNS_ENTRY_AGE (150*60)
182 #else
183 #define MAX_DNS_ENTRY_AGE (30*60)
184 #endif
186 /** How often do we rotate onion keys? */
187 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
188 /** How often do we rotate TLS contexts? */
189 #define MAX_SSL_KEY_LIFETIME (120*60)
191 /** How old do we allow a router to get before removing it, either
192 * from the descriptor list (for dirservers) or the router list (for others)?
193 * In seconds. */
194 #define ROUTER_MAX_AGE (60*60*24)
196 typedef enum {
197 CIRC_ID_TYPE_LOWER=0,
198 CIRC_ID_TYPE_HIGHER=1
199 } circ_id_type_t;
201 #define _CONN_TYPE_MIN 3
202 /** Type for sockets listening for OR connections. */
203 #define CONN_TYPE_OR_LISTENER 3
204 /** A bidirectional TLS connection transmitting a sequence of cells.
205 * May be from an OR to an OR, or from an OP to an OR. */
206 #define CONN_TYPE_OR 4
207 /** A TCP connection from an onion router to a stream's destination. */
208 #define CONN_TYPE_EXIT 5
209 /** Type for sockets listening for SOCKS connections. */
210 #define CONN_TYPE_AP_LISTENER 6
211 /** A SOCKS proxy connection from the user application to the onion
212 * proxy. */
213 #define CONN_TYPE_AP 7
214 /** Type for sockets listening for HTTP connections to the directory server. */
215 #define CONN_TYPE_DIR_LISTENER 8
216 /** Type for HTTP connections to the directory server. */
217 #define CONN_TYPE_DIR 9
218 /** Connection from the main process to a DNS worker process. */
219 #define CONN_TYPE_DNSWORKER 10
220 /** Connection from the main process to a CPU worker process. */
221 #define CONN_TYPE_CPUWORKER 11
222 /** Type for listenting for connections from user interface process */
223 #define CONN_TYPE_CONTROL_LISTENER 12
224 /** Type for connections from user interface process */
225 #define CONN_TYPE_CONTROL 13
226 #define _CONN_TYPE_MAX 13
228 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
230 /** State for any listener connection. */
231 #define LISTENER_STATE_READY 0
233 #define _DNSWORKER_STATE_MIN 1
234 /** State for a connection to a dnsworker process that's idle. */
235 #define DNSWORKER_STATE_IDLE 1
236 /** State for a connection to a dnsworker process that's resolving a hostname. */
237 #define DNSWORKER_STATE_BUSY 2
238 #define _DNSWORKER_STATE_MAX 2
240 #define _CPUWORKER_STATE_MIN 1
241 /** State for a connection to a cpuworker process that's idle. */
242 #define CPUWORKER_STATE_IDLE 1
243 /** State for a connection to a cpuworker process that's processing a
244 * handshake. */
245 #define CPUWORKER_STATE_BUSY_ONION 2
246 #define _CPUWORKER_STATE_MAX 2
248 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
250 #define _OR_CONN_STATE_MIN 1
251 /** State for a connection to an OR: waiting for connect() to finish. */
252 #define OR_CONN_STATE_CONNECTING 1
253 /** State for a connection to an OR: waiting for proxy command to flush. */
254 #define OR_CONN_STATE_PROXY_FLUSHING 2
255 /** State for a connection to an OR: waiting for proxy response. */
256 #define OR_CONN_STATE_PROXY_READING 3
257 /** State for a connection to an OR: SSL is handshaking, not done yet. */
258 #define OR_CONN_STATE_HANDSHAKING 4
259 /** State for a connection to an OR: Ready to send/receive cells. */
260 #define OR_CONN_STATE_OPEN 5
261 #define _OR_CONN_STATE_MAX 5
263 #define _EXIT_CONN_STATE_MIN 1
264 /** State for an exit connection: waiting for response from dns farm. */
265 #define EXIT_CONN_STATE_RESOLVING 1
266 /** State for an exit connection: waiting for connect() to finish. */
267 #define EXIT_CONN_STATE_CONNECTING 2
268 /** State for an exit connection: open and ready to transmit data. */
269 #define EXIT_CONN_STATE_OPEN 3
270 /** State for an exit connection: waiting to be removed. */
271 #define EXIT_CONN_STATE_RESOLVEFAILED 4
272 #define _EXIT_CONN_STATE_MAX 4
274 /* the AP state values must be disjoint from the EXIT state values */
275 #define _AP_CONN_STATE_MIN 5
276 /** State for a SOCKS connection: waiting for SOCKS request. */
277 #define AP_CONN_STATE_SOCKS_WAIT 5
278 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
279 * rendezvous rescriptor. */
280 #define AP_CONN_STATE_RENDDESC_WAIT 6
281 /** The controller will attach this connection to a circuit; it isn't our
282 * job to do so. */
283 #define AP_CONN_STATE_CONTROLLER_WAIT 7
284 /** State for a SOCKS connection: waiting for a completed circuit. */
285 #define AP_CONN_STATE_CIRCUIT_WAIT 8
286 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
287 #define AP_CONN_STATE_CONNECT_WAIT 9
288 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
289 #define AP_CONN_STATE_RESOLVE_WAIT 10
290 /** State for a SOCKS connection: ready to send and receive. */
291 #define AP_CONN_STATE_OPEN 11
292 #define _AP_CONN_STATE_MAX 11
294 #define _DIR_CONN_STATE_MIN 1
295 /** State for connection to directory server: waiting for connect(). */
296 #define DIR_CONN_STATE_CONNECTING 1
297 /** State for connection to directory server: sending HTTP request. */
298 #define DIR_CONN_STATE_CLIENT_SENDING 2
299 /** State for connection to directory server: reading HTTP response. */
300 #define DIR_CONN_STATE_CLIENT_READING 3
301 /** State for connection to directory server: happy and finished. */
302 #define DIR_CONN_STATE_CLIENT_FINISHED 4
303 /** State for connection at directory server: waiting for HTTP request. */
304 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
305 /** State for connection at directory server: sending HTTP response. */
306 #define DIR_CONN_STATE_SERVER_WRITING 6
307 #define _DIR_CONN_STATE_MAX 6
309 #define _CONTROL_CONN_STATE_MIN 1
310 #define CONTROL_CONN_STATE_OPEN_V0 1
311 #define CONTROL_CONN_STATE_OPEN_V1 2
312 #define CONTROL_CONN_STATE_NEEDAUTH_V0 3
313 #define CONTROL_CONN_STATE_NEEDAUTH_V1 4
314 #define _CONTROL_CONN_STATE_MAX 4
316 #define _DIR_PURPOSE_MIN 1
317 /** Purpose for connection to directory server: download a directory. */
318 #define DIR_PURPOSE_FETCH_DIR 1
319 /** Purpose for connection to directory server: download just the list
320 * of running routers. */
321 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
322 /** Purpose for connection to directory server: download a rendezvous
323 * descriptor. */
324 #define DIR_PURPOSE_FETCH_RENDDESC 3
325 /** Purpose for connection to directory server: set after a rendezvous
326 * descriptor is downloaded. */
327 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
328 /** A connection to a directory server: download one or more network-status
329 * objects */
330 #define DIR_PURPOSE_FETCH_NETWORKSTATUS 5
331 /** A connection to a directory server: download one or more server
332 * descriptors. */
333 #define DIR_PURPOSE_FETCH_SERVERDESC 6
334 /** Purpose for connection to directory server: upload a server descriptor. */
335 #define DIR_PURPOSE_UPLOAD_DIR 7
336 /** Purpose for connection to directory server: upload a rendezvous
337 * descriptor. */
338 #define DIR_PURPOSE_UPLOAD_RENDDESC 8
339 /** Purpose for connection at a directory server. */
340 #define DIR_PURPOSE_SERVER 9
341 #define _DIR_PURPOSE_MAX 9
343 #define _EXIT_PURPOSE_MIN 1
344 #define EXIT_PURPOSE_CONNECT 1
345 #define EXIT_PURPOSE_RESOLVE 2
346 #define _EXIT_PURPOSE_MAX 2
348 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
349 #define CIRCUIT_STATE_BUILDING 0
350 /** Circuit state: Waiting to process the onionskin. */
351 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
352 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
353 #define CIRCUIT_STATE_OR_WAIT 2
354 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
355 #define CIRCUIT_STATE_OPEN 3
357 #define _CIRCUIT_PURPOSE_MIN 1
359 /* these circuits were initiated elsewhere */
360 #define _CIRCUIT_PURPOSE_OR_MIN 1
361 /** OR-side circuit purpose: normal circuit, at OR. */
362 #define CIRCUIT_PURPOSE_OR 1
363 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
364 #define CIRCUIT_PURPOSE_INTRO_POINT 2
365 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
366 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
367 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
368 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
369 #define _CIRCUIT_PURPOSE_OR_MAX 4
371 /* these circuits originate at this node */
373 /* here's how circ client-side purposes work:
374 * normal circuits are C_GENERAL.
375 * circuits that are c_introducing are either on their way to
376 * becoming open, or they are open and waiting for a
377 * suitable rendcirc before they send the intro.
378 * circuits that are c_introduce_ack_wait have sent the intro,
379 * but haven't gotten a response yet.
380 * circuits that are c_establish_rend are either on their way
381 * to becoming open, or they are open and have sent the
382 * establish_rendezvous cell but haven't received an ack.
383 * circuits that are c_rend_ready are open and have received a
384 * rend ack, but haven't heard from bob yet. if they have a
385 * buildstate->pending_final_cpath then they're expecting a
386 * cell from bob, else they're not.
387 * circuits that are c_rend_ready_intro_acked are open, and
388 * some intro circ has sent its intro and received an ack.
389 * circuits that are c_rend_joined are open, have heard from
390 * bob, and are talking to him.
392 /** Client-side circuit purpose: Normal circuit, with cpath. */
393 #define CIRCUIT_PURPOSE_C_GENERAL 5
394 /** Client-side circuit purpose: at Alice, connecting to intro point. */
395 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
396 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
397 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
398 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
399 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
400 /** Client-side circuit purpose: at Alice, waiting for ack. */
401 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
402 /** Client-side circuit purpose: at Alice, waiting for Bob. */
403 #define CIRCUIT_PURPOSE_C_REND_READY 10
404 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
405 * has been acknowledged. */
406 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
407 /** Client-side circuit purpose: at Alice, rendezvous established. */
408 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
410 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
411 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
412 /** Hidden-service-side circuit purpose: at Bob, successfully established
413 * intro. */
414 #define CIRCUIT_PURPOSE_S_INTRO 14
415 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
416 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
417 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
418 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
419 /** A testing circuit; not meant to be used for actual traffic. */
420 #define CIRCUIT_PURPOSE_TESTING 17
421 #define _CIRCUIT_PURPOSE_MAX 17
423 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
424 * that this OP has originated. */
425 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
426 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
428 #define RELAY_COMMAND_BEGIN 1
429 #define RELAY_COMMAND_DATA 2
430 #define RELAY_COMMAND_END 3
431 #define RELAY_COMMAND_CONNECTED 4
432 #define RELAY_COMMAND_SENDME 5
433 #define RELAY_COMMAND_EXTEND 6
434 #define RELAY_COMMAND_EXTENDED 7
435 #define RELAY_COMMAND_TRUNCATE 8
436 #define RELAY_COMMAND_TRUNCATED 9
437 #define RELAY_COMMAND_DROP 10
438 #define RELAY_COMMAND_RESOLVE 11
439 #define RELAY_COMMAND_RESOLVED 12
441 #define RELAY_COMMAND_ESTABLISH_INTRO 32
442 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
443 #define RELAY_COMMAND_INTRODUCE1 34
444 #define RELAY_COMMAND_INTRODUCE2 35
445 #define RELAY_COMMAND_RENDEZVOUS1 36
446 #define RELAY_COMMAND_RENDEZVOUS2 37
447 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
448 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
449 #define RELAY_COMMAND_INTRODUCE_ACK 40
451 #define END_STREAM_REASON_MISC 1
452 #define END_STREAM_REASON_RESOLVEFAILED 2
453 #define END_STREAM_REASON_CONNECTREFUSED 3
454 #define END_STREAM_REASON_EXITPOLICY 4
455 #define END_STREAM_REASON_DESTROY 5
456 #define END_STREAM_REASON_DONE 6
457 #define END_STREAM_REASON_TIMEOUT 7
458 /* 8 is unallocated. */
459 #define END_STREAM_REASON_HIBERNATING 9
460 #define END_STREAM_REASON_INTERNAL 10
461 #define END_STREAM_REASON_RESOURCELIMIT 11
462 #define END_STREAM_REASON_CONNRESET 12
463 #define END_STREAM_REASON_TORPROTOCOL 13
465 /* These high-numbered end reasons are not part of the official spec,
466 * and are not intended to be put in relay end cells. They are here
467 * to be more informative when sending back socks replies to the
468 * application. */
469 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
470 #define END_STREAM_REASON_CANT_ATTACH 257
471 #define END_STREAM_REASON_NET_UNREACHABLE 258
473 #define RESOLVED_TYPE_HOSTNAME 0
474 #define RESOLVED_TYPE_IPV4 4
475 #define RESOLVED_TYPE_IPV6 6
476 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
477 #define RESOLVED_TYPE_ERROR 0xF1
479 /** Length of 'y' portion of 'y.onion' URL. */
480 #define REND_SERVICE_ID_LEN 16
482 #define CELL_DIRECTION_IN 1
483 #define CELL_DIRECTION_OUT 2
485 #ifdef TOR_PERF
486 #define CIRCWINDOW_START 10000
487 #define CIRCWINDOW_INCREMENT 1000
488 #define STREAMWINDOW_START 5000
489 #define STREAMWINDOW_INCREMENT 500
490 #else
491 #define CIRCWINDOW_START 1000
492 #define CIRCWINDOW_INCREMENT 100
493 #define STREAMWINDOW_START 500
494 #define STREAMWINDOW_INCREMENT 50
495 #endif
497 /* cell commands */
498 #define CELL_PADDING 0
499 #define CELL_CREATE 1
500 #define CELL_CREATED 2
501 #define CELL_RELAY 3
502 #define CELL_DESTROY 4
503 #define CELL_CREATE_FAST 5
504 #define CELL_CREATED_FAST 6
506 /* people behind fascist firewalls use only these ports */
507 #define REQUIRED_FIREWALL_DIRPORT 80
508 #define REQUIRED_FIREWALL_ORPORT 443
510 /* legal characters in a nickname */
511 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
513 /** Name to use in client TLS certificates if no nickname is given.*/
514 #define DEFAULT_CLIENT_NICKNAME "client"
516 #define SOCKS4_NETWORK_LEN 8
518 typedef enum {
519 SOCKS5_SUCCEEDED = 0x00,
520 SOCKS5_GENERAL_ERROR = 0x01,
521 SOCKS5_NOT_ALLOWED = 0x02,
522 SOCKS5_NET_UNREACHABLE = 0x03,
523 SOCKS5_HOST_UNREACHABLE = 0x04,
524 SOCKS5_CONNECTION_REFUSED = 0x05,
525 SOCKS5_TTL_EXPIRED = 0x06,
526 SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
527 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
528 } socks5_reply_status_t;
531 * Relay payload:
532 * Relay command [1 byte]
533 * Recognized [2 bytes]
534 * Stream ID [2 bytes]
535 * Partial SHA-1 [4 bytes]
536 * Length [2 bytes]
537 * Relay payload [498 bytes]
540 #define CELL_PAYLOAD_SIZE 509
541 #define CELL_NETWORK_SIZE 512
543 #define RELAY_HEADER_SIZE (1+2+2+4+2)
544 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
546 /** Parsed onion routing cell. All communication from OP-to-OR, or from
547 * OR-to-OR, is via cells. */
548 typedef struct {
549 uint16_t circ_id; /**< Circuit which received the cell. */
550 uint8_t command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
551 * or DESTROY. */
552 char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
553 } cell_t;
555 /** Beginning of a RELAY cell payload. */
556 typedef struct {
557 uint8_t command; /**< The end-to-end relay command. */
558 uint16_t recognized; /**< Used to tell whether cell is for us. */
559 uint16_t stream_id; /**< Which stream is this cell associated with? */
560 char integrity[4]; /**< Used to tell whether cell is corrupted. */
561 uint16_t length; /**< How long is the payload body? */
562 } relay_header_t;
564 typedef struct buf_t buf_t;
565 typedef struct socks_request_t socks_request_t;
567 #define CONNECTION_MAGIC 0x7C3C304Eu
569 /** Description of a connection to another host or process, and associated
570 * data.
572 * A connection is named based on what it's connected to -- an "OR
573 * connection" has an onion router on the other end, an "OP connection"
574 * (nearly obsolete) has an onion proxy on the other end, an "exit
575 * connection" has a website or other server on the other end, and an
576 * "AP connection" has an application proxy (and thus a user) on the
577 * other end.
579 * Every connection has a type and a state. Connections never change
580 * their type, but can go through many state changes in their lifetime.
582 * Every connection has two associated input and output buffers.
583 * Listeners don't use them. For non-listener connections, incoming
584 * data is appended to conn->inbuf, and outgoing data is taken from
585 * conn->outbuf. Connections differ primarily in the functions called
586 * to fill and drain these buffers.
588 struct connection_t {
589 uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
591 uint8_t type; /**< What kind of connection is this? */
592 uint8_t state; /**< Current state of this connection. */
593 uint8_t purpose; /**< Only used for DIR types currently. */
594 unsigned wants_to_read:1; /**< Boolean: should we start reading again once
595 * the bandwidth throttler allows it?
597 unsigned wants_to_write:1; /**< Boolean: should we start writing again once
598 * the bandwidth throttler allows reads?
600 unsigned hold_open_until_flushed:1; /**< Despite this connection's being
601 * marked for close, do we flush it
602 * before closing it?
604 unsigned has_sent_end:1; /**< For debugging; only used on edge connections.
605 * Set once we've set the stream end,
606 * and check in circuit_about_to_close_connection(). */
608 int s; /**< Our socket; -1 if this connection is closed. */
609 int poll_index; /* XXXX rename. */
610 struct event *read_event; /**< libevent event structure. */
611 struct event *write_event; /**< libevent event structure. */
612 int marked_for_close; /**< Should we close this conn on the next iteration
613 * of the main loop? (If true, holds the line number
614 * where this connection was marked.)
616 const char *marked_for_close_file; /**< For debugging: in which file were
617 * we marked for close? */
619 buf_t *inbuf; /**< Buffer holding data read over this connection. */
620 int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
621 time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
623 buf_t *outbuf; /**< Buffer holding data to write over this connection. */
624 size_t outbuf_flushlen; /**< How much data should we try to flush from the
625 * outbuf? */
626 time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
628 time_t timestamp_created; /**< When was this connection_t created? */
629 time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
631 uint32_t addr; /**< IP of the other side of the connection; used to identify
632 * routers, along with port. */
633 uint16_t port; /**< If non-zero, port on the other end
634 * of the connection. */
635 char *address; /**< FQDN (or IP) of the guy on the other end.
636 * strdup into this, because free_connection frees it.
638 crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
639 * signing key. */
640 char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
641 char *nickname; /**< Nickname of OR on other side (if any). */
643 /** Nickname of planned exit node -- used with .exit support. */
644 char *chosen_exit_name;
646 /* Used only by OR connections: */
647 tor_tls *tls; /**< TLS connection state (OR only.) */
648 uint16_t next_circ_id; /**< Which circ_id do we try to use next on
649 * this connection? This is always in the
650 * range 0..1<<15-1. (OR only.)*/
652 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
653 int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
654 int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
655 * add 'bandwidth' to this, capping it at 10*bandwidth.
656 * (OPEN ORs only)
658 circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
659 * connection, which half of the space should
660 * we use? */
662 /* Used only by DIR and AP connections: */
663 char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
664 * querying for? (DIR/AP only) */
666 /* Used only by edge connections: */
667 uint16_t stream_id;
668 struct connection_t *next_stream; /**< Points to the next stream at this
669 * edge, if any (Edge only). */
670 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
671 * this conn exits at. (Edge only.) */
672 int package_window; /**< How many more relay cells can i send into the
673 * circuit? (Edge only.) */
674 int deliver_window; /**< How many more relay cells can end at me? (Edge
675 * only.) */
677 #if 0
678 int done_sending; /**< For half-open connections; not used currently. */
679 int done_receiving; /**< For half-open connections; not used currently. */
680 #endif
681 struct circuit_t *on_circuit; /**< The circuit (if any) that this edge
682 * connection is using. */
684 /* Used only by Dir connections */
685 char *requested_resource; /**< Which 'resource' did we ask the directory for?*/
687 /* Used only by AP connections */
688 socks_request_t *socks_request; /**< SOCKS structure describing request (AP
689 * only.) */
691 /** Quasi-global identifier for this connection; used for control.c */
692 /* XXXX NM This can get re-used after 2**32 circuits. */
693 uint32_t global_identifier;
695 /* Used only by control connections */
696 uint32_t event_mask;
697 uint32_t incoming_cmd_len;
698 uint32_t incoming_cmd_cur_len;
699 char *incoming_cmd;
701 /* Used only by control v0 connections */
702 uint16_t incoming_cmd_type;
705 typedef struct connection_t connection_t;
707 typedef enum {
708 ADDR_POLICY_ACCEPT=1,
709 ADDR_POLICY_REJECT=2,
710 } addr_policy_action_t;
712 /** A linked list of policy rules */
713 typedef struct addr_policy_t {
714 addr_policy_action_t policy_type; /**< What to do when the policy matches.*/
715 char *string; /**< String representation of this rule. */
716 uint32_t addr; /**< Base address to accept or reject. */
717 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
718 * a &amp; msk == <b>addr</b> &amp; msk . */
719 uint16_t prt_min; /**< Lowest port number to accept/reject. */
720 uint16_t prt_max; /**< Highest port number to accept/reject. */
722 struct addr_policy_t *next; /**< Next rule in list. */
723 } addr_policy_t;
725 /** A cached_dir_t represents a cacheable directory object, along with its
726 * compressed form. */
727 typedef struct cached_dir_t {
728 char *dir; /**< Contents of this object */
729 char *dir_z; /**< Compressed contents of this object. */
730 size_t dir_len; /**< Length of <b>dir</b> */
731 size_t dir_z_len; /**< Length of <b>dir_z</b> */
732 time_t published; /**< When was this object published */
733 } cached_dir_t;
735 /** Information about another onion router in the network. */
736 typedef struct {
737 char *signed_descriptor; /**< The original signed descriptor for this router*/
738 size_t signed_descriptor_len; /**< The length of signed_descriptor */
739 char signed_descriptor_digest[DIGEST_LEN]; /**< The digest of the signed descriptor. */
741 char *address; /**< Location of OR: either a hostname or an IP address. */
742 char *nickname; /**< Human-readable OR name. */
744 uint32_t addr; /**< IPv4 address of OR, in host order. */
745 uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
746 uint16_t dir_port; /**< Port for HTTP directory connections. */
748 time_t published_on; /**< When was the information in this routerinfo_t
749 * published? */
751 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
752 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
753 char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
755 char *platform; /**< What software/operating system is this OR using? */
757 /* link info */
758 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
759 * bucket per second? */
760 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
761 /** How many bytes/s is this router known to handle? */
762 uint32_t bandwidthcapacity;
763 addr_policy_t *exit_policy; /**< What streams will this OR permit
764 * to exit? */
765 long uptime; /**< How many seconds the router claims to have been up */
766 smartlist_t *declared_family; /**< Nicknames of router which this router
767 * claims are its family. */
768 char *contact_info; /**< Declared contact info for this router. */
769 unsigned int is_hibernating:1; /**< Whether the router claims to be
770 * hibernating */
772 /* local info */
773 unsigned int is_running:1; /**< As far as we know, is this OR currently
774 * running? */
775 unsigned int is_verified:1; /**< Has a trusted dirserver validated this OR?
776 * (For Authdir: Have we validated this OR?)
778 unsigned int is_named:1; /**< Do we believe the nickname that this OR gives
779 * us? */
781 /* The below items are used only by authdirservers for
782 * reachability testing. */
783 /** When was the last time we could reach this OR? */
784 time_t last_reachable;
785 /** When did we start testing reachability for this OR? */
786 time_t testing_since;
787 /** How many times has a descriptor been posted and we believed
788 * this router to be unreachable? We only actually warn on the third. */
789 int num_unreachable_notifications;
790 } routerinfo_t;
792 /** Contents of a single router entry in a network status object.
794 typedef struct routerstatus_t {
795 time_t published_on; /**< When was this router published? */
796 char nickname[MAX_NICKNAME_LEN+1]; /**<The nickname this router says it has. */
797 char identity_digest[DIGEST_LEN]; /**< Digest of the router's identity key. */
798 char descriptor_digest[DIGEST_LEN]; /**< Digest of the router's most recent
799 * descriptor. */
800 uint32_t addr; /**< IPv4 address for this router. */
801 uint16_t or_port; /**< OR port for this router. */
802 uint16_t dir_port; /**< Directory port for this router. */
803 unsigned int is_exit:1; /**< True iff this router is a good exit. */
804 unsigned int is_stable:1; /**< True iff this router stays up a long time. */
805 unsigned int is_fast:1; /**< True iff this router has good bandwidth. */
806 unsigned int is_running:1; /**< True iff this router is up. */
807 unsigned int is_named:1; /**< True iff "nickname" belongs to this router. */
808 unsigned int is_valid:1; /**< True iff this router is validated. */
809 } routerstatus_t;
811 /** DOCDOC */
812 typedef struct local_routerstatus_t {
813 routerstatus_t status;
814 time_t next_attempt_at; /**< When should we try this descriptor again? */
815 uint8_t n_download_failures; /**< Number of failures trying to download the
816 * most recent descriptor. */
817 unsigned int should_download:1; /**< DOCDOC */
818 unsigned int name_lookup_warned:1; /*< Have we warned the user for referring
819 * to this (unnamed) router by nickname?
821 } local_routerstatus_t;
823 /*XXXX011 make this configurable? */
824 #define MAX_ROUTERDESC_DOWNLOAD_FAILURES 8
826 /** Contents of a (v2 or later) network status object. */
827 typedef struct networkstatus_t {
828 /** When did we receive the network-status document? */
829 time_t received_on;
831 /** What was the digest of the document? */
832 char networkstatus_digest[DIGEST_LEN];
834 unsigned int is_recent; /**< Is this recent enough to influence running
835 * status? */
837 /* These fields come from the actual network-status document.*/
838 time_t published_on; /**< Declared publication date. */
840 char *source_address; /**< Canonical directory server hostname. */
841 uint32_t source_addr; /**< Canonical directory server IP. */
842 uint16_t source_dirport; /**< Canonical directory server dirport. */
844 char identity_digest[DIGEST_LEN]; /**< Digest of signing key. */
845 char *contact; /**< How to contact directory admin? (may be NULL). */
846 crypto_pk_env_t *signing_key; /**< Key used to sign this directory. */
847 char *client_versions; /**< comma-separated list of recommended client
848 * versions. */
849 char *server_versions; /**< comma-separated list of recommended server
850 * versions. */
852 unsigned int binds_names:1; /**< True iff this directory server binds names. */
853 unsigned int recommends_versions:1; /**< True iff this directory server
854 * recommends client and server software
855 * versions. */
857 smartlist_t *entries; /**< List of router_status_t*. This list is kept
858 * sorted by identity_digest. */
859 } networkstatus_t;
861 /** Contents of a directory of onion routers. */
862 typedef struct {
863 /** List of routerinfo_t. */
864 smartlist_t *routers;
865 } routerlist_t;
867 /** Information on router used when extending a circuit. (We don't need a
868 * full routerinfo_t to extend: we only need addr:port:keyid to build an OR
869 * connection, and onion_key to create the onionskin.) */
870 typedef struct extend_info_t {
871 char nickname[MAX_HEX_NICKNAME_LEN+1]; /**< This router's nickname for display. */
872 char identity_digest[DIGEST_LEN]; /**< Hash of this router's identity key. */
873 uint32_t addr; /**< IP address in host order. */
874 uint16_t port; /**< OR port. */
875 crypto_pk_env_t *onion_key; /**< Current onionskin key. */
876 } extend_info_t;
878 #define CRYPT_PATH_MAGIC 0x70127012u
880 /** Holds accounting information for a single step in the layered encryption
881 * performed by a circuit. Used only at the client edge of a circuit. */
882 typedef struct crypt_path_t {
883 uint32_t magic;
885 /* crypto environments */
886 /** Encryption key and counter for cells heading towards the OR at this
887 * step. */
888 crypto_cipher_env_t *f_crypto;
889 /** Encryption key and counter for cells heading back from the OR at this
890 * step. */
891 crypto_cipher_env_t *b_crypto;
893 /** Digest state for cells heading towards the OR at this step. */
894 crypto_digest_env_t *f_digest; /* for integrity checking */
895 /** Digest state for cells heading away from the OR at this step. */
896 crypto_digest_env_t *b_digest;
898 /** Current state of Diffie-Hellman key negotiation with the OR at this
899 * step. */
900 crypto_dh_env_t *dh_handshake_state;
901 char fast_handshake_state[DIGEST_LEN];
902 /** Negotiated key material shared with the OR at this step. */
903 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
905 /** Information to extend to the OR at this step. */
906 extend_info_t *extend_info;
908 /** Is the circuit built to this step? Must be one of:
909 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
910 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
911 * and not received an EXTENDED/CREATED)
912 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
913 uint8_t state;
914 #define CPATH_STATE_CLOSED 0
915 #define CPATH_STATE_AWAITING_KEYS 1
916 #define CPATH_STATE_OPEN 2
917 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
918 * (The list is circular, so the last node
919 * links to the first.) */
920 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
921 * circuit. */
923 int package_window; /**< How many bytes are we allowed to originate ending
924 * at this step? */
925 int deliver_window; /**< How many bytes are we willing to deliver originating
926 * at this step? */
927 } crypt_path_t;
929 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
931 #define DH_KEY_LEN DH_BYTES
932 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
933 CIPHER_KEY_LEN+\
934 DH_KEY_LEN)
935 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
936 #define REND_COOKIE_LEN DIGEST_LEN
938 /** Information used to build a circuit. */
939 typedef struct {
940 /** Intended length of the final circuit. */
941 int desired_path_len;
942 /** How to extend to the planned exit node. */
943 extend_info_t *chosen_exit;
944 /** Whether every node in the circ must have adequate uptime. */
945 int need_uptime;
946 /** Whether every node in the circ must have adequate capacity. */
947 int need_capacity;
948 /** Whether the last hop was picked with exiting in mind. */
949 int is_internal;
950 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
951 crypt_path_t *pending_final_cpath;
952 /** How many times has building a circuit for this task failed? */
953 int failure_count;
954 /** At what time should we give up on this task? */
955 time_t expiry_time;
956 } cpath_build_state_t;
958 #define CIRCUIT_MAGIC 0x35315243u
961 * A circuit is a path over the onion routing
962 * network. Applications can connect to one end of the circuit, and can
963 * create exit connections at the other end of the circuit. AP and exit
964 * connections have only one circuit associated with them (and thus these
965 * connection types are closed when the circuit is closed), whereas
966 * OR connections multiplex many circuits at once, and stay standing even
967 * when there are no circuits running over them.
969 * A circuit_t structure fills two roles. First, a circuit_t links two
970 * connections together: either an edge connection and an OR connection,
971 * or two OR connections. (When joined to an OR connection, a circuit_t
972 * affects only cells sent to a particular circID on that connection. When
973 * joined to an edge connection, a circuit_t affects all data.)
975 * Second, a circuit_t holds the cipher keys and state for sending data
976 * along a given circuit. At the OP, it has a sequence of ciphers, each
977 * of which is shared with a single OR along the circuit. Separate
978 * ciphers are used for data going "forward" (away from the OP) and
979 * "backward" (towards the OP). At the OR, a circuit has only two stream
980 * ciphers: one for data going forward, and one for data going backward.
982 struct circuit_t {
983 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
985 int marked_for_close; /**< Should we close this circuit at the end of the
986 * main loop? (If true, holds the line number where
987 * this circuit was marked.) */
988 const char *marked_for_close_file; /**< For debugging: in which file was this
989 * circuit marked for close? */
991 /** The IPv4 address of the OR that is next in this circuit. */
992 uint32_t n_addr;
993 /** The port for the OR that is next in this circuit. */
994 uint16_t n_port;
995 /** The OR connection that is previous in this circuit. */
996 connection_t *p_conn;
997 /** The OR connection that is next in this circuit. */
998 connection_t *n_conn;
999 /** The identity hash of n_conn. */
1000 char n_conn_id_digest[DIGEST_LEN];
1001 /** Linked list of AP streams associated with this circuit. */
1002 connection_t *p_streams;
1003 /** Linked list of Exit streams associated with this circuit. */
1004 connection_t *n_streams;
1005 /** Linked list of Exit streams associated with this circuit that are
1006 * still being resolved. */
1007 connection_t *resolving_streams;
1008 /** The next stream_id that will be tried when we're attempting to
1009 * construct a new AP stream originating at this circuit. */
1010 uint16_t next_stream_id;
1011 /** How many relay data cells can we package (read from edge streams)
1012 * on this circuit before we receive a circuit-level sendme cell asking
1013 * for more? */
1014 int package_window;
1015 /** How many relay data cells will we deliver (write to edge streams)
1016 * on this circuit? When deliver_window gets low, we send some
1017 * circuit-level sendme cells to indicate that we're willing to accept
1018 * more. */
1019 int deliver_window;
1021 /** The circuit_id used in the previous (backward) hop of this circuit. */
1022 uint16_t p_circ_id;
1023 /** The circuit_id used in the next (forward) hop of this circuit. */
1024 uint16_t n_circ_id;
1026 /** The cipher used by intermediate hops for cells heading toward the
1027 * OP. */
1028 crypto_cipher_env_t *p_crypto;
1029 /** The cipher used by intermediate hops for cells heading away from
1030 * the OP. */
1031 crypto_cipher_env_t *n_crypto;
1033 /** The integrity-checking digest used by intermediate hops, for
1034 * cells packaged here and heading towards the OP.
1036 crypto_digest_env_t *p_digest;
1037 /** The integrity-checking digest used by intermediate hops, for
1038 * cells packaged at the OP and arriving here.
1040 crypto_digest_env_t *n_digest;
1042 /** Build state for this circuit. It includes the intended path
1043 * length, the chosen exit router, rendezvous information, etc.
1045 cpath_build_state_t *build_state;
1046 /** The doubly-linked list of crypt_path_t entries, one per hop,
1047 * for this circuit. This includes ciphers for each hop,
1048 * integrity-checking digests for each hop, and package/delivery
1049 * windows for each hop.
1051 * The cpath field is defined only when we are the circuit's origin.
1053 crypt_path_t *cpath;
1055 /** For storage while passing to cpuworker, or while n_conn is pending. */
1056 char onionskin[ONIONSKIN_CHALLENGE_LEN];
1058 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
1060 time_t timestamp_created; /**< When was this circuit created? */
1061 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
1062 * circuit is clean. */
1064 uint8_t state; /**< Current status of this circuit. */
1065 uint8_t purpose; /**< Why are we creating this circuit? */
1068 * The rend_query field holds y portion of y.onion (nul-terminated)
1069 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
1070 * for a hidden service, or is S_*.
1072 char rend_query[REND_SERVICE_ID_LEN+1];
1074 /** The rend_pk_digest field holds a hash of location-hidden service's
1075 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
1077 char rend_pk_digest[DIGEST_LEN];
1079 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
1080 * C_ESTABLISH_REND. Filled with zeroes otherwise.
1082 char rend_cookie[REND_COOKIE_LEN];
1084 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
1085 * is not marked for close. */
1086 struct circuit_t *rend_splice;
1088 /** Quasi-global identifier for this circuit; used for control.c */
1089 /* XXXX NM This can get re-used after 2**32 circuits. */
1090 uint32_t global_identifier;
1092 struct circuit_t *next; /**< Next circuit in linked list. */
1095 typedef struct circuit_t circuit_t;
1097 #define ALLOW_UNVERIFIED_ENTRY 1
1098 #define ALLOW_UNVERIFIED_EXIT 2
1099 #define ALLOW_UNVERIFIED_MIDDLE 4
1100 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
1101 #define ALLOW_UNVERIFIED_INTRODUCTION 16
1103 typedef struct exit_redirect_t {
1104 uint32_t addr;
1105 uint32_t mask;
1106 uint16_t port_min;
1107 uint16_t port_max;
1109 uint32_t addr_dest;
1110 uint16_t port_dest;
1111 unsigned is_redirect:1;
1112 } exit_redirect_t;
1114 typedef struct config_line_t {
1115 char *key;
1116 char *value;
1117 struct config_line_t *next;
1118 } config_line_t;
1120 /** Configuration options for a Tor process */
1121 typedef struct {
1122 uint32_t _magic;
1124 /** What should the tor process actually do? */
1125 enum {
1126 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
1127 CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS
1128 } command;
1129 const char *command_arg; /**< Argument for command-line option. */
1131 config_line_t *OldLogOptions; /**< List of configuration lines
1132 * for logfiles, old style. */
1134 config_line_t *Logs; /**< New-style list of configuration lines
1135 * for logs */
1137 char *DebugLogFile; /**< Where to send verbose log messages. */
1138 char *DataDirectory; /**< OR only: where to store long-term data. */
1139 char *Nickname; /**< OR only: nickname of this onion router. */
1140 char *Address; /**< OR only: configured address for this onion router. */
1141 char *PidFile; /**< Where to store PID of Tor process. */
1143 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
1144 * as exits. */
1145 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
1146 * as entry points. */
1147 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
1148 * stop building circuits? */
1149 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
1150 * stop building circuits? */
1151 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
1152 * use in circuits. */
1154 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
1155 * points. */
1156 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
1157 * as introduction points. */
1159 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
1160 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
1161 config_line_t *ExitPolicy; /**< Lists of exit policy components. */
1162 config_line_t *SocksPolicy; /**< Lists of socks policy components */
1163 config_line_t *DirPolicy; /**< Lists of dir policy components */
1164 /** Addresses to bind for listening for SOCKS connections. */
1165 config_line_t *SocksBindAddress;
1166 /** Addresses to bind for listening for OR connections. */
1167 config_line_t *ORBindAddress;
1168 /** Addresses to bind for listening for directory connections. */
1169 config_line_t *DirBindAddress;
1170 /** Local address to bind outbound sockets */
1171 char *OutboundBindAddress;
1172 /** Directory server only: which versions of
1173 * Tor should we tell users to run? */
1174 config_line_t *RecommendedVersions;
1175 config_line_t *RecommendedClientVersions;
1176 config_line_t *RecommendedServerVersions;
1177 /** Whether dirservers refuse router descriptors with private IPs. */
1178 int DirAllowPrivateAddresses;
1179 char *User; /**< Name of user to run Tor as. */
1180 char *Group; /**< Name of group to run Tor as. */
1181 double PathlenCoinWeight; /**< Parameter used to configure average path
1182 * length (alpha in geometric distribution). */
1183 int ORPort; /**< Port to listen on for OR connections. */
1184 int SocksPort; /**< Port to listen on for SOCKS connections. */
1185 int ControlPort; /**< Port to listen on for control connections. */
1186 int DirPort; /**< Port to listen on for directory connections. */
1187 int AssumeReachable; /**< Whether to publish our descriptor regardless. */
1188 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
1189 int V1AuthoritativeDir; /**< Boolean: is this an authoritative directory?
1190 * for version 1 directories? */
1191 int NamingAuthoritativeDir; /**< Boolean: is this an authoritative directory
1192 * that's willing to bind names? */
1193 int VersioningAuthoritativeDir; /**< Boolean: is this an authoritative directory
1194 * that's willing to recommend versions? */
1195 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
1196 int NoPublish; /**< Boolean: should we never publish a descriptor? */
1197 int ConnLimit; /**< Requested maximum number of simultaneous connections. */
1198 int _ConnLimit; /**< Actual maximum number of simultaneous connections. */
1199 int IgnoreVersion; /**< If true, run no matter what versions of Tor the
1200 * directory recommends. */
1201 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
1202 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
1203 smartlist_t *FirewallPorts; /**< Which ports our firewall allows (strings). */
1204 config_line_t *ReachableAddresses; /**< Which IP:ports our firewall allows
1205 * (exit policy.) */
1207 /** Application ports that require all nodes in circ to have sufficient uptime. */
1208 smartlist_t *LongLivedPorts;
1209 /** Should we try to reuse the same exit node for a given host */
1210 smartlist_t *TrackHostExits;
1211 int TrackHostExitsExpire; /**< Number of seconds until we expire an addressmap */
1212 config_line_t *AddressMap; /**< List of address map directives. */
1213 int DirFetchPeriod; /**< How often do we fetch new directories? */
1214 int DirPostPeriod; /**< How often do we post our server descriptor to the
1215 * authoritative directory servers? */
1216 int RendPostPeriod; /**< How often do we post each rendezvous service
1217 * descriptor? Remember to publish them independently. */
1218 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
1219 int KeepalivePeriod; /**< How often do we send padding cells to keep
1220 * connections alive? */
1221 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
1222 * to wait simultaneously before we start dropping
1223 * them? */
1224 int NewCircuitPeriod; /**< How long do we use a circuit before building
1225 * a new one? */
1226 int MaxCircuitDirtiness; /**< Never use circs that were first used more than
1227 this interval ago. */
1228 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
1229 * use in a second? */
1230 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
1231 * use in a second? */
1232 uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
1233 * tell people we have? */
1234 int NumCpus; /**< How many CPUs should we try to use? */
1235 int RunTesting; /**< If true, create testing circuits to measure how well the
1236 * other ORs are running. */
1237 config_line_t *RendConfigLines; /**< List of configuration lines
1238 * for rendezvous services. */
1239 char *ContactInfo; /**< Contact info to be published in the directory */
1241 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
1242 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
1243 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
1244 char *HttpProxyAuthenticator; /**< username:password string, if any */
1246 char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
1247 uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
1248 uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
1249 char *HttpsProxyAuthenticator; /**< username:password string, if any */
1251 config_line_t *DirServers; /**< List of configuration lines
1252 * for directory servers. */
1253 char *MyFamily; /**< Declared family for this OR. */
1254 config_line_t *NodeFamilies; /**< List of config lines for
1255 * node families */
1256 config_line_t *RedirectExit; /**< List of config lines for simple
1257 * addr/port redirection */
1258 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
1259 config_line_t *AuthDirReject; /**< Address policy for descriptors to
1260 * reject. */
1261 config_line_t *AuthDirInvalid; /**< Address policy for descriptors to
1262 * never mark as valid. */
1263 char *AccountingStart; /**< How long is the accounting interval, and when
1264 * does it start? */
1265 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
1266 * interval before hibernation? 0 for "never
1267 * hibernate." */
1268 int _AccountingMaxKB; /**< How many KB do we allow per accounting
1269 * interval before hibernation? 0 for "never
1270 * hibernate." (Based on a deprecated option)*/
1272 char *HashedControlPassword; /**< Base64-encoded hash of a password for
1273 * the control system. */
1274 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
1275 * the control system? */
1276 int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
1277 * circuits itself (0), or does it expect a controller
1278 * to cope? (1) */
1279 int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
1280 * long do we wait before exiting? */
1281 int SafeLogging; /**< Boolean: are we allowed to log sensitive strings
1282 * such as addresses (0), or do we scrub them first (1)? */
1283 int HardwareAccel; /**< Boolean: Should we enable OpenSSL hardware
1284 * acceleration where available? */
1285 int UseHelperNodes; /**< Boolean: Do we try to enter from a smallish number
1286 * of fixed nodes? */
1287 int NumHelperNodes; /**< How many helper nodes do we try to establish? */
1288 int RephistTrackTime; /**< How many seconds do we keep rephist info? */
1290 addr_policy_t *reachable_addr_policy; /**< Parsed from ReachableAddresses */
1291 } or_options_t;
1293 /** Persistent state for an onion router, as saved to disk. */
1294 typedef struct {
1295 uint32_t _magic;
1296 int dirty;
1298 /* XXXX These options aren't actually attached to anything yet. */
1299 time_t LastWritten;
1300 time_t AccountingIntervalStart;
1301 uint64_t AccountingBytesReadInInterval;
1302 uint64_t AccountingBytesWrittenInInterval;
1303 int AccountingSecondsActive;
1304 uint64_t AccountingExpectedUsage;
1306 config_line_t *HelperNodes;
1307 } or_state_t;
1309 #define MAX_SOCKS_REPLY_LEN 1024
1310 #define MAX_SOCKS_ADDR_LEN 256
1311 #define SOCKS_COMMAND_CONNECT 0x01
1312 #define SOCKS_COMMAND_RESOLVE 0xF0
1313 /** State of a SOCKS request from a user to an OP */
1314 struct socks_request_t {
1315 char socks_version; /**< Which version of SOCKS did the client use? */
1316 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
1317 size_t replylen; /**< Length of <b>reply</b>. */
1318 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
1319 * we want to specify our own socks reply,
1320 * rather than using the default socks4 or
1321 * socks5 socks reply. We use this for the
1322 * two-stage socks5 handshake.
1324 int has_finished; /**< Has the SOCKS handshake finished? */
1325 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
1326 uint16_t port; /**< What port did the client ask to connect to? */
1329 /* all the function prototypes go here */
1331 /********************************* buffers.c ***************************/
1333 buf_t *buf_new(void);
1334 buf_t *buf_new_with_capacity(size_t size);
1335 void buf_free(buf_t *buf);
1336 void buf_clear(buf_t *buf);
1337 void buf_shrink(buf_t *buf);
1339 size_t buf_datalen(const buf_t *buf);
1340 size_t buf_capacity(const buf_t *buf);
1341 const char *_buf_peek_raw_buffer(const buf_t *buf);
1343 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1344 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
1346 int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
1347 int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
1349 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1350 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1351 int fetch_from_buf_http(buf_t *buf,
1352 char **headers_out, size_t max_headerlen,
1353 char **body_out, size_t *body_used, size_t max_bodylen);
1354 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
1355 int fetch_from_buf_control0(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
1356 char **body_out, int check_for_v1);
1357 int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
1359 void assert_buf_ok(buf_t *buf);
1361 /********************************* circuitbuild.c **********************/
1363 char *circuit_list_path(circuit_t *circ, int verbose);
1364 void circuit_log_path(int severity, circuit_t *circ);
1365 void circuit_rep_hist_note_result(circuit_t *circ);
1366 void circuit_dump_by_conn(connection_t *conn, int severity);
1367 circuit_t *circuit_init(uint8_t purpose, int need_uptime,
1368 int need_capacity, int internal);
1369 circuit_t *circuit_establish_circuit(uint8_t purpose, extend_info_t *exit,
1370 int need_uptime, int need_capacity, int internal);
1371 int circuit_handle_first_hop(circuit_t *circ);
1372 void circuit_n_conn_done(connection_t *or_conn, int status);
1373 int circuit_send_next_onion_skin(circuit_t *circ);
1374 void circuit_note_clock_jumped(int seconds_elapsed);
1375 int circuit_extend(cell_t *cell, circuit_t *circ);
1376 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
1377 int circuit_finish_handshake(circuit_t *circ, uint8_t cell_type, char *reply);
1378 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1379 int onionskin_answer(circuit_t *circ, uint8_t cell_type, char *payload, char *keys);
1380 int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
1381 int *need_capacity);
1383 int circuit_append_new_exit(circuit_t *circ, extend_info_t *info);
1384 int circuit_extend_to_new_exit(circuit_t *circ, extend_info_t *info);
1385 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1386 extend_info_t *extend_info_from_router(routerinfo_t *r);
1387 extend_info_t *extend_info_dup(extend_info_t *info);
1388 void extend_info_free(extend_info_t *info);
1389 routerinfo_t *build_state_get_exit_router(cpath_build_state_t *state);
1390 const char *build_state_get_exit_nickname(cpath_build_state_t *state);
1392 void helper_node_set_status(const char *digest, int succeeded);
1393 void helper_nodes_set_status_from_directory(void);
1394 int helper_nodes_update_state(or_state_t *state);
1395 int helper_nodes_parse_state(or_state_t *state, int set, const char **err);
1396 int helper_nodes_getinfo_helper(const char *question, char **answer);
1397 void helper_nodes_free_all(void);
1399 /********************************* circuitlist.c ***********************/
1401 circuit_t * _circuit_get_global_list(void);
1402 const char *circuit_state_to_string(int state);
1403 enum which_conn_changed_t { P_CONN_CHANGED=1, N_CONN_CHANGED=0 };
1404 void circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
1405 connection_t *conn,
1406 enum which_conn_changed_t which);
1407 void circuit_close_all_marked(void);
1408 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1409 circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn);
1410 circuit_t *circuit_get_by_edge_conn(connection_t *conn);
1411 circuit_t *circuit_get_by_conn(connection_t *conn);
1412 circuit_t *circuit_get_by_global_id(uint32_t id);
1413 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
1414 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1415 const char *digest, uint8_t purpose);
1416 circuit_t *circuit_get_rendezvous(const char *cookie);
1417 circuit_t *circuit_get_clean_open(uint8_t purpose, int need_uptime,
1418 int need_capacity, int internal);
1419 void circuit_mark_all_unused_circs(void);
1420 void circuit_expire_all_dirty_circs(void);
1421 void _circuit_mark_for_close(circuit_t *circ, int line, const char *file);
1423 #define circuit_mark_for_close(c) \
1424 _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
1426 void assert_cpath_layer_ok(const crypt_path_t *cp);
1427 void assert_circuit_ok(const circuit_t *c);
1428 void circuit_free_all(void);
1430 /********************************* circuituse.c ************************/
1432 void circuit_expire_building(time_t now);
1433 void circuit_remove_handled_ports(smartlist_t *needed_ports);
1434 int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
1435 void circuit_build_needed_circs(time_t now);
1436 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1437 void circuit_about_to_close_connection(connection_t *conn);
1438 void circuit_has_opened(circuit_t *circ);
1439 void circuit_build_failed(circuit_t *circ);
1440 circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
1441 int need_uptime, int need_capacity, int is_internal);
1442 circuit_t *circuit_launch_by_extend_info(uint8_t purpose,
1443 extend_info_t *info,
1444 int need_uptime, int need_capacity, int is_internal);
1445 circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
1446 int need_uptime, int need_capacity, int is_internal);
1447 void circuit_reset_failure_count(int timeout);
1448 int connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
1449 circuit_t *circ);
1450 int connection_ap_handshake_attach_circuit(connection_t *conn);
1452 /********************************* command.c ***************************/
1454 void command_process_cell(cell_t *cell, connection_t *conn);
1456 extern unsigned long stats_n_padding_cells_processed;
1457 extern unsigned long stats_n_create_cells_processed;
1458 extern unsigned long stats_n_created_cells_processed;
1459 extern unsigned long stats_n_relay_cells_processed;
1460 extern unsigned long stats_n_destroy_cells_processed;
1462 /********************************* config.c ***************************/
1464 or_options_t *get_options(void);
1465 int set_options(or_options_t *new_val);
1466 void config_free_all(void);
1467 const char *safe_str(const char *address);
1469 int config_get_lines(char *string, config_line_t **result);
1470 void config_free_lines(config_line_t *front);
1471 int options_trial_assign(config_line_t *list, int use_defaults, int clear_first);
1472 int resolve_my_address(or_options_t *options, uint32_t *addr,
1473 char **hostname_out);
1474 void options_init(or_options_t *options);
1475 int options_init_from_torrc(int argc, char **argv);
1476 int options_init_logs(or_options_t *options, int validate_only);
1477 int config_parse_addr_policy(config_line_t *cfg,
1478 addr_policy_t **dest,
1479 int assume_action);
1480 void options_append_default_exit_policy(addr_policy_t **policy);
1481 void addr_policy_free(addr_policy_t *p);
1482 int option_is_recognized(const char *key);
1483 const char *option_get_canonical_name(const char *key);
1484 config_line_t *option_get_assignment(or_options_t *options,
1485 const char *key);
1486 char *options_dump(or_options_t *options, int minimal);
1487 int options_save_current(void);
1488 const char *get_torrc_fname(void);
1490 or_state_t *get_or_state(void);
1491 int or_state_load(void);
1492 int or_state_save(void);
1494 int config_getinfo_helper(const char *question, char **answer);
1496 int firewall_is_fascist(void);
1497 int fascist_firewall_allows_address(uint32_t addr, uint16_t port);
1499 /********************************* connection.c ***************************/
1501 const char *conn_type_to_string(int type);
1502 const char *conn_state_to_string(int type, int state);
1504 connection_t *connection_new(int type);
1505 void connection_unregister(connection_t *conn);
1506 void connection_free(connection_t *conn);
1507 void connection_free_all(void);
1508 void connection_about_to_close_connection(connection_t *conn);
1509 void connection_close_immediate(connection_t *conn);
1510 void _connection_mark_for_close(connection_t *conn,int line, const char *file);
1512 #define connection_mark_for_close(c) \
1513 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1515 void connection_expire_held_open(void);
1517 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
1518 int retry_all_listeners(int force, smartlist_t *replaced_conns,
1519 smartlist_t *new_conns);
1521 void connection_bucket_init(void);
1522 void connection_bucket_refill(struct timeval *now);
1524 int connection_handle_read(connection_t *conn);
1526 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1528 int connection_wants_to_flush(connection_t *conn);
1529 int connection_outbuf_too_full(connection_t *conn);
1530 int connection_handle_write(connection_t *conn);
1531 void _connection_controller_force_write(connection_t *conn);
1532 void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
1534 connection_t *connection_or_exact_get_by_addr_port(uint32_t addr, uint16_t port);
1535 connection_t *connection_get_by_identity_digest(const char *digest, int type);
1536 connection_t *connection_get_by_global_id(uint32_t id);
1538 connection_t *connection_get_by_type(int type);
1539 connection_t *connection_get_by_type_purpose(int type, int purpose);
1540 connection_t *connection_get_by_type_addr_port_purpose(int type, uint32_t addr,
1541 uint16_t port, int purpose);
1542 connection_t *connection_get_by_type_state(int type, int state);
1543 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1544 connection_t *connection_get_by_type_state_rendquery(int type, int state, const char *rendquery);
1546 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1547 #define connection_has_pending_tls_data(conn) \
1548 ((conn)->type == CONN_TYPE_OR && \
1549 (conn)->state == OR_CONN_STATE_OPEN && \
1550 tor_tls_get_pending_bytes((conn)->tls))
1551 int connection_is_listener(connection_t *conn);
1552 int connection_state_is_open(connection_t *conn);
1553 int connection_state_is_connecting(connection_t *conn);
1555 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
1556 char *alloc_http_authenticator(const char *authenticator);
1558 void assert_connection_ok(connection_t *conn, time_t now);
1559 int connection_or_nonopen_was_started_here(connection_t *conn);
1561 /********************************* connection_edge.c ***************************/
1563 #define connection_mark_unattached_ap(conn, endreason) \
1564 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1566 void _connection_mark_unattached_ap(connection_t *conn, int endreason,
1567 int line, const char *file);
1568 int connection_edge_reached_eof(connection_t *conn);
1569 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1570 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1571 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
1572 int connection_edge_end_errno(connection_t *conn, crypt_path_t *cpath_layer);
1573 int connection_edge_finished_flushing(connection_t *conn);
1574 int connection_edge_finished_connecting(connection_t *conn);
1576 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1577 int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
1579 int connection_ap_make_bridge(char *address, uint16_t port);
1580 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1581 size_t replylen,
1582 socks5_reply_status_t status);
1583 void connection_ap_handshake_socks_resolved(connection_t *conn,
1584 int answer_type,
1585 size_t answer_len,
1586 const char *answer,
1587 int ttl);
1589 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1590 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1591 void connection_exit_connect(connection_t *conn);
1592 int connection_edge_is_rendezvous_stream(connection_t *conn);
1593 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1594 void connection_ap_expire_beginning(void);
1595 void connection_ap_attach_pending(void);
1596 int connection_ap_detach_retriable(connection_t *conn, circuit_t *circ);
1598 void addressmap_init(void);
1599 void addressmap_clean(time_t now);
1600 void addressmap_clear_configured(void);
1601 void addressmap_clear_transient(void);
1602 void addressmap_free_all(void);
1603 void addressmap_rewrite(char *address, size_t maxlen);
1604 int addressmap_already_mapped(const char *address);
1605 void addressmap_register(const char *address, char *new_address, time_t expires);
1606 int client_dns_incr_failures(const char *address);
1607 void client_dns_clear_failures(const char *address);
1608 void client_dns_set_addressmap(const char *address, uint32_t val, const char *exitname, int ttl);
1609 int address_is_in_virtual_range(const char *addr);
1610 const char *addressmap_register_virtual_address(int type, char *new_address);
1611 void addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires);
1613 void parse_socks_policy(void);
1614 void free_socks_policy(void);
1615 int socks_policy_permits_address(uint32_t addr);
1617 void set_exit_redirects(smartlist_t *lst);
1618 typedef enum hostname_type_t {
1619 NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME, BAD_HOSTNAME
1620 } hostname_type_t;
1621 hostname_type_t parse_extended_hostname(char *address);
1623 /********************************* connection_or.c ***************************/
1625 int connection_or_reached_eof(connection_t *conn);
1626 int connection_or_process_inbuf(connection_t *conn);
1627 int connection_or_finished_flushing(connection_t *conn);
1628 int connection_or_finished_connecting(connection_t *conn);
1630 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1631 const char *id_digest);
1633 int connection_tls_start_handshake(connection_t *conn, int receiving);
1634 int connection_tls_continue_handshake(connection_t *conn);
1636 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1637 void connection_or_update_nickname(connection_t *conn);
1639 /********************************* control.c ***************************/
1641 typedef enum circuit_status_event_t {
1642 CIRC_EVENT_LAUNCHED = 0,
1643 CIRC_EVENT_BUILT = 1,
1644 CIRC_EVENT_EXTENDED = 2,
1645 CIRC_EVENT_FAILED = 3,
1646 CIRC_EVENT_CLOSED = 4,
1647 } circuit_status_event_t;
1649 typedef enum stream_status_event_t {
1650 STREAM_EVENT_SENT_CONNECT = 0,
1651 STREAM_EVENT_SENT_RESOLVE = 1,
1652 STREAM_EVENT_SUCCEEDED = 2,
1653 STREAM_EVENT_FAILED = 3,
1654 STREAM_EVENT_CLOSED = 4,
1655 STREAM_EVENT_NEW = 5,
1656 STREAM_EVENT_NEW_RESOLVE = 6,
1657 STREAM_EVENT_FAILED_RETRIABLE = 7
1658 } stream_status_event_t;
1660 typedef enum or_conn_status_event_t {
1661 OR_CONN_EVENT_LAUNCHED = 0,
1662 OR_CONN_EVENT_CONNECTED = 1,
1663 OR_CONN_EVENT_FAILED = 2,
1664 OR_CONN_EVENT_CLOSED = 3,
1665 } or_conn_status_event_t;
1667 void control_update_global_event_mask(void);
1668 void control_adjust_event_log_severity(void);
1669 void disable_control_logging(void);
1670 void enable_control_logging(void);
1672 /** Execute the statement <b>stmt</b>, which may log events concerning the
1673 * connection <b>conn</b>. To prevent infinite loops, disable log messages
1674 * being stent to controllers if <b>conn</b> is a control connection.
1676 * Stmt must not contain any return or goto statements.
1678 #define CONN_LOG_PROTECT(conn, stmt) \
1679 do { \
1680 int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
1681 if (_log_conn_is_control) \
1682 disable_control_logging(); \
1683 do {stmt;} while (0); \
1684 if (_log_conn_is_control) \
1685 enable_control_logging(); \
1686 } while (0)
1688 /** Log information about the connection <b>conn</b>, protecting it as with
1689 * CONN_LOG_PROTECT. Example:
1691 * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
1693 #define LOG_FN_CONN(conn, args) \
1694 CONN_LOG_PROTECT(conn, log_fn args)
1696 int connection_control_finished_flushing(connection_t *conn);
1697 int connection_control_reached_eof(connection_t *conn);
1698 int connection_control_process_inbuf(connection_t *conn);
1700 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1701 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1702 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1703 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1704 void control_event_logmsg(int severity, const char *msg);
1705 int control_event_descriptors_changed(smartlist_t *routers);
1706 int control_event_address_mapped(const char *from, const char *to,time_t expires);
1708 int init_cookie_authentication(int enabled);
1709 int decode_hashed_password(char *buf, const char *hashed);
1711 /********************************* cpuworker.c *****************************/
1713 void cpu_init(void);
1714 void cpuworkers_rotate(void);
1715 int connection_cpu_finished_flushing(connection_t *conn);
1716 int connection_cpu_reached_eof(connection_t *conn);
1717 int connection_cpu_process_inbuf(connection_t *conn);
1718 int assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
1719 void *task);
1721 /********************************* directory.c ***************************/
1723 int dir_policy_permits_address(uint32_t addr);
1724 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1725 size_t payload_len);
1726 void directory_get_from_dirserver(uint8_t purpose, const char *resource,
1727 int retry_if_no_servers);
1728 void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
1729 int private_connection, const char *resource,
1730 const char *payload, size_t payload_len);
1732 int parse_http_response(const char *headers, int *code, time_t *date,
1733 int *compression, char **response);
1735 int connection_dir_reached_eof(connection_t *conn);
1736 int connection_dir_process_inbuf(connection_t *conn);
1737 int connection_dir_finished_flushing(connection_t *conn);
1738 int connection_dir_finished_connecting(connection_t *conn);
1739 void connection_dir_request_failed(connection_t *conn);
1740 void parse_dir_policy(void);
1741 void free_dir_policy(void);
1742 int dir_split_resource_into_fingerprints(const char *resource,
1743 smartlist_t *fp_out, int *compresseed_out);
1745 /********************************* dirserv.c ***************************/
1747 void parse_authdir_policy(void);
1748 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1749 int dirserv_parse_fingerprint_file(const char *fname);
1750 void dirserv_free_fingerprint_list(void);
1751 const char *dirserv_get_nickname_by_digest(const char *digest);
1752 int dirserv_add_descriptor(const char *desc, const char **msg);
1753 char *dirserver_getinfo_unregistered(const char *question);
1754 void dirserv_free_descriptors(void);
1755 int dirserv_thinks_router_is_blatantly_unreachable(routerinfo_t *router,
1756 time_t now);
1757 int list_server_status(smartlist_t *routers, char **router_status_out);
1758 int dirserv_dump_directory_to_string(char **dir_out,
1759 crypto_pk_env_t *private_key);
1760 void directory_set_dirty(void);
1761 size_t dirserv_get_directory(const char **cp, int compress);
1762 size_t dirserv_get_runningrouters(const char **rr, int compress);
1763 void dirserv_set_cached_directory(const char *directory, time_t when,
1764 int is_running_routers);
1765 void dirserv_set_cached_networkstatus_v2(const char *directory, const char *fp,
1766 time_t published);
1767 int dirserv_get_networkstatus_v2(smartlist_t *result, const char *key);
1768 void dirserv_get_routerdescs(smartlist_t *descs_out, const char *key);
1769 void dirserv_orconn_tls_done(const char *address,
1770 uint16_t or_port,
1771 const char *digest_rcvd,
1772 const char *nickname,
1773 int as_advertised);
1774 int authdir_wants_to_reject_router(routerinfo_t *ri,
1775 const char **msg);
1776 void dirserv_free_all(void);
1778 /********************************* dns.c ***************************/
1780 void dns_init(void);
1781 void dns_free_all(void);
1782 int connection_dns_finished_flushing(connection_t *conn);
1783 int connection_dns_reached_eof(connection_t *conn);
1784 int connection_dns_process_inbuf(connection_t *conn);
1785 void dnsworkers_rotate(void);
1786 void connection_dns_remove(connection_t *conn);
1787 void assert_connection_edge_not_dns_pending(connection_t *conn);
1788 void assert_all_pending_dns_resolves_ok(void);
1789 void dns_cancel_pending_resolve(char *question);
1790 int dns_resolve(connection_t *exitconn);
1792 /********************************* hibernate.c **********************/
1794 int accounting_parse_options(or_options_t *options, int validate_only);
1795 int accounting_is_enabled(or_options_t *options);
1796 void configure_accounting(time_t now);
1797 void accounting_run_housekeeping(time_t now);
1798 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1799 int accounting_record_bandwidth_usage(time_t now);
1800 void hibernate_begin_shutdown(void);
1801 int we_are_hibernating(void);
1802 void consider_hibernation(time_t now);
1803 int accounting_getinfo_helper(const char *question, char **answer);
1804 void accounting_set_bandwidth_usage_from_state(or_state_t *state);
1806 /********************************* main.c ***************************/
1808 int connection_add(connection_t *conn);
1809 int connection_remove(connection_t *conn);
1810 int connection_in_array(connection_t *conn);
1811 void add_connection_to_closeable_list(connection_t *conn);
1812 int connection_is_on_closeable_list(connection_t *conn);
1814 void get_connection_array(connection_t ***array, int *n);
1816 void connection_watch_events(connection_t *conn, short events);
1817 int connection_is_reading(connection_t *conn);
1818 void connection_stop_reading(connection_t *conn);
1819 void connection_start_reading(connection_t *conn);
1821 int connection_is_writing(connection_t *conn);
1822 void connection_stop_writing(connection_t *conn);
1823 void connection_start_writing(connection_t *conn);
1825 void directory_all_unreachable(time_t now);
1826 void directory_info_has_arrived(time_t now, int from_cache);
1828 int control_signal_act(int the_signal);
1829 void handle_signals(int is_parent);
1830 void tor_cleanup(void);
1831 void tor_free_all(int postfork);
1833 int tor_main(int argc, char *argv[]);
1835 /********************************* onion.c ***************************/
1837 int onion_pending_add(circuit_t *circ);
1838 circuit_t *onion_next_task(void);
1839 void onion_pending_remove(circuit_t *circ);
1841 int onion_skin_create(crypto_pk_env_t *router_key,
1842 crypto_dh_env_t **handshake_state_out,
1843 char *onion_skin_out);
1845 int onion_skin_server_handshake(const char *onion_skin,
1846 crypto_pk_env_t *private_key,
1847 crypto_pk_env_t *prev_private_key,
1848 char *handshake_reply_out,
1849 char *key_out,
1850 size_t key_out_len);
1852 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
1853 const char *handshake_reply,
1854 char *key_out,
1855 size_t key_out_len);
1857 int fast_server_handshake(const char *key_in,
1858 char *handshake_reply_out,
1859 char *key_out,
1860 size_t key_out_len);
1862 int fast_client_handshake(const char *handshake_state,
1863 const char *handshake_reply_out,
1864 char *key_out,
1865 size_t key_out_len);
1867 void clear_pending_onions(void);
1869 /********************************* relay.c ***************************/
1871 extern unsigned long stats_n_relay_cells_relayed;
1872 extern unsigned long stats_n_relay_cells_delivered;
1874 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
1875 int cell_direction);
1877 void relay_header_pack(char *dest, const relay_header_t *src);
1878 void relay_header_unpack(relay_header_t *dest, const char *src);
1879 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
1880 int relay_command, const char *payload,
1881 size_t payload_len, crypt_path_t *cpath_layer);
1882 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
1883 void connection_edge_consider_sending_sendme(connection_t *conn);
1884 socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
1885 int errno_to_end_reason(int e);
1887 extern uint64_t stats_n_data_cells_packaged;
1888 extern uint64_t stats_n_data_bytes_packaged;
1889 extern uint64_t stats_n_data_cells_received;
1890 extern uint64_t stats_n_data_bytes_received;
1892 /********************************* rephist.c ***************************/
1894 void rep_hist_init(void);
1895 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1896 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1897 void rep_hist_note_disconnect(const char* nickname, time_t when);
1898 void rep_hist_note_connection_died(const char* nickname, time_t when);
1899 void rep_hist_note_extend_succeeded(const char *from_name,
1900 const char *to_name);
1901 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1902 void rep_hist_dump_stats(time_t now, int severity);
1903 void rep_hist_note_bytes_read(int num_bytes, time_t when);
1904 void rep_hist_note_bytes_written(int num_bytes, time_t when);
1905 int rep_hist_bandwidth_assess(void);
1906 char *rep_hist_get_bandwidth_lines(void);
1907 void rep_history_clean(time_t before);
1909 void rep_hist_note_used_port(uint16_t port, time_t now);
1910 smartlist_t *rep_hist_get_predicted_ports(time_t now);
1911 void rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity);
1912 int rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity);
1913 void rep_hist_note_used_resolve(time_t now);
1914 int rep_hist_get_predicted_resolve(time_t now);
1916 void rep_hist_free_all(void);
1918 /********************************* rendclient.c ***************************/
1920 void rend_client_introcirc_has_opened(circuit_t *circ);
1921 void rend_client_rendcirc_has_opened(circuit_t *circ);
1922 int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
1923 void rend_client_refetch_renddesc(const char *query);
1924 int rend_client_remove_intro_point(extend_info_t *failed_intro, const char *query);
1925 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
1926 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1927 void rend_client_desc_here(const char *query);
1929 extend_info_t *rend_client_get_random_intro(const char *query);
1931 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1933 /********************************* rendcommon.c ***************************/
1935 /** Information used to connect to a hidden service. */
1936 typedef struct rend_service_descriptor_t {
1937 crypto_pk_env_t *pk; /**< This service's public key. */
1938 int version; /**< 0 or 1. */
1939 time_t timestamp; /**< Time when the descriptor was generated. */
1940 uint16_t protocols; /**< Bitmask: which rendezvous protocols are supported?
1941 * (We allow bits '0', '1', and '2' to be set.) */
1942 int n_intro_points; /**< Number of introduction points. */
1943 /** Array of n_intro_points elements for this service's introduction points'
1944 * nicknames. Elements are removed from this array if introduction attempts
1945 * fail. */
1946 char **intro_points;
1947 /** Array of n_intro_points elements for this service's introduction points'
1948 * extend_infos, or NULL if this descriptor is V0. Elements are removed
1949 * from this array if introduction attempts fail. If this array is present,
1950 * its elements correspond to the elements of intro_points. */
1951 extend_info_t **intro_point_extend_info;
1952 } rend_service_descriptor_t;
1954 int rend_cmp_service_ids(const char *one, const char *two);
1956 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
1957 const char *payload);
1959 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1960 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1961 int version,
1962 crypto_pk_env_t *key,
1963 char **str_out,
1964 size_t *len_out);
1965 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
1966 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1968 typedef struct rend_cache_entry_t {
1969 size_t len; /* Length of desc */
1970 time_t received; /* When was the descriptor received? */
1971 char *desc; /* Service descriptor */
1972 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1973 } rend_cache_entry_t;
1975 void rend_cache_init(void);
1976 void rend_cache_clean(void);
1977 void rend_cache_free_all(void);
1978 int rend_valid_service_id(const char *query);
1979 int rend_cache_lookup_desc(const char *query, int version, const char **desc, size_t *desc_len);
1980 int rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **entry_out);
1981 int rend_cache_store(const char *desc, size_t desc_len);
1983 /********************************* rendservice.c ***************************/
1985 int num_rend_services(void);
1986 int rend_config_services(or_options_t *options, int validate_only);
1987 int rend_service_load_keys(void);
1988 void rend_services_init(void);
1989 void rend_services_introduce(void);
1990 void rend_consider_services_upload(time_t now);
1992 void rend_service_intro_has_opened(circuit_t *circuit);
1993 int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
1994 void rend_service_rendezvous_has_opened(circuit_t *circuit);
1995 int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
1996 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1997 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1998 void rend_service_dump_stats(int severity);
1999 void rend_service_free_all(void);
2001 /********************************* rendmid.c *******************************/
2002 int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
2003 int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
2004 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
2005 int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
2007 /********************************* router.c ***************************/
2008 typedef enum {
2009 ADDR_POLICY_ACCEPTED=0,
2010 ADDR_POLICY_REJECTED=-1,
2011 ADDR_POLICY_PROBABLY_ACCEPTED=1,
2012 ADDR_POLICY_PROBABLY_REJECTED=2
2013 } addr_policy_result_t;
2015 void set_onion_key(crypto_pk_env_t *k);
2016 crypto_pk_env_t *get_onion_key(void);
2017 crypto_pk_env_t *get_previous_onion_key(void);
2018 time_t get_onion_key_set_at(void);
2019 void set_identity_key(crypto_pk_env_t *k);
2020 crypto_pk_env_t *get_identity_key(void);
2021 int identity_key_is_set(void);
2022 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
2023 void rotate_onion_key(void);
2024 crypto_pk_env_t *init_key_from_file(const char *fname);
2025 int init_keys(void);
2027 int check_whether_orport_reachable(void);
2028 int check_whether_dirport_reachable(void);
2029 void consider_testing_reachability(void);
2030 void router_orport_found_reachable(void);
2031 void router_dirport_found_reachable(void);
2032 void server_has_changed_ip(void);
2033 void consider_publishable_server(time_t now, int force);
2035 int authdir_mode(or_options_t *options);
2036 int clique_mode(or_options_t *options);
2037 int server_mode(or_options_t *options);
2038 int advertised_server_mode(void);
2039 int proxy_mode(or_options_t *options);
2041 void router_retry_connections(int force);
2042 int router_is_clique_mode(routerinfo_t *router);
2043 void router_upload_dir_desc_to_dirservers(int force);
2044 void mark_my_descriptor_dirty_if_older_than(time_t when);
2045 void mark_my_descriptor_dirty(void);
2046 void check_descriptor_bandwidth_changed(time_t now);
2047 int router_compare_to_my_exit_policy(connection_t *conn);
2048 routerinfo_t *router_get_my_routerinfo(void);
2049 const char *router_get_my_descriptor(void);
2050 int router_digest_is_me(const char *digest);
2051 int router_is_me(routerinfo_t *router);
2052 int router_fingerprint_is_me(const char *fp);
2053 int router_rebuild_descriptor(int force);
2054 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
2055 crypto_pk_env_t *ident_key);
2056 int is_legal_nickname(const char *s);
2057 int is_legal_nickname_or_hexdigest(const char *s);
2058 void router_reset_warnings(void);
2059 void router_free_all(void);
2061 /********************************* routerlist.c ***************************/
2063 /** Represents information about a single trusted directory server. */
2064 typedef struct trusted_dir_server_t {
2065 char *description;
2066 char *nickname;
2067 char *address; /**< Hostname */
2068 uint32_t addr; /**< IPv4 address */
2069 uint16_t dir_port; /**< Directory port */
2070 char digest[DIGEST_LEN]; /**< Digest of identity key */
2071 unsigned int is_running:1; /**< True iff we think this server is running. */
2072 unsigned int supports_v1_protocol:1; /**< True iff this server is an
2073 * authority for the older ("v1")
2074 * directory protocol.*/
2075 int n_networkstatus_failures; /**< How many times have we asked for this
2076 * server's network-status unsuccessfully? */
2077 } trusted_dir_server_t;
2079 int router_reload_router_list(void);
2080 int router_reload_networkstatus(void);
2081 void router_get_trusted_dir_servers(smartlist_t **outp);
2082 routerinfo_t *router_pick_directory_server(int requireother,
2083 int fascistfirewall,
2084 int for_v2_directory,
2085 int retry_if_no_servers);
2086 trusted_dir_server_t *router_pick_trusteddirserver(int need_v1_support,
2087 int requireother,
2088 int fascistfirewall,
2089 int retry_if_no_servers);
2090 trusted_dir_server_t *router_get_trusteddirserver_by_digest(
2091 const char *digest);
2092 int all_trusted_directory_servers_down(void);
2093 void routerlist_add_family(smartlist_t *sl, routerinfo_t *router);
2094 void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down, int warn_if_unnamed);
2095 routerinfo_t *routerlist_find_my_routerinfo(void);
2096 int exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
2097 int warn);
2099 /** How many seconds a router must be up before we'll use it for
2100 * reliability-critical node positions.
2102 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
2103 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
2105 routerinfo_t *router_find_exact_exit_enclave(const char *address, uint16_t port);
2106 int router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity);
2107 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
2108 routerinfo_t *router_choose_random_node(const char *preferred,
2109 const char *excluded,
2110 smartlist_t *excludedsmartlist,
2111 int need_uptime, int need_bandwidth,
2112 int allow_unverified, int strict);
2113 routerinfo_t *router_get_by_nickname(const char *nickname,
2114 int warn_if_unnamed);
2115 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
2116 routerinfo_t *router_get_by_digest(const char *digest);
2117 int router_digest_is_trusted_dir(const char *digest);
2118 void router_get_routerlist(routerlist_t **prouterlist);
2119 void routerlist_reset_warnings(void);
2120 void routerlist_free(routerlist_t *routerlist);
2121 void routerinfo_free(routerinfo_t *router);
2122 void routerstatus_free(routerstatus_t *routerstatus);
2123 void networkstatus_free(networkstatus_t *networkstatus);
2124 void routerlist_free_all(void);
2125 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
2126 void router_mark_as_down(const char *digest);
2127 void routerlist_remove_old_routers(int age);
2128 void networkstatus_list_clean(time_t now);
2129 int router_add_to_routerlist(routerinfo_t *router, const char **msg,
2130 int from_cache);
2131 int router_load_single_router(const char *s, const char **msg);
2132 void router_load_routers_from_string(const char *s, int from_cache,
2133 smartlist_t *requested_fingerprints);
2134 typedef enum { NS_FROM_CACHE, NS_FROM_DIR, NS_GENERATED} networkstatus_source_t;
2135 int router_set_networkstatus(const char *s, time_t arrived_at,
2136 networkstatus_source_t source,
2137 smartlist_t *requested_fingerprints);
2138 addr_policy_result_t router_compare_addr_to_addr_policy(uint32_t addr,
2139 uint16_t port, addr_policy_t *policy);
2141 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
2142 int need_uptime);
2144 int router_exit_policy_rejects_all(routerinfo_t *router);
2145 void add_trusted_dir_server(const char *nickname,
2146 const char *address, uint16_t port,
2147 const char *digest, int supports_v1);
2148 void clear_trusted_dir_servers(void);
2149 networkstatus_t *networkstatus_get_by_digest(const char *digest);
2150 local_routerstatus_t *router_get_combined_status_by_digest(const char *digest);
2151 void update_networkstatus_downloads(time_t now);
2152 void update_router_descriptor_downloads(time_t now);
2153 void routers_update_all_from_networkstatus(void);
2154 void routers_update_status_from_networkstatus(smartlist_t *routers,
2155 int reset_failures);
2156 smartlist_t *router_list_superseded(void);
2157 int router_have_minimum_dir_info(void);
2158 void networkstatus_list_update_recent(time_t now);
2159 void router_reset_descriptor_download_failures(void);
2160 void router_reset_status_download_failures(void);
2162 /********************************* routerparse.c ************************/
2164 #define MAX_STATUS_TAG_LEN 32
2165 /** Structure to hold parsed Tor versions. This is a little messier
2166 * than we would like it to be, because we changed version schemes with 0.1.0.
2168 * See version-spec.txt for the whole business.
2170 typedef struct tor_version_t {
2171 int major;
2172 int minor;
2173 int micro;
2174 /** Release status. For version in the post-0.1 format, this is always
2175 * VER_RELEASE. */
2176 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
2177 int patchlevel;
2178 /** CVS status. For version in the post-0.1 format, this is always
2179 * IS_NOT_CVS */
2180 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
2181 char status_tag[MAX_STATUS_TAG_LEN];
2182 } tor_version_t;
2184 typedef enum version_status_t {
2185 VS_RECOMMENDED=0, /**< This version is listed as recommended. */
2186 VS_OLD=1, /**< This version is older than any recommended version. */
2187 VS_NEW=2, /**< This version is newer than any recommended version. */
2188 VS_NEW_IN_SERIES=3, /**< This version is newer than any recommended version
2189 * in its series, and such recommended versions exist. */
2190 VS_UNRECOMMENDED=4 /**< This version is not recommended (general case) */
2191 } version_status_t;
2193 int router_get_router_hash(const char *s, char *digest);
2194 int router_get_dir_hash(const char *s, char *digest);
2195 int router_get_runningrouters_hash(const char *s, char *digest);
2196 int router_get_networkstatus_v2_hash(const char *s, char *digest);
2197 int router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest,
2198 crypto_pk_env_t *private_key);
2199 int router_parse_list_from_string(const char **s,
2200 smartlist_t *dest);
2201 int router_parse_routerlist_from_directory(const char *s,
2202 routerlist_t **dest,
2203 crypto_pk_env_t *pkey,
2204 int check_version,
2205 int write_to_cache);
2206 int router_parse_runningrouters(const char *str);
2207 int router_parse_directory(const char *str);
2208 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
2209 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
2210 addr_policy_t *router_parse_addr_policy_from_string(const char *s,
2211 int assume_action);
2212 version_status_t tor_version_is_obsolete(const char *myversion,
2213 const char *versionlist);
2214 version_status_t version_status_join(version_status_t a, version_status_t b);
2215 int tor_version_parse(const char *s, tor_version_t *out);
2216 int tor_version_as_new_as(const char *platform, const char *cutoff);
2217 int tor_version_compare(tor_version_t *a, tor_version_t *b);
2218 void assert_addr_policy_ok(addr_policy_t *t);
2219 void sort_routerstatus_entries(smartlist_t *sl);
2221 networkstatus_t *networkstatus_parse_from_string(const char *s);
2223 #endif