On OSX, decline to use the built-in select-baed poll wrapper, since it seems to somet...
[tor.git] / src / or / or.h
blob980637305ceb6d813a7918927a8a8dc7d3381d23
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 #define WIN32_LEAN_AND_MEAN
89 #include <windows.h>
90 #define snprintf _snprintf
91 #endif
93 #include "../common/crypto.h"
94 #include "../common/tortls.h"
95 #include "../common/log.h"
96 #include "../common/util.h"
98 #define MAXCONNECTIONS 1000 /* upper bound on max connections.
99 can be lowered by config file */
101 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
102 #define MAX_NICKNAME_LEN 32
103 #define MAX_DIR_SIZE 500000
105 #ifdef TOR_PERF
106 #define MAX_DNS_ENTRY_AGE (150*60)
107 #else
108 #define MAX_DNS_ENTRY_AGE (15*60)
109 #endif
111 #define CIRC_ID_TYPE_LOWER 0
112 #define CIRC_ID_TYPE_HIGHER 1
114 #define _CONN_TYPE_MIN 3
115 #define CONN_TYPE_OR_LISTENER 3
116 #define CONN_TYPE_OR 4
117 #define CONN_TYPE_EXIT 5
118 #define CONN_TYPE_AP_LISTENER 6
119 #define CONN_TYPE_AP 7
120 #define CONN_TYPE_DIR_LISTENER 8
121 #define CONN_TYPE_DIR 9
122 #define CONN_TYPE_DNSWORKER 10
123 #define CONN_TYPE_CPUWORKER 11
124 #define _CONN_TYPE_MAX 11
126 #define LISTENER_STATE_READY 0
128 #define _DNSWORKER_STATE_MIN 1
129 #define DNSWORKER_STATE_IDLE 1
130 #define DNSWORKER_STATE_BUSY 2
131 #define _DNSWORKER_STATE_MAX 2
133 #define _CPUWORKER_STATE_MIN 1
134 #define CPUWORKER_STATE_IDLE 1
135 #define CPUWORKER_STATE_BUSY_ONION 2
136 #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
137 #define _CPUWORKER_STATE_MAX 3
139 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
141 #define _OR_CONN_STATE_MIN 1
142 #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
143 #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
144 #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
145 #define _OR_CONN_STATE_MAX 3
147 #define _EXIT_CONN_STATE_MIN 1
148 #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
149 #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
150 #define EXIT_CONN_STATE_OPEN 3
151 #define _EXIT_CONN_STATE_MAX 3
152 #if 0
153 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
154 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
155 #endif
157 /* the AP state values must be disjoint from the EXIT state values */
158 #define _AP_CONN_STATE_MIN 4
159 #define AP_CONN_STATE_SOCKS_WAIT 4
160 #define AP_CONN_STATE_CIRCUIT_WAIT 5
161 #define AP_CONN_STATE_CONNECTING 6
162 #define AP_CONN_STATE_OPEN 7
163 #define _AP_CONN_STATE_MAX 7
165 #define _DIR_CONN_STATE_MIN 1
166 #define DIR_CONN_STATE_CONNECTING_FETCH 1
167 #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
168 #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
169 #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
170 #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
171 #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
172 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
173 #define DIR_CONN_STATE_SERVER_WRITING 8
174 #define _DIR_CONN_STATE_MAX 8
176 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
177 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
178 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
179 #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
181 #define RELAY_COMMAND_BEGIN 1
182 #define RELAY_COMMAND_DATA 2
183 #define RELAY_COMMAND_END 3
184 #define RELAY_COMMAND_CONNECTED 4
185 #define RELAY_COMMAND_SENDME 5
186 #define RELAY_COMMAND_EXTEND 6
187 #define RELAY_COMMAND_EXTENDED 7
188 #define RELAY_COMMAND_TRUNCATE 8
189 #define RELAY_COMMAND_TRUNCATED 9
190 #define RELAY_COMMAND_DROP 10
192 #define END_STREAM_REASON_MISC 1
193 #define END_STREAM_REASON_RESOLVEFAILED 2
194 #define END_STREAM_REASON_CONNECTFAILED 3
195 #define END_STREAM_REASON_EXITPOLICY 4
196 #define END_STREAM_REASON_DESTROY 5
197 #define END_STREAM_REASON_DONE 6
199 /* default cipher function */
200 #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
201 /* Used to en/decrypt onion skins */
202 #define ONION_CIPHER DEFAULT_CIPHER
203 /* Used to en/decrypt RELAY cells */
204 #define CIRCUIT_CIPHER DEFAULT_CIPHER
206 #define CELL_DIRECTION_IN 1
207 #define CELL_DIRECTION_OUT 2
208 #define EDGE_EXIT CONN_TYPE_EXIT
209 #define EDGE_AP CONN_TYPE_AP
210 #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
212 #ifdef TOR_PERF
213 #define CIRCWINDOW_START 10000
214 #define CIRCWINDOW_INCREMENT 1000
215 #define STREAMWINDOW_START 5000
216 #define STREAMWINDOW_INCREMENT 500
217 #else
218 #define CIRCWINDOW_START 1000
219 #define CIRCWINDOW_INCREMENT 100
220 #define STREAMWINDOW_START 500
221 #define STREAMWINDOW_INCREMENT 50
222 #endif
224 /* cell commands */
225 #define CELL_PADDING 0
226 #define CELL_CREATE 1
227 #define CELL_CREATED 2
228 #define CELL_RELAY 3
229 #define CELL_DESTROY 4
231 /* legal characters in a filename */
232 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
233 /* legal characters in a nickname */
234 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
236 #define SOCKS4_NETWORK_LEN 8
239 * Relay payload:
240 * Relay command [1 byte]
241 * Recognized [2 bytes]
242 * Stream ID [2 bytes]
243 * Partial SHA-1 [4 bytes]
244 * Length [2 bytes]
245 * Relay payload [498 bytes]
248 #if 0
249 #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
250 #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
252 #define CELL_RELAY_RECOGNIZED(c) (ntohs(*(uint16_t*)((c).payload+1)))
253 #define SET_CELL_RELAY_RECOGNIZED(c,r) (*(uint16_t*)((c).payload+1) = htons(r))
255 #define STREAM_ID_SIZE 2
256 //#define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
257 #define CELL_RELAY_STREAM_ID(c) (ntohs(*(uint16_t*)((c).payload+3)))
258 #define SET_CELL_RELAY_STREAM_ID(c,id) (*(uint16_t*)((c).payload+3) = htons(id))
259 #define ZERO_STREAM 0
261 /* integrity is the first 32 bits (in network order) of a sha-1 of all
262 * cell payloads that are relay cells that have been sent / delivered
263 * to the hop on the * circuit (the integrity is zeroed while doing
264 * each calculation)
266 #define CELL_RELAY_INTEGRITY(c) (ntohl(*(uint32_t*)((c).payload+5)))
267 #define SET_CELL_RELAY_INTEGRITY(c,i) (*(uint32_t*)((c).payload+5) = htonl(i))
269 /* relay length is how many bytes are used in the cell payload past relay_header_size */
270 #define CELL_RELAY_LENGTH(c) (ntohs(*(uint16_t*)((c).payload+9)))
271 #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+9) = htons(len))
272 #endif
274 #define CELL_PAYLOAD_SIZE 509
275 #define CELL_NETWORK_SIZE 512
277 #define RELAY_HEADER_SIZE (1+2+2+4+2)
278 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
280 /* cell definition */
281 typedef struct {
282 uint16_t circ_id;
283 unsigned char command;
284 unsigned char payload[CELL_PAYLOAD_SIZE];
285 } cell_t;
287 typedef struct {
288 uint8_t command;
289 uint16_t recognized;
290 uint16_t stream_id;
291 char integrity[4];
292 uint16_t length;
293 } relay_header_t;
295 typedef struct buf_t buf_t;
296 typedef struct socks_request_t socks_request_t;
298 struct connection_t {
300 uint8_t type;
301 uint8_t state;
302 uint8_t wants_to_read; /* should we start reading again once
303 * the bandwidth throttler allows it?
305 uint8_t wants_to_write; /* should we start writing again once
306 * the bandwidth throttler allows reads?
308 int s; /* our socket */
309 int poll_index; /* index of this conn into the poll_array */
310 int marked_for_close; /* should we close this conn on the next
311 * iteration of the main loop?
314 buf_t *inbuf;
315 int inbuf_reached_eof; /* did read() return 0 on this conn? */
316 time_t timestamp_lastread; /* when was the last time poll() said we could read? */
318 buf_t *outbuf;
319 int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
320 time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
322 time_t timestamp_created; /* when was this connection_t created? */
324 uint32_t addr; /* these two uniquely identify a router. Both in host order. */
325 uint16_t port; /* if non-zero, they identify the guy on the other end
326 * of the connection. */
327 char *address; /* FQDN (or IP) of the guy on the other end.
328 * strdup into this, because free_connection frees it
330 crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
331 crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
332 crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
333 char *nickname;
335 /* Used only by OR connections: */
336 tor_tls *tls;
337 uint16_t next_circ_id; /* Which circ_id do we try to use next on this connection?
338 * This is always in the range 0..1<<15-1.*/
340 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
341 uint32_t bandwidth; /* connection bandwidth. */
342 int receiver_bucket; /* when this hits 0, stop receiving. Every second we
343 * add 'bandwidth' to this, capping it at 10*bandwidth.
346 /* Used only by edge connections: */
347 uint16_t stream_id;
348 struct connection_t *next_stream; /* points to the next stream at this edge, if any */
349 struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
350 int package_window; /* how many more relay cells can i send into the circuit? */
351 int deliver_window; /* how many more relay cells can end at me? */
353 int done_sending; /* for half-open connections; not used currently */
354 int done_receiving;
355 char has_sent_end; /* for debugging: set once we've set the stream end,
356 and check in circuit_about_to_close_connection() */
358 /* Used only by AP connections */
359 socks_request_t *socks_request;
362 typedef struct connection_t connection_t;
364 #define EXIT_POLICY_ACCEPT 1
365 #define EXIT_POLICY_REJECT 2
367 struct exit_policy_t {
368 char policy_type;
369 char *string;
370 uint32_t addr;
371 uint32_t msk;
372 uint16_t prt_min;
373 uint16_t prt_max;
375 struct exit_policy_t *next;
378 /* config stuff we know about the other ORs in the network */
379 typedef struct {
380 char *address;
381 char *nickname;
383 uint32_t addr; /* all host order */
384 uint16_t or_port;
385 uint16_t socks_port;
386 uint16_t dir_port;
388 time_t published_on;
390 crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
391 crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
392 crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
394 int is_running;
396 /* link info */
397 uint32_t bandwidthrate;
398 uint32_t bandwidthburst;
399 struct exit_policy_t *exit_policy;
400 } routerinfo_t;
402 #define MAX_ROUTERS_IN_DIR 1024
403 typedef struct {
404 routerinfo_t **routers;
405 int n_routers;
406 char *software_versions;
407 time_t published_on;
408 } routerlist_t;
410 struct crypt_path_t {
412 /* crypto environments */
413 crypto_cipher_env_t *f_crypto;
414 crypto_cipher_env_t *b_crypto;
416 crypto_digest_env_t *f_digest; /* for integrity checking */
417 crypto_digest_env_t *b_digest;
419 crypto_dh_env_t *handshake_state;
421 uint32_t addr;
422 uint16_t port;
424 uint8_t state;
425 #define CPATH_STATE_CLOSED 0
426 #define CPATH_STATE_AWAITING_KEYS 1
427 #define CPATH_STATE_OPEN 2
428 struct crypt_path_t *next;
429 struct crypt_path_t *prev; /* doubly linked list */
431 int package_window;
432 int deliver_window;
435 #define DH_KEY_LEN CRYPTO_DH_SIZE
436 #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN)
437 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
439 typedef struct crypt_path_t crypt_path_t;
441 typedef struct {
442 int desired_path_len;
443 char *chosen_exit; /* nickname of planned exit node */
444 } cpath_build_state_t;
446 /* struct for a path (circuit) through the network */
447 struct circuit_t {
448 uint32_t n_addr;
449 uint16_t n_port;
450 connection_t *p_conn;
451 connection_t *n_conn; /* for the OR conn, if there is one */
452 connection_t *p_streams;
453 connection_t *n_streams;
454 uint16_t next_stream_id;
455 int package_window;
456 int deliver_window;
458 uint16_t p_circ_id; /* circuit identifiers */
459 uint16_t n_circ_id;
461 crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
462 crypto_cipher_env_t *n_crypto;
464 crypto_digest_env_t *p_digest; /* for integrity checking, */
465 crypto_digest_env_t *n_digest; /* intermediate hops only */
467 cpath_build_state_t *build_state;
468 crypt_path_t *cpath;
470 char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
471 time_t timestamp_created;
472 time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
474 uint8_t state;
476 void *next;
479 typedef struct circuit_t circuit_t;
481 typedef struct {
482 char *LogLevel;
483 char *LogFile;
484 char *DebugLogFile;
485 char *DataDirectory;
486 char *RouterFile;
487 char *Nickname;
488 char *Address;
489 char *PidFile;
490 char *ExitNodes;
491 char *EntryNodes;
492 char *ExcludedNodes;
493 char *ExitPolicy;
494 char *SocksBindAddress;
495 char *ORBindAddress;
496 char *DirBindAddress;
497 char *RecommendedVersions;
498 char *User;
499 char *Group;
500 double PathlenCoinWeight;
501 int ORPort;
502 int SocksPort;
503 int DirPort;
504 int MaxConn;
505 int TrafficShaping;
506 int LinkPadding;
507 int IgnoreVersion;
508 int RunAsDaemon;
509 int DirRebuildPeriod;
510 int DirFetchPostPeriod;
511 int KeepalivePeriod;
512 int MaxOnionsPending;
513 int NewCircuitPeriod;
514 int BandwidthRate;
515 int BandwidthBurst;
516 int NumCpus;
517 int loglevel;
518 } or_options_t;
520 /* XXX are these good enough defaults? */
521 #define MAX_SOCKS_REPLY_LEN 256
522 /* Not 256; addresses must fit in a begin cell. */
523 #define MAX_SOCKS_ADDR_LEN 200
524 struct socks_request_t {
525 char socks_version;
526 int replylen;
527 char reply[MAX_SOCKS_REPLY_LEN];
528 char address[MAX_SOCKS_ADDR_LEN];
529 uint16_t port;
532 /* all the function prototypes go here */
534 /********************************* buffers.c ***************************/
536 int find_on_inbuf(char *string, int string_len, buf_t *buf);
538 buf_t *buf_new();
539 buf_t *buf_new_with_capacity(size_t size);
540 void buf_free(buf_t *buf);
542 size_t buf_datalen(const buf_t *buf);
543 size_t buf_capacity(const buf_t *buf);
544 const char *_buf_peek_raw_buffer(const buf_t *buf);
546 int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
547 int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
549 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
550 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
552 int write_to_buf(const char *string, int string_len, buf_t *buf);
553 int fetch_from_buf(char *string, int string_len, buf_t *buf);
554 int fetch_from_buf_http(buf_t *buf,
555 char **headers_out, int max_headerlen,
556 char **body_out, int max_bodylen);
557 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
559 /********************************* circuit.c ***************************/
561 void circuit_add(circuit_t *circ);
562 void circuit_remove(circuit_t *circ);
563 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
564 void circuit_free(circuit_t *circ);
565 void circuit_free_cpath(crypt_path_t *cpath);
567 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
568 circuit_t *circuit_get_by_conn(connection_t *conn);
569 circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
571 void circuit_expire_building(void);
572 int circuit_count_building(void);
573 int circuit_stream_is_being_handled(connection_t *conn);
575 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
576 int cell_direction);
577 int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
578 int cell_direction, crypt_path_t *layer_hint);
580 void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
581 int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
582 void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
584 void circuit_close(circuit_t *circ);
585 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
586 void circuit_about_to_close_connection(connection_t *conn);
588 void circuit_log_path(int severity, circuit_t *circ);
589 void circuit_dump_by_conn(connection_t *conn, int severity);
591 void circuit_expire_unused_circuits(void);
592 int circuit_launch_new(void);
593 void circuit_increment_failure_count(void);
594 void circuit_reset_failure_count(void);
595 int circuit_establish_circuit(void);
596 void circuit_n_conn_open(connection_t *or_conn);
597 int circuit_send_next_onion_skin(circuit_t *circ);
598 int circuit_extend(cell_t *cell, circuit_t *circ);
599 int circuit_finish_handshake(circuit_t *circ, char *reply);
600 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
602 void assert_cpath_ok(const crypt_path_t *c);
603 void assert_cpath_layer_ok(const crypt_path_t *c);
604 void assert_circuit_ok(const circuit_t *c);
606 extern unsigned long stats_n_relay_cells_relayed;
607 extern unsigned long stats_n_relay_cells_delivered;
609 /********************************* command.c ***************************/
611 void command_process_cell(cell_t *cell, connection_t *conn);
613 extern unsigned long stats_n_padding_cells_processed;
614 extern unsigned long stats_n_create_cells_processed;
615 extern unsigned long stats_n_created_cells_processed;
616 extern unsigned long stats_n_relay_cells_processed;
617 extern unsigned long stats_n_destroy_cells_processed;
619 /********************************* config.c ***************************/
621 int getconfig(int argc, char **argv, or_options_t *options);
623 /********************************* connection.c ***************************/
625 connection_t *connection_new(int type);
626 void connection_free(connection_t *conn);
627 void connection_free_all(void);
629 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
631 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
632 int retry_all_connections(void);
634 int connection_handle_read(connection_t *conn);
635 int connection_read_to_buf(connection_t *conn);
637 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
638 int connection_find_on_inbuf(char *string, int len, connection_t *conn);
640 int connection_wants_to_flush(connection_t *conn);
641 int connection_outbuf_too_full(connection_t *conn);
642 int connection_handle_write(connection_t *conn);
643 void connection_write_to_buf(const char *string, int len, connection_t *conn);
645 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
646 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
648 connection_t *connection_get_by_type(int type);
649 connection_t *connection_get_by_type_state(int type, int state);
650 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
652 int connection_receiver_bucket_should_increase(connection_t *conn);
654 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
655 #define connection_has_pending_tls_data(conn) \
656 ((conn)->type == CONN_TYPE_OR && \
657 (conn)->state == OR_CONN_STATE_OPEN && \
658 tor_tls_get_pending_bytes(conn->tls))
659 int connection_is_listener(connection_t *conn);
660 int connection_state_is_open(connection_t *conn);
662 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
664 int connection_process_inbuf(connection_t *conn);
665 int connection_finished_flushing(connection_t *conn);
667 void assert_connection_ok(connection_t *conn, time_t now);
669 /********************************* connection_edge.c ***************************/
671 void relay_header_pack(char *dest, const relay_header_t *src);
672 void relay_header_unpack(relay_header_t *dest, const char *src);
673 int connection_edge_process_inbuf(connection_t *conn);
674 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
676 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
677 void *payload, int payload_len, crypt_path_t *cpath_layer);
679 int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
680 int edge_type, crypt_path_t *layer_hint);
681 int connection_edge_finished_flushing(connection_t *conn);
683 int connection_edge_package_raw_inbuf(connection_t *conn);
685 void connection_exit_connect(connection_t *conn);
686 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
687 void connection_ap_expire_beginning(void);
688 void connection_ap_attach_pending(void);
690 extern uint64_t stats_n_data_cells_packaged;
691 extern uint64_t stats_n_data_bytes_packaged;
692 extern uint64_t stats_n_data_cells_received;
693 extern uint64_t stats_n_data_bytes_received;
695 void client_dns_init(void);
696 void client_dns_clean(void);
698 /********************************* connection_or.c ***************************/
700 int connection_or_process_inbuf(connection_t *conn);
701 int connection_or_finished_flushing(connection_t *conn);
703 void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
704 connection_t *connection_or_connect(routerinfo_t *router);
706 int connection_tls_start_handshake(connection_t *conn, int receiving);
707 int connection_tls_continue_handshake(connection_t *conn);
709 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
711 /********************************* cpuworker.c *****************************/
713 void cpu_init(void);
714 int connection_cpu_finished_flushing(connection_t *conn);
715 int connection_cpu_process_inbuf(connection_t *conn);
716 int cpuworker_main(void *data);
717 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
718 void *task);
720 /********************************* directory.c ***************************/
722 void directory_initiate_command(routerinfo_t *router, int command);
723 int connection_dir_process_inbuf(connection_t *conn);
724 int connection_dir_finished_flushing(connection_t *conn);
726 /********************************* dns.c ***************************/
728 void dns_init(void);
729 int connection_dns_finished_flushing(connection_t *conn);
730 int connection_dns_process_inbuf(connection_t *conn);
731 void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
732 int dns_resolve(connection_t *exitconn);
734 /********************************* main.c ***************************/
736 int connection_add(connection_t *conn);
737 int connection_remove(connection_t *conn);
738 void connection_set_poll_socket(connection_t *conn);
740 void get_connection_array(connection_t ***array, int *n);
742 void connection_watch_events(connection_t *conn, short events);
743 int connection_is_reading(connection_t *conn);
744 void connection_stop_reading(connection_t *conn);
745 void connection_start_reading(connection_t *conn);
746 void connection_stop_writing(connection_t *conn);
747 void connection_start_writing(connection_t *conn);
749 int main(int argc, char *argv[]);
751 /********************************* onion.c ***************************/
753 int decide_circ_id_type(char *local_nick, char *remote_nick);
755 int onion_pending_add(circuit_t *circ);
756 circuit_t *onion_next_task(void);
757 void onion_pending_remove(circuit_t *circ);
759 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
761 int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
762 routerinfo_t **router_out);
764 int onion_skin_create(crypto_pk_env_t *router_key,
765 crypto_dh_env_t **handshake_state_out,
766 char *onion_skin_out);
768 int onion_skin_server_handshake(char *onion_skin,
769 crypto_pk_env_t *private_key,
770 char *handshake_reply_out,
771 char *key_out,
772 int key_out_len);
774 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
775 char *handshake_reply,
776 char *key_out,
777 int key_out_len);
779 cpath_build_state_t *onion_new_cpath_build_state(void);
781 /********************************* router.c ***************************/
783 void set_onion_key(crypto_pk_env_t *k);
784 crypto_pk_env_t *get_onion_key(void);
785 void set_identity_key(crypto_pk_env_t *k);
786 crypto_pk_env_t *get_identity_key(void);
787 crypto_pk_env_t *get_link_key(void);
788 int init_keys(void);
790 void router_retry_connections(void);
791 void router_upload_desc_to_dirservers(void);
792 int router_compare_to_my_exit_policy(connection_t *conn);
793 const char *router_get_my_descriptor(void);
794 int router_rebuild_descriptor(void);
795 int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
796 crypto_pk_env_t *ident_key);
798 /********************************* routerlist.c ***************************/
800 routerinfo_t *router_pick_directory_server(void);
801 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
802 routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
803 routerinfo_t *router_get_by_nickname(char *nickname);
804 void router_get_routerlist(routerlist_t **prouterlist);
805 void routerinfo_free(routerinfo_t *router);
806 void router_mark_as_down(char *nickname);
807 int router_set_routerlist_from_file(char *routerfile);
808 int router_get_dir_hash(const char *s, char *digest);
809 int router_get_router_hash(const char *s, char *digest);
810 int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
811 routerinfo_t *router_get_entry_from_string(const char **s);
812 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
813 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
814 struct exit_policy_t *policy);
815 #define ADDR_POLICY_ACCEPTED 0
816 #define ADDR_POLICY_REJECTED -1
817 #define ADDR_POLICY_UNKNOWN 1
818 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
819 int router_exit_policy_rejects_all(routerinfo_t *router);
821 /********************************* dirserv.c ***************************/
822 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
823 int dirserv_parse_fingerprint_file(const char *fname);
824 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
825 void dirserv_free_fingerprint_list();
826 int dirserv_add_descriptor(const char **desc);
827 int dirserv_init_from_directory_string(const char *dir);
828 void dirserv_free_descriptors();
829 int dirserv_dump_directory_to_string(char *s, int maxlen,
830 crypto_pk_env_t *private_key);
831 void directory_set_dirty();
832 size_t dirserv_get_directory(const char **cp);
835 #endif
838 Local Variables:
839 mode:c
840 indent-tabs-mode:nil
841 c-basic-offset:2
842 End: