or.h 28 KB

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