or.h 35 KB

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