Make Tor build on win32 with VC6 without warnings.
[tor.git] / src / or / or.h
blob83d1bb7267055b1375c26a197fe3c230ea92a827
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __OR_H
6 #define __OR_H
8 #include "orconfig.h"
9 #ifdef MS_WINDOWS
10 #define WIN32_WINNT 0x400
11 #define _WIN32_WINNT 0x400
12 #define WIN32_LEAN_AND_MEAN
13 /* Number of fds that select will accept; default is 64. */
14 #define FD_SETSIZE 512
15 #endif
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <limits.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26 #ifdef HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29 #ifdef HAVE_NETDB_H
30 #include <netdb.h>
31 #endif
32 #ifdef HAVE_CTYPE_H
33 #include <ctype.h>
34 #endif
35 #include "../common/torint.h"
36 #include "../common/fakepoll.h"
37 #ifdef HAVE_SYS_LIMITS_H
38 #include <sys/limits.h>
39 #endif
40 #ifdef HAVE_MACHINE_LIMITS_H
41 #include <machine/limits.h>
42 #endif
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
45 #endif
46 #ifdef HAVE_SYS_WAIT_H
47 #include <sys/wait.h>
48 #endif
49 #ifdef HAVE_SYS_FCNTL_H
50 #include <sys/fcntl.h>
51 #endif
52 #ifdef HAVE_FCNTL_H
53 #include <fcntl.h>
54 #endif
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
58 #ifdef HAVE_SYS_SOCKET_H
59 #include <sys/socket.h>
60 #endif
61 #ifdef HAVE_SYS_TIME_H
62 #include <sys/time.h>
63 #endif
64 #ifdef HAVE_SYS_STAT_H
65 #include <sys/stat.h>
66 #endif
67 #ifdef HAVE_NETINET_IN_H
68 #include <netinet/in.h>
69 #endif
70 #ifdef HAVE_ARPA_INET_H
71 #include <arpa/inet.h>
72 #endif
73 #ifdef HAVE_ERRNO_H
74 #include <errno.h>
75 #endif
76 #ifdef HAVE_ASSERT_H
77 #include <assert.h>
78 #endif
79 #ifdef HAVE_TIME_H
80 #include <time.h>
81 #endif
82 #ifdef HAVE_WINSOCK_H
83 #include <winsock.h>
84 #endif
85 #if _MSC_VER > 1300
86 #include <winsock2.h>
87 #include <ws2tcpip.h>
88 #elif defined(_MSC_VER)
89 #include <winsock.h>
90 #endif
92 #ifdef MS_WINDOWS
93 #include <io.h>
94 #include <process.h>
95 #include <direct.h>
96 #include <windows.h>
97 #define snprintf _snprintf
98 #endif
100 #include "../common/crypto.h"
101 #include "../common/tortls.h"
102 #include "../common/log.h"
103 #include "../common/util.h"
105 #define MAXCONNECTIONS 1000 /* upper bound on max connections.
106 can be lowered by config file */
108 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
109 #define MAX_NICKNAME_LEN 19
110 #define MAX_DIR_SIZE 500000
112 #ifdef TOR_PERF
113 #define MAX_DNS_ENTRY_AGE (150*60)
114 #else
115 #define MAX_DNS_ENTRY_AGE (15*60)
116 #endif
118 #define MIN_ONION_KEY_LIFETIME (120*60)
119 #define MAX_SSL_KEY_LIFETIME (120*60)
121 #define CIRC_ID_TYPE_LOWER 0
122 #define CIRC_ID_TYPE_HIGHER 1
124 #define _CONN_TYPE_MIN 3
125 #define CONN_TYPE_OR_LISTENER 3
126 #define CONN_TYPE_OR 4
127 #define CONN_TYPE_EXIT 5
128 #define CONN_TYPE_AP_LISTENER 6
129 #define CONN_TYPE_AP 7
130 #define CONN_TYPE_DIR_LISTENER 8
131 #define CONN_TYPE_DIR 9
132 #define CONN_TYPE_DNSWORKER 10
133 #define CONN_TYPE_CPUWORKER 11
134 #define _CONN_TYPE_MAX 11
136 #define LISTENER_STATE_READY 0
138 #define _DNSWORKER_STATE_MIN 1
139 #define DNSWORKER_STATE_IDLE 1
140 #define DNSWORKER_STATE_BUSY 2
141 #define _DNSWORKER_STATE_MAX 2
143 #define _CPUWORKER_STATE_MIN 1
144 #define CPUWORKER_STATE_IDLE 1
145 #define CPUWORKER_STATE_BUSY_ONION 2
146 #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
147 #define _CPUWORKER_STATE_MAX 3
149 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
151 #define _OR_CONN_STATE_MIN 1
152 #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
153 #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
154 #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
155 #define _OR_CONN_STATE_MAX 3
157 #define _EXIT_CONN_STATE_MIN 1
158 #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
159 #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
160 #define EXIT_CONN_STATE_OPEN 3
161 #define EXIT_CONN_STATE_RESOLVEFAILED 4 /* waiting to be removed */
162 #define _EXIT_CONN_STATE_MAX 4
163 #if 0
164 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
165 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
166 #endif
168 /* the AP state values must be disjoint from the EXIT state values */
169 #define _AP_CONN_STATE_MIN 5
170 #define AP_CONN_STATE_SOCKS_WAIT 5
171 #define AP_CONN_STATE_RENDDESC_WAIT 6
172 #define AP_CONN_STATE_CIRCUIT_WAIT 7
173 #define AP_CONN_STATE_CONNECT_WAIT 8
174 #define AP_CONN_STATE_OPEN 9
175 #define _AP_CONN_STATE_MAX 9
177 #define _DIR_CONN_STATE_MIN 1
178 #define DIR_CONN_STATE_CONNECTING 1
179 #define DIR_CONN_STATE_CLIENT_SENDING 2
180 #define DIR_CONN_STATE_CLIENT_READING 3
181 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
182 #define DIR_CONN_STATE_SERVER_WRITING 5
183 #define _DIR_CONN_STATE_MAX 5
185 #define _DIR_PURPOSE_MIN 1
186 #define DIR_PURPOSE_FETCH_DIR 1
187 #define DIR_PURPOSE_FETCH_RENDDESC 2
188 #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 3
189 #define DIR_PURPOSE_UPLOAD_DIR 4
190 #define DIR_PURPOSE_UPLOAD_RENDDESC 5
191 #define DIR_PURPOSE_SERVER 6
192 #define _DIR_PURPOSE_MAX 6
194 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
195 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
196 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
197 #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
199 #define _CIRCUIT_PURPOSE_MIN 1
201 /* these circuits were initiated elsewhere */
202 #define _CIRCUIT_PURPOSE_OR_MIN 1
203 #define CIRCUIT_PURPOSE_OR 1 /* normal circuit, at OR. */
204 #define CIRCUIT_PURPOSE_INTRO_POINT 2 /* At OR, from Bob, waiting for intro from Alices */
205 #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3 /* At OR, from Alice, waiting for Bob */
206 #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4 /* At OR, both circuits have this purpose */
207 #define _CIRCUIT_PURPOSE_OR_MAX 4
209 /* these circuits originate at this node */
211 /* here's how circ client-side purposes work:
212 * normal circuits are C_GENERAL.
213 * circuits that are c_introducing are either on their way to
214 * becoming open, or they are open and waiting for a
215 * suitable rendcirc before they send the intro.
216 * circuits that are c_introduce_ack_wait have sent the intro,
217 * but haven't gotten a response yet.
218 * circuits that are c_establish_rend are either on their way
219 * to becoming open, or they are open and have sent the
220 * establish_rendezvous cell but haven't received an ack.
221 * circuits that are c_rend_ready are open and have received a
222 * rend ack, but haven't heard from bob yet. if they have a
223 * buildstate->pending_final_cpath then they're expecting a
224 * cell from bob, else they're not.
225 * circuits that are c_rend_ready_intro_acked are open, and
226 * some intro circ has sent its intro and received an ack.
227 * circuits that are c_rend_joined are open, have heard from
228 * bob, and are talking to him.
230 #define CIRCUIT_PURPOSE_C_GENERAL 5 /* normal circuit, with cpath */
231 #define CIRCUIT_PURPOSE_C_INTRODUCING 6 /* at Alice, connecting to intro point */
232 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7 /* at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK */
233 #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8 /* at Alice, introduced and acked, closing */
235 #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9 /* at Alice, waiting for ack */
236 #define CIRCUIT_PURPOSE_C_REND_READY 10 /* at Alice, waiting for Bob */
237 #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11 /* at Alice, waiting for Bob */
238 #define CIRCUIT_PURPOSE_C_REND_JOINED 12 /* at Alice, rendezvous established */
240 #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13 /* at Bob, waiting for introductions */
241 #define CIRCUIT_PURPOSE_S_INTRO 14 /* at Bob, successfully established intro */
242 #define CIRCUIT_PURPOSE_S_CONNECT_REND 15 /* at Bob, connecting to rend point */
244 #define CIRCUIT_PURPOSE_S_REND_JOINED 16 /* at Bob, rendezvous established.*/
245 #define _CIRCUIT_PURPOSE_MAX 16
247 #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
248 #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
250 #define RELAY_COMMAND_BEGIN 1
251 #define RELAY_COMMAND_DATA 2
252 #define RELAY_COMMAND_END 3
253 #define RELAY_COMMAND_CONNECTED 4
254 #define RELAY_COMMAND_SENDME 5
255 #define RELAY_COMMAND_EXTEND 6
256 #define RELAY_COMMAND_EXTENDED 7
257 #define RELAY_COMMAND_TRUNCATE 8
258 #define RELAY_COMMAND_TRUNCATED 9
259 #define RELAY_COMMAND_DROP 10
260 #define RELAY_COMMAND_RESOLVE 11
261 #define RELAY_COMMAND_RESOLVED 12
263 #define RELAY_COMMAND_ESTABLISH_INTRO 32
264 #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
265 #define RELAY_COMMAND_INTRODUCE1 34
266 #define RELAY_COMMAND_INTRODUCE2 35
267 #define RELAY_COMMAND_RENDEZVOUS1 36
268 #define RELAY_COMMAND_RENDEZVOUS2 37
269 /* DOCDOC Spec these next two. */
270 #define RELAY_COMMAND_INTRO_ESTABLISHED 38
271 #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
272 #define RELAY_COMMAND_INTRODUCE_ACK 40
274 #define _MIN_END_STREAM_REASON 1
275 #define END_STREAM_REASON_MISC 1
276 #define END_STREAM_REASON_RESOLVEFAILED 2
277 #define END_STREAM_REASON_CONNECTFAILED 3
278 #define END_STREAM_REASON_EXITPOLICY 4
279 #define END_STREAM_REASON_DESTROY 5
280 #define END_STREAM_REASON_DONE 6
281 #define END_STREAM_REASON_TIMEOUT 7
282 #define _MAX_END_STREAM_REASON 7
284 /* length of 'y' portion of 'y.onion' URL. */
285 #define REND_SERVICE_ID_LEN 16
287 /* Reasons used by connection_mark_for_close */
288 #define CLOSE_REASON_UNUSED_OR_CONN 100
290 #define CELL_DIRECTION_IN 1
291 #define CELL_DIRECTION_OUT 2
293 #ifdef TOR_PERF
294 #define CIRCWINDOW_START 10000
295 #define CIRCWINDOW_INCREMENT 1000
296 #define STREAMWINDOW_START 5000
297 #define STREAMWINDOW_INCREMENT 500
298 #else
299 #define CIRCWINDOW_START 1000
300 #define CIRCWINDOW_INCREMENT 100
301 #define STREAMWINDOW_START 500
302 #define STREAMWINDOW_INCREMENT 50
303 #endif
305 /* cell commands */
306 #define CELL_PADDING 0
307 #define CELL_CREATE 1
308 #define CELL_CREATED 2
309 #define CELL_RELAY 3
310 #define CELL_DESTROY 4
312 /* legal characters in a nickname */
313 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
315 #define SOCKS4_NETWORK_LEN 8
318 * Relay payload:
319 * Relay command [1 byte]
320 * Recognized [2 bytes]
321 * Stream ID [2 bytes]
322 * Partial SHA-1 [4 bytes]
323 * Length [2 bytes]
324 * Relay payload [498 bytes]
327 #define CELL_PAYLOAD_SIZE 509
328 #define CELL_NETWORK_SIZE 512
330 #define RELAY_HEADER_SIZE (1+2+2+4+2)
331 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
333 /* cell definition */
334 typedef struct {
335 uint16_t circ_id;
336 unsigned char command;
337 unsigned char payload[CELL_PAYLOAD_SIZE];
338 } cell_t;
340 typedef struct {
341 uint8_t command;
342 uint16_t recognized;
343 uint16_t stream_id;
344 char integrity[4];
345 uint16_t length;
346 } relay_header_t;
348 typedef struct buf_t buf_t;
349 typedef struct socks_request_t socks_request_t;
351 #define CONNECTION_MAGIC 0x7C3C304Eu
352 struct connection_t {
353 uint32_t magic; /* for memory debugging */
355 uint8_t type;
356 uint8_t state;
357 uint8_t purpose; /* only used for DIR types currently */
358 uint8_t wants_to_read; /* should we start reading again once
359 * the bandwidth throttler allows it?
361 uint8_t wants_to_write; /* should we start writing again once
362 * the bandwidth throttler allows reads?
364 int s; /* our socket */
365 int poll_index; /* index of this conn into the poll_array */
366 int marked_for_close; /* should we close this conn on the next
367 * iteration of the main loop?
369 char *marked_for_close_file; /* for debugging: in which file were we marked
370 * for close? */
371 int hold_open_until_flushed;
373 buf_t *inbuf;
374 int inbuf_reached_eof; /* did read() return 0 on this conn? */
375 time_t timestamp_lastread; /* when was the last time poll() said we could read? */
377 buf_t *outbuf;
378 int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
379 time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
381 time_t timestamp_created; /* when was this connection_t created? */
383 uint32_t addr; /* these two uniquely identify a router. Both in host order. */
384 uint16_t port; /* if non-zero, they identify the guy on the other end
385 * of the connection. */
386 char *address; /* FQDN (or IP) of the guy on the other end.
387 * strdup into this, because free_connection frees it
389 crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
390 char *nickname;
392 /* Used only by OR connections: */
393 tor_tls *tls;
394 uint16_t next_circ_id; /* Which circ_id do we try to use next on this connection?
395 * This is always in the range 0..1<<15-1.*/
397 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
398 int bandwidth; /* connection bandwidth. */
399 int receiver_bucket; /* when this hits 0, stop receiving. Every second we
400 * add 'bandwidth' to this, capping it at 10*bandwidth.
403 /* Used only by DIR and AP connections: */
404 char rend_query[REND_SERVICE_ID_LEN+1];
406 /* Used only by edge connections: */
407 uint16_t stream_id;
408 struct connection_t *next_stream; /* points to the next stream at this edge, if any */
409 struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
410 int package_window; /* how many more relay cells can i send into the circuit? */
411 int deliver_window; /* how many more relay cells can end at me? */
413 int done_sending; /* for half-open connections; not used currently */
414 int done_receiving;
415 char has_sent_end; /* for debugging: set once we've set the stream end,
416 and check in circuit_about_to_close_connection() */
417 char num_retries; /* how many times have we re-tried beginning this stream? */
419 /* Used only by AP connections */
420 socks_request_t *socks_request;
423 typedef struct connection_t connection_t;
425 #define EXIT_POLICY_ACCEPT 1
426 #define EXIT_POLICY_REJECT 2
428 struct exit_policy_t {
429 char policy_type;
430 char *string;
431 uint32_t addr;
432 uint32_t msk;
433 uint16_t prt_min;
434 uint16_t prt_max;
436 struct exit_policy_t *next;
439 /* config stuff we know about the other ORs in the network */
440 typedef struct {
441 char *address;
442 char *nickname;
444 uint32_t addr; /* all host order */
445 uint16_t or_port;
446 uint16_t socks_port;
447 uint16_t dir_port;
449 time_t published_on;
451 crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
452 crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
454 int is_running;
456 char *platform;
458 /* link info */
459 uint32_t bandwidthrate;
460 uint32_t bandwidthburst;
461 struct exit_policy_t *exit_policy;
462 } routerinfo_t;
464 #define MAX_ROUTERS_IN_DIR 1024
465 typedef struct {
466 smartlist_t *routers;
467 char *software_versions;
468 time_t published_on;
469 } routerlist_t;
471 struct crypt_path_t {
473 /* crypto environments */
474 crypto_cipher_env_t *f_crypto;
475 crypto_cipher_env_t *b_crypto;
477 crypto_digest_env_t *f_digest; /* for integrity checking */
478 crypto_digest_env_t *b_digest;
480 crypto_dh_env_t *handshake_state;
481 char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
483 uint32_t addr;
484 uint16_t port;
486 uint8_t state;
487 #define CPATH_STATE_CLOSED 0
488 #define CPATH_STATE_AWAITING_KEYS 1
489 #define CPATH_STATE_OPEN 2
490 struct crypt_path_t *next;
491 struct crypt_path_t *prev; /* doubly linked list */
493 int package_window;
494 int deliver_window;
497 #define DH_KEY_LEN DH_BYTES
498 #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
499 CIPHER_KEY_LEN+\
500 DH_KEY_LEN)
501 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
502 #define REND_COOKIE_LEN DIGEST_LEN
504 typedef struct crypt_path_t crypt_path_t;
506 typedef struct {
507 int desired_path_len;
508 /* nickname of planned exit node */
509 char *chosen_exit;
510 /* cpath to append after rendezvous. */
511 struct crypt_path_t *pending_final_cpath;
512 /* How many times has building a circuit for this task failed? */
513 int failure_count;
514 } cpath_build_state_t;
516 /* struct for a path (circuit) through the network */
517 #define CIRCUIT_MAGIC 0x35315243u
518 struct circuit_t {
519 uint32_t magic; /* for memory debugging. */
521 int marked_for_close; /* Should we close this circuit at the end of the main
522 * loop? */
523 char *marked_for_close_file;
525 uint32_t n_addr;
526 uint16_t n_port;
527 connection_t *p_conn;
528 connection_t *n_conn; /* for the OR conn, if there is one */
529 connection_t *p_streams;
530 connection_t *n_streams;
531 uint16_t next_stream_id;
532 int package_window;
533 int deliver_window;
535 uint16_t p_circ_id; /* circuit identifiers */
536 uint16_t n_circ_id;
538 crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
539 crypto_cipher_env_t *n_crypto;
541 crypto_digest_env_t *p_digest; /* for integrity checking, */
542 crypto_digest_env_t *n_digest; /* intermediate hops only */
544 cpath_build_state_t *build_state;
545 crypt_path_t *cpath;
547 char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
548 char handshake_digest[DIGEST_LEN]; /* Stores KH for intermediate hops */
550 time_t timestamp_created;
551 time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
553 uint8_t state;
554 uint8_t purpose;
557 * rend_query holds y portion of y.onion (nul-terminated) if purpose
558 * is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL for a
559 * hidden service, or is S_*.
561 char rend_query[REND_SERVICE_ID_LEN+1];
563 /* rend_pk_digest holds a hash of location-hidden service's PK if
564 * purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING
566 char rend_pk_digest[DIGEST_LEN];
568 /* Holds rendezvous cookie if purpose is REND_POINT_WAITING or
569 * C_ESTABLISH_REND. Filled with zeroes otherwise.
571 char rend_cookie[REND_COOKIE_LEN];
573 /* Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
574 * is not marked for close. */
575 struct circuit_t *rend_splice;
577 struct circuit_t *next;
580 typedef struct circuit_t circuit_t;
582 typedef struct circuit_data_rend_point_t {
583 /* for CIRCUIT_PURPOSE_INTRO_POINT (at OR, from Bob, waiting for intro) */
584 char rend_cookie[20];
585 } circuit_data_intro_point_t;
587 typedef struct {
588 char *LogLevel;
589 char *LogFile;
590 char *DebugLogFile;
591 char *DataDirectory;
592 char *RouterFile;
593 char *Nickname;
594 char *Address;
595 char *PidFile;
597 char *ExitNodes;
598 char *EntryNodes;
599 char *ExcludeNodes;
601 char *RendNodes;
602 char *RendExcludeNodes;
604 char *ExitPolicy;
605 char *SocksBindAddress;
606 char *ORBindAddress;
607 char *DirBindAddress;
608 char *RecommendedVersions;
609 char *User;
610 char *Group;
611 double PathlenCoinWeight;
612 int ORPort;
613 int SocksPort;
614 int DirPort;
615 int MaxConn;
616 int TrafficShaping;
617 int LinkPadding;
618 int IgnoreVersion;
619 int RunAsDaemon;
620 int DirRebuildPeriod;
621 int DirFetchPostPeriod;
622 int KeepalivePeriod;
623 int MaxOnionsPending;
624 int NewCircuitPeriod;
625 int BandwidthRate;
626 int BandwidthBurst;
627 int NumCpus;
628 int loglevel;
629 int RunTesting;
630 struct config_line_t *RendConfigLines;
631 } or_options_t;
633 /* XXX are these good enough defaults? */
634 #define MAX_SOCKS_REPLY_LEN 1024
635 #define MAX_SOCKS_ADDR_LEN 256
636 struct socks_request_t {
637 char socks_version;
638 int replylen;
639 char reply[MAX_SOCKS_REPLY_LEN];
640 int has_finished; /* has the socks handshake finished? */
641 char address[MAX_SOCKS_ADDR_LEN];
642 uint16_t port;
645 /* all the function prototypes go here */
647 /********************************* buffers.c ***************************/
649 int find_on_inbuf(char *string, int string_len, buf_t *buf);
651 buf_t *buf_new();
652 buf_t *buf_new_with_capacity(size_t size);
653 void buf_free(buf_t *buf);
654 void buf_clear(buf_t *buf);
656 size_t buf_datalen(const buf_t *buf);
657 size_t buf_capacity(const buf_t *buf);
658 const char *_buf_peek_raw_buffer(const buf_t *buf);
660 int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
661 int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
663 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
664 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
666 int write_to_buf(const char *string, int string_len, buf_t *buf);
667 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
668 int fetch_from_buf_http(buf_t *buf,
669 char **headers_out, int max_headerlen,
670 char **body_out, int *body_used, int max_bodylen);
671 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
673 void assert_buf_ok(buf_t *buf);
675 /********************************* circuit.c ***************************/
677 extern char *circuit_state_to_string[];
678 void circuit_add(circuit_t *circ);
679 void circuit_remove(circuit_t *circ);
680 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
681 void circuit_close_all_marked(void);
682 void circuit_free(circuit_t *circ);
683 void circuit_free_cpath(crypt_path_t *cpath);
684 int _circuit_mark_for_close(circuit_t *circ);
686 #define circuit_mark_for_close(c) \
687 do { \
688 if (_circuit_mark_for_close(c)<0) { \
689 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
690 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
691 } else { \
692 c->marked_for_close_file = __FILE__; \
693 c->marked_for_close = __LINE__; \
695 } while (0)
698 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
699 circuit_t *circuit_get_by_conn(connection_t *conn);
700 circuit_t *circuit_get_best(connection_t *conn,
701 int must_be_open, uint8_t purpose);
702 circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
703 circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *circuit,
704 const char *servid, uint8_t purpose);
705 circuit_t *circuit_get_rendezvous(const char *cookie);
707 void circuit_expire_building(time_t now);
708 int circuit_count_building(uint8_t purpose);
709 int circuit_stream_is_being_handled(connection_t *conn);
710 void circuit_build_needed_circs(time_t now);
712 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
713 int cell_direction);
714 int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
715 int cell_direction, crypt_path_t *layer_hint);
717 void circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
718 int circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
719 void circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
721 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
722 void circuit_about_to_close_connection(connection_t *conn);
724 void circuit_log_path(int severity, circuit_t *circ);
725 void circuit_dump_by_conn(connection_t *conn, int severity);
727 void circuit_expire_unused_circuits(void);
728 circuit_t *circuit_launch_new(uint8_t purpose, const char *exit_nickname);
729 void circuit_increment_failure_count(void);
730 void circuit_reset_failure_count(void);
731 void circuit_n_conn_open(connection_t *or_conn);
732 int circuit_send_next_onion_skin(circuit_t *circ);
733 int circuit_extend(cell_t *cell, circuit_t *circ);
734 #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
735 int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data,int reverse);
736 int circuit_finish_handshake(circuit_t *circ, char *reply);
737 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
739 void assert_cpath_ok(const crypt_path_t *c);
740 void assert_cpath_layer_ok(const crypt_path_t *c);
741 void assert_circuit_ok(const circuit_t *c);
743 extern unsigned long stats_n_relay_cells_relayed;
744 extern unsigned long stats_n_relay_cells_delivered;
746 /********************************* command.c ***************************/
748 void command_process_cell(cell_t *cell, connection_t *conn);
750 extern unsigned long stats_n_padding_cells_processed;
751 extern unsigned long stats_n_create_cells_processed;
752 extern unsigned long stats_n_created_cells_processed;
753 extern unsigned long stats_n_relay_cells_processed;
754 extern unsigned long stats_n_destroy_cells_processed;
756 /********************************* config.c ***************************/
758 struct config_line_t {
759 char *key;
760 char *value;
761 struct config_line_t *next;
764 int config_assign_default_dirservers(void);
765 int getconfig(int argc, char **argv, or_options_t *options);
767 /********************************* connection.c ***************************/
769 #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
770 "Unknown" : conn_type_to_string[(t)])
772 extern char *conn_type_to_string[];
774 connection_t *connection_new(int type);
775 void connection_free(connection_t *conn);
776 void connection_free_all(void);
777 void connection_close_immediate(connection_t *conn);
778 int _connection_mark_for_close(connection_t *conn, char reason);
780 #define connection_mark_for_close(c,r) \
781 do { \
782 if (_connection_mark_for_close(c,r)<0) { \
783 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
784 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
785 } else { \
786 c->marked_for_close_file = __FILE__; \
787 c->marked_for_close = __LINE__; \
789 } while (0)
791 void connection_expire_held_open(void);
793 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
795 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
796 int retry_all_connections(void);
798 void connection_bucket_init(void);
799 void connection_bucket_refill(struct timeval *now);
801 int connection_handle_read(connection_t *conn);
802 int connection_read_to_buf(connection_t *conn);
804 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
805 int connection_find_on_inbuf(char *string, int len, connection_t *conn);
807 int connection_wants_to_flush(connection_t *conn);
808 int connection_outbuf_too_full(connection_t *conn);
809 int connection_handle_write(connection_t *conn);
810 void connection_write_to_buf(const char *string, int len, connection_t *conn);
812 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
813 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
815 connection_t *connection_get_by_type(int type);
816 connection_t *connection_get_by_type_state(int type, int state);
817 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
818 connection_t *connection_get_by_type_rendquery(int type, char *rendquery);
820 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
821 #define connection_has_pending_tls_data(conn) \
822 ((conn)->type == CONN_TYPE_OR && \
823 (conn)->state == OR_CONN_STATE_OPEN && \
824 tor_tls_get_pending_bytes((conn)->tls))
825 int connection_is_listener(connection_t *conn);
826 int connection_state_is_open(connection_t *conn);
828 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
830 int connection_process_inbuf(connection_t *conn);
831 int connection_finished_flushing(connection_t *conn);
833 void assert_connection_ok(connection_t *conn, time_t now);
835 /********************************* connection_edge.c ***************************/
837 void relay_header_pack(char *dest, const relay_header_t *src);
838 void relay_header_unpack(relay_header_t *dest, const char *src);
839 int connection_edge_process_inbuf(connection_t *conn);
840 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
841 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
842 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
843 int relay_command, const char *payload,
844 int payload_len, crypt_path_t *cpath_layer);
845 int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
846 connection_t *conn,
847 crypt_path_t *layer_hint);
848 int connection_edge_finished_flushing(connection_t *conn);
850 int connection_edge_package_raw_inbuf(connection_t *conn);
852 int connection_ap_handshake_attach_circuit(connection_t *conn);
853 int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
855 int connection_ap_make_bridge(char *address, uint16_t port);
857 void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
858 int replylen, char success);
860 void connection_exit_connect(connection_t *conn);
861 int connection_edge_is_rendezvous_stream(connection_t *conn);
862 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
863 void connection_ap_expire_beginning(void);
864 void connection_ap_attach_pending(void);
866 extern uint64_t stats_n_data_cells_packaged;
867 extern uint64_t stats_n_data_bytes_packaged;
868 extern uint64_t stats_n_data_cells_received;
869 extern uint64_t stats_n_data_bytes_received;
871 void client_dns_init(void);
872 void client_dns_clean(void);
874 /********************************* connection_or.c ***************************/
876 int connection_or_process_inbuf(connection_t *conn);
877 int connection_or_finished_flushing(connection_t *conn);
879 void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
880 connection_t *connection_or_connect(routerinfo_t *router);
882 int connection_tls_start_handshake(connection_t *conn, int receiving);
883 int connection_tls_continue_handshake(connection_t *conn);
885 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
887 /********************************* cpuworker.c *****************************/
889 void cpu_init(void);
890 void cpuworkers_rotate(void);
891 int connection_cpu_finished_flushing(connection_t *conn);
892 int connection_cpu_process_inbuf(connection_t *conn);
893 int cpuworker_main(void *data);
894 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
895 void *task);
897 /********************************* directory.c ***************************/
899 void directory_initiate_command(routerinfo_t *router, int purpose,
900 const char *payload, int payload_len);
901 int connection_dir_process_inbuf(connection_t *conn);
902 int connection_dir_finished_flushing(connection_t *conn);
904 /********************************* dns.c ***************************/
906 void dns_init(void);
907 int connection_dns_finished_flushing(connection_t *conn);
908 int connection_dns_process_inbuf(connection_t *conn);
909 void connection_dns_remove(connection_t *conn);
910 void assert_connection_edge_not_dns_pending(connection_t *conn);
911 void assert_all_pending_dns_resolves_ok(void);
912 void dns_cancel_pending_resolve(char *question);
913 int dns_resolve(connection_t *exitconn);
915 /********************************* main.c ***************************/
917 int connection_add(connection_t *conn);
918 int connection_remove(connection_t *conn);
919 void connection_set_poll_socket(connection_t *conn);
921 void get_connection_array(connection_t ***array, int *n);
923 void connection_watch_events(connection_t *conn, short events);
924 int connection_is_reading(connection_t *conn);
925 void connection_stop_reading(connection_t *conn);
926 void connection_start_reading(connection_t *conn);
928 int connection_is_writing(connection_t *conn);
929 void connection_stop_writing(connection_t *conn);
930 void connection_start_writing(connection_t *conn);
932 void directory_has_arrived(void);
934 int main(int argc, char *argv[]);
936 /********************************* onion.c ***************************/
938 int decide_circ_id_type(char *local_nick, char *remote_nick);
940 int onion_pending_add(circuit_t *circ);
941 circuit_t *onion_next_task(void);
942 void onion_pending_remove(circuit_t *circ);
944 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
947 void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
948 int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
949 routerinfo_t **router_out);
951 int onion_skin_create(crypto_pk_env_t *router_key,
952 crypto_dh_env_t **handshake_state_out,
953 char *onion_skin_out);
955 int onion_skin_server_handshake(char *onion_skin,
956 crypto_pk_env_t *private_key,
957 crypto_pk_env_t *prev_private_key,
958 char *handshake_reply_out,
959 char *key_out,
960 int key_out_len);
962 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
963 char *handshake_reply,
964 char *key_out,
965 int key_out_len);
967 cpath_build_state_t *onion_new_cpath_build_state(uint8_t purpose,
968 const char *exit_nickname);
970 /********************************* router.c ***************************/
972 void set_onion_key(crypto_pk_env_t *k);
973 crypto_pk_env_t *get_onion_key(void);
974 crypto_pk_env_t *get_previous_onion_key(void);
975 time_t get_onion_key_set_at(void);
976 void set_identity_key(crypto_pk_env_t *k);
977 crypto_pk_env_t *get_identity_key(void);
978 int init_keys(void);
979 crypto_pk_env_t *init_key_from_file(const char *fname);
980 void rotate_onion_key(void);
982 void router_retry_connections(void);
983 void router_upload_dir_desc_to_dirservers(void);
984 void router_post_to_dirservers(uint8_t purpose, const char *payload, int payload_len);
985 int router_compare_to_my_exit_policy(connection_t *conn);
986 routerinfo_t *router_get_my_routerinfo(void);
987 const char *router_get_my_descriptor(void);
988 int router_is_me(routerinfo_t *router);
989 int router_rebuild_descriptor(void);
990 int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
991 crypto_pk_env_t *ident_key);
993 /********************************* routerlist.c ***************************/
995 routerinfo_t *router_pick_directory_server(void);
996 struct smartlist_t;
997 routerinfo_t *router_choose_random_node(routerlist_t *dir,
998 char *preferred, char *excluded,
999 struct smartlist_t *excludedsmartlist);
1000 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
1001 routerinfo_t *router_get_by_nickname(char *nickname);
1002 void router_get_routerlist(routerlist_t **prouterlist);
1003 void routerinfo_free(routerinfo_t *router);
1004 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
1005 void router_mark_as_down(char *nickname);
1006 int router_set_routerlist_from_file(char *routerfile);
1007 int router_set_routerlist_from_string(const char *s);
1008 int router_get_dir_hash(const char *s, char *digest);
1009 int router_get_router_hash(const char *s, char *digest);
1010 int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
1011 routerinfo_t *router_get_entry_from_string(const char *s, const char *end);
1012 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
1013 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
1014 struct exit_policy_t *policy);
1015 #define ADDR_POLICY_ACCEPTED 0
1016 #define ADDR_POLICY_REJECTED -1
1017 #define ADDR_POLICY_UNKNOWN 1
1018 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
1019 int router_exit_policy_rejects_all(routerinfo_t *router);
1021 /********************************* dirserv.c ***************************/
1022 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
1023 int dirserv_parse_fingerprint_file(const char *fname);
1024 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
1025 void dirserv_free_fingerprint_list();
1026 int dirserv_add_descriptor(const char **desc);
1027 int dirserv_init_from_directory_string(const char *dir);
1028 void dirserv_free_descriptors();
1029 int dirserv_dump_directory_to_string(char *s, unsigned int maxlen,
1030 crypto_pk_env_t *private_key);
1031 void directory_set_dirty(void);
1032 size_t dirserv_get_directory(const char **cp);
1033 void dirserv_remove_old_servers(void);
1036 /********************************* rephist.c ***************************/
1038 void rep_hist_init(void);
1039 void rep_hist_note_connect_failed(const char* nickname, time_t when);
1040 void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
1041 void rep_hist_note_disconnect(const char* nickname, time_t when);
1042 void rep_hist_note_connection_died(const char* nickname, time_t when);
1043 void rep_hist_note_extend_succeeded(const char *from_name,
1044 const char *to_name);
1045 void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
1046 void rep_hist_dump_stats(time_t now, int severity);
1048 /********************************* rendclient.c ***************************/
1050 void rend_client_introcirc_is_open(circuit_t *circ);
1051 void rend_client_rendcirc_is_open(circuit_t *circ);
1052 int rend_client_introduction_acked(circuit_t *circ, const char *request, int request_len);
1053 void rend_client_refetch_renddesc(char *query);
1054 int rend_client_remove_intro_point(char *failed_intro, char *query);
1055 int rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len);
1056 int rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len);
1057 void rend_client_desc_fetched(char *query, int success);
1059 int rend_cmp_service_ids(const char *one, const char *two);
1060 char *rend_client_get_random_intro(char *query);
1061 int rend_parse_rendezvous_address(char *address);
1063 int rend_client_send_establish_rendezvous(circuit_t *circ);
1064 int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
1066 /********************************* rendcommon.c ***************************/
1068 typedef struct rend_service_descriptor_t {
1069 crypto_pk_env_t *pk;
1070 time_t timestamp;
1071 int n_intro_points;
1072 char **intro_points;
1073 } rend_service_descriptor_t;
1075 void rend_process_relay_cell(circuit_t *circ, int command, int length,
1076 const char *payload);
1078 void rend_service_descriptor_free(rend_service_descriptor_t *desc);
1079 int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
1080 crypto_pk_env_t *key,
1081 char **str_out,
1082 int *len_out);
1083 rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, int len);
1084 int rend_get_service_id(crypto_pk_env_t *pk, char *out);
1086 typedef struct rend_cache_entry_t {
1087 int len; /* Length of desc */
1088 time_t received; /* When did we get the descriptor? */
1089 char *desc; /* Service descriptor */
1090 rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
1091 } rend_cache_entry_t;
1093 void rend_cache_init(void);
1094 void rend_cache_clean(void);
1095 int rend_valid_service_id(char *query);
1096 int rend_cache_lookup_desc(char *query, const char **desc, int *desc_len);
1097 int rend_cache_lookup_entry(char *query, rend_cache_entry_t **entry_out);
1098 int rend_cache_store(char *desc, int desc_len);
1100 /********************************* rendservice.c ***************************/
1102 int rend_config_services(or_options_t *options);
1103 int rend_service_load_keys(void);
1104 void rend_services_init(void);
1105 void rend_services_introduce(void);
1106 void rend_services_upload(int force);
1108 void rend_service_intro_is_ready(circuit_t *circuit);
1109 int rend_service_intro_established(circuit_t *circuit, const char *request, int request_len);
1110 void rend_service_rendezvous_is_ready(circuit_t *circuit);
1111 int rend_service_introduce(circuit_t *circuit, const char *request, int request_len);
1112 void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
1113 int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
1114 void rend_service_dump_stats(int severity);
1116 /********************************* rendmid.c *******************************/
1117 int rend_mid_establish_intro(circuit_t *circ, const char *request, int request_len);
1118 int rend_mid_introduce(circuit_t *circ, const char *request, int request_len);
1119 int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, int request_len);
1120 int rend_mid_rendezvous(circuit_t *circ, const char *request, int request_len);
1122 #endif
1125 Local Variables:
1126 mode:c
1127 indent-tabs-mode:nil
1128 c-basic-offset:2
1129 End: