or.h 33 KB

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