or.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 500000
  103. #define MAX_DNS_ENTRY_AGE (15*60)
  104. #define CIRC_ID_TYPE_LOWER 0
  105. #define CIRC_ID_TYPE_HIGHER 1
  106. #define _CONN_TYPE_MIN 3
  107. #define CONN_TYPE_OR_LISTENER 3
  108. #define CONN_TYPE_OR 4
  109. #define CONN_TYPE_EXIT 5
  110. #define CONN_TYPE_AP_LISTENER 6
  111. #define CONN_TYPE_AP 7
  112. #define CONN_TYPE_DIR_LISTENER 8
  113. #define CONN_TYPE_DIR 9
  114. #define CONN_TYPE_DNSWORKER 10
  115. #define CONN_TYPE_CPUWORKER 11
  116. #define _CONN_TYPE_MAX 11
  117. #define LISTENER_STATE_READY 0
  118. #define _DNSWORKER_STATE_MIN 1
  119. #define DNSWORKER_STATE_IDLE 1
  120. #define DNSWORKER_STATE_BUSY 2
  121. #define _DNSWORKER_STATE_MAX 2
  122. #define _CPUWORKER_STATE_MIN 1
  123. #define CPUWORKER_STATE_IDLE 1
  124. #define CPUWORKER_STATE_BUSY_ONION 2
  125. #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
  126. #define _CPUWORKER_STATE_MAX 3
  127. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  128. #define _OR_CONN_STATE_MIN 1
  129. #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
  130. #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
  131. #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
  132. #define _OR_CONN_STATE_MAX 3
  133. #define _EXIT_CONN_STATE_MIN 1
  134. #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
  135. #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
  136. #define EXIT_CONN_STATE_OPEN 3
  137. #define _EXIT_CONN_STATE_MAX 3
  138. #if 0
  139. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  140. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  141. #endif
  142. /* the AP state values must be disjoint from the EXIT state values */
  143. #define _AP_CONN_STATE_MIN 4
  144. #define AP_CONN_STATE_SOCKS_WAIT 4
  145. #define AP_CONN_STATE_CIRCUIT_WAIT 5
  146. #define AP_CONN_STATE_OPEN 6
  147. #define _AP_CONN_STATE_MAX 6
  148. #define _DIR_CONN_STATE_MIN 1
  149. #define DIR_CONN_STATE_CONNECTING_FETCH 1
  150. #define DIR_CONN_STATE_CONNECTING_UPLOAD 2
  151. #define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
  152. #define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
  153. #define DIR_CONN_STATE_CLIENT_READING_FETCH 5
  154. #define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
  155. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
  156. #define DIR_CONN_STATE_SERVER_WRITING 8
  157. #define _DIR_CONN_STATE_MAX 8
  158. #define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
  159. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
  160. #define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
  161. #define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
  162. #define RELAY_COMMAND_BEGIN 1
  163. #define RELAY_COMMAND_DATA 2
  164. #define RELAY_COMMAND_END 3
  165. #define RELAY_COMMAND_CONNECTED 4
  166. #define RELAY_COMMAND_SENDME 5
  167. #define RELAY_COMMAND_EXTEND 6
  168. #define RELAY_COMMAND_EXTENDED 7
  169. #define RELAY_COMMAND_TRUNCATE 8
  170. #define RELAY_COMMAND_TRUNCATED 9
  171. #define RELAY_COMMAND_DROP 10
  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. /* legal characters in a filename */
  200. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  201. /* legal characters in a nickname */
  202. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  203. #define SOCKS4_NETWORK_LEN 8
  204. typedef uint16_t circ_id_t;
  205. /*
  206. * Relay payload:
  207. * Relay command [1 byte]
  208. * Recognized [2 bytes]
  209. * Stream ID [2 bytes]
  210. * Partial SHA-1 [4 bytes]
  211. * Length [2 bytes]
  212. * Relay payload [498 bytes]
  213. */
  214. #if 0
  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 CELL_RELAY_RECOGNIZED(c) (ntohs(*(uint16_t*)((c).payload+1)))
  218. #define SET_CELL_RELAY_RECOGNIZED(c,r) (*(uint16_t*)((c).payload+1) = htons(r))
  219. #define STREAM_ID_SIZE 2
  220. //#define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
  221. #define CELL_RELAY_STREAM_ID(c) (ntohs(*(uint16_t*)((c).payload+3)))
  222. #define SET_CELL_RELAY_STREAM_ID(c,id) (*(uint16_t*)((c).payload+3) = htons(id))
  223. #define ZERO_STREAM 0
  224. /* integrity is the first 32 bits (in network order) of a sha-1 of all
  225. * cell payloads that are relay cells that have been sent / delivered
  226. * to the hop on the * circuit (the integrity is zeroed while doing
  227. * each calculation)
  228. */
  229. #define CELL_RELAY_INTEGRITY(c) (ntohl(*(uint32_t*)((c).payload+5)))
  230. #define SET_CELL_RELAY_INTEGRITY(c,i) (*(uint32_t*)((c).payload+5) = htonl(i))
  231. /* relay length is how many bytes are used in the cell payload past relay_header_size */
  232. #define CELL_RELAY_LENGTH(c) (ntohs(*(uint16_t*)((c).payload+9)))
  233. #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+9) = htons(len))
  234. #endif
  235. #define CELL_PAYLOAD_SIZE 509
  236. #define CELL_NETWORK_SIZE 512
  237. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  238. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  239. /* cell definition */
  240. typedef struct {
  241. circ_id_t circ_id;
  242. unsigned char command;
  243. unsigned char payload[CELL_PAYLOAD_SIZE];
  244. } cell_t;
  245. typedef struct {
  246. uint8_t command;
  247. uint16_t recognized;
  248. uint16_t stream_id;
  249. uint32_t integrity;
  250. uint16_t length;
  251. } relay_header_t;
  252. typedef struct buf_t buf_t;
  253. typedef struct socks_request_t socks_request_t;
  254. struct connection_t {
  255. uint8_t type;
  256. uint8_t state;
  257. uint8_t wants_to_read; /* should we start reading again once
  258. * the bandwidth throttler allows it?
  259. */
  260. uint8_t wants_to_write; /* should we start writing again once
  261. * the bandwidth throttler allows reads?
  262. */
  263. int s; /* our socket */
  264. int poll_index; /* index of this conn into the poll_array */
  265. int marked_for_close; /* should we close this conn on the next
  266. * iteration of the main loop?
  267. */
  268. buf_t *inbuf;
  269. int inbuf_reached_eof; /* did read() return 0 on this conn? */
  270. time_t timestamp_lastread; /* when was the last time poll() said we could read? */
  271. buf_t *outbuf;
  272. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  273. time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
  274. time_t timestamp_created; /* when was this connection_t created? */
  275. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  276. uint16_t port; /* if non-zero, they identify the guy on the other end
  277. * of the connection. */
  278. char *address; /* FQDN (or IP) of the guy on the other end.
  279. * strdup into this, because free_connection frees it
  280. */
  281. crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
  282. crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
  283. crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
  284. char *nickname;
  285. /* Used only by OR connections: */
  286. tor_tls *tls;
  287. circ_id_t next_circ_id; /* Which circ_id do we try to use next on this connection?
  288. * This is always in the range 0..1<<15-1.*/
  289. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  290. uint32_t bandwidth; /* connection bandwidth. */
  291. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  292. * add 'bandwidth' to this, capping it at 10*bandwidth.
  293. */
  294. /* Used only by edge connections: */
  295. uint16_t stream_id;
  296. struct connection_t *next_stream; /* points to the next stream at this edge, if any */
  297. struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
  298. int package_window; /* how many more relay cells can i send into the circuit? */
  299. int deliver_window; /* how many more relay cells can end at me? */
  300. int done_sending; /* for half-open connections; not used currently */
  301. int done_receiving;
  302. char has_sent_end; /* for debugging: set once we've set the stream end,
  303. and check in circuit_about_to_close_connection() */
  304. /* Used only by AP connections */
  305. socks_request_t *socks_request;
  306. };
  307. typedef struct connection_t connection_t;
  308. #define EXIT_POLICY_ACCEPT 1
  309. #define EXIT_POLICY_REJECT 2
  310. struct exit_policy_t {
  311. char policy_type;
  312. char *string;
  313. uint32_t addr;
  314. uint32_t msk;
  315. uint16_t prt_min;
  316. uint16_t prt_max;
  317. struct exit_policy_t *next;
  318. };
  319. /* config stuff we know about the other ORs in the network */
  320. typedef struct {
  321. char *address;
  322. char *nickname;
  323. uint32_t addr; /* all host order */
  324. uint16_t or_port;
  325. uint16_t socks_port;
  326. uint16_t dir_port;
  327. time_t published_on;
  328. crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
  329. crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
  330. crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
  331. int is_running;
  332. /* link info */
  333. uint32_t bandwidth;
  334. struct exit_policy_t *exit_policy;
  335. } routerinfo_t;
  336. #define MAX_ROUTERS_IN_DIR 1024
  337. typedef struct {
  338. routerinfo_t **routers;
  339. int n_routers;
  340. char *software_versions;
  341. time_t published_on;
  342. } routerlist_t;
  343. struct crypt_path_t {
  344. /* crypto environments */
  345. crypto_cipher_env_t *f_crypto;
  346. crypto_cipher_env_t *b_crypto;
  347. crypto_digest_env_t *f_digest; /* for integrity checking */
  348. crypto_digest_env_t *b_digest;
  349. crypto_dh_env_t *handshake_state;
  350. uint32_t addr;
  351. uint16_t port;
  352. uint8_t state;
  353. #define CPATH_STATE_CLOSED 0
  354. #define CPATH_STATE_AWAITING_KEYS 1
  355. #define CPATH_STATE_OPEN 2
  356. struct crypt_path_t *next;
  357. struct crypt_path_t *prev; /* doubly linked list */
  358. int package_window;
  359. int deliver_window;
  360. };
  361. #define DH_KEY_LEN CRYPTO_DH_SIZE
  362. #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN+6+8+16)
  363. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
  364. typedef struct crypt_path_t crypt_path_t;
  365. typedef struct {
  366. int desired_path_len;
  367. char *chosen_exit; /* nickname of planned exit node */
  368. } cpath_build_state_t;
  369. /* struct for a path (circuit) through the network */
  370. struct circuit_t {
  371. uint32_t n_addr;
  372. uint16_t n_port;
  373. connection_t *p_conn;
  374. connection_t *n_conn; /* for the OR conn, if there is one */
  375. connection_t *p_streams;
  376. connection_t *n_streams;
  377. int package_window;
  378. int deliver_window;
  379. circ_id_t p_circ_id; /* circuit identifiers */
  380. circ_id_t n_circ_id;
  381. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  382. crypto_cipher_env_t *n_crypto;
  383. crypto_digest_env_t *p_digest; /* for integrity checking, */
  384. crypto_digest_env_t *n_digest; /* intermediate hops only */
  385. cpath_build_state_t *build_state;
  386. crypt_path_t *cpath;
  387. char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
  388. time_t timestamp_created;
  389. time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
  390. uint8_t state;
  391. void *next;
  392. };
  393. typedef struct circuit_t circuit_t;
  394. typedef struct {
  395. char *LogLevel;
  396. char *LogFile;
  397. char *DebugLogFile;
  398. char *DataDirectory;
  399. char *RouterFile;
  400. char *Nickname;
  401. char *Address;
  402. char *PidFile;
  403. char *ExitNodes;
  404. char *EntryNodes;
  405. char *ExcludedNodes;
  406. char *ExitPolicy;
  407. char *SocksBindAddress;
  408. char *ORBindAddress;
  409. char *DirBindAddress;
  410. char *RecommendedVersions;
  411. char *User;
  412. char *Group;
  413. double PathlenCoinWeight;
  414. int ORPort;
  415. int SocksPort;
  416. int DirPort;
  417. int MaxConn;
  418. int TrafficShaping;
  419. int LinkPadding;
  420. int IgnoreVersion;
  421. int RunAsDaemon;
  422. int DirRebuildPeriod;
  423. int DirFetchPostPeriod;
  424. int KeepalivePeriod;
  425. int MaxOnionsPending;
  426. int NewCircuitPeriod;
  427. int TotalBandwidth;
  428. int NumCpus;
  429. int loglevel;
  430. } or_options_t;
  431. /* XXX are these good enough defaults? */
  432. #define MAX_SOCKS_REPLY_LEN 256
  433. /* Not 256; addresses must fit in a begin cell. */
  434. #define MAX_SOCKS_ADDR_LEN 200
  435. struct socks_request_t {
  436. char socks_version;
  437. int replylen;
  438. char reply[MAX_SOCKS_REPLY_LEN];
  439. char address[MAX_SOCKS_ADDR_LEN];
  440. uint16_t port;
  441. };
  442. /* all the function prototypes go here */
  443. /********************************* buffers.c ***************************/
  444. int find_on_inbuf(char *string, int string_len, buf_t *buf);
  445. buf_t *buf_new();
  446. buf_t *buf_new_with_capacity(size_t size);
  447. void buf_free(buf_t *buf);
  448. size_t buf_datalen(const buf_t *buf);
  449. size_t buf_capacity(const buf_t *buf);
  450. const char *_buf_peek_raw_buffer(const buf_t *buf);
  451. int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
  452. int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
  453. int flush_buf(int s, buf_t *buf, int *buf_flushlen);
  454. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
  455. int write_to_buf(const char *string, int string_len, buf_t *buf);
  456. int fetch_from_buf(char *string, int string_len, buf_t *buf);
  457. int fetch_from_buf_http(buf_t *buf,
  458. char **headers_out, int max_headerlen,
  459. char **body_out, int max_bodylen);
  460. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  461. /********************************* circuit.c ***************************/
  462. void circuit_add(circuit_t *circ);
  463. void circuit_remove(circuit_t *circ);
  464. circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn);
  465. void circuit_free(circuit_t *circ);
  466. void circuit_free_cpath(crypt_path_t *cpath);
  467. circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn);
  468. circuit_t *circuit_get_by_conn(connection_t *conn);
  469. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
  470. void circuit_expire_building(void);
  471. int circuit_count_building(void);
  472. int circuit_stream_is_being_handled(connection_t *conn);
  473. void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell);
  474. int relay_check_digest(crypto_digest_env_t *digest, cell_t *cell);
  475. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  476. int cell_direction, crypt_path_t *layer_hint);
  477. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  478. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  479. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  480. void circuit_close(circuit_t *circ);
  481. void circuit_about_to_close_connection(connection_t *conn);
  482. void circuit_dump_by_conn(connection_t *conn, int severity);
  483. void circuit_expire_unused_circuits(void);
  484. int circuit_launch_new(void);
  485. void circuit_increment_failure_count(void);
  486. void circuit_reset_failure_count(void);
  487. int circuit_establish_circuit(void);
  488. void circuit_n_conn_open(connection_t *or_conn);
  489. int circuit_send_next_onion_skin(circuit_t *circ);
  490. int circuit_extend(cell_t *cell, circuit_t *circ);
  491. int circuit_finish_handshake(circuit_t *circ, char *reply);
  492. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  493. void assert_cpath_ok(const crypt_path_t *c);
  494. void assert_cpath_layer_ok(const crypt_path_t *c);
  495. void assert_circuit_ok(const circuit_t *c);
  496. extern unsigned long stats_n_relay_cells_relayed;
  497. extern unsigned long stats_n_relay_cells_delivered;
  498. /********************************* command.c ***************************/
  499. void command_process_cell(cell_t *cell, connection_t *conn);
  500. extern unsigned long stats_n_padding_cells_processed;
  501. extern unsigned long stats_n_create_cells_processed;
  502. extern unsigned long stats_n_created_cells_processed;
  503. extern unsigned long stats_n_relay_cells_processed;
  504. extern unsigned long stats_n_destroy_cells_processed;
  505. /********************************* config.c ***************************/
  506. int getconfig(int argc, char **argv, or_options_t *options);
  507. /********************************* connection.c ***************************/
  508. connection_t *connection_new(int type);
  509. void connection_free(connection_t *conn);
  510. int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
  511. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  512. int retry_all_connections(void);
  513. int connection_handle_read(connection_t *conn);
  514. int connection_read_to_buf(connection_t *conn);
  515. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  516. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  517. int connection_wants_to_flush(connection_t *conn);
  518. int connection_outbuf_too_full(connection_t *conn);
  519. int connection_handle_write(connection_t *conn);
  520. void connection_write_to_buf(const char *string, int len, connection_t *conn);
  521. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  522. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  523. connection_t *connection_get_by_type(int type);
  524. connection_t *connection_get_by_type_state(int type, int state);
  525. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  526. int connection_receiver_bucket_should_increase(connection_t *conn);
  527. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  528. #define connection_has_pending_tls_data(conn) \
  529. ((conn)->type == CONN_TYPE_OR && \
  530. (conn)->state == OR_CONN_STATE_OPEN && \
  531. tor_tls_get_pending_bytes(conn->tls))
  532. int connection_is_listener(connection_t *conn);
  533. int connection_state_is_open(connection_t *conn);
  534. int connection_send_destroy(circ_id_t circ_id, connection_t *conn);
  535. int connection_process_inbuf(connection_t *conn);
  536. int connection_finished_flushing(connection_t *conn);
  537. void assert_connection_ok(connection_t *conn, time_t now);
  538. /********************************* connection_edge.c ***************************/
  539. void relay_header_pack(char *dest, const relay_header_t *src);
  540. void relay_header_unpack(relay_header_t *dest, const char *src);
  541. int connection_edge_process_inbuf(connection_t *conn);
  542. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  543. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
  544. void *payload, int payload_len, crypt_path_t *cpath_layer);
  545. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  546. int edge_type, crypt_path_t *layer_hint);
  547. int connection_edge_finished_flushing(connection_t *conn);
  548. int connection_edge_package_raw_inbuf(connection_t *conn);
  549. void connection_exit_connect(connection_t *conn);
  550. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  551. void connection_ap_attach_pending(void);
  552. extern uint64_t stats_n_data_cells_packaged;
  553. extern uint64_t stats_n_data_bytes_packaged;
  554. extern uint64_t stats_n_data_cells_received;
  555. extern uint64_t stats_n_data_bytes_received;
  556. void client_dns_init(void);
  557. void client_dns_clean(void);
  558. /********************************* connection_or.c ***************************/
  559. int connection_or_process_inbuf(connection_t *conn);
  560. int connection_or_finished_flushing(connection_t *conn);
  561. void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
  562. connection_t *connection_or_connect(routerinfo_t *router);
  563. int connection_tls_start_handshake(connection_t *conn, int receiving);
  564. int connection_tls_continue_handshake(connection_t *conn);
  565. void connection_or_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
  566. /********************************* cpuworker.c *****************************/
  567. void cpu_init(void);
  568. int connection_cpu_finished_flushing(connection_t *conn);
  569. int connection_cpu_process_inbuf(connection_t *conn);
  570. int cpuworker_main(void *data);
  571. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  572. void *task);
  573. /********************************* directory.c ***************************/
  574. void directory_initiate_command(routerinfo_t *router, int command);
  575. int connection_dir_process_inbuf(connection_t *conn);
  576. int connection_dir_finished_flushing(connection_t *conn);
  577. /********************************* dns.c ***************************/
  578. void dns_init(void);
  579. int connection_dns_finished_flushing(connection_t *conn);
  580. int connection_dns_process_inbuf(connection_t *conn);
  581. void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
  582. int dns_resolve(connection_t *exitconn);
  583. /********************************* main.c ***************************/
  584. int connection_add(connection_t *conn);
  585. int connection_remove(connection_t *conn);
  586. void connection_set_poll_socket(connection_t *conn);
  587. void get_connection_array(connection_t ***array, int *n);
  588. void connection_watch_events(connection_t *conn, short events);
  589. int connection_is_reading(connection_t *conn);
  590. void connection_stop_reading(connection_t *conn);
  591. void connection_start_reading(connection_t *conn);
  592. void connection_stop_writing(connection_t *conn);
  593. void connection_start_writing(connection_t *conn);
  594. int main(int argc, char *argv[]);
  595. /********************************* onion.c ***************************/
  596. int decide_circ_id_type(char *local_nick, char *remote_nick);
  597. int onion_pending_add(circuit_t *circ);
  598. circuit_t *onion_next_task(void);
  599. void onion_pending_remove(circuit_t *circ);
  600. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  601. int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
  602. routerinfo_t **router_out);
  603. int onion_skin_create(crypto_pk_env_t *router_key,
  604. crypto_dh_env_t **handshake_state_out,
  605. char *onion_skin_out);
  606. int onion_skin_server_handshake(char *onion_skin,
  607. crypto_pk_env_t *private_key,
  608. char *handshake_reply_out,
  609. char *key_out,
  610. int key_out_len);
  611. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  612. char *handshake_reply,
  613. char *key_out,
  614. int key_out_len);
  615. cpath_build_state_t *onion_new_cpath_build_state(void);
  616. /********************************* router.c ***************************/
  617. void set_onion_key(crypto_pk_env_t *k);
  618. crypto_pk_env_t *get_onion_key(void);
  619. void set_identity_key(crypto_pk_env_t *k);
  620. crypto_pk_env_t *get_identity_key(void);
  621. crypto_pk_env_t *get_link_key(void);
  622. int init_keys(void);
  623. void router_retry_connections(void);
  624. void router_upload_desc_to_dirservers(void);
  625. int router_compare_to_my_exit_policy(connection_t *conn);
  626. const char *router_get_my_descriptor(void);
  627. int router_rebuild_descriptor(void);
  628. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  629. crypto_pk_env_t *ident_key);
  630. /********************************* routerlist.c ***************************/
  631. routerinfo_t *router_pick_directory_server(void);
  632. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  633. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
  634. routerinfo_t *router_get_by_nickname(char *nickname);
  635. void router_get_routerlist(routerlist_t **prouterlist);
  636. void routerinfo_free(routerinfo_t *router);
  637. void router_mark_as_down(char *nickname);
  638. int router_set_routerlist_from_file(char *routerfile);
  639. int router_get_dir_hash(const char *s, char *digest);
  640. int router_get_router_hash(const char *s, char *digest);
  641. int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
  642. routerinfo_t *router_get_entry_from_string(const char **s);
  643. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  644. int router_supports_exit_address(uint32_t addr, uint16_t port,
  645. routerinfo_t *router);
  646. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  647. struct exit_policy_t *policy);
  648. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  649. int router_exit_policy_rejects_all(routerinfo_t *router);
  650. /********************************* dirserv.c ***************************/
  651. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  652. int dirserv_parse_fingerprint_file(const char *fname);
  653. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  654. void dirserv_free_fingerprint_list();
  655. int dirserv_add_descriptor(const char **desc);
  656. int dirserv_init_from_directory_string(const char *dir);
  657. void dirserv_free_descriptors();
  658. int dirserv_dump_directory_to_string(char *s, int maxlen,
  659. crypto_pk_env_t *private_key);
  660. void directory_set_dirty();
  661. size_t dirserv_get_directory(const char **cp);
  662. #endif
  663. /*
  664. Local Variables:
  665. mode:c
  666. indent-tabs-mode:nil
  667. c-basic-offset:2
  668. End:
  669. */