1 /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
28 #include "../common/torint.h"
29 #ifdef HAVE_SYS_POLL_H
34 #include "../common/fakepoll.h"
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_WAIT_H
42 #ifdef HAVE_SYS_FCNTL_H
43 #include <sys/fcntl.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
54 #ifdef HAVE_SYS_TIME_H
57 #ifdef HAVE_SYS_STAT_H
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
81 #elif defined(_MSC_VER)
87 #define WIN32_LEAN_AND_MEAN
89 #define snprintf _snprintf
92 #include "../common/crypto.h"
93 #include "../common/tortls.h"
94 #include "../common/log.h"
95 #include "../common/util.h"
97 #define RECOMMENDED_SOFTWARE_VERSIONS "0.0.2pre6,0.0.2pre7"
99 #define MAXCONNECTIONS 1000 /* upper bound on max connections.
100 can be lowered by config file */
102 #define MAX_BUF_SIZE (640*1024)
103 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
105 #define HANDSHAKE_AS_OP 1
106 #define HANDSHAKE_AS_OR 2
108 #define ACI_TYPE_LOWER 0
109 #define ACI_TYPE_HIGHER 1
110 #define ACI_TYPE_BOTH 2
112 #define CONN_TYPE_OR_LISTENER 3
113 #define CONN_TYPE_OR 4
114 #define CONN_TYPE_EXIT 5
115 #define CONN_TYPE_AP_LISTENER 6
116 #define CONN_TYPE_AP 7
117 #define CONN_TYPE_DIR_LISTENER 8
118 #define CONN_TYPE_DIR 9
119 #define CONN_TYPE_DNSWORKER 10
120 #define CONN_TYPE_CPUWORKER 11
122 #define LISTENER_STATE_READY 0
124 #define DNSWORKER_STATE_IDLE 0
125 #define DNSWORKER_STATE_BUSY 1
127 #define CPUWORKER_STATE_IDLE 0
128 #define CPUWORKER_STATE_BUSY_ONION 1
129 #define CPUWORKER_STATE_BUSY_HANDSHAKE 2
131 #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
132 #define CPUWORKER_TASK_HANDSHAKE CPUWORKER_STATE_BUSY_HANDSHAKE
134 /* how to read these states:
135 * foo_CONN_STATE_bar_baz:
136 * "I am acting as a bar, currently in stage baz of talking with a foo."
138 //#define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
139 #define OR_CONN_STATE_OP_SENDING_KEYS 1
140 #define OR_CONN_STATE_CLIENT_CONNECTING 2 /* connecting to this OR */
141 #define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
142 #define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
143 #define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
144 #define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
145 #define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
146 #define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
147 #define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
149 #define EXIT_CONN_STATE_RESOLVING 0 /* waiting for response from dns farm */
150 #define EXIT_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
151 #define EXIT_CONN_STATE_OPEN 2
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 */
157 #define AP_CONN_STATE_SOCKS_WAIT 3
158 #define AP_CONN_STATE_OR_WAIT 4
159 #define AP_CONN_STATE_OPEN 5
161 #define DIR_CONN_STATE_CONNECTING 0
162 #define DIR_CONN_STATE_SENDING_COMMAND 1
163 #define DIR_CONN_STATE_READING 2
164 #define DIR_CONN_STATE_COMMAND_WAIT 3
165 #define DIR_CONN_STATE_WRITING 4
167 #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
168 #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onion */
169 #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
170 #define CIRCUIT_STATE_OPEN 3 /* onion processed, ready to send data along the connection */
171 //#define CIRCUIT_STATE_CLOSE_WAIT1 4 /* sent two "destroy" signals, waiting for acks */
172 //#define CIRCUIT_STATE_CLOSE_WAIT2 5 /* received one ack, waiting for one more
173 // (or if just one was sent, waiting for that one */
174 //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
176 #define RELAY_COMMAND_BEGIN 1
177 #define RELAY_COMMAND_DATA 2
178 #define RELAY_COMMAND_END 3
179 #define RELAY_COMMAND_CONNECTED 4
180 #define RELAY_COMMAND_SENDME 5
181 #define RELAY_COMMAND_EXTEND 6
182 #define RELAY_COMMAND_EXTENDED 7
183 #define RELAY_COMMAND_TRUNCATE 8
184 #define RELAY_COMMAND_TRUNCATED 9
186 #define RELAY_HEADER_SIZE 8
188 #define RELAY_STATE_RESOLVING
190 /* default cipher function */
191 #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
192 /* Used to en/decrypt onion skins */
193 #define ONION_CIPHER DEFAULT_CIPHER
194 /* Used to en/decrypt cells between ORs/OPs. */
195 #define CONNECTION_CIPHER DEFAULT_CIPHER
196 /* Used to en/decrypt RELAY cells */
197 #define CIRCUIT_CIPHER DEFAULT_CIPHER
199 #define CELL_DIRECTION_IN 1
200 #define CELL_DIRECTION_OUT 2
201 #define EDGE_EXIT CONN_TYPE_EXIT
202 #define EDGE_AP CONN_TYPE_AP
203 #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
205 #define CIRCWINDOW_START 1000
206 #define CIRCWINDOW_INCREMENT 100
208 #define STREAMWINDOW_START 500
209 #define STREAMWINDOW_INCREMENT 50
212 #define CELL_PADDING 0
213 #define CELL_CREATE 1
214 #define CELL_CREATED 2
216 #define CELL_DESTROY 4
218 #define CELL_PAYLOAD_SIZE 248
219 #define CELL_NETWORK_SIZE 256
221 /* enumeration of types which option values can take */
222 #define CONFIG_TYPE_STRING 0
223 #define CONFIG_TYPE_CHAR 1
224 #define CONFIG_TYPE_INT 2
225 #define CONFIG_TYPE_LONG 3
226 #define CONFIG_TYPE_DOUBLE 4
227 #define CONFIG_TYPE_BOOL 5
229 #define CONFIG_LINE_MAXLEN 1024
231 /* legal characters in a filename */
232 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
237 struct config_line
*next
;
240 typedef uint16_t aci_t
;
242 /* cell definition */
244 aci_t aci
; /* Anonymous Connection Identifier */
245 unsigned char command
;
246 unsigned char length
; /* of payload if relay cell */
247 uint32_t seq
; /* sequence number */
249 unsigned char payload
[CELL_PAYLOAD_SIZE
];
251 #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
252 #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
253 #define STREAM_ID_SIZE 7
254 #define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
256 #define ZERO_STREAM "\0\0\0\0\0\0\0\0"
258 #define SOCKS4_REQUEST_GRANTED 90
259 #define SOCKS4_REQUEST_REJECT 91
260 #define SOCKS4_REQUEST_IDENT_FAILED 92
261 #define SOCKS4_REQUEST_IDENT_CONFLICT 93
263 /* structure of a socks client operation */
265 unsigned char version
; /* socks version number */
266 unsigned char command
; /* command code */
267 unsigned char destport
[2]; /* destination port, network order */
268 unsigned char destip
[4]; /* destination address */
269 /* userid follows, terminated by a NULL */
270 /* dest host follows, terminated by a NULL */
273 struct connection_t
{
275 /* Used by all types: */
279 uint8_t wants_to_read
;
280 int s
; /* our socket */
282 int marked_for_close
;
287 int inbuf_reached_eof
;
288 long timestamp_lastread
;
291 int outbuflen
; /* how many bytes are allocated for the outbuf? */
292 int outbuf_flushlen
; /* how much data should we try to flush from the outbuf? */
293 int outbuf_datalen
; /* how much data is there total on the outbuf? */
294 long timestamp_lastwritten
;
296 long timestamp_created
;
298 /* used by OR and OP: */
300 uint32_t bandwidth
; /* connection bandwidth */
301 int receiver_bucket
; /* when this hits 0, stop receiving. Every second we
302 * add 'bandwidth' to this, capping it at 10*bandwidth.
304 struct timeval send_timeval
; /* for determining when to send the next cell */
306 /* link encryption */
307 crypto_cipher_env_t
*f_crypto
;
308 crypto_cipher_env_t
*b_crypto
;
310 // struct timeval lastsend; /* time of last transmission to the client */
311 // struct timeval interval; /* transmission interval */
313 uint32_t addr
; /* these two uniquely identify a router. Both in host order. */
316 /* used by exit and ap: */
317 char stream_id
[STREAM_ID_SIZE
];
318 struct connection_t
*next_stream
;
319 struct crypt_path_t
*cpath_layer
; /* a pointer to which node in the circ this conn exits at */
329 /* Used by exit and ap: */
331 uint16_t dest_port
; /* host order */
333 /* Used by everyone */
334 char *address
; /* strdup into this, because free_connection frees it */
335 /* Used for cell connections */
336 crypto_pk_env_t
*pkey
; /* public RSA key for the other side */
338 /* Used while negotiating OR/OR connections */
341 /* Used by worker connections */
342 int num_processed
; /* statistics kept by dns worker */
343 struct circuit_t
*circ
; /* by cpu worker to know who he's working for */
346 typedef struct connection_t connection_t
;
348 #define EXIT_POLICY_ACCEPT 1
349 #define EXIT_POLICY_REJECT 2
351 struct exit_policy_t
{
357 struct exit_policy_t
*next
;
360 /* config stuff we know about the other ORs in the network */
364 uint32_t addr
; /* all host order */
369 crypto_pk_env_t
*pkey
; /* public RSA key */
370 crypto_pk_env_t
*signing_pkey
; /* May be null */
374 struct exit_policy_t
*exit_policy
;
377 #define MAX_ROUTERS_IN_DIR 1024
379 routerinfo_t
**routers
;
381 char *software_versions
;
384 struct crypt_path_t
{
386 /* crypto environments */
387 crypto_cipher_env_t
*f_crypto
;
388 crypto_cipher_env_t
*b_crypto
;
390 crypto_dh_env_t
*handshake_state
;
396 #define CPATH_STATE_CLOSED 0
397 #define CPATH_STATE_AWAITING_KEYS 1
398 #define CPATH_STATE_OPEN 2
399 struct crypt_path_t
*next
;
400 struct crypt_path_t
*prev
; /* doubly linked list */
406 #define DH_KEY_LEN CRYPTO_DH_SIZE
407 #define DH_ONIONSKIN_LEN DH_KEY_LEN+16
409 typedef struct crypt_path_t crypt_path_t
;
411 /* struct for a path (circuit) through the network */
415 connection_t
*p_conn
;
416 connection_t
*n_conn
; /* for the OR conn, if there is one */
417 connection_t
*p_streams
;
418 connection_t
*n_streams
;
422 aci_t p_aci
; /* connection identifiers */
425 crypto_cipher_env_t
*p_crypto
; /* used only for intermediate hops */
426 crypto_cipher_env_t
*n_crypto
;
430 char onionskin
[DH_ONIONSKIN_LEN
]; /* for storage while onionskin pending */
431 long timestamp_created
;
432 char dirty
; /* whether this circuit has been used yet */
436 // unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
437 // uint32_t onionlen; /* total onion length */
438 // uint32_t recvlen; /* length of the onion so far */
443 typedef struct circuit_t circuit_t
;
445 struct onion_queue_t
{
447 struct onion_queue_t
*next
;
453 char *SigningPrivateKeyFile
;
454 char *PrivateKeyFile
;
465 int DirRebuildPeriod
;
468 int MaxOnionsPending
;
469 int NewCircuitPeriod
;
476 /* all the function prototypes go here */
478 /********************************* buffers.c ***************************/
480 int buf_new(char **buf
, int *buflen
, int *buf_datalen
);
482 void buf_free(char *buf
);
484 int read_to_buf(int s
, int at_most
, char **buf
, int *buflen
, int *buf_datalen
, int *reached_eof
);
485 /* grab from s, put onto buf, return how many bytes read */
486 int read_to_buf_tls(tor_tls
*tls
, int at_most
, char **buf
, int *buflen
, int *buf_datalen
);
487 /* grab from s, put onto buf, return how many bytes read or a TLS
488 * status (same status codes as tor_tls_read) */
490 int flush_buf(int s
, char **buf
, int *buflen
, int *buf_flushlen
, int *buf_datalen
);
491 /* push from buf onto s
492 * then memmove to front of buf
493 * return -1 or how many bytes remain on the buf */
494 int flush_buf_tls(tor_tls
*tls
, char **buf
, int *buflen
, int *buf_flushlen
, int *buf_datalen
);
495 /* As flush_buf, but returns number of bytes written or TLS status
496 * (same status codes as tor_tls_write) */
498 int write_to_buf(char *string
, int string_len
,
499 char **buf
, int *buflen
, int *buf_datalen
);
500 /* append string to buf (growing as needed, return -1 if "too big")
501 * return total number of bytes on the buf
504 int fetch_from_buf(char *string
, int string_len
,
505 char **buf
, int *buflen
, int *buf_datalen
);
506 /* if there is string_len bytes in buf, write them onto string,
507 * then memmove buf back (that is, remove them from buf)
510 int find_on_inbuf(char *string
, int string_len
,
511 char *buf
, int buf_datalen
);
512 /* find first instance of needle 'string' on haystack 'buf'. return how
513 * many bytes from the beginning of buf to the end of string.
514 * If it's not there, return -1.
517 /********************************* cell.c ***************************/
519 int pack_create(uint16_t aci
, unsigned char *onion
, uint32_t onionlen
, unsigned char **cellbuf
, unsigned int *cellbuflen
);
521 /********************************* circuit.c ***************************/
523 void circuit_add(circuit_t
*circ
);
524 void circuit_remove(circuit_t
*circ
);
526 circuit_t
*circuit_new(aci_t p_aci
, connection_t
*p_conn
);
529 aci_t
get_unique_aci_by_addr_port(uint32_t addr
, uint16_t port
, int aci_type
);
531 circuit_t
*circuit_get_by_aci_conn(aci_t aci
, connection_t
*conn
);
532 circuit_t
*circuit_get_by_conn(connection_t
*conn
);
533 circuit_t
*circuit_get_newest_ap(void);
534 circuit_t
*circuit_enumerate_by_naddr_nport(circuit_t
*start
, uint32_t naddr
, uint16_t nport
);
536 int circuit_deliver_relay_cell(cell_t
*cell
, circuit_t
*circ
,
537 int cell_direction
, crypt_path_t
*layer_hint
);
538 int relay_crypt(circuit_t
*circ
, char *in
, int inlen
, char cell_direction
,
539 crypt_path_t
**layer_hint
, char *recognized
, connection_t
**conn
);
540 int relay_check_recognized(circuit_t
*circ
, int cell_direction
, char *stream
, connection_t
**conn
);
542 void circuit_resume_edge_reading(circuit_t
*circ
, int edge_type
, crypt_path_t
*layer_hint
);
543 int circuit_consider_stop_edge_reading(circuit_t
*circ
, int edge_type
, crypt_path_t
*layer_hint
);
544 int circuit_consider_sending_sendme(circuit_t
*circ
, int edge_type
, crypt_path_t
*layer_hint
);
546 void circuit_free(circuit_t
*circ
);
547 void circuit_free_cpath(crypt_path_t
*cpath
);
548 void circuit_free_cpath_node(crypt_path_t
*victim
);
550 void circuit_close(circuit_t
*circ
);
552 void circuit_about_to_close_connection(connection_t
*conn
);
553 /* flush and send destroys for all circuits using conn */
555 void circuit_dump_by_conn(connection_t
*conn
);
557 void circuit_expire_unused_circuits(void);
558 void circuit_launch_new(int failure_status
);
559 int circuit_establish_circuit(void);
560 void circuit_n_conn_open(connection_t
*or_conn
);
561 int circuit_send_next_onion_skin(circuit_t
*circ
);
562 int circuit_extend(cell_t
*cell
, circuit_t
*circ
);
563 int circuit_finish_handshake(circuit_t
*circ
, char *reply
);
564 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
);
566 /********************************* command.c ***************************/
568 void command_process_cell(cell_t
*cell
, connection_t
*conn
);
570 void command_process_create_cell(cell_t
*cell
, connection_t
*conn
);
571 void command_process_created_cell(cell_t
*cell
, connection_t
*conn
);
572 void command_process_sendme_cell(cell_t
*cell
, connection_t
*conn
);
573 void command_process_relay_cell(cell_t
*cell
, connection_t
*conn
);
574 void command_process_destroy_cell(cell_t
*cell
, connection_t
*conn
);
575 void command_process_connected_cell(cell_t
*cell
, connection_t
*conn
);
577 /********************************* config.c ***************************/
579 /* open configuration file for reading */
580 FILE *config_open(const unsigned char *filename
);
582 /* close configuration file */
583 int config_close(FILE *f
);
585 struct config_line
*config_get_commandlines(int argc
, char **argv
);
587 /* parse the config file and strdup into key/value strings. Return list.
588 * * * Warn and ignore mangled lines. */
589 struct config_line
*config_get_lines(FILE *f
);
591 void config_free_lines(struct config_line
*front
);
593 int config_compare(struct config_line
*c
, char *key
, int type
, void *arg
);
595 void config_assign(or_options_t
*options
, struct config_line
*list
);
597 /* return 0 if success, <0 if failure. */
598 int getconfig(int argc
, char **argv
, or_options_t
*options
);
600 /********************************* connection.c ***************************/
602 int tv_cmp(struct timeval
*a
, struct timeval
*b
);
604 connection_t
*connection_new(int type
);
606 void connection_free(connection_t
*conn
);
608 int connection_create_listener(struct sockaddr_in
*bindaddr
, int type
);
610 int connection_handle_listener_read(connection_t
*conn
, int new_type
, int new_state
);
612 /* start all connections that should be up but aren't */
613 int retry_all_connections(uint16_t or_listenport
, uint16_t ap_listenport
, uint16_t dir_listenport
);
615 int connection_read_to_buf(connection_t
*conn
);
617 int connection_fetch_from_buf(char *string
, int len
, connection_t
*conn
);
619 int connection_outbuf_too_full(connection_t
*conn
);
620 int connection_find_on_inbuf(char *string
, int len
, connection_t
*conn
);
621 int connection_wants_to_flush(connection_t
*conn
);
622 int connection_flush_buf(connection_t
*conn
);
624 int connection_write_to_buf(char *string
, int len
, connection_t
*conn
);
625 void connection_send_cell(connection_t
*conn
);
627 int connection_receiver_bucket_should_increase(connection_t
*conn
);
629 void connection_increment_send_timeval(connection_t
*conn
);
630 void connection_init_timeval(connection_t
*conn
);
632 #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
633 int connection_is_listener(connection_t
*conn
);
634 int connection_state_is_open(connection_t
*conn
);
636 int connection_send_destroy(aci_t aci
, connection_t
*conn
);
637 int connection_send_connected(aci_t aci
, connection_t
*conn
);
638 int connection_encrypt_cell(char *cellp
, connection_t
*conn
);
639 int connection_write_cell_to_buf(const cell_t
*cellp
, connection_t
*conn
);
641 int connection_process_inbuf(connection_t
*conn
);
642 int connection_package_raw_inbuf(connection_t
*conn
);
643 int connection_process_cell_from_inbuf(connection_t
*conn
);
645 int connection_consider_sending_sendme(connection_t
*conn
, int edge_type
);
646 int connection_finished_flushing(connection_t
*conn
);
648 void cell_pack(char *dest
, const cell_t
*src
);
649 void cell_unpack(cell_t
*dest
, const char *src
);
651 /********************************* connection_ap.c ****************************/
653 int ap_handshake_process_socks(connection_t
*conn
);
655 int ap_handshake_send_begin(connection_t
*ap_conn
, circuit_t
*circ
);
657 int ap_handshake_socks_reply(connection_t
*conn
, char result
);
659 int connection_ap_create_listener(struct sockaddr_in
*bindaddr
);
661 int connection_ap_handle_listener_read(connection_t
*conn
);
663 /********************************* connection_edge.c ***************************/
665 int connection_edge_process_inbuf(connection_t
*conn
);
666 int connection_edge_send_command(connection_t
*fromconn
, circuit_t
*circ
, int relay_command
);
667 int connection_edge_process_relay_cell(cell_t
*cell
, circuit_t
*circ
, connection_t
*conn
, int edge_type
, crypt_path_t
*layer_hint
);
668 int connection_edge_finished_flushing(connection_t
*conn
);
670 /********************************* connection_exit.c ***************************/
672 int connection_exit_send_connected(connection_t
*conn
);
673 int connection_exit_begin_conn(cell_t
*cell
, circuit_t
*circ
);
675 int connection_exit_connect(connection_t
*conn
);
677 /********************************* connection_op.c ***************************/
679 int op_handshake_process_keys(connection_t
*conn
);
681 int connection_op_process_inbuf(connection_t
*conn
);
683 int connection_op_finished_flushing(connection_t
*conn
);
685 int connection_op_create_listener(struct sockaddr_in
*bindaddr
);
687 int connection_op_handle_listener_read(connection_t
*conn
);
689 /********************************* connection_or.c ***************************/
691 int connection_or_process_inbuf(connection_t
*conn
);
692 int connection_or_finished_flushing(connection_t
*conn
);
694 connection_t
*connection_or_connect(routerinfo_t
*router
);
696 int connection_or_create_listener(struct sockaddr_in
*bindaddr
);
697 int connection_or_handle_listener_read(connection_t
*conn
);
699 /********************************* cpuworker.c *****************************/
702 int connection_cpu_finished_flushing(connection_t
*conn
);
703 int connection_cpu_process_inbuf(connection_t
*conn
);
704 int cpuworker_main(void *data
);
705 int assign_to_cpuworker(connection_t
*cpuworker
, unsigned char question_type
,
708 /********************************* directory.c ***************************/
710 void directory_initiate_fetch(routerinfo_t
*router
);
711 int directory_send_command(connection_t
*conn
);
712 void directory_set_dirty(void);
713 void directory_rebuild(void);
714 int connection_dir_process_inbuf(connection_t
*conn
);
715 int directory_handle_command(connection_t
*conn
);
716 int directory_handle_reading(connection_t
*conn
);
717 int connection_dir_finished_flushing(connection_t
*conn
);
718 int connection_dir_create_listener(struct sockaddr_in
*bindaddr
);
719 int connection_dir_handle_listener_read(connection_t
*conn
);
721 /********************************* dns.c ***************************/
724 int connection_dns_finished_flushing(connection_t
*conn
);
725 int connection_dns_process_inbuf(connection_t
*conn
);
726 void dns_cancel_pending_resolve(char *question
, connection_t
*onlyconn
);
727 int dns_resolve(connection_t
*exitconn
);
729 /********************************* main.c ***************************/
731 void set_privatekey(crypto_pk_env_t
*k
);
732 crypto_pk_env_t
*get_privatekey(void);
733 void set_signing_privatekey(crypto_pk_env_t
*k
);
734 crypto_pk_env_t
*get_signing_privatekey(void);
735 int connection_add(connection_t
*conn
);
736 int connection_remove(connection_t
*conn
);
737 void connection_set_poll_socket(connection_t
*conn
);
739 connection_t
*connection_twin_get_by_addr_port(uint32_t addr
, uint16_t port
);
740 connection_t
*connection_exact_get_by_addr_port(uint32_t addr
, uint16_t port
);
742 connection_t
*connection_get_by_type(int type
);
743 connection_t
*connection_get_by_type_state(int type
, int state
);
744 connection_t
*connection_get_by_type_state_lastwritten(int type
, int state
);
746 void connection_watch_events(connection_t
*conn
, short events
);
747 void connection_stop_reading(connection_t
*conn
);
748 void connection_start_reading(connection_t
*conn
);
749 void connection_stop_writing(connection_t
*conn
);
750 void connection_start_writing(connection_t
*conn
);
752 int dump_signed_directory_to_string(char *s
, int maxlen
,
753 crypto_pk_env_t
*private_key
);
754 /* Exported for debugging */
755 int dump_signed_directory_to_string_impl(char *s
, int maxlen
,
757 crypto_pk_env_t
*private_key
);
759 int main(int argc
, char *argv
[]);
761 /********************************* onion.c ***************************/
763 int decide_aci_type(uint32_t local_addr
, uint16_t local_port
,
764 uint32_t remote_addr
, uint16_t remote_port
);
766 int onion_pending_add(circuit_t
*circ
);
767 circuit_t
*onion_next_task(void);
768 void onion_pending_remove(circuit_t
*circ
);
770 int onionskin_process(circuit_t
*circ
, unsigned char *payload
, unsigned char *keys
);
772 /* uses a weighted coin with weight cw to choose a route length */
773 int chooselen(double cw
);
775 /* returns an array of pointers to routent that define a new route through the OR network
776 * int cw is the coin weight to use when choosing the route
777 * order of routers is from last to first
779 unsigned int *new_route(double cw
, routerinfo_t
**rarray
, int rarray_len
, int *routelen
);
781 crypt_path_t
*onion_generate_cpath(routerinfo_t
**firsthop
);
783 int onion_skin_create(crypto_pk_env_t
*router_key
,
784 crypto_dh_env_t
**handshake_state_out
,
785 char *onion_skin_out
); /* Must be DH_ONIONSKIN_LEN bytes long */
787 int onion_skin_server_handshake(char *onion_skin
, /* DH_ONIONSKIN_LEN bytes long */
788 crypto_pk_env_t
*private_key
,
789 char *handshake_reply_out
, /* DH_KEY_LEN bytes long */
793 int onion_skin_client_handshake(crypto_dh_env_t
*handshake_state
,
794 char *handshake_reply
,/* Must be DH_KEY_LEN bytes long*/
798 /********************************* routers.c ***************************/
800 int learn_my_address(struct sockaddr_in
*me
);
801 void router_retry_connections(void);
802 routerinfo_t
*router_pick_directory_server(void);
803 routerinfo_t
*router_get_by_addr_port(uint32_t addr
, uint16_t port
);
804 void router_get_directory(directory_t
**pdirectory
);
805 int router_is_me(uint32_t addr
, uint16_t port
);
806 void router_forget_router(uint32_t addr
, uint16_t port
);
807 int router_get_list_from_file(char *routerfile
);
808 int router_resolve(routerinfo_t
*router
);
809 int router_resolve_directory(directory_t
*dir
);
811 /* Reads a list of known routers, unsigned. */
812 int router_get_list_from_string(char *s
);
813 /* Exported for debugging */
814 int router_get_list_from_string_impl(char *s
, directory_t
**dest
);
815 /* Reads a signed directory. */
816 int router_get_dir_from_string(char *s
, crypto_pk_env_t
*pkey
);
817 /* Exported or debugging */
818 int router_get_dir_from_string_impl(char *s
, directory_t
**dest
,
819 crypto_pk_env_t
*pkey
);
820 routerinfo_t
*router_get_entry_from_string(char **s
);
821 int router_compare_to_exit_policy(connection_t
*conn
);
822 void routerinfo_free(routerinfo_t
*router
);