Get entropy in windows.
[tor.git] / src / or / or.h
blob854e1528c3bd431697dd1620ca72602925e4a72f
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"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <limits.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #ifdef HAVE_STRING_H
17 #include <string.h>
18 #endif
19 #ifdef HAVE_SIGNAL_H
20 #include <signal.h>
21 #endif
22 #ifdef HAVE_NETDB_H
23 #include <netdb.h>
24 #endif
25 #ifdef HAVE_CTYPE_H
26 #include <ctype.h>
27 #endif
28 #include "../common/torint.h"
29 #include "../common/fakepoll.h"
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
32 #endif
33 #ifdef HAVE_SYS_WAIT_H
34 #include <sys/wait.h>
35 #endif
36 #ifdef HAVE_SYS_FCNTL_H
37 #include <sys/fcntl.h>
38 #endif
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #ifdef HAVE_SYS_TIME_H
49 #include <sys/time.h>
50 #endif
51 #ifdef HAVE_SYS_STAT_H
52 #include <sys/stat.h>
53 #endif
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
56 #endif
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60 #ifdef HAVE_ERRNO_H
61 #include <errno.h>
62 #endif
63 #ifdef HAVE_ASSERT_H
64 #include <assert.h>
65 #endif
66 #ifdef HAVE_TIME_H
67 #include <time.h>
68 #endif
69 #ifdef HAVE_PWD_H
70 #include <pwd.h>
71 #endif
72 #ifdef HAVE_GRP_H
73 #include <grp.h>
74 #endif
75 #ifdef HAVE_WINSOCK_H
76 #include <winsock.h>
77 #endif
78 #if _MSC_VER > 1300
79 #include <winsock2.h>
80 #include <ws2tcpip.h>
81 #elif defined(_MSC_VER)
82 #include <winsock.h>
83 #endif
85 #ifdef MS_WINDOWS
86 #include <io.h>
87 #include <process.h>
88 #include <direct.h>
89 #define WIN32_WINNT 0x400
90 #define _WIN32_WINNT 0x400
91 #define WIN32_LEAN_AND_MEAN
92 #include <windows.h>
93 #define snprintf _snprintf
94 #endif
96 #include "../common/crypto.h"
97 #include "../common/tortls.h"
98 #include "../common/log.h"
99 #include "../common/util.h"
101 #define MAXCONNECTIONS 1000 /* upper bound on max connections.
102 can be lowered by config file */
104 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
105 #define MAX_NICKNAME_LEN 32
106 #define MAX_DIR_SIZE 500000
108 #ifdef TOR_PERF
109 #define MAX_DNS_ENTRY_AGE (150*60)
110 #else
111 #define MAX_DNS_ENTRY_AGE (15*60)
112 #endif
114 #define CIRC_ID_TYPE_LOWER 0
115 #define CIRC_ID_TYPE_HIGHER 1
117 #define _CONN_TYPE_MIN 3
118 #define CONN_TYPE_OR_LISTENER 3
119 #define CONN_TYPE_OR 4
120 #define CONN_TYPE_EXIT 5
121 #define CONN_TYPE_AP_LISTENER 6
122 #define CONN_TYPE_AP 7
123 #define CONN_TYPE_DIR_LISTENER 8
124 #define CONN_TYPE_DIR 9
125 #define CONN_TYPE_DNSWORKER 10
126 #define CONN_TYPE_CPUWORKER 11
127 #define _CONN_TYPE_MAX 11
129 #define LISTENER_STATE_READY 0
131 #define _DNSWORKER_STATE_MIN 1
132 #define DNSWORKER_STATE_IDLE 1
133 #define DNSWORKER_STATE_BUSY 2
134 #define _DNSWORKER_STATE_MAX 2
136 #define _CPUWORKER_STATE_MIN 1
137 #define CPUWORKER_STATE_IDLE 1
138 #define CPUWORKER_STATE_BUSY_ONION 2
139 #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
140 #define _CPUWORKER_STATE_MAX 3
142 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
144 #define _OR_CONN_STATE_MIN 1
145 #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
146 #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
147 #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
148 #define _OR_CONN_STATE_MAX 3
150 #define _EXIT_CONN_STATE_MIN 1
151 #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
152 #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
153 #define EXIT_CONN_STATE_OPEN 3
154 #define EXIT_CONN_STATE_RESOLVEFAILED 4 /* waiting to be removed */
155 #define _EXIT_CONN_STATE_MAX 4
156 #if 0
157 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
158 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
159 #endif
161 /* the AP state values must be disjoint from the EXIT state values */
162 #define _AP_CONN_STATE_MIN 5
163 #define AP_CONN_STATE_SOCKS_WAIT 5
164 #define AP_CONN_STATE_CIRCUIT_WAIT 6
165 #define AP_CONN_STATE_CONNECT_WAIT 7
166 #define AP_CONN_STATE_OPEN 8
167 #define _AP_CONN_STATE_MAX 8
169 #define _DIR_CONN_STATE_MIN 1
170 #define DIR_CONN_STATE_CONNECTING_FETCH 1
171 #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
172 #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
173 #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
174 #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
175 #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
176 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
177 #define DIR_CONN_STATE_SERVER_WRITING 8
178 #define _DIR_CONN_STATE_MAX 8
180 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
181 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
182 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
183 #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
185 #define RELAY_COMMAND_BEGIN 1
186 #define RELAY_COMMAND_DATA 2
187 #define RELAY_COMMAND_END 3
188 #define RELAY_COMMAND_CONNECTED 4
189 #define RELAY_COMMAND_SENDME 5
190 #define RELAY_COMMAND_EXTEND 6
191 #define RELAY_COMMAND_EXTENDED 7
192 #define RELAY_COMMAND_TRUNCATE 8
193 #define RELAY_COMMAND_TRUNCATED 9
194 #define RELAY_COMMAND_DROP 10
195 #define RELAY_COMMAND_RESOLVE 11
196 #define RELAY_COMMAND_RESOLVED 12
198 #define _MIN_END_STREAM_REASON 1
199 #define END_STREAM_REASON_MISC 1
200 #define END_STREAM_REASON_RESOLVEFAILED 2
201 #define END_STREAM_REASON_CONNECTFAILED 3
202 #define END_STREAM_REASON_EXITPOLICY 4
203 #define END_STREAM_REASON_DESTROY 5
204 #define END_STREAM_REASON_DONE 6
205 #define END_STREAM_REASON_TIMEOUT 7
206 #define _MAX_END_STREAM_REASON 7
208 /* default cipher function */
209 #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
210 /* Used to en/decrypt onion skins */
211 #define ONION_CIPHER DEFAULT_CIPHER
212 /* Used to en/decrypt RELAY cells */
213 #define CIRCUIT_CIPHER DEFAULT_CIPHER
215 #define CELL_DIRECTION_IN 1
216 #define CELL_DIRECTION_OUT 2
217 #define EDGE_EXIT CONN_TYPE_EXIT
218 #define EDGE_AP CONN_TYPE_AP
219 #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
221 #ifdef TOR_PERF
222 #define CIRCWINDOW_START 10000
223 #define CIRCWINDOW_INCREMENT 1000
224 #define STREAMWINDOW_START 5000
225 #define STREAMWINDOW_INCREMENT 500
226 #else
227 #define CIRCWINDOW_START 1000
228 #define CIRCWINDOW_INCREMENT 100
229 #define STREAMWINDOW_START 500
230 #define STREAMWINDOW_INCREMENT 50
231 #endif
233 /* cell commands */
234 #define CELL_PADDING 0
235 #define CELL_CREATE 1
236 #define CELL_CREATED 2
237 #define CELL_RELAY 3
238 #define CELL_DESTROY 4
240 /* legal characters in a filename */
241 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
242 /* legal characters in a nickname */
243 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
245 #define SOCKS4_NETWORK_LEN 8
248 * Relay payload:
249 * Relay command [1 byte]
250 * Recognized [2 bytes]
251 * Stream ID [2 bytes]
252 * Partial SHA-1 [4 bytes]
253 * Length [2 bytes]
254 * Relay payload [498 bytes]
257 #if 0
258 #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
259 #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
261 #define CELL_RELAY_RECOGNIZED(c) (ntohs(*(uint16_t*)((c).payload+1)))
262 #define SET_CELL_RELAY_RECOGNIZED(c,r) (*(uint16_t*)((c).payload+1) = htons(r))
264 #define STREAM_ID_SIZE 2
265 //#define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
266 #define CELL_RELAY_STREAM_ID(c) (ntohs(*(uint16_t*)((c).payload+3)))
267 #define SET_CELL_RELAY_STREAM_ID(c,id) (*(uint16_t*)((c).payload+3) = htons(id))
268 #define ZERO_STREAM 0
270 /* integrity is the first 32 bits (in network order) of a sha-1 of all
271 * cell payloads that are relay cells that have been sent / delivered
272 * to the hop on the * circuit (the integrity is zeroed while doing
273 * each calculation)
275 #define CELL_RELAY_INTEGRITY(c) (ntohl(*(uint32_t*)((c).payload+5)))
276 #define SET_CELL_RELAY_INTEGRITY(c,i) (*(uint32_t*)((c).payload+5) = htonl(i))
278 /* relay length is how many bytes are used in the cell payload past relay_header_size */
279 #define CELL_RELAY_LENGTH(c) (ntohs(*(uint16_t*)((c).payload+9)))
280 #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+9) = htons(len))
281 #endif
283 #define CELL_PAYLOAD_SIZE 509
284 #define CELL_NETWORK_SIZE 512
286 #define RELAY_HEADER_SIZE (1+2+2+4+2)
287 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
289 /* cell definition */
290 typedef struct {
291 uint16_t circ_id;
292 unsigned char command;
293 unsigned char payload[CELL_PAYLOAD_SIZE];
294 } cell_t;
296 typedef struct {
297 uint8_t command;
298 uint16_t recognized;
299 uint16_t stream_id;
300 char integrity[4];
301 uint16_t length;
302 } relay_header_t;
304 typedef struct buf_t buf_t;
305 typedef struct socks_request_t socks_request_t;
307 #define CONNECTION_MAGIC 0x7C3C304Eu
308 struct connection_t {
309 uint32_t magic; /* for memory debugging */
311 uint8_t type;
312 uint8_t state;
313 uint8_t wants_to_read; /* should we start reading again once
314 * the bandwidth throttler allows it?
316 uint8_t wants_to_write; /* should we start writing again once
317 * the bandwidth throttler allows reads?
319 int s; /* our socket */
320 int poll_index; /* index of this conn into the poll_array */
321 int marked_for_close; /* should we close this conn on the next
322 * iteration of the main loop?
324 char *marked_for_close_file; /* for debugging: in which file were we marked
325 * for close? */
326 int hold_open_until_flushed;
328 buf_t *inbuf;
329 int inbuf_reached_eof; /* did read() return 0 on this conn? */
330 time_t timestamp_lastread; /* when was the last time poll() said we could read? */
332 buf_t *outbuf;
333 int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
334 time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
336 time_t timestamp_created; /* when was this connection_t created? */
338 uint32_t addr; /* these two uniquely identify a router. Both in host order. */
339 uint16_t port; /* if non-zero, they identify the guy on the other end
340 * of the connection. */
341 char *address; /* FQDN (or IP) of the guy on the other end.
342 * strdup into this, because free_connection frees it
344 crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
345 crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
346 crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
347 char *nickname;
349 /* Used only by OR connections: */
350 tor_tls *tls;
351 uint16_t next_circ_id; /* Which circ_id do we try to use next on this connection?
352 * This is always in the range 0..1<<15-1.*/
354 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
355 int bandwidth; /* connection bandwidth. */
356 int receiver_bucket; /* when this hits 0, stop receiving. Every second we
357 * add 'bandwidth' to this, capping it at 10*bandwidth.
360 /* Used only by edge connections: */
361 uint16_t stream_id;
362 struct connection_t *next_stream; /* points to the next stream at this edge, if any */
363 struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
364 int package_window; /* how many more relay cells can i send into the circuit? */
365 int deliver_window; /* how many more relay cells can end at me? */
367 int done_sending; /* for half-open connections; not used currently */
368 int done_receiving;
369 char has_sent_end; /* for debugging: set once we've set the stream end,
370 and check in circuit_about_to_close_connection() */
371 char num_retries; /* how many times have we re-tried beginning this stream? */
373 /* Used only by AP connections */
374 socks_request_t *socks_request;
377 typedef struct connection_t connection_t;
379 #define EXIT_POLICY_ACCEPT 1
380 #define EXIT_POLICY_REJECT 2
382 struct exit_policy_t {
383 char policy_type;
384 char *string;
385 uint32_t addr;
386 uint32_t msk;
387 uint16_t prt_min;
388 uint16_t prt_max;
390 struct exit_policy_t *next;
393 /* config stuff we know about the other ORs in the network */
394 typedef struct {
395 char *address;
396 char *nickname;
398 uint32_t addr; /* all host order */
399 uint16_t or_port;
400 uint16_t socks_port;
401 uint16_t dir_port;
403 time_t published_on;
405 crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
406 crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
407 crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
409 int is_running;
411 /* link info */
412 uint32_t bandwidthrate;
413 uint32_t bandwidthburst;
414 struct exit_policy_t *exit_policy;
415 } routerinfo_t;
417 #define MAX_ROUTERS_IN_DIR 1024
418 typedef struct {
419 routerinfo_t **routers;
420 int n_routers;
421 char *software_versions;
422 time_t published_on;
423 } routerlist_t;
425 struct crypt_path_t {
427 /* crypto environments */
428 crypto_cipher_env_t *f_crypto;
429 crypto_cipher_env_t *b_crypto;
431 crypto_digest_env_t *f_digest; /* for integrity checking */
432 crypto_digest_env_t *b_digest;
434 crypto_dh_env_t *handshake_state;
436 uint32_t addr;
437 uint16_t port;
439 uint8_t state;
440 #define CPATH_STATE_CLOSED 0
441 #define CPATH_STATE_AWAITING_KEYS 1
442 #define CPATH_STATE_OPEN 2
443 struct crypt_path_t *next;
444 struct crypt_path_t *prev; /* doubly linked list */
446 int package_window;
447 int deliver_window;
450 #define DH_KEY_LEN CRYPTO_DH_SIZE
451 #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN)
452 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
454 typedef struct crypt_path_t crypt_path_t;
456 typedef struct {
457 int desired_path_len;
458 char *chosen_exit; /* nickname of planned exit node */
459 } cpath_build_state_t;
461 /* struct for a path (circuit) through the network */
462 #define CIRCUIT_MAGIC 0x35315243u
463 struct circuit_t {
464 uint32_t magic; /* for memory debugging. */
466 int marked_for_close; /* Should we close this circuit at the end of the main
467 * loop? */
468 char *marked_for_close_file;
470 uint32_t n_addr;
471 uint16_t n_port;
472 connection_t *p_conn;
473 connection_t *n_conn; /* for the OR conn, if there is one */
474 connection_t *p_streams;
475 connection_t *n_streams;
476 uint16_t next_stream_id;
477 int package_window;
478 int deliver_window;
480 uint16_t p_circ_id; /* circuit identifiers */
481 uint16_t n_circ_id;
483 crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
484 crypto_cipher_env_t *n_crypto;
486 crypto_digest_env_t *p_digest; /* for integrity checking, */
487 crypto_digest_env_t *n_digest; /* intermediate hops only */
489 cpath_build_state_t *build_state;
490 crypt_path_t *cpath;
492 char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
493 time_t timestamp_created;
494 time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
496 uint8_t state;
498 struct circuit_t *next;
501 typedef struct circuit_t circuit_t;
503 typedef struct {
504 char *LogLevel;
505 char *LogFile;
506 char *DebugLogFile;
507 char *DataDirectory;
508 char *RouterFile;
509 char *Nickname;
510 char *Address;
511 char *PidFile;
512 char *ExitNodes;
513 char *EntryNodes;
514 char *ExcludeNodes;
515 char *ExitPolicy;
516 char *SocksBindAddress;
517 char *ORBindAddress;
518 char *DirBindAddress;
519 char *RecommendedVersions;
520 char *User;
521 char *Group;
522 double PathlenCoinWeight;
523 int ORPort;
524 int SocksPort;
525 int DirPort;
526 int MaxConn;
527 int TrafficShaping;
528 int LinkPadding;
529 int IgnoreVersion;
530 int RunAsDaemon;
531 int DirRebuildPeriod;
532 int DirFetchPostPeriod;
533 int KeepalivePeriod;
534 int MaxOnionsPending;
535 int NewCircuitPeriod;
536 int BandwidthRate;
537 int BandwidthBurst;
538 int NumCpus;
539 int loglevel;
540 } or_options_t;
542 /* XXX are these good enough defaults? */
543 #define MAX_SOCKS_REPLY_LEN 1024
544 #define MAX_SOCKS_ADDR_LEN 256
545 struct socks_request_t {
546 char socks_version;
547 int replylen;
548 char reply[MAX_SOCKS_REPLY_LEN];
549 char address[MAX_SOCKS_ADDR_LEN];
550 uint16_t port;
553 /* all the function prototypes go here */
555 /********************************* buffers.c ***************************/
557 int find_on_inbuf(char *string, int string_len, buf_t *buf);
559 buf_t *buf_new();
560 buf_t *buf_new_with_capacity(size_t size);
561 void buf_free(buf_t *buf);
562 void buf_clear(buf_t *buf);
564 size_t buf_datalen(const buf_t *buf);
565 size_t buf_capacity(const buf_t *buf);
566 const char *_buf_peek_raw_buffer(const buf_t *buf);
568 int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
569 int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
571 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
572 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
574 int write_to_buf(const char *string, int string_len, buf_t *buf);
575 int fetch_from_buf(char *string, int string_len, buf_t *buf);
576 int fetch_from_buf_http(buf_t *buf,
577 char **headers_out, int max_headerlen,
578 char **body_out, int max_bodylen);
579 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
581 void assert_buf_ok(buf_t *buf);
583 /********************************* circuit.c ***************************/
585 void circuit_add(circuit_t *circ);
586 void circuit_remove(circuit_t *circ);
587 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
588 void circuit_close_all_marked(void);
589 void circuit_free(circuit_t *circ);
590 void circuit_free_cpath(crypt_path_t *cpath);
591 int _circuit_mark_for_close(circuit_t *circ);
593 #define circuit_mark_for_close(c) \
594 do { \
595 if (_circuit_mark_for_close(c)<0) { \
596 log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
597 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
598 } else { \
599 c->marked_for_close_file = __FILE__; \
600 c->marked_for_close = __LINE__; \
602 } while (0)
605 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
606 circuit_t *circuit_get_by_conn(connection_t *conn);
607 circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
609 void circuit_expire_building(void);
610 int circuit_count_building(void);
611 int circuit_stream_is_being_handled(connection_t *conn);
613 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
614 int cell_direction);
615 int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
616 int cell_direction, crypt_path_t *layer_hint);
618 void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
619 int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
620 void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
622 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
623 void circuit_about_to_close_connection(connection_t *conn);
625 void circuit_log_path(int severity, circuit_t *circ);
626 void circuit_dump_by_conn(connection_t *conn, int severity);
628 void circuit_expire_unused_circuits(void);
629 int circuit_launch_new(void);
630 void circuit_increment_failure_count(void);
631 void circuit_reset_failure_count(void);
632 int circuit_establish_circuit(void);
633 void circuit_n_conn_open(connection_t *or_conn);
634 int circuit_send_next_onion_skin(circuit_t *circ);
635 int circuit_extend(cell_t *cell, circuit_t *circ);
636 int circuit_finish_handshake(circuit_t *circ, char *reply);
637 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
639 void assert_cpath_ok(const crypt_path_t *c);
640 void assert_cpath_layer_ok(const crypt_path_t *c);
641 void assert_circuit_ok(const circuit_t *c);
643 extern unsigned long stats_n_relay_cells_relayed;
644 extern unsigned long stats_n_relay_cells_delivered;
646 /********************************* command.c ***************************/
648 void command_process_cell(cell_t *cell, connection_t *conn);
650 extern unsigned long stats_n_padding_cells_processed;
651 extern unsigned long stats_n_create_cells_processed;
652 extern unsigned long stats_n_created_cells_processed;
653 extern unsigned long stats_n_relay_cells_processed;
654 extern unsigned long stats_n_destroy_cells_processed;
656 /********************************* config.c ***************************/
658 int config_assign_default_dirservers(void);
659 int getconfig(int argc, char **argv, or_options_t *options);
661 /********************************* connection.c ***************************/
663 connection_t *connection_new(int type);
664 void connection_free(connection_t *conn);
665 void connection_free_all(void);
666 void connection_close_immediate(connection_t *conn);
667 int _connection_mark_for_close(connection_t *conn, char reason);
669 #define connection_mark_for_close(c,r) \
670 do { \
671 if (_connection_mark_for_close(c,r)<0) { \
672 log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
673 __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
674 } else { \
675 c->marked_for_close_file = __FILE__; \
676 c->marked_for_close = __LINE__; \
678 } while (0)
680 void connection_expire_held_open(void);
682 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
684 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
685 int retry_all_connections(void);
687 int connection_handle_read(connection_t *conn);
688 int connection_read_to_buf(connection_t *conn);
690 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
691 int connection_find_on_inbuf(char *string, int len, connection_t *conn);
693 int connection_wants_to_flush(connection_t *conn);
694 int connection_outbuf_too_full(connection_t *conn);
695 int connection_handle_write(connection_t *conn);
696 void connection_write_to_buf(const char *string, int len, connection_t *conn);
698 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
699 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
701 connection_t *connection_get_by_type(int type);
702 connection_t *connection_get_by_type_state(int type, int state);
703 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
705 int connection_receiver_bucket_should_increase(connection_t *conn);
707 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
708 #define connection_has_pending_tls_data(conn) \
709 ((conn)->type == CONN_TYPE_OR && \
710 (conn)->state == OR_CONN_STATE_OPEN && \
711 tor_tls_get_pending_bytes(conn->tls))
712 int connection_is_listener(connection_t *conn);
713 int connection_state_is_open(connection_t *conn);
715 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
717 int connection_process_inbuf(connection_t *conn);
718 int connection_finished_flushing(connection_t *conn);
720 void assert_connection_ok(connection_t *conn, time_t now);
722 /********************************* connection_edge.c ***************************/
724 void relay_header_pack(char *dest, const relay_header_t *src);
725 void relay_header_unpack(relay_header_t *dest, const char *src);
726 int connection_edge_process_inbuf(connection_t *conn);
727 int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
728 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
730 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
731 void *payload, int payload_len, crypt_path_t *cpath_layer);
733 int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
734 int edge_type, crypt_path_t *layer_hint);
735 int connection_edge_finished_flushing(connection_t *conn);
737 int connection_edge_package_raw_inbuf(connection_t *conn);
739 void connection_exit_connect(connection_t *conn);
740 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
741 void connection_ap_expire_beginning(void);
742 void connection_ap_attach_pending(void);
744 extern uint64_t stats_n_data_cells_packaged;
745 extern uint64_t stats_n_data_bytes_packaged;
746 extern uint64_t stats_n_data_cells_received;
747 extern uint64_t stats_n_data_bytes_received;
749 void client_dns_init(void);
750 void client_dns_clean(void);
752 /********************************* connection_or.c ***************************/
754 int connection_or_process_inbuf(connection_t *conn);
755 int connection_or_finished_flushing(connection_t *conn);
757 void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
758 connection_t *connection_or_connect(routerinfo_t *router);
760 int connection_tls_start_handshake(connection_t *conn, int receiving);
761 int connection_tls_continue_handshake(connection_t *conn);
763 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
765 /********************************* cpuworker.c *****************************/
767 void cpu_init(void);
768 int connection_cpu_finished_flushing(connection_t *conn);
769 int connection_cpu_process_inbuf(connection_t *conn);
770 int cpuworker_main(void *data);
771 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
772 void *task);
774 /********************************* directory.c ***************************/
776 void directory_initiate_command(routerinfo_t *router, int command);
777 int connection_dir_process_inbuf(connection_t *conn);
778 int connection_dir_finished_flushing(connection_t *conn);
780 /********************************* dns.c ***************************/
782 void dns_init(void);
783 int connection_dns_finished_flushing(connection_t *conn);
784 int connection_dns_process_inbuf(connection_t *conn);
785 void connection_dns_remove(connection_t *conn);
786 void dns_cancel_pending_resolve(char *question);
787 int dns_resolve(connection_t *exitconn);
789 /********************************* main.c ***************************/
791 int connection_add(connection_t *conn);
792 int connection_remove(connection_t *conn);
793 void connection_set_poll_socket(connection_t *conn);
795 void get_connection_array(connection_t ***array, int *n);
797 void connection_watch_events(connection_t *conn, short events);
798 int connection_is_reading(connection_t *conn);
799 void connection_stop_reading(connection_t *conn);
800 void connection_start_reading(connection_t *conn);
802 int connection_is_writing(connection_t *conn);
803 void connection_stop_writing(connection_t *conn);
804 void connection_start_writing(connection_t *conn);
806 int main(int argc, char *argv[]);
808 /********************************* onion.c ***************************/
810 int decide_circ_id_type(char *local_nick, char *remote_nick);
812 int onion_pending_add(circuit_t *circ);
813 circuit_t *onion_next_task(void);
814 void onion_pending_remove(circuit_t *circ);
816 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
818 int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
819 routerinfo_t **router_out);
821 int onion_skin_create(crypto_pk_env_t *router_key,
822 crypto_dh_env_t **handshake_state_out,
823 char *onion_skin_out);
825 int onion_skin_server_handshake(char *onion_skin,
826 crypto_pk_env_t *private_key,
827 char *handshake_reply_out,
828 char *key_out,
829 int key_out_len);
831 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
832 char *handshake_reply,
833 char *key_out,
834 int key_out_len);
836 cpath_build_state_t *onion_new_cpath_build_state(void);
838 /********************************* router.c ***************************/
840 void set_onion_key(crypto_pk_env_t *k);
841 crypto_pk_env_t *get_onion_key(void);
842 void set_identity_key(crypto_pk_env_t *k);
843 crypto_pk_env_t *get_identity_key(void);
844 crypto_pk_env_t *get_link_key(void);
845 int init_keys(void);
847 void router_retry_connections(void);
848 void router_upload_desc_to_dirservers(void);
849 int router_compare_to_my_exit_policy(connection_t *conn);
850 const char *router_get_my_descriptor(void);
851 int router_rebuild_descriptor(void);
852 int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
853 crypto_pk_env_t *ident_key);
855 /********************************* routerlist.c ***************************/
857 routerinfo_t *router_pick_directory_server(void);
858 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
859 routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
860 routerinfo_t *router_get_by_nickname(char *nickname);
861 void router_get_routerlist(routerlist_t **prouterlist);
862 void routerinfo_free(routerinfo_t *router);
863 void router_mark_as_down(char *nickname);
864 int router_set_routerlist_from_file(char *routerfile);
865 int router_set_routerlist_from_string(const char *s);
866 int router_get_dir_hash(const char *s, char *digest);
867 int router_get_router_hash(const char *s, char *digest);
868 int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
869 routerinfo_t *router_get_entry_from_string(const char *s, const char *end);
870 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
871 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
872 struct exit_policy_t *policy);
873 #define ADDR_POLICY_ACCEPTED 0
874 #define ADDR_POLICY_REJECTED -1
875 #define ADDR_POLICY_UNKNOWN 1
876 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
877 int router_exit_policy_rejects_all(routerinfo_t *router);
879 /********************************* dirserv.c ***************************/
880 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
881 int dirserv_parse_fingerprint_file(const char *fname);
882 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
883 void dirserv_free_fingerprint_list();
884 int dirserv_add_descriptor(const char **desc);
885 int dirserv_init_from_directory_string(const char *dir);
886 void dirserv_free_descriptors();
887 int dirserv_dump_directory_to_string(char *s, int maxlen,
888 crypto_pk_env_t *private_key);
889 void directory_set_dirty();
890 size_t dirserv_get_directory(const char **cp);
893 #endif
896 Local Variables:
897 mode:c
898 indent-tabs-mode:nil
899 c-basic-offset:2
900 End: