or.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /* Copyright 2001,2002,2003 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_PWD_H
  73. #include <pwd.h>
  74. #endif
  75. #ifdef HAVE_GRP_H
  76. #include <grp.h>
  77. #endif
  78. #ifdef HAVE_WINSOCK_H
  79. #include <winsock.h>
  80. #endif
  81. #if _MSC_VER > 1300
  82. #include <winsock2.h>
  83. #include <ws2tcpip.h>
  84. #elif defined(_MSC_VER)
  85. #include <winsock.h>
  86. #endif
  87. #ifdef MS_WINDOWS
  88. #include <io.h>
  89. #include <process.h>
  90. #define WIN32_LEAN_AND_MEAN
  91. #include <windows.h>
  92. #define snprintf _snprintf
  93. #endif
  94. #include "../common/crypto.h"
  95. #include "../common/tortls.h"
  96. #include "../common/log.h"
  97. #include "../common/util.h"
  98. #define MAXCONNECTIONS 1000 /* upper bound on max connections.
  99. can be lowered by config file */
  100. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  101. #define MAX_NICKNAME_LEN 32
  102. #define MAX_DIR_SIZE 50000 /* XXX, big enough? */
  103. #define CIRC_ID_TYPE_LOWER 0
  104. #define CIRC_ID_TYPE_HIGHER 1
  105. #define _CONN_TYPE_MIN 3
  106. #define CONN_TYPE_OR_LISTENER 3
  107. #define CONN_TYPE_OR 4
  108. #define CONN_TYPE_EXIT 5
  109. #define CONN_TYPE_AP_LISTENER 6
  110. #define CONN_TYPE_AP 7
  111. #define CONN_TYPE_DIR_LISTENER 8
  112. #define CONN_TYPE_DIR 9
  113. #define CONN_TYPE_DNSWORKER 10
  114. #define CONN_TYPE_CPUWORKER 11
  115. #define _CONN_TYPE_MAX 11
  116. #define LISTENER_STATE_READY 0
  117. #define _DNSWORKER_STATE_MIN 1
  118. #define DNSWORKER_STATE_IDLE 1
  119. #define DNSWORKER_STATE_BUSY 2
  120. #define _DNSWORKER_STATE_MAX 2
  121. #define _CPUWORKER_STATE_MIN 1
  122. #define CPUWORKER_STATE_IDLE 1
  123. #define CPUWORKER_STATE_BUSY_ONION 2
  124. #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
  125. #define _CPUWORKER_STATE_MAX 3
  126. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  127. #define _OR_CONN_STATE_MIN 1
  128. #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
  129. #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
  130. #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
  131. #define _OR_CONN_STATE_MAX 3
  132. #define _EXIT_CONN_STATE_MIN 1
  133. #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
  134. #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
  135. #define EXIT_CONN_STATE_OPEN 3
  136. #define _EXIT_CONN_STATE_MAX 3
  137. #if 0
  138. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  139. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  140. #endif
  141. /* the AP state values must be disjoint from the EXIT state values */
  142. #define _AP_CONN_STATE_MIN 4
  143. #define AP_CONN_STATE_SOCKS_WAIT 4
  144. #define AP_CONN_STATE_CIRCUIT_WAIT 5
  145. #define AP_CONN_STATE_OPEN 6
  146. #define _AP_CONN_STATE_MAX 6
  147. #define _DIR_CONN_STATE_MIN 1
  148. #define DIR_CONN_STATE_CONNECTING_FETCH 1
  149. #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
  150. #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
  151. #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
  152. #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
  153. #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
  154. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
  155. #define DIR_CONN_STATE_SERVER_WRITING 8
  156. #define _DIR_CONN_STATE_MAX 8
  157. #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
  158. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
  159. #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
  160. #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
  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_COMMAND_DROP 10
  171. #define RELAY_HEADER_SIZE 8
  172. #define END_STREAM_REASON_MISC 1
  173. #define END_STREAM_REASON_RESOLVEFAILED 2
  174. #define END_STREAM_REASON_CONNECTFAILED 3
  175. #define END_STREAM_REASON_EXITPOLICY 4
  176. #define END_STREAM_REASON_DESTROY 5
  177. #define END_STREAM_REASON_DONE 6
  178. /* default cipher function */
  179. #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
  180. /* Used to en/decrypt onion skins */
  181. #define ONION_CIPHER DEFAULT_CIPHER
  182. /* Used to en/decrypt RELAY cells */
  183. #define CIRCUIT_CIPHER DEFAULT_CIPHER
  184. #define CELL_DIRECTION_IN 1
  185. #define CELL_DIRECTION_OUT 2
  186. #define EDGE_EXIT CONN_TYPE_EXIT
  187. #define EDGE_AP CONN_TYPE_AP
  188. #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
  189. #define CIRCWINDOW_START 1000
  190. #define CIRCWINDOW_INCREMENT 100
  191. #define STREAMWINDOW_START 500
  192. #define STREAMWINDOW_INCREMENT 50
  193. /* cell commands */
  194. #define CELL_PADDING 0
  195. #define CELL_CREATE 1
  196. #define CELL_CREATED 2
  197. #define CELL_RELAY 3
  198. #define CELL_DESTROY 4
  199. #define CELL_PAYLOAD_SIZE 248
  200. #define CELL_NETWORK_SIZE 256
  201. /* legal characters in a filename */
  202. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  203. /* legal characters in a nickname */
  204. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  205. #define SOCKS4_NETWORK_LEN 8
  206. typedef uint16_t circ_id_t;
  207. /* cell definition */
  208. typedef struct {
  209. circ_id_t circ_id;
  210. unsigned char command;
  211. unsigned char length; /* of payload if relay cell */
  212. uint32_t seq; /* sequence number */
  213. unsigned char payload[CELL_PAYLOAD_SIZE];
  214. } cell_t;
  215. #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
  216. #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
  217. #define STREAM_ID_SIZE 7
  218. #define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
  219. #define CELL_RELAY_COMMAND_END_REASON(c) (*(uint8_t)((c).payload+1))
  220. #define ZERO_STREAM "\0\0\0\0\0\0\0\0"
  221. typedef struct buf_t buf_t;
  222. typedef struct socks_request_t socks_request_t;
  223. struct connection_t {
  224. uint8_t type;
  225. uint8_t state;
  226. uint8_t wants_to_read; /* should we start reading again once
  227. * the bandwidth throttler allows it?
  228. */
  229. uint8_t wants_to_write; /* should we start writing again once
  230. * the bandwidth throttler allows reads?
  231. */
  232. int s; /* our socket */
  233. int poll_index; /* index of this conn into the poll_array */
  234. int marked_for_close; /* should we close this conn on the next
  235. * iteration of the main loop?
  236. */
  237. buf_t *inbuf;
  238. int inbuf_reached_eof; /* did read() return 0 on this conn? */
  239. time_t timestamp_lastread; /* when was the last time poll() said we could read? */
  240. buf_t *outbuf;
  241. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  242. time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
  243. time_t timestamp_created; /* when was this connection_t created? */
  244. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  245. uint16_t port; /* if non-zero, they identify the guy on the other end
  246. * of the connection. */
  247. char *address; /* FQDN (or IP) of the guy on the other end.
  248. * strdup into this, because free_connection frees it
  249. */
  250. crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
  251. crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
  252. crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
  253. char *nickname;
  254. /* Used only by OR connections: */
  255. tor_tls *tls;
  256. circ_id_t next_circ_id; /* Which circ_id do we try to use next on this connection?
  257. * This is always in the range 0..1<<15-1.*/
  258. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  259. uint32_t bandwidth; /* connection bandwidth. */
  260. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  261. * add 'bandwidth' to this, capping it at 10*bandwidth.
  262. */
  263. /* Used only by edge connections: */
  264. char stream_id[STREAM_ID_SIZE];
  265. struct connection_t *next_stream; /* points to the next stream at this edge, if any */
  266. struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
  267. int package_window; /* how many more relay cells can i send into the circuit? */
  268. int deliver_window; /* how many more relay cells can end at me? */
  269. int done_sending; /* for half-open connections; not used currently */
  270. int done_receiving;
  271. char has_sent_end; /* for debugging: set once we've set the stream end,
  272. and check in circuit_about_to_close_connection() */
  273. /* Used only by AP connections */
  274. socks_request_t *socks_request;
  275. };
  276. typedef struct connection_t connection_t;
  277. #define EXIT_POLICY_ACCEPT 1
  278. #define EXIT_POLICY_REJECT 2
  279. struct exit_policy_t {
  280. char policy_type;
  281. char *string;
  282. uint32_t addr;
  283. uint32_t msk;
  284. uint16_t prt;
  285. struct exit_policy_t *next;
  286. };
  287. /* config stuff we know about the other ORs in the network */
  288. typedef struct {
  289. char *address;
  290. char *nickname;
  291. uint32_t addr; /* all host order */
  292. uint16_t or_port;
  293. uint16_t socks_port;
  294. uint16_t dir_port;
  295. time_t published_on;
  296. crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
  297. crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
  298. crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
  299. int is_running;
  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. time_t published_on;
  310. } directory_t;
  311. struct crypt_path_t {
  312. /* crypto environments */
  313. crypto_cipher_env_t *f_crypto;
  314. crypto_cipher_env_t *b_crypto;
  315. crypto_dh_env_t *handshake_state;
  316. uint32_t addr;
  317. uint16_t port;
  318. uint8_t state;
  319. #define CPATH_STATE_CLOSED 0
  320. #define CPATH_STATE_AWAITING_KEYS 1
  321. #define CPATH_STATE_OPEN 2
  322. struct crypt_path_t *next;
  323. struct crypt_path_t *prev; /* doubly linked list */
  324. int package_window;
  325. int deliver_window;
  326. };
  327. #define DH_KEY_LEN CRYPTO_DH_SIZE
  328. #define DH_ONIONSKIN_LEN DH_KEY_LEN+16
  329. typedef struct crypt_path_t crypt_path_t;
  330. typedef struct {
  331. int desired_path_len;
  332. char *chosen_exit; /* nickname of planned exit node */
  333. } cpath_build_state_t;
  334. /* struct for a path (circuit) through the network */
  335. struct circuit_t {
  336. uint32_t n_addr;
  337. uint16_t n_port;
  338. connection_t *p_conn;
  339. connection_t *n_conn; /* for the OR conn, if there is one */
  340. connection_t *p_streams;
  341. connection_t *n_streams;
  342. int package_window;
  343. int deliver_window;
  344. circ_id_t p_circ_id; /* circuit identifiers */
  345. circ_id_t n_circ_id;
  346. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  347. crypto_cipher_env_t *n_crypto;
  348. cpath_build_state_t *build_state;
  349. crypt_path_t *cpath;
  350. char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
  351. time_t timestamp_created;
  352. time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
  353. uint8_t state;
  354. void *next;
  355. };
  356. typedef struct circuit_t circuit_t;
  357. typedef struct {
  358. char *LogLevel;
  359. char *LogFile;
  360. char *DebugLogFile;
  361. char *DataDirectory;
  362. char *RouterFile;
  363. char *Nickname;
  364. char *Address;
  365. char *PidFile;
  366. char *ExitNodes;
  367. char *EntryNodes;
  368. char *ExitPolicy;
  369. char *SocksBindAddress;
  370. char *ORBindAddress;
  371. char *DirBindAddress;
  372. char *RecommendedVersions;
  373. char *User;
  374. char *Group;
  375. double PathlenCoinWeight;
  376. int ORPort;
  377. int SocksPort;
  378. int DirPort;
  379. int MaxConn;
  380. int TrafficShaping;
  381. int LinkPadding;
  382. int IgnoreVersion;
  383. int RunAsDaemon;
  384. int DirRebuildPeriod;
  385. int DirFetchPostPeriod;
  386. int KeepalivePeriod;
  387. int MaxOnionsPending;
  388. int NewCircuitPeriod;
  389. int TotalBandwidth;
  390. int NumCpus;
  391. int loglevel;
  392. } or_options_t;
  393. #define MAX_SOCKS_REPLY_LEN 256
  394. /* Not 256; addresses must fit in a begin cell. */
  395. #define MAX_SOCKS_ADDR_LEN 200
  396. struct socks_request_t {
  397. char socks_version;
  398. int replylen;
  399. char reply[MAX_SOCKS_REPLY_LEN];
  400. char address[MAX_SOCKS_ADDR_LEN];
  401. uint16_t port;
  402. };
  403. /* all the function prototypes go here */
  404. /********************************* buffers.c ***************************/
  405. int find_on_inbuf(char *string, int string_len, buf_t *buf);
  406. buf_t *buf_new();
  407. buf_t *buf_new_with_capacity(size_t size);
  408. void buf_free(buf_t *buf);
  409. size_t buf_datalen(const buf_t *buf);
  410. size_t buf_capacity(const buf_t *buf);
  411. const char *_buf_peek_raw_buffer(const buf_t *buf);
  412. int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
  413. int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
  414. int flush_buf(int s, buf_t *buf, int *buf_flushlen);
  415. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
  416. int write_to_buf(const char *string, int string_len, buf_t *buf);
  417. int fetch_from_buf(char *string, int string_len, buf_t *buf);
  418. int fetch_from_buf_http(buf_t *buf,
  419. char *headers_out, int max_headerlen,
  420. char *body_out, int max_bodylen);
  421. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  422. /********************************* circuit.c ***************************/
  423. void circuit_add(circuit_t *circ);
  424. void circuit_remove(circuit_t *circ);
  425. circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn);
  426. void circuit_free(circuit_t *circ);
  427. void circuit_free_cpath(crypt_path_t *cpath);
  428. circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn);
  429. circuit_t *circuit_get_by_conn(connection_t *conn);
  430. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
  431. void circuit_expire_building(void);
  432. int circuit_count_building(void);
  433. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  434. int cell_direction, crypt_path_t *layer_hint);
  435. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  436. crypt_path_t **layer_hint, char *recognized, connection_t **conn);
  437. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
  438. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  439. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  440. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  441. void circuit_close(circuit_t *circ);
  442. void circuit_about_to_close_connection(connection_t *conn);
  443. void circuit_dump_by_conn(connection_t *conn, int severity);
  444. void circuit_expire_unused_circuits(void);
  445. int circuit_launch_new(void);
  446. void circuit_increment_failure_count(void);
  447. void circuit_reset_failure_count(void);
  448. int circuit_establish_circuit(void);
  449. void circuit_n_conn_open(connection_t *or_conn);
  450. int circuit_send_next_onion_skin(circuit_t *circ);
  451. int circuit_extend(cell_t *cell, circuit_t *circ);
  452. int circuit_finish_handshake(circuit_t *circ, char *reply);
  453. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  454. void assert_cpath_ok(const crypt_path_t *c);
  455. void assert_cpath_layer_ok(const crypt_path_t *c);
  456. void assert_circuit_ok(const circuit_t *c);
  457. extern unsigned long stats_n_relay_cells_relayed;
  458. extern unsigned long stats_n_relay_cells_delivered;
  459. /********************************* command.c ***************************/
  460. void command_process_cell(cell_t *cell, connection_t *conn);
  461. extern unsigned long stats_n_padding_cells_processed;
  462. extern unsigned long stats_n_create_cells_processed;
  463. extern unsigned long stats_n_created_cells_processed;
  464. extern unsigned long stats_n_relay_cells_processed;
  465. extern unsigned long stats_n_destroy_cells_processed;
  466. /********************************* config.c ***************************/
  467. int getconfig(int argc, char **argv, or_options_t *options);
  468. /********************************* connection.c ***************************/
  469. connection_t *connection_new(int type);
  470. void connection_free(connection_t *conn);
  471. int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
  472. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  473. int retry_all_connections(void);
  474. int connection_handle_read(connection_t *conn);
  475. int connection_read_to_buf(connection_t *conn);
  476. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  477. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  478. int connection_wants_to_flush(connection_t *conn);
  479. int connection_outbuf_too_full(connection_t *conn);
  480. int connection_handle_write(connection_t *conn);
  481. void connection_write_to_buf(const char *string, int len, connection_t *conn);
  482. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  483. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  484. connection_t *connection_get_by_type(int type);
  485. connection_t *connection_get_by_type_state(int type, int state);
  486. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  487. int connection_receiver_bucket_should_increase(connection_t *conn);
  488. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  489. #define connection_has_pending_tls_data(conn) \
  490. ((conn)->type == CONN_TYPE_OR && \
  491. (conn)->state == OR_CONN_STATE_OPEN && \
  492. tor_tls_get_pending_bytes(conn->tls))
  493. int connection_is_listener(connection_t *conn);
  494. int connection_state_is_open(connection_t *conn);
  495. int connection_send_destroy(circ_id_t circ_id, connection_t *conn);
  496. int connection_process_inbuf(connection_t *conn);
  497. int connection_finished_flushing(connection_t *conn);
  498. void assert_connection_ok(connection_t *conn, time_t now);
  499. /********************************* connection_edge.c ***************************/
  500. int connection_edge_process_inbuf(connection_t *conn);
  501. void connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  502. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
  503. void *payload, int payload_len, crypt_path_t *cpath_layer);
  504. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  505. int edge_type, crypt_path_t *layer_hint);
  506. int connection_edge_finished_flushing(connection_t *conn);
  507. int connection_edge_package_raw_inbuf(connection_t *conn);
  508. void connection_exit_connect(connection_t *conn);
  509. void connection_ap_attach_pending(void);
  510. extern uint64_t stats_n_data_cells_packaged;
  511. extern uint64_t stats_n_data_bytes_packaged;
  512. extern uint64_t stats_n_data_cells_received;
  513. extern uint64_t stats_n_data_bytes_received;
  514. void client_dns_init(void);
  515. void client_dns_clean(void);
  516. /********************************* connection_or.c ***************************/
  517. int connection_or_process_inbuf(connection_t *conn);
  518. int connection_or_finished_flushing(connection_t *conn);
  519. void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
  520. connection_t *connection_or_connect(routerinfo_t *router);
  521. int connection_tls_start_handshake(connection_t *conn, int receiving);
  522. int connection_tls_continue_handshake(connection_t *conn);
  523. void connection_or_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
  524. /********************************* cpuworker.c *****************************/
  525. void cpu_init(void);
  526. int connection_cpu_finished_flushing(connection_t *conn);
  527. int connection_cpu_process_inbuf(connection_t *conn);
  528. int cpuworker_main(void *data);
  529. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  530. void *task);
  531. /********************************* directory.c ***************************/
  532. void directory_initiate_command(routerinfo_t *router, int command);
  533. int connection_dir_process_inbuf(connection_t *conn);
  534. int connection_dir_finished_flushing(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_onion_key(crypto_pk_env_t *k);
  543. crypto_pk_env_t *get_onion_key(void);
  544. void set_identity_key(crypto_pk_env_t *k);
  545. crypto_pk_env_t *get_identity_key(void);
  546. crypto_pk_env_t *get_link_key(void);
  547. int connection_add(connection_t *conn);
  548. int connection_remove(connection_t *conn);
  549. void connection_set_poll_socket(connection_t *conn);
  550. void get_connection_array(connection_t ***array, int *n);
  551. void connection_watch_events(connection_t *conn, short events);
  552. int connection_is_reading(connection_t *conn);
  553. void connection_stop_reading(connection_t *conn);
  554. void connection_start_reading(connection_t *conn);
  555. void connection_stop_writing(connection_t *conn);
  556. void connection_start_writing(connection_t *conn);
  557. const char *router_get_my_descriptor(void);
  558. int main(int argc, char *argv[]);
  559. /********************************* onion.c ***************************/
  560. int decide_circ_id_type(char *local_nick, char *remote_nick);
  561. int onion_pending_add(circuit_t *circ);
  562. circuit_t *onion_next_task(void);
  563. void onion_pending_remove(circuit_t *circ);
  564. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  565. char **parse_nickname_list(char *start, int *num);
  566. int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
  567. routerinfo_t **router_out);
  568. int onion_skin_create(crypto_pk_env_t *router_key,
  569. crypto_dh_env_t **handshake_state_out,
  570. char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
  571. int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
  572. crypto_pk_env_t *private_key,
  573. char *handshake_reply_out, /* DH_KEY_LEN bytes long */
  574. char *key_out,
  575. int key_out_len);
  576. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  577. char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
  578. char *key_out,
  579. int key_out_len);
  580. cpath_build_state_t *onion_new_cpath_build_state(void);
  581. /********************************* routers.c ***************************/
  582. int learn_my_address(struct sockaddr_in *me);
  583. void router_retry_connections(void);
  584. routerinfo_t *router_pick_directory_server(void);
  585. void router_upload_desc_to_dirservers(void);
  586. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  587. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
  588. routerinfo_t *router_get_by_nickname(char *nickname);
  589. void router_get_directory(directory_t **pdirectory);
  590. int router_is_me(uint32_t addr, uint16_t port);
  591. void router_mark_as_down(char *nickname);
  592. int router_get_list_from_file(char *routerfile);
  593. int router_get_router_hash(char *s, char *digest);
  594. int router_get_dir_hash(char *s, char *digest);
  595. /* Reads a list of known routers, unsigned. */
  596. int router_get_list_from_string(char *s);
  597. /* Exported for debugging */
  598. int router_get_list_from_string_impl(char **s, directory_t **dest, int n_good_nicknames, const char *good_nickname_lst[]);
  599. /* Reads a signed directory. */
  600. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey);
  601. /* Exported or debugging */
  602. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  603. crypto_pk_env_t *pkey);
  604. routerinfo_t *router_get_entry_from_string(char **s);
  605. int router_supports_exit_address(uint32_t addr, uint16_t port,
  606. routerinfo_t *router);
  607. int router_compare_to_exit_policy(connection_t *conn);
  608. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  609. struct exit_policy_t *policy);
  610. void routerinfo_free(routerinfo_t *router);
  611. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  612. crypto_pk_env_t *ident_key);
  613. const routerinfo_t *router_get_desc_routerinfo(void);
  614. const char *router_get_my_descriptor(void);
  615. int router_rebuild_descriptor(void);
  616. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  617. /********************************* dirserv.c ***************************/
  618. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  619. int dirserv_parse_fingerprint_file(const char *fname);
  620. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  621. void dirserv_free_fingerprint_list();
  622. int dirserv_add_descriptor(const char **desc);
  623. int dirserv_init_from_directory_string(const char *dir);
  624. void dirserv_free_descriptors();
  625. int dirserv_dump_directory_to_string(char *s, int maxlen,
  626. crypto_pk_env_t *private_key);
  627. void directory_set_dirty();
  628. size_t dirserv_get_directory(const char **cp);
  629. #endif
  630. /*
  631. Local Variables:
  632. mode:c
  633. indent-tabs-mode:nil
  634. c-basic-offset:2
  635. End:
  636. */