or.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #include "../common/fakepoll.h"
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  29. #endif
  30. #ifdef HAVE_SYS_WAIT_H
  31. #include <sys/wait.h>
  32. #endif
  33. #ifdef HAVE_SYS_FCNTL_H
  34. #include <sys/fcntl.h>
  35. #endif
  36. #ifdef HAVE_FCNTL_H
  37. #include <fcntl.h>
  38. #endif
  39. #ifdef HAVE_SYS_IOCTL_H
  40. #include <sys/ioctl.h>
  41. #endif
  42. #ifdef HAVE_SYS_SOCKET_H
  43. #include <sys/socket.h>
  44. #endif
  45. #ifdef HAVE_SYS_TIME_H
  46. #include <sys/time.h>
  47. #endif
  48. #ifdef HAVE_SYS_STAT_H
  49. #include <sys/stat.h>
  50. #endif
  51. #ifdef HAVE_NETINET_IN_H
  52. #include <netinet/in.h>
  53. #endif
  54. #ifdef HAVE_ARPA_INET_H
  55. #include <arpa/inet.h>
  56. #endif
  57. #ifdef HAVE_ERRNO_H
  58. #include <errno.h>
  59. #endif
  60. #ifdef HAVE_ASSERT_H
  61. #include <assert.h>
  62. #endif
  63. #ifdef HAVE_TIME_H
  64. #include <time.h>
  65. #endif
  66. #ifdef HAVE_PWD_H
  67. #include <pwd.h>
  68. #endif
  69. #ifdef HAVE_GRP_H
  70. #include <grp.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. #include <process.h>
  84. #define WIN32_LEAN_AND_MEAN
  85. #include <windows.h>
  86. #define snprintf _snprintf
  87. #endif
  88. #include "../common/crypto.h"
  89. #include "../common/tortls.h"
  90. #include "../common/log.h"
  91. #include "../common/util.h"
  92. #define MAXCONNECTIONS 1000 /* upper bound on max connections.
  93. can be lowered by config file */
  94. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  95. #define MAX_NICKNAME_LEN 32
  96. #define MAX_DIR_SIZE 500000
  97. #ifdef TOR_PERF
  98. #define MAX_DNS_ENTRY_AGE (150*60)
  99. #else
  100. #define MAX_DNS_ENTRY_AGE (15*60)
  101. #endif
  102. #define CIRC_ID_TYPE_LOWER 0
  103. #define CIRC_ID_TYPE_HIGHER 1
  104. #define _CONN_TYPE_MIN 3
  105. #define CONN_TYPE_OR_LISTENER 3
  106. #define CONN_TYPE_OR 4
  107. #define CONN_TYPE_EXIT 5
  108. #define CONN_TYPE_AP_LISTENER 6
  109. #define CONN_TYPE_AP 7
  110. #define CONN_TYPE_DIR_LISTENER 8
  111. #define CONN_TYPE_DIR 9
  112. #define CONN_TYPE_DNSWORKER 10
  113. #define CONN_TYPE_CPUWORKER 11
  114. #define _CONN_TYPE_MAX 11
  115. #define LISTENER_STATE_READY 0
  116. #define _DNSWORKER_STATE_MIN 1
  117. #define DNSWORKER_STATE_IDLE 1
  118. #define DNSWORKER_STATE_BUSY 2
  119. #define _DNSWORKER_STATE_MAX 2
  120. #define _CPUWORKER_STATE_MIN 1
  121. #define CPUWORKER_STATE_IDLE 1
  122. #define CPUWORKER_STATE_BUSY_ONION 2
  123. #define CPUWORKER_STATE_BUSY_HANDSHAKE 3
  124. #define _CPUWORKER_STATE_MAX 3
  125. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  126. #define _OR_CONN_STATE_MIN 1
  127. #define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
  128. #define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
  129. #define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
  130. #define _OR_CONN_STATE_MAX 3
  131. #define _EXIT_CONN_STATE_MIN 1
  132. #define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
  133. #define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
  134. #define EXIT_CONN_STATE_OPEN 3
  135. #define _EXIT_CONN_STATE_MAX 3
  136. #if 0
  137. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  138. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  139. #endif
  140. /* the AP state values must be disjoint from the EXIT state values */
  141. #define _AP_CONN_STATE_MIN 4
  142. #define AP_CONN_STATE_SOCKS_WAIT 4
  143. #define AP_CONN_STATE_CIRCUIT_WAIT 5
  144. #define AP_CONN_STATE_CONNECTING 6
  145. #define AP_CONN_STATE_OPEN 7
  146. #define _AP_CONN_STATE_MAX 7
  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 _MIN_END_STREAM_REASON 1
  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. #define _MAX_END_STREAM_REASON 6
  179. /* default cipher function */
  180. #define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
  181. /* Used to en/decrypt onion skins */
  182. #define ONION_CIPHER DEFAULT_CIPHER
  183. /* Used to en/decrypt RELAY cells */
  184. #define CIRCUIT_CIPHER DEFAULT_CIPHER
  185. #define CELL_DIRECTION_IN 1
  186. #define CELL_DIRECTION_OUT 2
  187. #define EDGE_EXIT CONN_TYPE_EXIT
  188. #define EDGE_AP CONN_TYPE_AP
  189. #define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
  190. #ifdef TOR_PERF
  191. #define CIRCWINDOW_START 10000
  192. #define CIRCWINDOW_INCREMENT 1000
  193. #define STREAMWINDOW_START 5000
  194. #define STREAMWINDOW_INCREMENT 500
  195. #else
  196. #define CIRCWINDOW_START 1000
  197. #define CIRCWINDOW_INCREMENT 100
  198. #define STREAMWINDOW_START 500
  199. #define STREAMWINDOW_INCREMENT 50
  200. #endif
  201. /* cell commands */
  202. #define CELL_PADDING 0
  203. #define CELL_CREATE 1
  204. #define CELL_CREATED 2
  205. #define CELL_RELAY 3
  206. #define CELL_DESTROY 4
  207. /* legal characters in a filename */
  208. #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
  209. /* legal characters in a nickname */
  210. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  211. #define SOCKS4_NETWORK_LEN 8
  212. /*
  213. * Relay payload:
  214. * Relay command [1 byte]
  215. * Recognized [2 bytes]
  216. * Stream ID [2 bytes]
  217. * Partial SHA-1 [4 bytes]
  218. * Length [2 bytes]
  219. * Relay payload [498 bytes]
  220. */
  221. #if 0
  222. #define CELL_RELAY_COMMAND(c) (*(uint8_t*)((c).payload))
  223. #define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
  224. #define CELL_RELAY_RECOGNIZED(c) (ntohs(*(uint16_t*)((c).payload+1)))
  225. #define SET_CELL_RELAY_RECOGNIZED(c,r) (*(uint16_t*)((c).payload+1) = htons(r))
  226. #define STREAM_ID_SIZE 2
  227. //#define SET_CELL_STREAM_ID(c,id) memcpy((c).payload+1,(id),STREAM_ID_SIZE)
  228. #define CELL_RELAY_STREAM_ID(c) (ntohs(*(uint16_t*)((c).payload+3)))
  229. #define SET_CELL_RELAY_STREAM_ID(c,id) (*(uint16_t*)((c).payload+3) = htons(id))
  230. #define ZERO_STREAM 0
  231. /* integrity is the first 32 bits (in network order) of a sha-1 of all
  232. * cell payloads that are relay cells that have been sent / delivered
  233. * to the hop on the * circuit (the integrity is zeroed while doing
  234. * each calculation)
  235. */
  236. #define CELL_RELAY_INTEGRITY(c) (ntohl(*(uint32_t*)((c).payload+5)))
  237. #define SET_CELL_RELAY_INTEGRITY(c,i) (*(uint32_t*)((c).payload+5) = htonl(i))
  238. /* relay length is how many bytes are used in the cell payload past relay_header_size */
  239. #define CELL_RELAY_LENGTH(c) (ntohs(*(uint16_t*)((c).payload+9)))
  240. #define SET_CELL_RELAY_LENGTH(c,len) (*(uint16_t*)((c).payload+9) = htons(len))
  241. #endif
  242. #define CELL_PAYLOAD_SIZE 509
  243. #define CELL_NETWORK_SIZE 512
  244. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  245. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  246. /* cell definition */
  247. typedef struct {
  248. uint16_t circ_id;
  249. unsigned char command;
  250. unsigned char payload[CELL_PAYLOAD_SIZE];
  251. } cell_t;
  252. typedef struct {
  253. uint8_t command;
  254. uint16_t recognized;
  255. uint16_t stream_id;
  256. char integrity[4];
  257. uint16_t length;
  258. } relay_header_t;
  259. typedef struct buf_t buf_t;
  260. typedef struct socks_request_t socks_request_t;
  261. #define CONNECTION_MAGIC 0x7C3C304Eu
  262. struct connection_t {
  263. uint32_t magic; /* for memory debugging */
  264. uint8_t type;
  265. uint8_t state;
  266. uint8_t wants_to_read; /* should we start reading again once
  267. * the bandwidth throttler allows it?
  268. */
  269. uint8_t wants_to_write; /* should we start writing again once
  270. * the bandwidth throttler allows reads?
  271. */
  272. int s; /* our socket */
  273. int poll_index; /* index of this conn into the poll_array */
  274. int marked_for_close; /* should we close this conn on the next
  275. * iteration of the main loop?
  276. */
  277. char *marked_for_close_file; /* for debugging: in which file were we marked
  278. * for close? */
  279. buf_t *inbuf;
  280. int inbuf_reached_eof; /* did read() return 0 on this conn? */
  281. time_t timestamp_lastread; /* when was the last time poll() said we could read? */
  282. buf_t *outbuf;
  283. int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  284. time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
  285. time_t timestamp_created; /* when was this connection_t created? */
  286. uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  287. uint16_t port; /* if non-zero, they identify the guy on the other end
  288. * of the connection. */
  289. char *address; /* FQDN (or IP) of the guy on the other end.
  290. * strdup into this, because free_connection frees it
  291. */
  292. crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
  293. crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
  294. crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
  295. char *nickname;
  296. /* Used only by OR connections: */
  297. tor_tls *tls;
  298. uint16_t next_circ_id; /* Which circ_id do we try to use next on this connection?
  299. * This is always in the range 0..1<<15-1.*/
  300. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  301. uint32_t bandwidth; /* connection bandwidth. */
  302. int receiver_bucket; /* when this hits 0, stop receiving. Every second we
  303. * add 'bandwidth' to this, capping it at 10*bandwidth.
  304. */
  305. /* Used only by edge connections: */
  306. uint16_t stream_id;
  307. struct connection_t *next_stream; /* points to the next stream at this edge, if any */
  308. struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
  309. int package_window; /* how many more relay cells can i send into the circuit? */
  310. int deliver_window; /* how many more relay cells can end at me? */
  311. int done_sending; /* for half-open connections; not used currently */
  312. int done_receiving;
  313. char has_sent_end; /* for debugging: set once we've set the stream end,
  314. and check in circuit_about_to_close_connection() */
  315. /* Used only by AP connections */
  316. socks_request_t *socks_request;
  317. };
  318. typedef struct connection_t connection_t;
  319. #define EXIT_POLICY_ACCEPT 1
  320. #define EXIT_POLICY_REJECT 2
  321. struct exit_policy_t {
  322. char policy_type;
  323. char *string;
  324. uint32_t addr;
  325. uint32_t msk;
  326. uint16_t prt_min;
  327. uint16_t prt_max;
  328. struct exit_policy_t *next;
  329. };
  330. /* config stuff we know about the other ORs in the network */
  331. typedef struct {
  332. char *address;
  333. char *nickname;
  334. uint32_t addr; /* all host order */
  335. uint16_t or_port;
  336. uint16_t socks_port;
  337. uint16_t dir_port;
  338. time_t published_on;
  339. crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
  340. crypto_pk_env_t *link_pkey; /* public RSA key for TLS */
  341. crypto_pk_env_t *identity_pkey; /* public RSA key for signing */
  342. int is_running;
  343. /* link info */
  344. uint32_t bandwidthrate;
  345. uint32_t bandwidthburst;
  346. struct exit_policy_t *exit_policy;
  347. } routerinfo_t;
  348. #define MAX_ROUTERS_IN_DIR 1024
  349. typedef struct {
  350. routerinfo_t **routers;
  351. int n_routers;
  352. char *software_versions;
  353. time_t published_on;
  354. } routerlist_t;
  355. struct crypt_path_t {
  356. /* crypto environments */
  357. crypto_cipher_env_t *f_crypto;
  358. crypto_cipher_env_t *b_crypto;
  359. crypto_digest_env_t *f_digest; /* for integrity checking */
  360. crypto_digest_env_t *b_digest;
  361. crypto_dh_env_t *handshake_state;
  362. uint32_t addr;
  363. uint16_t port;
  364. uint8_t state;
  365. #define CPATH_STATE_CLOSED 0
  366. #define CPATH_STATE_AWAITING_KEYS 1
  367. #define CPATH_STATE_OPEN 2
  368. struct crypt_path_t *next;
  369. struct crypt_path_t *prev; /* doubly linked list */
  370. int package_window;
  371. int deliver_window;
  372. };
  373. #define DH_KEY_LEN CRYPTO_DH_SIZE
  374. #define ONIONSKIN_CHALLENGE_LEN (16+DH_KEY_LEN)
  375. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+20)
  376. typedef struct crypt_path_t crypt_path_t;
  377. typedef struct {
  378. int desired_path_len;
  379. char *chosen_exit; /* nickname of planned exit node */
  380. } cpath_build_state_t;
  381. /* struct for a path (circuit) through the network */
  382. #define CIRCUIT_MAGIC 0x35315243u
  383. struct circuit_t {
  384. uint32_t magic; /* for memory debugging. */
  385. int marked_for_close; /* Should we close this circuit at the end of the main
  386. * loop? */
  387. uint32_t n_addr;
  388. uint16_t n_port;
  389. connection_t *p_conn;
  390. connection_t *n_conn; /* for the OR conn, if there is one */
  391. connection_t *p_streams;
  392. connection_t *n_streams;
  393. uint16_t next_stream_id;
  394. int package_window;
  395. int deliver_window;
  396. uint16_t p_circ_id; /* circuit identifiers */
  397. uint16_t n_circ_id;
  398. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  399. crypto_cipher_env_t *n_crypto;
  400. crypto_digest_env_t *p_digest; /* for integrity checking, */
  401. crypto_digest_env_t *n_digest; /* intermediate hops only */
  402. cpath_build_state_t *build_state;
  403. crypt_path_t *cpath;
  404. char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
  405. time_t timestamp_created;
  406. time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
  407. uint8_t state;
  408. void *next;
  409. };
  410. typedef struct circuit_t circuit_t;
  411. typedef struct {
  412. char *LogLevel;
  413. char *LogFile;
  414. char *DebugLogFile;
  415. char *DataDirectory;
  416. char *RouterFile;
  417. char *Nickname;
  418. char *Address;
  419. char *PidFile;
  420. char *ExitNodes;
  421. char *EntryNodes;
  422. char *ExcludeNodes;
  423. char *ExitPolicy;
  424. char *SocksBindAddress;
  425. char *ORBindAddress;
  426. char *DirBindAddress;
  427. char *RecommendedVersions;
  428. char *User;
  429. char *Group;
  430. double PathlenCoinWeight;
  431. int ORPort;
  432. int SocksPort;
  433. int DirPort;
  434. int MaxConn;
  435. int TrafficShaping;
  436. int LinkPadding;
  437. int IgnoreVersion;
  438. int RunAsDaemon;
  439. int DirRebuildPeriod;
  440. int DirFetchPostPeriod;
  441. int KeepalivePeriod;
  442. int MaxOnionsPending;
  443. int NewCircuitPeriod;
  444. int BandwidthRate;
  445. int BandwidthBurst;
  446. int NumCpus;
  447. int loglevel;
  448. } or_options_t;
  449. /* XXX are these good enough defaults? */
  450. #define MAX_SOCKS_REPLY_LEN 1024
  451. #define MAX_SOCKS_ADDR_LEN 256
  452. struct socks_request_t {
  453. char socks_version;
  454. int replylen;
  455. char reply[MAX_SOCKS_REPLY_LEN];
  456. char address[MAX_SOCKS_ADDR_LEN];
  457. uint16_t port;
  458. };
  459. /* all the function prototypes go here */
  460. /********************************* buffers.c ***************************/
  461. int find_on_inbuf(char *string, int string_len, buf_t *buf);
  462. buf_t *buf_new();
  463. buf_t *buf_new_with_capacity(size_t size);
  464. void buf_free(buf_t *buf);
  465. void buf_clear(buf_t *buf);
  466. size_t buf_datalen(const buf_t *buf);
  467. size_t buf_capacity(const buf_t *buf);
  468. const char *_buf_peek_raw_buffer(const buf_t *buf);
  469. int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
  470. int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
  471. int flush_buf(int s, buf_t *buf, int *buf_flushlen);
  472. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
  473. int write_to_buf(const char *string, int string_len, buf_t *buf);
  474. int fetch_from_buf(char *string, int string_len, buf_t *buf);
  475. int fetch_from_buf_http(buf_t *buf,
  476. char **headers_out, int max_headerlen,
  477. char **body_out, int max_bodylen);
  478. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  479. /********************************* circuit.c ***************************/
  480. void circuit_add(circuit_t *circ);
  481. void circuit_remove(circuit_t *circ);
  482. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
  483. void circuit_free(circuit_t *circ);
  484. void circuit_free_cpath(crypt_path_t *cpath);
  485. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
  486. circuit_t *circuit_get_by_conn(connection_t *conn);
  487. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
  488. void circuit_expire_building(void);
  489. int circuit_count_building(void);
  490. int circuit_stream_is_being_handled(connection_t *conn);
  491. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  492. int cell_direction);
  493. int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  494. int cell_direction, crypt_path_t *layer_hint);
  495. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  496. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  497. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  498. void circuit_close(circuit_t *circ);
  499. void circuit_detach_stream(circuit_t *circ, connection_t *conn);
  500. void circuit_about_to_close_connection(connection_t *conn);
  501. void circuit_log_path(int severity, circuit_t *circ);
  502. void circuit_dump_by_conn(connection_t *conn, int severity);
  503. void circuit_expire_unused_circuits(void);
  504. int circuit_launch_new(void);
  505. void circuit_increment_failure_count(void);
  506. void circuit_reset_failure_count(void);
  507. int circuit_establish_circuit(void);
  508. void circuit_n_conn_open(connection_t *or_conn);
  509. int circuit_send_next_onion_skin(circuit_t *circ);
  510. int circuit_extend(cell_t *cell, circuit_t *circ);
  511. int circuit_finish_handshake(circuit_t *circ, char *reply);
  512. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  513. void assert_cpath_ok(const crypt_path_t *c);
  514. void assert_cpath_layer_ok(const crypt_path_t *c);
  515. void assert_circuit_ok(const circuit_t *c);
  516. extern unsigned long stats_n_relay_cells_relayed;
  517. extern unsigned long stats_n_relay_cells_delivered;
  518. /********************************* command.c ***************************/
  519. void command_process_cell(cell_t *cell, connection_t *conn);
  520. extern unsigned long stats_n_padding_cells_processed;
  521. extern unsigned long stats_n_create_cells_processed;
  522. extern unsigned long stats_n_created_cells_processed;
  523. extern unsigned long stats_n_relay_cells_processed;
  524. extern unsigned long stats_n_destroy_cells_processed;
  525. /********************************* config.c ***************************/
  526. int config_assign_default_dirservers(void);
  527. int getconfig(int argc, char **argv, or_options_t *options);
  528. /********************************* connection.c ***************************/
  529. connection_t *connection_new(int type);
  530. void connection_free(connection_t *conn);
  531. void connection_free_all(void);
  532. void connection_close_immediate(connection_t *conn);
  533. int _connection_mark_for_close(connection_t *conn, char reason);
  534. #define connection_mark_for_close(c,r) \
  535. do { \
  536. if (_connection_mark_for_close(c,r)<0) { \
  537. log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
  538. __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
  539. } else { \
  540. c->marked_for_close_file = __FILE__; \
  541. c->marked_for_close = __LINE__; \
  542. } \
  543. } while (0)
  544. int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
  545. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  546. int retry_all_connections(void);
  547. int connection_handle_read(connection_t *conn);
  548. int connection_read_to_buf(connection_t *conn);
  549. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  550. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  551. int connection_wants_to_flush(connection_t *conn);
  552. int connection_outbuf_too_full(connection_t *conn);
  553. int connection_handle_write(connection_t *conn);
  554. void connection_write_to_buf(const char *string, int len, connection_t *conn);
  555. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  556. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  557. connection_t *connection_get_by_type(int type);
  558. connection_t *connection_get_by_type_state(int type, int state);
  559. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  560. int connection_receiver_bucket_should_increase(connection_t *conn);
  561. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  562. #define connection_has_pending_tls_data(conn) \
  563. ((conn)->type == CONN_TYPE_OR && \
  564. (conn)->state == OR_CONN_STATE_OPEN && \
  565. tor_tls_get_pending_bytes(conn->tls))
  566. int connection_is_listener(connection_t *conn);
  567. int connection_state_is_open(connection_t *conn);
  568. int connection_send_destroy(uint16_t circ_id, connection_t *conn);
  569. int connection_process_inbuf(connection_t *conn);
  570. int connection_finished_flushing(connection_t *conn);
  571. void assert_connection_ok(connection_t *conn, time_t now);
  572. /********************************* connection_edge.c ***************************/
  573. void relay_header_pack(char *dest, const relay_header_t *src);
  574. void relay_header_unpack(relay_header_t *dest, const char *src);
  575. int connection_edge_process_inbuf(connection_t *conn);
  576. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  577. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
  578. void *payload, int payload_len, crypt_path_t *cpath_layer);
  579. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  580. int edge_type, crypt_path_t *layer_hint);
  581. int connection_edge_finished_flushing(connection_t *conn);
  582. int connection_edge_package_raw_inbuf(connection_t *conn);
  583. void connection_exit_connect(connection_t *conn);
  584. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  585. void connection_ap_expire_beginning(void);
  586. void connection_ap_attach_pending(void);
  587. extern uint64_t stats_n_data_cells_packaged;
  588. extern uint64_t stats_n_data_bytes_packaged;
  589. extern uint64_t stats_n_data_cells_received;
  590. extern uint64_t stats_n_data_bytes_received;
  591. void client_dns_init(void);
  592. void client_dns_clean(void);
  593. /********************************* connection_or.c ***************************/
  594. int connection_or_process_inbuf(connection_t *conn);
  595. int connection_or_finished_flushing(connection_t *conn);
  596. void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
  597. connection_t *connection_or_connect(routerinfo_t *router);
  598. int connection_tls_start_handshake(connection_t *conn, int receiving);
  599. int connection_tls_continue_handshake(connection_t *conn);
  600. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
  601. /********************************* cpuworker.c *****************************/
  602. void cpu_init(void);
  603. int connection_cpu_finished_flushing(connection_t *conn);
  604. int connection_cpu_process_inbuf(connection_t *conn);
  605. int cpuworker_main(void *data);
  606. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  607. void *task);
  608. /********************************* directory.c ***************************/
  609. void directory_initiate_command(routerinfo_t *router, int command);
  610. int connection_dir_process_inbuf(connection_t *conn);
  611. int connection_dir_finished_flushing(connection_t *conn);
  612. /********************************* dns.c ***************************/
  613. void dns_init(void);
  614. int connection_dns_finished_flushing(connection_t *conn);
  615. int connection_dns_process_inbuf(connection_t *conn);
  616. void connection_dns_remove(connection_t *conn);
  617. void dns_cancel_pending_resolve(char *question);
  618. int dns_resolve(connection_t *exitconn);
  619. /********************************* main.c ***************************/
  620. int connection_add(connection_t *conn);
  621. int connection_remove(connection_t *conn);
  622. void connection_set_poll_socket(connection_t *conn);
  623. void get_connection_array(connection_t ***array, int *n);
  624. void connection_watch_events(connection_t *conn, short events);
  625. int connection_is_reading(connection_t *conn);
  626. void connection_stop_reading(connection_t *conn);
  627. void connection_start_reading(connection_t *conn);
  628. int connection_is_writing(connection_t *conn);
  629. void connection_stop_writing(connection_t *conn);
  630. void connection_start_writing(connection_t *conn);
  631. int main(int argc, char *argv[]);
  632. /********************************* onion.c ***************************/
  633. int decide_circ_id_type(char *local_nick, char *remote_nick);
  634. int onion_pending_add(circuit_t *circ);
  635. circuit_t *onion_next_task(void);
  636. void onion_pending_remove(circuit_t *circ);
  637. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  638. int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
  639. routerinfo_t **router_out);
  640. int onion_skin_create(crypto_pk_env_t *router_key,
  641. crypto_dh_env_t **handshake_state_out,
  642. char *onion_skin_out);
  643. int onion_skin_server_handshake(char *onion_skin,
  644. crypto_pk_env_t *private_key,
  645. char *handshake_reply_out,
  646. char *key_out,
  647. int key_out_len);
  648. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  649. char *handshake_reply,
  650. char *key_out,
  651. int key_out_len);
  652. cpath_build_state_t *onion_new_cpath_build_state(void);
  653. /********************************* router.c ***************************/
  654. void set_onion_key(crypto_pk_env_t *k);
  655. crypto_pk_env_t *get_onion_key(void);
  656. void set_identity_key(crypto_pk_env_t *k);
  657. crypto_pk_env_t *get_identity_key(void);
  658. crypto_pk_env_t *get_link_key(void);
  659. int init_keys(void);
  660. void router_retry_connections(void);
  661. void router_upload_desc_to_dirservers(void);
  662. int router_compare_to_my_exit_policy(connection_t *conn);
  663. const char *router_get_my_descriptor(void);
  664. int router_rebuild_descriptor(void);
  665. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  666. crypto_pk_env_t *ident_key);
  667. /********************************* routerlist.c ***************************/
  668. routerinfo_t *router_pick_directory_server(void);
  669. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  670. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
  671. routerinfo_t *router_get_by_nickname(char *nickname);
  672. void router_get_routerlist(routerlist_t **prouterlist);
  673. void routerinfo_free(routerinfo_t *router);
  674. void router_mark_as_down(char *nickname);
  675. int router_set_routerlist_from_file(char *routerfile);
  676. int router_set_routerlist_from_string(const char *s);
  677. int router_get_dir_hash(const char *s, char *digest);
  678. int router_get_router_hash(const char *s, char *digest);
  679. int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
  680. routerinfo_t *router_get_entry_from_string(const char **s);
  681. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  682. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  683. struct exit_policy_t *policy);
  684. #define ADDR_POLICY_ACCEPTED 0
  685. #define ADDR_POLICY_REJECTED -1
  686. #define ADDR_POLICY_UNKNOWN 1
  687. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  688. int router_exit_policy_rejects_all(routerinfo_t *router);
  689. /********************************* dirserv.c ***************************/
  690. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  691. int dirserv_parse_fingerprint_file(const char *fname);
  692. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  693. void dirserv_free_fingerprint_list();
  694. int dirserv_add_descriptor(const char **desc);
  695. int dirserv_init_from_directory_string(const char *dir);
  696. void dirserv_free_descriptors();
  697. int dirserv_dump_directory_to_string(char *s, int maxlen,
  698. crypto_pk_env_t *private_key);
  699. void directory_set_dirty();
  700. size_t dirserv_get_directory(const char **cp);
  701. #endif
  702. /*
  703. Local Variables:
  704. mode:c
  705. indent-tabs-mode:nil
  706. c-basic-offset:2
  707. End:
  708. */