or.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. #ifdef USE_ZLIB
  37. #include <zlib.h>
  38. #endif
  39. #include "../common/crypto.h"
  40. #include "../common/log.h"
  41. #include "../common/ss.h"
  42. #include "../common/version.h"
  43. #define MAXCONNECTIONS 1000 /* upper bound on max connections.
  44. can be lowered by config file */
  45. #define MAX_BUF_SIZE (640*1024)
  46. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  47. #define ACI_TYPE_LOWER 0
  48. #define ACI_TYPE_HIGHER 1
  49. #define ACI_TYPE_BOTH 2
  50. /* bitvector of the roles that we might want to play. You can or (|) them together */
  51. #define ROLE_OR_LISTEN 1
  52. #define ROLE_OR_CONNECT_ALL 2
  53. #define ROLE_OP_LISTEN 4
  54. #define ROLE_AP_LISTEN 8
  55. #define ROLE_DIR_LISTEN 16
  56. #define ROLE_DIR_SERVER 32
  57. #define ROLE_IS_OR(role) ((role & ROLE_OR_LISTEN) || (role & ROLE_OR_CONNECT_ALL) || (role & ROLE_OP_LISTEN))
  58. #define CONN_TYPE_OP_LISTENER 1
  59. #define CONN_TYPE_OP 2
  60. #define CONN_TYPE_OR_LISTENER 3
  61. #define CONN_TYPE_OR 4
  62. #define CONN_TYPE_EXIT 5
  63. #define CONN_TYPE_AP_LISTENER 6
  64. #define CONN_TYPE_AP 7
  65. #define CONN_TYPE_DIR_LISTENER 8
  66. #define CONN_TYPE_DIR 9
  67. #define CONN_TYPE_DNSMASTER 10
  68. #define LISTENER_STATE_READY 0
  69. #define DNSMASTER_STATE_OPEN 0
  70. #define OP_CONN_STATE_AWAITING_KEYS 0
  71. #define OP_CONN_STATE_OPEN 1
  72. #if 0
  73. #define OP_CONN_STATE_CLOSE 2 /* flushing the buffer, then will close */
  74. #define OP_CONN_STATE_CLOSE_WAIT 3 /* have sent a destroy, awaiting a confirmation */
  75. #endif
  76. /* how to read these states:
  77. * foo_CONN_STATE_bar_baz:
  78. * "I am acting as a bar, currently in stage baz of talking with a foo."
  79. */
  80. #define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
  81. #define OR_CONN_STATE_OP_SENDING_KEYS 1
  82. #define OR_CONN_STATE_CLIENT_CONNECTING 2 /* I'm connecting to this OR as an OR */
  83. #define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
  84. #define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
  85. #define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
  86. #define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
  87. #define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
  88. #define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
  89. #define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
  90. #define EXIT_CONN_STATE_RESOLVING 0 /* waiting for response from dnsmaster */
  91. #define EXIT_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
  92. #define EXIT_CONN_STATE_OPEN 2
  93. #if 0
  94. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  95. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  96. #endif
  97. #define AP_CONN_STATE_SOCKS_WAIT 0
  98. #define AP_CONN_STATE_OR_WAIT 1
  99. #define AP_CONN_STATE_OPEN 2
  100. #define DIR_CONN_STATE_CONNECTING 0
  101. #define DIR_CONN_STATE_SENDING_COMMAND 1
  102. #define DIR_CONN_STATE_READING 2
  103. #define DIR_CONN_STATE_COMMAND_WAIT 3
  104. #define DIR_CONN_STATE_WRITING 4
  105. #define CIRCUIT_STATE_ONION_WAIT 0 /* receiving the onion */
  106. #define CIRCUIT_STATE_ONION_PENDING 1 /* waiting to process the onion */
  107. #define CIRCUIT_STATE_OR_WAIT 2 /* I'm at the beginning of the path, my firsthop is still connecting */
  108. #define CIRCUIT_STATE_OPEN 3 /* onion processed, ready to send data along the connection */
  109. //#define CIRCUIT_STATE_CLOSE_WAIT1 4 /* sent two "destroy" signals, waiting for acks */
  110. //#define CIRCUIT_STATE_CLOSE_WAIT2 5 /* received one ack, waiting for one more
  111. // (or if just one was sent, waiting for that one */
  112. //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
  113. #define TOPIC_COMMAND_BEGIN 1
  114. #define TOPIC_COMMAND_DATA 2
  115. #define TOPIC_COMMAND_END 3
  116. #define TOPIC_COMMAND_CONNECTED 4
  117. #define TOPIC_COMMAND_SENDME 5
  118. #define TOPIC_HEADER_SIZE 4
  119. #define TOPIC_STATE_RESOLVING
  120. /* available cipher functions */
  121. #define ONION_CIPHER_IDENTITY 0
  122. #define ONION_CIPHER_DES 1
  123. #define ONION_CIPHER_RC4 2
  124. /* default cipher function */
  125. #define ONION_DEFAULT_CIPHER ONION_CIPHER_DES
  126. #define CELL_DIRECTION_IN 1
  127. #define CELL_DIRECTION_OUT 2
  128. #define EDGE_EXIT 3 /* make direction and edge values not overlap, to help catch bugs */
  129. #define EDGE_AP 4
  130. #define CIRCWINDOW_START 1000
  131. #define CIRCWINDOW_INCREMENT 100
  132. #define TOPICWINDOW_START 500
  133. #define TOPICWINDOW_INCREMENT 50
  134. /* cell commands */
  135. #define CELL_PADDING 0
  136. #define CELL_CREATE 1
  137. #define CELL_DATA 2
  138. #define CELL_DESTROY 3
  139. #define CELL_SENDME 4
  140. #define CELL_PAYLOAD_SIZE 120
  141. #define CELL_NETWORK_SIZE 128
  142. /* enumeration of types which option values can take */
  143. #define CONFIG_TYPE_STRING 0
  144. #define CONFIG_TYPE_CHAR 1
  145. #define CONFIG_TYPE_INT 2
  146. #define CONFIG_TYPE_LONG 3
  147. #define CONFIG_TYPE_DOUBLE 4
  148. #define CONFIG_TYPE_BOOL 5
  149. #define CONFIG_LINE_MAXLEN 1024
  150. /* legal characters in a filename */
  151. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  152. struct config_line {
  153. char *key;
  154. char *value;
  155. struct config_line *next;
  156. };
  157. typedef uint16_t aci_t;
  158. /* cell definition */
  159. typedef struct {
  160. aci_t aci; /* Anonymous Connection Identifier */
  161. unsigned char command;
  162. unsigned char length; /* of payload if data cell, else value of sendme */
  163. uint32_t seq; /* sequence number */
  164. unsigned char payload[120];
  165. } cell_t;
  166. #define SOCKS4_REQUEST_GRANTED 90
  167. #define SOCKS4_REQUEST_REJECT 91
  168. #define SOCKS4_REQUEST_IDENT_FAILED 92
  169. #define SOCKS4_REQUEST_IDENT_CONFLICT 93
  170. /* structure of a socks client operation */
  171. typedef struct {
  172. unsigned char version; /* socks version number */
  173. unsigned char command; /* command code */
  174. unsigned char destport[2]; /* destination port, network order */
  175. unsigned char destip[4]; /* destination address */
  176. /* userid follows, terminated by a NULL */
  177. /* dest host follows, terminated by a NULL */
  178. } socks4_t;
  179. struct connection_t {
  180. /* Used by all types: */
  181. unsigned char type;
  182. int state;
  183. int s; /* our socket */
  184. int poll_index;
  185. int marked_for_close;
  186. char *inbuf;
  187. int inbuflen;
  188. int inbuf_datalen;
  189. int inbuf_reached_eof;
  190. long timestamp_lastread;
  191. char *outbuf;
  192. int outbuflen; /* how many bytes are allocated for the outbuf? */
  193. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  194. int outbuf_datalen; /* how much data is there total on the outbuf? */
  195. long timestamp_lastwritten;
  196. long timestamp_created;
  197. int onions_handled_this_second;
  198. /* used by OR and OP: */
  199. uint32_t bandwidth; /* connection bandwidth */
  200. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  201. * add 'bandwidth' to this, capping it at 10*bandwidth.
  202. */
  203. struct timeval send_timeval; /* for determining when to send the next cell */
  204. /* link encryption */
  205. crypto_cipher_env_t *f_crypto;
  206. crypto_cipher_env_t *b_crypto;
  207. // struct timeval lastsend; /* time of last transmission to the client */
  208. // struct timeval interval; /* transmission interval */
  209. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  210. uint16_t port;
  211. /* used by exit and ap: */
  212. uint16_t topic_id;
  213. struct connection_t *next_topic;
  214. int n_receive_topicwindow;
  215. int p_receive_topicwindow;
  216. int done_sending;
  217. int done_receiving;
  218. #ifdef USE_ZLIB
  219. char *z_outbuf;
  220. int z_outbuflen;
  221. int z_outbuf_datalen;
  222. z_stream *compression;
  223. z_stream *decompression;
  224. #endif
  225. /* Used by ap: */
  226. char socks_version;
  227. char read_username;
  228. /* Used by exit and ap: */
  229. char *dest_addr;
  230. uint16_t dest_port; /* host order */
  231. /* Used by ap: */
  232. char dest_tmp[512];
  233. int dest_tmplen;
  234. /* Used by everyone */
  235. char *address; /* strdup into this, because free_connection frees it */
  236. /* Used for cell connections */
  237. crypto_pk_env_t *pkey; /* public RSA key for the other side */
  238. /* Used while negotiating OR/OR connections */
  239. char nonce[8];
  240. };
  241. typedef struct connection_t connection_t;
  242. /* config stuff we know about the other ORs in the network */
  243. typedef struct {
  244. char *address;
  245. uint32_t addr; /* all host order */
  246. uint16_t or_port;
  247. uint16_t op_port;
  248. uint16_t ap_port;
  249. uint16_t dir_port;
  250. crypto_pk_env_t *pkey; /* public RSA key */
  251. /* link info */
  252. uint32_t bandwidth;
  253. // struct timeval min_interval;
  254. /* time when last data was sent to that router */
  255. // struct timeval lastsend;
  256. /* socket */
  257. // int s;
  258. void *next;
  259. } routerinfo_t;
  260. typedef struct {
  261. unsigned int forwf;
  262. unsigned int backf;
  263. char digest2[20]; /* second SHA output for onion_layer_t.keyseed */
  264. char digest3[20]; /* third SHA output for onion_layer_t.keyseed */
  265. /* crypto environments */
  266. crypto_cipher_env_t *f_crypto;
  267. crypto_cipher_env_t *b_crypto;
  268. } crypt_path_t;
  269. struct data_queue_t {
  270. cell_t *cell;
  271. struct data_queue_t *next;
  272. };
  273. /* per-anonymous-connection struct */
  274. typedef struct {
  275. uint32_t n_addr;
  276. uint16_t n_port;
  277. connection_t *p_conn;
  278. connection_t *n_conn;
  279. int n_receive_circwindow;
  280. int p_receive_circwindow;
  281. aci_t p_aci; /* connection identifiers */
  282. aci_t n_aci;
  283. struct data_queue_t *data_queue; /* for queueing cells at the edges */
  284. unsigned char p_f; /* crypto functions */
  285. unsigned char n_f;
  286. crypto_cipher_env_t *p_crypto; /* crypto environments */
  287. crypto_cipher_env_t *n_crypto;
  288. crypt_path_t **cpath;
  289. int cpathlen;
  290. uint32_t expire; /* expiration time for the corresponding onion */
  291. int state;
  292. unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  293. uint32_t onionlen; /* total onion length */
  294. uint32_t recvlen; /* length of the onion so far */
  295. void *next;
  296. } circuit_t;
  297. struct onion_queue_t {
  298. circuit_t *circ;
  299. struct data_queue_t *data_cells;
  300. struct onion_queue_t *next;
  301. };
  302. #if 0
  303. typedef struct
  304. {
  305. int zero:1;
  306. int version:7;
  307. int backf:4;
  308. int forwf:4;
  309. uint16_t port;
  310. uint32_t addr;
  311. uint32_t expire;
  312. unsigned char keyseed[16];
  313. } onion_layer_t;
  314. #endif
  315. #define ONION_LAYER_SIZE 28
  316. #define ONION_PADDING_SIZE (128-ONION_LAYER_SIZE)
  317. typedef struct {
  318. char *LogLevel;
  319. char *RouterFile;
  320. char *PrivateKeyFile;
  321. double CoinWeight;
  322. int Daemon;
  323. int ORPort;
  324. int OPPort;
  325. int APPort;
  326. int DirPort;
  327. int MaxConn;
  328. int TrafficShaping;
  329. int LinkPadding;
  330. int DirRebuildPeriod;
  331. int DirFetchPeriod;
  332. int KeepalivePeriod;
  333. int MaxOnionsPending;
  334. int Role;
  335. int loglevel;
  336. } or_options_t;
  337. /* all the function prototypes go here */
  338. /********************************* buffers.c ***************************/
  339. int buf_new(char **buf, int *buflen, int *buf_datalen);
  340. void buf_free(char *buf);
  341. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof);
  342. /* grab from s, put onto buf, return how many bytes read */
  343. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
  344. /* push from buf onto s
  345. * then memmove to front of buf
  346. * return -1 or how many bytes remain on the buf */
  347. int write_to_buf(char *string, int string_len,
  348. char **buf, int *buflen, int *buf_datalen);
  349. /* append string to buf (growing as needed, return -1 if "too big")
  350. * return total number of bytes on the buf
  351. */
  352. int fetch_from_buf(char *string, int string_len,
  353. char **buf, int *buflen, int *buf_datalen);
  354. /* if there is string_len bytes in buf, write them onto string,
  355. * then memmove buf back (that is, remove them from buf)
  356. */
  357. #ifdef USE_ZLIB
  358. int compress_from_buf(char *string, int string_len,
  359. char **buf_in, int *buflen_in, int *buf_datalen_in,
  360. z_stream *zstream, int flush);
  361. /* read and compress as many characters as possible from buf, writing up to
  362. * string_len of them onto string, then memmove buf back. Return number of
  363. * characters written.
  364. */
  365. int decompress_buf_to_buf(char **buf_in, int *buflen_in, int *buf_datalen_in,
  366. char **buf_out, int *buflen_out, int *buf_datalen_out,
  367. z_stream *zstream, int flush);
  368. /* XXX document this NM
  369. */
  370. #endif
  371. int find_on_inbuf(char *string, int string_len,
  372. char *buf, int buf_datalen);
  373. /* find first instance of needle 'string' on haystack 'buf'. return how
  374. * many bytes from the beginning of buf to the end of string.
  375. * If it's not there, return -1.
  376. */
  377. /********************************* cell.c ***************************/
  378. int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
  379. /********************************* circuit.c ***************************/
  380. void circuit_add(circuit_t *circ);
  381. void circuit_remove(circuit_t *circ);
  382. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  383. /* internal */
  384. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  385. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  386. circuit_t *circuit_get_by_conn(connection_t *conn);
  387. circuit_t *circuit_get_by_edge_type(char edge_type);
  388. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
  389. int circuit_deliver_data_cell_from_edge(cell_t *cell, circuit_t *circ, char edge_type);
  390. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, int crypt_type);
  391. int circuit_crypt(circuit_t *circ, char *in, int inlen, char crypt_type);
  392. void circuit_resume_edge_reading(circuit_t *circ, int edge_type);
  393. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type);
  394. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type);
  395. int circuit_init(circuit_t *circ, int aci_type);
  396. void circuit_free(circuit_t *circ);
  397. void circuit_free_cpath(crypt_path_t **cpath, int cpathlen);
  398. void circuit_close(circuit_t *circ);
  399. void circuit_about_to_close_connection(connection_t *conn);
  400. /* flush and send destroys for all circuits using conn */
  401. void circuit_dump_by_conn(connection_t *conn);
  402. /********************************* command.c ***************************/
  403. void command_process_cell(cell_t *cell, connection_t *conn);
  404. void command_process_create_cell(cell_t *cell, connection_t *conn);
  405. void command_process_sendme_cell(cell_t *cell, connection_t *conn);
  406. void command_process_data_cell(cell_t *cell, connection_t *conn);
  407. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  408. void command_process_connected_cell(cell_t *cell, connection_t *conn);
  409. /********************************* config.c ***************************/
  410. const char *basename(const char *filename);
  411. /* open configuration file for reading */
  412. FILE *config_open(const unsigned char *filename);
  413. /* close configuration file */
  414. int config_close(FILE *f);
  415. struct config_line *config_get_commandlines(int argc, char **argv);
  416. /* parse the config file and strdup into key/value strings. Return list.
  417. * * * Warn and ignore mangled lines. */
  418. struct config_line *config_get_lines(FILE *f);
  419. void config_free_lines(struct config_line *front);
  420. int config_compare(struct config_line *c, char *key, int type, void *arg);
  421. void config_assign(or_options_t *options, struct config_line *list);
  422. /* return 0 if success, <0 if failure. */
  423. int getconfig(int argc, char **argv, or_options_t *options);
  424. /********************************* connection.c ***************************/
  425. int tv_cmp(struct timeval *a, struct timeval *b);
  426. connection_t *connection_new(int type);
  427. void connection_free(connection_t *conn);
  428. int connection_create_listener(struct sockaddr_in *bindaddr, int type);
  429. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  430. /* start all connections that should be up but aren't */
  431. int retry_all_connections(int role, uint16_t or_listenport,
  432. uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
  433. int connection_read_to_buf(connection_t *conn);
  434. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  435. #ifdef USE_ZLIB
  436. int connection_compress_from_buf(char *string, int len, connection_t *conn,
  437. int flush);
  438. int connection_decompress_to_buf(char *string, int len, connection_t *conn,
  439. int flush);
  440. #endif
  441. int connection_outbuf_too_full(connection_t *conn);
  442. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  443. int connection_wants_to_flush(connection_t *conn);
  444. int connection_flush_buf(connection_t *conn);
  445. int connection_write_to_buf(char *string, int len, connection_t *conn);
  446. void connection_send_cell(connection_t *conn);
  447. int connection_receiver_bucket_should_increase(connection_t *conn);
  448. void connection_increment_receiver_bucket (connection_t *conn);
  449. void connection_increment_send_timeval(connection_t *conn);
  450. void connection_init_timeval(connection_t *conn);
  451. int connection_speaks_cells(connection_t *conn);
  452. int connection_is_listener(connection_t *conn);
  453. int connection_state_is_open(connection_t *conn);
  454. int connection_send_destroy(aci_t aci, connection_t *conn);
  455. int connection_send_connected(aci_t aci, connection_t *conn);
  456. int connection_encrypt_cell(char *cellp, connection_t *conn);
  457. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
  458. int connection_process_inbuf(connection_t *conn);
  459. int connection_package_raw_inbuf(connection_t *conn);
  460. int connection_process_cell_from_inbuf(connection_t *conn);
  461. int connection_consider_sending_sendme(connection_t *conn, int edge_type);
  462. int connection_finished_flushing(connection_t *conn);
  463. /********************************* connection_ap.c ****************************/
  464. int connection_ap_process_inbuf(connection_t *conn);
  465. int ap_handshake_process_socks(connection_t *conn);
  466. int ap_handshake_create_onion(connection_t *conn);
  467. int ap_handshake_establish_circuit(connection_t *conn, unsigned int *route, int routelen, char *onion,
  468. int onionlen, crypt_path_t **cpath);
  469. void ap_handshake_n_conn_open(connection_t *or_conn);
  470. int ap_handshake_send_onion(connection_t *ap_conn, connection_t *or_conn, circuit_t *circ);
  471. int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  472. int ap_handshake_socks_reply(connection_t *conn, char result);
  473. int connection_ap_send_connected(connection_t *conn);
  474. int connection_ap_process_data_cell(cell_t *cell, circuit_t *circ);
  475. int connection_ap_finished_flushing(connection_t *conn);
  476. int connection_ap_create_listener(struct sockaddr_in *bindaddr);
  477. int connection_ap_handle_listener_read(connection_t *conn);
  478. /********************************* connection_exit.c ***************************/
  479. int connection_exit_process_inbuf(connection_t *conn);
  480. int connection_exit_package_inbuf(connection_t *conn);
  481. int connection_exit_send_connected(connection_t *conn);
  482. int connection_exit_process_data_cell(cell_t *cell, circuit_t *circ);
  483. int connection_exit_finished_flushing(connection_t *conn);
  484. int connection_exit_connect(connection_t *conn);
  485. /********************************* connection_op.c ***************************/
  486. int op_handshake_process_keys(connection_t *conn);
  487. int connection_op_process_inbuf(connection_t *conn);
  488. int connection_op_finished_flushing(connection_t *conn);
  489. int connection_op_create_listener(struct sockaddr_in *bindaddr);
  490. int connection_op_handle_listener_read(connection_t *conn);
  491. /********************************* connection_or.c ***************************/
  492. int connection_or_process_inbuf(connection_t *conn);
  493. int connection_or_finished_flushing(connection_t *conn);
  494. void conn_or_init_crypto(connection_t *conn);
  495. int or_handshake_op_send_keys(connection_t *conn);
  496. int or_handshake_op_finished_sending_keys(connection_t *conn);
  497. int or_handshake_client_process_auth(connection_t *conn);
  498. int or_handshake_client_send_auth(connection_t *conn);
  499. int or_handshake_server_process_auth(connection_t *conn);
  500. int or_handshake_server_process_nonce(connection_t *conn);
  501. connection_t *connect_to_router_as_or(routerinfo_t *router);
  502. connection_t *connection_or_connect_as_or(routerinfo_t *router);
  503. connection_t *connection_or_connect_as_op(routerinfo_t *router);
  504. int connection_or_create_listener(struct sockaddr_in *bindaddr);
  505. int connection_or_handle_listener_read(connection_t *conn);
  506. /********************************* directory.c ***************************/
  507. void directory_initiate_fetch(routerinfo_t *router);
  508. int directory_send_command(connection_t *conn);
  509. void directory_set_dirty(void);
  510. void directory_rebuild(void);
  511. int connection_dir_process_inbuf(connection_t *conn);
  512. int directory_handle_command(connection_t *conn);
  513. int directory_handle_reading(connection_t *conn);
  514. int connection_dir_finished_flushing(connection_t *conn);
  515. int connection_dir_create_listener(struct sockaddr_in *bindaddr);
  516. int connection_dir_handle_listener_read(connection_t *conn);
  517. /********************************* dns.c ***************************/
  518. int connection_dns_finished_flushing(connection_t *conn);
  519. int connection_dns_process_inbuf(connection_t *conn);
  520. void init_cache_tree(void);
  521. int dns_resolve(connection_t *exitconn);
  522. int dns_master_start(void);
  523. /********************************* main.c ***************************/
  524. void setprivatekey(crypto_pk_env_t *k);
  525. crypto_pk_env_t *getprivatekey(void);
  526. int connection_add(connection_t *conn);
  527. int connection_remove(connection_t *conn);
  528. void connection_set_poll_socket(connection_t *conn);
  529. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  530. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  531. connection_t *connection_get_by_type(int type);
  532. void connection_watch_events(connection_t *conn, short events);
  533. void connection_stop_reading(connection_t *conn);
  534. void connection_start_reading(connection_t *conn);
  535. void connection_stop_writing(connection_t *conn);
  536. void connection_start_writing(connection_t *conn);
  537. void check_conn_read(int i);
  538. void check_conn_marked(int i);
  539. void check_conn_write(int i);
  540. int prepare_for_poll(int *timeout);
  541. int do_main_loop(void);
  542. void dumpstats(void);
  543. void dump_directory_to_string(char *s, int maxlen);
  544. int main(int argc, char *argv[]);
  545. /********************************* onion.c ***************************/
  546. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  547. uint32_t remote_addr, uint16_t remote_port);
  548. int onion_pending_add(circuit_t *circ);
  549. int onion_pending_check(void);
  550. void onion_pending_process_one(void);
  551. void onion_pending_remove(circuit_t *circ);
  552. struct data_queue_t *data_queue_add(struct data_queue_t *list, cell_t *cell);
  553. void onion_pending_data_add(circuit_t *circ, cell_t *cell);
  554. /* uses a weighted coin with weight cw to choose a route length */
  555. int chooselen(double cw);
  556. /* returns an array of pointers to routent that define a new route through the OR network
  557. * int cw is the coin weight to use when choosing the route
  558. * order of routers is from last to first
  559. */
  560. unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
  561. /* create a cipher by onion cipher type. */
  562. crypto_cipher_env_t *create_onion_cipher(int cipher_type, char *key, char *iv, int encrypt_mode);
  563. /* creates a new onion from route, stores it and its length into bufp and lenp respectively */
  564. unsigned char *create_onion(routerinfo_t **rarray, int rarray_len, unsigned int *route, int routelen, int *len, crypt_path_t **cpath);
  565. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  566. * DES OFB with the key as defined in the outter layer */
  567. int encrypt_onion(unsigned char *onion, uint32_t onionlen, crypto_pk_env_t *pkey);
  568. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  569. int decrypt_onion(unsigned char *onion, uint32_t onionlen, crypto_pk_env_t *prkey);
  570. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  571. void pad_onion(unsigned char *onion, uint32_t onionlen, int n);
  572. void init_tracked_tree(void);
  573. /********************************* routers.c ***************************/
  574. int learn_my_address(struct sockaddr_in *me);
  575. void router_retry_connections(void);
  576. routerinfo_t *router_pick_directory_server(void);
  577. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  578. unsigned int *router_new_route(int *routelen);
  579. unsigned char *router_create_onion(unsigned int *route, int routelen, int *len, crypt_path_t **cpath);
  580. int router_is_me(uint32_t addr, uint16_t port);
  581. routerinfo_t *router_get_first_in_route(unsigned int *route, int routelen);
  582. void router_forget_router(uint32_t addr, uint16_t port);
  583. int router_get_list_from_file(char *routerfile);
  584. int router_get_list_from_string(char *s);
  585. #endif