Add some "to-be-safe" escaped() wrappers to log statements in rend*.c, though I am...
[tor.git] / src / or / or.h
blob5b0f576aa440c285cc21e47daa638b13aaac24f5
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, 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
192 * from the router list? In seconds. */
193 #define ROUTER_MAX_AGE (60*60*24)
194 /** How old do we let a saved descriptor get before removing it? */
195 #define OLD_ROUTER_DESC_MAX_AGE (60*60*48)
197 typedef enum {
198 CIRC_ID_TYPE_LOWER=0,
199 CIRC_ID_TYPE_HIGHER=1
200 } circ_id_type_t;
202 #define _CONN_TYPE_MIN 3
203 /** Type for sockets listening for OR connections. */
204 #define CONN_TYPE_OR_LISTENER 3
205 /** A bidirectional TLS connection transmitting a sequence of cells.
206 * May be from an OR to an OR, or from an OP to an OR. */
207 #define CONN_TYPE_OR 4
208 /** A TCP connection from an onion router to a stream's destination. */
209 #define CONN_TYPE_EXIT 5
210 /** Type for sockets listening for SOCKS connections. */
211 #define CONN_TYPE_AP_LISTENER 6
212 /** A SOCKS proxy connection from the user application to the onion
213 * proxy. */
214 #define CONN_TYPE_AP 7
215 /** Type for sockets listening for HTTP connections to the directory server. */
216 #define CONN_TYPE_DIR_LISTENER 8
217 /** Type for HTTP connections to the directory server. */
218 #define CONN_TYPE_DIR 9
219 /** Connection from the main process to a DNS worker process. */
220 #define CONN_TYPE_DNSWORKER 10
221 /** Connection from the main process to a CPU worker process. */
222 #define CONN_TYPE_CPUWORKER 11
223 /** Type for listenting for connections from user interface process */
224 #define CONN_TYPE_CONTROL_LISTENER 12
225 /** Type for connections from user interface process */
226 #define CONN_TYPE_CONTROL 13
227 #define _CONN_TYPE_MAX 13
229 #define CONN_IS_EDGE(x) \
230 ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
232 /** State for any listener connection. */
233 #define LISTENER_STATE_READY 0
235 #define _DNSWORKER_STATE_MIN 1
236 /** State for a connection to a dnsworker process that's idle. */
237 #define DNSWORKER_STATE_IDLE 1
238 /** State for a connection to a dnsworker process that's resolving a
239 * hostname. */
240 #define DNSWORKER_STATE_BUSY 2
241 #define _DNSWORKER_STATE_MAX 2
243 #define _CPUWORKER_STATE_MIN 1
244 /** State for a connection to a cpuworker process that's idle. */
245 #define CPUWORKER_STATE_IDLE 1
246 /** State for a connection to a cpuworker process that's processing a
247 * handshake. */
248 #define CPUWORKER_STATE_BUSY_ONION 2
249 #define _CPUWORKER_STATE_MAX 2
251 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
253 #define _OR_CONN_STATE_MIN 1
254 /** State for a connection to an OR: waiting for connect() to finish. */
255 #define OR_CONN_STATE_CONNECTING 1
256 /** State for a connection to an OR: waiting for proxy command to flush. */
257 #define OR_CONN_STATE_PROXY_FLUSHING 2
258 /** State for a connection to an OR: waiting for proxy response. */
259 #define OR_CONN_STATE_PROXY_READING 3
260 /** State for a connection to an OR: SSL is handshaking, not done yet. */
261 #define OR_CONN_STATE_HANDSHAKING 4
262 /** State for a connection to an OR: Ready to send/receive cells. */
263 #define OR_CONN_STATE_OPEN 5
264 #define _OR_CONN_STATE_MAX 5
266 #define _EXIT_CONN_STATE_MIN 1
267 /** State for an exit connection: waiting for response from dns farm. */
268 #define EXIT_CONN_STATE_RESOLVING 1
269 /** State for an exit connection: waiting for connect() to finish. */
270 #define EXIT_CONN_STATE_CONNECTING 2
271 /** State for an exit connection: open and ready to transmit data. */
272 #define EXIT_CONN_STATE_OPEN 3
273 /** State for an exit connection: waiting to be removed. */
274 #define EXIT_CONN_STATE_RESOLVEFAILED 4
275 #define _EXIT_CONN_STATE_MAX 4
277 /* the AP state values must be disjoint from the EXIT state values */
278 #define _AP_CONN_STATE_MIN 5
279 /** State for a SOCKS connection: waiting for SOCKS request. */
280 #define AP_CONN_STATE_SOCKS_WAIT 5
281 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
282 * rendezvous rescriptor. */
283 #define AP_CONN_STATE_RENDDESC_WAIT 6
284 /** The controller will attach this connection to a circuit; it isn't our
285 * job to do so. */
286 #define AP_CONN_STATE_CONTROLLER_WAIT 7
287 /** State for a SOCKS connection: waiting for a completed circuit. */
288 #define AP_CONN_STATE_CIRCUIT_WAIT 8
289 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
290 #define AP_CONN_STATE_CONNECT_WAIT 9
291 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
292 #define AP_CONN_STATE_RESOLVE_WAIT 10
293 /** State for a SOCKS connection: ready to send and receive. */
294 #define AP_CONN_STATE_OPEN 11
295 #define _AP_CONN_STATE_MAX 11
297 #define _DIR_CONN_STATE_MIN 1
298 /** State for connection to directory server: waiting for connect(). */
299 #define DIR_CONN_STATE_CONNECTING 1
300 /** State for connection to directory server: sending HTTP request. */
301 #define DIR_CONN_STATE_CLIENT_SENDING 2
302 /** State for connection to directory server: reading HTTP response. */
303 #define DIR_CONN_STATE_CLIENT_READING 3
304 /** State for connection to directory server: happy and finished. */
305 #define DIR_CONN_STATE_CLIENT_FINISHED 4
306 /** State for connection at directory server: waiting for HTTP request. */
307 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
308 /** State for connection at directory server: sending HTTP response. */
309 #define DIR_CONN_STATE_SERVER_WRITING 6
310 #define _DIR_CONN_STATE_MAX 6
312 #define _CONTROL_CONN_STATE_MIN 1
313 #define CONTROL_CONN_STATE_OPEN_V0 1
314 #define CONTROL_CONN_STATE_OPEN_V1 2
315 #define CONTROL_CONN_STATE_NEEDAUTH_V0 3
316 #define CONTROL_CONN_STATE_NEEDAUTH_V1 4
317 #define _CONTROL_CONN_STATE_MAX 4
319 #define _DIR_PURPOSE_MIN 1
320 /** A connection to a directory server: download a directory. */
321 #define DIR_PURPOSE_FETCH_DIR 1
322 /** A connection to a directory server: download just the list
323 * of running routers. */
324 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
325 /** A connection to a directory server: download a rendezvous
326 * descriptor. */
327 #define DIR_PURPOSE_FETCH_RENDDESC 3
328 /** A connection to a directory server: set after a rendezvous
329 * descriptor is downloaded. */
330 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
331 /** A connection to a directory server: download one or more network-status
332 * objects */
333 #define DIR_PURPOSE_FETCH_NETWORKSTATUS 5
334 /** A connection to a directory server: download one or more server
335 * descriptors. */
336 #define DIR_PURPOSE_FETCH_SERVERDESC 6
337 /** A connection to a directory server: upload a server descriptor. */
338 #define DIR_PURPOSE_UPLOAD_DIR 7
339 /** A connection to a directory server: upload a rendezvous
340 * descriptor. */
341 #define DIR_PURPOSE_UPLOAD_RENDDESC 8
342 /** Purpose for connection at a directory server. */
343 #define DIR_PURPOSE_SERVER 9
344 #define _DIR_PURPOSE_MAX 9
346 #define _EXIT_PURPOSE_MIN 1
347 #define EXIT_PURPOSE_CONNECT 1
348 #define EXIT_PURPOSE_RESOLVE 2
349 #define _EXIT_PURPOSE_MAX 2
351 /** Circuit state: I'm the origin, still haven't done all my handshakes. */
352 #define CIRCUIT_STATE_BUILDING 0
353 /** Circuit state: Waiting to process the onionskin. */
354 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
355 /** Circuit state: I'd like to deliver a create, but my n_conn is still
356 * connecting. */
357 #define CIRCUIT_STATE_OR_WAIT 2
358 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
359 #define CIRCUIT_STATE_OPEN 3
361 #define _CIRCUIT_PURPOSE_MIN 1
363 /* these circuits were initiated elsewhere */
364 #define _CIRCUIT_PURPOSE_OR_MIN 1
365 /** OR-side circuit purpose: normal circuit, at OR. */
366 #define CIRCUIT_PURPOSE_OR 1
367 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
368 #define CIRCUIT_PURPOSE_INTRO_POINT 2
369 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
370 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
371 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
372 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
373 #define _CIRCUIT_PURPOSE_OR_MAX 4
375 /* these circuits originate at this node */
377 /* here's how circ client-side purposes work:
378 * normal circuits are C_GENERAL.
379 * circuits that are c_introducing are either on their way to
380 * becoming open, or they are open and waiting for a
381 * suitable rendcirc before they send the intro.
382 * circuits that are c_introduce_ack_wait have sent the intro,
383 * but haven't gotten a response yet.
384 * circuits that are c_establish_rend are either on their way
385 * to becoming open, or they are open and have sent the
386 * establish_rendezvous cell but haven't received an ack.
387 * circuits that are c_rend_ready are open and have received a
388 * rend ack, but haven't heard from bob yet. if they have a
389 * buildstate->pending_final_cpath then they're expecting a
390 * cell from bob, else they're not.
391 * circuits that are c_rend_ready_intro_acked are open, and
392 * some intro circ has sent its intro and received an ack.
393 * circuits that are c_rend_joined are open, have heard from
394 * bob, and are talking to him.
396 /** Client-side circuit purpose: Normal circuit, with cpath. */
397 #define CIRCUIT_PURPOSE_C_GENERAL 5
398 /** Client-side circuit purpose: at Alice, connecting to intro point. */
399 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
400 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point,
401 * waiting for ACK/NAK. */
402 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
403 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
404 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
405 /** Client-side circuit purpose: at Alice, waiting for ack. */
406 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
407 /** Client-side circuit purpose: at Alice, waiting for Bob. */
408 #define CIRCUIT_PURPOSE_C_REND_READY 10
409 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
410 * has been acknowledged. */
411 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
412 /** Client-side circuit purpose: at Alice, rendezvous established. */
413 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
415 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
416 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
417 /** Hidden-service-side circuit purpose: at Bob, successfully established
418 * intro. */
419 #define CIRCUIT_PURPOSE_S_INTRO 14
420 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
421 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
422 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
423 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
424 /** A testing circuit; not meant to be used for actual traffic. */
425 #define CIRCUIT_PURPOSE_TESTING 17
426 /** A controller made this circuit and Tor should not use it. */
427 #define CIRCUIT_PURPOSE_CONTROLLER 18
428 #define _CIRCUIT_PURPOSE_MAX 18
430 /** True iff the circuit purpose <b>p</b> is for a circuit that
431 * originated at this node. */
432 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
433 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
435 #define RELAY_COMMAND_BEGIN 1
436 #define RELAY_COMMAND_DATA 2
437 #define RELAY_COMMAND_END 3
438 #define RELAY_COMMAND_CONNECTED 4
439 #define RELAY_COMMAND_SENDME 5
440 #define RELAY_COMMAND_EXTEND 6
441 #define RELAY_COMMAND_EXTENDED 7
442 #define RELAY_COMMAND_TRUNCATE 8
443 #define RELAY_COMMAND_TRUNCATED 9
444 #define RELAY_COMMAND_DROP 10
445 #define RELAY_COMMAND_RESOLVE 11
446 #define RELAY_COMMAND_RESOLVED 12
448 #define RELAY_COMMAND_ESTABLISH_INTRO 32
449 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
450 #define RELAY_COMMAND_INTRODUCE1 34
451 #define RELAY_COMMAND_INTRODUCE2 35
452 #define RELAY_COMMAND_RENDEZVOUS1 36
453 #define RELAY_COMMAND_RENDEZVOUS2 37
454 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
455 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
456 #define RELAY_COMMAND_INTRODUCE_ACK 40
458 #define END_STREAM_REASON_MISC 1
459 #define END_STREAM_REASON_RESOLVEFAILED 2
460 #define END_STREAM_REASON_CONNECTREFUSED 3
461 #define END_STREAM_REASON_EXITPOLICY 4
462 #define END_STREAM_REASON_DESTROY 5
463 #define END_STREAM_REASON_DONE 6
464 #define END_STREAM_REASON_TIMEOUT 7
465 /* 8 is unallocated. */
466 #define END_STREAM_REASON_HIBERNATING 9
467 #define END_STREAM_REASON_INTERNAL 10
468 #define END_STREAM_REASON_RESOURCELIMIT 11
469 #define END_STREAM_REASON_CONNRESET 12
470 #define END_STREAM_REASON_TORPROTOCOL 13
472 /* These high-numbered end reasons are not part of the official spec,
473 * and are not intended to be put in relay end cells. They are here
474 * to be more informative when sending back socks replies to the
475 * application. */
476 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
477 #define END_STREAM_REASON_CANT_ATTACH 257
478 #define END_STREAM_REASON_NET_UNREACHABLE 258
480 #define RESOLVED_TYPE_HOSTNAME 0
481 #define RESOLVED_TYPE_IPV4 4
482 #define RESOLVED_TYPE_IPV6 6
483 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
484 #define RESOLVED_TYPE_ERROR 0xF1
486 #define END_CIRC_AT_ORIGIN -1
487 #define _END_CIRC_REASON_MIN 0
488 #define END_CIRC_REASON_NONE 0
489 #define END_CIRC_REASON_TORPROTOCOL 1
490 #define END_CIRC_REASON_INTERNAL 2
491 #define END_CIRC_REASON_REQUESTED 3
492 #define END_CIRC_REASON_HIBERNATING 4
493 #define END_CIRC_REASON_RESOURCELIMIT 5
494 #define END_CIRC_REASON_CONNECTFAILED 6
495 #define END_CIRC_REASON_OR_IDENTITY 7
496 #define END_CIRC_REASON_OR_CONN_CLOSED 8
497 #define _END_CIRC_REASON_MAX 8
499 /** Length of 'y' portion of 'y.onion' URL. */
500 #define REND_SERVICE_ID_LEN 16
502 #define CELL_DIRECTION_IN 1
503 #define CELL_DIRECTION_OUT 2
505 #ifdef TOR_PERF
506 #define CIRCWINDOW_START 10000
507 #define CIRCWINDOW_INCREMENT 1000
508 #define STREAMWINDOW_START 5000
509 #define STREAMWINDOW_INCREMENT 500
510 #else
511 #define CIRCWINDOW_START 1000
512 #define CIRCWINDOW_INCREMENT 100
513 #define STREAMWINDOW_START 500
514 #define STREAMWINDOW_INCREMENT 50
515 #endif
517 /* cell commands */
518 #define CELL_PADDING 0
519 #define CELL_CREATE 1
520 #define CELL_CREATED 2
521 #define CELL_RELAY 3
522 #define CELL_DESTROY 4
523 #define CELL_CREATE_FAST 5
524 #define CELL_CREATED_FAST 6
526 /** How long to test reachability before complaining to the user. */
527 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60) /* 20 minutes */
529 /* people behind fascist firewalls use only these ports */
530 #define REQUIRED_FIREWALL_DIRPORT 80
531 #define REQUIRED_FIREWALL_ORPORT 443
533 /* legal characters in a nickname */
534 #define LEGAL_NICKNAME_CHARACTERS \
535 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
537 /** Name to use in client TLS certificates if no nickname is given.*/
538 #define DEFAULT_CLIENT_NICKNAME "client"
540 #define SOCKS4_NETWORK_LEN 8
542 typedef enum {
543 SOCKS5_SUCCEEDED = 0x00,
544 SOCKS5_GENERAL_ERROR = 0x01,
545 SOCKS5_NOT_ALLOWED = 0x02,
546 SOCKS5_NET_UNREACHABLE = 0x03,
547 SOCKS5_HOST_UNREACHABLE = 0x04,
548 SOCKS5_CONNECTION_REFUSED = 0x05,
549 SOCKS5_TTL_EXPIRED = 0x06,
550 SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
551 SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
552 } socks5_reply_status_t;
555 * Relay payload:
556 * Relay command [1 byte]
557 * Recognized [2 bytes]
558 * Stream ID [2 bytes]
559 * Partial SHA-1 [4 bytes]
560 * Length [2 bytes]
561 * Relay payload [498 bytes]
564 #define CELL_PAYLOAD_SIZE 509
565 #define CELL_NETWORK_SIZE 512
567 #define RELAY_HEADER_SIZE (1+2+2+4+2)
568 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
570 /** Parsed onion routing cell. All communication between nodes
571 * is via cells. */
572 typedef struct {
573 uint16_t circ_id; /**< Circuit which received the cell. */
574 uint8_t command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
575 * or DESTROY. */
576 char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
577 } cell_t;
579 /** Beginning of a RELAY cell payload. */
580 typedef struct {
581 uint8_t command; /**< The end-to-end relay command. */
582 uint16_t recognized; /**< Used to tell whether cell is for us. */
583 uint16_t stream_id; /**< Which stream is this cell associated with? */
584 char integrity[4]; /**< Used to tell whether cell is corrupted. */
585 uint16_t length; /**< How long is the payload body? */
586 } relay_header_t;
588 typedef struct buf_t buf_t;
589 typedef struct socks_request_t socks_request_t;
591 #define CONNECTION_MAGIC 0x7C3C304Eu
593 /** Description of a connection to another host or process, and associated
594 * data.
596 * A connection is named based on what it's connected to -- an "OR
597 * connection" has a Tor node on the other end, an "exit
598 * connection" has a website or other server on the other end, and an
599 * "AP connection" has an application proxy (and thus a user) on the
600 * other end.
602 * Every connection has a type and a state. Connections never change
603 * their type, but can go through many state changes in their lifetime.
605 * Every connection has two associated input and output buffers.
606 * Listeners don't use them. For non-listener connections, incoming
607 * data is appended to conn->inbuf, and outgoing data is taken from
608 * conn->outbuf. Connections differ primarily in the functions called
609 * to fill and drain these buffers.
611 struct connection_t {
612 uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
614 uint8_t type; /**< What kind of connection is this? */
615 uint8_t state; /**< Current state of this connection. */
616 uint8_t purpose; /**< Only used for DIR types currently. */
617 unsigned wants_to_read:1; /**< Boolean: should we start reading again once
618 * the bandwidth throttler allows it?
620 unsigned wants_to_write:1; /**< Boolean: should we start writing again once
621 * the bandwidth throttler allows reads?
623 unsigned hold_open_until_flushed:1; /**< Despite this connection's being
624 * marked for close, do we flush it
625 * before closing it?
627 unsigned has_sent_end:1; /**< For debugging; only used on edge connections.
628 * Set once we've set the stream end,
629 * and check in circuit_about_to_close_connection(). */
630 /** For control connections only. If set, we send extended info with control
631 * events as appropriate. */
632 unsigned int control_events_are_extended:1;
633 /** Used for OR conns that shouldn't get any new circs attached to them. */
634 unsigned int is_obsolete:1;
636 int s; /**< Our socket; -1 if this connection is closed. */
637 int poll_index; /* XXXX rename. */
638 struct event *read_event; /**< libevent event structure. */
639 struct event *write_event; /**< libevent event structure. */
640 buf_t *inbuf; /**< Buffer holding data read over this connection. */
641 int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
642 time_t timestamp_lastread; /**< When was the last time poll() said we could
643 * read? */
645 buf_t *outbuf; /**< Buffer holding data to write over this connection. */
646 size_t outbuf_flushlen; /**< How much data should we try to flush from the
647 * outbuf? */
648 time_t timestamp_lastwritten; /**< When was the last time poll() said we
649 * could write? */
651 time_t timestamp_created; /**< When was this connection_t created? */
652 time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
654 uint32_t addr; /**< IP of the other side of the connection; used to identify
655 * routers, along with port. */
656 uint16_t port; /**< If non-zero, port on the other end
657 * of the connection. */
658 uint16_t marked_for_close; /**< Should we close this conn on the next
659 * iteration of the main loop? (If true, holds
660 * the line number where this connection was
661 * marked.)
663 const char *marked_for_close_file; /**< For debugging: in which file were
664 * we marked for close? */
665 char *address; /**< FQDN (or IP) of the guy on the other end.
666 * strdup into this, because free_connection frees it.
668 crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
669 * signing key. */
670 char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
671 char *nickname; /**< Nickname of OR on other side (if any). */
673 /** Nickname of planned exit node -- used with .exit support. */
674 char *chosen_exit_name;
676 /* Used only by OR connections: */
677 tor_tls_t *tls; /**< TLS connection state (OR only.) */
679 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
680 int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
681 int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
682 * add 'bandwidth' to this, capping it at 10*bandwidth.
683 * (OPEN ORs only)
685 circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
686 * connection, which half of the space should
687 * we use? */
688 int n_circuits; /**< How many circuits use this connection as p_conn or
689 * n_conn ? */
690 struct connection_t *next_with_same_id; /**< Next connection with same
691 * identity digest as this one. */
692 uint16_t next_circ_id; /**< Which circ_id do we try to use next on
693 * this connection? This is always in the
694 * range 0..1<<15-1. (OR only.)*/
696 /* Used only by edge connections: */
697 uint16_t stream_id;
698 struct connection_t *next_stream; /**< Points to the next stream at this
699 * edge, if any (Edge only). */
700 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
701 * this conn exits at. (Edge only.) */
702 int package_window; /**< How many more relay cells can i send into the
703 * circuit? (Edge only.) */
704 int deliver_window; /**< How many more relay cells can end at me? (Edge
705 * only.) */
707 /* Used only by Dir connections */
708 char *requested_resource; /**< Which 'resource' did we ask the directory
709 * for?*/
711 /* Used only by AP connections */
712 socks_request_t *socks_request; /**< SOCKS structure describing request (AP
713 * only.) */
715 /** Quasi-global identifier for this connection; used for control.c */
716 /* XXXX NM This can get re-used after 2**32 circuits. */
717 uint32_t global_identifier;
719 /* Used only by control connections */
720 uint32_t event_mask;
721 uint32_t incoming_cmd_len;
722 uint32_t incoming_cmd_cur_len;
723 char *incoming_cmd;
725 /* Used only by DIR and AP connections: */
726 struct circuit_t *on_circuit; /**< The circuit (if any) that this edge
727 * connection is using. */
728 char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
729 * querying for? (DIR/AP only) */
731 /* Used only by control v0 connections */
732 uint16_t incoming_cmd_type;
735 typedef struct connection_t connection_t;
737 typedef enum {
738 ADDR_POLICY_ACCEPT=1,
739 ADDR_POLICY_REJECT=2,
740 } addr_policy_action_t;
742 /** A linked list of policy rules */
743 typedef struct addr_policy_t {
744 addr_policy_action_t policy_type; /**< What to do when the policy matches.*/
745 char *string; /**< String representation of this rule. */
746 uint32_t addr; /**< Base address to accept or reject. */
747 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
748 * a &amp; msk == <b>addr</b> &amp; msk . */
749 uint16_t prt_min; /**< Lowest port number to accept/reject. */
750 uint16_t prt_max; /**< Highest port number to accept/reject. */
752 struct addr_policy_t *next; /**< Next rule in list. */
753 } addr_policy_t;
755 /** A cached_dir_t represents a cacheable directory object, along with its
756 * compressed form. */
757 typedef struct cached_dir_t {
758 char *dir; /**< Contents of this object */
759 char *dir_z; /**< Compressed contents of this object. */
760 size_t dir_len; /**< Length of <b>dir</b> */
761 size_t dir_z_len; /**< Length of <b>dir_z</b> */
762 time_t published; /**< When was this object published */
763 } cached_dir_t;
765 /** Information need to cache an onion router's descriptor. */
766 typedef struct signed_descriptor_t {
767 char *signed_descriptor_body;
768 size_t signed_descriptor_len;
769 char signed_descriptor_digest[DIGEST_LEN];
770 char identity_digest[DIGEST_LEN];
771 time_t published_on;
772 } signed_descriptor_t;
774 /** Information about another onion router in the network. */
775 typedef struct {
776 signed_descriptor_t cache_info;
777 char *address; /**< Location of OR: either a hostname or an IP address. */
778 char *nickname; /**< Human-readable OR name. */
780 uint32_t addr; /**< IPv4 address of OR, in host order. */
781 uint16_t or_port; /**< Port for TLS connections. */
782 uint16_t dir_port; /**< Port for HTTP directory connections. */
784 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
785 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
787 char *platform; /**< What software/operating system is this OR using? */
789 /* link info */
790 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
791 * bucket per second? */
792 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
793 /** How many bytes/s is this router known to handle? */
794 uint32_t bandwidthcapacity;
795 addr_policy_t *exit_policy; /**< What streams will this OR permit
796 * to exit? */
797 long uptime; /**< How many seconds the router claims to have been up */
798 smartlist_t *declared_family; /**< Nicknames of router which this router
799 * claims are its family. */
800 char *contact_info; /**< Declared contact info for this router. */
801 unsigned int is_hibernating:1; /**< Whether the router claims to be
802 * hibernating */
804 /* local info */
805 unsigned int is_running:1; /**< As far as we know, is this OR currently
806 * running? */
807 unsigned int is_verified:1; /**< Has a trusted dirserver validated this OR?
808 * (For Authdir: Have we validated this OR?)
810 unsigned int is_named:1; /**< Do we believe the nickname that this OR gives
811 * us? */
812 unsigned int is_fast:1; /** Do we think this is a fast OR? */
813 unsigned int is_stable:1; /** Do we think this is a stable OR? */
814 unsigned int is_possible_guard:1; /**< Do we think this is an OK guard? */
816 /* The below items are used only by authdirservers for
817 * reachability testing. */
818 /** When was the last time we could reach this OR? */
819 time_t last_reachable;
820 /** When did we start testing reachability for this OR? */
821 time_t testing_since;
822 /** How many times has a descriptor been posted and we believed
823 * this router to be unreachable? We only actually warn on the third. */
824 int num_unreachable_notifications;
825 } routerinfo_t;
827 /** Contents of a single router entry in a network status object.
829 typedef struct routerstatus_t {
830 time_t published_on; /**< When was this router published? */
831 char nickname[MAX_NICKNAME_LEN+1]; /**< The nickname this router says it
832 * has. */
833 char identity_digest[DIGEST_LEN]; /**< Digest of the router's identity
834 * key. */
835 char descriptor_digest[DIGEST_LEN]; /**< Digest of the router's most recent
836 * descriptor. */
837 uint32_t addr; /**< IPv4 address for this router. */
838 uint16_t or_port; /**< OR port for this router. */
839 uint16_t dir_port; /**< Directory port for this router. */
840 unsigned int is_exit:1; /**< True iff this router is a good exit. */
841 unsigned int is_stable:1; /**< True iff this router stays up a long time. */
842 unsigned int is_fast:1; /**< True iff this router has good bandwidth. */
843 unsigned int is_running:1; /**< True iff this router is up. */
844 unsigned int is_named:1; /**< True iff "nickname" belongs to this router. */
845 unsigned int is_valid:1; /**< True iff this router is validated. */
846 unsigned int is_v2_dir:1; /**< True iff this router can serve directory
847 * information with v2 of the directory
848 * protocol. (All directory caches cache v1
849 * directories.) */
850 unsigned int is_possible_guard:1; /**< True iff this router would be a good
851 * choice as an entry guard. */
853 /** True if we, as a directory mirror, want to download the corresponding
854 * routerinfo from the authority who gave us this routerstatus. (That is,
855 * if we don't have the routerinfo, and if we haven't already tried to get it
856 * from this authority.)
858 unsigned int need_to_mirror:1;
859 } routerstatus_t;
861 /** Our "local" or combined view of the info from all networkstatus objects
862 * about a single router. */
863 typedef struct local_routerstatus_t {
864 /** What do we believe to be the case about this router? In this field,
865 * descriptor_digest represents the descriptor we would most like to use for
866 * this router. */
867 routerstatus_t status;
868 time_t next_attempt_at; /**< When should we try this descriptor again? */
869 uint8_t n_download_failures; /**< Number of failures trying to download the
870 * most recent descriptor. */
871 unsigned int name_lookup_warned:1; /**< Have we warned the user for referring
872 * to this (unnamed) router by nickname?
874 } local_routerstatus_t;
876 /*XXXX011 make this configurable? */
877 #define MAX_ROUTERDESC_DOWNLOAD_FAILURES 8
879 /** Contents of a (v2 or later) network status object. */
880 typedef struct networkstatus_t {
881 /** When did we receive the network-status document? */
882 time_t received_on;
884 /** What was the digest of the document? */
885 char networkstatus_digest[DIGEST_LEN];
887 unsigned int is_recent; /**< Is this recent enough to influence running
888 * status? */
890 /* These fields come from the actual network-status document.*/
891 time_t published_on; /**< Declared publication date. */
893 char *source_address; /**< Canonical directory server hostname. */
894 uint32_t source_addr; /**< Canonical directory server IP. */
895 uint16_t source_dirport; /**< Canonical directory server dirport. */
897 char identity_digest[DIGEST_LEN]; /**< Digest of signing key. */
898 char *contact; /**< How to contact directory admin? (may be NULL). */
899 crypto_pk_env_t *signing_key; /**< Key used to sign this directory. */
900 char *client_versions; /**< comma-separated list of recommended client
901 * versions. */
902 char *server_versions; /**< comma-separated list of recommended server
903 * versions. */
905 unsigned int binds_names:1; /**< True iff this directory server binds
906 * names. */
907 unsigned int recommends_versions:1; /**< True iff this directory server
908 * recommends client and server software
909 * versions. */
911 smartlist_t *entries; /**< List of router_status_t*. This list is kept
912 * sorted by identity_digest. */
913 } networkstatus_t;
915 /** Contents of a directory of onion routers. */
916 typedef struct {
917 /** Map from server identity digest to a member of routers. */
918 digestmap_t *identity_map;
919 /** Map from server descriptor digest to a signed_descriptor_t from
920 * routers or old_routers. */
921 digestmap_t *desc_digest_map;
922 /** List of routerinfo_t for all currently live routers we know. */
923 smartlist_t *routers;
924 /** List of signed_descriptor_t for older router descriptors we're
925 * caching. */
926 smartlist_t *old_routers;
927 } routerlist_t;
929 /** Information on router used when extending a circuit. (We don't need a
930 * full routerinfo_t to extend: we only need addr:port:keyid to build an OR
931 * connection, and onion_key to create the onionskin.) */
932 typedef struct extend_info_t {
933 char nickname[MAX_HEX_NICKNAME_LEN+1]; /**< This router's nickname for
934 * display. */
935 char identity_digest[DIGEST_LEN]; /**< Hash of this router's identity key. */
936 uint32_t addr; /**< IP address in host order. */
937 uint16_t port; /**< OR port. */
938 crypto_pk_env_t *onion_key; /**< Current onionskin key. */
939 } extend_info_t;
941 #define CRYPT_PATH_MAGIC 0x70127012u
943 /** Holds accounting information for a single step in the layered encryption
944 * performed by a circuit. Used only at the client edge of a circuit. */
945 typedef struct crypt_path_t {
946 uint32_t magic;
948 /* crypto environments */
949 /** Encryption key and counter for cells heading towards the OR at this
950 * step. */
951 crypto_cipher_env_t *f_crypto;
952 /** Encryption key and counter for cells heading back from the OR at this
953 * step. */
954 crypto_cipher_env_t *b_crypto;
956 /** Digest state for cells heading towards the OR at this step. */
957 crypto_digest_env_t *f_digest; /* for integrity checking */
958 /** Digest state for cells heading away from the OR at this step. */
959 crypto_digest_env_t *b_digest;
961 /** Current state of Diffie-Hellman key negotiation with the OR at this
962 * step. */
963 crypto_dh_env_t *dh_handshake_state;
964 /** Current state of 'fast' (non-PK) key negotiation with the OR at this
965 * step. Used to save CPU when TLS is already providing all the
966 * authentication, secrecy, and integrity we need, and we're already
967 * distinguishable from an OR.
969 char fast_handshake_state[DIGEST_LEN];
970 /** Negotiated key material shared with the OR at this step. */
971 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
973 /** Information to extend to the OR at this step. */
974 extend_info_t *extend_info;
976 /** Is the circuit built to this step? Must be one of:
977 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
978 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
979 * and not received an EXTENDED/CREATED)
980 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
981 uint8_t state;
982 #define CPATH_STATE_CLOSED 0
983 #define CPATH_STATE_AWAITING_KEYS 1
984 #define CPATH_STATE_OPEN 2
985 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
986 * (The list is circular, so the last node
987 * links to the first.) */
988 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
989 * circuit. */
991 int package_window; /**< How many bytes are we allowed to originate ending
992 * at this step? */
993 int deliver_window; /**< How many bytes are we willing to deliver originating
994 * at this step? */
995 } crypt_path_t;
997 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
999 #define DH_KEY_LEN DH_BYTES
1000 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
1001 CIPHER_KEY_LEN+\
1002 DH_KEY_LEN)
1003 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
1004 #define REND_COOKIE_LEN DIGEST_LEN
1006 /** Information used to build a circuit. */
1007 typedef struct {
1008 /** Intended length of the final circuit. */
1009 int desired_path_len;
1010 /** How to extend to the planned exit node. */
1011 extend_info_t *chosen_exit;
1012 /** Whether every node in the circ must have adequate uptime. */
1013 int need_uptime;
1014 /** Whether every node in the circ must have adequate capacity. */
1015 int need_capacity;
1016 /** Whether the last hop was picked with exiting in mind. */
1017 int is_internal;
1018 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
1019 crypt_path_t *pending_final_cpath;
1020 /** How many times has building a circuit for this task failed? */
1021 int failure_count;
1022 /** At what time should we give up on this task? */
1023 time_t expiry_time;
1024 } cpath_build_state_t;
1026 #define CIRCUIT_MAGIC 0x35315243u
1029 * A circuit is a path over the onion routing
1030 * network. Applications can connect to one end of the circuit, and can
1031 * create exit connections at the other end of the circuit. AP and exit
1032 * connections have only one circuit associated with them (and thus these
1033 * connection types are closed when the circuit is closed), whereas
1034 * OR connections multiplex many circuits at once, and stay standing even
1035 * when there are no circuits running over them.
1037 * A circuit_t structure fills two roles. First, a circuit_t links two
1038 * connections together: either an edge connection and an OR connection,
1039 * or two OR connections. (When joined to an OR connection, a circuit_t
1040 * affects only cells sent to a particular circID on that connection. When
1041 * joined to an edge connection, a circuit_t affects all data.)
1043 * Second, a circuit_t holds the cipher keys and state for sending data
1044 * along a given circuit. At the OP, it has a sequence of ciphers, each
1045 * of which is shared with a single OR along the circuit. Separate
1046 * ciphers are used for data going "forward" (away from the OP) and
1047 * "backward" (towards the OP). At the OR, a circuit has only two stream
1048 * ciphers: one for data going forward, and one for data going backward.
1050 struct circuit_t {
1051 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
1053 /** The OR connection that is previous in this circuit. */
1054 connection_t *p_conn;
1055 /** The OR connection that is next in this circuit. */
1056 connection_t *n_conn;
1057 /** The identity hash of n_conn. */
1058 char n_conn_id_digest[DIGEST_LEN];
1059 /** Linked list of AP streams associated with this circuit. */
1060 connection_t *p_streams;
1061 /** Linked list of Exit streams associated with this circuit. */
1062 connection_t *n_streams;
1063 /** Linked list of Exit streams associated with this circuit that are
1064 * still being resolved. */
1065 connection_t *resolving_streams;
1066 /** The IPv4 address of the OR that is next in this circuit. */
1067 uint32_t n_addr;
1068 /** The port for the OR that is next in this circuit. */
1069 uint16_t n_port;
1070 /** The next stream_id that will be tried when we're attempting to
1071 * construct a new AP stream originating at this circuit. */
1072 uint16_t next_stream_id;
1073 /** How many relay data cells can we package (read from edge streams)
1074 * on this circuit before we receive a circuit-level sendme cell asking
1075 * for more? */
1076 int package_window;
1077 /** How many relay data cells will we deliver (write to edge streams)
1078 * on this circuit? When deliver_window gets low, we send some
1079 * circuit-level sendme cells to indicate that we're willing to accept
1080 * more. */
1081 int deliver_window;
1083 /** The circuit_id used in the previous (backward) hop of this circuit. */
1084 uint16_t p_circ_id;
1085 /** The circuit_id used in the next (forward) hop of this circuit. */
1086 uint16_t n_circ_id;
1088 /** The cipher used by intermediate hops for cells heading toward the
1089 * OP. */
1090 crypto_cipher_env_t *p_crypto;
1091 /** The cipher used by intermediate hops for cells heading away from
1092 * the OP. */
1093 crypto_cipher_env_t *n_crypto;
1095 /** The integrity-checking digest used by intermediate hops, for
1096 * cells packaged here and heading towards the OP.
1098 crypto_digest_env_t *p_digest;
1099 /** The integrity-checking digest used by intermediate hops, for
1100 * cells packaged at the OP and arriving here.
1102 crypto_digest_env_t *n_digest;
1104 /** Build state for this circuit. It includes the intended path
1105 * length, the chosen exit router, rendezvous information, etc.
1107 cpath_build_state_t *build_state;
1108 /** The doubly-linked list of crypt_path_t entries, one per hop,
1109 * for this circuit. This includes ciphers for each hop,
1110 * integrity-checking digests for each hop, and package/delivery
1111 * windows for each hop.
1113 * The cpath field is defined only when we are the circuit's origin.
1115 crypt_path_t *cpath;
1117 /** For storage while passing to cpuworker (state
1118 * CIRCUIT_STATE_ONIONSKIN_PENDING), or while n_conn is pending
1119 * (state CIRCUIT_STATE_OR_WAIT). When defined, it is always
1120 * length ONIONSKIN_CHALLENGE_LEN. */
1121 char *onionskin;
1123 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
1125 time_t timestamp_created; /**< When was this circuit created? */
1126 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
1127 * circuit is clean. */
1129 uint8_t state; /**< Current status of this circuit. */
1130 uint8_t purpose; /**< Why are we creating this circuit? */
1132 uint16_t marked_for_close; /**< Should we close this circuit at the end of
1133 * the main loop? (If true, holds the line number
1134 * where this circuit was marked.) */
1135 const char *marked_for_close_file; /**< For debugging: in which file was this
1136 * circuit marked for close? */
1139 * The rend_query field holds the y portion of y.onion (nul-terminated)
1140 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
1141 * for a hidden service, or is S_*.
1143 char rend_query[REND_SERVICE_ID_LEN+1];
1145 /** The rend_pk_digest field holds a hash of location-hidden service's
1146 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
1148 char rend_pk_digest[DIGEST_LEN];
1150 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
1151 * C_ESTABLISH_REND. Filled with zeroes otherwise.
1153 char rend_cookie[REND_COOKIE_LEN];
1155 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
1156 * is not marked for close. */
1157 struct circuit_t *rend_splice;
1159 /** Quasi-global identifier for this circuit; used for control.c */
1160 /* XXXX NM This can get re-used after 2**32 circuits. */
1161 uint32_t global_identifier;
1163 struct circuit_t *next; /**< Next circuit in linked list. */
1166 typedef struct circuit_t circuit_t;
1168 #define ALLOW_UNVERIFIED_ENTRY 1
1169 #define ALLOW_UNVERIFIED_EXIT 2
1170 #define ALLOW_UNVERIFIED_MIDDLE 4
1171 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
1172 #define ALLOW_UNVERIFIED_INTRODUCTION 16
1174 /** An entry specifying a set of addresses and ports that should be remapped
1175 * to another address and port before exiting this exit node. */
1176 typedef struct exit_redirect_t {
1177 uint32_t addr;
1178 uint32_t mask;
1179 uint16_t port_min;
1180 uint16_t port_max;
1182 uint32_t addr_dest;
1183 uint16_t port_dest;
1184 unsigned is_redirect:1;
1185 } exit_redirect_t;
1187 /** A linked list of lines in a config file. */
1188 typedef struct config_line_t {
1189 char *key;
1190 char *value;
1191 struct config_line_t *next;
1192 } config_line_t;
1194 /** Configuration options for a Tor process. */
1195 typedef struct {
1196 uint32_t _magic;
1198 /** What should the tor process actually do? */
1199 enum {
1200 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
1201 CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS
1202 } command;
1203 const char *command_arg; /**< Argument for command-line option. */
1205 config_line_t *OldLogOptions; /**< List of configuration lines
1206 * for logfiles, old style. */
1208 config_line_t *Logs; /**< New-style list of configuration lines
1209 * for logs */
1211 char *DebugLogFile; /**< Where to send verbose log messages. */
1212 char *DataDirectory; /**< OR only: where to store long-term data. */
1213 char *Nickname; /**< OR only: nickname of this onion router. */
1214 char *Address; /**< OR only: configured address for this onion router. */
1215 char *PidFile; /**< Where to store PID of Tor process. */
1217 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
1218 * as exits. */
1219 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
1220 * as entry points. */
1221 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
1222 * stop building circuits? */
1223 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
1224 * stop building circuits? */
1225 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
1226 * use in circuits. */
1228 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
1229 * points. */
1230 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
1231 * as introduction points. */
1233 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
1234 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
1235 config_line_t *ExitPolicy; /**< Lists of exit policy components. */
1236 int ExitPolicyRejectPrivate; /**< Should we not exit to local addresses? */
1237 config_line_t *SocksPolicy; /**< Lists of socks policy components */
1238 config_line_t *DirPolicy; /**< Lists of dir policy components */
1239 /** Addresses to bind for listening for SOCKS connections. */
1240 config_line_t *SocksListenAddress;
1241 /** Addresses to bind for listening for OR connections. */
1242 config_line_t *ORListenAddress;
1243 /** Addresses to bind for listening for directory connections. */
1244 config_line_t *DirListenAddress;
1245 /** Addresses to bind for listening for control connections. */
1246 config_line_t *ControlListenAddress;
1247 /** Local address to bind outbound sockets */
1248 char *OutboundBindAddress;
1249 /** Directory server only: which versions of
1250 * Tor should we tell users to run? */
1251 config_line_t *RecommendedVersions;
1252 config_line_t *RecommendedClientVersions;
1253 config_line_t *RecommendedServerVersions;
1254 /** Whether dirservers refuse router descriptors with private IPs. */
1255 int DirAllowPrivateAddresses;
1256 char *User; /**< Name of user to run Tor as. */
1257 char *Group; /**< Name of group to run Tor as. */
1258 double PathlenCoinWeight; /**< Parameter used to configure average path
1259 * length (alpha in geometric distribution). */
1260 int ORPort; /**< Port to listen on for OR connections. */
1261 int SocksPort; /**< Port to listen on for SOCKS connections. */
1262 int ControlPort; /**< Port to listen on for control connections. */
1263 int DirPort; /**< Port to listen on for directory connections. */
1264 int AssumeReachable; /**< Whether to publish our descriptor regardless. */
1265 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
1266 int V1AuthoritativeDir; /**< Boolean: is this an authoritative directory?
1267 * for version 1 directories? */
1268 int NamingAuthoritativeDir; /**< Boolean: is this an authoritative directory
1269 * that's willing to bind names? */
1270 int VersioningAuthoritativeDir; /**< Boolean: is this an authoritative
1271 * directory that's willing to recommend
1272 * versions? */
1273 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
1274 int NoPublish; /**< Boolean: should we never publish a descriptor? */
1275 int PublishServerDescriptor; /**< Do we publish our descriptor as normal? */
1276 int PublishHidServDescriptors; /**< and our hidden service descriptors? */
1277 int FetchServerDescriptors; /**< Do we fetch server descriptors as normal? */
1278 int FetchHidServDescriptors; /** and hidden service descriptors? */
1279 int AllDirActionsPrivate; /**< Should every directory action be sent
1280 * through a Tor circuit? */
1282 int ConnLimit; /**< Demanded minimum number of simultaneous connections. */
1283 int _ConnLimit; /**< Maximum allowed number of simultaneous connections. */
1284 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
1285 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
1286 smartlist_t *FirewallPorts; /**< Which ports our firewall allows
1287 * (strings). */
1288 config_line_t *ReachableAddresses; /**< IP:ports our firewall allows. */
1289 config_line_t *ReachableORAddresses; /**< IP:ports for OR conns. */
1290 config_line_t *ReachableDirAddresses; /**< IP:ports for Dir conns. */
1292 /** Application ports that require all nodes in circ to have sufficient
1293 * uptime. */
1294 smartlist_t *LongLivedPorts;
1295 /** Should we try to reuse the same exit node for a given host */
1296 smartlist_t *TrackHostExits;
1297 int TrackHostExitsExpire; /**< Number of seconds until we expire an
1298 * addressmap */
1299 config_line_t *AddressMap; /**< List of address map directives. */
1300 int DirFetchPeriod; /**< How often do we fetch new directories? */
1301 int DirPostPeriod; /**< How often do we post our server descriptor to the
1302 * authoritative directory servers? */
1303 int RendPostPeriod; /**< How often do we post each rendezvous service
1304 * descriptor? Remember to publish them independently. */
1305 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
1306 int KeepalivePeriod; /**< How often do we send padding cells to keep
1307 * connections alive? */
1308 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
1309 * to wait simultaneously before we start dropping
1310 * them? */
1311 int NewCircuitPeriod; /**< How long do we use a circuit before building
1312 * a new one? */
1313 int MaxCircuitDirtiness; /**< Never use circs that were first used more than
1314 this interval ago. */
1315 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing
1316 * to use in a second? */
1317 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing
1318 * to use in a second? */
1319 uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
1320 * tell people we have? */
1321 int NumCpus; /**< How many CPUs should we try to use? */
1322 int RunTesting; /**< If true, create testing circuits to measure how well the
1323 * other ORs are running. */
1324 config_line_t *RendConfigLines; /**< List of configuration lines
1325 * for rendezvous services. */
1326 char *ContactInfo; /**< Contact info to be published in the directory */
1328 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
1329 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
1330 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
1331 char *HttpProxyAuthenticator; /**< username:password string, if any */
1333 char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
1334 uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
1335 uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
1336 char *HttpsProxyAuthenticator; /**< username:password string, if any */
1338 config_line_t *DirServers; /**< List of configuration lines
1339 * for directory servers. */
1340 char *MyFamily; /**< Declared family for this OR. */
1341 config_line_t *NodeFamilies; /**< List of config lines for
1342 * node families */
1343 config_line_t *RedirectExit; /**< List of config lines for simple
1344 * addr/port redirection */
1345 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
1346 config_line_t *AuthDirReject; /**< Address policy for descriptors to
1347 * reject. */
1348 config_line_t *AuthDirInvalid; /**< Address policy for descriptors to
1349 * never mark as valid. */
1350 int AuthDirRejectUnlisted; /**< Boolean: do we reject all routers that
1351 * aren't named in our fingerprint file? */
1352 char *AccountingStart; /**< How long is the accounting interval, and when
1353 * does it start? */
1354 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
1355 * interval before hibernation? 0 for "never
1356 * hibernate." */
1357 int _AccountingMaxKB; /**< How many KB do we allow per accounting
1358 * interval before hibernation? 0 for "never
1359 * hibernate." (Based on a deprecated option)*/
1361 char *HashedControlPassword; /**< Base64-encoded hash of a password for
1362 * the control system. */
1363 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
1364 * the control system? */
1365 int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
1366 * circuits itself (0), or does it expect a controller
1367 * to cope? (1) */
1368 int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
1369 * long do we wait before exiting? */
1370 int SafeLogging; /**< Boolean: are we allowed to log sensitive strings
1371 * such as addresses (0), or do we scrub them first (1)? */
1372 #define LOG_PROTOCOL_WARN (get_options()->ProtocolWarnings ? \
1373 LOG_WARN : LOG_INFO)
1374 int ProtocolWarnings; /**< Boolean: when other parties screw up the Tor
1375 * protocol, is it a warn or an info in our logs? */
1376 int TestSocks; /**< Boolean: when we get a socks connection, do we loudly
1377 * log whether it was DNS-leaking or not? */
1378 int HardwareAccel; /**< Boolean: Should we enable OpenSSL hardware
1379 * acceleration where available? */
1380 int UseEntryGuards; /**< Boolean: Do we try to enter from a smallish number
1381 * of fixed nodes? */
1382 int NumEntryGuards; /**< How many entry guards do we try to establish? */
1383 int RephistTrackTime; /**< How many seconds do we keep rephist info? */
1384 int FastFirstHopPK; /**< If Tor believes it is safe, should we save a third
1385 * of our PK time by sending CREATE_FAST cells? */
1387 addr_policy_t *reachable_addr_policy; /**< Parsed from ReachableAddresses */
1388 } or_options_t;
1390 /** Persistent state for an onion router, as saved to disk. */
1391 typedef struct {
1392 uint32_t _magic;
1393 int dirty;
1395 /* XXXX These options aren't actually attached to anything yet. */
1396 time_t LastWritten;
1397 time_t AccountingIntervalStart;
1398 uint64_t AccountingBytesReadInInterval;
1399 uint64_t AccountingBytesWrittenInInterval;
1400 int AccountingSecondsActive;
1401 uint64_t AccountingExpectedUsage;
1403 config_line_t *EntryGuards;
1405 time_t BWHistoryReadEnds;
1406 int BWHistoryReadInterval;
1407 smartlist_t *BWHistoryReadValues;
1408 time_t BWHistoryWriteEnds;
1409 int BWHistoryWriteInterval;
1410 smartlist_t *BWHistoryWriteValues;
1412 char *TorVersion;
1414 config_line_t *ExtraLines;
1415 } or_state_t;
1417 #define MAX_SOCKS_REPLY_LEN 1024
1418 #define MAX_SOCKS_ADDR_LEN 256
1419 #define SOCKS_COMMAND_CONNECT 0x01
1420 #define SOCKS_COMMAND_RESOLVE 0xF0
1421 /** State of a SOCKS request from a user to an OP */
1422 struct socks_request_t {
1423 char socks_version; /**< Which version of SOCKS did the client use? */
1424 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
1425 size_t replylen; /**< Length of <b>reply</b>. */
1426 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
1427 * we want to specify our own socks reply,
1428 * rather than using the default socks4 or
1429 * socks5 socks reply. We use this for the
1430 * two-stage socks5 handshake.
1432 int has_finished; /**< Has the SOCKS handshake finished? */
1433 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to
1434 connect to? */
1435 uint16_t port; /**< What port did the client ask to connect to? */
1438 /* all the function prototypes go here */
1440 /********************************* buffers.c ***************************/
1442 buf_t *buf_new(void);
1443 buf_t *buf_new_with_capacity(size_t size);
1444 void buf_free(buf_t *buf);
1445 void buf_clear(buf_t *buf);
1446 void buf_shrink(buf_t *buf);
1448 size_t buf_datalen(const buf_t *buf);
1449 size_t buf_capacity(const buf_t *buf);
1450 const char *_buf_peek_raw_buffer(const buf_t *buf);
1452 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1453 int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
1455 int flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen);
1456 int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
1458 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1459 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1460 int fetch_from_buf_http(buf_t *buf,
1461 char **headers_out, size_t max_headerlen,
1462 char **body_out, size_t *body_used, size_t max_bodylen,
1463 int force_complete);
1464 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req, int log_sockstype);
1465 int fetch_from_buf_control0(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
1466 char **body_out, int check_for_v1);
1467 int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
1469 void assert_buf_ok(buf_t *buf);
1471 /********************************* circuitbuild.c **********************/
1473 char *circuit_list_path(circuit_t *circ, int verbose);
1474 void circuit_log_path(int severity, unsigned int domain, circuit_t *circ);
1475 void circuit_rep_hist_note_result(circuit_t *circ);
1476 void circuit_dump_by_conn(connection_t *conn, int severity);
1477 circuit_t *circuit_init(uint8_t purpose, int need_uptime,
1478 int need_capacity, int internal);
1479 circuit_t *circuit_establish_circuit(uint8_t purpose, extend_info_t *exit,
1480 int need_uptime, int need_capacity,
1481 int internal);
1482 int circuit_handle_first_hop(circuit_t *circ);
1483 void circuit_n_conn_done(connection_t *or_conn, int status);
1484 int inform_testing_reachability(void);
1485 int circuit_send_next_onion_skin(circuit_t *circ);
1486 void circuit_note_clock_jumped(int seconds_elapsed);
1487 int circuit_extend(cell_t *cell, circuit_t *circ);
1488 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data,
1489 int reverse);
1490 int circuit_finish_handshake(circuit_t *circ, uint8_t cell_type, char *reply);
1491 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1492 int onionskin_answer(circuit_t *circ, uint8_t cell_type, char *payload,
1493 char *keys);
1494 int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
1495 int *need_capacity);
1497 int circuit_append_new_exit(circuit_t *circ, extend_info_t *info);
1498 int circuit_extend_to_new_exit(circuit_t *circ, extend_info_t *info);
1499 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1500 extend_info_t *extend_info_from_router(routerinfo_t *r);
1501 extend_info_t *extend_info_dup(extend_info_t *info);
1502 void extend_info_free(extend_info_t *info);
1503 routerinfo_t *build_state_get_exit_router(cpath_build_state_t *state);
1504 const char *build_state_get_exit_nickname(cpath_build_state_t *state);
1506 void entry_guards_set_status_from_directory(void);
1507 int entry_guard_set_status(const char *digest, int succeeded);
1508 void entry_nodes_should_be_added(void);
1509 void entry_guards_prepend_from_config(void);
1510 void entry_guards_update_state(or_state_t *state);
1511 int entry_guards_parse_state(or_state_t *state, int set, const char **err);
1512 int entry_guards_getinfo(const char *question, char **answer);
1513 void entry_guards_free_all(void);
1515 /********************************* circuitlist.c ***********************/
1517 circuit_t * _circuit_get_global_list(void);
1518 const char *circuit_state_to_string(int state);
1519 enum which_conn_changed_t { P_CONN_CHANGED=1, N_CONN_CHANGED=0 };
1520 void circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
1521 connection_t *conn,
1522 enum which_conn_changed_t which);
1523 void circuit_set_state(circuit_t *circ, int state);
1524 void circuit_close_all_marked(void);
1525 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1526 circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn);
1527 int circuit_id_used_on_conn(uint16_t circ_id, connection_t *conn);
1528 circuit_t *circuit_get_by_edge_conn(connection_t *conn);
1529 void circuit_unlink_all_from_or_conn(connection_t *conn, int reason);
1530 circuit_t *circuit_get_by_global_id(uint32_t id);
1531 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query,
1532 uint8_t purpose);
1533 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1534 const char *digest, uint8_t purpose);
1535 circuit_t *circuit_get_rendezvous(const char *cookie);
1536 circuit_t *circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
1537 int need_uptime,
1538 int need_capacity, int internal);
1539 void circuit_mark_all_unused_circs(void);
1540 void circuit_expire_all_dirty_circs(void);
1541 void _circuit_mark_for_close(circuit_t *circ, int reason,
1542 int line, const char *file);
1544 #define circuit_mark_for_close(c, reason) \
1545 _circuit_mark_for_close((c), (reason), __LINE__, _SHORT_FILE_)
1547 void assert_cpath_layer_ok(const crypt_path_t *cp);
1548 void assert_circuit_ok(const circuit_t *c);
1549 void circuit_free_all(void);
1551 /********************************* circuituse.c ************************/
1553 void circuit_expire_building(time_t now);
1554 void circuit_remove_handled_ports(smartlist_t *needed_ports);
1555 int circuit_stream_is_being_handled(connection_t *conn, uint16_t port,
1556 int min);
1557 void circuit_build_needed_circs(time_t now);
1558 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1559 void circuit_about_to_close_connection(connection_t *conn);
1560 void circuit_has_opened(circuit_t *circ);
1561 void circuit_build_failed(circuit_t *circ);
1562 circuit_t *circuit_launch_by_nickname(uint8_t purpose,
1563 const char *exit_nickname,
1564 int need_uptime, int need_capacity,
1565 int is_internal);
1566 circuit_t *circuit_launch_by_extend_info(uint8_t purpose,
1567 extend_info_t *info,
1568 int need_uptime, int need_capacity,
1569 int is_internal);
1570 circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
1571 int need_uptime, int need_capacity,
1572 int is_internal);
1573 void circuit_reset_failure_count(int timeout);
1574 int connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
1575 circuit_t *circ);
1576 int connection_ap_handshake_attach_circuit(connection_t *conn);
1578 /********************************* command.c ***************************/
1580 void command_process_cell(cell_t *cell, connection_t *conn);
1582 extern uint64_t stats_n_padding_cells_processed;
1583 extern uint64_t stats_n_create_cells_processed;
1584 extern uint64_t stats_n_created_cells_processed;
1585 extern uint64_t stats_n_relay_cells_processed;
1586 extern uint64_t stats_n_destroy_cells_processed;
1588 /********************************* config.c ***************************/
1590 or_options_t *get_options(void);
1591 int set_options(or_options_t *new_val);
1592 void config_free_all(void);
1593 const char *safe_str(const char *address);
1594 const char *escaped_safe_str(const char *address);
1596 int config_get_lines(char *string, config_line_t **result);
1597 void config_free_lines(config_line_t *front);
1598 int options_trial_assign(config_line_t *list, int use_defaults,
1599 int clear_first);
1600 int resolve_my_address(or_options_t *options, uint32_t *addr,
1601 char **hostname_out);
1602 void options_init(or_options_t *options);
1603 int options_init_from_torrc(int argc, char **argv);
1604 int options_init_logs(or_options_t *options, int validate_only);
1605 int config_parse_exit_policy(config_line_t *cfg,
1606 addr_policy_t **dest,
1607 int rejectprivate);
1608 int config_parse_addr_policy(config_line_t *cfg,
1609 addr_policy_t **dest,
1610 int assume_action);
1611 int config_cmp_addr_policies(addr_policy_t *a, addr_policy_t *b);
1612 void addr_policy_free(addr_policy_t *p);
1613 int option_is_recognized(const char *key);
1614 const char *option_get_canonical_name(const char *key);
1615 config_line_t *option_get_assignment(or_options_t *options,
1616 const char *key);
1617 char *options_dump(or_options_t *options, int minimal);
1618 int options_save_current(void);
1619 const char *get_torrc_fname(void);
1621 or_state_t *get_or_state(void);
1622 int or_state_load(void);
1623 int or_state_save(void);
1625 int config_getinfo_helper(const char *question, char **answer);
1627 int firewall_is_fascist_or(void);
1628 int fascist_firewall_allows_address_or(uint32_t addr, uint16_t port);
1629 int fascist_firewall_allows_address_dir(uint32_t addr, uint16_t port);
1631 /********************************* connection.c ***************************/
1633 const char *conn_type_to_string(int type);
1634 const char *conn_state_to_string(int type, int state);
1636 connection_t *connection_new(int type);
1637 void connection_unregister(connection_t *conn);
1638 void connection_free(connection_t *conn);
1639 void connection_free_all(void);
1640 void connection_about_to_close_connection(connection_t *conn);
1641 void connection_close_immediate(connection_t *conn);
1642 void _connection_mark_for_close(connection_t *conn,int line, const char *file);
1644 #define connection_mark_for_close(c) \
1645 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1647 void connection_expire_held_open(void);
1649 int connection_connect(connection_t *conn, char *address, uint32_t addr,
1650 uint16_t port);
1651 int retry_all_listeners(int force, smartlist_t *replaced_conns,
1652 smartlist_t *new_conns);
1654 int connection_bucket_write_limit(connection_t *conn);
1655 int global_write_bucket_empty(void);
1656 void connection_bucket_init(void);
1657 void connection_bucket_refill(struct timeval *now);
1659 int connection_handle_read(connection_t *conn);
1661 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1663 int connection_wants_to_flush(connection_t *conn);
1664 int connection_outbuf_too_full(connection_t *conn);
1665 int connection_handle_write(connection_t *conn);
1666 void _connection_controller_force_write(connection_t *conn);
1667 void connection_write_to_buf(const char *string, size_t len,
1668 connection_t *conn);
1670 connection_t *connection_or_exact_get_by_addr_port(uint32_t addr,
1671 uint16_t port);
1672 connection_t *connection_get_by_global_id(uint32_t id);
1674 connection_t *connection_get_by_type(int type);
1675 connection_t *connection_get_by_type_purpose(int type, int purpose);
1676 connection_t *connection_get_by_type_addr_port_purpose(int type, uint32_t addr,
1677 uint16_t port, int purpose);
1678 connection_t *connection_get_by_type_state(int type, int state);
1679 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1680 connection_t *connection_get_by_type_state_rendquery(int type, int state,
1681 const char *rendquery);
1683 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1684 #define connection_has_pending_tls_data(conn) \
1685 ((conn)->type == CONN_TYPE_OR && \
1686 (conn)->state == OR_CONN_STATE_OPEN && \
1687 tor_tls_get_pending_bytes((conn)->tls))
1688 int connection_is_listener(connection_t *conn);
1689 int connection_state_is_open(connection_t *conn);
1690 int connection_state_is_connecting(connection_t *conn);
1692 char *alloc_http_authenticator(const char *authenticator);
1694 void assert_connection_ok(connection_t *conn, time_t now);
1695 int connection_or_nonopen_was_started_here(connection_t *conn);
1697 /********************************* connection_edge.c *************************/
1699 #define connection_mark_unattached_ap(conn, endreason) \
1700 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1702 void _connection_mark_unattached_ap(connection_t *conn, int endreason,
1703 int line, const char *file);
1704 int connection_edge_reached_eof(connection_t *conn);
1705 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1706 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1707 int connection_edge_end(connection_t *conn, char reason,
1708 crypt_path_t *cpath_layer);
1709 int connection_edge_end_errno(connection_t *conn, crypt_path_t *cpath_layer);
1710 int connection_edge_finished_flushing(connection_t *conn);
1711 int connection_edge_finished_connecting(connection_t *conn);
1713 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1714 int connection_ap_handshake_send_resolve(connection_t *ap_conn,
1715 circuit_t *circ);
1717 int connection_ap_make_bridge(char *address, uint16_t port);
1718 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1719 size_t replylen,
1720 socks5_reply_status_t status);
1721 void connection_ap_handshake_socks_resolved(connection_t *conn,
1722 int answer_type,
1723 size_t answer_len,
1724 const char *answer,
1725 int ttl);
1727 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1728 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1729 void connection_exit_connect(connection_t *conn);
1730 int connection_edge_is_rendezvous_stream(connection_t *conn);
1731 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1732 void connection_ap_expire_beginning(void);
1733 void connection_ap_attach_pending(void);
1734 int connection_ap_detach_retriable(connection_t *conn, circuit_t *circ);
1736 void addressmap_init(void);
1737 void addressmap_clean(time_t now);
1738 void addressmap_clear_configured(void);
1739 void addressmap_clear_transient(void);
1740 void addressmap_free_all(void);
1741 void addressmap_rewrite(char *address, size_t maxlen);
1742 int addressmap_already_mapped(const char *address);
1743 void addressmap_register(const char *address, char *new_address,
1744 time_t expires);
1745 int client_dns_incr_failures(const char *address);
1746 void client_dns_clear_failures(const char *address);
1747 void client_dns_set_addressmap(const char *address, uint32_t val,
1748 const char *exitname, int ttl);
1749 int address_is_in_virtual_range(const char *addr);
1750 const char *addressmap_register_virtual_address(int type, char *new_address);
1751 void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
1752 time_t max_expires);
1754 connection_ap_handshake_rewrite_and_attach(connection_t *conn);
1756 void parse_socks_policy(void);
1757 void free_socks_policy(void);
1758 int socks_policy_permits_address(uint32_t addr);
1760 void set_exit_redirects(smartlist_t *lst);
1761 typedef enum hostname_type_t {
1762 NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME, BAD_HOSTNAME
1763 } hostname_type_t;
1764 hostname_type_t parse_extended_hostname(char *address);
1766 /********************************* connection_or.c ***************************/
1768 void connection_or_remove_from_identity_map(connection_t *conn);
1769 void connection_or_clear_identity_map(void);
1770 connection_t *connection_or_get_by_identity_digest(const char *digest);
1772 int connection_or_reached_eof(connection_t *conn);
1773 int connection_or_process_inbuf(connection_t *conn);
1774 int connection_or_finished_flushing(connection_t *conn);
1775 int connection_or_finished_connecting(connection_t *conn);
1777 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1778 const char *id_digest);
1780 int connection_tls_start_handshake(connection_t *conn, int receiving);
1781 int connection_tls_continue_handshake(connection_t *conn);
1783 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1784 int connection_or_send_destroy(uint16_t circ_id, connection_t *conn,
1785 int reason);
1787 /********************************* control.c ***************************/
1789 typedef enum circuit_status_event_t {
1790 CIRC_EVENT_LAUNCHED = 0,
1791 CIRC_EVENT_BUILT = 1,
1792 CIRC_EVENT_EXTENDED = 2,
1793 CIRC_EVENT_FAILED = 3,
1794 CIRC_EVENT_CLOSED = 4,
1795 } circuit_status_event_t;
1797 typedef enum stream_status_event_t {
1798 STREAM_EVENT_SENT_CONNECT = 0,
1799 STREAM_EVENT_SENT_RESOLVE = 1,
1800 STREAM_EVENT_SUCCEEDED = 2,
1801 STREAM_EVENT_FAILED = 3,
1802 STREAM_EVENT_CLOSED = 4,
1803 STREAM_EVENT_NEW = 5,
1804 STREAM_EVENT_NEW_RESOLVE = 6,
1805 STREAM_EVENT_FAILED_RETRIABLE = 7
1806 } stream_status_event_t;
1808 typedef enum or_conn_status_event_t {
1809 OR_CONN_EVENT_LAUNCHED = 0,
1810 OR_CONN_EVENT_CONNECTED = 1,
1811 OR_CONN_EVENT_FAILED = 2,
1812 OR_CONN_EVENT_CLOSED = 3,
1813 } or_conn_status_event_t;
1815 void control_update_global_event_mask(void);
1816 void control_adjust_event_log_severity(void);
1818 /** Execute the statement <b>stmt</b>, which may log events concerning the
1819 * connection <b>conn</b>. To prevent infinite loops, disable log messages
1820 * being stent to controllers if <b>conn</b> is a control connection.
1822 * Stmt must not contain any return or goto statements.
1824 #define CONN_LOG_PROTECT(conn, stmt) \
1825 do { \
1826 int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
1827 if (_log_conn_is_control) \
1828 disable_control_logging(); \
1829 do {stmt;} while (0); \
1830 if (_log_conn_is_control) \
1831 enable_control_logging(); \
1832 } while (0)
1834 /** Log information about the connection <b>conn</b>, protecting it as with
1835 * CONN_LOG_PROTECT. Example:
1837 * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
1839 #define LOG_FN_CONN(conn, args) \
1840 CONN_LOG_PROTECT(conn, log_fn args)
1842 int connection_control_finished_flushing(connection_t *conn);
1843 int connection_control_reached_eof(connection_t *conn);
1844 int connection_control_process_inbuf(connection_t *conn);
1846 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1847 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1848 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1849 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1850 void control_event_logmsg(int severity, unsigned int domain, const char *msg);
1851 int control_event_descriptors_changed(smartlist_t *routers);
1852 int control_event_address_mapped(const char *from, const char *to,
1853 time_t expires);
1854 int control_event_or_authdir_new_descriptor(const char *action,
1855 const char *descriptor,
1856 const char *msg);
1858 int init_cookie_authentication(int enabled);
1859 int decode_hashed_password(char *buf, const char *hashed);
1860 void disable_control_logging(void);
1861 void enable_control_logging(void);
1863 /********************************* cpuworker.c *****************************/
1865 void cpu_init(void);
1866 void cpuworkers_rotate(void);
1867 int connection_cpu_finished_flushing(connection_t *conn);
1868 int connection_cpu_reached_eof(connection_t *conn);
1869 int connection_cpu_process_inbuf(connection_t *conn);
1870 int assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
1871 void *task);
1873 /********************************* directory.c ***************************/
1875 int dir_policy_permits_address(uint32_t addr);
1876 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1877 size_t payload_len);
1878 void directory_get_from_dirserver(uint8_t purpose, const char *resource,
1879 int retry_if_no_servers);
1880 void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
1881 int private_connection,
1882 const char *resource,
1883 const char *payload,
1884 size_t payload_len);
1885 void directory_initiate_command_routerstatus(routerstatus_t *status,
1886 uint8_t purpose,
1887 int private_connection,
1888 const char *resource,
1889 const char *payload,
1890 size_t payload_len);
1892 int parse_http_response(const char *headers, int *code, time_t *date,
1893 int *compression, char **response);
1895 int connection_dir_reached_eof(connection_t *conn);
1896 int connection_dir_process_inbuf(connection_t *conn);
1897 int connection_dir_finished_flushing(connection_t *conn);
1898 int connection_dir_finished_connecting(connection_t *conn);
1899 void connection_dir_request_failed(connection_t *conn);
1900 void parse_dir_policy(void);
1901 void free_dir_policy(void);
1902 int dir_split_resource_into_fingerprints(const char *resource,
1903 smartlist_t *fp_out, int *compresseed_out,
1904 int decode_hex);
1905 char *directory_dump_request_log(void);
1907 /********************************* dirserv.c ***************************/
1909 void parse_authdir_policy(void);
1910 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1911 int dirserv_parse_fingerprint_file(const char *fname);
1912 void dirserv_free_fingerprint_list(void);
1913 const char *dirserv_get_nickname_by_digest(const char *digest);
1914 int dirserv_add_descriptor(const char *desc, const char **msg);
1915 char *dirserver_getinfo_unregistered(const char *question);
1916 void dirserv_free_descriptors(void);
1917 int dirserv_thinks_router_is_blatantly_unreachable(routerinfo_t *router,
1918 time_t now);
1919 int list_server_status(smartlist_t *routers, char **router_status_out);
1920 int dirserv_dump_directory_to_string(char **dir_out,
1921 crypto_pk_env_t *private_key);
1922 void directory_set_dirty(void);
1923 size_t dirserv_get_directory(const char **cp, int compress);
1924 size_t dirserv_get_runningrouters(const char **rr, int compress);
1925 void dirserv_set_cached_directory(const char *directory, time_t when,
1926 int is_running_routers);
1927 void dirserv_set_cached_networkstatus_v2(const char *directory,
1928 const char *identity,
1929 time_t published);
1930 int dirserv_get_networkstatus_v2(smartlist_t *result, const char *key);
1931 int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
1932 const char **msg);
1933 void dirserv_orconn_tls_done(const char *address,
1934 uint16_t or_port,
1935 const char *digest_rcvd,
1936 const char *nickname,
1937 int as_advertised);
1938 int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
1939 int complain);
1940 int dirserv_would_reject_router(routerstatus_t *rs);
1941 void dirserv_free_all(void);
1943 /********************************* dns.c ***************************/
1945 void dns_init(void);
1946 void dns_free_all(void);
1947 int connection_dns_finished_flushing(connection_t *conn);
1948 int connection_dns_reached_eof(connection_t *conn);
1949 int connection_dns_process_inbuf(connection_t *conn);
1950 void dnsworkers_rotate(void);
1951 void connection_dns_remove(connection_t *conn);
1952 void assert_connection_edge_not_dns_pending(connection_t *conn);
1953 void assert_all_pending_dns_resolves_ok(void);
1954 void dns_cancel_pending_resolve(char *question);
1955 int dns_resolve(connection_t *exitconn);
1957 /********************************* hibernate.c **********************/
1959 int accounting_parse_options(or_options_t *options, int validate_only);
1960 int accounting_is_enabled(or_options_t *options);
1961 void configure_accounting(time_t now);
1962 void accounting_run_housekeeping(time_t now);
1963 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1964 int accounting_record_bandwidth_usage(time_t now);
1965 void hibernate_begin_shutdown(void);
1966 int we_are_hibernating(void);
1967 void consider_hibernation(time_t now);
1968 int accounting_getinfo_helper(const char *question, char **answer);
1969 void accounting_set_bandwidth_usage_from_state(or_state_t *state);
1971 /********************************* main.c ***************************/
1973 int connection_add(connection_t *conn);
1974 int connection_remove(connection_t *conn);
1975 int connection_in_array(connection_t *conn);
1976 void add_connection_to_closeable_list(connection_t *conn);
1977 int connection_is_on_closeable_list(connection_t *conn);
1979 void get_connection_array(connection_t ***array, int *n);
1981 void connection_watch_events(connection_t *conn, short events);
1982 int connection_is_reading(connection_t *conn);
1983 void connection_stop_reading(connection_t *conn);
1984 void connection_start_reading(connection_t *conn);
1986 int connection_is_writing(connection_t *conn);
1987 void connection_stop_writing(connection_t *conn);
1988 void connection_start_writing(connection_t *conn);
1990 void directory_all_unreachable(time_t now);
1991 void directory_info_has_arrived(time_t now, int from_cache);
1993 int control_signal_act(int the_signal);
1994 void handle_signals(int is_parent);
1995 void tor_cleanup(void);
1996 void tor_free_all(int postfork);
1998 int tor_main(int argc, char *argv[]);
2000 /********************************* onion.c ***************************/
2002 int onion_pending_add(circuit_t *circ);
2003 circuit_t *onion_next_task(void);
2004 void onion_pending_remove(circuit_t *circ);
2006 int onion_skin_create(crypto_pk_env_t *router_key,
2007 crypto_dh_env_t **handshake_state_out,
2008 char *onion_skin_out);
2010 int onion_skin_server_handshake(const char *onion_skin,
2011 crypto_pk_env_t *private_key,
2012 crypto_pk_env_t *prev_private_key,
2013 char *handshake_reply_out,
2014 char *key_out,
2015 size_t key_out_len);
2017 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
2018 const char *handshake_reply,
2019 char *key_out,
2020 size_t key_out_len);
2022 int fast_server_handshake(const char *key_in,
2023 char *handshake_reply_out,
2024 char *key_out,
2025 size_t key_out_len);
2027 int fast_client_handshake(const char *handshake_state,
2028 const char *handshake_reply_out,
2029 char *key_out,
2030 size_t key_out_len);
2032 void clear_pending_onions(void);
2034 /********************************* relay.c ***************************/
2036 extern uint64_t stats_n_relay_cells_relayed;
2037 extern uint64_t stats_n_relay_cells_delivered;
2039 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
2040 int cell_direction);
2042 void relay_header_pack(char *dest, const relay_header_t *src);
2043 void relay_header_unpack(relay_header_t *dest, const char *src);
2044 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
2045 int relay_command, const char *payload,
2046 size_t payload_len,
2047 crypt_path_t *cpath_layer);
2048 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
2049 void connection_edge_consider_sending_sendme(connection_t *conn);
2050 socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
2051 int errno_to_end_reason(int e);
2053 extern uint64_t stats_n_data_cells_packaged;
2054 extern uint64_t stats_n_data_bytes_packaged;
2055 extern uint64_t stats_n_data_cells_received;
2056 extern uint64_t stats_n_data_bytes_received;
2058 /********************************* rephist.c ***************************/
2060 void rep_hist_init(void);
2061 void rep_hist_note_connect_failed(const char* nickname, time_t when);
2062 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
2063 void rep_hist_note_disconnect(const char* nickname, time_t when);
2064 void rep_hist_note_connection_died(const char* nickname, time_t when);
2065 void rep_hist_note_extend_succeeded(const char *from_name,
2066 const char *to_name);
2067 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
2068 void rep_hist_dump_stats(time_t now, int severity);
2069 void rep_hist_note_bytes_read(int num_bytes, time_t when);
2070 void rep_hist_note_bytes_written(int num_bytes, time_t when);
2071 int rep_hist_bandwidth_assess(void);
2072 char *rep_hist_get_bandwidth_lines(void);
2073 void rep_history_clean(time_t before);
2075 void rep_hist_note_used_port(uint16_t port, time_t now);
2076 smartlist_t *rep_hist_get_predicted_ports(time_t now);
2077 void rep_hist_note_used_resolve(time_t now);
2078 void rep_hist_note_used_internal(time_t now, int need_uptime,
2079 int need_capacity);
2080 int rep_hist_get_predicted_internal(time_t now, int *need_uptime,
2081 int *need_capacity);
2083 void rep_hist_update_state(or_state_t *state);
2084 int rep_hist_load_state(or_state_t *state, const char **err);
2086 void rep_hist_free_all(void);
2088 /********************************* rendclient.c ***************************/
2090 void rend_client_introcirc_has_opened(circuit_t *circ);
2091 void rend_client_rendcirc_has_opened(circuit_t *circ);
2092 int rend_client_introduction_acked(circuit_t *circ, const char *request,
2093 size_t request_len);
2094 void rend_client_refetch_renddesc(const char *query);
2095 int rend_client_remove_intro_point(extend_info_t *failed_intro,
2096 const char *query);
2097 int rend_client_rendezvous_acked(circuit_t *circ, const char *request,
2098 size_t request_len);
2099 int rend_client_receive_rendezvous(circuit_t *circ, const char *request,
2100 size_t request_len);
2101 void rend_client_desc_here(const char *query);
2103 extend_info_t *rend_client_get_random_intro(const char *query);
2105 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
2107 /********************************* rendcommon.c ***************************/
2109 /** Information used to connect to a hidden service. */
2110 typedef struct rend_service_descriptor_t {
2111 crypto_pk_env_t *pk; /**< This service's public key. */
2112 int version; /**< 0 or 1. */
2113 time_t timestamp; /**< Time when the descriptor was generated. */
2114 uint16_t protocols; /**< Bitmask: which rendezvous protocols are supported?
2115 * (We allow bits '0', '1', and '2' to be set.) */
2116 int n_intro_points; /**< Number of introduction points. */
2117 /** Array of n_intro_points elements for this service's introduction points'
2118 * nicknames. Elements are removed from this array if introduction attempts
2119 * fail. */
2120 char **intro_points;
2121 /** Array of n_intro_points elements for this service's introduction points'
2122 * extend_infos, or NULL if this descriptor is V0. Elements are removed
2123 * from this array if introduction attempts fail. If this array is present,
2124 * its elements correspond to the elements of intro_points. */
2125 extend_info_t **intro_point_extend_info;
2126 } rend_service_descriptor_t;
2128 int rend_cmp_service_ids(const char *one, const char *two);
2130 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
2131 const char *payload);
2133 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
2134 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
2135 int version,
2136 crypto_pk_env_t *key,
2137 char **str_out,
2138 size_t *len_out);
2139 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str,
2140 size_t len);
2141 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
2143 /** A cached rendezvous descriptor. */
2144 typedef struct rend_cache_entry_t {
2145 size_t len; /** Length of <b>desc</b> */
2146 time_t received; /** When was the descriptor received? */
2147 char *desc; /** Service descriptor */
2148 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
2149 } rend_cache_entry_t;
2151 void rend_cache_init(void);
2152 void rend_cache_clean(void);
2153 void rend_cache_free_all(void);
2154 int rend_valid_service_id(const char *query);
2155 int rend_cache_lookup_desc(const char *query, int version, const char **desc,
2156 size_t *desc_len);
2157 int rend_cache_lookup_entry(const char *query, int version,
2158 rend_cache_entry_t **entry_out);
2159 int rend_cache_store(const char *desc, size_t desc_len);
2161 /********************************* rendservice.c ***************************/
2163 int num_rend_services(void);
2164 int rend_config_services(or_options_t *options, int validate_only);
2165 int rend_service_load_keys(void);
2166 void rend_services_init(void);
2167 void rend_services_introduce(void);
2168 void rend_consider_services_upload(time_t now);
2170 void rend_service_intro_has_opened(circuit_t *circuit);
2171 int rend_service_intro_established(circuit_t *circuit, const char *request,
2172 size_t request_len);
2173 void rend_service_rendezvous_has_opened(circuit_t *circuit);
2174 int rend_service_introduce(circuit_t *circuit, const char *request,
2175 size_t request_len);
2176 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
2177 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
2178 void rend_service_dump_stats(int severity);
2179 void rend_service_free_all(void);
2181 /********************************* rendmid.c *******************************/
2182 int rend_mid_establish_intro(circuit_t *circ, const char *request,
2183 size_t request_len);
2184 int rend_mid_introduce(circuit_t *circ, const char *request,
2185 size_t request_len);
2186 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request,
2187 size_t request_len);
2188 int rend_mid_rendezvous(circuit_t *circ, const char *request,
2189 size_t request_len);
2191 /********************************* router.c ***************************/
2192 typedef enum {
2193 ADDR_POLICY_ACCEPTED=0,
2194 ADDR_POLICY_REJECTED=-1,
2195 ADDR_POLICY_PROBABLY_ACCEPTED=1,
2196 ADDR_POLICY_PROBABLY_REJECTED=2
2197 } addr_policy_result_t;
2199 void set_onion_key(crypto_pk_env_t *k);
2200 crypto_pk_env_t *get_onion_key(void);
2201 time_t get_onion_key_set_at(void);
2202 void set_identity_key(crypto_pk_env_t *k);
2203 crypto_pk_env_t *get_identity_key(void);
2204 int identity_key_is_set(void);
2205 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
2206 void rotate_onion_key(void);
2207 crypto_pk_env_t *init_key_from_file(const char *fname);
2208 int init_keys(void);
2210 int check_whether_orport_reachable(void);
2211 int check_whether_dirport_reachable(void);
2212 void consider_testing_reachability(void);
2213 void router_orport_found_reachable(void);
2214 void router_dirport_found_reachable(void);
2215 void server_has_changed_ip(void);
2216 void consider_publishable_server(time_t now, int force);
2218 int authdir_mode(or_options_t *options);
2219 int clique_mode(or_options_t *options);
2220 int server_mode(or_options_t *options);
2221 int advertised_server_mode(void);
2222 int proxy_mode(or_options_t *options);
2224 void router_retry_connections(int testing_reachability, int try_all);
2225 int router_is_clique_mode(routerinfo_t *router);
2226 void router_upload_dir_desc_to_dirservers(int force);
2227 void mark_my_descriptor_dirty_if_older_than(time_t when);
2228 void mark_my_descriptor_dirty(void);
2229 void check_descriptor_bandwidth_changed(time_t now);
2230 void check_descriptor_ipaddress_changed(time_t now);
2231 int router_compare_to_my_exit_policy(connection_t *conn);
2232 routerinfo_t *router_get_my_routerinfo(void);
2233 const char *router_get_my_descriptor(void);
2234 int router_digest_is_me(const char *digest);
2235 int router_is_me(routerinfo_t *router);
2236 int router_fingerprint_is_me(const char *fp);
2237 int router_rebuild_descriptor(int force);
2238 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
2239 crypto_pk_env_t *ident_key);
2240 int is_legal_nickname(const char *s);
2241 int is_legal_nickname_or_hexdigest(const char *s);
2242 void router_reset_warnings(void);
2243 void router_free_all(void);
2245 /********************************* routerlist.c ***************************/
2247 /** Represents information about a single trusted directory server. */
2248 typedef struct trusted_dir_server_t {
2249 char *description;
2250 char *nickname;
2251 char *address; /**< Hostname */
2252 uint32_t addr; /**< IPv4 address */
2253 uint16_t dir_port; /**< Directory port */
2254 char digest[DIGEST_LEN]; /**< Digest of identity key */
2255 unsigned int is_running:1; /**< True iff we think this server is running. */
2256 /** True iff this server is an authority for the older ("v1") directory
2257 * protocol. (All authorities are v2 authorities.) */
2258 unsigned int is_v1_authority:1;
2259 int n_networkstatus_failures; /**< How many times have we asked for this
2260 * server's network-status unsuccessfully? */
2261 routerstatus_t fake_status; /**< Used when we need to pass this trusted
2262 * dir_server_t to directory_initiate_command_*
2263 * as a routerstatus_t. Not updated by the
2264 * router-status management code!
2266 } trusted_dir_server_t;
2268 int router_reload_router_list(void);
2269 int router_reload_networkstatus(void);
2270 void router_get_trusted_dir_servers(smartlist_t **outp);
2271 routerstatus_t *router_pick_directory_server(int requireother,
2272 int fascistfirewall,
2273 int for_v2_directory,
2274 int retry_if_no_servers);
2275 routerstatus_t *router_pick_trusteddirserver(int need_v1_authority,
2276 int requireother,
2277 int fascistfirewall,
2278 int retry_if_no_servers);
2279 trusted_dir_server_t *router_get_trusteddirserver_by_digest(
2280 const char *digest);
2281 void routerlist_add_family(smartlist_t *sl, routerinfo_t *router);
2282 void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
2283 int must_be_running,
2284 int warn_if_down, int warn_if_unnamed);
2285 routerinfo_t *routerlist_find_my_routerinfo(void);
2286 routerinfo_t *router_find_exact_exit_enclave(const char *address,
2287 uint16_t port);
2289 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
2290 int router_is_unreliable(routerinfo_t *router, int need_uptime,
2291 int need_capacity, int need_guard);
2292 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
2293 routerinfo_t *router_choose_random_node(const char *preferred,
2294 const char *excluded,
2295 smartlist_t *excludedsmartlist,
2296 int need_uptime, int need_bandwidth,
2297 int need_guard,
2298 int allow_unverified, int strict);
2299 routerinfo_t *router_get_by_nickname(const char *nickname,
2300 int warn_if_unnamed);
2301 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
2302 routerinfo_t *router_get_by_digest(const char *digest);
2303 signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
2304 const char *signed_descriptor_get_body(signed_descriptor_t *desc);
2305 int router_digest_version_as_new_as(const char *digest, const char *cutoff);
2306 int router_digest_is_trusted_dir(const char *digest);
2307 routerlist_t *router_get_routerlist(void);
2308 void routerlist_reset_warnings(void);
2309 void routerlist_free(routerlist_t *routerlist);
2310 void dump_routerlist_mem_usage(int severity);
2311 void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int idx,
2312 int make_old);
2313 void routerinfo_free(routerinfo_t *router);
2314 void routerstatus_free(routerstatus_t *routerstatus);
2315 void networkstatus_free(networkstatus_t *networkstatus);
2316 void routerlist_free_all(void);
2317 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
2318 void router_mark_as_down(const char *digest);
2319 void routerlist_remove_old_routers(void);
2320 void networkstatus_list_clean(time_t now);
2321 int router_add_to_routerlist(routerinfo_t *router, const char **msg,
2322 int from_cache, int from_fetch);
2323 int router_load_single_router(const char *s, const char **msg);
2324 void router_load_routers_from_string(const char *s, int from_cache,
2325 smartlist_t *requested_fingerprints);
2326 typedef enum {
2327 NS_FROM_CACHE, NS_FROM_DIR, NS_GENERATED
2328 } networkstatus_source_t;
2329 int router_set_networkstatus(const char *s, time_t arrived_at,
2330 networkstatus_source_t source,
2331 smartlist_t *requested_fingerprints);
2332 addr_policy_result_t router_compare_addr_to_addr_policy(uint32_t addr,
2333 uint16_t port, addr_policy_t *policy);
2335 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
2336 int need_uptime);
2338 int router_exit_policy_rejects_all(routerinfo_t *router);
2339 void add_trusted_dir_server(const char *nickname,
2340 const char *address, uint16_t port,
2341 const char *digest, int supports_v1);
2342 void clear_trusted_dir_servers(void);
2343 networkstatus_t *networkstatus_get_by_digest(const char *digest);
2344 local_routerstatus_t *router_get_combined_status_by_digest(const char *digest);
2345 void update_networkstatus_downloads(time_t now);
2346 void update_router_descriptor_downloads(time_t now);
2347 void routers_update_all_from_networkstatus(void);
2348 void routers_update_status_from_networkstatus(smartlist_t *routers,
2349 int reset_failures);
2350 smartlist_t *router_list_superseded(void);
2351 int router_have_minimum_dir_info(void);
2352 void networkstatus_list_update_recent(time_t now);
2353 void router_reset_descriptor_download_failures(void);
2354 void router_reset_status_download_failures(void);
2355 int router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2);
2357 /********************************* routerparse.c ************************/
2359 #define MAX_STATUS_TAG_LEN 32
2360 /** Structure to hold parsed Tor versions. This is a little messier
2361 * than we would like it to be, because we changed version schemes with 0.1.0.
2363 * See version-spec.txt for the whole business.
2365 typedef struct tor_version_t {
2366 int major;
2367 int minor;
2368 int micro;
2369 /** Release status. For version in the post-0.1 format, this is always
2370 * VER_RELEASE. */
2371 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
2372 int patchlevel;
2373 /** CVS status. For version in the post-0.1 format, this is always
2374 * IS_NOT_CVS */
2375 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
2376 char status_tag[MAX_STATUS_TAG_LEN];
2377 } tor_version_t;
2379 typedef enum version_status_t {
2380 VS_RECOMMENDED=0, /**< This version is listed as recommended. */
2381 VS_OLD=1, /**< This version is older than any recommended version. */
2382 VS_NEW=2, /**< This version is newer than any recommended version. */
2383 VS_NEW_IN_SERIES=3, /**< This version is newer than any recommended version
2384 * in its series, and such recommended versions exist. */
2385 VS_UNRECOMMENDED=4 /**< This version is not recommended (general case) */
2386 } version_status_t;
2388 int router_get_router_hash(const char *s, char *digest);
2389 int router_get_dir_hash(const char *s, char *digest);
2390 int router_get_runningrouters_hash(const char *s, char *digest);
2391 int router_get_networkstatus_v2_hash(const char *s, char *digest);
2392 int router_append_dirobj_signature(char *buf, size_t buf_len,
2393 const char *digest,
2394 crypto_pk_env_t *private_key);
2395 int router_parse_list_from_string(const char **s,
2396 smartlist_t *dest);
2397 int router_parse_routerlist_from_directory(const char *s,
2398 routerlist_t **dest,
2399 crypto_pk_env_t *pkey,
2400 int check_version,
2401 int write_to_cache);
2402 int router_parse_runningrouters(const char *str);
2403 int router_parse_directory(const char *str);
2404 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
2405 addr_policy_t *router_parse_addr_policy_from_string(const char *s,
2406 int assume_action);
2407 version_status_t tor_version_is_obsolete(const char *myversion,
2408 const char *versionlist);
2409 version_status_t version_status_join(version_status_t a, version_status_t b);
2410 int tor_version_parse(const char *s, tor_version_t *out);
2411 int tor_version_as_new_as(const char *platform, const char *cutoff);
2412 int tor_version_compare(tor_version_t *a, tor_version_t *b);
2413 void sort_version_list(smartlist_t *lst);
2414 void assert_addr_policy_ok(addr_policy_t *t);
2416 networkstatus_t *networkstatus_parse_from_string(const char *s);
2418 #endif