Fix for 152: reject malformed .onion addresses rather then passing them on
[tor.git] / src / or / or.h
blob916476cf7edb2feca2042bf53b935c6f424234d5
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 DEFAULT_BANDWIDTH_OP (1024 * 1000)
150 #define MAX_NICKNAME_LEN 19
151 /* Hex digest plus dollar sign. */
152 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
153 /** Maximum size, in bytes, for resized buffers. */
154 #define MAX_BUF_SIZE ((1<<24)-1)
155 #define MAX_DIR_SIZE MAX_BUF_SIZE
157 /* For http parsing */
158 #define MAX_HEADERS_SIZE 50000
159 #define MAX_BODY_SIZE 500000
161 #ifdef TOR_PERF
162 /** How long do we keep DNS cache entries before purging them? */
163 #define MAX_DNS_ENTRY_AGE (150*60)
164 #else
165 #define MAX_DNS_ENTRY_AGE (15*60)
166 #endif
168 /** How often do we rotate onion keys? */
169 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
170 /** How often do we rotate TLS contexts? */
171 #define MAX_SSL_KEY_LIFETIME (120*60)
173 /** How old do we allow a router to get before removing it, either
174 * from the descriptor list (for dirservers) or the router list (for others)?
175 * In seconds. */
176 #define ROUTER_MAX_AGE (60*60*24)
178 typedef enum {
179 CIRC_ID_TYPE_LOWER=0,
180 CIRC_ID_TYPE_HIGHER=1
181 } circ_id_type_t;
183 #define _CONN_TYPE_MIN 3
184 /** Type for sockets listening for OR connections. */
185 #define CONN_TYPE_OR_LISTENER 3
186 /** Type for OR-to-OR or OP-to-OR connections. */
187 #define CONN_TYPE_OR 4
188 /** Type for connections from final OR to chosen destination. */
189 #define CONN_TYPE_EXIT 5
190 /** Type for sockets listening for SOCKS connections. */
191 #define CONN_TYPE_AP_LISTENER 6
192 /** Type for SOCKS connections to OP. */
193 #define CONN_TYPE_AP 7
194 /** Type for sockets listening for HTTP connections to the directory server. */
195 #define CONN_TYPE_DIR_LISTENER 8
196 /** Type for HTTP connections to the directory server. */
197 #define CONN_TYPE_DIR 9
198 /** Type for connections to local dnsworker processes. */
199 #define CONN_TYPE_DNSWORKER 10
200 /** Type for connections to local cpuworker processes. */
201 #define CONN_TYPE_CPUWORKER 11
202 /** Type for listenting for connections from user interface process */
203 #define CONN_TYPE_CONTROL_LISTENER 12
204 /** Type for connections from user interface process */
205 #define CONN_TYPE_CONTROL 13
206 #define _CONN_TYPE_MAX 13
208 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
210 /** State for any listener connection. */
211 #define LISTENER_STATE_READY 0
213 #define _DNSWORKER_STATE_MIN 1
214 /** State for a connection to a dnsworker process that's idle. */
215 #define DNSWORKER_STATE_IDLE 1
216 /** State for a connection to a dnsworker process that's resolving a hostname. */
217 #define DNSWORKER_STATE_BUSY 2
218 #define _DNSWORKER_STATE_MAX 2
220 #define _CPUWORKER_STATE_MIN 1
221 /** State for a connection to a cpuworker process that's idle. */
222 #define CPUWORKER_STATE_IDLE 1
223 /** State for a connection to a cpuworker process that's processing a
224 * handshake. */
225 #define CPUWORKER_STATE_BUSY_ONION 2
226 #define _CPUWORKER_STATE_MAX 2
228 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
230 #define _OR_CONN_STATE_MIN 1
231 /** State for a connection to an OR: waiting for connect() to finish. */
232 #define OR_CONN_STATE_CONNECTING 1
233 /** State for a connection to an OR: waiting for proxy command to flush. */
234 #define OR_CONN_STATE_PROXY_FLUSHING 2
235 /** State for a connection to an OR: waiting for proxy response. */
236 #define OR_CONN_STATE_PROXY_READING 3
237 /** State for a connection to an OR: SSL is handshaking, not done yet. */
238 #define OR_CONN_STATE_HANDSHAKING 4
239 /** State for a connection to an OR: Ready to send/receive cells. */
240 #define OR_CONN_STATE_OPEN 5
241 #define _OR_CONN_STATE_MAX 5
243 #define _EXIT_CONN_STATE_MIN 1
244 /** State for an exit connection: waiting for response from dns farm. */
245 #define EXIT_CONN_STATE_RESOLVING 1
246 /** State for an exit connection: waiting for connect() to finish. */
247 #define EXIT_CONN_STATE_CONNECTING 2
248 /** State for an exit connection: open and ready to transmit data. */
249 #define EXIT_CONN_STATE_OPEN 3
250 /** State for an exit connection: waiting to be removed. */
251 #define EXIT_CONN_STATE_RESOLVEFAILED 4
252 #define _EXIT_CONN_STATE_MAX 4
253 #if 0
254 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
255 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
256 #endif
258 /* the AP state values must be disjoint from the EXIT state values */
259 #define _AP_CONN_STATE_MIN 5
260 /** State for a SOCKS connection: waiting for SOCKS request. */
261 #define AP_CONN_STATE_SOCKS_WAIT 5
262 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
263 * rendezvous rescriptor. */
264 #define AP_CONN_STATE_RENDDESC_WAIT 6
265 /** The controller will attach this connection to a circuit; it isn't our
266 * job to do so. */
267 #define AP_CONN_STATE_CONTROLLER_WAIT 7
268 /** State for a SOCKS connection: waiting for a completed circuit. */
269 #define AP_CONN_STATE_CIRCUIT_WAIT 8
270 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
271 #define AP_CONN_STATE_CONNECT_WAIT 9
272 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
273 #define AP_CONN_STATE_RESOLVE_WAIT 10
274 /** State for a SOCKS connection: ready to send and receive. */
275 #define AP_CONN_STATE_OPEN 11
276 #define _AP_CONN_STATE_MAX 11
278 #define _DIR_CONN_STATE_MIN 1
279 /** State for connection to directory server: waiting for connect(). */
280 #define DIR_CONN_STATE_CONNECTING 1
281 /** State for connection to directory server: sending HTTP request. */
282 #define DIR_CONN_STATE_CLIENT_SENDING 2
283 /** State for connection to directory server: reading HTTP response. */
284 #define DIR_CONN_STATE_CLIENT_READING 3
285 /** State for connection at directory server: waiting for HTTP request. */
286 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
287 /** State for connection at directory server: sending HTTP response. */
288 #define DIR_CONN_STATE_SERVER_WRITING 5
289 #define _DIR_CONN_STATE_MAX 5
291 #define _CONTROL_CONN_STATE_MIN 1
292 #define CONTROL_CONN_STATE_OPEN 1
293 #define CONTROL_CONN_STATE_NEEDAUTH 2
294 #define _CONTROL_CONN_STATE_MAX 2
296 #define _DIR_PURPOSE_MIN 1
297 /** Purpose for connection to directory server: download a directory. */
298 #define DIR_PURPOSE_FETCH_DIR 1
299 /** Purpose for connection to directory server: download just the list
300 * of running routers. */
301 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
302 /** Purpose for connection to directory server: download a rendezvous
303 * descriptor. */
304 #define DIR_PURPOSE_FETCH_RENDDESC 3
305 /** Purpose for connection to directory server: set after a rendezvous
306 * descriptor is downloaded. */
307 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
308 /** Purpose for connection to directory server: upload a server descriptor. */
309 #define DIR_PURPOSE_UPLOAD_DIR 5
310 /** Purpose for connection to directory server: upload a rendezvous
311 * descriptor. */
312 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
313 /** Purpose for connection at a directory server. */
314 #define DIR_PURPOSE_SERVER 7
315 #define _DIR_PURPOSE_MAX 7
317 #define _EXIT_PURPOSE_MIN 1
318 #define EXIT_PURPOSE_CONNECT 1
319 #define EXIT_PURPOSE_RESOLVE 2
320 #define _EXIT_PURPOSE_MAX 2
322 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
323 #define CIRCUIT_STATE_BUILDING 0
324 /** Circuit state: Waiting to process the onionskin. */
325 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
326 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
327 #define CIRCUIT_STATE_OR_WAIT 2
328 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
329 #define CIRCUIT_STATE_OPEN 3
331 #define _CIRCUIT_PURPOSE_MIN 1
333 /* these circuits were initiated elsewhere */
334 #define _CIRCUIT_PURPOSE_OR_MIN 1
335 /** OR-side circuit purpose: normal circuit, at OR. */
336 #define CIRCUIT_PURPOSE_OR 1
337 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
338 #define CIRCUIT_PURPOSE_INTRO_POINT 2
339 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
340 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
341 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
342 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
343 #define _CIRCUIT_PURPOSE_OR_MAX 4
345 /* these circuits originate at this node */
347 /* here's how circ client-side purposes work:
348 * normal circuits are C_GENERAL.
349 * circuits that are c_introducing are either on their way to
350 * becoming open, or they are open and waiting for a
351 * suitable rendcirc before they send the intro.
352 * circuits that are c_introduce_ack_wait have sent the intro,
353 * but haven't gotten a response yet.
354 * circuits that are c_establish_rend are either on their way
355 * to becoming open, or they are open and have sent the
356 * establish_rendezvous cell but haven't received an ack.
357 * circuits that are c_rend_ready are open and have received a
358 * rend ack, but haven't heard from bob yet. if they have a
359 * buildstate->pending_final_cpath then they're expecting a
360 * cell from bob, else they're not.
361 * circuits that are c_rend_ready_intro_acked are open, and
362 * some intro circ has sent its intro and received an ack.
363 * circuits that are c_rend_joined are open, have heard from
364 * bob, and are talking to him.
366 /** Client-side circuit purpose: Normal circuit, with cpath. */
367 #define CIRCUIT_PURPOSE_C_GENERAL 5
368 /** Client-side circuit purpose: at Alice, connecting to intro point. */
369 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
370 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
371 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
372 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
373 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
374 /** Client-side circuit purpose: at Alice, waiting for ack. */
375 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
376 /** Client-side circuit purpose: at Alice, waiting for Bob. */
377 #define CIRCUIT_PURPOSE_C_REND_READY 10
378 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
379 * has been acknowledged. */
380 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
381 /** Client-side circuit purpose: at Alice, rendezvous established. */
382 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
384 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
385 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
386 /** Hidden-service-side circuit purpose: at Bob, successfully established
387 * intro. */
388 #define CIRCUIT_PURPOSE_S_INTRO 14
389 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
390 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
391 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
392 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
393 /** A testing circuit; not meant to be used for actual traffic. */
394 #define CIRCUIT_PURPOSE_TESTING 17
395 #define _CIRCUIT_PURPOSE_MAX 17
397 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
398 * that this OP has originated. */
399 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
400 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
402 #define RELAY_COMMAND_BEGIN 1
403 #define RELAY_COMMAND_DATA 2
404 #define RELAY_COMMAND_END 3
405 #define RELAY_COMMAND_CONNECTED 4
406 #define RELAY_COMMAND_SENDME 5
407 #define RELAY_COMMAND_EXTEND 6
408 #define RELAY_COMMAND_EXTENDED 7
409 #define RELAY_COMMAND_TRUNCATE 8
410 #define RELAY_COMMAND_TRUNCATED 9
411 #define RELAY_COMMAND_DROP 10
412 #define RELAY_COMMAND_RESOLVE 11
413 #define RELAY_COMMAND_RESOLVED 12
415 #define RELAY_COMMAND_ESTABLISH_INTRO 32
416 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
417 #define RELAY_COMMAND_INTRODUCE1 34
418 #define RELAY_COMMAND_INTRODUCE2 35
419 #define RELAY_COMMAND_RENDEZVOUS1 36
420 #define RELAY_COMMAND_RENDEZVOUS2 37
421 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
422 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
423 #define RELAY_COMMAND_INTRODUCE_ACK 40
425 #define END_STREAM_REASON_MISC 1
426 #define END_STREAM_REASON_RESOLVEFAILED 2
427 #define END_STREAM_REASON_CONNECTREFUSED 3
428 #define END_STREAM_REASON_EXITPOLICY 4
429 #define END_STREAM_REASON_DESTROY 5
430 #define END_STREAM_REASON_DONE 6
431 #define END_STREAM_REASON_TIMEOUT 7
432 /* 8 is unallocated. */
433 #define END_STREAM_REASON_HIBERNATING 9
434 #define END_STREAM_REASON_INTERNAL 10
435 #define END_STREAM_REASON_RESOURCELIMIT 11
436 #define END_STREAM_REASON_CONNRESET 12
437 #define END_STREAM_REASON_TORPROTOCOL 13
439 /* These high-numbered end reasons are not part of the official spec,
440 * and are not intended to be put in relay end cells. They are here
441 * to be more informative when sending back socks replies to the
442 * application. */
443 #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
444 #define END_STREAM_REASON_CANT_ATTACH 257
445 #define END_STREAM_REASON_NET_UNREACHABLE 258
447 #define RESOLVED_TYPE_HOSTNAME 0
448 #define RESOLVED_TYPE_IPV4 4
449 #define RESOLVED_TYPE_IPV6 6
450 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
451 #define RESOLVED_TYPE_ERROR 0xF1
453 /** Length of 'y' portion of 'y.onion' URL. */
454 #define REND_SERVICE_ID_LEN 16
456 #define CELL_DIRECTION_IN 1
457 #define CELL_DIRECTION_OUT 2
459 #ifdef TOR_PERF
460 #define CIRCWINDOW_START 10000
461 #define CIRCWINDOW_INCREMENT 1000
462 #define STREAMWINDOW_START 5000
463 #define STREAMWINDOW_INCREMENT 500
464 #else
465 #define CIRCWINDOW_START 1000
466 #define CIRCWINDOW_INCREMENT 100
467 #define STREAMWINDOW_START 500
468 #define STREAMWINDOW_INCREMENT 50
469 #endif
471 /* cell commands */
472 #define CELL_PADDING 0
473 #define CELL_CREATE 1
474 #define CELL_CREATED 2
475 #define CELL_RELAY 3
476 #define CELL_DESTROY 4
477 #define CELL_CREATE_FAST 5
478 #define CELL_CREATED_FAST 6
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 uint8_t command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
525 * or DESTROY. */
526 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 uint16_t stream_id;
618 struct connection_t *next_stream; /**< Points to the next stream at this
619 * edge, if any (Edge only). */
620 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
621 * this conn exits at. (Edge only.) */
622 int package_window; /**< How many more relay cells can i send into the
623 * circuit? (Edge only.) */
624 int deliver_window; /**< How many more relay cells can end at me? (Edge
625 * only.) */
627 int done_sending; /**< For half-open connections; not used currently. */
628 int done_receiving; /**< For half-open connections; not used currently. */
629 char has_sent_end; /**< For debugging: set once we've set the stream end,
630 and check in circuit_about_to_close_connection(). */
631 struct circuit_t *on_circuit; /**< DOCDOC */
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;
643 uint16_t incoming_cmd_type;
644 uint32_t incoming_cmd_len;
645 uint32_t incoming_cmd_cur_len;
646 char *incoming_cmd;
649 typedef struct connection_t connection_t;
651 #define ADDR_POLICY_ACCEPT 1
652 #define ADDR_POLICY_REJECT 2
654 /** A linked list of policy rules */
655 typedef struct addr_policy_t {
656 char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
657 char *string; /**< String representation of this rule. */
658 uint32_t addr; /**< Base address to accept or reject. */
659 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
660 * a &amp; msk == <b>addr</b> &amp; msk . */
661 uint16_t prt_min; /**< Lowest port number to accept/reject. */
662 uint16_t prt_max; /**< Highest port number to accept/reject. */
664 struct addr_policy_t *next; /**< Next rule in list. */
665 } addr_policy_t;
667 /** Information about another onion router in the network. */
668 typedef struct {
669 char *signed_descriptor; /**< The original signed descriptor for this router*/
671 char *address; /**< Location of OR: either a hostname or an IP address. */
672 char *nickname; /**< Human-readable OR name. */
674 uint32_t addr; /**< IPv4 address of OR, in host order. */
675 uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
676 uint16_t dir_port; /**< Port for HTTP directory connections. */
678 time_t published_on; /**< When was the information in this routerinfo_t
679 * published? */
681 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
682 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
683 char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
685 char *platform; /**< What software/operating system is this OR using? */
687 /* link info */
688 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
689 * bucket per second? */
690 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
691 /** How many bytes/s is this router known to handle? */
692 uint32_t bandwidthcapacity;
693 addr_policy_t *exit_policy; /**< What streams will this OR permit
694 * to exit? */
695 long uptime; /**< How many seconds the router claims to have been up */
696 /* local info */
697 int is_running; /**< As far as we know, is this OR currently running? */
698 time_t status_set_at; /**< When did we last update is_running? */
699 int is_verified; /**< Has a trusted dirserver validated this OR? */
701 smartlist_t *declared_family; /**< Nicknames of router which this router
702 * claims are its family. */
704 char *contact_info; /**< Declared contact info for this router. */
705 } routerinfo_t;
707 /** Contents of a running-routers list */
708 typedef struct running_routers_t {
709 time_t published_on; /**< When was the list marked as published? */
710 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
711 smartlist_t *running_routers;
712 } running_routers_t;
714 /** Contents of a directory of onion routers. */
715 typedef struct {
716 /** List of routerinfo_t */
717 smartlist_t *routers;
718 /** Which versions of tor are recommended by this directory? */
719 char *software_versions;
720 /** When was the most recent directory that contributed to this list
721 * published?
723 time_t published_on;
724 time_t running_routers_updated_on;
725 /** What is the most recently received running_routers structure? */
726 running_routers_t *running_routers;
727 /** Which router is claimed to have signed it? */
728 char *signing_router;
729 } routerlist_t;
731 #define CRYPT_PATH_MAGIC 0x70127012u
733 /** Holds accounting information for a single step in the layered encryption
734 * performed by a circuit. Used only at the client edge of a circuit. */
735 struct crypt_path_t {
736 uint32_t magic;
738 /* crypto environments */
739 /** Encryption key and counter for cells heading towards the OR at this
740 * step. */
741 crypto_cipher_env_t *f_crypto;
742 /** Encryption key and counter for cells heading back from the OR at this
743 * step. */
744 crypto_cipher_env_t *b_crypto;
746 /** Digest state for cells heading towards the OR at this step. */
747 crypto_digest_env_t *f_digest; /* for integrity checking */
748 /** Digest state for cells heading away from the OR at this step. */
749 crypto_digest_env_t *b_digest;
751 /** Current state of Diffie-Hellman key negotiation with the OR at this
752 * step. */
753 crypto_dh_env_t *dh_handshake_state;
754 char fast_handshake_state[DIGEST_LEN];
755 /** Negotiated key material shared with the OR at this step. */
756 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
758 /** IP4 address of the OR at this step. */
759 uint32_t addr;
760 /** Port of the OR at this step. */
761 uint16_t port;
762 /** Identity key digest of the OR at this step. */
763 char identity_digest[DIGEST_LEN];
765 /** Is the circuit built to this step? Must be one of:
766 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
767 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
768 * and not received an EXTENDED/CREATED)
769 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
770 uint8_t state;
771 #define CPATH_STATE_CLOSED 0
772 #define CPATH_STATE_AWAITING_KEYS 1
773 #define CPATH_STATE_OPEN 2
774 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
775 * (The list is circular, so the last node
776 * links to the first.) */
777 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
778 * circuit. */
780 int package_window; /**< How many bytes are we allowed to originate ending
781 * at this step? */
782 int deliver_window; /**< How many bytes are we willing to deliver originating
783 * at this step? */
786 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
788 #define DH_KEY_LEN DH_BYTES
789 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
790 CIPHER_KEY_LEN+\
791 DH_KEY_LEN)
792 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
793 #define REND_COOKIE_LEN DIGEST_LEN
795 typedef struct crypt_path_t crypt_path_t;
797 /** Information used to build a circuit. */
798 typedef struct {
799 /** Intended length of the final circuit. */
800 int desired_path_len;
801 /** Nickname of planned exit node. */
802 char *chosen_exit_name;
803 /** Identity of planned exit node. */
804 char chosen_exit_digest[DIGEST_LEN];
805 /** Whether every node in the circ must have adequate uptime. */
806 int need_uptime;
807 /** Whether every node in the circ must have adequate capacity. */
808 int need_capacity;
809 /** Whether the last hop was picked with exiting in mind. */
810 int is_internal;
811 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
812 struct crypt_path_t *pending_final_cpath;
813 /** How many times has building a circuit for this task failed? */
814 int failure_count;
815 /** At what time should we give up on this task? */
816 time_t expiry_time;
817 } cpath_build_state_t;
819 #define CIRCUIT_MAGIC 0x35315243u
820 /** Struct for a path (circuit) through the onion routing network. */
821 struct circuit_t {
822 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
824 int marked_for_close; /**< Should we close this circuit at the end of the
825 * main loop? */
826 const char *marked_for_close_file; /**< For debugging: in which file was this
827 * circuit marked for close? */
829 /** The IPv4 address of the OR that is next in this circuit. */
830 uint32_t n_addr;
831 /** The port for the OR that is next in this circuit. */
832 uint16_t n_port;
833 /** The OR connection that is previous in this circuit. */
834 connection_t *p_conn;
835 /** The OR connection that is next in this circuit. */
836 connection_t *n_conn;
837 /** The identity hash of n_conn. */
838 char n_conn_id_digest[DIGEST_LEN];
839 /** Linked list of AP streams associated with this circuit. */
840 connection_t *p_streams;
841 /** Linked list of Exit streams associated with this circuit. */
842 connection_t *n_streams;
843 /** Linked list of Exit streams associated with this circuit that are
844 * still being resolved. */
845 connection_t *resolving_streams;
846 /** The next stream_id that will be tried when we're attempting to
847 * construct a new AP stream originating at this circuit. */
848 uint16_t next_stream_id;
849 /** How many relay data cells can we package (read from edge streams)
850 * on this circuit before we receive a circuit-level sendme cell asking
851 * for more? */
852 int package_window;
853 /** How many relay data cells will we deliver (write to edge streams)
854 * on this circuit? When deliver_window gets low, we send some
855 * circuit-level sendme cells to indicate that we're willing to accept
856 * more. */
857 int deliver_window;
859 /** The circuit_id used in the previous (backward) hop of this circuit. */
860 uint16_t p_circ_id;
861 /** The circuit_id used in the next (forward) hop of this circuit. */
862 uint16_t n_circ_id;
864 /** The cipher used by intermediate hops for cells heading toward the
865 * OP. */
866 crypto_cipher_env_t *p_crypto;
867 /** The cipher used by intermediate hops for cells heading away from
868 * the OP. */
869 crypto_cipher_env_t *n_crypto;
871 /** The integrity-checking digest used by intermediate hops, for
872 * cells packaged here and heading towards the OP.
874 crypto_digest_env_t *p_digest;
875 /** The integrity-checking digest used by intermediate hops, for
876 * cells packaged at the OP and arriving here.
878 crypto_digest_env_t *n_digest;
880 /** Build state for this circuit. It includes the intended path
881 * length, the chosen exit router, rendezvous information, etc.
883 cpath_build_state_t *build_state;
884 /** The doubly-linked list of crypt_path_t entries, one per hop,
885 * for this circuit. This includes ciphers for each hop,
886 * integrity-checking digests for each hop, and package/delivery
887 * windows for each hop.
889 * The cpath field is defined only when we are the circuit's origin.
891 crypt_path_t *cpath;
893 /** For storage while passing to cpuworker, or while n_conn is pending. */
894 char onionskin[ONIONSKIN_CHALLENGE_LEN];
896 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
898 time_t timestamp_created; /**< When was this circuit created? */
899 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
900 * circuit is clean. */
902 uint8_t state; /**< Current status of this circuit. */
903 uint8_t purpose; /**< Why are we creating this circuit? */
906 * The rend_query field holds y portion of y.onion (nul-terminated)
907 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
908 * for a hidden service, or is S_*.
910 char rend_query[REND_SERVICE_ID_LEN+1];
912 /** The rend_pk_digest field holds a hash of location-hidden service's
913 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
915 char rend_pk_digest[DIGEST_LEN];
917 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
918 * C_ESTABLISH_REND. Filled with zeroes otherwise.
920 char rend_cookie[REND_COOKIE_LEN];
922 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
923 * is not marked for close. */
924 struct circuit_t *rend_splice;
926 /** Quasi-global identifier for this circuit; used for control.c */
927 /* XXXX NM This can get re-used after 2**32 circuits. */
928 uint32_t global_identifier;
930 struct circuit_t *next; /**< Next circuit in linked list. */
933 typedef struct circuit_t circuit_t;
935 #define ALLOW_UNVERIFIED_ENTRY 1
936 #define ALLOW_UNVERIFIED_EXIT 2
937 #define ALLOW_UNVERIFIED_MIDDLE 4
938 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
939 #define ALLOW_UNVERIFIED_INTRODUCTION 16
941 typedef struct exit_redirect_t {
942 uint32_t addr;
943 uint32_t mask;
944 uint16_t port_min;
945 uint16_t port_max;
947 int is_redirect;
948 uint32_t addr_dest;
949 uint16_t port_dest;
950 } exit_redirect_t;
952 /** Configuration options for a Tor process */
953 typedef struct {
954 /** What should the tor process actually do? */
955 enum {
956 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
957 CMD_VERIFY_CONFIG,
958 } command;
959 const char *command_arg; /**< Argument for command-line option. */
961 struct config_line_t *OldLogOptions; /**< List of configuration lines
962 * for logfiles, old style. */
964 struct config_line_t *Logs; /**< New-style list of configuration lines
965 * for logs */
967 char *DebugLogFile; /**< Where to send verbose log messages. */
968 char *DataDirectory; /**< OR only: where to store long-term data. */
969 char *Nickname; /**< OR only: nickname of this onion router. */
970 char *Address; /**< OR only: configured address for this onion router. */
971 char *PidFile; /**< Where to store PID of Tor process. */
973 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
974 * as exits. */
975 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
976 * as entry points. */
977 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
978 * stop building circuits? */
979 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
980 * stop building circuits? */
981 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
982 * use in circuits. */
984 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
985 * points. */
986 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
987 * as introduction points. */
989 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
990 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
991 struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
992 struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
993 struct config_line_t *DirPolicy; /**< Lists of dir policy components */
994 /** Addresses to bind for listening for SOCKS connections. */
995 struct config_line_t *SocksBindAddress;
996 /** Addresses to bind for listening for OR connections. */
997 struct config_line_t *ORBindAddress;
998 /** Addresses to bind for listening for directory connections. */
999 struct config_line_t *DirBindAddress;
1000 /** Local address to bind outbound sockets */
1001 char *OutboundBindAddress;
1002 /** Directory server only: which versions of
1003 * Tor should we tell users to run? */
1004 struct config_line_t *RecommendedVersions;
1005 /** Whether dirservers refuse router descriptors with private IPs. */
1006 int DirAllowPrivateAddresses;
1007 char *User; /**< Name of user to run Tor as. */
1008 char *Group; /**< Name of group to run Tor as. */
1009 double PathlenCoinWeight; /**< Parameter used to configure average path
1010 * length (alpha in geometric distribution). */
1011 int ORPort; /**< Port to listen on for OR connections. */
1012 int SocksPort; /**< Port to listen on for SOCKS connections. */
1013 int ControlPort; /**< Port to listen on for control connections. */
1014 int DirPort; /**< Port to listen on for directory connections. */
1015 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
1016 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
1017 int NoPublish; /**< Boolean: should we never publish a descriptor? */
1018 int ConnLimit; /**< Requested maximum number of simultaneous connections. */
1019 int _ConnLimit; /**< Actual maximum number of simultaneous connections. */
1020 int IgnoreVersion; /**< If true, run no matter what versions of Tor the
1021 * directory recommends. */
1022 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
1023 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
1024 smartlist_t *FirewallPorts; /**< Which ports our firewall allows (strings). */
1025 /** Application ports that require all nodes in circ to have sufficient uptime. */
1026 smartlist_t *LongLivedPorts;
1027 /** Should we try to reuse the same exit node for a given host */
1028 smartlist_t *TrackHostExits;
1029 int TrackHostExitsExpire; /**< Number of seconds until we expire an addressmap */
1030 struct config_line_t *AddressMap; /**< List of address map directives. */
1031 int DirFetchPeriod; /**< How often do we fetch new directories? */
1032 int DirPostPeriod; /**< How often do we post our server descriptor to the
1033 * authoritative directory servers? */
1034 int RendPostPeriod; /**< How often do we post each rendezvous service
1035 * descriptor? Remember to publish them independently. */
1036 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
1037 int KeepalivePeriod; /**< How often do we send padding cells to keep
1038 * connections alive? */
1039 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
1040 * to wait simultaneously before we start dropping
1041 * them? */
1042 int NewCircuitPeriod; /**< How long do we use a circuit before building
1043 * a new one? */
1044 int MaxCircuitDirtiness; /**< Never use circs that were first used more than
1045 this interval ago. */
1046 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
1047 * use in a second? */
1048 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
1049 * use in a second? */
1050 uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
1051 * tell people we have? */
1052 int NumCpus; /**< How many CPUs should we try to use? */
1053 int RunTesting; /**< If true, create testing circuits to measure how well the
1054 * other ORs are running. */
1055 struct config_line_t *RendConfigLines; /**< List of configuration lines
1056 * for rendezvous services. */
1057 char *ContactInfo; /**< Contact info to be published in the directory */
1059 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
1060 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
1061 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
1062 char *HttpProxyAuthenticator; /**< username:password string, if any */
1064 char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
1065 uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
1066 uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
1067 char *HttpsProxyAuthenticator; /**< username:password string, if any */
1069 struct config_line_t *DirServers; /**< List of configuration lines
1070 * for directory servers. */
1071 char *MyFamily; /**< Declared family for this OR. */
1072 struct config_line_t *NodeFamilies; /**< List of config lines for
1073 * node families */
1074 struct config_line_t *RedirectExit; /**< List of config lines for simple
1075 * addr/port redirection */
1076 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
1077 int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
1078 * interval starts */
1079 char *AccountingStart; /**< How long is the accounting interval, and when
1080 * does it start? */
1081 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
1082 * interval before hibernation? 0 for "never
1083 * hibernate." */
1084 int _AccountingMaxKB; /**< How many KB do we allow per accounting
1085 * interval before hibernation? 0 for "never
1086 * hibernate." (Based on a deprecated option)*/
1088 char *HashedControlPassword; /**< Base64-encoded hash of a password for
1089 * the control system. */
1090 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
1091 * the control system? */
1092 int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
1093 * circuits itself (0), or does it expect a controller
1094 * to cope? (1) */
1095 int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
1096 * long do we wait before exiting? */
1097 int SafeLogging; /**< Boolean: are we allowed to log sensitive strings
1098 * such as addresses (0), or do we scrub them first (1)? */
1099 } or_options_t;
1101 #define MAX_SOCKS_REPLY_LEN 1024
1102 #define MAX_SOCKS_ADDR_LEN 256
1103 #define SOCKS_COMMAND_CONNECT 0x01
1104 #define SOCKS_COMMAND_RESOLVE 0xF0
1105 /** State of a SOCKS request from a user to an OP */
1106 struct socks_request_t {
1107 char socks_version; /**< Which version of SOCKS did the client use? */
1108 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
1109 size_t replylen; /**< Length of <b>reply</b>. */
1110 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
1111 * we want to specify our own socks reply,
1112 * rather than using the default socks4 or
1113 * socks5 socks reply. We use this for the
1114 * two-stage socks5 handshake.
1116 int has_finished; /**< Has the SOCKS handshake finished? */
1117 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
1118 uint16_t port; /**< What port did the client ask to connect to? */
1121 /* all the function prototypes go here */
1123 /********************************* buffers.c ***************************/
1125 buf_t *buf_new(void);
1126 buf_t *buf_new_with_capacity(size_t size);
1127 void buf_free(buf_t *buf);
1128 void buf_clear(buf_t *buf);
1129 void buf_shrink(buf_t *buf);
1131 size_t buf_datalen(const buf_t *buf);
1132 size_t buf_capacity(const buf_t *buf);
1133 const char *_buf_peek_raw_buffer(const buf_t *buf);
1135 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1136 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
1138 int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
1139 int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
1141 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1142 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1143 int fetch_from_buf_http(buf_t *buf,
1144 char **headers_out, size_t max_headerlen,
1145 char **body_out, size_t *body_used, size_t max_bodylen);
1146 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
1147 int fetch_from_buf_control(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
1148 char **body_out);
1150 void assert_buf_ok(buf_t *buf);
1152 /********************************* circuitbuild.c **********************/
1154 char *circuit_list_path(circuit_t *circ, int verbose);
1155 void circuit_log_path(int severity, circuit_t *circ);
1156 void circuit_rep_hist_note_result(circuit_t *circ);
1157 void circuit_dump_by_conn(connection_t *conn, int severity);
1158 circuit_t *circuit_init(uint8_t purpose, int need_uptime,
1159 int need_capacity, int internal);
1160 circuit_t *circuit_establish_circuit(uint8_t purpose, routerinfo_t *exit,
1161 int need_uptime, int need_capacity, int internal);
1162 int circuit_handle_first_hop(circuit_t *circ);
1163 void circuit_n_conn_done(connection_t *or_conn, int status);
1164 int circuit_send_next_onion_skin(circuit_t *circ);
1165 void circuit_note_clock_jumped(int seconds_elapsed);
1166 int circuit_extend(cell_t *cell, circuit_t *circ);
1167 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
1168 int circuit_finish_handshake(circuit_t *circ, uint8_t cell_type, char *reply);
1169 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1170 int onionskin_answer(circuit_t *circ, uint8_t cell_type, char *payload, char *keys);
1171 int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
1172 int *need_capacity);
1174 int circuit_append_new_exit(circuit_t *circ, routerinfo_t *exit);
1175 int circuit_extend_to_new_exit(circuit_t *circ, routerinfo_t *exit);
1176 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1178 /********************************* circuitlist.c ***********************/
1180 const char *circuit_state_to_string(int state);
1181 enum which_conn_changed_t { P_CONN_CHANGED=1, N_CONN_CHANGED=0 };
1182 void circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
1183 connection_t *conn,
1184 enum which_conn_changed_t which);
1185 void circuit_close_all_marked(void);
1186 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1187 circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn);
1188 circuit_t *circuit_get_by_edge_conn(connection_t *conn);
1189 circuit_t *circuit_get_by_conn(connection_t *conn);
1190 circuit_t *circuit_get_by_global_id(uint32_t id);
1191 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
1192 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1193 const char *digest, uint8_t purpose);
1194 circuit_t *circuit_get_rendezvous(const char *cookie);
1195 circuit_t *circuit_get_clean_open(uint8_t purpose, int need_uptime,
1196 int need_capacity, int internal);
1197 void circuit_mark_all_unused_circs(void);
1198 void _circuit_mark_for_close(circuit_t *circ, int line, const char *file);
1200 #define circuit_mark_for_close(c) \
1201 _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
1203 void assert_cpath_layer_ok(const crypt_path_t *cp);
1204 void assert_circuit_ok(const circuit_t *c);
1205 void circuit_free_all(void);
1207 /********************************* circuituse.c ************************/
1209 void circuit_expire_building(time_t now);
1210 void circuit_remove_handled_ports(smartlist_t *needed_ports);
1211 int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
1212 void circuit_build_needed_circs(time_t now);
1213 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1214 void circuit_about_to_close_connection(connection_t *conn);
1215 void circuit_has_opened(circuit_t *circ);
1216 void circuit_build_failed(circuit_t *circ);
1217 circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
1218 int need_uptime, int need_capacity, int is_internal);
1219 circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
1220 int need_uptime, int need_capacity, int is_internal);
1221 void circuit_reset_failure_count(int timeout);
1222 int connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
1223 circuit_t *circ);
1224 int connection_ap_handshake_attach_circuit(connection_t *conn);
1226 /********************************* command.c ***************************/
1228 void command_process_cell(cell_t *cell, connection_t *conn);
1230 extern unsigned long stats_n_padding_cells_processed;
1231 extern unsigned long stats_n_create_cells_processed;
1232 extern unsigned long stats_n_created_cells_processed;
1233 extern unsigned long stats_n_relay_cells_processed;
1234 extern unsigned long stats_n_destroy_cells_processed;
1236 /********************************* config.c ***************************/
1238 struct config_line_t {
1239 char *key;
1240 char *value;
1241 struct config_line_t *next;
1244 or_options_t *get_options(void);
1245 void set_options(or_options_t *new_val);
1246 int options_act(void);
1247 void config_free_all(void);
1248 const char *safe_str(const char *address);
1250 int config_get_lines(char *string, struct config_line_t **result);
1251 void config_free_lines(struct config_line_t *front);
1252 int config_trial_assign(struct config_line_t *list, int reset);
1253 int resolve_my_address(or_options_t *options, uint32_t *addr);
1254 void options_init(or_options_t *options);
1255 int init_from_config(int argc, char **argv);
1256 int config_init_logs(or_options_t *options, int validate_only);
1257 int config_parse_addr_policy(struct config_line_t *cfg,
1258 addr_policy_t **dest);
1259 void config_append_default_exit_policy(addr_policy_t **policy);
1260 void addr_policy_free(addr_policy_t *p);
1261 int config_option_is_recognized(const char *key);
1262 struct config_line_t *config_get_assigned_option(or_options_t *options,
1263 const char *key);
1264 char *config_dump_options(or_options_t *options, int minimal);
1265 int save_current_config(void);
1267 /********************************* connection.c ***************************/
1269 const char *conn_type_to_string(int type);
1270 const char *conn_state_to_string(int type, int state);
1272 connection_t *connection_new(int type);
1273 void connection_unregister(connection_t *conn);
1274 void connection_free(connection_t *conn);
1275 void connection_free_all(void);
1276 void connection_about_to_close_connection(connection_t *conn);
1277 void connection_close_immediate(connection_t *conn);
1278 void _connection_mark_for_close(connection_t *conn,int line, const char *file);
1280 #define connection_mark_for_close(c) \
1281 _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
1283 void connection_expire_held_open(void);
1285 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
1286 int retry_all_listeners(int force);
1288 void connection_bucket_init(void);
1289 void connection_bucket_refill(struct timeval *now);
1291 int connection_handle_read(connection_t *conn);
1293 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1295 int connection_wants_to_flush(connection_t *conn);
1296 int connection_outbuf_too_full(connection_t *conn);
1297 int connection_handle_write(connection_t *conn);
1298 void _connection_controller_force_write(connection_t *conn);
1299 void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
1301 connection_t *connection_or_exact_get_by_addr_port(uint32_t addr, uint16_t port);
1302 connection_t *connection_get_by_identity_digest(const char *digest, int type);
1303 connection_t *connection_get_by_global_id(uint32_t id);
1305 connection_t *connection_get_by_type(int type);
1306 connection_t *connection_get_by_type_state(int type, int state);
1307 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1308 connection_t *connection_get_by_type_state_rendquery(int type, int state, const char *rendquery);
1310 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1311 #define connection_has_pending_tls_data(conn) \
1312 ((conn)->type == CONN_TYPE_OR && \
1313 (conn)->state == OR_CONN_STATE_OPEN && \
1314 tor_tls_get_pending_bytes((conn)->tls))
1315 int connection_is_listener(connection_t *conn);
1316 int connection_state_is_open(connection_t *conn);
1317 int connection_state_is_connecting(connection_t *conn);
1319 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
1320 char *alloc_http_authenticator(const char *authenticator);
1322 void assert_connection_ok(connection_t *conn, time_t now);
1323 int connection_or_nonopen_was_started_here(connection_t *conn);
1325 /********************************* connection_edge.c ***************************/
1327 #define connection_mark_unattached_ap(conn, endreason) \
1328 _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
1330 void _connection_mark_unattached_ap(connection_t *conn, int endreason,
1331 int line, const char *file);
1332 int connection_edge_reached_eof(connection_t *conn);
1333 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1334 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1335 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
1336 int connection_edge_end_errno(connection_t *conn, crypt_path_t *cpath_layer);
1337 int connection_edge_finished_flushing(connection_t *conn);
1338 int connection_edge_finished_connecting(connection_t *conn);
1340 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1341 int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
1343 int connection_ap_make_bridge(char *address, uint16_t port);
1344 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1345 size_t replylen,
1346 socks5_reply_status_t status);
1347 void connection_ap_handshake_socks_resolved(connection_t *conn,
1348 int answer_type,
1349 size_t answer_len,
1350 const char *answer);
1352 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1353 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1354 void connection_exit_connect(connection_t *conn);
1355 int connection_edge_is_rendezvous_stream(connection_t *conn);
1356 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1357 void connection_ap_expire_beginning(void);
1358 void connection_ap_attach_pending(void);
1359 int connection_ap_detach_retriable(connection_t *conn, circuit_t *circ);
1361 void addressmap_init(void);
1362 void addressmap_clean(time_t now);
1363 void addressmap_clear_configured(void);
1364 void addressmap_clear_transient(void);
1365 void addressmap_free_all(void);
1366 void addressmap_rewrite(char *address, size_t maxlen);
1367 int addressmap_already_mapped(const char *address);
1368 void addressmap_register(const char *address, char *new_address, time_t expires);
1369 int client_dns_incr_failures(const char *address);
1370 void client_dns_set_addressmap(const char *address, uint32_t val, const char *exitname);
1371 const char *addressmap_register_virtual_address(int type, char *new_address);
1372 void addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires);
1374 void parse_socks_policy(void);
1375 void free_socks_policy(void);
1376 int socks_policy_permits_address(uint32_t addr);
1378 void set_exit_redirects(smartlist_t *lst);
1379 typedef enum hostname_type_t {
1380 NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME, BAD_HOSTNAME
1381 } hostname_type_t;
1382 hostname_type_t parse_extended_hostname(char *address);
1384 /********************************* connection_or.c ***************************/
1386 int connection_or_reached_eof(connection_t *conn);
1387 int connection_or_process_inbuf(connection_t *conn);
1388 int connection_or_finished_flushing(connection_t *conn);
1389 int connection_or_finished_connecting(connection_t *conn);
1391 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1392 const char *id_digest);
1394 int connection_tls_start_handshake(connection_t *conn, int receiving);
1395 int connection_tls_continue_handshake(connection_t *conn);
1397 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1398 void connection_or_update_nickname(connection_t *conn);
1400 /********************************* control.c ***************************/
1402 typedef enum circuit_status_event_t {
1403 CIRC_EVENT_LAUNCHED = 0,
1404 CIRC_EVENT_BUILT = 1,
1405 CIRC_EVENT_EXTENDED = 2,
1406 CIRC_EVENT_FAILED = 3,
1407 CIRC_EVENT_CLOSED = 4,
1408 } circuit_status_event_t;
1410 typedef enum stream_status_event_t {
1411 STREAM_EVENT_SENT_CONNECT = 0,
1412 STREAM_EVENT_SENT_RESOLVE = 1,
1413 STREAM_EVENT_SUCCEEDED = 2,
1414 STREAM_EVENT_FAILED = 3,
1415 STREAM_EVENT_CLOSED = 4,
1416 STREAM_EVENT_NEW = 5,
1417 STREAM_EVENT_NEW_RESOLVE = 6,
1418 STREAM_EVENT_FAILED_RETRIABLE = 7
1419 } stream_status_event_t;
1421 typedef enum or_conn_status_event_t {
1422 OR_CONN_EVENT_LAUNCHED = 0,
1423 OR_CONN_EVENT_CONNECTED = 1,
1424 OR_CONN_EVENT_FAILED = 2,
1425 OR_CONN_EVENT_CLOSED = 3,
1426 } or_conn_status_event_t;
1428 void adjust_event_log_severity(void);
1429 int connection_control_finished_flushing(connection_t *conn);
1430 int connection_control_reached_eof(connection_t *conn);
1431 int connection_control_process_inbuf(connection_t *conn);
1433 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1434 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1435 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1436 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1437 void control_event_logmsg(int severity, const char *msg);
1438 int control_event_descriptors_changed(smartlist_t *routers);
1440 int init_cookie_authentication(int enabled);
1441 int decode_hashed_password(char *buf, const char *hashed);
1443 /********************************* cpuworker.c *****************************/
1445 void cpu_init(void);
1446 void cpuworkers_rotate(void);
1447 int connection_cpu_finished_flushing(connection_t *conn);
1448 int connection_cpu_reached_eof(connection_t *conn);
1449 int connection_cpu_process_inbuf(connection_t *conn);
1450 int assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
1451 void *task);
1453 /********************************* directory.c ***************************/
1455 int dir_policy_permits_address(uint32_t addr);
1456 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1457 size_t payload_len);
1458 void directory_get_from_dirserver(uint8_t purpose, const char *resource,
1459 int retry_if_no_servers);
1460 void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
1461 int private_connection, const char *resource,
1462 const char *payload, size_t payload_len);
1464 int parse_http_response(const char *headers, int *code, time_t *date,
1465 int *compression, char **response);
1467 int connection_dir_reached_eof(connection_t *conn);
1468 int connection_dir_process_inbuf(connection_t *conn);
1469 int connection_dir_finished_flushing(connection_t *conn);
1470 int connection_dir_finished_connecting(connection_t *conn);
1471 void connection_dir_connect_failed(connection_t *conn);
1472 void parse_dir_policy(void);
1473 void free_dir_policy(void);
1475 /********************************* dirserv.c ***************************/
1477 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1478 int dirserv_parse_fingerprint_file(const char *fname);
1479 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
1480 void dirserv_free_fingerprint_list(void);
1481 const char *dirserv_get_nickname_by_digest(const char *digest);
1482 int dirserv_add_descriptor(const char **desc, const char **msg);
1483 int dirserv_load_from_directory_string(const char *dir);
1484 void dirserv_free_descriptors(void);
1485 int list_server_status(smartlist_t *routers, char **router_status_out);
1486 void dirserv_remove_old_servers(int age);
1487 int dirserv_dump_directory_to_string(char **dir_out,
1488 crypto_pk_env_t *private_key);
1489 void directory_set_dirty(void);
1490 size_t dirserv_get_directory(const char **cp, int compress);
1491 size_t dirserv_get_runningrouters(const char **rr, int compress);
1492 void dirserv_set_cached_directory(const char *directory, time_t when,
1493 int is_running_routers);
1494 void dirserv_orconn_tls_done(const char *address,
1495 uint16_t or_port,
1496 const char *digest_rcvd,
1497 const char *nickname);
1498 void dirserv_free_all(void);
1500 /********************************* dns.c ***************************/
1502 void dns_init(void);
1503 void dns_free_all(void);
1504 int connection_dns_finished_flushing(connection_t *conn);
1505 int connection_dns_reached_eof(connection_t *conn);
1506 int connection_dns_process_inbuf(connection_t *conn);
1507 void dnsworkers_rotate(void);
1508 void connection_dns_remove(connection_t *conn);
1509 void assert_connection_edge_not_dns_pending(connection_t *conn);
1510 void assert_all_pending_dns_resolves_ok(void);
1511 void dns_cancel_pending_resolve(char *question);
1512 int dns_resolve(connection_t *exitconn);
1514 /********************************* hibernate.c **********************/
1516 int accounting_parse_options(or_options_t *options, int validate_only);
1517 int accounting_is_enabled(or_options_t *options);
1518 void configure_accounting(time_t now);
1519 void accounting_run_housekeeping(time_t now);
1520 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1521 int accounting_record_bandwidth_usage(time_t now);
1522 void hibernate_begin_shutdown(void);
1523 int we_are_hibernating(void);
1524 void consider_hibernation(time_t now);
1526 /********************************* main.c ***************************/
1528 int connection_add(connection_t *conn);
1529 int connection_remove(connection_t *conn);
1530 int connection_in_array(connection_t *conn);
1531 void add_connection_to_closeable_list(connection_t *conn);
1532 int connection_is_on_closeable_list(connection_t *conn);
1534 void get_connection_array(connection_t ***array, int *n);
1536 void connection_watch_events(connection_t *conn, short events);
1537 int connection_is_reading(connection_t *conn);
1538 void connection_stop_reading(connection_t *conn);
1539 void connection_start_reading(connection_t *conn);
1541 int connection_is_writing(connection_t *conn);
1542 void connection_stop_writing(connection_t *conn);
1543 void connection_start_writing(connection_t *conn);
1545 void directory_all_unreachable(time_t now);
1546 void directory_has_arrived(time_t now, char *identity_digest);
1548 int control_signal_act(int the_signal);
1549 void handle_signals(int is_parent);
1550 void tor_cleanup(void);
1551 void tor_free_all(int postfork);
1553 int tor_main(int argc, char *argv[]);
1555 /********************************* onion.c ***************************/
1557 int onion_pending_add(circuit_t *circ);
1558 circuit_t *onion_next_task(void);
1559 void onion_pending_remove(circuit_t *circ);
1561 int onion_skin_create(crypto_pk_env_t *router_key,
1562 crypto_dh_env_t **handshake_state_out,
1563 char *onion_skin_out);
1565 int onion_skin_server_handshake(const char *onion_skin,
1566 crypto_pk_env_t *private_key,
1567 crypto_pk_env_t *prev_private_key,
1568 char *handshake_reply_out,
1569 char *key_out,
1570 size_t key_out_len);
1572 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
1573 const char *handshake_reply,
1574 char *key_out,
1575 size_t key_out_len);
1577 int fast_server_handshake(const char *key_in,
1578 char *handshake_reply_out,
1579 char *key_out,
1580 size_t key_out_len);
1582 int fast_client_handshake(const char *handshake_state,
1583 const char *handshake_reply_out,
1584 char *key_out,
1585 size_t key_out_len);
1587 void clear_pending_onions(void);
1589 /********************************* relay.c ***************************/
1591 extern unsigned long stats_n_relay_cells_relayed;
1592 extern unsigned long stats_n_relay_cells_delivered;
1594 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
1595 int cell_direction);
1597 void relay_header_pack(char *dest, const relay_header_t *src);
1598 void relay_header_unpack(relay_header_t *dest, const char *src);
1599 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
1600 int relay_command, const char *payload,
1601 size_t payload_len, crypt_path_t *cpath_layer);
1602 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
1603 void connection_edge_consider_sending_sendme(connection_t *conn);
1604 socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
1605 int errno_to_end_reason(int e);
1607 extern uint64_t stats_n_data_cells_packaged;
1608 extern uint64_t stats_n_data_bytes_packaged;
1609 extern uint64_t stats_n_data_cells_received;
1610 extern uint64_t stats_n_data_bytes_received;
1612 /********************************* rephist.c ***************************/
1614 void rep_hist_init(void);
1615 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1616 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1617 void rep_hist_note_disconnect(const char* nickname, time_t when);
1618 void rep_hist_note_connection_died(const char* nickname, time_t when);
1619 void rep_hist_note_extend_succeeded(const char *from_name,
1620 const char *to_name);
1621 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1622 void rep_hist_dump_stats(time_t now, int severity);
1623 void rep_hist_note_bytes_read(int num_bytes, time_t when);
1624 void rep_hist_note_bytes_written(int num_bytes, time_t when);
1625 int rep_hist_bandwidth_assess(void);
1626 char *rep_hist_get_bandwidth_lines(void);
1627 void rep_history_clean(time_t before);
1629 void rep_hist_note_used_port(uint16_t port, time_t now);
1630 smartlist_t *rep_hist_get_predicted_ports(time_t now);
1631 void rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity);
1632 int rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity);
1633 void rep_hist_note_used_resolve(time_t now);
1634 int rep_hist_get_predicted_resolve(time_t now);
1636 void rep_hist_free_all(void);
1638 /********************************* rendclient.c ***************************/
1640 void rend_client_introcirc_has_opened(circuit_t *circ);
1641 void rend_client_rendcirc_has_opened(circuit_t *circ);
1642 int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
1643 void rend_client_refetch_renddesc(const char *query);
1644 int rend_client_remove_intro_point(char *failed_intro, const char *query);
1645 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
1646 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1647 void rend_client_desc_here(char *query);
1649 char *rend_client_get_random_intro(char *query);
1651 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1653 /********************************* rendcommon.c ***************************/
1655 typedef struct rend_service_descriptor_t {
1656 crypto_pk_env_t *pk;
1657 time_t timestamp;
1658 int n_intro_points;
1659 char **intro_points;
1660 } rend_service_descriptor_t;
1662 int rend_cmp_service_ids(const char *one, const char *two);
1664 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
1665 const char *payload);
1667 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1668 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1669 crypto_pk_env_t *key,
1670 char **str_out,
1671 size_t *len_out);
1672 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
1673 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1675 typedef struct rend_cache_entry_t {
1676 size_t len; /* Length of desc */
1677 time_t received; /* When did we get the descriptor? */
1678 char *desc; /* Service descriptor */
1679 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1680 } rend_cache_entry_t;
1682 void rend_cache_init(void);
1683 void rend_cache_clean(void);
1684 void rend_cache_free_all(void);
1685 int rend_valid_service_id(const char *query);
1686 int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
1687 int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
1688 int rend_cache_store(const char *desc, size_t desc_len);
1690 /********************************* rendservice.c ***************************/
1692 int rend_config_services(or_options_t *options, int validate_only);
1693 int rend_service_load_keys(void);
1694 void rend_services_init(void);
1695 void rend_services_introduce(void);
1696 void rend_consider_services_upload(time_t now);
1698 void rend_service_intro_has_opened(circuit_t *circuit);
1699 int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
1700 void rend_service_rendezvous_has_opened(circuit_t *circuit);
1701 int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
1702 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1703 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1704 void rend_service_dump_stats(int severity);
1705 void rend_service_free_all(void);
1707 /********************************* rendmid.c *******************************/
1708 int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
1709 int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
1710 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1711 int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1713 /********************************* router.c ***************************/
1714 typedef enum {
1715 ADDR_POLICY_ACCEPTED=0,
1716 ADDR_POLICY_REJECTED=-1,
1717 ADDR_POLICY_PROBABLY_ACCEPTED=1,
1718 ADDR_POLICY_PROBABLY_REJECTED=2
1719 } addr_policy_result_t;
1721 void set_onion_key(crypto_pk_env_t *k);
1722 crypto_pk_env_t *get_onion_key(void);
1723 crypto_pk_env_t *get_previous_onion_key(void);
1724 time_t get_onion_key_set_at(void);
1725 void set_identity_key(crypto_pk_env_t *k);
1726 crypto_pk_env_t *get_identity_key(void);
1727 int identity_key_is_set(void);
1728 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
1729 void rotate_onion_key(void);
1730 crypto_pk_env_t *init_key_from_file(const char *fname);
1731 int init_keys(void);
1733 int check_whether_orport_reachable(void);
1734 int check_whether_dirport_reachable(void);
1735 void consider_testing_reachability(void);
1736 void router_orport_found_reachable(void);
1737 void router_dirport_found_reachable(void);
1738 void server_has_changed_ip(void);
1739 void consider_publishable_server(time_t now, int force);
1741 int authdir_mode(or_options_t *options);
1742 int clique_mode(or_options_t *options);
1743 int server_mode(or_options_t *options);
1744 int advertised_server_mode(void);
1745 int proxy_mode(or_options_t *options);
1747 void router_retry_connections(void);
1748 int router_is_clique_mode(routerinfo_t *router);
1749 void router_upload_dir_desc_to_dirservers(int force);
1750 void mark_my_descriptor_dirty(void);
1751 int router_compare_to_my_exit_policy(connection_t *conn);
1752 routerinfo_t *router_get_my_routerinfo(void);
1753 const char *router_get_my_descriptor(void);
1754 int router_digest_is_me(const char *digest);
1755 int router_is_me(routerinfo_t *router);
1756 int router_rebuild_descriptor(int force);
1757 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
1758 crypto_pk_env_t *ident_key);
1759 int is_legal_nickname(const char *s);
1760 int is_legal_nickname_or_hexdigest(const char *s);
1761 void router_free_all_keys(void);
1763 /********************************* routerlist.c ***************************/
1765 typedef struct trusted_dir_server_t {
1766 char *address;
1767 uint32_t addr;
1768 uint16_t dir_port;
1769 char digest[DIGEST_LEN];
1770 int is_running;
1771 } trusted_dir_server_t;
1773 int router_reload_router_list(void);
1774 void router_get_trusted_dir_servers(smartlist_t **outp);
1775 routerinfo_t *router_pick_directory_server(int requireothers,
1776 int fascistfirewall,
1777 int for_running_routers,
1778 int retry_if_no_servers);
1779 trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
1780 int fascistfirewall,
1781 int retry_if_no_servers);
1782 int all_trusted_directory_servers_down(void);
1783 struct smartlist_t;
1784 void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
1785 void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
1786 int router_nickname_is_in_list(routerinfo_t *router, const char *list);
1787 routerinfo_t *routerlist_find_my_routerinfo(void);
1788 int router_nickname_matches(routerinfo_t *router, const char *nickname);
1789 int exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
1790 int warn);
1792 /** How many seconds a router must be up before we'll use it for
1793 * reliability-critical node positions.
1795 #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
1796 #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
1798 int router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity);
1799 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
1800 routerinfo_t *router_choose_random_node(const char *preferred,
1801 const char *excluded,
1802 struct smartlist_t *excludedsmartlist,
1803 int need_uptime, int need_bandwidth,
1804 int allow_unverified, int strict);
1805 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
1806 routerinfo_t *router_get_by_nickname(const char *nickname);
1807 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
1808 routerinfo_t *router_get_by_digest(const char *digest);
1809 int router_digest_is_trusted_dir(const char *digest);
1810 void router_get_routerlist(routerlist_t **prouterlist);
1811 time_t routerlist_get_published_time(void);
1812 void routerlist_free(routerlist_t *routerlist);
1813 void routerinfo_free(routerinfo_t *router);
1814 void routerlist_free_current(void);
1815 void free_trusted_dir_servers(void);
1816 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
1817 void router_mark_as_down(const char *digest);
1818 void routerlist_remove_old_routers(int age);
1819 int router_load_single_router(const char *s, const char **msg);
1820 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
1821 int dir_is_recent, int dir_is_cached);
1822 addr_policy_result_t router_compare_addr_to_addr_policy(uint32_t addr,
1823 uint16_t port, addr_policy_t *policy);
1825 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
1826 int need_uptime);
1828 int router_exit_policy_rejects_all(routerinfo_t *router);
1829 void running_routers_free(running_routers_t *rr);
1830 void routerlist_set_runningrouters(routerlist_t *list, running_routers_t *rr);
1831 void routerlist_update_from_runningrouters(routerlist_t *list,
1832 running_routers_t *rr);
1833 int routers_update_status_from_entry(smartlist_t *routers,
1834 time_t list_time,
1835 const char *s);
1836 int router_update_status_from_smartlist(routerinfo_t *r,
1837 time_t list_time,
1838 smartlist_t *running_list);
1839 void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
1840 void clear_trusted_dir_servers(void);
1842 /********************************* routerparse.c ************************/
1844 #define MAX_STATUS_TAG_LEN 32
1845 /** Structure to hold parsed Tor versions. This is a little messier
1846 * than we would like it to be, because we changed version schemes with 0.1.0.
1848 * See version-spec.txt for the whole business.
1850 typedef struct tor_version_t {
1851 int major;
1852 int minor;
1853 int micro;
1854 /** Release status. For version in the post-0.1 format, this is always
1855 * VER_RELEASE. */
1856 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
1857 int patchlevel;
1858 /** CVS status. For version in the post-0.1 format, this is always
1859 * IS_NOT_CVS */
1860 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
1861 char status_tag[MAX_STATUS_TAG_LEN];
1862 } tor_version_t;
1864 int router_get_router_hash(const char *s, char *digest);
1865 int router_get_dir_hash(const char *s, char *digest);
1866 int router_get_runningrouters_hash(const char *s, char *digest);
1867 int router_parse_list_from_string(const char **s,
1868 routerlist_t **dest,
1869 smartlist_t *good_nickname_list,
1870 int rr_format,
1871 time_t published);
1872 int router_parse_routerlist_from_directory(const char *s,
1873 routerlist_t **dest,
1874 crypto_pk_env_t *pkey,
1875 int check_version,
1876 int write_to_cache);
1877 running_routers_t *router_parse_runningrouters(const char *str,
1878 int write_to_cache);
1879 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
1880 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
1881 addr_policy_t *router_parse_addr_policy_from_string(const char *s);
1882 int check_software_version_against_directory(const char *directory,
1883 int ignoreversion);
1884 int tor_version_parse(const char *s, tor_version_t *out);
1885 int tor_version_as_new_as(const char *platform, const char *cutoff);
1886 int tor_version_compare(tor_version_t *a, tor_version_t *b);
1887 void assert_addr_policy_ok(addr_policy_t *t);
1889 #endif