or.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. } cpath_build_state_t;
  421. /* struct for a path (circuit) through the network */
  422. #define CIRCUIT_MAGIC 0x35315243u
  423. struct circuit_t {
  424. uint32_t magic; /* for memory debugging. */
  425. int marked_for_close; /* Should we close this circuit at the end of the main
  426. * loop? */
  427. char *marked_for_close_file;
  428. uint32_t n_addr;
  429. uint16_t n_port;
  430. connection_t *p_conn;
  431. connection_t *n_conn; /* for the OR conn, if there is one */
  432. connection_t *p_streams;
  433. connection_t *n_streams;
  434. uint16_t next_stream_id;
  435. int package_window;
  436. int deliver_window;
  437. uint16_t p_circ_id; /* circuit identifiers */
  438. uint16_t n_circ_id;
  439. crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
  440. crypto_cipher_env_t *n_crypto;
  441. crypto_digest_env_t *p_digest; /* for integrity checking, */
  442. crypto_digest_env_t *n_digest; /* intermediate hops only */
  443. cpath_build_state_t *build_state;
  444. crypt_path_t *cpath;
  445. char onionskin[ONIONSKIN_CHALLENGE_LEN]; /* for storage while onionskin pending */
  446. time_t timestamp_created;
  447. time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
  448. uint8_t state;
  449. uint8_t purpose;
  450. /*
  451. * holds hash of location-hidden service's PK if purpose is INTRO_POINT
  452. * or S_ESTABLISH_INTRO or S_RENDEZVOUSING;
  453. * holds y portion of y.onion (zero-padded) if purpose is C_INTRODUCING or
  454. * C_ESTABLISH_REND, or is a C_GENERAL for a hidden service.
  455. * filled with zeroes otherwise.
  456. */
  457. char rend_service[CRYPTO_SHA1_DIGEST_LEN];
  458. /* Holds rendezvous cookie if purpose is REND_POINT_WAITING or
  459. * S_RENDEZVOUSING. Filled with zeroes otherwise.
  460. */
  461. char rend_cookie[REND_COOKIE_LEN];
  462. /* Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  463. * is not marked for close. */
  464. struct circuit_t *rend_splice;
  465. struct circuit_t *next;
  466. };
  467. typedef struct circuit_t circuit_t;
  468. typedef struct circuit_data_rend_point_t {
  469. /* for CIRCUIT_PURPOSE_INTRO_POINT (at OR, from Bob, waiting for intro) */
  470. char rend_cookie[20];
  471. } circuit_data_intro_point_t;
  472. typedef struct {
  473. char *LogLevel;
  474. char *LogFile;
  475. char *DebugLogFile;
  476. char *DataDirectory;
  477. char *RouterFile;
  478. char *Nickname;
  479. char *Address;
  480. char *PidFile;
  481. char *ExitNodes;
  482. char *EntryNodes;
  483. char *ExcludeNodes;
  484. char *ExitPolicy;
  485. char *SocksBindAddress;
  486. char *ORBindAddress;
  487. char *DirBindAddress;
  488. char *RecommendedVersions;
  489. char *User;
  490. char *Group;
  491. double PathlenCoinWeight;
  492. int ORPort;
  493. int SocksPort;
  494. int DirPort;
  495. int MaxConn;
  496. int TrafficShaping;
  497. int LinkPadding;
  498. int IgnoreVersion;
  499. int RunAsDaemon;
  500. int DirRebuildPeriod;
  501. int DirFetchPostPeriod;
  502. int KeepalivePeriod;
  503. int MaxOnionsPending;
  504. int NewCircuitPeriod;
  505. int BandwidthRate;
  506. int BandwidthBurst;
  507. int NumCpus;
  508. int loglevel;
  509. int RunTesting;
  510. struct config_line_t *RendConfigLines;
  511. } or_options_t;
  512. /* XXX are these good enough defaults? */
  513. #define MAX_SOCKS_REPLY_LEN 1024
  514. #define MAX_SOCKS_ADDR_LEN 256
  515. struct socks_request_t {
  516. char socks_version;
  517. int replylen;
  518. char reply[MAX_SOCKS_REPLY_LEN];
  519. int has_finished; /* has the socks handshake finished? */
  520. char address[MAX_SOCKS_ADDR_LEN];
  521. uint16_t port;
  522. };
  523. /* all the function prototypes go here */
  524. /********************************* buffers.c ***************************/
  525. int find_on_inbuf(char *string, int string_len, buf_t *buf);
  526. buf_t *buf_new();
  527. buf_t *buf_new_with_capacity(size_t size);
  528. void buf_free(buf_t *buf);
  529. void buf_clear(buf_t *buf);
  530. size_t buf_datalen(const buf_t *buf);
  531. size_t buf_capacity(const buf_t *buf);
  532. const char *_buf_peek_raw_buffer(const buf_t *buf);
  533. int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
  534. int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
  535. int flush_buf(int s, buf_t *buf, int *buf_flushlen);
  536. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
  537. int write_to_buf(const char *string, int string_len, buf_t *buf);
  538. int fetch_from_buf(char *string, int string_len, buf_t *buf);
  539. int fetch_from_buf_http(buf_t *buf,
  540. char **headers_out, int max_headerlen,
  541. char **body_out, int *body_used, int max_bodylen);
  542. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  543. void assert_buf_ok(buf_t *buf);
  544. /********************************* circuit.c ***************************/
  545. void circuit_add(circuit_t *circ);
  546. void circuit_remove(circuit_t *circ);
  547. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
  548. void circuit_close_all_marked(void);
  549. void circuit_free(circuit_t *circ);
  550. void circuit_free_cpath(crypt_path_t *cpath);
  551. int _circuit_mark_for_close(circuit_t *circ);
  552. #define circuit_mark_for_close(c) \
  553. do { \
  554. if (_circuit_mark_for_close(c)<0) { \
  555. log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
  556. __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
  557. } else { \
  558. c->marked_for_close_file = __FILE__; \
  559. c->marked_for_close = __LINE__; \
  560. } \
  561. } while (0)
  562. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
  563. circuit_t *circuit_get_by_conn(connection_t *conn);
  564. circuit_t *circuit_get_newest(connection_t *conn,
  565. int must_be_open, int must_be_clean);
  566. circuit_t *circuit_get_by_service_and_purpose(const char *servid, int purpose);
  567. void circuit_expire_building(void);
  568. int circuit_count_building(void);
  569. int circuit_stream_is_being_handled(connection_t *conn);
  570. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  571. int cell_direction);
  572. int circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  573. int cell_direction, crypt_path_t *layer_hint);
  574. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  575. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  576. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
  577. void circuit_detach_stream(circuit_t *circ, connection_t *conn);
  578. void circuit_about_to_close_connection(connection_t *conn);
  579. void circuit_log_path(int severity, circuit_t *circ);
  580. void circuit_dump_by_conn(connection_t *conn, int severity);
  581. void circuit_expire_unused_circuits(void);
  582. int circuit_launch_new(uint8_t purpose);
  583. void circuit_increment_failure_count(void);
  584. void circuit_reset_failure_count(void);
  585. void circuit_n_conn_open(connection_t *or_conn);
  586. int circuit_send_next_onion_skin(circuit_t *circ);
  587. int circuit_extend(cell_t *cell, circuit_t *circ);
  588. int circuit_finish_handshake(circuit_t *circ, char *reply);
  589. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  590. void assert_cpath_ok(const crypt_path_t *c);
  591. void assert_cpath_layer_ok(const crypt_path_t *c);
  592. void assert_circuit_ok(const circuit_t *c);
  593. extern unsigned long stats_n_relay_cells_relayed;
  594. extern unsigned long stats_n_relay_cells_delivered;
  595. /********************************* command.c ***************************/
  596. void command_process_cell(cell_t *cell, connection_t *conn);
  597. extern unsigned long stats_n_padding_cells_processed;
  598. extern unsigned long stats_n_create_cells_processed;
  599. extern unsigned long stats_n_created_cells_processed;
  600. extern unsigned long stats_n_relay_cells_processed;
  601. extern unsigned long stats_n_destroy_cells_processed;
  602. /********************************* config.c ***************************/
  603. struct config_line_t {
  604. char *key;
  605. char *value;
  606. struct config_line_t *next;
  607. };
  608. int config_assign_default_dirservers(void);
  609. int getconfig(int argc, char **argv, or_options_t *options);
  610. /********************************* connection.c ***************************/
  611. #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
  612. "Unknown" : conn_type_to_string[(t)])
  613. extern char *conn_type_to_string[];
  614. connection_t *connection_new(int type);
  615. void connection_free(connection_t *conn);
  616. void connection_free_all(void);
  617. void connection_close_immediate(connection_t *conn);
  618. int _connection_mark_for_close(connection_t *conn, char reason);
  619. #define connection_mark_for_close(c,r) \
  620. do { \
  621. if (_connection_mark_for_close(c,r)<0) { \
  622. log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
  623. __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
  624. } else { \
  625. c->marked_for_close_file = __FILE__; \
  626. c->marked_for_close = __LINE__; \
  627. } \
  628. } while (0)
  629. void connection_expire_held_open(void);
  630. int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
  631. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  632. int retry_all_connections(void);
  633. void connection_bucket_init(void);
  634. void connection_bucket_refill(struct timeval *now);
  635. int connection_handle_read(connection_t *conn);
  636. int connection_read_to_buf(connection_t *conn);
  637. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  638. int connection_find_on_inbuf(char *string, int len, connection_t *conn);
  639. int connection_wants_to_flush(connection_t *conn);
  640. int connection_outbuf_too_full(connection_t *conn);
  641. int connection_handle_write(connection_t *conn);
  642. void connection_write_to_buf(const char *string, int len, connection_t *conn);
  643. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  644. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  645. connection_t *connection_get_by_type(int type);
  646. connection_t *connection_get_by_type_state(int type, int state);
  647. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  648. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  649. #define connection_has_pending_tls_data(conn) \
  650. ((conn)->type == CONN_TYPE_OR && \
  651. (conn)->state == OR_CONN_STATE_OPEN && \
  652. tor_tls_get_pending_bytes((conn)->tls))
  653. int connection_is_listener(connection_t *conn);
  654. int connection_state_is_open(connection_t *conn);
  655. int connection_send_destroy(uint16_t circ_id, connection_t *conn);
  656. int connection_process_inbuf(connection_t *conn);
  657. int connection_finished_flushing(connection_t *conn);
  658. void assert_connection_ok(connection_t *conn, time_t now);
  659. /********************************* connection_edge.c ***************************/
  660. void relay_header_pack(char *dest, const relay_header_t *src);
  661. void relay_header_unpack(relay_header_t *dest, const char *src);
  662. int connection_edge_process_inbuf(connection_t *conn);
  663. int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
  664. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  665. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  666. int relay_command, void *payload,
  667. int payload_len, crypt_path_t *cpath_layer);
  668. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  669. connection_t *conn, int edge_type,
  670. crypt_path_t *layer_hint);
  671. int connection_edge_finished_flushing(connection_t *conn);
  672. int connection_edge_package_raw_inbuf(connection_t *conn);
  673. int connection_ap_make_bridge(char *address, uint16_t port);
  674. void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  675. int replylen, char success);
  676. void connection_exit_connect(connection_t *conn);
  677. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  678. void connection_ap_expire_beginning(void);
  679. void connection_ap_attach_pending(void);
  680. extern uint64_t stats_n_data_cells_packaged;
  681. extern uint64_t stats_n_data_bytes_packaged;
  682. extern uint64_t stats_n_data_cells_received;
  683. extern uint64_t stats_n_data_bytes_received;
  684. void client_dns_init(void);
  685. void client_dns_clean(void);
  686. /********************************* connection_or.c ***************************/
  687. int connection_or_process_inbuf(connection_t *conn);
  688. int connection_or_finished_flushing(connection_t *conn);
  689. void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
  690. connection_t *connection_or_connect(routerinfo_t *router);
  691. int connection_tls_start_handshake(connection_t *conn, int receiving);
  692. int connection_tls_continue_handshake(connection_t *conn);
  693. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
  694. /********************************* cpuworker.c *****************************/
  695. void cpu_init(void);
  696. int connection_cpu_finished_flushing(connection_t *conn);
  697. int connection_cpu_process_inbuf(connection_t *conn);
  698. int cpuworker_main(void *data);
  699. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  700. void *task);
  701. /********************************* directory.c ***************************/
  702. void directory_initiate_command(routerinfo_t *router, int purpose,
  703. const char *payload, int payload_len);
  704. int connection_dir_process_inbuf(connection_t *conn);
  705. int connection_dir_finished_flushing(connection_t *conn);
  706. /********************************* dns.c ***************************/
  707. void dns_init(void);
  708. int connection_dns_finished_flushing(connection_t *conn);
  709. int connection_dns_process_inbuf(connection_t *conn);
  710. void connection_dns_remove(connection_t *conn);
  711. void assert_connection_edge_not_dns_pending(connection_t *conn);
  712. void dns_cancel_pending_resolve(char *question);
  713. int dns_resolve(connection_t *exitconn);
  714. /********************************* main.c ***************************/
  715. int connection_add(connection_t *conn);
  716. int connection_remove(connection_t *conn);
  717. void connection_set_poll_socket(connection_t *conn);
  718. void get_connection_array(connection_t ***array, int *n);
  719. void connection_watch_events(connection_t *conn, short events);
  720. int connection_is_reading(connection_t *conn);
  721. void connection_stop_reading(connection_t *conn);
  722. void connection_start_reading(connection_t *conn);
  723. int connection_is_writing(connection_t *conn);
  724. void connection_stop_writing(connection_t *conn);
  725. void connection_start_writing(connection_t *conn);
  726. void directory_has_arrived(void);
  727. int main(int argc, char *argv[]);
  728. /********************************* onion.c ***************************/
  729. int decide_circ_id_type(char *local_nick, char *remote_nick);
  730. int onion_pending_add(circuit_t *circ);
  731. circuit_t *onion_next_task(void);
  732. void onion_pending_remove(circuit_t *circ);
  733. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  734. int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state,
  735. routerinfo_t **router_out);
  736. int onion_skin_create(crypto_pk_env_t *router_key,
  737. crypto_dh_env_t **handshake_state_out,
  738. char *onion_skin_out);
  739. int onion_skin_server_handshake(char *onion_skin,
  740. crypto_pk_env_t *private_key,
  741. char *handshake_reply_out,
  742. char *key_out,
  743. int key_out_len);
  744. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  745. char *handshake_reply,
  746. char *key_out,
  747. int key_out_len);
  748. cpath_build_state_t *onion_new_cpath_build_state(void);
  749. /********************************* router.c ***************************/
  750. void set_onion_key(crypto_pk_env_t *k);
  751. crypto_pk_env_t *get_onion_key(void);
  752. void set_identity_key(crypto_pk_env_t *k);
  753. crypto_pk_env_t *get_identity_key(void);
  754. crypto_pk_env_t *get_link_key(void);
  755. int init_keys(void);
  756. crypto_pk_env_t *init_key_from_file(const char *fname);
  757. void router_retry_connections(void);
  758. void router_upload_desc_to_dirservers(void);
  759. int router_compare_to_my_exit_policy(connection_t *conn);
  760. const char *router_get_my_descriptor(void);
  761. int router_rebuild_descriptor(void);
  762. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  763. crypto_pk_env_t *ident_key);
  764. /********************************* routerlist.c ***************************/
  765. routerinfo_t *router_pick_directory_server(void);
  766. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  767. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
  768. routerinfo_t *router_get_by_nickname(char *nickname);
  769. void router_get_routerlist(routerlist_t **prouterlist);
  770. void routerinfo_free(routerinfo_t *router);
  771. void router_mark_as_down(char *nickname);
  772. int router_set_routerlist_from_file(char *routerfile);
  773. int router_set_routerlist_from_string(const char *s);
  774. int router_get_dir_hash(const char *s, char *digest);
  775. int router_get_router_hash(const char *s, char *digest);
  776. int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey);
  777. routerinfo_t *router_get_entry_from_string(const char *s, const char *end);
  778. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  779. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  780. struct exit_policy_t *policy);
  781. #define ADDR_POLICY_ACCEPTED 0
  782. #define ADDR_POLICY_REJECTED -1
  783. #define ADDR_POLICY_UNKNOWN 1
  784. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  785. int router_exit_policy_rejects_all(routerinfo_t *router);
  786. /********************************* dirserv.c ***************************/
  787. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  788. int dirserv_parse_fingerprint_file(const char *fname);
  789. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  790. void dirserv_free_fingerprint_list();
  791. int dirserv_add_descriptor(const char **desc);
  792. int dirserv_init_from_directory_string(const char *dir);
  793. void dirserv_free_descriptors();
  794. int dirserv_dump_directory_to_string(char *s, int maxlen,
  795. crypto_pk_env_t *private_key);
  796. void directory_set_dirty(void);
  797. size_t dirserv_get_directory(const char **cp);
  798. void dirserv_remove_old_servers(void);
  799. /********************************* rephist.c ***************************/
  800. void rep_hist_init(void);
  801. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  802. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  803. void rep_hist_note_disconnect(const char* nickname, time_t when);
  804. void rep_hist_note_connection_died(const char* nickname, time_t when);
  805. void rep_hist_note_extend_succeeded(const char *from_name,
  806. const char *to_name);
  807. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  808. void rep_hist_dump_stats(time_t now, int severity);
  809. /********************************* rendcommon.c ***************************/
  810. /* length of 'y' portion of 'y.onion' URL. */
  811. #define REND_SERVICE_ID_LEN 16
  812. typedef struct rend_service_descriptor_t {
  813. crypto_pk_env_t *pk;
  814. time_t timestamp;
  815. int n_intro_points;
  816. char **intro_points;
  817. } rend_service_descriptor_t;
  818. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  819. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  820. crypto_pk_env_t *key,
  821. char **str_out,
  822. int *len_out);
  823. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, int len);
  824. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  825. void rend_cache_init(void);
  826. void rend_cache_clean(void);
  827. int rend_cache_lookup(char *query, const char **desc, int *desc_len);
  828. int rend_cache_store(char *desc, int desc_len);
  829. int rend_parse_rendezvous_address(char *address);
  830. /********************************* rendservice.c ***************************/
  831. int rend_config_services(or_options_t *options);
  832. int rend_service_init_keys(void);
  833. #endif
  834. /*
  835. Local Variables:
  836. mode:c
  837. indent-tabs-mode:nil
  838. c-basic-offset:2
  839. End:
  840. */