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