or.h 30 KB

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