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