or.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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/config.h"
  37. #include "../common/crypto.h"
  38. #include "../common/log.h"
  39. #include "../common/ss.h"
  40. #include "../common/version.h"
  41. #define MAXCONNECTIONS 200 /* upper bound on max connections.
  42. can be overridden by config file */
  43. #define MAX_BUF_SIZE (640*1024)
  44. #define DEFAULT_BANDWIDTH_OP 102400
  45. #define ACI_TYPE_LOWER 0
  46. #define ACI_TYPE_HIGHER 1
  47. #define ACI_TYPE_BOTH 2
  48. /* bitvector of the roles that we might want to play. You can or (|) them together */
  49. #define ROLE_OR_LISTEN 1
  50. #define ROLE_OR_CONNECT_ALL 2
  51. #define ROLE_OP_LISTEN 4
  52. #define ROLE_AP_LISTEN 8
  53. #define ROLE_DIR_LISTEN 16
  54. #define ROLE_DIR_SERVER 32
  55. #define ROLE_IS_OR(role) ((role & ROLE_OR_LISTEN) || (role & ROLE_OR_CONNECT_ALL) || (role & ROLE_OP_LISTEN))
  56. #define CONN_TYPE_OP_LISTENER 1
  57. #define CONN_TYPE_OP 2
  58. #define CONN_TYPE_OR_LISTENER 3
  59. #define CONN_TYPE_OR 4
  60. #define CONN_TYPE_EXIT 5
  61. #define CONN_TYPE_AP_LISTENER 6
  62. #define CONN_TYPE_AP 7
  63. #define CONN_TYPE_DIR_LISTENER 8
  64. #define CONN_TYPE_DIR 9
  65. #define LISTENER_STATE_READY 0
  66. #define OP_CONN_STATE_AWAITING_KEYS 0
  67. #define OP_CONN_STATE_OPEN 1
  68. #if 0
  69. #define OP_CONN_STATE_CLOSE 2 /* flushing the buffer, then will close */
  70. #define OP_CONN_STATE_CLOSE_WAIT 3 /* have sent a destroy, awaiting a confirmation */
  71. #endif
  72. /* how to read these states:
  73. * foo_CONN_STATE_bar_baz:
  74. * "I am acting as a bar, currently in stage baz of talking with a foo."
  75. */
  76. #define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
  77. #define OR_CONN_STATE_OP_SENDING_KEYS 1
  78. #define OR_CONN_STATE_CLIENT_CONNECTING 2 /* I'm connecting to this OR as an OR */
  79. #define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
  80. #define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
  81. #define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
  82. #define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
  83. #define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
  84. #define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
  85. #define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
  86. #define EXIT_CONN_STATE_CONNECTING_WAIT 0 /* waiting for standard structure or dest info */
  87. #define EXIT_CONN_STATE_CONNECTING 1
  88. #define EXIT_CONN_STATE_OPEN 2
  89. #if 0
  90. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  91. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  92. #endif
  93. #define AP_CONN_STATE_SOCKS_WAIT 0
  94. #define AP_CONN_STATE_OR_WAIT 1
  95. #define AP_CONN_STATE_OPEN 2
  96. #define DIR_CONN_STATE_CONNECTING 0
  97. #define DIR_CONN_STATE_SENDING_COMMAND 1
  98. #define DIR_CONN_STATE_READING 2
  99. #define DIR_CONN_STATE_COMMAND_WAIT 3
  100. #define DIR_CONN_STATE_WRITING 4
  101. #define CIRCUIT_STATE_OPEN_WAIT 0 /* receiving/processing the onion */
  102. #define CIRCUIT_STATE_OR_WAIT 1 /* I'm at the beginning of the path, my firsthop is still connecting */
  103. #define CIRCUIT_STATE_OPEN 2 /* onion processed, ready to send data along the connection */
  104. #define CIRCUIT_STATE_CLOSE_WAIT1 3 /* sent two "destroy" signals, waiting for acks */
  105. #define CIRCUIT_STATE_CLOSE_WAIT2 4 /* received one ack, waiting for one more
  106. (or if just one was sent, waiting for that one */
  107. //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
  108. /* available cipher functions */
  109. #define ONION_CIPHER_IDENTITY 0
  110. #define ONION_CIPHER_DES 1
  111. #define ONION_CIPHER_RC4 2
  112. /* default cipher function */
  113. #define ONION_DEFAULT_CIPHER ONION_CIPHER_DES
  114. #define RECEIVE_WINDOW_START 100
  115. #define RECEIVE_WINDOW_INCREMENT 10
  116. /* cell commands */
  117. #define CELL_PADDING 0
  118. #define CELL_CREATE 1
  119. #define CELL_DATA 2
  120. #define CELL_DESTROY 3
  121. #define CELL_ACK 4
  122. #define CELL_NACK 5
  123. #define CELL_SENDME 6
  124. #define CELL_CONNECTED 7
  125. #define CELL_PAYLOAD_SIZE 120
  126. typedef uint16_t aci_t;
  127. /* cell definition */
  128. typedef struct
  129. {
  130. aci_t aci; /* Anonymous Connection Identifier */
  131. unsigned char command;
  132. unsigned char length; /* of payload if data cell, else value of sendme */
  133. uint32_t seq; /* sequence number */
  134. unsigned char payload[120];
  135. } cell_t;
  136. #define SOCKS4_REQUEST_GRANTED 90
  137. #define SOCKS4_REQUEST_REJECT 91
  138. #define SOCKS4_REQUEST_IDENT_FAILED 92
  139. #define SOCKS4_REQUEST_IDENT_CONFLICT 93
  140. /* structure of a socks client operation */
  141. typedef struct {
  142. unsigned char version; /* socks version number */
  143. unsigned char command; /* command code */
  144. unsigned char destport[2]; /* destination port, network order */
  145. unsigned char destip[4]; /* destination address */
  146. /* userid follows, terminated by a NULL */
  147. /* dest host follows, terminated by a NULL */
  148. } socks4_t;
  149. typedef struct
  150. {
  151. /* Used by all types: */
  152. unsigned char type;
  153. int state;
  154. int s; /* our socket */
  155. int poll_index;
  156. int marked_for_close;
  157. char *inbuf;
  158. int inbuflen;
  159. int inbuf_datalen;
  160. int inbuf_reached_eof;
  161. char *outbuf;
  162. int outbuflen; /* how many bytes are allocated for the outbuf? */
  163. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  164. int outbuf_datalen; /* how much data is there total on the outbuf? */
  165. // uint16_t aci; /* anonymous connection identifier */
  166. /* used by OR and OP: */
  167. uint32_t bandwidth; /* connection bandwidth */
  168. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  169. * add 'bandwidth' to this, capping it at 10*bandwidth.
  170. */
  171. struct timeval send_timeval; /* for determining when to send the next cell */
  172. /* link encryption */
  173. crypto_cipher_env_t *f_crypto;
  174. crypto_cipher_env_t *b_crypto;
  175. // struct timeval lastsend; /* time of last transmission to the client */
  176. // struct timeval interval; /* transmission interval */
  177. uint32_t addr; /* these two uniquely identify a router */
  178. uint16_t port;
  179. /* used by exit and ap: */
  180. // ss_t ss; /* standard structure */
  181. // int ss_received; /* size of ss, received so far */
  182. char socks_version;
  183. char read_username;
  184. char *dest_addr;
  185. uint16_t dest_port;
  186. char dest_tmp[512];
  187. int dest_tmplen;
  188. /* used by OR, to keep state while connect()ing: Kludge. */
  189. struct sockaddr_in local;
  190. #if 0 /* obsolete, we now use conn->bandwidth */
  191. /* link info */
  192. uint32_t min;
  193. uint32_t max;
  194. #endif
  195. char *address; /* strdup into this, because free_connection frees it */
  196. crypto_pk_env_t *pkey; /* public RSA key for the other side */
  197. char nonce[8];
  198. } connection_t;
  199. /* config stuff we know about the other ORs in the network */
  200. typedef struct
  201. {
  202. char *address;
  203. uint32_t addr;
  204. uint16_t or_port;
  205. uint16_t op_port;
  206. uint16_t ap_port;
  207. uint16_t dir_port;
  208. crypto_pk_env_t *pkey; /* public RSA key */
  209. /* link info */
  210. uint32_t bandwidth;
  211. // struct timeval min_interval;
  212. /* time when last data was sent to that router */
  213. // struct timeval lastsend;
  214. /* socket */
  215. // int s;
  216. void *next;
  217. } routerinfo_t;
  218. typedef struct
  219. {
  220. unsigned int forwf;
  221. unsigned int backf;
  222. char digest2[20]; /* second SHA output for onion_layer_t.keyseed */
  223. char digest3[20]; /* third SHA output for onion_layer_t.keyseed */
  224. /* crypto environments */
  225. crypto_cipher_env_t *f_crypto;
  226. crypto_cipher_env_t *b_crypto;
  227. } crypt_path_t;
  228. /* per-anonymous-connection struct */
  229. typedef struct
  230. {
  231. #if 0
  232. uint32_t p_addr; /* all in network order */
  233. uint16_t p_port;
  234. #endif
  235. uint32_t n_addr;
  236. uint16_t n_port;
  237. connection_t *p_conn;
  238. connection_t *n_conn;
  239. int n_receive_window;
  240. int p_receive_window;
  241. aci_t p_aci; /* connection identifiers */
  242. aci_t n_aci;
  243. unsigned char p_f; /* crypto functions */
  244. unsigned char n_f;
  245. crypto_cipher_env_t *p_crypto; /* crypto environments */
  246. crypto_cipher_env_t *n_crypto;
  247. crypt_path_t **cpath;
  248. int cpathlen;
  249. uint32_t expire; /* expiration time for the corresponding onion */
  250. int state;
  251. unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  252. uint32_t onionlen; /* total onion length */
  253. uint32_t recvlen; /* length of the onion so far */
  254. void *next;
  255. } circuit_t;
  256. typedef struct
  257. {
  258. int zero:1;
  259. int version:7;
  260. int backf:4;
  261. int forwf:4;
  262. uint16_t port;
  263. uint32_t addr;
  264. time_t expire;
  265. unsigned char keyseed[16];
  266. } onion_layer_t;
  267. typedef struct
  268. {
  269. time_t expire;
  270. char digest[20]; /* SHA digest of the onion */
  271. void *prev;
  272. void *next;
  273. } tracked_onion_t;
  274. typedef struct
  275. {
  276. char *LogLevel;
  277. char *RouterFile;
  278. char *PrivateKeyFile;
  279. float CoinWeight;
  280. int ORPort;
  281. int OPPort;
  282. int APPort;
  283. int DirPort;
  284. int MaxConn;
  285. int TrafficShaping;
  286. int LinkPadding;
  287. int DirRebuildPeriod;
  288. int DirFetchPeriod;
  289. int Role;
  290. int loglevel;
  291. } or_options_t;
  292. /* all the function prototypes go here */
  293. /********************************* buffers.c ***************************/
  294. int buf_new(char **buf, int *buflen, int *buf_datalen);
  295. void buf_free(char *buf);
  296. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof);
  297. /* grab from s, put onto buf, return how many bytes read */
  298. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
  299. /* push from buf onto s
  300. * then memmove to front of buf
  301. * return -1 or how many bytes remain on the buf */
  302. int write_to_buf(char *string, int string_len,
  303. char **buf, int *buflen, int *buf_datalen);
  304. /* append string to buf (growing as needed, return -1 if "too big")
  305. * return total number of bytes on the buf
  306. */
  307. int fetch_from_buf(char *string, int string_len,
  308. char **buf, int *buflen, int *buf_datalen);
  309. /* if there is string_len bytes in buf, write them onto string,
  310. * then memmove buf back (that is, remove them from buf)
  311. */
  312. int find_on_inbuf(char *string, int string_len,
  313. char *buf, int buf_datalen);
  314. /* find first instance of needle 'string' on haystack 'buf'. return how
  315. * many bytes from the beginning of buf to the end of string.
  316. * If it's not there, return -1.
  317. */
  318. /********************************* cell.c ***************************/
  319. int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
  320. /********************************* circuit.c ***************************/
  321. void circuit_add(circuit_t *circ);
  322. void circuit_remove(circuit_t *circ);
  323. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  324. /* internal */
  325. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  326. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  327. circuit_t *circuit_get_by_conn(connection_t *conn);
  328. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
  329. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int crypt_type);
  330. int circuit_crypt(circuit_t *circ, char *in, int inlen, char crypt_type);
  331. int circuit_init(circuit_t *circ, int aci_type);
  332. void circuit_free(circuit_t *circ);
  333. void circuit_free_cpath(crypt_path_t **cpath, int cpathlen);
  334. void circuit_close(circuit_t *circ);
  335. void circuit_about_to_close_connection(connection_t *conn);
  336. /* flush and send destroys for all circuits using conn */
  337. void circuit_dump_by_conn(connection_t *conn);
  338. /********************************* command.c ***************************/
  339. void command_process_cell(cell_t *cell, connection_t *conn);
  340. void command_process_create_cell(cell_t *cell, connection_t *conn);
  341. void command_process_sendme_cell(cell_t *cell, connection_t *conn);
  342. void command_process_data_cell(cell_t *cell, connection_t *conn);
  343. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  344. void command_process_connected_cell(cell_t *cell, connection_t *conn);
  345. /********************************* config.c ***************************/
  346. /* loads the configuration file */
  347. int getconfig(char *filename, config_opt_t *options);
  348. /* create or_options_t from command-line args and config files(s) */
  349. int getoptions(int argc, char **argv, or_options_t *options);
  350. /********************************* connection.c ***************************/
  351. int tv_cmp(struct timeval *a, struct timeval *b);
  352. connection_t *connection_new(int type);
  353. void connection_free(connection_t *conn);
  354. int connection_create_listener(struct sockaddr_in *local, int type);
  355. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  356. /* start all connections that should be up but aren't */
  357. int learn_local(struct sockaddr_in *local);
  358. int retry_all_connections(int role, uint16_t or_listenport,
  359. uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
  360. connection_t *connection_connect_to_router_as_op(routerinfo_t *router, uint16_t local_or_port);
  361. int connection_read_to_buf(connection_t *conn);
  362. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  363. int connection_outbuf_too_full(connection_t *conn);
  364. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  365. int connection_wants_to_flush(connection_t *conn);
  366. int connection_flush_buf(connection_t *conn);
  367. int connection_write_to_buf(char *string, int len, connection_t *conn);
  368. void connection_send_cell(connection_t *conn);
  369. int connection_receiver_bucket_should_increase(connection_t *conn);
  370. void connection_increment_receiver_bucket (connection_t *conn);
  371. void connection_increment_send_timeval(connection_t *conn);
  372. void connection_init_timeval(connection_t *conn);
  373. int connection_speaks_cells(connection_t *conn);
  374. int connection_is_listener(connection_t *conn);
  375. int connection_state_is_open(connection_t *conn);
  376. int connection_send_destroy(aci_t aci, connection_t *conn);
  377. int connection_send_connected(aci_t aci, connection_t *conn);
  378. int connection_encrypt_cell(cell_t *cellp, connection_t *conn);
  379. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn);
  380. int connection_process_inbuf(connection_t *conn);
  381. int connection_package_raw_inbuf(connection_t *conn);
  382. int connection_process_cell_from_inbuf(connection_t *conn);
  383. int connection_consider_sending_sendme(connection_t *conn);
  384. int connection_finished_flushing(connection_t *conn);
  385. /********************************* connection_ap.c ****************************/
  386. int connection_ap_process_inbuf(connection_t *conn);
  387. int ap_handshake_process_socks(connection_t *conn);
  388. int ap_handshake_create_onion(connection_t *conn);
  389. int ap_handshake_establish_circuit(connection_t *conn, unsigned int *route, int routelen, char *onion,
  390. int onionlen, crypt_path_t **cpath);
  391. void ap_handshake_n_conn_open(connection_t *or_conn);
  392. int ap_handshake_send_onion(connection_t *ap_conn, connection_t *or_conn, circuit_t *circ);
  393. int ap_handshake_socks_reply(connection_t *conn, char result);
  394. int connection_ap_send_connected(connection_t *conn);
  395. int connection_ap_process_data_cell(cell_t *cell, connection_t *conn);
  396. int connection_ap_finished_flushing(connection_t *conn);
  397. int connection_ap_create_listener(struct sockaddr_in *local);
  398. int connection_ap_handle_listener_read(connection_t *conn);
  399. /********************************* connection_exit.c ***************************/
  400. int connection_exit_process_inbuf(connection_t *conn);
  401. int connection_exit_package_inbuf(connection_t *conn);
  402. int connection_exit_send_connected(connection_t *conn);
  403. int connection_exit_process_data_cell(cell_t *cell, connection_t *conn);
  404. int connection_exit_finished_flushing(connection_t *conn);
  405. /********************************* connection_op.c ***************************/
  406. int op_handshake_process_keys(connection_t *conn);
  407. int connection_op_process_inbuf(connection_t *conn);
  408. int connection_op_finished_flushing(connection_t *conn);
  409. int connection_op_create_listener(struct sockaddr_in *local);
  410. int connection_op_handle_listener_read(connection_t *conn);
  411. /********************************* connection_or.c ***************************/
  412. int connection_or_process_inbuf(connection_t *conn);
  413. int connection_or_finished_flushing(connection_t *conn);
  414. void conn_or_init_crypto(connection_t *conn);
  415. int or_handshake_op_send_keys(connection_t *conn);
  416. int or_handshake_op_finished_sending_keys(connection_t *conn);
  417. int or_handshake_client_process_auth(connection_t *conn);
  418. int or_handshake_client_send_auth(connection_t *conn);
  419. int or_handshake_server_process_auth(connection_t *conn);
  420. int or_handshake_server_process_nonce(connection_t *conn);
  421. connection_t *connect_to_router_as_or(routerinfo_t *router, struct sockaddr_in *local);
  422. connection_t *connection_or_connect_as_or(routerinfo_t *router, struct sockaddr_in *local);
  423. connection_t *connection_or_connect_as_op(routerinfo_t *router, struct sockaddr_in *local);
  424. int connection_or_create_listener(struct sockaddr_in *local);
  425. int connection_or_handle_listener_read(connection_t *conn);
  426. /********************************* directory.c ***************************/
  427. void directory_initiate_fetch(routerinfo_t *router);
  428. int directory_send_command(connection_t *conn);
  429. void directory_rebuild(void);
  430. int connection_dir_process_inbuf(connection_t *conn);
  431. int directory_handle_command(connection_t *conn);
  432. int directory_handle_reading(connection_t *conn);
  433. int connection_dir_finished_flushing(connection_t *conn);
  434. int connection_dir_create_listener(struct sockaddr_in *local);
  435. int connection_dir_handle_listener_read(connection_t *conn);
  436. /********************************* main.c ***************************/
  437. void setprivatekey(crypto_pk_env_t *k);
  438. crypto_pk_env_t *getprivatekey(void);
  439. int connection_add(connection_t *conn);
  440. int connection_remove(connection_t *conn);
  441. void connection_set_poll_socket(connection_t *conn);
  442. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  443. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  444. connection_t *connection_get_by_type(int type);
  445. connection_t *connect_to_router_as_op(routerinfo_t *router);
  446. void connection_watch_events(connection_t *conn, short events);
  447. void connection_stop_reading(connection_t *conn);
  448. void connection_start_reading(connection_t *conn);
  449. void connection_stop_writing(connection_t *conn);
  450. void connection_start_writing(connection_t *conn);
  451. void check_conn_read(int i);
  452. void check_conn_marked(int i);
  453. void check_conn_write(int i);
  454. int prepare_for_poll(int *timeout);
  455. int do_main_loop(void);
  456. void dumpstats(void);
  457. void dump_directory_to_string(char *s, int maxlen);
  458. int main(int argc, char *argv[]);
  459. /********************************* onion.c ***************************/
  460. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  461. uint32_t remote_addr, uint16_t remote_port);
  462. int process_onion(circuit_t *circ, connection_t *conn);
  463. /* uses a weighted coin with weight cw to choose a route length */
  464. int chooselen(double cw);
  465. /* returns an array of pointers to routent that define a new route through the OR network
  466. * int cw is the coin weight to use when choosing the route
  467. * order of routers is from last to first
  468. */
  469. unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
  470. /* creates a new onion from route, stores it and its length into bufp and lenp respectively */
  471. unsigned char *create_onion(routerinfo_t **rarray, int rarray_len, unsigned int *route, int routelen, int *len, crypt_path_t **cpath);
  472. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  473. * DES OFB with the key as defined in the outter layer */
  474. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *pkey);
  475. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  476. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *prkey);
  477. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  478. void pad_onion(unsigned char *onion, uint32_t onionlen, int n);
  479. /* create a new tracked_onion entry */
  480. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  481. /* delete a tracked onion entry */
  482. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  483. /* find a tracked onion in the linked list of tracked onions */
  484. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions);
  485. /********************************* routers.c ***************************/
  486. void router_retry_connections(struct sockaddr_in *local);
  487. routerinfo_t *router_pick_directory_server(void);
  488. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  489. unsigned int *router_new_route(int *routelen);
  490. unsigned char *router_create_onion(unsigned int *route, int routelen, int *len, crypt_path_t **cpath);
  491. routerinfo_t *router_get_first_in_route(unsigned int *route, int routelen);
  492. void router_forget_router(uint32_t addr, uint16_t port);
  493. int router_get_list_from_file(char *routerfile, uint16_t or_listenport);
  494. int router_get_list_from_string(char *s, uint16_t or_listenport);
  495. #endif