or.h 26 KB

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