adding the reattach-after-timeout feature wasn't so easy after all.
[tor.git] / src / or / or.h
bloba293e6f0100ee6bc781481347e310539abb2ca11
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 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #elif HAVE_POLL_H
32 #include <poll.h>
33 #else
34 #include "../common/fakepoll.h"
35 #endif
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
38 #endif
39 #ifdef HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
42 #ifdef HAVE_SYS_FCNTL_H
43 #include <sys/fcntl.h>
44 #endif
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
50 #endif
51 #ifdef HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #endif
54 #ifdef HAVE_SYS_TIME_H
55 #include <sys/time.h>
56 #endif
57 #ifdef HAVE_SYS_STAT_H
58 #include <sys/stat.h>
59 #endif
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
65 #endif
66 #ifdef HAVE_ERRNO_H
67 #include <errno.h>
68 #endif
69 #ifdef HAVE_ASSERT_H
70 #include <assert.h>
71 #endif
72 #ifdef HAVE_TIME_H
73 #include <time.h>
74 #endif
75 #ifdef HAVE_PWD_H
76 #include <pwd.h>
77 #endif
78 #ifdef HAVE_GRP_H
79 #include <grp.h>
80 #endif
81 #ifdef HAVE_WINSOCK_H
82 #include <winsock.h>
83 #endif
84 #if _MSC_VER > 1300
85 #include <winsock2.h>
86 #include <ws2tcpip.h>
87 #elif defined(_MSC_VER)
88 #include <winsock.h>
89 #endif
91 #ifdef MS_WINDOWS
92 #include <io.h>
93 #include <process.h>
94 #define WIN32_LEAN_AND_MEAN
95 #include <windows.h>
96 #define snprintf _snprintf
97 #endif
99 #include "../common/crypto.h"
100 #include "../common/tortls.h"
101 #include "../common/log.h"
102 #include "../common/util.h"
104 #define MAXCONNECTIONS 1000 /* upper bound on max connections.
105 can be lowered by config file */
107 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
108 #define MAX_NICKNAME_LEN 32
109 #define MAX_DIR_SIZE 500000
111 #ifdef TOR_PERF
112 #define MAX_DNS_ENTRY_AGE (150*60)
113 #else
114 #define MAX_DNS_ENTRY_AGE (15*60)
115 #endif
117 #define CIRC_ID_TYPE_LOWER 0
118 #define CIRC_ID_TYPE_HIGHER 1
120 #define _CONN_TYPE_MIN 3
121 #define CONN_TYPE_OR_LISTENER 3
122 #define CONN_TYPE_OR 4
123 #define CONN_TYPE_EXIT 5
124 #define CONN_TYPE_AP_LISTENER 6
125 #define CONN_TYPE_AP 7
126 #define CONN_TYPE_DIR_LISTENER 8
127 #define CONN_TYPE_DIR 9
128 #define CONN_TYPE_DNSWORKER 10
129 #define CONN_TYPE_CPUWORKER 11
130 #define _CONN_TYPE_MAX 11
132 #define LISTENER_STATE_READY 0
134 #define _DNSWORKER_STATE_MIN 1
135 #define DNSWORKER_STATE_IDLE 1
136 #define DNSWORKER_STATE_BUSY 2
137 #define _DNSWORKER_STATE_MAX 2
139 #define _CPUWORKER_STATE_MIN 1
140 #define CPUWORKER_STATE_IDLE 1
141 #define CPUWORKER_STATE_BUSY_ONION 2
142 #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
143 #define _CPUWORKER_STATE_MAX 3
145 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
147 #define _OR_CONN_STATE_MIN 1
148 #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
149 #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
150 #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
151 #define _OR_CONN_STATE_MAX 3
153 #define _EXIT_CONN_STATE_MIN 1
154 #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
155 #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
156 #define EXIT_CONN_STATE_OPEN 3
157 #define _EXIT_CONN_STATE_MAX 3
158 #if 0
159 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
160 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
161 #endif
163 /* the AP state values must be disjoint from the EXIT state values */
164 #define _AP_CONN_STATE_MIN 4
165 #define AP_CONN_STATE_SOCKS_WAIT 4
166 #define AP_CONN_STATE_CIRCUIT_WAIT 5
167 #define AP_CONN_STATE_CONNECTING 6
168 #define AP_CONN_STATE_OPEN 7
169 #define _AP_CONN_STATE_MAX 7
171 #define _DIR_CONN_STATE_MIN 1
172 #define DIR_CONN_STATE_CONNECTING_FETCH 1
173 #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
174 #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
175 #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
176 #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
177 #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
178 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
179 #define DIR_CONN_STATE_SERVER_WRITING 8
180 #define _DIR_CONN_STATE_MAX 8
182 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
183 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
184 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
185 #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
187 #define RELAY_COMMAND_BEGIN 1
188 #define RELAY_COMMAND_DATA 2
189 #define RELAY_COMMAND_END 3
190 #define RELAY_COMMAND_CONNECTED 4
191 #define RELAY_COMMAND_SENDME 5
192 #define RELAY_COMMAND_EXTEND 6
193 #define RELAY_COMMAND_EXTENDED 7
194 #define RELAY_COMMAND_TRUNCATE 8
195 #define RELAY_COMMAND_TRUNCATED 9
196 #define RELAY_COMMAND_DROP 10
198 #define END_STREAM_REASON_MISC 1
199 #define END_STREAM_REASON_RESOLVEFAILED 2
200 #define END_STREAM_REASON_CONNECTFAILED 3
201 #define END_STREAM_REASON_EXITPOLICY 4
202 #define END_STREAM_REASON_DESTROY 5
203 #define END_STREAM_REASON_DONE 6
205 /* default cipher function */
206 #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
207 /* Used to en/decrypt onion skins */
208 #define ONION_CIPHER DEFAULT_CIPHER
209 /* Used to en/decrypt RELAY cells */
210 #define CIRCUIT_CIPHER DEFAULT_CIPHER
212 #define CELL_DIRECTION_IN 1
213 #define CELL_DIRECTION_OUT 2
214 #define EDGE_EXIT CONN_TYPE_EXIT
215 #define EDGE_AP CONN_TYPE_AP
216 #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
218 #ifdef TOR_PERF
219 #define CIRCWINDOW_START 10000
220 #define CIRCWINDOW_INCREMENT 1000
221 #define STREAMWINDOW_START 5000
222 #define STREAMWINDOW_INCREMENT 500
223 #else
224 #define CIRCWINDOW_START 1000
225 #define CIRCWINDOW_INCREMENT 100
226 #define STREAMWINDOW_START 500
227 #define STREAMWINDOW_INCREMENT 50
228 #endif
230 /* cell commands */
231 #define CELL_PADDING 0
232 #define CELL_CREATE 1
233 #define CELL_CREATED 2
234 #define CELL_RELAY 3
235 #define CELL_DESTROY 4
237 /* legal characters in a filename */
238 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
239 /* legal characters in a nickname */
240 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
242 #define SOCKS4_NETWORK_LEN 8
245 * Relay payload:
246 * Relay command [1 byte]
247 * Recognized [2 bytes]
248 * Stream ID [2 bytes]
249 * Partial SHA-1 [4 bytes]
250 * Length [2 bytes]
251 * Relay payload [498 bytes]
254 #if 0
255 #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
256 #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
258 #define CELL_RELAY_RECOGNIZED(c) (ntohs(*(uint16_t*)((c).payload+1)))
259 #define SET_CELL_RELAY_RECOGNIZED(c,r) (*(uint16_t*)((c).payload+1) = htons(r))
261 #define STREAM_ID_SIZE 2
262 //#define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
263 #define CELL_RELAY_STREAM_ID(c) (ntohs(*(uint16_t*)((c).payload+3)))
264 #define SET_CELL_RELAY_STREAM_ID(c,id) (*(uint16_t*)((c).payload+3) = htons(id))
265 #define ZERO_STREAM 0
267 /* integrity is the first 32 bits (in network order) of a sha-1 of all
268 * cell payloads that are relay cells that have been sent / delivered
269 * to the hop on the * circuit (the integrity is zeroed while doing
270 * each calculation)
272 #define CELL_RELAY_INTEGRITY(c) (ntohl(*(uint32_t*)((c).payload+5)))
273 #define SET_CELL_RELAY_INTEGRITY(c,i) (*(uint32_t*)((c).payload+5) = htonl(i))
275 /* relay length is how many bytes are used in the cell payload past relay_header_size */
276 #define CELL_RELAY_LENGTH(c) (ntohs(*(uint16_t*)((c).payload+9)))
277 #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+9) = htons(len))
278 #endif
280 #define CELL_PAYLOAD_SIZE 509
281 #define CELL_NETWORK_SIZE 512
283 #define RELAY_HEADER_SIZE (1+2+2+4+2)
284 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
286 /* cell definition */
287 typedef struct {
288 uint16_t circ_id;
289 unsigned char command;
290 unsigned char payload[CELL_PAYLOAD_SIZE];
291 } cell_t;
293 typedef struct {
294 uint8_t command;
295 uint16_t recognized;
296 uint16_t stream_id;
297 char integrity[4];
298 uint16_t length;
299 } relay_header_t;
301 typedef struct buf_t buf_t;
302 typedef struct socks_request_t socks_request_t;
304 struct connection_t {
306 uint8_t type;
307 uint8_t state;
308 uint8_t wants_to_read; /* should we start reading again once
309 * the bandwidth throttler allows it?
311 uint8_t wants_to_write; /* should we start writing again once
312 * the bandwidth throttler allows reads?
314 int s; /* our socket */
315 int poll_index; /* index of this conn into the poll_array */
316 int marked_for_close; /* should we close this conn on the next
317 * iteration of the main loop?
320 buf_t *inbuf;
321 int inbuf_reached_eof; /* did read() return 0 on this conn? */
322 time_t timestamp_lastread; /* when was the last time poll() said we could read? */
324 buf_t *outbuf;
325 int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
326 time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
328 time_t timestamp_created; /* when was this connection_t created? */
330 uint32_t addr; /* these two uniquely identify a router. Both in host order. */
331 uint16_t port; /* if non-zero, they identify the guy on the other end
332 * of the connection. */
333 char *address; /* FQDN (or IP) of the guy on the other end.
334 * strdup into this, because free_connection frees it
336 crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
337 crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
338 crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
339 char *nickname;
341 /* Used only by OR connections: */
342 tor_tls *tls;
343 uint16_t next_circ_id; /* Which circ_id do we try to use next on this connection?
344 * This is always in the range 0..1<<15-1.*/
346 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
347 uint32_t bandwidth; /* connection bandwidth. */
348 int receiver_bucket; /* when this hits 0, stop receiving. Every second we
349 * add 'bandwidth' to this, capping it at 10*bandwidth.
352 /* Used only by edge connections: */
353 uint16_t stream_id;
354 struct connection_t *next_stream; /* points to the next stream at this edge, if any */
355 struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
356 int package_window; /* how many more relay cells can i send into the circuit? */
357 int deliver_window; /* how many more relay cells can end at me? */
359 int done_sending; /* for half-open connections; not used currently */
360 int done_receiving;
361 char has_sent_end; /* for debugging: set once we've set the stream end,
362 and check in circuit_about_to_close_connection() */
364 /* Used only by AP connections */
365 socks_request_t *socks_request;
368 typedef struct connection_t connection_t;
370 #define EXIT_POLICY_ACCEPT 1
371 #define EXIT_POLICY_REJECT 2
373 struct exit_policy_t {
374 char policy_type;
375 char *string;
376 uint32_t addr;
377 uint32_t msk;
378 uint16_t prt_min;
379 uint16_t prt_max;
381 struct exit_policy_t *next;
384 /* config stuff we know about the other ORs in the network */
385 typedef struct {
386 char *address;
387 char *nickname;
389 uint32_t addr; /* all host order */
390 uint16_t or_port;
391 uint16_t socks_port;
392 uint16_t dir_port;
394 time_t published_on;
396 crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
397 crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
398 crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
400 int is_running;
402 /* link info */
403 uint32_t bandwidthrate;
404 uint32_t bandwidthburst;
405 struct exit_policy_t *exit_policy;
406 } routerinfo_t;
408 #define MAX_ROUTERS_IN_DIR 1024
409 typedef struct {
410 routerinfo_t **routers;
411 int n_routers;
412 char *software_versions;
413 time_t published_on;
414 } routerlist_t;
416 struct crypt_path_t {
418 /* crypto environments */
419 crypto_cipher_env_t *f_crypto;
420 crypto_cipher_env_t *b_crypto;
422 crypto_digest_env_t *f_digest; /* for integrity checking */
423 crypto_digest_env_t *b_digest;
425 crypto_dh_env_t *handshake_state;
427 uint32_t addr;
428 uint16_t port;
430 uint8_t state;
431 #define CPATH_STATE_CLOSED 0
432 #define CPATH_STATE_AWAITING_KEYS 1
433 #define CPATH_STATE_OPEN 2
434 struct crypt_path_t *next;
435 struct crypt_path_t *prev; /* doubly linked list */
437 int package_window;
438 int deliver_window;
441 #define DH_KEY_LEN CRYPTO_DH_SIZE
442 #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN)
443 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
445 typedef struct crypt_path_t crypt_path_t;
447 typedef struct {
448 int desired_path_len;
449 char *chosen_exit; /* nickname of planned exit node */
450 } cpath_build_state_t;
452 /* struct for a path (circuit) through the network */
453 struct circuit_t {
454 uint32_t n_addr;
455 uint16_t n_port;
456 connection_t *p_conn;
457 connection_t *n_conn; /* for the OR conn, if there is one */
458 connection_t *p_streams;
459 connection_t *n_streams;
460 uint16_t next_stream_id;
461 int package_window;
462 int deliver_window;
464 uint16_t p_circ_id; /* circuit identifiers */
465 uint16_t n_circ_id;
467 crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
468 crypto_cipher_env_t *n_crypto;
470 crypto_digest_env_t *p_digest; /* for integrity checking, */
471 crypto_digest_env_t *n_digest; /* intermediate hops only */
473 cpath_build_state_t *build_state;
474 crypt_path_t *cpath;
476 char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
477 time_t timestamp_created;
478 time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
480 uint8_t state;
482 void *next;
485 typedef struct circuit_t circuit_t;
487 typedef struct {
488 char *LogLevel;
489 char *LogFile;
490 char *DebugLogFile;
491 char *DataDirectory;
492 char *RouterFile;
493 char *Nickname;
494 char *Address;
495 char *PidFile;
496 char *ExitNodes;
497 char *EntryNodes;
498 char *ExcludedNodes;
499 char *ExitPolicy;
500 char *SocksBindAddress;
501 char *ORBindAddress;
502 char *DirBindAddress;
503 char *RecommendedVersions;
504 char *User;
505 char *Group;
506 double PathlenCoinWeight;
507 int ORPort;
508 int SocksPort;
509 int DirPort;
510 int MaxConn;
511 int TrafficShaping;
512 int LinkPadding;
513 int IgnoreVersion;
514 int RunAsDaemon;
515 int DirRebuildPeriod;
516 int DirFetchPostPeriod;
517 int KeepalivePeriod;
518 int MaxOnionsPending;
519 int NewCircuitPeriod;
520 int BandwidthRate;
521 int BandwidthBurst;
522 int NumCpus;
523 int loglevel;
524 } or_options_t;
526 /* XXX are these good enough defaults? */
527 #define MAX_SOCKS_REPLY_LEN 256
528 /* Not 256; addresses must fit in a begin cell. */
529 #define MAX_SOCKS_ADDR_LEN 200
530 struct socks_request_t {
531 char socks_version;
532 int replylen;
533 char reply[MAX_SOCKS_REPLY_LEN];
534 char address[MAX_SOCKS_ADDR_LEN];
535 uint16_t port;
538 /* all the function prototypes go here */
540 /********************************* buffers.c ***************************/
542 int find_on_inbuf(char *string, int string_len, buf_t *buf);
544 buf_t *buf_new();
545 buf_t *buf_new_with_capacity(size_t size);
546 void buf_free(buf_t *buf);
548 size_t buf_datalen(const buf_t *buf);
549 size_t buf_capacity(const buf_t *buf);
550 const char *_buf_peek_raw_buffer(const buf_t *buf);
552 int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
553 int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
555 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
556 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
558 int write_to_buf(const char *string, int string_len, buf_t *buf);
559 int fetch_from_buf(char *string, int string_len, buf_t *buf);
560 int fetch_from_buf_http(buf_t *buf,
561 char **headers_out, int max_headerlen,
562 char **body_out, int max_bodylen);
563 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
565 /********************************* circuit.c ***************************/
567 void circuit_add(circuit_t *circ);
568 void circuit_remove(circuit_t *circ);
569 circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
570 void circuit_free(circuit_t *circ);
571 void circuit_free_cpath(crypt_path_t *cpath);
573 circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
574 circuit_t *circuit_get_by_conn(connection_t *conn);
575 circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
577 void circuit_expire_building(void);
578 int circuit_count_building(void);
579 int circuit_stream_is_being_handled(connection_t *conn);
581 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
582 int cell_direction);
583 int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
584 int cell_direction, crypt_path_t *layer_hint);
586 void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
587 int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
588 void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
590 void circuit_close(circuit_t *circ);
591 void circuit_detach_stream(circuit_t *circ, connection_t *conn);
592 void circuit_about_to_close_connection(connection_t *conn);
594 void circuit_log_path(int severity, circuit_t *circ);
595 void circuit_dump_by_conn(connection_t *conn, int severity);
597 void circuit_expire_unused_circuits(void);
598 int circuit_launch_new(void);
599 void circuit_increment_failure_count(void);
600 void circuit_reset_failure_count(void);
601 int circuit_establish_circuit(void);
602 void circuit_n_conn_open(connection_t *or_conn);
603 int circuit_send_next_onion_skin(circuit_t *circ);
604 int circuit_extend(cell_t *cell, circuit_t *circ);
605 int circuit_finish_handshake(circuit_t *circ, char *reply);
606 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
608 void assert_cpath_ok(const crypt_path_t *c);
609 void assert_cpath_layer_ok(const crypt_path_t *c);
610 void assert_circuit_ok(const circuit_t *c);
612 extern unsigned long stats_n_relay_cells_relayed;
613 extern unsigned long stats_n_relay_cells_delivered;
615 /********************************* command.c ***************************/
617 void command_process_cell(cell_t *cell, connection_t *conn);
619 extern unsigned long stats_n_padding_cells_processed;
620 extern unsigned long stats_n_create_cells_processed;
621 extern unsigned long stats_n_created_cells_processed;
622 extern unsigned long stats_n_relay_cells_processed;
623 extern unsigned long stats_n_destroy_cells_processed;
625 /********************************* config.c ***************************/
627 int getconfig(int argc, char **argv, or_options_t *options);
629 /********************************* connection.c ***************************/
631 connection_t *connection_new(int type);
632 void connection_free(connection_t *conn);
633 void connection_free_all(void);
635 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
637 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
638 int retry_all_connections(void);
640 int connection_handle_read(connection_t *conn);
641 int connection_read_to_buf(connection_t *conn);
643 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
644 int connection_find_on_inbuf(char *string, int len, connection_t *conn);
646 int connection_wants_to_flush(connection_t *conn);
647 int connection_outbuf_too_full(connection_t *conn);
648 int connection_handle_write(connection_t *conn);
649 void connection_write_to_buf(const char *string, int len, connection_t *conn);
651 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
652 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
654 connection_t *connection_get_by_type(int type);
655 connection_t *connection_get_by_type_state(int type, int state);
656 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
658 int connection_receiver_bucket_should_increase(connection_t *conn);
660 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
661 #define connection_has_pending_tls_data(conn) \
662 ((conn)->type == CONN_TYPE_OR && \
663 (conn)->state == OR_CONN_STATE_OPEN && \
664 tor_tls_get_pending_bytes(conn->tls))
665 int connection_is_listener(connection_t *conn);
666 int connection_state_is_open(connection_t *conn);
668 int connection_send_destroy(uint16_t circ_id, connection_t *conn);
670 int connection_process_inbuf(connection_t *conn);
671 int connection_finished_flushing(connection_t *conn);
673 void assert_connection_ok(connection_t *conn, time_t now);
675 /********************************* connection_edge.c ***************************/
677 void relay_header_pack(char *dest, const relay_header_t *src);
678 void relay_header_unpack(relay_header_t *dest, const char *src);
679 int connection_edge_process_inbuf(connection_t *conn);
680 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
682 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
683 void *payload, int payload_len, crypt_path_t *cpath_layer);
685 int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
686 int edge_type, crypt_path_t *layer_hint);
687 int connection_edge_finished_flushing(connection_t *conn);
689 int connection_edge_package_raw_inbuf(connection_t *conn);
691 void connection_exit_connect(connection_t *conn);
692 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
693 void connection_ap_expire_beginning(void);
694 void connection_ap_attach_pending(void);
696 extern uint64_t stats_n_data_cells_packaged;
697 extern uint64_t stats_n_data_bytes_packaged;
698 extern uint64_t stats_n_data_cells_received;
699 extern uint64_t stats_n_data_bytes_received;
701 void client_dns_init(void);
702 void client_dns_clean(void);
704 /********************************* connection_or.c ***************************/
706 int connection_or_process_inbuf(connection_t *conn);
707 int connection_or_finished_flushing(connection_t *conn);
709 void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
710 connection_t *connection_or_connect(routerinfo_t *router);
712 int connection_tls_start_handshake(connection_t *conn, int receiving);
713 int connection_tls_continue_handshake(connection_t *conn);
715 void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
717 /********************************* cpuworker.c *****************************/
719 void cpu_init(void);
720 int connection_cpu_finished_flushing(connection_t *conn);
721 int connection_cpu_process_inbuf(connection_t *conn);
722 int cpuworker_main(void *data);
723 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
724 void *task);
726 /********************************* directory.c ***************************/
728 void directory_initiate_command(routerinfo_t *router, int command);
729 int connection_dir_process_inbuf(connection_t *conn);
730 int connection_dir_finished_flushing(connection_t *conn);
732 /********************************* dns.c ***************************/
734 void dns_init(void);
735 int connection_dns_finished_flushing(connection_t *conn);
736 int connection_dns_process_inbuf(connection_t *conn);
737 void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
738 int dns_resolve(connection_t *exitconn);
740 /********************************* main.c ***************************/
742 int connection_add(connection_t *conn);
743 int connection_remove(connection_t *conn);
744 void connection_set_poll_socket(connection_t *conn);
746 void get_connection_array(connection_t ***array, int *n);
748 void connection_watch_events(connection_t *conn, short events);
749 int connection_is_reading(connection_t *conn);
750 void connection_stop_reading(connection_t *conn);
751 void connection_start_reading(connection_t *conn);
752 void connection_stop_writing(connection_t *conn);
753 void connection_start_writing(connection_t *conn);
755 int main(int argc, char *argv[]);
757 /********************************* onion.c ***************************/
759 int decide_circ_id_type(char *local_nick, char *remote_nick);
761 int onion_pending_add(circuit_t *circ);
762 circuit_t *onion_next_task(void);
763 void onion_pending_remove(circuit_t *circ);
765 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
767 int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
768 routerinfo_t **router_out);
770 int onion_skin_create(crypto_pk_env_t *router_key,
771 crypto_dh_env_t **handshake_state_out,
772 char *onion_skin_out);
774 int onion_skin_server_handshake(char *onion_skin,
775 crypto_pk_env_t *private_key,
776 char *handshake_reply_out,
777 char *key_out,
778 int key_out_len);
780 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
781 char *handshake_reply,
782 char *key_out,
783 int key_out_len);
785 cpath_build_state_t *onion_new_cpath_build_state(void);
787 /********************************* router.c ***************************/
789 void set_onion_key(crypto_pk_env_t *k);
790 crypto_pk_env_t *get_onion_key(void);
791 void set_identity_key(crypto_pk_env_t *k);
792 crypto_pk_env_t *get_identity_key(void);
793 crypto_pk_env_t *get_link_key(void);
794 int init_keys(void);
796 void router_retry_connections(void);
797 void router_upload_desc_to_dirservers(void);
798 int router_compare_to_my_exit_policy(connection_t *conn);
799 const char *router_get_my_descriptor(void);
800 int router_rebuild_descriptor(void);
801 int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
802 crypto_pk_env_t *ident_key);
804 /********************************* routerlist.c ***************************/
806 routerinfo_t *router_pick_directory_server(void);
807 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
808 routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
809 routerinfo_t *router_get_by_nickname(char *nickname);
810 void router_get_routerlist(routerlist_t **prouterlist);
811 void routerinfo_free(routerinfo_t *router);
812 void router_mark_as_down(char *nickname);
813 int router_set_routerlist_from_file(char *routerfile);
814 int router_get_dir_hash(const char *s, char *digest);
815 int router_get_router_hash(const char *s, char *digest);
816 int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
817 routerinfo_t *router_get_entry_from_string(const char **s);
818 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
819 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
820 struct exit_policy_t *policy);
821 #define ADDR_POLICY_ACCEPTED 0
822 #define ADDR_POLICY_REJECTED -1
823 #define ADDR_POLICY_UNKNOWN 1
824 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
825 int router_exit_policy_rejects_all(routerinfo_t *router);
827 /********************************* dirserv.c ***************************/
828 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
829 int dirserv_parse_fingerprint_file(const char *fname);
830 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
831 void dirserv_free_fingerprint_list();
832 int dirserv_add_descriptor(const char **desc);
833 int dirserv_init_from_directory_string(const char *dir);
834 void dirserv_free_descriptors();
835 int dirserv_dump_directory_to_string(char *s, int maxlen,
836 crypto_pk_env_t *private_key);
837 void directory_set_dirty();
838 size_t dirserv_get_directory(const char **cp);
841 #endif
844 Local Variables:
845 mode:c
846 indent-tabs-mode:nil
847 c-basic-offset:2
848 End: