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