or.h 25 KB

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