or.h 36 KB

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