or.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __OR_H
  5. #define __OR_H
  6. #include "orconfig.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <limits.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <netdb.h>
  14. #include <ctype.h>
  15. #ifdef HAVE_STDINT_H
  16. #include <stdint.h>
  17. #endif
  18. #ifdef HAVE_SYS_POLL_H
  19. #include <sys/poll.h>
  20. #elif HAVE_POLL_H
  21. #include <poll.h>
  22. #else
  23. #include "../common/fakepoll.h"
  24. #endif
  25. #include <sys/types.h>
  26. #include <sys/fcntl.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/socket.h>
  29. #include <sys/time.h>
  30. #include <sys/stat.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <time.h>
  36. #include "../common/crypto.h"
  37. #include "../common/log.h"
  38. #include "../common/util.h"
  39. #define MAXCONNECTIONS 1000 /* upper bound on max connections.
  40. can be lowered by config file */
  41. #define MAX_BUF_SIZE (640*1024)
  42. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  43. #define HANDSHAKE_AS_OP 1
  44. #define HANDSHAKE_AS_OR 2
  45. #define ACI_TYPE_LOWER 0
  46. #define ACI_TYPE_HIGHER 1
  47. #define ACI_TYPE_BOTH 2
  48. #define CONN_TYPE_OR_LISTENER 3
  49. #define CONN_TYPE_OR 4
  50. #define CONN_TYPE_EXIT 5
  51. #define CONN_TYPE_AP_LISTENER 6
  52. #define CONN_TYPE_AP 7
  53. #define CONN_TYPE_DIR_LISTENER 8
  54. #define CONN_TYPE_DIR 9
  55. #define CONN_TYPE_DNSWORKER 10
  56. #define LISTENER_STATE_READY 0
  57. #define DNSWORKER_STATE_IDLE 0
  58. #define DNSWORKER_STATE_BUSY 1
  59. /* how to read these states:
  60. * foo_CONN_STATE_bar_baz:
  61. * "I am acting as a bar, currently in stage baz of talking with a foo."
  62. */
  63. //#define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
  64. #define OR_CONN_STATE_OP_SENDING_KEYS 1
  65. #define OR_CONN_STATE_CLIENT_CONNECTING 2 /* connecting to this OR */
  66. #define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
  67. #define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
  68. #define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
  69. #define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
  70. #define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
  71. #define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
  72. #define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
  73. #define EXIT_CONN_STATE_RESOLVING 0 /* waiting for response from dns farm */
  74. #define EXIT_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
  75. #define EXIT_CONN_STATE_OPEN 2
  76. #if 0
  77. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  78. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  79. #endif
  80. #define AP_CONN_STATE_SOCKS_WAIT 3
  81. #define AP_CONN_STATE_OR_WAIT 4
  82. #define AP_CONN_STATE_OPEN 5
  83. #define DIR_CONN_STATE_CONNECTING 0
  84. #define DIR_CONN_STATE_SENDING_COMMAND 1
  85. #define DIR_CONN_STATE_READING 2
  86. #define DIR_CONN_STATE_COMMAND_WAIT 3
  87. #define DIR_CONN_STATE_WRITING 4
  88. #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
  89. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onion */
  90. #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
  91. #define CIRCUIT_STATE_OPEN 3 /* onion processed, ready to send data along the connection */
  92. //#define CIRCUIT_STATE_CLOSE_WAIT1 4 /* sent two "destroy" signals, waiting for acks */
  93. //#define CIRCUIT_STATE_CLOSE_WAIT2 5 /* received one ack, waiting for one more
  94. // (or if just one was sent, waiting for that one */
  95. //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
  96. #define RELAY_COMMAND_BEGIN 1
  97. #define RELAY_COMMAND_DATA 2
  98. #define RELAY_COMMAND_END 3
  99. #define RELAY_COMMAND_CONNECTED 4
  100. #define RELAY_COMMAND_SENDME 5
  101. #define RELAY_COMMAND_EXTEND 6
  102. #define RELAY_COMMAND_EXTENDED 7
  103. #define RELAY_COMMAND_TRUNCATE 8
  104. #define RELAY_COMMAND_TRUNCATED 9
  105. #define RELAY_HEADER_SIZE 8
  106. #define RELAY_STATE_RESOLVING
  107. /* default cipher function */
  108. #define DEFAULT_CIPHER CRYPTO_CIPHER_3DES
  109. #define CELL_DIRECTION_IN 1
  110. #define CELL_DIRECTION_OUT 2
  111. #define EDGE_EXIT CONN_TYPE_EXIT
  112. #define EDGE_AP CONN_TYPE_AP
  113. #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
  114. #define CIRCWINDOW_START 1000
  115. #define CIRCWINDOW_INCREMENT 100
  116. #define STREAMWINDOW_START 500
  117. #define STREAMWINDOW_INCREMENT 50
  118. /* cell commands */
  119. #define CELL_PADDING 0
  120. #define CELL_CREATE 1
  121. #define CELL_CREATED 2
  122. #define CELL_RELAY 3
  123. #define CELL_DESTROY 4
  124. #define CELL_PAYLOAD_SIZE 248
  125. #define CELL_NETWORK_SIZE 256
  126. /* enumeration of types which option values can take */
  127. #define CONFIG_TYPE_STRING 0
  128. #define CONFIG_TYPE_CHAR 1
  129. #define CONFIG_TYPE_INT 2
  130. #define CONFIG_TYPE_LONG 3
  131. #define CONFIG_TYPE_DOUBLE 4
  132. #define CONFIG_TYPE_BOOL 5
  133. #define CONFIG_LINE_MAXLEN 1024
  134. /* legal characters in a filename */
  135. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  136. struct config_line {
  137. char *key;
  138. char *value;
  139. struct config_line *next;
  140. };
  141. typedef uint16_t aci_t;
  142. /* cell definition */
  143. typedef struct {
  144. aci_t aci; /* Anonymous Connection Identifier */
  145. unsigned char command;
  146. unsigned char length; /* of payload if relay cell */
  147. uint32_t seq; /* sequence number */
  148. unsigned char payload[CELL_PAYLOAD_SIZE];
  149. } cell_t;
  150. #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
  151. #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
  152. #define STREAM_ID_SIZE 7
  153. #define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
  154. #define ZERO_STREAM "\0\0\0\0\0\0\0\0"
  155. #define SOCKS4_REQUEST_GRANTED 90
  156. #define SOCKS4_REQUEST_REJECT 91
  157. #define SOCKS4_REQUEST_IDENT_FAILED 92
  158. #define SOCKS4_REQUEST_IDENT_CONFLICT 93
  159. /* structure of a socks client operation */
  160. typedef struct {
  161. unsigned char version; /* socks version number */
  162. unsigned char command; /* command code */
  163. unsigned char destport[2]; /* destination port, network order */
  164. unsigned char destip[4]; /* destination address */
  165. /* userid follows, terminated by a NULL */
  166. /* dest host follows, terminated by a NULL */
  167. } socks4_t;
  168. struct connection_t {
  169. /* Used by all types: */
  170. uint8_t type;
  171. int state;
  172. uint8_t wants_to_read;
  173. int s; /* our socket */
  174. int poll_index;
  175. int marked_for_close;
  176. char *inbuf;
  177. int inbuflen;
  178. int inbuf_datalen;
  179. int inbuf_reached_eof;
  180. long timestamp_lastread;
  181. char *outbuf;
  182. int outbuflen; /* how many bytes are allocated for the outbuf? */
  183. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  184. int outbuf_datalen; /* how much data is there total on the outbuf? */
  185. long timestamp_lastwritten;
  186. long timestamp_created;
  187. /* used by OR and OP: */
  188. uint32_t bandwidth; /* connection bandwidth */
  189. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  190. * add 'bandwidth' to this, capping it at 10*bandwidth.
  191. */
  192. struct timeval send_timeval; /* for determining when to send the next cell */
  193. /* link encryption */
  194. crypto_cipher_env_t *f_crypto;
  195. crypto_cipher_env_t *b_crypto;
  196. // struct timeval lastsend; /* time of last transmission to the client */
  197. // struct timeval interval; /* transmission interval */
  198. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  199. uint16_t port;
  200. /* used by exit and ap: */
  201. char stream_id[STREAM_ID_SIZE];
  202. struct connection_t *next_stream;
  203. struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
  204. int package_window;
  205. int deliver_window;
  206. int done_sending;
  207. int done_receiving;
  208. /* Used by ap: */
  209. char socks_version;
  210. char read_username;
  211. /* Used by exit and ap: */
  212. char *dest_addr;
  213. uint16_t dest_port; /* host order */
  214. /* Used by everyone */
  215. char *address; /* strdup into this, because free_connection frees it */
  216. /* Used for cell connections */
  217. crypto_pk_env_t *pkey; /* public RSA key for the other side */
  218. /* Used while negotiating OR/OR connections */
  219. char nonce[8];
  220. /* Used by worker connections */
  221. int num_processed;
  222. };
  223. typedef struct connection_t connection_t;
  224. #define EXIT_POLICY_ACCEPT 1
  225. #define EXIT_POLICY_REJECT 2
  226. struct exit_policy_t {
  227. char policy_type;
  228. char *string;
  229. char *address;
  230. char *port;
  231. struct exit_policy_t *next;
  232. };
  233. /* config stuff we know about the other ORs in the network */
  234. typedef struct {
  235. char *address;
  236. uint32_t addr; /* all host order */
  237. uint16_t or_port;
  238. uint16_t op_port;
  239. uint16_t ap_port;
  240. uint16_t dir_port;
  241. crypto_pk_env_t *pkey; /* public RSA key */
  242. crypto_pk_env_t *signing_pkey; /* May be null */
  243. /* link info */
  244. uint32_t bandwidth;
  245. struct exit_policy_t *exit_policy;
  246. } routerinfo_t;
  247. #define MAX_ROUTERS_IN_DIR 1024
  248. typedef struct {
  249. routerinfo_t **routers;
  250. int n_routers;
  251. char *software_versions;
  252. } directory_t;
  253. struct crypt_path_t {
  254. /* crypto environments */
  255. crypto_cipher_env_t *f_crypto;
  256. crypto_cipher_env_t *b_crypto;
  257. crypto_dh_env_t *handshake_state;
  258. uint32_t addr;
  259. uint16_t port;
  260. char state;
  261. #define CPATH_STATE_CLOSED 0
  262. #define CPATH_STATE_AWAITING_KEYS 1
  263. #define CPATH_STATE_OPEN 2
  264. struct crypt_path_t *next;
  265. struct crypt_path_t *prev; /* doubly linked list */
  266. int package_window;
  267. int deliver_window;
  268. };
  269. #define DH_KEY_LEN CRYPTO_DH_SIZE
  270. #define DH_ONIONSKIN_LEN DH_KEY_LEN+16
  271. typedef struct crypt_path_t crypt_path_t;
  272. /* struct for a path (circuit) through the network */
  273. typedef struct {
  274. uint32_t n_addr;
  275. uint16_t n_port;
  276. connection_t *p_conn;
  277. connection_t *n_conn; /* for the OR conn, if there is one */
  278. connection_t *p_streams;
  279. connection_t *n_streams;
  280. int package_window;
  281. int deliver_window;
  282. aci_t p_aci; /* connection identifiers */
  283. aci_t n_aci;
  284. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  285. crypto_cipher_env_t *n_crypto;
  286. crypt_path_t *cpath;
  287. char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
  288. long timestamp_created;
  289. char dirty; /* whether this circuit has been used yet */
  290. int state;
  291. // unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  292. // uint32_t onionlen; /* total onion length */
  293. // uint32_t recvlen; /* length of the onion so far */
  294. void *next;
  295. } circuit_t;
  296. struct onion_queue_t {
  297. circuit_t *circ;
  298. struct onion_queue_t *next;
  299. };
  300. typedef struct {
  301. char *LogLevel;
  302. char *RouterFile;
  303. char *SigningPrivateKeyFile;
  304. char *PrivateKeyFile;
  305. double CoinWeight;
  306. int Daemon;
  307. int ORPort;
  308. int APPort;
  309. int DirPort;
  310. int MaxConn;
  311. int OnionRouter;
  312. int TrafficShaping;
  313. int LinkPadding;
  314. int DirRebuildPeriod;
  315. int DirFetchPeriod;
  316. int KeepalivePeriod;
  317. int MaxOnionsPending;
  318. int NewCircuitPeriod;
  319. int TotalBandwidth;
  320. int Role;
  321. int loglevel;
  322. } or_options_t;
  323. /* all the function prototypes go here */
  324. /********************************* buffers.c ***************************/
  325. int buf_new(char **buf, int *buflen, int *buf_datalen);
  326. void buf_free(char *buf);
  327. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof);
  328. /* grab from s, put onto buf, return how many bytes read */
  329. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
  330. /* push from buf onto s
  331. * then memmove to front of buf
  332. * return -1 or how many bytes remain on the buf */
  333. int write_to_buf(char *string, int string_len,
  334. char **buf, int *buflen, int *buf_datalen);
  335. /* append string to buf (growing as needed, return -1 if "too big")
  336. * return total number of bytes on the buf
  337. */
  338. int fetch_from_buf(char *string, int string_len,
  339. char **buf, int *buflen, int *buf_datalen);
  340. /* if there is string_len bytes in buf, write them onto string,
  341. * then memmove buf back (that is, remove them from buf)
  342. */
  343. int find_on_inbuf(char *string, int string_len,
  344. char *buf, int buf_datalen);
  345. /* find first instance of needle 'string' on haystack 'buf'. return how
  346. * many bytes from the beginning of buf to the end of string.
  347. * If it's not there, return -1.
  348. */
  349. /********************************* cell.c ***************************/
  350. int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
  351. /********************************* circuit.c ***************************/
  352. void circuit_add(circuit_t *circ);
  353. void circuit_remove(circuit_t *circ);
  354. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  355. /* internal */
  356. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  357. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  358. circuit_t *circuit_get_by_conn(connection_t *conn);
  359. circuit_t *circuit_get_newest_ap(void);
  360. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
  361. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  362. int cell_direction, crypt_path_t *layer_hint);
  363. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  364. crypt_path_t **layer_hint, char *recognized, connection_t **conn);
  365. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
  366. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  367. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  368. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  369. void circuit_free(circuit_t *circ);
  370. void circuit_free_cpath(crypt_path_t *cpath);
  371. void circuit_free_cpath_node(crypt_path_t *victim);
  372. void circuit_close(circuit_t *circ);
  373. void circuit_about_to_close_connection(connection_t *conn);
  374. /* flush and send destroys for all circuits using conn */
  375. void circuit_dump_by_conn(connection_t *conn);
  376. void circuit_expire_unused_circuits(void);
  377. void circuit_launch_new(int failure_status);
  378. int circuit_establish_circuit(void);
  379. void circuit_n_conn_open(connection_t *or_conn);
  380. int circuit_send_next_onion_skin(circuit_t *circ);
  381. int circuit_extend(cell_t *cell, circuit_t *circ);
  382. int circuit_finish_handshake(circuit_t *circ, char *reply);
  383. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  384. /********************************* command.c ***************************/
  385. void command_process_cell(cell_t *cell, connection_t *conn);
  386. void command_process_create_cell(cell_t *cell, connection_t *conn);
  387. void command_process_created_cell(cell_t *cell, connection_t *conn);
  388. void command_process_sendme_cell(cell_t *cell, connection_t *conn);
  389. void command_process_relay_cell(cell_t *cell, connection_t *conn);
  390. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  391. void command_process_connected_cell(cell_t *cell, connection_t *conn);
  392. /********************************* config.c ***************************/
  393. const char *basename(const char *filename);
  394. /* open configuration file for reading */
  395. FILE *config_open(const unsigned char *filename);
  396. /* close configuration file */
  397. int config_close(FILE *f);
  398. struct config_line *config_get_commandlines(int argc, char **argv);
  399. /* parse the config file and strdup into key/value strings. Return list.
  400. * * * Warn and ignore mangled lines. */
  401. struct config_line *config_get_lines(FILE *f);
  402. void config_free_lines(struct config_line *front);
  403. int config_compare(struct config_line *c, char *key, int type, void *arg);
  404. void config_assign(or_options_t *options, struct config_line *list);
  405. /* return 0 if success, <0 if failure. */
  406. int getconfig(int argc, char **argv, or_options_t *options);
  407. /********************************* connection.c ***************************/
  408. int tv_cmp(struct timeval *a, struct timeval *b);
  409. connection_t *connection_new(int type);
  410. void connection_free(connection_t *conn);
  411. int connection_create_listener(struct sockaddr_in *bindaddr, int type);
  412. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  413. /* start all connections that should be up but aren't */
  414. int retry_all_connections(uint16_t or_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
  415. int connection_read_to_buf(connection_t *conn);
  416. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  417. int connection_outbuf_too_full(connection_t *conn);
  418. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  419. int connection_wants_to_flush(connection_t *conn);
  420. int connection_flush_buf(connection_t *conn);
  421. int connection_write_to_buf(char *string, int len, connection_t *conn);
  422. void connection_send_cell(connection_t *conn);
  423. int connection_receiver_bucket_should_increase(connection_t *conn);
  424. void connection_increment_send_timeval(connection_t *conn);
  425. void connection_init_timeval(connection_t *conn);
  426. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  427. int connection_is_listener(connection_t *conn);
  428. int connection_state_is_open(connection_t *conn);
  429. int connection_send_destroy(aci_t aci, connection_t *conn);
  430. int connection_send_connected(aci_t aci, connection_t *conn);
  431. int connection_encrypt_cell(char *cellp, connection_t *conn);
  432. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
  433. int connection_process_inbuf(connection_t *conn);
  434. int connection_package_raw_inbuf(connection_t *conn);
  435. int connection_process_cell_from_inbuf(connection_t *conn);
  436. int connection_consider_sending_sendme(connection_t *conn, int edge_type);
  437. int connection_finished_flushing(connection_t *conn);
  438. void cell_pack(char *dest, const cell_t *src);
  439. void cell_unpack(cell_t *dest, const char *src);
  440. /********************************* connection_ap.c ****************************/
  441. int ap_handshake_process_socks(connection_t *conn);
  442. int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  443. int ap_handshake_socks_reply(connection_t *conn, char result);
  444. int connection_ap_create_listener(struct sockaddr_in *bindaddr);
  445. int connection_ap_handle_listener_read(connection_t *conn);
  446. /********************************* connection_edge.c ***************************/
  447. int connection_edge_process_inbuf(connection_t *conn);
  448. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command);
  449. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int edge_type, crypt_path_t *layer_hint);
  450. int connection_edge_finished_flushing(connection_t *conn);
  451. /********************************* connection_exit.c ***************************/
  452. int connection_exit_send_connected(connection_t *conn);
  453. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  454. int connection_exit_connect(connection_t *conn);
  455. /********************************* connection_op.c ***************************/
  456. int op_handshake_process_keys(connection_t *conn);
  457. int connection_op_process_inbuf(connection_t *conn);
  458. int connection_op_finished_flushing(connection_t *conn);
  459. int connection_op_create_listener(struct sockaddr_in *bindaddr);
  460. int connection_op_handle_listener_read(connection_t *conn);
  461. /********************************* connection_or.c ***************************/
  462. int connection_or_process_inbuf(connection_t *conn);
  463. int connection_or_finished_flushing(connection_t *conn);
  464. connection_t *connection_or_connect(routerinfo_t *router);
  465. int connection_or_create_listener(struct sockaddr_in *bindaddr);
  466. int connection_or_handle_listener_read(connection_t *conn);
  467. /********************************* directory.c ***************************/
  468. void directory_initiate_fetch(routerinfo_t *router);
  469. int directory_send_command(connection_t *conn);
  470. void directory_set_dirty(void);
  471. void directory_rebuild(void);
  472. int connection_dir_process_inbuf(connection_t *conn);
  473. int directory_handle_command(connection_t *conn);
  474. int directory_handle_reading(connection_t *conn);
  475. int connection_dir_finished_flushing(connection_t *conn);
  476. int connection_dir_create_listener(struct sockaddr_in *bindaddr);
  477. int connection_dir_handle_listener_read(connection_t *conn);
  478. /********************************* dns.c ***************************/
  479. void dns_init(void);
  480. int connection_dns_finished_flushing(connection_t *conn);
  481. int connection_dns_process_inbuf(connection_t *conn);
  482. void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
  483. int dns_resolve(connection_t *exitconn);
  484. /********************************* main.c ***************************/
  485. void set_privatekey(crypto_pk_env_t *k);
  486. crypto_pk_env_t *get_privatekey(void);
  487. void set_signing_privatekey(crypto_pk_env_t *k);
  488. crypto_pk_env_t *get_signing_privatekey(void);
  489. int connection_add(connection_t *conn);
  490. int connection_remove(connection_t *conn);
  491. void connection_set_poll_socket(connection_t *conn);
  492. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  493. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  494. connection_t *connection_get_by_type(int type);
  495. connection_t *connection_get_by_type_state(int type, int state);
  496. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  497. void connection_watch_events(connection_t *conn, short events);
  498. void connection_stop_reading(connection_t *conn);
  499. void connection_start_reading(connection_t *conn);
  500. void connection_stop_writing(connection_t *conn);
  501. void connection_start_writing(connection_t *conn);
  502. int dump_signed_directory_to_string(char *s, int maxlen,
  503. crypto_pk_env_t *private_key);
  504. /* Exported for debugging */
  505. int dump_signed_directory_to_string_impl(char *s, int maxlen,
  506. directory_t *dir,
  507. crypto_pk_env_t *private_key);
  508. int main(int argc, char *argv[]);
  509. /********************************* onion.c ***************************/
  510. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  511. uint32_t remote_addr, uint16_t remote_port);
  512. int onion_pending_add(circuit_t *circ);
  513. int onion_pending_check(void);
  514. void onion_pending_process_one(void);
  515. void onion_pending_remove(circuit_t *circ);
  516. /* uses a weighted coin with weight cw to choose a route length */
  517. int chooselen(double cw);
  518. /* returns an array of pointers to routent that define a new route through the OR network
  519. * int cw is the coin weight to use when choosing the route
  520. * order of routers is from last to first
  521. */
  522. unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
  523. crypt_path_t *onion_generate_cpath(routerinfo_t **firsthop);
  524. int onion_skin_create(crypto_pk_env_t *router_key,
  525. crypto_dh_env_t **handshake_state_out,
  526. char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
  527. int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
  528. crypto_pk_env_t *private_key,
  529. char *handshake_reply_out, /* DH_KEY_LEN bytes long */
  530. char *key_out,
  531. int key_out_len);
  532. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  533. char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
  534. char *key_out,
  535. int key_out_len);
  536. /********************************* routers.c ***************************/
  537. int learn_my_address(struct sockaddr_in *me);
  538. void router_retry_connections(void);
  539. routerinfo_t *router_pick_directory_server(void);
  540. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  541. void router_get_directory(directory_t **pdirectory);
  542. int router_is_me(uint32_t addr, uint16_t port);
  543. void router_forget_router(uint32_t addr, uint16_t port);
  544. int router_get_list_from_file(char *routerfile);
  545. int router_resolve(routerinfo_t *router);
  546. int router_resolve_directory(directory_t *dir);
  547. /* Reads a list of known routers, unsigned. */
  548. int router_get_list_from_string(char *s);
  549. /* Exported for debugging */
  550. int router_get_list_from_string_impl(char *s, directory_t **dest);
  551. /* Reads a signed directory. */
  552. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey);
  553. /* Exported or debugging */
  554. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  555. crypto_pk_env_t *pkey);
  556. routerinfo_t *router_get_entry_from_string(char **s);
  557. int router_compare_to_exit_policy(connection_t *conn);
  558. void routerinfo_free(routerinfo_t *router);
  559. #endif
  560. /*
  561. Local Variables:
  562. mode:c
  563. indent-tabs-mode:nil
  564. c-basic-offset:2
  565. End:
  566. */