or.h 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file or.h
  6. *
  7. * \brief Master header file for Tor-specific functionality.
  8. */
  9. #ifndef __OR_H
  10. #define __OR_H
  11. #include "orconfig.h"
  12. #ifdef MS_WINDOWS
  13. #define WIN32_WINNT 0x400
  14. #define _WIN32_WINNT 0x400
  15. #define WIN32_LEAN_AND_MEAN
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <limits.h>
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef HAVE_STRING_H
  24. #include <string.h>
  25. #endif
  26. #ifdef HAVE_SIGNAL_H
  27. #include <signal.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_CTYPE_H
  33. #include <ctype.h>
  34. #endif
  35. #include "../common/torint.h"
  36. #include "../common/fakepoll.h"
  37. #ifdef HAVE_INTTYPES_H
  38. #include <inttypes.h>
  39. #endif
  40. #ifdef HAVE_SYS_PARAM_H
  41. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  42. #endif
  43. #ifdef HAVE_SYS_LIMITS_H
  44. #include <sys/limits.h>
  45. #endif
  46. #ifdef HAVE_MACHINE_LIMITS_H
  47. #ifndef __FreeBSD__
  48. /* FreeBSD has a bug where it complains that this file is obsolete,
  49. and I should migrate to using sys/limits. It complains even when
  50. I include both. */
  51. #include <machine/limits.h>
  52. #endif
  53. #endif
  54. #ifdef HAVE_SYS_TYPES_H
  55. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  56. #endif
  57. #ifdef HAVE_SYS_WAIT_H
  58. #include <sys/wait.h>
  59. #endif
  60. #ifdef HAVE_SYS_FCNTL_H
  61. #include <sys/fcntl.h>
  62. #endif
  63. #ifdef HAVE_FCNTL_H
  64. #include <fcntl.h>
  65. #endif
  66. #ifdef HAVE_SYS_IOCTL_H
  67. #include <sys/ioctl.h>
  68. #endif
  69. #ifdef HAVE_SYS_SOCKET_H
  70. #include <sys/socket.h>
  71. #endif
  72. #ifdef HAVE_SYS_TIME_H
  73. #include <sys/time.h>
  74. #endif
  75. #ifdef HAVE_SYS_STAT_H
  76. #include <sys/stat.h>
  77. #endif
  78. #ifdef HAVE_NETINET_IN_H
  79. #include <netinet/in.h>
  80. #endif
  81. #ifdef HAVE_ARPA_INET_H
  82. #include <arpa/inet.h>
  83. #endif
  84. #ifdef HAVE_ERRNO_H
  85. #include <errno.h>
  86. #endif
  87. #ifdef HAVE_ASSERT_H
  88. #include <assert.h>
  89. #endif
  90. #ifdef HAVE_TIME_H
  91. #include <time.h>
  92. #endif
  93. #ifdef HAVE_WINSOCK_H
  94. #include <winsock.h>
  95. #endif
  96. #if _MSC_VER > 1300
  97. #include <winsock2.h>
  98. #include <ws2tcpip.h>
  99. #elif defined(_MSC_VER)
  100. #include <winsock.h>
  101. #endif
  102. #ifdef MS_WINDOWS
  103. #include <io.h>
  104. #include <process.h>
  105. #include <direct.h>
  106. #include <windows.h>
  107. #define snprintf _snprintf
  108. #endif
  109. #include "../common/crypto.h"
  110. #include "../common/tortls.h"
  111. #include "../common/log.h"
  112. #include "../common/util.h"
  113. /** Upper bound on maximum simultaneous connections; can be lowered by
  114. * config file. */
  115. #define MAXCONNECTIONS 10000
  116. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  117. #define MAX_NICKNAME_LEN 19
  118. #define MAX_DIR_SIZE 500000
  119. #ifdef TOR_PERF
  120. /** How long do we keep DNS cache entries before purging them? */
  121. #define MAX_DNS_ENTRY_AGE (150*60)
  122. #else
  123. #define MAX_DNS_ENTRY_AGE (15*60)
  124. #endif
  125. /** How often do we rotate onion keys? */
  126. #define MIN_ONION_KEY_LIFETIME (120*60)
  127. /** How often do we rotate TLS contexts? */
  128. #define MAX_SSL_KEY_LIFETIME (120*60)
  129. /** How old do we allow a router to get before removing it, either
  130. * from the descriptor list (for dirservers) or the router list (for others)?
  131. * In seconds. */
  132. #define ROUTER_MAX_AGE (60*60*24)
  133. #define CIRC_ID_TYPE_LOWER 0
  134. #define CIRC_ID_TYPE_HIGHER 1
  135. #define _CONN_TYPE_MIN 3
  136. /** Type for sockets listening for OR connections. */
  137. #define CONN_TYPE_OR_LISTENER 3
  138. /** Type for OR-to-OR or OP-to-OR connections. */
  139. #define CONN_TYPE_OR 4
  140. /** Type for connections from final OR to chosen destination. */
  141. #define CONN_TYPE_EXIT 5
  142. /** Type for sockets listening for SOCKS connections. */
  143. #define CONN_TYPE_AP_LISTENER 6
  144. /** Type for SOCKS connections to OP. */
  145. #define CONN_TYPE_AP 7
  146. /** Type for sockets listening for HTTP connections to the directory server. */
  147. #define CONN_TYPE_DIR_LISTENER 8
  148. /** Type for HTTP connections to the directory server. */
  149. #define CONN_TYPE_DIR 9
  150. /** Type for connections to local dnsworker processes. */
  151. #define CONN_TYPE_DNSWORKER 10
  152. /** Type for connections to local cpuworker processes. */
  153. #define CONN_TYPE_CPUWORKER 11
  154. #define _CONN_TYPE_MAX 11
  155. /** State for any listener connection. */
  156. #define LISTENER_STATE_READY 0
  157. #define _DNSWORKER_STATE_MIN 1
  158. /** State for a connection to a dnsworker process that's idle. */
  159. #define DNSWORKER_STATE_IDLE 1
  160. /** State for a connection to a dnsworker process that's resolving a hostname. */
  161. #define DNSWORKER_STATE_BUSY 2
  162. #define _DNSWORKER_STATE_MAX 2
  163. #define _CPUWORKER_STATE_MIN 1
  164. /** State for a connection to a cpuworker process that's idle. */
  165. #define CPUWORKER_STATE_IDLE 1
  166. /** State for a connection to a cpuworker process that's processing a
  167. * handshake. */
  168. #define CPUWORKER_STATE_BUSY_ONION 2
  169. #define _CPUWORKER_STATE_MAX 2
  170. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  171. #define _OR_CONN_STATE_MIN 1
  172. /** State for a connection to an OR: waiting for connect() to finish. */
  173. #define OR_CONN_STATE_CONNECTING 1
  174. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  175. #define OR_CONN_STATE_HANDSHAKING 2
  176. /** State for a connection to an OR: Ready to send/receive cells. */
  177. #define OR_CONN_STATE_OPEN 3
  178. #define _OR_CONN_STATE_MAX 3
  179. #define _EXIT_CONN_STATE_MIN 1
  180. /** State for an exit connection: waiting for response from dns farm. */
  181. #define EXIT_CONN_STATE_RESOLVING 1
  182. /** State for an exit connection: waiting for connect() to finish. */
  183. #define EXIT_CONN_STATE_CONNECTING 2
  184. /** State for an exit connection: open and ready to transmit data. */
  185. #define EXIT_CONN_STATE_OPEN 3
  186. /** State for an exit connection: waiting to be removed. */
  187. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  188. #define _EXIT_CONN_STATE_MAX 4
  189. #if 0
  190. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  191. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  192. #endif
  193. /* the AP state values must be disjoint from the EXIT state values */
  194. #define _AP_CONN_STATE_MIN 5
  195. /** State for a SOCKS connection: waiting for SOCKS request. */
  196. #define AP_CONN_STATE_SOCKS_WAIT 5
  197. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  198. * rendezvous rescriptor. */
  199. #define AP_CONN_STATE_RENDDESC_WAIT 6
  200. /** State for a SOCKS connection: waiting for a completed circuit. */
  201. #define AP_CONN_STATE_CIRCUIT_WAIT 7
  202. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  203. #define AP_CONN_STATE_CONNECT_WAIT 8
  204. /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
  205. #define AP_CONN_STATE_RESOLVE_WAIT 9
  206. /** State for a SOCKS connection: ready to send and receive. */
  207. #define AP_CONN_STATE_OPEN 10
  208. #define _AP_CONN_STATE_MAX 10
  209. #define _DIR_CONN_STATE_MIN 1
  210. /** State for connection to directory server: waiting for connect(). */
  211. #define DIR_CONN_STATE_CONNECTING 1
  212. /** State for connection to directory server: sending HTTP request. */
  213. #define DIR_CONN_STATE_CLIENT_SENDING 2
  214. /** State for connection to directory server: reading HTTP response. */
  215. #define DIR_CONN_STATE_CLIENT_READING 3
  216. /** State for connection at directory server: waiting for HTTP request. */
  217. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
  218. /** State for connection at directory server: sending HTTP response. */
  219. #define DIR_CONN_STATE_SERVER_WRITING 5
  220. #define _DIR_CONN_STATE_MAX 5
  221. #define _DIR_PURPOSE_MIN 1
  222. /** Purpose for connection to directory server: download a directory. */
  223. #define DIR_PURPOSE_FETCH_DIR 1
  224. /** Purpose for connection to directory server: download just the list
  225. * of running routers. */
  226. #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
  227. /** Purpose for connection to directory server: download a rendezvous
  228. * descriptor. */
  229. #define DIR_PURPOSE_FETCH_RENDDESC 3
  230. /** Purpose for connection to directory server: set after a rendezvous
  231. * descriptor is downloaded. */
  232. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
  233. /** Purpose for connection to directory server: upload a server descriptor. */
  234. #define DIR_PURPOSE_UPLOAD_DIR 5
  235. /** Purpose for connection to directory server: upload a rendezvous
  236. * descriptor. */
  237. #define DIR_PURPOSE_UPLOAD_RENDDESC 6
  238. /** Purpose for connection at a directory server. */
  239. #define DIR_PURPOSE_SERVER 7
  240. #define _DIR_PURPOSE_MAX 7
  241. #define _EXIT_PURPOSE_MIN 1
  242. #define EXIT_PURPOSE_CONNECT 1
  243. #define EXIT_PURPOSE_RESOLVE 2
  244. #define _EXIT_PURPOSE_MAX 2
  245. /** Circuit state: I'm the OP, still haven't done all my handshakes. */
  246. #define CIRCUIT_STATE_BUILDING 0
  247. /** Circuit state: Waiting to process the onionskin. */
  248. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  249. /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
  250. #define CIRCUIT_STATE_OR_WAIT 2
  251. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  252. #define CIRCUIT_STATE_OPEN 3
  253. #define _CIRCUIT_PURPOSE_MIN 1
  254. /* these circuits were initiated elsewhere */
  255. #define _CIRCUIT_PURPOSE_OR_MIN 1
  256. /** OR-side circuit purpose: normal circuit, at OR. */
  257. #define CIRCUIT_PURPOSE_OR 1
  258. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  259. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  260. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  261. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  262. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  263. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  264. #define _CIRCUIT_PURPOSE_OR_MAX 4
  265. /* these circuits originate at this node */
  266. /* here's how circ client-side purposes work:
  267. * normal circuits are C_GENERAL.
  268. * circuits that are c_introducing are either on their way to
  269. * becoming open, or they are open and waiting for a
  270. * suitable rendcirc before they send the intro.
  271. * circuits that are c_introduce_ack_wait have sent the intro,
  272. * but haven't gotten a response yet.
  273. * circuits that are c_establish_rend are either on their way
  274. * to becoming open, or they are open and have sent the
  275. * establish_rendezvous cell but haven't received an ack.
  276. * circuits that are c_rend_ready are open and have received a
  277. * rend ack, but haven't heard from bob yet. if they have a
  278. * buildstate->pending_final_cpath then they're expecting a
  279. * cell from bob, else they're not.
  280. * circuits that are c_rend_ready_intro_acked are open, and
  281. * some intro circ has sent its intro and received an ack.
  282. * circuits that are c_rend_joined are open, have heard from
  283. * bob, and are talking to him.
  284. */
  285. /** Client-side circuit purpose: Normal circuit, with cpath. */
  286. #define CIRCUIT_PURPOSE_C_GENERAL 5
  287. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  288. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  289. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
  290. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  291. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  292. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  293. /** Client-side circuit purpose: at Alice, waiting for ack. */
  294. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  295. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  296. #define CIRCUIT_PURPOSE_C_REND_READY 10
  297. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  298. * has been acknowledged. */
  299. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  300. /** Client-side circuit purpose: at Alice, rendezvous established. */
  301. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  302. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  303. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  304. /** Hidden-service-side circuit purpose: at Bob, successfully established
  305. * intro. */
  306. #define CIRCUIT_PURPOSE_S_INTRO 14
  307. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  308. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  309. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  310. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  311. #define _CIRCUIT_PURPOSE_MAX 16
  312. /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
  313. * that this OP has originated. */
  314. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  315. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  316. #define RELAY_COMMAND_BEGIN 1
  317. #define RELAY_COMMAND_DATA 2
  318. #define RELAY_COMMAND_END 3
  319. #define RELAY_COMMAND_CONNECTED 4
  320. #define RELAY_COMMAND_SENDME 5
  321. #define RELAY_COMMAND_EXTEND 6
  322. #define RELAY_COMMAND_EXTENDED 7
  323. #define RELAY_COMMAND_TRUNCATE 8
  324. #define RELAY_COMMAND_TRUNCATED 9
  325. #define RELAY_COMMAND_DROP 10
  326. #define RELAY_COMMAND_RESOLVE 11
  327. #define RELAY_COMMAND_RESOLVED 12
  328. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  329. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  330. #define RELAY_COMMAND_INTRODUCE1 34
  331. #define RELAY_COMMAND_INTRODUCE2 35
  332. #define RELAY_COMMAND_RENDEZVOUS1 36
  333. #define RELAY_COMMAND_RENDEZVOUS2 37
  334. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  335. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  336. #define RELAY_COMMAND_INTRODUCE_ACK 40
  337. #define _MIN_END_STREAM_REASON 1
  338. #define END_STREAM_REASON_MISC 1
  339. #define END_STREAM_REASON_RESOLVEFAILED 2
  340. #define END_STREAM_REASON_CONNECTFAILED 3
  341. #define END_STREAM_REASON_EXITPOLICY 4
  342. #define END_STREAM_REASON_DESTROY 5
  343. #define END_STREAM_REASON_DONE 6
  344. #define END_STREAM_REASON_TIMEOUT 7
  345. #define _MAX_END_STREAM_REASON 7
  346. #define RESOLVED_TYPE_IPV4 4
  347. #define RESOLVED_TYPE_IPV6 6
  348. #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
  349. #define RESOLVED_TYPE_ERROR 0xF1
  350. /** Length of 'y' portion of 'y.onion' URL. */
  351. #define REND_SERVICE_ID_LEN 16
  352. #define CELL_DIRECTION_IN 1
  353. #define CELL_DIRECTION_OUT 2
  354. #ifdef TOR_PERF
  355. #define CIRCWINDOW_START 10000
  356. #define CIRCWINDOW_INCREMENT 1000
  357. #define STREAMWINDOW_START 5000
  358. #define STREAMWINDOW_INCREMENT 500
  359. #else
  360. #define CIRCWINDOW_START 1000
  361. #define CIRCWINDOW_INCREMENT 100
  362. #define STREAMWINDOW_START 500
  363. #define STREAMWINDOW_INCREMENT 50
  364. #endif
  365. /* cell commands */
  366. #define CELL_PADDING 0
  367. #define CELL_CREATE 1
  368. #define CELL_CREATED 2
  369. #define CELL_RELAY 3
  370. #define CELL_DESTROY 4
  371. /* legal characters in a nickname */
  372. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  373. #define SOCKS4_NETWORK_LEN 8
  374. /*
  375. * Relay payload:
  376. * Relay command [1 byte]
  377. * Recognized [2 bytes]
  378. * Stream ID [2 bytes]
  379. * Partial SHA-1 [4 bytes]
  380. * Length [2 bytes]
  381. * Relay payload [498 bytes]
  382. */
  383. #define CELL_PAYLOAD_SIZE 509
  384. #define CELL_NETWORK_SIZE 512
  385. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  386. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  387. /** Parsed onion routing cell. All communication from OP-to-OR, or from
  388. * OR-to-OR, is via cells. */
  389. typedef struct {
  390. uint16_t circ_id; /**< Circuit which received the cell. */
  391. unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  392. * or DESTROY. */
  393. unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  394. } cell_t;
  395. /** Beginning of a RELAY cell payload. */
  396. typedef struct {
  397. uint8_t command; /**< The end-to-end relay command. */
  398. uint16_t recognized; /**< Used to tell whether cell is for us. */
  399. uint16_t stream_id; /**< Which stream is this cell associated with? */
  400. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  401. uint16_t length; /**< How long is the payload body? */
  402. } relay_header_t;
  403. typedef struct buf_t buf_t;
  404. typedef struct socks_request_t socks_request_t;
  405. #define CONNECTION_MAGIC 0x7C3C304Eu
  406. /** Description of a connection to another host or process, and associated
  407. * data. */
  408. struct connection_t {
  409. uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
  410. uint8_t type; /**< What kind of connection is this? */
  411. uint8_t state; /**< Current state of this connection. */
  412. uint8_t purpose; /**< Only used for DIR types currently. */
  413. uint8_t wants_to_read; /**< Boolean: should we start reading again once
  414. * the bandwidth throttler allows it?
  415. */
  416. uint8_t wants_to_write; /**< Boolean: should we start writing again once
  417. * the bandwidth throttler allows reads?
  418. */
  419. int s; /**< Our socket; -1 if this connection is closed. */
  420. int poll_index; /**< Index of this conn into the poll_array. */
  421. int marked_for_close; /**< Boolean: should we close this conn on the next
  422. * iteration of the main loop?
  423. */
  424. char *marked_for_close_file; /**< For debugging: in which file were we marked
  425. * for close? */
  426. int hold_open_until_flushed; /**< Despite this connection's being marked
  427. * for close, do we flush it before closing it?
  428. */
  429. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  430. int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
  431. time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
  432. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  433. int outbuf_flushlen; /**< How much data should we try to flush from the
  434. * outbuf? */
  435. time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
  436. time_t timestamp_created; /**< When was this connection_t created? */
  437. uint32_t addr; /**< IP of the other side of the connection; used to identify
  438. * routers, along with port. */
  439. uint16_t port; /**< If non-zero, port on the other end
  440. * of the connection. */
  441. char *address; /**< FQDN (or IP) of the guy on the other end.
  442. * strdup into this, because free_connection frees it.
  443. */
  444. crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
  445. * signing key. */
  446. char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
  447. char *nickname; /**< Nickname of OR on other side (if any). */
  448. /* Used only by OR connections: */
  449. tor_tls *tls; /**< TLS connection state (OR only.) */
  450. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  451. * this connection? This is always in the
  452. * range 0..1<<15-1. (OR only.)*/
  453. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  454. int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
  455. int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
  456. * add 'bandwidth' to this, capping it at 10*bandwidth.
  457. * (OPEN ORs only)
  458. */
  459. /* Used only by DIR and AP connections: */
  460. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  461. * querying for? (DIR/AP only) */
  462. /* Used only by edge connections: */
  463. uint16_t stream_id;
  464. struct connection_t *next_stream; /**< Points to the next stream at this
  465. * edge, if any (Edge only). */
  466. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  467. * this conn exits at. (Edge only.) */
  468. int package_window; /**< How many more relay cells can i send into the
  469. * circuit? (Edge only.) */
  470. int deliver_window; /**< How many more relay cells can end at me? (Edge
  471. * only.) */
  472. int done_sending; /**< For half-open connections; not used currently. */
  473. int done_receiving; /**< For half-open connections; not used currently. */
  474. char has_sent_end; /**< For debugging: set once we've set the stream end,
  475. and check in circuit_about_to_close_connection(). */
  476. char num_retries; /**< How many times have we re-tried beginning this stream? (Edge only) */
  477. /* Used only by AP connections */
  478. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  479. * only.) */
  480. };
  481. typedef struct connection_t connection_t;
  482. #define EXIT_POLICY_ACCEPT 1
  483. #define EXIT_POLICY_REJECT 2
  484. /** A linked list of exit policy rules */
  485. struct exit_policy_t {
  486. char policy_type; /**< One of EXIT_POLICY_ACCEPT or EXIT_POLICY_REJECT. */
  487. char *string; /**< String representation of this rule. */
  488. uint32_t addr; /**< Base address to accept or reject. */
  489. uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that a & msk ==
  490. * <b>addr</b> & msk . */
  491. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  492. uint16_t prt_max; /**< Highest port number to accept/reject. */
  493. struct exit_policy_t *next; /**< Next rule in list. */
  494. };
  495. /** Information about another onion router in the network. */
  496. typedef struct {
  497. char *address; /**< Location of OR: either a hostname or an IP address. */
  498. char *nickname; /**< Human-readable OR name. */
  499. uint32_t addr; /**< IPv4 address of OR, in host order. */
  500. uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
  501. uint16_t socks_port; /**< Port for SOCKS connections. */
  502. uint16_t dir_port; /**< Port for HTTP directory connections. */
  503. time_t published_on; /**< When was the information in this routerinfo_t
  504. * published? */
  505. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  506. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  507. char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
  508. char *platform; /**< What software/operating system is this OR using? */
  509. /* link info */
  510. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  511. * bucket per second? */
  512. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  513. /** How many bytes/s is this router known to handle? */
  514. uint32_t advertisedbandwidth;
  515. struct exit_policy_t *exit_policy; /**< What streams will this OR permit
  516. * to exit? */
  517. /* local info */
  518. int is_running; /**< As far as we know, is this OR currently running? */
  519. time_t status_set_at; /**< When did we last update is_running? */
  520. int is_verified; /**< Has a trusted dirserver validated this OR? */
  521. int is_trusted_dir; /**< Do we trust this OR as a directory server? */
  522. } routerinfo_t;
  523. /** Contents of a directory of onion routers. */
  524. typedef struct {
  525. /** List of routerinfo_t */
  526. smartlist_t *routers;
  527. /** Which versions of tor are recommended by this directory? */
  528. char *software_versions;
  529. /** When was the most recent directory that contributed to this list
  530. * published?
  531. */
  532. time_t published_on;
  533. time_t running_routers_updated_on;
  534. /** Which router is claimed to have signed it? */
  535. char *signing_router;
  536. } routerlist_t;
  537. /** Contents of a running-routers list */
  538. typedef struct running_routers_t {
  539. time_t published_on; /**< When was the list marked as published? */
  540. /** Which ORs are on the list? Entries may be prefixed with ! and $. */
  541. smartlist_t *running_routers;
  542. } running_routers_t;
  543. /** Holds accounting information for a single step in the layered encryption
  544. * performed by a circuit. Used only at the client edge of a circuit. */
  545. struct crypt_path_t {
  546. /* crypto environments */
  547. /** Encryption key and counter for cells heading towards the OR at this
  548. * step. */
  549. crypto_cipher_env_t *f_crypto;
  550. /** Encryption key and counter for cells heading back from the OR at this
  551. * step. */
  552. crypto_cipher_env_t *b_crypto;
  553. /** Digest state for cells heading towards the OR at this step. */
  554. crypto_digest_env_t *f_digest; /* for integrity checking */
  555. /** Digest state for cells heading away from the OR at this step. */
  556. crypto_digest_env_t *b_digest;
  557. /** Current state of Diffie-Hellman key negotiation with the OR at this
  558. * step. */
  559. crypto_dh_env_t *handshake_state;
  560. /** Negotiated key material shared with the OR at this step. */
  561. char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
  562. /** IP4 address of the OR at this step. */
  563. uint32_t addr;
  564. /** Port of the OR at this step. */
  565. uint16_t port;
  566. /** Identity key digest of the OR at this step. */
  567. char identity_digest[DIGEST_LEN];
  568. /** Is the circuit built to this step? Must be one of:
  569. * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
  570. * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
  571. * and not received an EXTENDED/CREATED)
  572. * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
  573. uint8_t state;
  574. #define CPATH_STATE_CLOSED 0
  575. #define CPATH_STATE_AWAITING_KEYS 1
  576. #define CPATH_STATE_OPEN 2
  577. struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
  578. * (The list is circular, so the last node
  579. * links to the first.) */
  580. struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
  581. * circuit. */
  582. int package_window; /**< How many bytes are we allowed to originate ending
  583. * at this step? */
  584. int deliver_window; /**< How many bytes are we willing to deliver originating
  585. * at this step? */
  586. };
  587. #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
  588. #define DH_KEY_LEN DH_BYTES
  589. #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
  590. CIPHER_KEY_LEN+\
  591. DH_KEY_LEN)
  592. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
  593. #define REND_COOKIE_LEN DIGEST_LEN
  594. typedef struct crypt_path_t crypt_path_t;
  595. /** Information used to build a circuit. */
  596. typedef struct {
  597. /** Intended length of the final circuit. */
  598. int desired_path_len;
  599. /** Nickname of planned exit node. */
  600. char *chosen_exit_name;
  601. /** Identity of planned exit node. */
  602. char chosen_exit_digest[DIGEST_LEN];
  603. /** The crypt_path_t to append after rendezvous: used for rendezvous. */
  604. struct crypt_path_t *pending_final_cpath;
  605. /** How many times has building a circuit for this task failed? */
  606. int failure_count;
  607. } cpath_build_state_t;
  608. #define CIRCUIT_MAGIC 0x35315243u
  609. /** Struct for a path (circuit) through the onion routing network. */
  610. struct circuit_t {
  611. uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
  612. int marked_for_close; /**< Should we close this circuit at the end of the
  613. * main loop? */
  614. char *marked_for_close_file; /**< For debugging: in which file was this
  615. * circuit marked for close? */
  616. /** The IPv4 address of the OR that is next in this circuit. */
  617. uint32_t n_addr;
  618. /** The port for the OR that is next in this circuit. */
  619. uint16_t n_port;
  620. /** The OR connection that is previous in this circuit. */
  621. connection_t *p_conn;
  622. /** The OR connection that is next in this circuit. */
  623. connection_t *n_conn;
  624. /** The identity hash of n_conn. */
  625. char n_conn_id_digest[DIGEST_LEN];
  626. /** Linked list of AP streams associated with this circuit. */
  627. connection_t *p_streams;
  628. /** Linked list of Exit streams associated with this circuit. */
  629. connection_t *n_streams;
  630. /** Linked list of Exit streams associated with this circuit that are
  631. * still being resolved. */
  632. connection_t *resolving_streams;
  633. /** The next stream_id that will be tried when we're attempting to
  634. * construct a new AP stream originating at this circuit. */
  635. uint16_t next_stream_id;
  636. /** How many relay data cells can we package (read from edge streams)
  637. * on this circuit before we receive a circuit-level sendme cell asking
  638. * for more? */
  639. int package_window;
  640. /** How many relay data cells will we deliver (write to edge streams)
  641. * on this circuit? When deliver_window gets low, we send some
  642. * circuit-level sendme cells to indicate that we're willing to accept
  643. * more. */
  644. int deliver_window;
  645. /** The circuit_id used in the previous (backward) hop of this circuit. */
  646. uint16_t p_circ_id;
  647. /** The circuit_id used in the next (forward) hop of this circuit. */
  648. uint16_t n_circ_id;
  649. /** The cipher used by intermediate hops for cells heading toward the
  650. * OP. */
  651. crypto_cipher_env_t *p_crypto;
  652. /** The cipher used by intermediate hops for cells heading away from
  653. * the OP. */
  654. crypto_cipher_env_t *n_crypto;
  655. /** The integrity-checking digest used by intermediate hops, for
  656. * cells packaged here and heading towards the OP.
  657. */
  658. crypto_digest_env_t *p_digest;
  659. /** The integrity-checking digest used by intermediate hops, for
  660. * cells packaged at the OP and arriving here.
  661. */
  662. crypto_digest_env_t *n_digest;
  663. /** Build state for this circuit. It includes the intended path
  664. * length, the chosen exit router, rendezvous information, etc.
  665. */
  666. cpath_build_state_t *build_state;
  667. /** The doubly-linked list of crypt_path_t entries, one per hop,
  668. * for this circuit. This includes ciphers for each hop,
  669. * integrity-checking digests for each hop, and package/delivery
  670. * windows for each hop.
  671. *
  672. * The cpath field is defined only when we are the circuit's origin.
  673. */
  674. crypt_path_t *cpath;
  675. /** For storage while passing to cpuworker, or while n_conn is pending. */
  676. char onionskin[ONIONSKIN_CHALLENGE_LEN];
  677. char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
  678. time_t timestamp_created; /**< When was this circuit created? */
  679. time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
  680. * circuit is clean. */
  681. uint8_t state; /**< Current status of this circuit. */
  682. uint8_t purpose; /**< Why are we creating this circuit? */
  683. /**
  684. * The rend_query field holds y portion of y.onion (nul-terminated)
  685. * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
  686. * for a hidden service, or is S_*.
  687. */
  688. char rend_query[REND_SERVICE_ID_LEN+1];
  689. /** The rend_pk_digest field holds a hash of location-hidden service's
  690. * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
  691. */
  692. char rend_pk_digest[DIGEST_LEN];
  693. /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
  694. * C_ESTABLISH_REND. Filled with zeroes otherwise.
  695. */
  696. char rend_cookie[REND_COOKIE_LEN];
  697. /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  698. * is not marked for close. */
  699. struct circuit_t *rend_splice;
  700. struct circuit_t *next; /**< Next circuit in linked list. */
  701. };
  702. typedef struct circuit_t circuit_t;
  703. /** Configuration options for a Tor process */
  704. typedef struct {
  705. struct config_line_t *LogOptions; /**< List of configuration lines
  706. * for logfiles */
  707. char *DebugLogFile; /**< Where to send verbose log messages. */
  708. char *DataDirectory; /**< OR only: where to store long-term data. */
  709. char *RouterFile; /**< Where to find starting list of ORs. */
  710. char *Nickname; /**< OR only: nickname of this onion router. */
  711. char *Address; /**< OR only: configured address for this onion router. */
  712. char *PidFile; /**< Where to store PID of Tor process. */
  713. char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
  714. * as exits. */
  715. char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
  716. * as entry points. */
  717. char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
  718. * use in circuits. */
  719. char *RendNodes; /**< Comma-separated list of nicknames used as introduction
  720. * points. */
  721. char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
  722. * as introduction points. */
  723. struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
  724. struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
  725. struct config_line_t *SocksBindAddress;
  726. /**< Addresses to bind for listening for SOCKS connections. */
  727. struct config_line_t *ORBindAddress;
  728. /**< Addresses to bind for listening for OR connections. */
  729. struct config_line_t *DirBindAddress;
  730. /**< Addresses to bind for listening for directory connections. */
  731. char *RecommendedVersions; /**< Directory server only: which versions of
  732. * Tor should we tell users to run? */
  733. char *User; /**< Name of user to run Tor as. */
  734. char *Group; /**< Name of group to run Tor as. */
  735. double PathlenCoinWeight; /**< Parameter used to configure average path
  736. * length (alpha in geometric distribution). */
  737. int ORPort; /**< Port to listen on for OR connections. */
  738. int SocksPort; /**< Port to listen on for SOCKS connections. */
  739. int DirPort; /**< Port to listen on for directory connections. */
  740. int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
  741. int ClientOnly; /**< Boolean: should we never evolve into a server role? */
  742. int MaxConn; /**< Maximum number of simultaneous connections. */
  743. int TrafficShaping; /**< Unused. */
  744. int LinkPadding; /**< Unused. */
  745. int IgnoreVersion; /**< If true, run no matter what versions of Tor the
  746. * directory recommends. */
  747. int RunAsDaemon; /**< If true, run in the background. (Unix only) */
  748. int DirFetchPostPeriod; /**< How often do we fetch new directories
  749. * and post server descriptros to the directory
  750. * server? */
  751. int KeepalivePeriod; /**< How often do we send padding cells to keep
  752. * connections alive? */
  753. int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
  754. * to wait simultaneously before we start dropping
  755. * them? */
  756. int NewCircuitPeriod; /**< How long do we use a circuit before building
  757. * a new one? */
  758. int BandwidthRate; /**< How much bandwidth, on average, are we willing to
  759. * use in a second? */
  760. int BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
  761. * use in a second? */
  762. int NumCpus; /**< How many CPUs should we try to use? */
  763. int RunTesting; /**< If true, create testing circuits to measure how well the
  764. * other ORs are running. */
  765. struct config_line_t *RendConfigLines; /**< List of configuration lines
  766. * for rendezvous services. */
  767. char *ContactInfo; /** Contact info to be published in the directory */
  768. } or_options_t;
  769. /* XXX are these good enough defaults? */
  770. #define MAX_SOCKS_REPLY_LEN 1024
  771. #define MAX_SOCKS_ADDR_LEN 256
  772. #define SOCKS_COMMAND_CONNECT 0x01
  773. #define SOCKS_COMMAND_RESOLVE 0xF0
  774. /** State of a SOCKS request from a user to an OP */
  775. struct socks_request_t {
  776. char socks_version; /**< Which version of SOCKS did the client use? */
  777. int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
  778. int replylen; /**< Length of <b>reply</b>. */
  779. char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
  780. * we want to specify our own socks reply,
  781. * rather than using the default socks4 or
  782. * socks5 socks reply. We use this for the
  783. * two-stage socks5 handshake.
  784. */
  785. int has_finished; /**< Has the SOCKS handshake finished? */
  786. char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
  787. uint16_t port; /**< What port did the client ask to connect to? */
  788. };
  789. /* all the function prototypes go here */
  790. /********************************* buffers.c ***************************/
  791. buf_t *buf_new();
  792. buf_t *buf_new_with_capacity(size_t size);
  793. void buf_free(buf_t *buf);
  794. void buf_clear(buf_t *buf);
  795. size_t buf_datalen(const buf_t *buf);
  796. size_t buf_capacity(const buf_t *buf);
  797. const char *_buf_peek_raw_buffer(const buf_t *buf);
  798. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
  799. int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
  800. int flush_buf(int s, buf_t *buf, int *buf_flushlen);
  801. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
  802. int write_to_buf(const char *string, int string_len, buf_t *buf);
  803. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  804. int fetch_from_buf_http(buf_t *buf,
  805. char **headers_out, int max_headerlen,
  806. char **body_out, int *body_used, int max_bodylen);
  807. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  808. void assert_buf_ok(buf_t *buf);
  809. /********************************* circuitbuild.c **********************/
  810. void circuit_log_path(int severity, circuit_t *circ);
  811. void circuit_rep_hist_note_result(circuit_t *circ);
  812. void circuit_dump_by_conn(connection_t *conn, int severity);
  813. circuit_t *circuit_establish_circuit(uint8_t purpose,
  814. const char *exit_digest);
  815. void circuit_n_conn_done(connection_t *or_conn, int success);
  816. int circuit_send_next_onion_skin(circuit_t *circ);
  817. int circuit_extend(cell_t *cell, circuit_t *circ);
  818. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
  819. int circuit_finish_handshake(circuit_t *circ, char *reply);
  820. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  821. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  822. void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
  823. /********************************* circuitlist.c ***********************/
  824. extern char *circuit_state_to_string[];
  825. void circuit_close_all_marked(void);
  826. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
  827. void circuit_free_cpath_node(crypt_path_t *victim);
  828. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
  829. circuit_t *circuit_get_by_conn(connection_t *conn);
  830. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
  831. circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
  832. const char *digest, uint8_t purpose);
  833. circuit_t *circuit_get_rendezvous(const char *cookie);
  834. int circuit_count_building(uint8_t purpose);
  835. circuit_t *circuit_get_youngest_clean_open(uint8_t purpose);
  836. int _circuit_mark_for_close(circuit_t *circ);
  837. #define circuit_mark_for_close(c) \
  838. do { \
  839. if (_circuit_mark_for_close(c)<0) { \
  840. log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
  841. __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
  842. } else { \
  843. c->marked_for_close_file = __FILE__; \
  844. c->marked_for_close = __LINE__; \
  845. } \
  846. } while (0)
  847. void assert_cpath_layer_ok(const crypt_path_t *cp);
  848. void assert_circuit_ok(const circuit_t *c);
  849. /********************************* circuituse.c ************************/
  850. void circuit_expire_building(time_t now);
  851. int circuit_stream_is_being_handled(connection_t *conn);
  852. void circuit_build_needed_circs(time_t now);
  853. void circuit_detach_stream(circuit_t *circ, connection_t *conn);
  854. void circuit_about_to_close_connection(connection_t *conn);
  855. void circuit_has_opened(circuit_t *circ);
  856. void circuit_build_failed(circuit_t *circ);
  857. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname);
  858. circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest);
  859. void circuit_reset_failure_count(void);
  860. int connection_ap_handshake_attach_circuit(connection_t *conn);
  861. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data,int reverse);
  862. int circuit_finish_handshake(circuit_t *circ, char *reply);
  863. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  864. void assert_cpath_layer_ok(const crypt_path_t *c);
  865. void assert_circuit_ok(const circuit_t *c);
  866. /********************************* command.c ***************************/
  867. void command_process_cell(cell_t *cell, connection_t *conn);
  868. extern unsigned long stats_n_padding_cells_processed;
  869. extern unsigned long stats_n_create_cells_processed;
  870. extern unsigned long stats_n_created_cells_processed;
  871. extern unsigned long stats_n_relay_cells_processed;
  872. extern unsigned long stats_n_destroy_cells_processed;
  873. /********************************* config.c ***************************/
  874. struct config_line_t {
  875. char *key;
  876. char *value;
  877. struct config_line_t *next;
  878. };
  879. int config_assign_default_dirservers(void);
  880. int getconfig(int argc, char **argv, or_options_t *options);
  881. int config_init_logs(or_options_t *options);
  882. void config_parse_exit_policy(struct config_line_t *cfg,
  883. struct exit_policy_t **dest);
  884. void exit_policy_free(struct exit_policy_t *p);
  885. const char *get_data_directory(or_options_t *options);
  886. /********************************* connection.c ***************************/
  887. #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
  888. "Unknown" : conn_type_to_string[(t)])
  889. extern char *conn_type_to_string[];
  890. extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
  891. connection_t *connection_new(int type);
  892. void connection_free(connection_t *conn);
  893. void connection_free_all(void);
  894. void connection_about_to_close_connection(connection_t *conn);
  895. void connection_close_immediate(connection_t *conn);
  896. int _connection_mark_for_close(connection_t *conn);
  897. #define connection_mark_for_close(c) \
  898. do { \
  899. if (_connection_mark_for_close(c)<0) { \
  900. log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
  901. __FILE__,__LINE__,c->marked_for_close_file,c->marked_for_close); \
  902. } else { \
  903. c->marked_for_close_file = __FILE__; \
  904. c->marked_for_close = __LINE__; \
  905. } \
  906. } while (0)
  907. void connection_expire_held_open(void);
  908. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  909. int retry_all_connections(void);
  910. void connection_bucket_init(void);
  911. void connection_bucket_refill(struct timeval *now);
  912. int connection_handle_read(connection_t *conn);
  913. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  914. int connection_wants_to_flush(connection_t *conn);
  915. int connection_outbuf_too_full(connection_t *conn);
  916. int connection_handle_write(connection_t *conn);
  917. void connection_write_to_buf(const char *string, int len, connection_t *conn);
  918. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
  919. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  920. connection_t *connection_get_by_identity_digest(const char *digest, int type);
  921. connection_t *connection_get_by_type(int type);
  922. connection_t *connection_get_by_type_state(int type, int state);
  923. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  924. connection_t *connection_get_by_type_rendquery(int type, const char *rendquery);
  925. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  926. #define connection_has_pending_tls_data(conn) \
  927. ((conn)->type == CONN_TYPE_OR && \
  928. (conn)->state == OR_CONN_STATE_OPEN && \
  929. tor_tls_get_pending_bytes((conn)->tls))
  930. int connection_is_listener(connection_t *conn);
  931. int connection_state_is_open(connection_t *conn);
  932. int connection_state_is_connecting(connection_t *conn);
  933. int connection_send_destroy(uint16_t circ_id, connection_t *conn);
  934. void assert_connection_ok(connection_t *conn, time_t now);
  935. /********************************* connection_edge.c ***************************/
  936. int connection_edge_process_inbuf(connection_t *conn);
  937. int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
  938. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  939. int connection_edge_finished_flushing(connection_t *conn);
  940. int connection_edge_finished_connecting(connection_t *conn);
  941. int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  942. int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
  943. int connection_ap_make_bridge(char *address, uint16_t port);
  944. void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  945. int replylen, char success);
  946. void connection_ap_handshake_socks_resolved(connection_t *conn,
  947. int answer_type,
  948. int answer_len,
  949. const char *answer);
  950. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  951. int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
  952. void connection_exit_connect(connection_t *conn);
  953. int connection_edge_is_rendezvous_stream(connection_t *conn);
  954. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  955. void connection_ap_expire_beginning(void);
  956. void connection_ap_attach_pending(void);
  957. int socks_policy_permits_address(uint32_t addr);
  958. void client_dns_init(void);
  959. uint32_t client_dns_lookup_entry(const char *address);
  960. int client_dns_incr_failures(const char *address);
  961. void client_dns_set_entry(const char *address, uint32_t val);
  962. void client_dns_clean(void);
  963. /********************************* connection_or.c ***************************/
  964. int connection_or_process_inbuf(connection_t *conn);
  965. int connection_or_finished_flushing(connection_t *conn);
  966. int connection_or_finished_connecting(connection_t *conn);
  967. connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  968. const char *id_digest);
  969. int connection_tls_start_handshake(connection_t *conn, int receiving);
  970. int connection_tls_continue_handshake(connection_t *conn);
  971. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
  972. /********************************* cpuworker.c *****************************/
  973. void cpu_init(void);
  974. void cpuworkers_rotate(void);
  975. int connection_cpu_finished_flushing(connection_t *conn);
  976. int connection_cpu_process_inbuf(connection_t *conn);
  977. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  978. void *task);
  979. /********************************* directory.c ***************************/
  980. void directory_post_to_dirservers(uint8_t purpose, const char *payload,
  981. int payload_len);
  982. void directory_get_from_dirserver(uint8_t purpose, const char *payload,
  983. int payload_len);
  984. int connection_dir_process_inbuf(connection_t *conn);
  985. int connection_dir_finished_flushing(connection_t *conn);
  986. int connection_dir_finished_connecting(connection_t *conn);
  987. /********************************* dirserv.c ***************************/
  988. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  989. int dirserv_parse_fingerprint_file(const char *fname);
  990. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  991. void dirserv_free_fingerprint_list();
  992. int dirserv_add_descriptor(const char **desc);
  993. int dirserv_load_from_directory_string(const char *dir);
  994. void dirserv_free_descriptors();
  995. void dirserv_remove_old_servers(void);
  996. int dirserv_dump_directory_to_string(char *s, unsigned int maxlen,
  997. crypto_pk_env_t *private_key);
  998. void directory_set_dirty(void);
  999. size_t dirserv_get_directory(const char **cp);
  1000. size_t dirserv_get_runningrouters(const char **rr);
  1001. void dirserv_set_cached_directory(const char *directory, time_t when);
  1002. /********************************* dns.c ***************************/
  1003. void dns_init(void);
  1004. int connection_dns_finished_flushing(connection_t *conn);
  1005. int connection_dns_process_inbuf(connection_t *conn);
  1006. void dnsworkers_rotate(void);
  1007. void connection_dns_remove(connection_t *conn);
  1008. void assert_connection_edge_not_dns_pending(connection_t *conn);
  1009. void assert_all_pending_dns_resolves_ok(void);
  1010. void dns_cancel_pending_resolve(char *question);
  1011. int dns_resolve(connection_t *exitconn);
  1012. /********************************* main.c ***************************/
  1013. int connection_add(connection_t *conn);
  1014. int connection_remove(connection_t *conn);
  1015. int connection_in_array(connection_t *conn);
  1016. void get_connection_array(connection_t ***array, int *n);
  1017. void connection_watch_events(connection_t *conn, short events);
  1018. int connection_is_reading(connection_t *conn);
  1019. void connection_stop_reading(connection_t *conn);
  1020. void connection_start_reading(connection_t *conn);
  1021. int connection_is_writing(connection_t *conn);
  1022. void connection_stop_writing(connection_t *conn);
  1023. void connection_start_writing(connection_t *conn);
  1024. void directory_has_arrived(void);
  1025. int clique_mode(void);
  1026. int server_mode(void);
  1027. int exit_server_mode(void);
  1028. int proxy_mode(void);
  1029. int main(int argc, char *argv[]);
  1030. /********************************* onion.c ***************************/
  1031. int onion_pending_add(circuit_t *circ);
  1032. circuit_t *onion_next_task(void);
  1033. void onion_pending_remove(circuit_t *circ);
  1034. int onion_skin_create(crypto_pk_env_t *router_key,
  1035. crypto_dh_env_t **handshake_state_out,
  1036. char *onion_skin_out);
  1037. int onion_skin_server_handshake(char *onion_skin,
  1038. crypto_pk_env_t *private_key,
  1039. crypto_pk_env_t *prev_private_key,
  1040. char *handshake_reply_out,
  1041. char *key_out,
  1042. int key_out_len);
  1043. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  1044. char *handshake_reply,
  1045. char *key_out,
  1046. int key_out_len);
  1047. /********************************* relay.c ***************************/
  1048. extern unsigned long stats_n_relay_cells_relayed;
  1049. extern unsigned long stats_n_relay_cells_delivered;
  1050. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  1051. int cell_direction);
  1052. void relay_header_pack(char *dest, const relay_header_t *src);
  1053. void relay_header_unpack(relay_header_t *dest, const char *src);
  1054. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  1055. int relay_command, const char *payload,
  1056. int payload_len, crypt_path_t *cpath_layer);
  1057. int connection_edge_package_raw_inbuf(connection_t *conn);
  1058. void connection_edge_consider_sending_sendme(connection_t *conn);
  1059. extern uint64_t stats_n_data_cells_packaged;
  1060. extern uint64_t stats_n_data_bytes_packaged;
  1061. extern uint64_t stats_n_data_cells_received;
  1062. extern uint64_t stats_n_data_bytes_received;
  1063. /********************************* rephist.c ***************************/
  1064. void rep_hist_init(void);
  1065. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  1066. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  1067. void rep_hist_note_disconnect(const char* nickname, time_t when);
  1068. void rep_hist_note_connection_died(const char* nickname, time_t when);
  1069. void rep_hist_note_extend_succeeded(const char *from_name,
  1070. const char *to_name);
  1071. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  1072. void rep_hist_dump_stats(time_t now, int severity);
  1073. /********************************* rendclient.c ***************************/
  1074. void rend_client_introcirc_has_opened(circuit_t *circ);
  1075. void rend_client_rendcirc_has_opened(circuit_t *circ);
  1076. int rend_client_introduction_acked(circuit_t *circ, const char *request, int request_len);
  1077. void rend_client_refetch_renddesc(const char *query);
  1078. int rend_client_remove_intro_point(char *failed_intro, const char *query);
  1079. int rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len);
  1080. int rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len);
  1081. void rend_client_desc_fetched(char *query, int success);
  1082. char *rend_client_get_random_intro(char *query);
  1083. int rend_parse_rendezvous_address(char *address);
  1084. int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
  1085. /********************************* rendcommon.c ***************************/
  1086. typedef struct rend_service_descriptor_t {
  1087. crypto_pk_env_t *pk;
  1088. time_t timestamp;
  1089. int n_intro_points;
  1090. char **intro_points;
  1091. } rend_service_descriptor_t;
  1092. int rend_cmp_service_ids(const char *one, const char *two);
  1093. void rend_process_relay_cell(circuit_t *circ, int command, int length,
  1094. const char *payload);
  1095. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  1096. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  1097. crypto_pk_env_t *key,
  1098. char **str_out,
  1099. int *len_out);
  1100. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, int len);
  1101. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  1102. typedef struct rend_cache_entry_t {
  1103. int len; /* Length of desc */
  1104. time_t received; /* When did we get the descriptor? */
  1105. char *desc; /* Service descriptor */
  1106. rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
  1107. } rend_cache_entry_t;
  1108. void rend_cache_init(void);
  1109. void rend_cache_clean(void);
  1110. int rend_valid_service_id(const char *query);
  1111. int rend_cache_lookup_desc(const char *query, const char **desc, int *desc_len);
  1112. int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
  1113. int rend_cache_store(const char *desc, int desc_len);
  1114. /********************************* rendservice.c ***************************/
  1115. int rend_config_services(or_options_t *options);
  1116. int rend_service_load_keys(void);
  1117. void rend_services_init(void);
  1118. void rend_services_introduce(void);
  1119. void rend_services_upload(int force);
  1120. void rend_service_intro_has_opened(circuit_t *circuit);
  1121. int rend_service_intro_established(circuit_t *circuit, const char *request, int request_len);
  1122. void rend_service_rendezvous_has_opened(circuit_t *circuit);
  1123. int rend_service_introduce(circuit_t *circuit, const char *request, int request_len);
  1124. void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
  1125. int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
  1126. void rend_service_dump_stats(int severity);
  1127. /********************************* rendmid.c *******************************/
  1128. int rend_mid_establish_intro(circuit_t *circ, const char *request, int request_len);
  1129. int rend_mid_introduce(circuit_t *circ, const char *request, int request_len);
  1130. int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, int request_len);
  1131. int rend_mid_rendezvous(circuit_t *circ, const char *request, int request_len);
  1132. /********************************* router.c ***************************/
  1133. void set_onion_key(crypto_pk_env_t *k);
  1134. crypto_pk_env_t *get_onion_key(void);
  1135. crypto_pk_env_t *get_previous_onion_key(void);
  1136. time_t get_onion_key_set_at(void);
  1137. void set_identity_key(crypto_pk_env_t *k);
  1138. crypto_pk_env_t *get_identity_key(void);
  1139. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  1140. int init_keys(void);
  1141. crypto_pk_env_t *init_key_from_file(const char *fname);
  1142. void rotate_onion_key(void);
  1143. void router_retry_connections(void);
  1144. void router_upload_dir_desc_to_dirservers(void);
  1145. int router_compare_to_my_exit_policy(connection_t *conn);
  1146. routerinfo_t *router_get_my_routerinfo(void);
  1147. const char *router_get_my_descriptor(void);
  1148. int router_is_me(routerinfo_t *router);
  1149. int router_rebuild_descriptor(void);
  1150. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  1151. crypto_pk_env_t *ident_key);
  1152. /********************************* routerlist.c ***************************/
  1153. routerinfo_t *router_pick_directory_server(void);
  1154. int all_directory_servers_down(void);
  1155. struct smartlist_t;
  1156. void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list);
  1157. void router_add_running_routers_to_smartlist(struct smartlist_t *sl);
  1158. int router_nickname_matches(routerinfo_t *router, const char *nickname);
  1159. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  1160. struct smartlist_t *excludedsmartlist);
  1161. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  1162. routerinfo_t *router_get_by_nickname(const char *nickname);
  1163. routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
  1164. routerinfo_t *router_get_by_digest(const char *digest);
  1165. void router_get_routerlist(routerlist_t **prouterlist);
  1166. void routerlist_free(routerlist_t *routerlist);
  1167. void routerlist_clear_trusted_directories(void);
  1168. void routerinfo_free(routerinfo_t *router);
  1169. routerinfo_t *routerinfo_copy(const routerinfo_t *router);
  1170. void router_mark_as_down(const char *digest);
  1171. void routerlist_remove_old_routers(void);
  1172. int router_load_routerlist_from_file(char *routerfile, int trusted);
  1173. int router_load_routerlist_from_string(const char *s, int trusted);
  1174. int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey);
  1175. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  1176. struct exit_policy_t *policy);
  1177. #define ADDR_POLICY_ACCEPTED 0
  1178. #define ADDR_POLICY_REJECTED -1
  1179. #define ADDR_POLICY_UNKNOWN 1
  1180. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  1181. int router_exit_policy_rejects_all(routerinfo_t *router);
  1182. void running_routers_free(running_routers_t *rr);
  1183. void routerlist_update_from_runningrouters(routerlist_t *list,
  1184. running_routers_t *rr);
  1185. void router_update_status_from_smartlist(routerinfo_t *r,
  1186. time_t list_time,
  1187. smartlist_t *running_list);
  1188. /********************************* routerparse.c ************************/
  1189. typedef struct tor_version_t {
  1190. int major;
  1191. int minor;
  1192. int micro;
  1193. enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2 } status;
  1194. int patchlevel;
  1195. enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
  1196. } tor_version_t;
  1197. int router_get_router_hash(const char *s, char *digest);
  1198. int router_get_dir_hash(const char *s, char *digest);
  1199. int router_get_runningrouters_hash(const char *s, char *digest);
  1200. int router_parse_list_from_string(const char **s,
  1201. routerlist_t **dest,
  1202. smartlist_t *good_nickname_list);
  1203. int router_parse_routerlist_from_directory(const char *s,
  1204. routerlist_t **dest,
  1205. crypto_pk_env_t *pkey);
  1206. running_routers_t *router_parse_runningrouters(const char *str);
  1207. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
  1208. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  1209. struct exit_policy_t *router_parse_exit_policy_from_string(const char *s);
  1210. int check_software_version_against_directory(const char *directory,
  1211. int ignoreversion);
  1212. int tor_version_parse(const char *s, tor_version_t *out);
  1213. int tor_version_compare(tor_version_t *a, tor_version_t *b);
  1214. int tor_version_compare_to_mine(const char *s);
  1215. #endif
  1216. /*
  1217. Local Variables:
  1218. mode:c
  1219. indent-tabs-mode:nil
  1220. c-basic-offset:2
  1221. End:
  1222. */