or.h 27 KB

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