move cell size to 512 bytes
[tor.git] / src / or / or.h
blob0b63ccf2862d399ee578fe25cc31409e2f7c0c1e
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 50000 /* XXX, big enough? */
111 #define MAX_DNS_ENTRY_AGE (15*60)
113 #define CIRC_ID_TYPE_LOWER 0
114 #define CIRC_ID_TYPE_HIGHER 1
116 #define _CONN_TYPE_MIN 3
117 #define CONN_TYPE_OR_LISTENER 3
118 #define CONN_TYPE_OR 4
119 #define CONN_TYPE_EXIT 5
120 #define CONN_TYPE_AP_LISTENER 6
121 #define CONN_TYPE_AP 7
122 #define CONN_TYPE_DIR_LISTENER 8
123 #define CONN_TYPE_DIR 9
124 #define CONN_TYPE_DNSWORKER 10
125 #define CONN_TYPE_CPUWORKER 11
126 #define _CONN_TYPE_MAX 11
128 #define LISTENER_STATE_READY 0
130 #define _DNSWORKER_STATE_MIN 1
131 #define DNSWORKER_STATE_IDLE 1
132 #define DNSWORKER_STATE_BUSY 2
133 #define _DNSWORKER_STATE_MAX 2
135 #define _CPUWORKER_STATE_MIN 1
136 #define CPUWORKER_STATE_IDLE 1
137 #define CPUWORKER_STATE_BUSY_ONION 2
138 #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
139 #define _CPUWORKER_STATE_MAX 3
141 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
143 #define _OR_CONN_STATE_MIN 1
144 #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
145 #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
146 #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
147 #define _OR_CONN_STATE_MAX 3
149 #define _EXIT_CONN_STATE_MIN 1
150 #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
151 #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
152 #define EXIT_CONN_STATE_OPEN 3
153 #define _EXIT_CONN_STATE_MAX 3
154 #if 0
155 #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
156 #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
157 #endif
159 /* the AP state values must be disjoint from the EXIT state values */
160 #define _AP_CONN_STATE_MIN 4
161 #define AP_CONN_STATE_SOCKS_WAIT 4
162 #define AP_CONN_STATE_CIRCUIT_WAIT 5
163 #define AP_CONN_STATE_OPEN 6
164 #define _AP_CONN_STATE_MAX 6
166 #define _DIR_CONN_STATE_MIN 1
167 #define DIR_CONN_STATE_CONNECTING_FETCH 1
168 #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
169 #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
170 #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
171 #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
172 #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
173 #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
174 #define DIR_CONN_STATE_SERVER_WRITING 8
175 #define _DIR_CONN_STATE_MAX 8
177 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
178 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
179 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
180 #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
182 #define RELAY_COMMAND_BEGIN 1
183 #define RELAY_COMMAND_DATA 2
184 #define RELAY_COMMAND_END 3
185 #define RELAY_COMMAND_CONNECTED 4
186 #define RELAY_COMMAND_SENDME 5
187 #define RELAY_COMMAND_EXTEND 6
188 #define RELAY_COMMAND_EXTENDED 7
189 #define RELAY_COMMAND_TRUNCATE 8
190 #define RELAY_COMMAND_TRUNCATED 9
191 #define RELAY_COMMAND_DROP 10
193 #define END_STREAM_REASON_MISC 1
194 #define END_STREAM_REASON_RESOLVEFAILED 2
195 #define END_STREAM_REASON_CONNECTFAILED 3
196 #define END_STREAM_REASON_EXITPOLICY 4
197 #define END_STREAM_REASON_DESTROY 5
198 #define END_STREAM_REASON_DONE 6
200 /* default cipher function */
201 #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
202 /* Used to en/decrypt onion skins */
203 #define ONION_CIPHER DEFAULT_CIPHER
204 /* Used to en/decrypt RELAY cells */
205 #define CIRCUIT_CIPHER DEFAULT_CIPHER
207 #define CELL_DIRECTION_IN 1
208 #define CELL_DIRECTION_OUT 2
209 #define EDGE_EXIT CONN_TYPE_EXIT
210 #define EDGE_AP CONN_TYPE_AP
211 #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
213 #define CIRCWINDOW_START 1000
214 #define CIRCWINDOW_INCREMENT 100
216 #define STREAMWINDOW_START 500
217 #define STREAMWINDOW_INCREMENT 50
219 /* cell commands */
220 #define CELL_PADDING 0
221 #define CELL_CREATE 1
222 #define CELL_CREATED 2
223 #define CELL_RELAY 3
224 #define CELL_DESTROY 4
226 /* legal characters in a filename */
227 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
228 /* legal characters in a nickname */
229 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
231 #define SOCKS4_NETWORK_LEN 8
233 typedef uint16_t circ_id_t;
236 * Relay payload:
237 * Relay command [1 byte]
238 * Stream ID [7 bytes]
239 * Partial SHA-1 [4 bytes]
240 * Length [2 bytes]
241 * Relay payload [495 bytes]
244 #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
245 #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
247 #define STREAM_ID_SIZE 7
248 #define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
249 #define ZERO_STREAM "\0\0\0\0\0\0\0"
251 #define CELL_RELAY_COMMAND_END_REASON(c) (*(uint8_t)((c).payload+1))
253 /* relay length is how many bytes are used in the cell payload past relay_header_size */
254 #define CELL_RELAY_LENGTH(c) (*(uint16_t*)((c).payload+1+STREAM_ID_SIZE+4))
255 #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+1+STREAM_ID_SIZE+4) = (len))
257 #define CELL_PAYLOAD_SIZE 509
258 #define CELL_NETWORK_SIZE 512
260 /* cell definition */
261 typedef struct {
262 circ_id_t circ_id;
263 unsigned char command;
264 unsigned char payload[CELL_PAYLOAD_SIZE];
265 } cell_t;
267 #define RELAY_HEADER_SIZE (1+STREAM_ID_SIZE+4+2)
268 #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
270 typedef struct buf_t buf_t;
271 typedef struct socks_request_t socks_request_t;
273 struct connection_t {
275 uint8_t type;
276 uint8_t state;
277 uint8_t wants_to_read; /* should we start reading again once
278 * the bandwidth throttler allows it?
280 uint8_t wants_to_write; /* should we start writing again once
281 * the bandwidth throttler allows reads?
283 int s; /* our socket */
284 int poll_index; /* index of this conn into the poll_array */
285 int marked_for_close; /* should we close this conn on the next
286 * iteration of the main loop?
289 buf_t *inbuf;
290 int inbuf_reached_eof; /* did read() return 0 on this conn? */
291 time_t timestamp_lastread; /* when was the last time poll() said we could read? */
293 buf_t *outbuf;
294 int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
295 time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
297 time_t timestamp_created; /* when was this connection_t created? */
299 uint32_t addr; /* these two uniquely identify a router. Both in host order. */
300 uint16_t port; /* if non-zero, they identify the guy on the other end
301 * of the connection. */
302 char *address; /* FQDN (or IP) of the guy on the other end.
303 * strdup into this, because free_connection frees it
305 crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
306 crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
307 crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
308 char *nickname;
310 /* Used only by OR connections: */
311 tor_tls *tls;
312 circ_id_t next_circ_id; /* Which circ_id do we try to use next on this connection?
313 * This is always in the range 0..1<<15-1.*/
315 /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
316 uint32_t bandwidth; /* connection bandwidth. */
317 int receiver_bucket; /* when this hits 0, stop receiving. Every second we
318 * add 'bandwidth' to this, capping it at 10*bandwidth.
321 /* Used only by edge connections: */
322 char stream_id[STREAM_ID_SIZE];
323 struct connection_t *next_stream; /* points to the next stream at this edge, if any */
324 struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
325 int package_window; /* how many more relay cells can i send into the circuit? */
326 int deliver_window; /* how many more relay cells can end at me? */
328 int done_sending; /* for half-open connections; not used currently */
329 int done_receiving;
330 char has_sent_end; /* for debugging: set once we've set the stream end,
331 and check in circuit_about_to_close_connection() */
333 /* Used only by AP connections */
334 socks_request_t *socks_request;
337 typedef struct connection_t connection_t;
339 #define EXIT_POLICY_ACCEPT 1
340 #define EXIT_POLICY_REJECT 2
342 struct exit_policy_t {
343 char policy_type;
344 char *string;
345 uint32_t addr;
346 uint32_t msk;
347 uint16_t prt_min;
348 uint16_t prt_max;
350 struct exit_policy_t *next;
353 /* config stuff we know about the other ORs in the network */
354 typedef struct {
355 char *address;
356 char *nickname;
358 uint32_t addr; /* all host order */
359 uint16_t or_port;
360 uint16_t socks_port;
361 uint16_t dir_port;
363 time_t published_on;
365 crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
366 crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
367 crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
369 int is_running;
371 /* link info */
372 uint32_t bandwidth;
373 struct exit_policy_t *exit_policy;
374 } routerinfo_t;
376 #define MAX_ROUTERS_IN_DIR 1024
377 typedef struct {
378 routerinfo_t **routers;
379 int n_routers;
380 char *software_versions;
381 time_t published_on;
382 } routerlist_t;
384 struct crypt_path_t {
386 /* crypto environments */
387 crypto_cipher_env_t *f_crypto;
388 crypto_cipher_env_t *b_crypto;
390 crypto_digest_env_t *f_digest; /* for integrity checking */
391 crypto_digest_env_t *b_digest;
393 crypto_dh_env_t *handshake_state;
395 uint32_t addr;
396 uint16_t port;
398 uint8_t state;
399 #define CPATH_STATE_CLOSED 0
400 #define CPATH_STATE_AWAITING_KEYS 1
401 #define CPATH_STATE_OPEN 2
402 struct crypt_path_t *next;
403 struct crypt_path_t *prev; /* doubly linked list */
405 int package_window;
406 int deliver_window;
409 #define DH_KEY_LEN CRYPTO_DH_SIZE
410 #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN+6+8+16)
411 #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
413 typedef struct crypt_path_t crypt_path_t;
415 typedef struct {
416 int desired_path_len;
417 char *chosen_exit; /* nickname of planned exit node */
418 } cpath_build_state_t;
420 /* struct for a path (circuit) through the network */
421 struct circuit_t {
422 uint32_t n_addr;
423 uint16_t n_port;
424 connection_t *p_conn;
425 connection_t *n_conn; /* for the OR conn, if there is one */
426 connection_t *p_streams;
427 connection_t *n_streams;
428 int package_window;
429 int deliver_window;
431 circ_id_t p_circ_id; /* circuit identifiers */
432 circ_id_t n_circ_id;
434 crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
435 crypto_cipher_env_t *n_crypto;
437 crypto_digest_env_t *p_digest; /* for integrity checking, */
438 crypto_digest_env_t *n_digest; /* intermediate hops only */
440 cpath_build_state_t *build_state;
441 crypt_path_t *cpath;
443 char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
444 time_t timestamp_created;
445 time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
447 uint8_t state;
449 void *next;
452 typedef struct circuit_t circuit_t;
454 typedef struct {
455 char *LogLevel;
456 char *LogFile;
457 char *DebugLogFile;
458 char *DataDirectory;
459 char *RouterFile;
460 char *Nickname;
461 char *Address;
462 char *PidFile;
463 char *ExitNodes;
464 char *EntryNodes;
465 char *ExcludedNodes;
466 char *ExitPolicy;
467 char *SocksBindAddress;
468 char *ORBindAddress;
469 char *DirBindAddress;
470 char *RecommendedVersions;
471 char *User;
472 char *Group;
473 double PathlenCoinWeight;
474 int ORPort;
475 int SocksPort;
476 int DirPort;
477 int MaxConn;
478 int TrafficShaping;
479 int LinkPadding;
480 int IgnoreVersion;
481 int RunAsDaemon;
482 int DirRebuildPeriod;
483 int DirFetchPostPeriod;
484 int KeepalivePeriod;
485 int MaxOnionsPending;
486 int NewCircuitPeriod;
487 int TotalBandwidth;
488 int NumCpus;
489 int loglevel;
490 } or_options_t;
492 /* XXX are these good enough defaults? */
493 #define MAX_SOCKS_REPLY_LEN 256
494 /* Not 256; addresses must fit in a begin cell. */
495 #define MAX_SOCKS_ADDR_LEN 200
496 struct socks_request_t {
497 char socks_version;
498 int replylen;
499 char reply[MAX_SOCKS_REPLY_LEN];
500 char address[MAX_SOCKS_ADDR_LEN];
501 uint16_t port;
504 /* all the function prototypes go here */
506 /********************************* buffers.c ***************************/
508 int find_on_inbuf(char *string, int string_len, buf_t *buf);
510 buf_t *buf_new();
511 buf_t *buf_new_with_capacity(size_t size);
512 void buf_free(buf_t *buf);
514 size_t buf_datalen(const buf_t *buf);
515 size_t buf_capacity(const buf_t *buf);
516 const char *_buf_peek_raw_buffer(const buf_t *buf);
518 int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
519 int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
521 int flush_buf(int s, buf_t *buf, int *buf_flushlen);
522 int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
524 int write_to_buf(const char *string, int string_len, buf_t *buf);
525 int fetch_from_buf(char *string, int string_len, buf_t *buf);
526 int fetch_from_buf_http(buf_t *buf,
527 char *headers_out, int max_headerlen,
528 char *body_out, int max_bodylen);
529 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
531 /********************************* circuit.c ***************************/
533 void circuit_add(circuit_t *circ);
534 void circuit_remove(circuit_t *circ);
535 circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn);
536 void circuit_free(circuit_t *circ);
537 void circuit_free_cpath(crypt_path_t *cpath);
539 circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn);
540 circuit_t *circuit_get_by_conn(connection_t *conn);
541 circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
543 void circuit_expire_building(void);
544 int circuit_count_building(void);
545 int circuit_stream_is_being_handled(connection_t *conn);
547 int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
548 int cell_direction, crypt_path_t *layer_hint);
549 int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
550 crypt_path_t **layer_hint, char *recognized, connection_t **conn);
551 int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
553 void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
554 int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
555 int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
557 void circuit_close(circuit_t *circ);
558 void circuit_about_to_close_connection(connection_t *conn);
560 void circuit_dump_by_conn(connection_t *conn, int severity);
562 void circuit_expire_unused_circuits(void);
563 int circuit_launch_new(void);
564 void circuit_increment_failure_count(void);
565 void circuit_reset_failure_count(void);
566 int circuit_establish_circuit(void);
567 void circuit_n_conn_open(connection_t *or_conn);
568 int circuit_send_next_onion_skin(circuit_t *circ);
569 int circuit_extend(cell_t *cell, circuit_t *circ);
570 int circuit_finish_handshake(circuit_t *circ, char *reply);
571 int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
573 void assert_cpath_ok(const crypt_path_t *c);
574 void assert_cpath_layer_ok(const crypt_path_t *c);
575 void assert_circuit_ok(const circuit_t *c);
577 extern unsigned long stats_n_relay_cells_relayed;
578 extern unsigned long stats_n_relay_cells_delivered;
580 /********************************* command.c ***************************/
582 void command_process_cell(cell_t *cell, connection_t *conn);
584 extern unsigned long stats_n_padding_cells_processed;
585 extern unsigned long stats_n_create_cells_processed;
586 extern unsigned long stats_n_created_cells_processed;
587 extern unsigned long stats_n_relay_cells_processed;
588 extern unsigned long stats_n_destroy_cells_processed;
590 /********************************* config.c ***************************/
592 int getconfig(int argc, char **argv, or_options_t *options);
594 /********************************* connection.c ***************************/
596 connection_t *connection_new(int type);
597 void connection_free(connection_t *conn);
599 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
601 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
602 int retry_all_connections(void);
604 int connection_handle_read(connection_t *conn);
605 int connection_read_to_buf(connection_t *conn);
607 int connection_fetch_from_buf(char *string, int len, connection_t *conn);
608 int connection_find_on_inbuf(char *string, int len, connection_t *conn);
610 int connection_wants_to_flush(connection_t *conn);
611 int connection_outbuf_too_full(connection_t *conn);
612 int connection_handle_write(connection_t *conn);
613 void connection_write_to_buf(const char *string, int len, connection_t *conn);
615 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
616 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
618 connection_t *connection_get_by_type(int type);
619 connection_t *connection_get_by_type_state(int type, int state);
620 connection_t *connection_get_by_type_state_lastwritten(int type, int state);
622 int connection_receiver_bucket_should_increase(connection_t *conn);
624 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
625 #define connection_has_pending_tls_data(conn) \
626 ((conn)->type == CONN_TYPE_OR && \
627 (conn)->state == OR_CONN_STATE_OPEN && \
628 tor_tls_get_pending_bytes(conn->tls))
629 int connection_is_listener(connection_t *conn);
630 int connection_state_is_open(connection_t *conn);
632 int connection_send_destroy(circ_id_t circ_id, connection_t *conn);
634 int connection_process_inbuf(connection_t *conn);
635 int connection_finished_flushing(connection_t *conn);
637 void assert_connection_ok(connection_t *conn, time_t now);
639 /********************************* connection_edge.c ***************************/
641 int connection_edge_process_inbuf(connection_t *conn);
642 int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
644 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
645 void *payload, int payload_len, crypt_path_t *cpath_layer);
647 int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
648 int edge_type, crypt_path_t *layer_hint);
649 int connection_edge_finished_flushing(connection_t *conn);
651 int connection_edge_package_raw_inbuf(connection_t *conn);
653 void connection_exit_connect(connection_t *conn);
654 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
655 void connection_ap_attach_pending(void);
657 extern uint64_t stats_n_data_cells_packaged;
658 extern uint64_t stats_n_data_bytes_packaged;
659 extern uint64_t stats_n_data_cells_received;
660 extern uint64_t stats_n_data_bytes_received;
662 void client_dns_init(void);
663 void client_dns_clean(void);
665 /********************************* connection_or.c ***************************/
667 int connection_or_process_inbuf(connection_t *conn);
668 int connection_or_finished_flushing(connection_t *conn);
670 void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
671 connection_t *connection_or_connect(routerinfo_t *router);
673 int connection_tls_start_handshake(connection_t *conn, int receiving);
674 int connection_tls_continue_handshake(connection_t *conn);
676 void connection_or_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
678 /********************************* cpuworker.c *****************************/
680 void cpu_init(void);
681 int connection_cpu_finished_flushing(connection_t *conn);
682 int connection_cpu_process_inbuf(connection_t *conn);
683 int cpuworker_main(void *data);
684 int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
685 void *task);
687 /********************************* directory.c ***************************/
689 void directory_initiate_command(routerinfo_t *router, int command);
690 int connection_dir_process_inbuf(connection_t *conn);
691 int connection_dir_finished_flushing(connection_t *conn);
693 /********************************* dns.c ***************************/
695 void dns_init(void);
696 int connection_dns_finished_flushing(connection_t *conn);
697 int connection_dns_process_inbuf(connection_t *conn);
698 void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
699 int dns_resolve(connection_t *exitconn);
701 /********************************* main.c ***************************/
703 int connection_add(connection_t *conn);
704 int connection_remove(connection_t *conn);
705 void connection_set_poll_socket(connection_t *conn);
707 void get_connection_array(connection_t ***array, int *n);
709 void connection_watch_events(connection_t *conn, short events);
710 int connection_is_reading(connection_t *conn);
711 void connection_stop_reading(connection_t *conn);
712 void connection_start_reading(connection_t *conn);
713 void connection_stop_writing(connection_t *conn);
714 void connection_start_writing(connection_t *conn);
716 int main(int argc, char *argv[]);
718 /********************************* onion.c ***************************/
720 int decide_circ_id_type(char *local_nick, char *remote_nick);
722 int onion_pending_add(circuit_t *circ);
723 circuit_t *onion_next_task(void);
724 void onion_pending_remove(circuit_t *circ);
726 int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
728 int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
729 routerinfo_t **router_out);
731 int onion_skin_create(crypto_pk_env_t *router_key,
732 crypto_dh_env_t **handshake_state_out,
733 char *onion_skin_out);
735 int onion_skin_server_handshake(char *onion_skin,
736 crypto_pk_env_t *private_key,
737 char *handshake_reply_out,
738 char *key_out,
739 int key_out_len);
741 int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
742 char *handshake_reply,
743 char *key_out,
744 int key_out_len);
746 cpath_build_state_t *onion_new_cpath_build_state(void);
748 /********************************* router.c ***************************/
750 void set_onion_key(crypto_pk_env_t *k);
751 crypto_pk_env_t *get_onion_key(void);
752 void set_identity_key(crypto_pk_env_t *k);
753 crypto_pk_env_t *get_identity_key(void);
754 crypto_pk_env_t *get_link_key(void);
755 int init_keys(void);
757 void router_retry_connections(void);
758 void router_upload_desc_to_dirservers(void);
759 int router_compare_to_my_exit_policy(connection_t *conn);
760 const char *router_get_my_descriptor(void);
761 int router_rebuild_descriptor(void);
762 int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
763 crypto_pk_env_t *ident_key);
765 /********************************* routerlist.c ***************************/
767 routerinfo_t *router_pick_directory_server(void);
768 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
769 routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
770 routerinfo_t *router_get_by_nickname(char *nickname);
771 void router_get_routerlist(routerlist_t **prouterlist);
772 void routerinfo_free(routerinfo_t *router);
773 void router_mark_as_down(char *nickname);
774 int router_set_routerlist_from_file(char *routerfile);
775 int router_get_dir_hash(const char *s, char *digest);
776 int router_get_router_hash(const char *s, char *digest);
777 int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
778 routerinfo_t *router_get_entry_from_string(const char **s);
779 int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
780 int router_supports_exit_address(uint32_t addr, uint16_t port,
781 routerinfo_t *router);
782 int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
783 struct exit_policy_t *policy);
784 int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
785 int router_exit_policy_rejects_all(routerinfo_t *router);
787 /********************************* dirserv.c ***************************/
788 int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
789 int dirserv_parse_fingerprint_file(const char *fname);
790 int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
791 void dirserv_free_fingerprint_list();
792 int dirserv_add_descriptor(const char **desc);
793 int dirserv_init_from_directory_string(const char *dir);
794 void dirserv_free_descriptors();
795 int dirserv_dump_directory_to_string(char *s, int maxlen,
796 crypto_pk_env_t *private_key);
797 void directory_set_dirty();
798 size_t dirserv_get_directory(const char **cp);
801 #endif
804 Local Variables:
805 mode:c
806 indent-tabs-mode:nil
807 c-basic-offset:2
808 End: