or.h 32 KB

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