or.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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> /* Must be included before sys/stat.h for Ultrix */
  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. uint8_t wants_to_write; /* should we start writing again once
  246. * the bandwidth throttler allows reads?
  247. */
  248. int s; /* our socket */
  249. int poll_index; /* index of this conn into the poll_array */
  250. int marked_for_close; /* should we close this conn on the next
  251. * iteration of the main loop?
  252. */
  253. char *inbuf;
  254. int inbuflen; /* how many bytes are alloc'ed for inbuf? */
  255. int inbuf_datalen; /* how many bytes of data are on inbuf? */
  256. int inbuf_reached_eof; /* did read() return 0 on this conn? */
  257. long timestamp_lastread; /* when was the last time poll() said we could read? */
  258. char *outbuf;
  259. int outbuflen; /* how many bytes are allocated for the outbuf? */
  260. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  261. int outbuf_datalen; /* how much data is there total on the outbuf? */
  262. long timestamp_lastwritten; /* when was the last time poll() said we could write? */
  263. long timestamp_created; /* when was this connection_t created? */
  264. uint32_t bandwidth; /* connection bandwidth. Set to -1 for non-OR conns. */
  265. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  266. * add 'bandwidth' to this, capping it at 10*bandwidth.
  267. * Set to -1 for non-OR conns.
  268. */
  269. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  270. uint16_t port; /* if non-zero, they identify the guy on the other end
  271. * of the connection. */
  272. char *address; /* FQDN (or IP) of the guy on the other end.
  273. * strdup into this, because free_connection frees it
  274. */
  275. crypto_pk_env_t *pkey; /* public RSA key for the other side */
  276. #ifndef TOR_TLS
  277. /* Used only by OR connections: */
  278. /* link encryption */
  279. crypto_cipher_env_t *f_crypto;
  280. crypto_cipher_env_t *b_crypto;
  281. char nonce[8];
  282. #endif
  283. /* Used only by edge connections: */
  284. char stream_id[STREAM_ID_SIZE];
  285. struct connection_t *next_stream; /* points to the next stream at this edge, if any */
  286. struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
  287. int package_window; /* how many more relay cells can i send into the circuit? */
  288. int deliver_window; /* how many more relay cells can end at me? */
  289. int done_sending; /* for half-open connections; not used currently */
  290. int done_receiving;
  291. /* Used only by AP connections: */
  292. char socks_version; /* what socks version are they speaking at me? */
  293. char read_username; /* have i read the username yet? */
  294. char *dest_addr; /* what address and port are this stream's destination? */
  295. uint16_t dest_port; /* host order */
  296. /* Used only by worker connections: */
  297. int num_processed; /* statistics kept by dns worker */
  298. struct circuit_t *circ; /* by cpu worker to know who he's working for */
  299. };
  300. typedef struct connection_t connection_t;
  301. #define EXIT_POLICY_ACCEPT 1
  302. #define EXIT_POLICY_REJECT 2
  303. struct exit_policy_t {
  304. char policy_type;
  305. char *string;
  306. char *address;
  307. char *port;
  308. struct exit_policy_t *next;
  309. };
  310. /* config stuff we know about the other ORs in the network */
  311. typedef struct {
  312. char *address;
  313. uint32_t addr; /* all host order */
  314. uint16_t or_port;
  315. uint16_t ap_port;
  316. uint16_t dir_port;
  317. crypto_pk_env_t *pkey; /* public RSA key */
  318. crypto_pk_env_t *signing_pkey; /* May be null */
  319. /* link info */
  320. uint32_t bandwidth;
  321. struct exit_policy_t *exit_policy;
  322. } routerinfo_t;
  323. #define MAX_ROUTERS_IN_DIR 1024
  324. typedef struct {
  325. routerinfo_t **routers;
  326. int n_routers;
  327. char *software_versions;
  328. } directory_t;
  329. struct crypt_path_t {
  330. /* crypto environments */
  331. crypto_cipher_env_t *f_crypto;
  332. crypto_cipher_env_t *b_crypto;
  333. crypto_dh_env_t *handshake_state;
  334. uint32_t addr;
  335. uint16_t port;
  336. uint8_t state;
  337. #define CPATH_STATE_CLOSED 0
  338. #define CPATH_STATE_AWAITING_KEYS 1
  339. #define CPATH_STATE_OPEN 2
  340. struct crypt_path_t *next;
  341. struct crypt_path_t *prev; /* doubly linked list */
  342. int package_window;
  343. int deliver_window;
  344. };
  345. #define DH_KEY_LEN CRYPTO_DH_SIZE
  346. #define DH_ONIONSKIN_LEN DH_KEY_LEN+16
  347. typedef struct crypt_path_t crypt_path_t;
  348. /* struct for a path (circuit) through the network */
  349. struct circuit_t {
  350. uint32_t n_addr;
  351. uint16_t n_port;
  352. connection_t *p_conn;
  353. connection_t *n_conn; /* for the OR conn, if there is one */
  354. connection_t *p_streams;
  355. connection_t *n_streams;
  356. int package_window;
  357. int deliver_window;
  358. aci_t p_aci; /* connection identifiers */
  359. aci_t n_aci;
  360. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  361. crypto_cipher_env_t *n_crypto;
  362. crypt_path_t *cpath;
  363. char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
  364. long timestamp_created;
  365. uint8_t dirty; /* whether this circuit has been used yet */
  366. uint8_t state;
  367. // unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  368. // uint32_t onionlen; /* total onion length */
  369. // uint32_t recvlen; /* length of the onion so far */
  370. void *next;
  371. };
  372. typedef struct circuit_t circuit_t;
  373. struct onion_queue_t {
  374. circuit_t *circ;
  375. struct onion_queue_t *next;
  376. };
  377. typedef struct {
  378. char *LogLevel;
  379. char *RouterFile;
  380. char *SigningPrivateKeyFile;
  381. char *PrivateKeyFile;
  382. double CoinWeight;
  383. int Daemon;
  384. int ORPort;
  385. int APPort;
  386. int DirPort;
  387. int MaxConn;
  388. int OnionRouter;
  389. int TrafficShaping;
  390. int LinkPadding;
  391. int IgnoreVersion;
  392. int DirRebuildPeriod;
  393. int DirFetchPeriod;
  394. int KeepalivePeriod;
  395. int MaxOnionsPending;
  396. int NewCircuitPeriod;
  397. int TotalBandwidth;
  398. int NumCpus;
  399. int Role;
  400. int loglevel;
  401. } or_options_t;
  402. /* all the function prototypes go here */
  403. /********************************* buffers.c ***************************/
  404. int buf_new(char **buf, int *buflen, int *buf_datalen);
  405. void buf_free(char *buf);
  406. int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof);
  407. /* grab from s, put onto buf, return how many bytes read */
  408. int read_to_buf_tls(tor_tls *tls, int at_most, char **buf, int *buflen, int *buf_datalen);
  409. /* grab from tls, put onto buf, return how many bytes read or a TLS
  410. * status (same status codes as tor_tls_read) */
  411. int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
  412. /* push from buf onto s
  413. * then memmove to front of buf
  414. * return -1 or how many bytes remain on the buf */
  415. int flush_buf_tls(tor_tls *tls, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
  416. /* As flush_buf, but returns number of bytes written or TLS status
  417. * (same status codes as tor_tls_write) */
  418. int write_to_buf(char *string, int string_len,
  419. char **buf, int *buflen, int *buf_datalen);
  420. /* append string to buf (growing as needed, return -1 if "too big")
  421. * return total number of bytes on the buf
  422. */
  423. int fetch_from_buf(char *string, int string_len,
  424. char **buf, int *buflen, int *buf_datalen);
  425. /* if there is string_len bytes in buf, write them onto string,
  426. * then memmove buf back (that is, remove them from buf)
  427. */
  428. int find_on_inbuf(char *string, int string_len,
  429. char *buf, int buf_datalen);
  430. /* find first instance of needle 'string' on haystack 'buf'. return how
  431. * many bytes from the beginning of buf to the end of string.
  432. * If it's not there, return -1.
  433. */
  434. /********************************* cell.c ***************************/
  435. int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
  436. /********************************* circuit.c ***************************/
  437. void circuit_add(circuit_t *circ);
  438. void circuit_remove(circuit_t *circ);
  439. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  440. /* internal */
  441. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  442. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  443. circuit_t *circuit_get_by_conn(connection_t *conn);
  444. circuit_t *circuit_get_newest_ap(void);
  445. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
  446. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  447. int cell_direction, crypt_path_t *layer_hint);
  448. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  449. crypt_path_t **layer_hint, char *recognized, connection_t **conn);
  450. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
  451. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  452. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  453. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  454. void circuit_free(circuit_t *circ);
  455. void circuit_free_cpath(crypt_path_t *cpath);
  456. void circuit_free_cpath_node(crypt_path_t *victim);
  457. void circuit_close(circuit_t *circ);
  458. void circuit_about_to_close_connection(connection_t *conn);
  459. /* flush and send destroys for all circuits using conn */
  460. void circuit_dump_by_conn(connection_t *conn);
  461. void circuit_expire_unused_circuits(void);
  462. void circuit_launch_new(int failure_status);
  463. int circuit_establish_circuit(void);
  464. void circuit_n_conn_open(connection_t *or_conn);
  465. int circuit_send_next_onion_skin(circuit_t *circ);
  466. int circuit_extend(cell_t *cell, circuit_t *circ);
  467. int circuit_finish_handshake(circuit_t *circ, char *reply);
  468. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  469. /********************************* command.c ***************************/
  470. void command_process_cell(cell_t *cell, connection_t *conn);
  471. void command_process_create_cell(cell_t *cell, connection_t *conn);
  472. void command_process_created_cell(cell_t *cell, connection_t *conn);
  473. void command_process_sendme_cell(cell_t *cell, connection_t *conn);
  474. void command_process_relay_cell(cell_t *cell, connection_t *conn);
  475. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  476. void command_process_connected_cell(cell_t *cell, connection_t *conn);
  477. /********************************* config.c ***************************/
  478. /* open configuration file for reading */
  479. FILE *config_open(const unsigned char *filename);
  480. /* close configuration file */
  481. int config_close(FILE *f);
  482. struct config_line *config_get_commandlines(int argc, char **argv);
  483. /* parse the config file and strdup into key/value strings. Return list.
  484. * * * Warn and ignore mangled lines. */
  485. struct config_line *config_get_lines(FILE *f);
  486. void config_free_lines(struct config_line *front);
  487. int config_compare(struct config_line *c, char *key, int type, void *arg);
  488. void config_assign(or_options_t *options, struct config_line *list);
  489. /* return 0 if success, <0 if failure. */
  490. int getconfig(int argc, char **argv, or_options_t *options);
  491. /********************************* connection.c ***************************/
  492. int tv_cmp(struct timeval *a, struct timeval *b);
  493. connection_t *connection_new(int type);
  494. void connection_free(connection_t *conn);
  495. int connection_create_listener(struct sockaddr_in *bindaddr, int type);
  496. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  497. /* start all connections that should be up but aren't */
  498. int retry_all_connections(uint16_t or_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
  499. int connection_handle_read(connection_t *conn);
  500. int connection_read_to_buf(connection_t *conn);
  501. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  502. int connection_outbuf_too_full(connection_t *conn);
  503. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  504. int connection_wants_to_flush(connection_t *conn);
  505. int connection_flush_buf(connection_t *conn);
  506. int connection_handle_write(connection_t *conn);
  507. int connection_write_to_buf(char *string, int len, connection_t *conn);
  508. void connection_send_cell(connection_t *conn);
  509. int connection_receiver_bucket_should_increase(connection_t *conn);
  510. void connection_increment_send_timeval(connection_t *conn);
  511. void connection_init_timeval(connection_t *conn);
  512. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  513. int connection_is_listener(connection_t *conn);
  514. int connection_state_is_open(connection_t *conn);
  515. int connection_send_destroy(aci_t aci, connection_t *conn);
  516. int connection_send_connected(aci_t aci, connection_t *conn);
  517. #ifndef USE_TLS
  518. int connection_encrypt_cell(char *cellp, connection_t *conn);
  519. #endif
  520. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
  521. int connection_process_inbuf(connection_t *conn);
  522. int connection_package_raw_inbuf(connection_t *conn);
  523. int connection_process_cell_from_inbuf(connection_t *conn);
  524. int connection_consider_sending_sendme(connection_t *conn, int edge_type);
  525. int connection_finished_flushing(connection_t *conn);
  526. void cell_pack(char *dest, const cell_t *src);
  527. void cell_unpack(cell_t *dest, const char *src);
  528. /********************************* connection_ap.c ****************************/
  529. int ap_handshake_process_socks(connection_t *conn);
  530. int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  531. int ap_handshake_socks_reply(connection_t *conn, char result);
  532. int connection_ap_create_listener(struct sockaddr_in *bindaddr);
  533. int connection_ap_handle_listener_read(connection_t *conn);
  534. /********************************* connection_edge.c ***************************/
  535. int connection_edge_process_inbuf(connection_t *conn);
  536. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command);
  537. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int edge_type, crypt_path_t *layer_hint);
  538. int connection_edge_finished_flushing(connection_t *conn);
  539. /********************************* connection_exit.c ***************************/
  540. int connection_exit_send_connected(connection_t *conn);
  541. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  542. int connection_exit_connect(connection_t *conn);
  543. /********************************* connection_op.c ***************************/
  544. int op_handshake_process_keys(connection_t *conn);
  545. int connection_op_process_inbuf(connection_t *conn);
  546. int connection_op_finished_flushing(connection_t *conn);
  547. int connection_op_create_listener(struct sockaddr_in *bindaddr);
  548. int connection_op_handle_listener_read(connection_t *conn);
  549. /********************************* connection_or.c ***************************/
  550. int connection_or_process_inbuf(connection_t *conn);
  551. int connection_or_finished_flushing(connection_t *conn);
  552. connection_t *connection_or_connect(routerinfo_t *router);
  553. int connection_or_create_listener(struct sockaddr_in *bindaddr);
  554. int connection_or_handle_listener_read(connection_t *conn);
  555. /********************************* cpuworker.c *****************************/
  556. void cpu_init(void);
  557. int connection_cpu_finished_flushing(connection_t *conn);
  558. int connection_cpu_process_inbuf(connection_t *conn);
  559. int cpuworker_main(void *data);
  560. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  561. void *task);
  562. /********************************* directory.c ***************************/
  563. void directory_initiate_fetch(routerinfo_t *router);
  564. int directory_send_command(connection_t *conn);
  565. void directory_set_dirty(void);
  566. void directory_rebuild(void);
  567. int connection_dir_process_inbuf(connection_t *conn);
  568. int directory_handle_command(connection_t *conn);
  569. int directory_handle_reading(connection_t *conn);
  570. int connection_dir_finished_flushing(connection_t *conn);
  571. int connection_dir_create_listener(struct sockaddr_in *bindaddr);
  572. int connection_dir_handle_listener_read(connection_t *conn);
  573. /********************************* dns.c ***************************/
  574. void dns_init(void);
  575. int connection_dns_finished_flushing(connection_t *conn);
  576. int connection_dns_process_inbuf(connection_t *conn);
  577. void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
  578. int dns_resolve(connection_t *exitconn);
  579. /********************************* main.c ***************************/
  580. void set_privatekey(crypto_pk_env_t *k);
  581. crypto_pk_env_t *get_privatekey(void);
  582. void set_signing_privatekey(crypto_pk_env_t *k);
  583. crypto_pk_env_t *get_signing_privatekey(void);
  584. int connection_add(connection_t *conn);
  585. int connection_remove(connection_t *conn);
  586. void connection_set_poll_socket(connection_t *conn);
  587. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  588. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  589. connection_t *connection_get_by_type(int type);
  590. connection_t *connection_get_by_type_state(int type, int state);
  591. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  592. void connection_watch_events(connection_t *conn, short events);
  593. int connection_is_reading(connection_t *conn);
  594. void connection_stop_reading(connection_t *conn);
  595. void connection_start_reading(connection_t *conn);
  596. void connection_stop_writing(connection_t *conn);
  597. void connection_start_writing(connection_t *conn);
  598. int dump_signed_directory_to_string(char *s, int maxlen,
  599. crypto_pk_env_t *private_key);
  600. /* Exported for debugging */
  601. int dump_signed_directory_to_string_impl(char *s, int maxlen,
  602. directory_t *dir,
  603. crypto_pk_env_t *private_key);
  604. int main(int argc, char *argv[]);
  605. /********************************* onion.c ***************************/
  606. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  607. uint32_t remote_addr, uint16_t remote_port);
  608. int onion_pending_add(circuit_t *circ);
  609. circuit_t *onion_next_task(void);
  610. void onion_pending_remove(circuit_t *circ);
  611. int onionskin_process(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  612. /* uses a weighted coin with weight cw to choose a route length */
  613. int chooselen(double cw);
  614. /* returns an array of pointers to routent that define a new route through the OR network
  615. * int cw is the coin weight to use when choosing the route
  616. * order of routers is from last to first
  617. */
  618. unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
  619. crypt_path_t *onion_generate_cpath(routerinfo_t **firsthop);
  620. int onion_skin_create(crypto_pk_env_t *router_key,
  621. crypto_dh_env_t **handshake_state_out,
  622. char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
  623. int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
  624. crypto_pk_env_t *private_key,
  625. char *handshake_reply_out, /* DH_KEY_LEN bytes long */
  626. char *key_out,
  627. int key_out_len);
  628. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  629. char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
  630. char *key_out,
  631. int key_out_len);
  632. /********************************* routers.c ***************************/
  633. int learn_my_address(struct sockaddr_in *me);
  634. void router_retry_connections(void);
  635. routerinfo_t *router_pick_directory_server(void);
  636. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  637. void router_get_directory(directory_t **pdirectory);
  638. int router_is_me(uint32_t addr, uint16_t port);
  639. void router_forget_router(uint32_t addr, uint16_t port);
  640. int router_get_list_from_file(char *routerfile);
  641. int router_resolve(routerinfo_t *router);
  642. int router_resolve_directory(directory_t *dir);
  643. /* Reads a list of known routers, unsigned. */
  644. int router_get_list_from_string(char *s);
  645. /* Exported for debugging */
  646. int router_get_list_from_string_impl(char *s, directory_t **dest);
  647. /* Reads a signed directory. */
  648. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey);
  649. /* Exported or debugging */
  650. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  651. crypto_pk_env_t *pkey);
  652. routerinfo_t *router_get_entry_from_string(char **s);
  653. int router_compare_to_exit_policy(connection_t *conn);
  654. void routerinfo_free(routerinfo_t *router);
  655. #endif
  656. /*
  657. Local Variables:
  658. mode:c
  659. indent-tabs-mode:nil
  660. c-basic-offset:2
  661. End:
  662. */