note possible alternative for how to do CVS versions; ask arma which he likes.
[tor.git] / src / or / or.h
blob47eec972ada9d9b984b146822f3189bb379a22fe
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 #include "../common/fakepoll.h"
44 #ifdef HAVE_INTTYPES_H
45 #include <inttypes.h>
46 #endif
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
49 #endif
50 #ifdef HAVE_SYS_LIMITS_H
51 #include <sys/limits.h>
52 #endif
53 #ifdef HAVE_MACHINE_LIMITS_H
54 #ifndef __FreeBSD__
55 /* FreeBSD has a bug where it complains that this file is obsolete,
56 and I should migrate to using sys/limits. It complains even when
57 I include both. */
58 #include <machine/limits.h>
59 #endif
60 #endif
61 #ifdef HAVE_SYS_TYPES_H
62 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
63 #endif
64 #ifdef HAVE_SYS_WAIT_H
65 #include <sys/wait.h>
66 #endif
67 #ifdef HAVE_SYS_FCNTL_H
68 #include <sys/fcntl.h>
69 #endif
70 #ifdef HAVE_FCNTL_H
71 #include <fcntl.h>
72 #endif
73 #ifdef HAVE_SYS_IOCTL_H
74 #include <sys/ioctl.h>
75 #endif
76 #ifdef HAVE_SYS_SOCKET_H
77 #include <sys/socket.h>
78 #endif
79 #ifdef HAVE_SYS_TIME_H
80 #include <sys/time.h>
81 #endif
82 #ifdef HAVE_SYS_STAT_H
83 #include <sys/stat.h>
84 #endif
85 #ifdef HAVE_NETINET_IN_H
86 #include <netinet/in.h>
87 #endif
88 #ifdef HAVE_ARPA_INET_H
89 #include <arpa/inet.h>
90 #endif
91 #ifdef HAVE_ERRNO_H
92 #include <errno.h>
93 #endif
94 #ifdef HAVE_ASSERT_H
95 #include <assert.h>
96 #endif
97 #ifdef HAVE_TIME_H
98 #include <time.h>
99 #endif
101 #ifdef MS_WINDOWS
102 #if (_MSC_VER <= 1300)
103 #include <winsock.h>
104 #else
105 #include <winsock2.h>
106 #include <ws2tcpip.h>
107 #endif
108 #endif
110 #ifdef MS_WINDOWS
111 #include <io.h>
112 #include <process.h>
113 #include <direct.h>
114 #include <windows.h>
115 #define snprintf _snprintf
116 #endif
118 #include "../common/crypto.h"
119 #include "../common/tortls.h"
120 #include "../common/log.h"
121 #include "../common/compat.h"
122 #include "../common/container.h"
123 #include "../common/util.h"
124 #include "../common/torgzip.h"
126 /** Upper bound on maximum simultaneous connections; can be lowered by
127 * config file. */
128 #define MAXCONNECTIONS 10000
130 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
131 #define MAX_NICKNAME_LEN 19
132 /* Hex digest plus dollar sign. */
133 #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
134 #define MAX_DIR_SIZE 500000
136 #ifdef TOR_PERF
137 /** How long do we keep DNS cache entries before purging them? */
138 #define MAX_DNS_ENTRY_AGE (150*60)
139 #else
140 #define MAX_DNS_ENTRY_AGE (15*60)
141 #endif
143 /** How often do we rotate onion keys? */
144 #define OLD_MIN_ONION_KEY_LIFETIME (12*60*60) /* twice a day */
145 #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
146 /** How often do we rotate TLS contexts? */
147 #define MAX_SSL_KEY_LIFETIME (120*60)
149 /** How old do we allow a router to get before removing it, either
150 * from the descriptor list (for dirservers) or the router list (for others)?
151 * In seconds. */
152 #define ROUTER_MAX_AGE (60*60*24)
154 typedef enum {
155 CIRC_ID_TYPE_LOWER=0,
156 CIRC_ID_TYPE_HIGHER=1
157 } circ_id_type_t;
159 #define _CONN_TYPE_MIN 3
160 /** Type for sockets listening for OR connections. */
161 #define CONN_TYPE_OR_LISTENER 3
162 /** Type for OR-to-OR or OP-to-OR connections. */
163 #define CONN_TYPE_OR 4
164 /** Type for connections from final OR to chosen destination. */
165 #define CONN_TYPE_EXIT 5
166 /** Type for sockets listening for SOCKS connections. */
167 #define CONN_TYPE_AP_LISTENER 6
168 /** Type for SOCKS connections to OP. */
169 #define CONN_TYPE_AP 7
170 /** Type for sockets listening for HTTP connections to the directory server. */
171 #define CONN_TYPE_DIR_LISTENER 8
172 /** Type for HTTP connections to the directory server. */
173 #define CONN_TYPE_DIR 9
174 /** Type for connections to local dnsworker processes. */
175 #define CONN_TYPE_DNSWORKER 10
176 /** Type for connections to local cpuworker processes. */
177 #define CONN_TYPE_CPUWORKER 11
178 /** Type for listenting for connections from user interface process */
179 #define CONN_TYPE_CONTROL_LISTENER 12
180 /** Type for connections from user interface process */
181 #define CONN_TYPE_CONTROL 13
182 #define _CONN_TYPE_MAX 13
184 #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
186 /** State for any listener connection. */
187 #define LISTENER_STATE_READY 0
189 #define _DNSWORKER_STATE_MIN 1
190 /** State for a connection to a dnsworker process that's idle. */
191 #define DNSWORKER_STATE_IDLE 1
192 /** State for a connection to a dnsworker process that's resolving a hostname. */
193 #define DNSWORKER_STATE_BUSY 2
194 #define _DNSWORKER_STATE_MAX 2
196 #define _CPUWORKER_STATE_MIN 1
197 /** State for a connection to a cpuworker process that's idle. */
198 #define CPUWORKER_STATE_IDLE 1
199 /** State for a connection to a cpuworker process that's processing a
200 * handshake. */
201 #define CPUWORKER_STATE_BUSY_ONION 2
202 #define _CPUWORKER_STATE_MAX 2
204 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
206 #define _OR_CONN_STATE_MIN 1
207 /** State for a connection to an OR: waiting for connect() to finish. */
208 #define OR_CONN_STATE_CONNECTING 1
209 /** State for a connection to an OR: SSL is handshaking, not done yet. */
210 #define OR_CONN_STATE_HANDSHAKING 2
211 /** State for a connection to an OR: Ready to send/receive cells. */
212 #define OR_CONN_STATE_OPEN 3
213 #define _OR_CONN_STATE_MAX 3
215 #define _EXIT_CONN_STATE_MIN 1
216 /** State for an exit connection: waiting for response from dns farm. */
217 #define EXIT_CONN_STATE_RESOLVING 1
218 /** State for an exit connection: waiting for connect() to finish. */
219 #define EXIT_CONN_STATE_CONNECTING 2
220 /** State for an exit connection: open and ready to transmit data. */
221 #define EXIT_CONN_STATE_OPEN 3
222 /** State for an exit connection: waiting to be removed. */
223 #define EXIT_CONN_STATE_RESOLVEFAILED 4
224 #define _EXIT_CONN_STATE_MAX 4
225 #if 0
226 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
227 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
228 #endif
230 /* the AP state values must be disjoint from the EXIT state values */
231 #define _AP_CONN_STATE_MIN 5
232 /** State for a SOCKS connection: waiting for SOCKS request. */
233 #define AP_CONN_STATE_SOCKS_WAIT 5
234 /** State for a SOCKS connection: got a y.onion URL; waiting to receive
235 * rendezvous rescriptor. */
236 #define AP_CONN_STATE_RENDDESC_WAIT 6
237 /** State for a SOCKS connection: waiting for a completed circuit. */
238 #define AP_CONN_STATE_CIRCUIT_WAIT 7
239 /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
240 #define AP_CONN_STATE_CONNECT_WAIT 8
241 /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
242 #define AP_CONN_STATE_RESOLVE_WAIT 9
243 /** State for a SOCKS connection: ready to send and receive. */
244 #define AP_CONN_STATE_OPEN 10
245 #define _AP_CONN_STATE_MAX 10
247 #define _DIR_CONN_STATE_MIN 1
248 /** State for connection to directory server: waiting for connect(). */
249 #define DIR_CONN_STATE_CONNECTING 1
250 /** State for connection to directory server: sending HTTP request. */
251 #define DIR_CONN_STATE_CLIENT_SENDING 2
252 /** State for connection to directory server: reading HTTP response. */
253 #define DIR_CONN_STATE_CLIENT_READING 3
254 /** State for connection at directory server: waiting for HTTP request. */
255 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
256 /** State for connection at directory server: sending HTTP response. */
257 #define DIR_CONN_STATE_SERVER_WRITING 5
258 #define _DIR_CONN_STATE_MAX 5
260 #define _CONTROL_CONN_STATE_MIN 1
261 #define CONTROL_CONN_STATE_OPEN 1
262 #define CONTROL_CONN_STATE_NEEDAUTH 2
263 #define _CONTROL_CONN_STATE_MAX 2
265 #define _DIR_PURPOSE_MIN 1
266 /** Purpose for connection to directory server: download a directory. */
267 #define DIR_PURPOSE_FETCH_DIR 1
268 /** Purpose for connection to directory server: download just the list
269 * of running routers. */
270 #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
271 /** Purpose for connection to directory server: download a rendezvous
272 * descriptor. */
273 #define DIR_PURPOSE_FETCH_RENDDESC 3
274 /** Purpose for connection to directory server: set after a rendezvous
275 * descriptor is downloaded. */
276 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
277 /** Purpose for connection to directory server: upload a server descriptor. */
278 #define DIR_PURPOSE_UPLOAD_DIR 5
279 /** Purpose for connection to directory server: upload a rendezvous
280 * descriptor. */
281 #define DIR_PURPOSE_UPLOAD_RENDDESC 6
282 /** Purpose for connection at a directory server. */
283 #define DIR_PURPOSE_SERVER 7
284 #define _DIR_PURPOSE_MAX 7
286 #define _EXIT_PURPOSE_MIN 1
287 #define EXIT_PURPOSE_CONNECT 1
288 #define EXIT_PURPOSE_RESOLVE 2
289 #define _EXIT_PURPOSE_MAX 2
291 /** Circuit state: I'm the OP, still haven't done all my handshakes. */
292 #define CIRCUIT_STATE_BUILDING 0
293 /** Circuit state: Waiting to process the onionskin. */
294 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
295 /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
296 #define CIRCUIT_STATE_OR_WAIT 2
297 /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
298 #define CIRCUIT_STATE_OPEN 3
300 #define _CIRCUIT_PURPOSE_MIN 1
302 /* these circuits were initiated elsewhere */
303 #define _CIRCUIT_PURPOSE_OR_MIN 1
304 /** OR-side circuit purpose: normal circuit, at OR. */
305 #define CIRCUIT_PURPOSE_OR 1
306 /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
307 #define CIRCUIT_PURPOSE_INTRO_POINT 2
308 /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
309 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
310 /** OR-side circuit purpose: At OR, both circuits have this purpose. */
311 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
312 #define _CIRCUIT_PURPOSE_OR_MAX 4
314 /* these circuits originate at this node */
316 /* here's how circ client-side purposes work:
317 * normal circuits are C_GENERAL.
318 * circuits that are c_introducing are either on their way to
319 * becoming open, or they are open and waiting for a
320 * suitable rendcirc before they send the intro.
321 * circuits that are c_introduce_ack_wait have sent the intro,
322 * but haven't gotten a response yet.
323 * circuits that are c_establish_rend are either on their way
324 * to becoming open, or they are open and have sent the
325 * establish_rendezvous cell but haven't received an ack.
326 * circuits that are c_rend_ready are open and have received a
327 * rend ack, but haven't heard from bob yet. if they have a
328 * buildstate->pending_final_cpath then they're expecting a
329 * cell from bob, else they're not.
330 * circuits that are c_rend_ready_intro_acked are open, and
331 * some intro circ has sent its intro and received an ack.
332 * circuits that are c_rend_joined are open, have heard from
333 * bob, and are talking to him.
335 /** Client-side circuit purpose: Normal circuit, with cpath. */
336 #define CIRCUIT_PURPOSE_C_GENERAL 5
337 /** Client-side circuit purpose: at Alice, connecting to intro point. */
338 #define CIRCUIT_PURPOSE_C_INTRODUCING 6
339 /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
340 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
341 /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
342 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
343 /** Client-side circuit purpose: at Alice, waiting for ack. */
344 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
345 /** Client-side circuit purpose: at Alice, waiting for Bob. */
346 #define CIRCUIT_PURPOSE_C_REND_READY 10
347 /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
348 * has been acknowledged. */
349 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
350 /** Client-side circuit purpose: at Alice, rendezvous established. */
351 #define CIRCUIT_PURPOSE_C_REND_JOINED 12
353 /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
354 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
355 /** Hidden-service-side circuit purpose: at Bob, successfully established
356 * intro. */
357 #define CIRCUIT_PURPOSE_S_INTRO 14
358 /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
359 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
360 /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
361 #define CIRCUIT_PURPOSE_S_REND_JOINED 16
362 #define _CIRCUIT_PURPOSE_MAX 16
364 /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
365 * that this OP has originated. */
366 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
367 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
369 #define RELAY_COMMAND_BEGIN 1
370 #define RELAY_COMMAND_DATA 2
371 #define RELAY_COMMAND_END 3
372 #define RELAY_COMMAND_CONNECTED 4
373 #define RELAY_COMMAND_SENDME 5
374 #define RELAY_COMMAND_EXTEND 6
375 #define RELAY_COMMAND_EXTENDED 7
376 #define RELAY_COMMAND_TRUNCATE 8
377 #define RELAY_COMMAND_TRUNCATED 9
378 #define RELAY_COMMAND_DROP 10
379 #define RELAY_COMMAND_RESOLVE 11
380 #define RELAY_COMMAND_RESOLVED 12
382 #define RELAY_COMMAND_ESTABLISH_INTRO 32
383 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
384 #define RELAY_COMMAND_INTRODUCE1 34
385 #define RELAY_COMMAND_INTRODUCE2 35
386 #define RELAY_COMMAND_RENDEZVOUS1 36
387 #define RELAY_COMMAND_RENDEZVOUS2 37
388 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
389 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
390 #define RELAY_COMMAND_INTRODUCE_ACK 40
392 #define _MIN_END_STREAM_REASON 1
393 #define END_STREAM_REASON_MISC 1
394 #define END_STREAM_REASON_RESOLVEFAILED 2
395 #define END_STREAM_REASON_CONNECTFAILED 3
396 #define END_STREAM_REASON_EXITPOLICY 4
397 #define END_STREAM_REASON_DESTROY 5
398 #define END_STREAM_REASON_DONE 6
399 #define END_STREAM_REASON_TIMEOUT 7
400 #define _MAX_END_STREAM_REASON 7
402 #define RESOLVED_TYPE_IPV4 4
403 #define RESOLVED_TYPE_IPV6 6
404 #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
405 #define RESOLVED_TYPE_ERROR 0xF1
407 /** Length of 'y' portion of 'y.onion' URL. */
408 #define REND_SERVICE_ID_LEN 16
410 #define CELL_DIRECTION_IN 1
411 #define CELL_DIRECTION_OUT 2
413 #ifdef TOR_PERF
414 #define CIRCWINDOW_START 10000
415 #define CIRCWINDOW_INCREMENT 1000
416 #define STREAMWINDOW_START 5000
417 #define STREAMWINDOW_INCREMENT 500
418 #else
419 #define CIRCWINDOW_START 1000
420 #define CIRCWINDOW_INCREMENT 100
421 #define STREAMWINDOW_START 500
422 #define STREAMWINDOW_INCREMENT 50
423 #endif
425 /* cell commands */
426 #define CELL_PADDING 0
427 #define CELL_CREATE 1
428 #define CELL_CREATED 2
429 #define CELL_RELAY 3
430 #define CELL_DESTROY 4
432 /* people behind fascist firewalls use only these ports */
433 #define REQUIRED_FIREWALL_DIRPORT 80
434 #define REQUIRED_FIREWALL_ORPORT 443
436 /* legal characters in a nickname */
437 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
439 /** Name to use in client TLS certificates if no nickname is given.*/
440 #define DEFAULT_CLIENT_NICKNAME "client"
442 #define SOCKS4_NETWORK_LEN 8
445 * Relay payload:
446 * Relay command [1 byte]
447 * Recognized [2 bytes]
448 * Stream ID [2 bytes]
449 * Partial SHA-1 [4 bytes]
450 * Length [2 bytes]
451 * Relay payload [498 bytes]
454 #define CELL_PAYLOAD_SIZE 509
455 #define CELL_NETWORK_SIZE 512
457 #define RELAY_HEADER_SIZE (1+2+2+4+2)
458 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
460 /** Parsed onion routing cell. All communication from OP-to-OR, or from
461 * OR-to-OR, is via cells. */
462 typedef struct {
463 uint16_t circ_id; /**< Circuit which received the cell. */
464 unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
465 * or DESTROY. */
466 unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
467 } cell_t;
469 /** Beginning of a RELAY cell payload. */
470 typedef struct {
471 uint8_t command; /**< The end-to-end relay command. */
472 uint16_t recognized; /**< Used to tell whether cell is for us. */
473 uint16_t stream_id; /**< Which stream is this cell associated with? */
474 char integrity[4]; /**< Used to tell whether cell is corrupted. */
475 uint16_t length; /**< How long is the payload body? */
476 } relay_header_t;
478 typedef struct buf_t buf_t;
479 typedef struct socks_request_t socks_request_t;
481 #define CONNECTION_MAGIC 0x7C3C304Eu
482 /** Description of a connection to another host or process, and associated
483 * data. */
484 struct connection_t {
485 uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
487 uint8_t type; /**< What kind of connection is this? */
488 uint8_t state; /**< Current state of this connection. */
489 uint8_t purpose; /**< Only used for DIR types currently. */
490 uint8_t wants_to_read; /**< Boolean: should we start reading again once
491 * the bandwidth throttler allows it?
493 uint8_t wants_to_write; /**< Boolean: should we start writing again once
494 * the bandwidth throttler allows reads?
496 int s; /**< Our socket; -1 if this connection is closed. */
497 int poll_index; /**< Index of this conn into the poll_array. */
498 int marked_for_close; /**< Boolean: should we close this conn on the next
499 * iteration of the main loop?
501 const char *marked_for_close_file; /**< For debugging: in which file were
502 * we marked for close? */
503 int hold_open_until_flushed; /**< Despite this connection's being marked
504 * for close, do we flush it before closing it?
507 buf_t *inbuf; /**< Buffer holding data read over this connection. */
508 int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
509 time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
511 buf_t *outbuf; /**< Buffer holding data to write over this connection. */
512 size_t outbuf_flushlen; /**< How much data should we try to flush from the
513 * outbuf? */
514 time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
516 time_t timestamp_created; /**< When was this connection_t created? */
518 uint32_t addr; /**< IP of the other side of the connection; used to identify
519 * routers, along with port. */
520 uint16_t port; /**< If non-zero, port on the other end
521 * of the connection. */
522 char *address; /**< FQDN (or IP) of the guy on the other end.
523 * strdup into this, because free_connection frees it.
525 crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
526 * signing key. */
527 char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
528 char *nickname; /**< Nickname of OR on other side (if any). */
530 /** Nickname of planned exit node -- to be used with .exit support. */
531 char *chosen_exit_name;
533 /* Used only by OR connections: */
534 tor_tls *tls; /**< TLS connection state (OR only.) */
535 uint16_t next_circ_id; /**< Which circ_id do we try to use next on
536 * this connection? This is always in the
537 * range 0..1<<15-1. (OR only.)*/
539 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
540 int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
541 int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
542 * add 'bandwidth' to this, capping it at 10*bandwidth.
543 * (OPEN ORs only)
545 circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
546 * connection, which half of the space should
547 * we use? */
549 /* Used only by DIR and AP connections: */
550 char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
551 * querying for? (DIR/AP only) */
553 /* Used only by edge connections: */
554 size_t stream_size; /**< Used for debugging. */
555 uint16_t stream_id;
556 struct connection_t *next_stream; /**< Points to the next stream at this
557 * edge, if any (Edge only). */
558 struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
559 * this conn exits at. (Edge only.) */
560 int package_window; /**< How many more relay cells can i send into the
561 * circuit? (Edge only.) */
562 int deliver_window; /**< How many more relay cells can end at me? (Edge
563 * only.) */
565 int done_sending; /**< For half-open connections; not used currently. */
566 int done_receiving; /**< For half-open connections; not used currently. */
567 char has_sent_end; /**< For debugging: set once we've set the stream end,
568 and check in circuit_about_to_close_connection(). */
569 char num_retries; /**< How many times have we re-tried beginning this stream? (Edge only) */
571 /* Used only by AP connections */
572 socks_request_t *socks_request; /**< SOCKS structure describing request (AP
573 * only.) */
575 /* Used only by control connections */
576 uint32_t event_mask;
579 typedef struct connection_t connection_t;
581 #define ADDR_POLICY_ACCEPT 1
582 #define ADDR_POLICY_REJECT 2
584 /** A linked list of policy rules */
585 struct addr_policy_t {
586 char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
587 char *string; /**< String representation of this rule. */
588 uint32_t addr; /**< Base address to accept or reject. */
589 uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
590 * a &amp; msk == <b>addr</b> &amp; msk . */
591 uint16_t prt_min; /**< Lowest port number to accept/reject. */
592 uint16_t prt_max; /**< Highest port number to accept/reject. */
594 struct addr_policy_t *next; /**< Next rule in list. */
597 /** Information about another onion router in the network. */
598 typedef struct {
599 char *address; /**< Location of OR: either a hostname or an IP address. */
600 char *nickname; /**< Human-readable OR name. */
602 uint32_t addr; /**< IPv4 address of OR, in host order. */
603 uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
604 uint16_t socks_port; /**< Port for SOCKS connections. */
605 uint16_t dir_port; /**< Port for HTTP directory connections. */
607 time_t published_on; /**< When was the information in this routerinfo_t
608 * published? */
610 crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
611 crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
612 char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
614 char *platform; /**< What software/operating system is this OR using? */
616 /* link info */
617 uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
618 * bucket per second? */
619 uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
620 /** How many bytes/s is this router known to handle? */
621 uint32_t bandwidthcapacity;
622 struct addr_policy_t *exit_policy; /**< What streams will this OR permit
623 * to exit? */
624 long uptime; /**< How many seconds the router claims to have been up */
625 /* local info */
626 int is_running; /**< As far as we know, is this OR currently running? */
627 time_t status_set_at; /**< When did we last update is_running? */
628 int is_verified; /**< Has a trusted dirserver validated this OR? */
630 smartlist_t *declared_family; /**< Nicknames of router which this router
631 * claims are its family. */
632 } routerinfo_t;
634 /** Contents of a directory of onion routers. */
635 typedef struct {
636 /** List of routerinfo_t */
637 smartlist_t *routers;
638 /** Which versions of tor are recommended by this directory? */
639 char *software_versions;
640 /** When was the most recent directory that contributed to this list
641 * published?
643 time_t published_on;
644 time_t running_routers_updated_on;
645 /** Which router is claimed to have signed it? */
646 char *signing_router;
647 } routerlist_t;
649 /** Contents of a running-routers list */
650 typedef struct running_routers_t {
651 time_t published_on; /**< When was the list marked as published? */
652 /** Which ORs are on the list? Entries may be prefixed with ! and $. */
653 smartlist_t *running_routers;
654 int is_running_routers_format; /**< Are we using the old entry format? */
655 } running_routers_t;
657 /** Holds accounting information for a single step in the layered encryption
658 * performed by a circuit. Used only at the client edge of a circuit. */
659 struct crypt_path_t {
661 /* crypto environments */
662 /** Encryption key and counter for cells heading towards the OR at this
663 * step. */
664 crypto_cipher_env_t *f_crypto;
665 /** Encryption key and counter for cells heading back from the OR at this
666 * step. */
667 crypto_cipher_env_t *b_crypto;
669 /** Digest state for cells heading towards the OR at this step. */
670 crypto_digest_env_t *f_digest; /* for integrity checking */
671 /** Digest state for cells heading away from the OR at this step. */
672 crypto_digest_env_t *b_digest;
674 /** Current state of Diffie-Hellman key negotiation with the OR at this
675 * step. */
676 crypto_dh_env_t *handshake_state;
677 /** Negotiated key material shared with the OR at this step. */
678 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
680 /** IP4 address of the OR at this step. */
681 uint32_t addr;
682 /** Port of the OR at this step. */
683 uint16_t port;
684 /** Identity key digest of the OR at this step. */
685 char identity_digest[DIGEST_LEN];
687 /** Is the circuit built to this step? Must be one of:
688 * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
689 * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
690 * and not received an EXTENDED/CREATED)
691 * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
692 uint8_t state;
693 #define CPATH_STATE_CLOSED 0
694 #define CPATH_STATE_AWAITING_KEYS 1
695 #define CPATH_STATE_OPEN 2
696 struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
697 * (The list is circular, so the last node
698 * links to the first.) */
699 struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
700 * circuit. */
702 int package_window; /**< How many bytes are we allowed to originate ending
703 * at this step? */
704 int deliver_window; /**< How many bytes are we willing to deliver originating
705 * at this step? */
708 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
710 #define DH_KEY_LEN DH_BYTES
711 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
712 CIPHER_KEY_LEN+\
713 DH_KEY_LEN)
714 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
715 #define REND_COOKIE_LEN DIGEST_LEN
717 typedef struct crypt_path_t crypt_path_t;
719 /** Information used to build a circuit. */
720 typedef struct {
721 /** Intended length of the final circuit. */
722 int desired_path_len;
723 /** Nickname of planned exit node. */
724 char *chosen_exit_name;
725 /** Identity of planned exit node. */
726 char chosen_exit_digest[DIGEST_LEN];
727 /** The crypt_path_t to append after rendezvous: used for rendezvous. */
728 struct crypt_path_t *pending_final_cpath;
729 /** How many times has building a circuit for this task failed? */
730 int failure_count;
731 } cpath_build_state_t;
733 #define CIRCUIT_MAGIC 0x35315243u
734 /** Struct for a path (circuit) through the onion routing network. */
735 struct circuit_t {
736 uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
738 int marked_for_close; /**< Should we close this circuit at the end of the
739 * main loop? */
740 const char *marked_for_close_file; /**< For debugging: in which file was this
741 * circuit marked for close? */
743 /** The IPv4 address of the OR that is next in this circuit. */
744 uint32_t n_addr;
745 /** The port for the OR that is next in this circuit. */
746 uint16_t n_port;
747 /** The OR connection that is previous in this circuit. */
748 connection_t *p_conn;
749 /** The OR connection that is next in this circuit. */
750 connection_t *n_conn;
751 /** The identity hash of n_conn. */
752 char n_conn_id_digest[DIGEST_LEN];
753 /** Linked list of AP streams associated with this circuit. */
754 connection_t *p_streams;
755 /** Linked list of Exit streams associated with this circuit. */
756 connection_t *n_streams;
757 /** Linked list of Exit streams associated with this circuit that are
758 * still being resolved. */
759 connection_t *resolving_streams;
760 /** The next stream_id that will be tried when we're attempting to
761 * construct a new AP stream originating at this circuit. */
762 uint16_t next_stream_id;
763 /** How many relay data cells can we package (read from edge streams)
764 * on this circuit before we receive a circuit-level sendme cell asking
765 * for more? */
766 int package_window;
767 /** How many relay data cells will we deliver (write to edge streams)
768 * on this circuit? When deliver_window gets low, we send some
769 * circuit-level sendme cells to indicate that we're willing to accept
770 * more. */
771 int deliver_window;
773 /** The circuit_id used in the previous (backward) hop of this circuit. */
774 uint16_t p_circ_id;
775 /** The circuit_id used in the next (forward) hop of this circuit. */
776 uint16_t n_circ_id;
778 /** The cipher used by intermediate hops for cells heading toward the
779 * OP. */
780 crypto_cipher_env_t *p_crypto;
781 /** The cipher used by intermediate hops for cells heading away from
782 * the OP. */
783 crypto_cipher_env_t *n_crypto;
785 /** The integrity-checking digest used by intermediate hops, for
786 * cells packaged here and heading towards the OP.
788 crypto_digest_env_t *p_digest;
789 /** The integrity-checking digest used by intermediate hops, for
790 * cells packaged at the OP and arriving here.
792 crypto_digest_env_t *n_digest;
794 /** Build state for this circuit. It includes the intended path
795 * length, the chosen exit router, rendezvous information, etc.
797 cpath_build_state_t *build_state;
798 /** The doubly-linked list of crypt_path_t entries, one per hop,
799 * for this circuit. This includes ciphers for each hop,
800 * integrity-checking digests for each hop, and package/delivery
801 * windows for each hop.
803 * The cpath field is defined only when we are the circuit's origin.
805 crypt_path_t *cpath;
807 /** For storage while passing to cpuworker, or while n_conn is pending. */
808 char onionskin[ONIONSKIN_CHALLENGE_LEN];
810 char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
812 time_t timestamp_created; /**< When was this circuit created? */
813 time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
814 * circuit is clean. */
816 uint8_t state; /**< Current status of this circuit. */
817 uint8_t purpose; /**< Why are we creating this circuit? */
820 * The rend_query field holds y portion of y.onion (nul-terminated)
821 * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
822 * for a hidden service, or is S_*.
824 char rend_query[REND_SERVICE_ID_LEN+1];
826 /** The rend_pk_digest field holds a hash of location-hidden service's
827 * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
829 char rend_pk_digest[DIGEST_LEN];
831 /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
832 * C_ESTABLISH_REND. Filled with zeroes otherwise.
834 char rend_cookie[REND_COOKIE_LEN];
836 /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
837 * is not marked for close. */
838 struct circuit_t *rend_splice;
840 /** Quasi-global identifier for this circuit; used for control.c */
841 /* XXXX009 NM This can get re-used after 2**32 circuits. */
842 uint32_t global_identifier;
844 struct circuit_t *next; /**< Next circuit in linked list. */
847 typedef struct circuit_t circuit_t;
849 #define ALLOW_UNVERIFIED_ENTRY 1
850 #define ALLOW_UNVERIFIED_EXIT 2
851 #define ALLOW_UNVERIFIED_MIDDLE 4
852 #define ALLOW_UNVERIFIED_RENDEZVOUS 8
853 #define ALLOW_UNVERIFIED_INTRODUCTION 16
855 typedef struct exit_redirect_t {
856 uint32_t addr;
857 uint32_t mask;
858 uint16_t port_min;
859 uint16_t port_max;
861 int is_redirect;
862 uint32_t addr_dest;
863 uint16_t port_dest;
864 } exit_redirect_t;
866 /** Configuration options for a Tor process */
867 typedef struct {
868 /** What should the tor process actually do? */
869 enum {
870 CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
871 } command;
872 const char *command_arg; /**< Argument for command-line option. */
874 struct config_line_t *OldLogOptions; /**< List of configuration lines
875 * for logfiles, old style. */
877 struct config_line_t *Logs; /**< New-style list of configuration lines
878 * for logs */
880 char *DebugLogFile; /**< Where to send verbose log messages. */
881 char *DataDirectory; /**< OR only: where to store long-term data. */
882 char *Nickname; /**< OR only: nickname of this onion router. */
883 char *Address; /**< OR only: configured address for this onion router. */
884 char *PidFile; /**< Where to store PID of Tor process. */
886 char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
887 * as exits. */
888 char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
889 * as entry points. */
890 int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
891 * stop building circuits? */
892 int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
893 * stop building circuits? */
894 char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
895 * use in circuits. */
897 char *RendNodes; /**< Comma-separated list of nicknames used as introduction
898 * points. */
899 char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
900 * as introduction points. */
902 smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
903 int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
904 struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
905 struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
906 struct config_line_t *DirPolicy; /**< Lists of dir policy components */
907 /** Addresses to bind for listening for SOCKS connections. */
908 struct config_line_t *SocksBindAddress;
909 /** Addresses to bind for listening for OR connections. */
910 struct config_line_t *ORBindAddress;
911 /** Addresses to bind for listening for directory connections. */
912 struct config_line_t *DirBindAddress;
913 /** Local address to bind outbound sockets */
914 char *OutboundBindAddress;
915 struct config_line_t *RecommendedVersions;
916 /**< Directory server only: which versions of
917 * Tor should we tell users to run? */
918 char *User; /**< Name of user to run Tor as. */
919 char *Group; /**< Name of group to run Tor as. */
920 double PathlenCoinWeight; /**< Parameter used to configure average path
921 * length (alpha in geometric distribution). */
922 int ORPort; /**< Port to listen on for OR connections. */
923 int SocksPort; /**< Port to listen on for SOCKS connections. */
924 int ControlPort; /**< Port to listen on for control connections. */
925 int DirPort; /**< Port to listen on for directory connections. */
926 int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
927 int ClientOnly; /**< Boolean: should we never evolve into a server role? */
928 int MaxConn; /**< Maximum number of simultaneous connections. */
929 int IgnoreVersion; /**< If true, run no matter what versions of Tor the
930 * directory recommends. */
931 int RunAsDaemon; /**< If true, run in the background. (Unix only) */
932 int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
933 smartlist_t *FirewallPorts; /** Which ports our firewall allows. */
934 int DirFetchPeriod; /**< How often do we fetch new directories? */
935 int DirPostPeriod; /**< How often do we post our server descriptor to the
936 * authoritative directory servers? */
937 int RendPostPeriod; /**< How often do we post each rendezvous service
938 * descriptor? Remember to publish them independently. */
939 int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
940 int KeepalivePeriod; /**< How often do we send padding cells to keep
941 * connections alive? */
942 int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
943 * to wait simultaneously before we start dropping
944 * them? */
945 int NewCircuitPeriod; /**< How long do we use a circuit before building
946 * a new one? */
947 uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
948 * use in a second? */
949 uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
950 * use in a second? */
951 int NumCpus; /**< How many CPUs should we try to use? */
952 int RunTesting; /**< If true, create testing circuits to measure how well the
953 * other ORs are running. */
954 struct config_line_t *RendConfigLines; /**< List of configuration lines
955 * for rendezvous services. */
956 char *ContactInfo; /**< Contact info to be published in the directory */
958 char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
959 uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
960 uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
962 struct config_line_t *DirServers; /**< List of configuration lines
963 * for directory servers. */
964 char *MyFamily; /**< Declared family for this OR. */
965 struct config_line_t *NodeFamilies; /**< List of config lines for
966 * node families */
967 struct config_line_t *RedirectExit; /**< List of config lines for simple
968 * addr/port redirection */
969 smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
970 int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
971 * interval starts */
972 char *AccountingStart; /** How long is the accounting interval, and when
973 * does it start? */
974 uint64_t AccountingMax; /**< How many bytes do we allow per accounting
975 * interval before hibernation? 0 for "never
976 * hibernate." */
977 int _AccountingMaxKB; /**< How many KB do we allow per accounting
978 * interval before hibernation? 0 for "never
979 * hibernate." (Based on a deprecated option)*/
981 char *HashedControlPassword; /**< Base64-encoded hash of a password for
982 * the control system. */
983 int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
984 * the control system? */
985 } or_options_t;
987 #define MAX_SOCKS_REPLY_LEN 1024
988 #define MAX_SOCKS_ADDR_LEN 256
989 #define SOCKS_COMMAND_CONNECT 0x01
990 #define SOCKS_COMMAND_RESOLVE 0xF0
991 /** State of a SOCKS request from a user to an OP */
992 struct socks_request_t {
993 char socks_version; /**< Which version of SOCKS did the client use? */
994 int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
995 size_t replylen; /**< Length of <b>reply</b>. */
996 char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
997 * we want to specify our own socks reply,
998 * rather than using the default socks4 or
999 * socks5 socks reply. We use this for the
1000 * two-stage socks5 handshake.
1002 int has_finished; /**< Has the SOCKS handshake finished? */
1003 char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
1004 uint16_t port; /**< What port did the client ask to connect to? */
1007 /* all the function prototypes go here */
1009 /********************************* buffers.c ***************************/
1011 buf_t *buf_new(void);
1012 buf_t *buf_new_with_capacity(size_t size);
1013 void buf_free(buf_t *buf);
1014 void buf_clear(buf_t *buf);
1016 size_t buf_datalen(const buf_t *buf);
1017 size_t buf_capacity(const buf_t *buf);
1018 const char *_buf_peek_raw_buffer(const buf_t *buf);
1020 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
1021 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
1023 int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
1024 int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
1026 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
1027 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
1028 int fetch_from_buf_http(buf_t *buf,
1029 char **headers_out, size_t max_headerlen,
1030 char **body_out, size_t *body_used, size_t max_bodylen);
1031 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
1032 int fetch_from_buf_control(buf_t *buf, uint16_t *len_out, uint16_t *type_out,
1033 char **body_out);
1035 void assert_buf_ok(buf_t *buf);
1037 /********************************* circuitbuild.c **********************/
1039 char *circuit_list_path(circuit_t *circ, int verbose);
1040 void circuit_log_path(int severity, circuit_t *circ);
1041 void circuit_rep_hist_note_result(circuit_t *circ);
1042 void circuit_dump_by_conn(connection_t *conn, int severity);
1043 circuit_t *circuit_establish_circuit(uint8_t purpose,
1044 const char *exit_digest);
1045 void circuit_n_conn_done(connection_t *or_conn, int status);
1046 int circuit_send_next_onion_skin(circuit_t *circ);
1047 int circuit_extend(cell_t *cell, circuit_t *circ);
1048 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
1049 int circuit_finish_handshake(circuit_t *circ, char *reply);
1050 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
1051 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
1052 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
1054 /********************************* circuitlist.c ***********************/
1056 extern const char *circuit_state_to_string[];
1057 void circuit_close_all_marked(void);
1058 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
1059 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
1060 circuit_t *circuit_get_by_conn(connection_t *conn);
1061 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
1062 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
1063 const char *digest, uint8_t purpose);
1064 circuit_t *circuit_get_rendezvous(const char *cookie);
1065 int circuit_count_building(uint8_t purpose);
1066 circuit_t *circuit_get_youngest_clean_open(uint8_t purpose);
1067 int _circuit_mark_for_close(circuit_t *circ);
1069 #define circuit_mark_for_close(c) \
1070 do { \
1071 if (_circuit_mark_for_close(c)<0) { \
1072 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
1073 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
1074 } else { \
1075 c->marked_for_close_file = __FILE__; \
1076 c->marked_for_close = __LINE__; \
1078 } while (0)
1080 void assert_cpath_layer_ok(const crypt_path_t *cp);
1081 void assert_circuit_ok(const circuit_t *c);
1083 /********************************* circuituse.c ************************/
1085 void circuit_expire_building(time_t now);
1086 int circuit_stream_is_being_handled(connection_t *conn);
1087 void circuit_build_needed_circs(time_t now);
1088 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
1089 void circuit_about_to_close_connection(connection_t *conn);
1090 void circuit_has_opened(circuit_t *circ);
1091 void circuit_build_failed(circuit_t *circ);
1092 circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname);
1093 circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest);
1094 void circuit_reset_failure_count(int timeout);
1095 int connection_ap_handshake_attach_circuit(connection_t *conn);
1097 /********************************* command.c ***************************/
1099 void command_process_cell(cell_t *cell, connection_t *conn);
1101 extern unsigned long stats_n_padding_cells_processed;
1102 extern unsigned long stats_n_create_cells_processed;
1103 extern unsigned long stats_n_created_cells_processed;
1104 extern unsigned long stats_n_relay_cells_processed;
1105 extern unsigned long stats_n_destroy_cells_processed;
1107 /********************************* config.c ***************************/
1109 struct config_line_t {
1110 char *key;
1111 char *value;
1112 struct config_line_t *next;
1115 or_options_t *get_options(void);
1116 void set_options(or_options_t *new_val);
1117 int options_act(void);
1119 int config_get_lines(char *string, struct config_line_t **result);
1120 void config_free_lines(struct config_line_t *front);
1121 int config_trial_assign(struct config_line_t *list, int reset);
1122 int resolve_my_address(const char *address, uint32_t *addr);
1123 void options_init(or_options_t *options);
1124 int init_from_config(int argc, char **argv);
1125 int config_init_logs(or_options_t *options, int validate_only);
1126 int config_parse_addr_policy(struct config_line_t *cfg,
1127 struct addr_policy_t **dest);
1128 void addr_policy_free(struct addr_policy_t *p);
1129 int config_option_is_recognized(const char *key);
1130 struct config_line_t *config_get_assigned_option(or_options_t *options,
1131 const char *key);
1132 struct config_line_t *config_line_prepend(struct config_line_t *front,
1133 const char *key, const char *val);
1134 char *config_dump_options(or_options_t *options, int minimal);
1135 int save_current_config(void);
1137 /********************************* connection.c ***************************/
1139 #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
1140 "Unknown" : conn_type_to_string[(t)])
1142 extern const char *conn_type_to_string[];
1143 extern const char *conn_state_to_string[][_CONN_TYPE_MAX+1];
1145 connection_t *connection_new(int type);
1146 void connection_free(connection_t *conn);
1147 void connection_free_all(void);
1148 void connection_about_to_close_connection(connection_t *conn);
1149 void connection_close_immediate(connection_t *conn);
1150 int _connection_mark_for_close(connection_t *conn);
1152 #define connection_mark_for_close(c) \
1153 do { \
1154 if (_connection_mark_for_close(c)<0) { \
1155 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
1156 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
1157 } else { \
1158 c->marked_for_close_file = __FILE__; \
1159 c->marked_for_close = __LINE__; \
1161 } while (0)
1163 void connection_expire_held_open(void);
1165 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
1166 int retry_all_listeners(int force);
1168 void connection_bucket_init(void);
1169 void connection_bucket_refill(struct timeval *now);
1171 int connection_handle_read(connection_t *conn);
1173 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
1175 int connection_wants_to_flush(connection_t *conn);
1176 int connection_outbuf_too_full(connection_t *conn);
1177 int connection_handle_write(connection_t *conn);
1178 void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
1180 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
1181 connection_t *connection_get_by_identity_digest(const char *digest, int type);
1183 connection_t *connection_get_by_type(int type);
1184 connection_t *connection_get_by_type_state(int type, int state);
1185 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
1186 connection_t *connection_get_by_type_rendquery(int type, const char *rendquery);
1188 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
1189 #define connection_has_pending_tls_data(conn) \
1190 ((conn)->type == CONN_TYPE_OR && \
1191 (conn)->state == OR_CONN_STATE_OPEN && \
1192 tor_tls_get_pending_bytes((conn)->tls))
1193 int connection_is_listener(connection_t *conn);
1194 int connection_state_is_open(connection_t *conn);
1195 int connection_state_is_connecting(connection_t *conn);
1197 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
1199 void assert_connection_ok(connection_t *conn, time_t now);
1200 int connection_or_nonopen_was_started_here(connection_t *conn);
1202 /********************************* connection_edge.c ***************************/
1204 int connection_edge_reached_eof(connection_t *conn);
1205 int connection_edge_process_inbuf(connection_t *conn, int package_partial);
1206 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
1207 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
1208 int connection_edge_finished_flushing(connection_t *conn);
1209 int connection_edge_finished_connecting(connection_t *conn);
1211 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
1212 int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
1214 int connection_ap_make_bridge(char *address, uint16_t port);
1215 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
1216 size_t replylen, int status);
1217 void connection_ap_handshake_socks_resolved(connection_t *conn,
1218 int answer_type,
1219 size_t answer_len,
1220 const char *answer);
1222 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
1223 int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
1224 void connection_exit_connect(connection_t *conn);
1225 int connection_edge_is_rendezvous_stream(connection_t *conn);
1226 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
1227 void connection_ap_expire_beginning(void);
1228 void connection_ap_attach_pending(void);
1230 void parse_socks_policy(void);
1231 int socks_policy_permits_address(uint32_t addr);
1233 void client_dns_init(void);
1234 uint32_t client_dns_lookup_entry(const char *address);
1235 int client_dns_incr_failures(const char *address);
1236 void client_dns_set_entry(const char *address, uint32_t val);
1237 void client_dns_clean(void);
1238 void set_exit_redirects(smartlist_t *lst);
1239 int parse_address(char *address);
1241 /********************************* connection_or.c ***************************/
1243 int connection_or_reached_eof(connection_t *conn);
1244 int connection_or_process_inbuf(connection_t *conn);
1245 int connection_or_finished_flushing(connection_t *conn);
1246 int connection_or_finished_connecting(connection_t *conn);
1248 connection_t *connection_or_connect(uint32_t addr, uint16_t port,
1249 const char *id_digest);
1251 int connection_tls_start_handshake(connection_t *conn, int receiving);
1252 int connection_tls_continue_handshake(connection_t *conn);
1254 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
1255 void connection_or_update_nickname(connection_t *conn);
1257 /********************************* control.c ***************************/
1259 typedef enum circuit_status_event_t {
1260 CIRC_EVENT_LAUNCHED = 0,
1261 CIRC_EVENT_BUILT = 1,
1262 CIRC_EVENT_EXTENDED = 2,
1263 CIRC_EVENT_FAILED = 3,
1264 CIRC_EVENT_CLOSED = 4,
1265 } circuit_status_event_t;
1267 typedef enum stream_status_event_t {
1268 STREAM_EVENT_SENT_CONNECT = 0,
1269 STREAM_EVENT_SENT_RESOLVE = 1,
1270 STREAM_EVENT_SUCCEEDED = 2,
1271 STREAM_EVENT_FAILED = 3,
1272 STREAM_EVENT_CLOSED = 4
1273 } stream_status_event_t;
1275 typedef enum or_conn_status_event_t {
1276 OR_CONN_EVENT_LAUNCHED = 0,
1277 OR_CONN_EVENT_CONNECTED = 1,
1278 OR_CONN_EVENT_FAILED = 2,
1279 OR_CONN_EVENT_CLOSED = 3,
1280 } or_conn_status_event_t;
1282 int connection_control_finished_flushing(connection_t *conn);
1283 int connection_control_reached_eof(connection_t *conn);
1284 int connection_control_process_inbuf(connection_t *conn);
1286 int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
1287 int control_event_stream_status(connection_t *conn, stream_status_event_t e);
1288 int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
1289 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
1290 void control_event_logmsg(int severity, const char *msg);
1292 int init_cookie_authentication(int enabled);
1294 /********************************* cpuworker.c *****************************/
1296 void cpu_init(void);
1297 void cpuworkers_rotate(void);
1298 int connection_cpu_finished_flushing(connection_t *conn);
1299 int connection_cpu_reached_eof(connection_t *conn);
1300 int connection_cpu_process_inbuf(connection_t *conn);
1301 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
1302 void *task);
1304 /********************************* directory.c ***************************/
1306 int dir_policy_permits_address(uint32_t addr);
1307 void directory_post_to_dirservers(uint8_t purpose, const char *payload,
1308 size_t payload_len);
1309 void directory_get_from_dirserver(uint8_t purpose, const char *resource);
1310 int connection_dir_reached_eof(connection_t *conn);
1311 int connection_dir_process_inbuf(connection_t *conn);
1312 int connection_dir_finished_flushing(connection_t *conn);
1313 int connection_dir_finished_connecting(connection_t *conn);
1314 void parse_dir_policy(void);
1316 /********************************* dirserv.c ***************************/
1318 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1319 int dirserv_parse_fingerprint_file(const char *fname);
1320 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
1321 void dirserv_free_fingerprint_list(void);
1322 const char *dirserv_get_nickname_by_digest(const char *digest);
1323 int dirserv_add_descriptor(const char **desc);
1324 int dirserv_load_from_directory_string(const char *dir);
1325 void dirserv_free_descriptors(void);
1326 void dirserv_remove_old_servers(int age);
1327 int dirserv_dump_directory_to_string(char *s, size_t maxlen,
1328 crypto_pk_env_t *private_key);
1329 void directory_set_dirty(void);
1330 size_t dirserv_get_directory(const char **cp, int compress);
1331 size_t dirserv_get_runningrouters(const char **rr, int compress);
1332 void dirserv_set_cached_directory(const char *directory, time_t when,
1333 int is_running_routers);
1335 /********************************* dns.c ***************************/
1337 void dns_init(void);
1338 int connection_dns_finished_flushing(connection_t *conn);
1339 int connection_dns_reached_eof(connection_t *conn);
1340 int connection_dns_process_inbuf(connection_t *conn);
1341 void dnsworkers_rotate(void);
1342 void connection_dns_remove(connection_t *conn);
1343 void assert_connection_edge_not_dns_pending(connection_t *conn);
1344 void assert_all_pending_dns_resolves_ok(void);
1345 void dns_cancel_pending_resolve(char *question);
1346 int dns_resolve(connection_t *exitconn);
1348 /********************************* hibernate.c **********************/
1350 int accounting_parse_options(or_options_t *options, int validate_only);
1351 int accounting_is_enabled(or_options_t *options);
1352 void configure_accounting(time_t now);
1353 void accounting_run_housekeeping(time_t now);
1354 void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
1355 int accounting_record_bandwidth_usage(time_t now);
1356 void hibernate_begin_shutdown(void);
1357 int we_are_hibernating(void);
1358 void consider_hibernation(time_t now);
1360 /********************************* main.c ***************************/
1362 int connection_add(connection_t *conn);
1363 int connection_remove(connection_t *conn);
1364 int connection_in_array(connection_t *conn);
1366 void get_connection_array(connection_t ***array, int *n);
1368 void connection_watch_events(connection_t *conn, short events);
1369 int connection_is_reading(connection_t *conn);
1370 void connection_stop_reading(connection_t *conn);
1371 void connection_start_reading(connection_t *conn);
1373 int connection_is_writing(connection_t *conn);
1374 void connection_stop_writing(connection_t *conn);
1375 void connection_start_writing(connection_t *conn);
1377 void directory_has_arrived(time_t now);
1378 int authdir_mode(or_options_t *options);
1379 int clique_mode(or_options_t *options);
1380 int server_mode(or_options_t *options);
1381 int advertised_server_mode(void);
1382 int proxy_mode(or_options_t *options);
1384 void handle_signals(int is_parent);
1385 void tor_cleanup(void);
1387 int tor_main(int argc, char *argv[]);
1389 /********************************* onion.c ***************************/
1391 int onion_pending_add(circuit_t *circ);
1392 circuit_t *onion_next_task(void);
1393 void onion_pending_remove(circuit_t *circ);
1395 int onion_skin_create(crypto_pk_env_t *router_key,
1396 crypto_dh_env_t **handshake_state_out,
1397 char *onion_skin_out);
1399 int onion_skin_server_handshake(char *onion_skin,
1400 crypto_pk_env_t *private_key,
1401 crypto_pk_env_t *prev_private_key,
1402 char *handshake_reply_out,
1403 char *key_out,
1404 size_t key_out_len);
1406 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
1407 char *handshake_reply,
1408 char *key_out,
1409 size_t key_out_len);
1411 /********************************* relay.c ***************************/
1413 extern unsigned long stats_n_relay_cells_relayed;
1414 extern unsigned long stats_n_relay_cells_delivered;
1416 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
1417 int cell_direction);
1419 void relay_header_pack(char *dest, const relay_header_t *src);
1420 void relay_header_unpack(relay_header_t *dest, const char *src);
1421 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
1422 int relay_command, const char *payload,
1423 size_t payload_len, crypt_path_t *cpath_layer);
1424 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
1425 void connection_edge_consider_sending_sendme(connection_t *conn);
1427 extern uint64_t stats_n_data_cells_packaged;
1428 extern uint64_t stats_n_data_bytes_packaged;
1429 extern uint64_t stats_n_data_cells_received;
1430 extern uint64_t stats_n_data_bytes_received;
1432 /********************************* rephist.c ***************************/
1434 void rep_hist_init(void);
1435 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1436 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1437 void rep_hist_note_disconnect(const char* nickname, time_t when);
1438 void rep_hist_note_connection_died(const char* nickname, time_t when);
1439 void rep_hist_note_extend_succeeded(const char *from_name,
1440 const char *to_name);
1441 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1442 void rep_hist_dump_stats(time_t now, int severity);
1443 void rep_hist_note_bytes_read(int num_bytes, time_t when);
1444 void rep_hist_note_bytes_written(int num_bytes, time_t when);
1445 int rep_hist_bandwidth_assess(void);
1446 char *rep_hist_get_bandwidth_lines(void);
1447 void rep_history_clean(time_t before);
1449 /********************************* rendclient.c ***************************/
1451 void rend_client_introcirc_has_opened(circuit_t *circ);
1452 void rend_client_rendcirc_has_opened(circuit_t *circ);
1453 int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
1454 void rend_client_refetch_renddesc(const char *query);
1455 int rend_client_remove_intro_point(char *failed_intro, const char *query);
1456 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
1457 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1458 void rend_client_desc_fetched(char *query, int status);
1460 char *rend_client_get_random_intro(char *query);
1462 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1464 /********************************* rendcommon.c ***************************/
1466 typedef struct rend_service_descriptor_t {
1467 crypto_pk_env_t *pk;
1468 time_t timestamp;
1469 int n_intro_points;
1470 char **intro_points;
1471 } rend_service_descriptor_t;
1473 int rend_cmp_service_ids(const char *one, const char *two);
1475 void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
1476 const char *payload);
1478 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1479 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1480 crypto_pk_env_t *key,
1481 char **str_out,
1482 size_t *len_out);
1483 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
1484 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1486 typedef struct rend_cache_entry_t {
1487 size_t len; /* Length of desc */
1488 time_t received; /* When did we get the descriptor? */
1489 char *desc; /* Service descriptor */
1490 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1491 } rend_cache_entry_t;
1493 void rend_cache_init(void);
1494 void rend_cache_clean(void);
1495 int rend_valid_service_id(const char *query);
1496 int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
1497 int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
1498 int rend_cache_store(const char *desc, size_t desc_len);
1500 /********************************* rendservice.c ***************************/
1502 int rend_config_services(or_options_t *options, int validate_only);
1503 int rend_service_load_keys(void);
1504 void rend_services_init(void);
1505 void rend_services_introduce(void);
1506 void rend_consider_services_upload(time_t now);
1508 void rend_service_intro_has_opened(circuit_t *circuit);
1509 int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
1510 void rend_service_rendezvous_has_opened(circuit_t *circuit);
1511 int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
1512 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1513 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1514 void rend_service_dump_stats(int severity);
1516 /********************************* rendmid.c *******************************/
1517 int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
1518 int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
1519 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1520 int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
1522 /********************************* router.c ***************************/
1524 void set_onion_key(crypto_pk_env_t *k);
1525 crypto_pk_env_t *get_onion_key(void);
1526 crypto_pk_env_t *get_previous_onion_key(void);
1527 time_t get_onion_key_set_at(void);
1528 void set_identity_key(crypto_pk_env_t *k);
1529 crypto_pk_env_t *get_identity_key(void);
1530 int identity_key_is_set(void);
1531 void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
1532 int init_keys(void);
1533 crypto_pk_env_t *init_key_from_file(const char *fname);
1534 void rotate_onion_key(void);
1535 void router_set_bandwidth_capacity(int bw);
1536 int router_get_bandwidth_capacity(void);
1538 void router_retry_connections(void);
1539 int router_is_clique_mode(routerinfo_t *router);
1540 void router_upload_dir_desc_to_dirservers(int force);
1541 void mark_my_descriptor_dirty(void);
1542 int router_compare_to_my_exit_policy(connection_t *conn);
1543 routerinfo_t *router_get_my_routerinfo(void);
1544 const char *router_get_my_descriptor(void);
1545 int router_is_me(routerinfo_t *router);
1546 int router_rebuild_descriptor(int force);
1547 int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
1548 crypto_pk_env_t *ident_key);
1549 int is_legal_nickname(const char *s);
1550 int is_legal_nickname_or_hexdigest(const char *s);
1552 /********************************* routerlist.c ***************************/
1554 typedef struct trusted_dir_server_t {
1555 char *address;
1556 uint32_t addr;
1557 uint16_t dir_port;
1558 char digest[DIGEST_LEN];
1559 int is_running;
1560 } trusted_dir_server_t;
1562 int router_reload_router_list(void);
1563 void router_get_trusted_dir_servers(smartlist_t **outp);
1564 routerinfo_t *router_pick_directory_server(int requireothers, int fascistfirewall, int for_running_routers);
1565 trusted_dir_server_t *router_pick_trusteddirserver(int requireothers, int fascistfirewall);
1566 int all_trusted_directory_servers_down(void);
1567 struct smartlist_t;
1568 void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
1569 void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
1570 int router_nickname_is_in_list(routerinfo_t *router, const char *list);
1571 routerinfo_t *routerlist_find_my_routerinfo(void);
1572 int router_nickname_matches(routerinfo_t *router, const char *nickname);
1573 int router_is_unreliable_router(routerinfo_t *router, int need_uptime, int need_bw);
1574 routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
1575 routerinfo_t *router_choose_random_node(const char *preferred,
1576 const char *excluded,
1577 struct smartlist_t *excludedsmartlist,
1578 int preferuptime, int preferbandwidth,
1579 int allow_unverified, int strict);
1580 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
1581 routerinfo_t *router_get_by_nickname(const char *nickname);
1582 routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
1583 routerinfo_t *router_get_by_digest(const char *digest);
1584 int router_digest_is_trusted_dir(const char *digest);
1585 void router_get_routerlist(routerlist_t **prouterlist);
1586 time_t routerlist_get_published_time(void);
1587 void routerlist_free(routerlist_t *routerlist);
1588 void routerinfo_free(routerinfo_t *router);
1589 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
1590 void router_mark_as_down(const char *digest);
1591 void routerlist_remove_old_routers(int age);
1592 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
1593 int check_version);
1594 int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
1595 struct addr_policy_t *policy);
1596 #define ADDR_POLICY_ACCEPTED 0
1597 #define ADDR_POLICY_REJECTED -1
1598 #define ADDR_POLICY_UNKNOWN 1
1599 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
1600 int router_exit_policy_rejects_all(routerinfo_t *router);
1601 void running_routers_free(running_routers_t *rr);
1602 void routerlist_update_from_runningrouters(routerlist_t *list,
1603 running_routers_t *rr);
1604 int routers_update_status_from_entry(smartlist_t *routers,
1605 time_t list_time,
1606 const char *s,
1607 int rr_format);
1608 int router_update_status_from_smartlist(routerinfo_t *r,
1609 time_t list_time,
1610 smartlist_t *running_list,
1611 int rr_format);
1612 void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
1613 void clear_trusted_dir_servers(void);
1615 /********************************* routerparse.c ************************/
1617 #define MAX_STATUS_TAG_LEN 32
1618 /** Structure to hold parsed Tor versions. This is a little messier
1619 * than we would like it to be, because we changed version schemes with 0.1.0.
1621 * Before 0.1.0, versions were of the format:
1622 * MAJOR.MINOR.MICRO(status(PATCHLEVEL))?(-cvs)?
1623 * where MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, status is one of
1624 * "pre" (for an alpha release), "rc" (for a release candidate), or "." for a
1625 * release. As a special case, "a.b.c" was equivalent to "a.b.c.0". We
1626 * compare the elements in order (major, minor, micro, status, patchlevel,
1627 * cvs), with "cvs" preceding non-cvs.
1629 * We would start each development branch with a final version in mind: say,
1630 * "0.0.8". Our first pre-release would be "0.0.8pre1", followed by (for
1631 * example) "0.0.8pre2-cvs", "0.0.8pre2", "0.0.8pre3-cvs", "0.0.8rc1",
1632 * "0.0.8rc2-cvs", and "0.0.8rc2". Finally, we'd release 0.0.8. The stable
1633 * CVS branch would then be versioned "0.0.8.1-cvs", and any eventual bugfix
1634 * release would be "0.0.8.1".
1636 * After 0.1.0, versions are of the format:
1637 * MAJOR.MINOR.MICRO(.PATCHLEVEL([-.]status_tag)?)?
1638 * As before, MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, with an absent
1639 * number equivalent to 0. All versions _should_ be distinguishable purely by
1640 * those four numbers; the status tag is purely informational. If we *do*
1641 * encounter two versions that differ only by status tag, we compare them
1642 * lexically.
1644 * Now, we start each development branch with (say) 0.1.1.1-cvs. The
1645 * patchlevel increments consistently as the status tag changes, for example,
1646 * as in: 0.1.1.2-alpha, 0.1.1.3-cvs, 0.1.1.4-alpha, 0.1.1.5-cvs, 0.1.1.6-rc
1647 * 0.1.1.7-cvs, 0.1.1.8-rc, 0.1.1.9-cvs. Eventually, we release 0.1.1.10.
1648 * The stable CVS repository gets the version 0.1.1.11-maint_cvs; the
1649 * next patch release is 0.1.1.12.
1651 * XXXX(Alternatively, we could go: 0.1.1.1-alpha, 0.1.1.1-alpha_cvs,
1652 * 0.1.1.2-alpha, 0.1.1.2-alpha_cvs . This wouldn't violate our
1653 * only-one-release-per-number rule, since CVS versions aren't
1654 * released. Roger?)
1656 typedef struct tor_version_t {
1657 int major;
1658 int minor;
1659 int micro;
1660 /** Release status. For version in the post-0.1 format, this is always
1661 * VER_RELEASE. */
1662 enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
1663 int patchlevel;
1664 /** CVS status. For version in the post-0.1 format, this is always
1665 * IS_NOT_CVS */
1666 enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
1667 char status_tag[MAX_STATUS_TAG_LEN];
1668 } tor_version_t;
1670 int router_get_router_hash(const char *s, char *digest);
1671 int router_get_dir_hash(const char *s, char *digest);
1672 int router_get_runningrouters_hash(const char *s, char *digest);
1673 int router_parse_list_from_string(const char **s,
1674 routerlist_t **dest,
1675 smartlist_t *good_nickname_list,
1676 int rr_format,
1677 time_t published);
1678 int router_parse_routerlist_from_directory(const char *s,
1679 routerlist_t **dest,
1680 crypto_pk_env_t *pkey,
1681 int check_version);
1682 running_routers_t *router_parse_runningrouters(const char *str);
1683 routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
1684 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
1685 struct addr_policy_t *router_parse_addr_policy_from_string(const char *s);
1686 int check_software_version_against_directory(const char *directory,
1687 int ignoreversion);
1688 int tor_version_parse(const char *s, tor_version_t *out);
1689 int tor_version_as_new_as(const char *platform, const char *cutoff);
1690 int tor_version_compare(tor_version_t *a, tor_version_t *b);
1692 #endif