or.h 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file or.h
  8. *
  9. * \brief Master header file for Tor-specific functionality.
  10. */
  11. #ifndef __OR_H
  12. #define __OR_H
  13. #define OR_H_ID "$Id$"
  14. #include "orconfig.h"
  15. #ifdef MS_WINDOWS
  16. #define WIN32_WINNT 0x400
  17. #define _WIN32_WINNT 0x400
  18. #define WIN32_LEAN_AND_MEAN
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_SIGNAL_H
  30. #include <signal.h>
  31. #endif
  32. #ifdef HAVE_NETDB_H
  33. #include <netdb.h>
  34. #endif
  35. #ifdef HAVE_CTYPE_H
  36. #include <ctype.h>
  37. #endif
  38. #include "../common/torint.h"
  39. #ifdef HAVE_INTTYPES_H
  40. #include <inttypes.h>
  41. #endif
  42. #ifdef HAVE_SYS_PARAM_H
  43. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  44. #endif
  45. #ifdef HAVE_SYS_LIMITS_H
  46. #include <sys/limits.h>
  47. #endif
  48. #ifdef HAVE_MACHINE_LIMITS_H
  49. #ifndef __FreeBSD__
  50. /* FreeBSD has a bug where it complains that this file is obsolete,
  51. and I should migrate to using sys/limits. It complains even when
  52. I include both. */
  53. #include <machine/limits.h>
  54. #endif
  55. #endif
  56. #ifdef HAVE_SYS_TYPES_H
  57. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  58. #endif
  59. #ifdef HAVE_SYS_WAIT_H
  60. #include <sys/wait.h>
  61. #endif
  62. #ifdef HAVE_SYS_FCNTL_H
  63. #include <sys/fcntl.h>
  64. #endif
  65. #ifdef HAVE_FCNTL_H
  66. #include <fcntl.h>
  67. #endif
  68. #ifdef HAVE_SYS_IOCTL_H
  69. #include <sys/ioctl.h>
  70. #endif
  71. #ifdef HAVE_SYS_SOCKET_H
  72. #include <sys/socket.h>
  73. #endif
  74. #ifdef HAVE_SYS_TIME_H
  75. #include <sys/time.h>
  76. #endif
  77. #ifdef HAVE_SYS_STAT_H
  78. #include <sys/stat.h>
  79. #endif
  80. #ifdef HAVE_NETINET_IN_H
  81. #include <netinet/in.h>
  82. #endif
  83. #ifdef HAVE_ARPA_INET_H
  84. #include <arpa/inet.h>
  85. #endif
  86. #ifdef HAVE_ERRNO_H
  87. #include <errno.h>
  88. #endif
  89. #ifdef HAVE_ASSERT_H
  90. #include <assert.h>
  91. #endif
  92. #ifdef HAVE_TIME_H
  93. #include <time.h>
  94. #endif
  95. /** Upper bound on maximum simultaneous connections; can be lowered by
  96. * config file. */
  97. #define MAXCONNECTIONS 15000
  98. #ifdef MS_WINDOWS
  99. /* No, we don't need to redefine FD_SETSIZE before including winsock:
  100. * we use libevent now, and libevent handles the select() stuff. Yes,
  101. * some documents imply that we need to redefine anyway if we're using
  102. * select() anywhere in our application or in anything it links to: these
  103. * documents are either the holy texts of a cargo cult of network
  104. * programmers, or more likely a simplification of what's going on for
  105. * people who haven't read winsock[2].c for themselves.
  106. */
  107. #if (_MSC_VER <= 1300)
  108. #include <winsock.h>
  109. #else
  110. #include <winsock2.h>
  111. #include <ws2tcpip.h>
  112. #endif
  113. #endif
  114. #ifdef MS_WINDOWS
  115. #include <io.h>
  116. #include <process.h>
  117. #include <direct.h>
  118. #include <windows.h>
  119. #define snprintf _snprintf
  120. #endif
  121. #ifdef HAVE_EVENT_H
  122. #include <event.h>
  123. #else
  124. #error "Tor requires libevent to build."
  125. #endif
  126. #include "../common/crypto.h"
  127. #include "../common/tortls.h"
  128. #include "../common/log.h"
  129. #include "../common/compat.h"
  130. #include "../common/container.h"
  131. #include "../common/util.h"
  132. #include "../common/torgzip.h"
  133. #if (SIZEOF_CELL_T != 0)
  134. /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
  135. * that our stuff always calls cell_t something different. */
  136. #define cell_t tor_cell_t
  137. #endif
  138. /** Define this if you want Tor to crash when any problem comes up,
  139. * so you can get a coredump and track things down. */
  140. #undef TOR_FRAGILE
  141. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  142. #define MAX_NICKNAME_LEN 19
  143. /* Hex digest plus dollar sign. */
  144. #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
  145. #define MAX_DIR_SIZE 500000
  146. /* For http parsing */
  147. #define MAX_HEADERS_SIZE 50000
  148. #define MAX_BODY_SIZE 500000
  149. #ifdef TOR_PERF
  150. /** How long do we keep DNS cache entries before purging them? */
  151. #define MAX_DNS_ENTRY_AGE (150*60)
  152. #else
  153. #define MAX_DNS_ENTRY_AGE (15*60)
  154. #endif
  155. /** How often do we rotate onion keys? */
  156. #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
  157. /** How often do we rotate TLS contexts? */
  158. #define MAX_SSL_KEY_LIFETIME (120*60)
  159. /** How old do we allow a router to get before removing it, either
  160. * from the descriptor list (for dirservers) or the router list (for others)?
  161. * In seconds. */
  162. #define ROUTER_MAX_AGE (60*60*24)
  163. typedef enum {
  164. CIRC_ID_TYPE_LOWER=0,
  165. CIRC_ID_TYPE_HIGHER=1
  166. } circ_id_type_t;
  167. #define _CONN_TYPE_MIN 3
  168. /** Type for sockets listening for OR connections. */
  169. #define CONN_TYPE_OR_LISTENER 3
  170. /** Type for OR-to-OR or OP-to-OR connections. */
  171. #define CONN_TYPE_OR 4
  172. /** Type for connections from final OR to chosen destination. */
  173. #define CONN_TYPE_EXIT 5
  174. /** Type for sockets listening for SOCKS connections. */
  175. #define CONN_TYPE_AP_LISTENER 6
  176. /** Type for SOCKS connections to OP. */
  177. #define CONN_TYPE_AP 7
  178. /** Type for sockets listening for HTTP connections to the directory server. */
  179. #define CONN_TYPE_DIR_LISTENER 8
  180. /** Type for HTTP connections to the directory server. */
  181. #define CONN_TYPE_DIR 9
  182. /** Type for connections to local dnsworker processes. */
  183. #define CONN_TYPE_DNSWORKER 10
  184. /** Type for connections to local cpuworker processes. */
  185. #define CONN_TYPE_CPUWORKER 11
  186. /** Type for listenting for connections from user interface process */
  187. #define CONN_TYPE_CONTROL_LISTENER 12
  188. /** Type for connections from user interface process */
  189. #define CONN_TYPE_CONTROL 13
  190. #define _CONN_TYPE_MAX 13
  191. #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  192. /** State for any listener connection. */
  193. #define LISTENER_STATE_READY 0
  194. #define _DNSWORKER_STATE_MIN 1
  195. /** State for a connection to a dnsworker process that's idle. */
  196. #define DNSWORKER_STATE_IDLE 1
  197. /** State for a connection to a dnsworker process that's resolving a hostname. */
  198. #define DNSWORKER_STATE_BUSY 2
  199. #define _DNSWORKER_STATE_MAX 2
  200. #define _CPUWORKER_STATE_MIN 1
  201. /** State for a connection to a cpuworker process that's idle. */
  202. #define CPUWORKER_STATE_IDLE 1
  203. /** State for a connection to a cpuworker process that's processing a
  204. * handshake. */
  205. #define CPUWORKER_STATE_BUSY_ONION 2
  206. #define _CPUWORKER_STATE_MAX 2
  207. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  208. #define _OR_CONN_STATE_MIN 1
  209. /** State for a connection to an OR: waiting for connect() to finish. */
  210. #define OR_CONN_STATE_CONNECTING 1
  211. /** State for a connection to an OR: waiting for proxy command to flush. */
  212. #define OR_CONN_STATE_PROXY_FLUSHING 2
  213. /** State for a connection to an OR: waiting for proxy response. */
  214. #define OR_CONN_STATE_PROXY_READING 3
  215. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  216. #define OR_CONN_STATE_HANDSHAKING 4
  217. /** State for a connection to an OR: Ready to send/receive cells. */
  218. #define OR_CONN_STATE_OPEN 5
  219. #define _OR_CONN_STATE_MAX 5
  220. #define _EXIT_CONN_STATE_MIN 1
  221. /** State for an exit connection: waiting for response from dns farm. */
  222. #define EXIT_CONN_STATE_RESOLVING 1
  223. /** State for an exit connection: waiting for connect() to finish. */
  224. #define EXIT_CONN_STATE_CONNECTING 2
  225. /** State for an exit connection: open and ready to transmit data. */
  226. #define EXIT_CONN_STATE_OPEN 3
  227. /** State for an exit connection: waiting to be removed. */
  228. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  229. #define _EXIT_CONN_STATE_MAX 4
  230. #if 0
  231. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  232. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  233. #endif
  234. /* the AP state values must be disjoint from the EXIT state values */
  235. #define _AP_CONN_STATE_MIN 5
  236. /** State for a SOCKS connection: waiting for SOCKS request. */
  237. #define AP_CONN_STATE_SOCKS_WAIT 5
  238. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  239. * rendezvous rescriptor. */
  240. #define AP_CONN_STATE_RENDDESC_WAIT 6
  241. /** The controller will attach this connection to a circuit; it isn't our
  242. * job to do so. */
  243. #define AP_CONN_STATE_CONTROLLER_WAIT 7
  244. /** State for a SOCKS connection: waiting for a completed circuit. */
  245. #define AP_CONN_STATE_CIRCUIT_WAIT 8
  246. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  247. #define AP_CONN_STATE_CONNECT_WAIT 9
  248. /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
  249. #define AP_CONN_STATE_RESOLVE_WAIT 10
  250. /** State for a SOCKS connection: ready to send and receive. */
  251. #define AP_CONN_STATE_OPEN 11
  252. #define _AP_CONN_STATE_MAX 11
  253. #define _DIR_CONN_STATE_MIN 1
  254. /** State for connection to directory server: waiting for connect(). */
  255. #define DIR_CONN_STATE_CONNECTING 1
  256. /** State for connection to directory server: sending HTTP request. */
  257. #define DIR_CONN_STATE_CLIENT_SENDING 2
  258. /** State for connection to directory server: reading HTTP response. */
  259. #define DIR_CONN_STATE_CLIENT_READING 3
  260. /** State for connection at directory server: waiting for HTTP request. */
  261. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
  262. /** State for connection at directory server: sending HTTP response. */
  263. #define DIR_CONN_STATE_SERVER_WRITING 5
  264. #define _DIR_CONN_STATE_MAX 5
  265. #define _CONTROL_CONN_STATE_MIN 1
  266. #define CONTROL_CONN_STATE_OPEN 1
  267. #define CONTROL_CONN_STATE_NEEDAUTH 2
  268. #define _CONTROL_CONN_STATE_MAX 2
  269. #define _DIR_PURPOSE_MIN 1
  270. /** Purpose for connection to directory server: download a directory. */
  271. #define DIR_PURPOSE_FETCH_DIR 1
  272. /** Purpose for connection to directory server: download just the list
  273. * of running routers. */
  274. #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
  275. /** Purpose for connection to directory server: download a rendezvous
  276. * descriptor. */
  277. #define DIR_PURPOSE_FETCH_RENDDESC 3
  278. /** Purpose for connection to directory server: set after a rendezvous
  279. * descriptor is downloaded. */
  280. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
  281. /** Purpose for connection to directory server: upload a server descriptor. */
  282. #define DIR_PURPOSE_UPLOAD_DIR 5
  283. /** Purpose for connection to directory server: upload a rendezvous
  284. * descriptor. */
  285. #define DIR_PURPOSE_UPLOAD_RENDDESC 6
  286. /** Purpose for connection at a directory server. */
  287. #define DIR_PURPOSE_SERVER 7
  288. #define _DIR_PURPOSE_MAX 7
  289. #define _EXIT_PURPOSE_MIN 1
  290. #define EXIT_PURPOSE_CONNECT 1
  291. #define EXIT_PURPOSE_RESOLVE 2
  292. #define _EXIT_PURPOSE_MAX 2
  293. /** Circuit state: I'm the OP, still haven't done all my handshakes. */
  294. #define CIRCUIT_STATE_BUILDING 0
  295. /** Circuit state: Waiting to process the onionskin. */
  296. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  297. /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
  298. #define CIRCUIT_STATE_OR_WAIT 2
  299. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  300. #define CIRCUIT_STATE_OPEN 3
  301. #define _CIRCUIT_PURPOSE_MIN 1
  302. /* these circuits were initiated elsewhere */
  303. #define _CIRCUIT_PURPOSE_OR_MIN 1
  304. /** OR-side circuit purpose: normal circuit, at OR. */
  305. #define CIRCUIT_PURPOSE_OR 1
  306. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  307. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  308. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  309. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  310. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  311. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  312. #define _CIRCUIT_PURPOSE_OR_MAX 4
  313. /* these circuits originate at this node */
  314. /* here's how circ client-side purposes work:
  315. * normal circuits are C_GENERAL.
  316. * circuits that are c_introducing are either on their way to
  317. * becoming open, or they are open and waiting for a
  318. * suitable rendcirc before they send the intro.
  319. * circuits that are c_introduce_ack_wait have sent the intro,
  320. * but haven't gotten a response yet.
  321. * circuits that are c_establish_rend are either on their way
  322. * to becoming open, or they are open and have sent the
  323. * establish_rendezvous cell but haven't received an ack.
  324. * circuits that are c_rend_ready are open and have received a
  325. * rend ack, but haven't heard from bob yet. if they have a
  326. * buildstate->pending_final_cpath then they're expecting a
  327. * cell from bob, else they're not.
  328. * circuits that are c_rend_ready_intro_acked are open, and
  329. * some intro circ has sent its intro and received an ack.
  330. * circuits that are c_rend_joined are open, have heard from
  331. * bob, and are talking to him.
  332. */
  333. /** Client-side circuit purpose: Normal circuit, with cpath. */
  334. #define CIRCUIT_PURPOSE_C_GENERAL 5
  335. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  336. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  337. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
  338. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  339. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  340. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  341. /** Client-side circuit purpose: at Alice, waiting for ack. */
  342. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  343. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  344. #define CIRCUIT_PURPOSE_C_REND_READY 10
  345. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  346. * has been acknowledged. */
  347. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  348. /** Client-side circuit purpose: at Alice, rendezvous established. */
  349. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  350. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  351. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  352. /** Hidden-service-side circuit purpose: at Bob, successfully established
  353. * intro. */
  354. #define CIRCUIT_PURPOSE_S_INTRO 14
  355. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  356. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  357. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  358. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  359. /** A testing circuit; not meant to be used for actual traffic. */
  360. #define CIRCUIT_PURPOSE_TESTING 17
  361. #define _CIRCUIT_PURPOSE_MAX 17
  362. /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
  363. * that this OP has originated. */
  364. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  365. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  366. #define RELAY_COMMAND_BEGIN 1
  367. #define RELAY_COMMAND_DATA 2
  368. #define RELAY_COMMAND_END 3
  369. #define RELAY_COMMAND_CONNECTED 4
  370. #define RELAY_COMMAND_SENDME 5
  371. #define RELAY_COMMAND_EXTEND 6
  372. #define RELAY_COMMAND_EXTENDED 7
  373. #define RELAY_COMMAND_TRUNCATE 8
  374. #define RELAY_COMMAND_TRUNCATED 9
  375. #define RELAY_COMMAND_DROP 10
  376. #define RELAY_COMMAND_RESOLVE 11
  377. #define RELAY_COMMAND_RESOLVED 12
  378. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  379. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  380. #define RELAY_COMMAND_INTRODUCE1 34
  381. #define RELAY_COMMAND_INTRODUCE2 35
  382. #define RELAY_COMMAND_RENDEZVOUS1 36
  383. #define RELAY_COMMAND_RENDEZVOUS2 37
  384. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  385. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  386. #define RELAY_COMMAND_INTRODUCE_ACK 40
  387. #define END_STREAM_REASON_MISC 1
  388. #define END_STREAM_REASON_RESOLVEFAILED 2
  389. #define END_STREAM_REASON_CONNECTREFUSED 3
  390. #define END_STREAM_REASON_EXITPOLICY 4
  391. #define END_STREAM_REASON_DESTROY 5
  392. #define END_STREAM_REASON_DONE 6
  393. #define END_STREAM_REASON_TIMEOUT 7
  394. /* 8 is unallocated. */
  395. #define END_STREAM_REASON_HIBERNATING 9
  396. #define END_STREAM_REASON_INTERNAL 10
  397. #define END_STREAM_REASON_RESOURCELIMIT 11
  398. #define END_STREAM_REASON_CONNRESET 12
  399. #define END_STREAM_REASON_TORPROTOCOL 13
  400. /* These high-numbered end reasons are not part of the official spec,
  401. * and are not intended to be put in relay end cells. They are here
  402. * to be more informative when sending back socks replies to the
  403. * application. */
  404. #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
  405. #define END_STREAM_REASON_CANT_ATTACH 257
  406. #define END_STREAM_REASON_NET_UNREACHABLE 258
  407. #define RESOLVED_TYPE_HOSTNAME 0
  408. #define RESOLVED_TYPE_IPV4 4
  409. #define RESOLVED_TYPE_IPV6 6
  410. #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
  411. #define RESOLVED_TYPE_ERROR 0xF1
  412. /** Length of 'y' portion of 'y.onion' URL. */
  413. #define REND_SERVICE_ID_LEN 16
  414. #define CELL_DIRECTION_IN 1
  415. #define CELL_DIRECTION_OUT 2
  416. #ifdef TOR_PERF
  417. #define CIRCWINDOW_START 10000
  418. #define CIRCWINDOW_INCREMENT 1000
  419. #define STREAMWINDOW_START 5000
  420. #define STREAMWINDOW_INCREMENT 500
  421. #else
  422. #define CIRCWINDOW_START 1000
  423. #define CIRCWINDOW_INCREMENT 100
  424. #define STREAMWINDOW_START 500
  425. #define STREAMWINDOW_INCREMENT 50
  426. #endif
  427. /* cell commands */
  428. #define CELL_PADDING 0
  429. #define CELL_CREATE 1
  430. #define CELL_CREATED 2
  431. #define CELL_RELAY 3
  432. #define CELL_DESTROY 4
  433. /* people behind fascist firewalls use only these ports */
  434. #define REQUIRED_FIREWALL_DIRPORT 80
  435. #define REQUIRED_FIREWALL_ORPORT 443
  436. /* legal characters in a nickname */
  437. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  438. /** Name to use in client TLS certificates if no nickname is given.*/
  439. #define DEFAULT_CLIENT_NICKNAME "client"
  440. #define SOCKS4_NETWORK_LEN 8
  441. typedef enum {
  442. SOCKS5_SUCCEEDED = 0x00,
  443. SOCKS5_GENERAL_ERROR = 0x01,
  444. SOCKS5_NOT_ALLOWED = 0x02,
  445. SOCKS5_NET_UNREACHABLE = 0x03,
  446. SOCKS5_HOST_UNREACHABLE = 0x04,
  447. SOCKS5_CONNECTION_REFUSED = 0x05,
  448. SOCKS5_TTL_EXPIRED = 0x06,
  449. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  450. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  451. } socks5_reply_status_t;
  452. /*
  453. * Relay payload:
  454. * Relay command [1 byte]
  455. * Recognized [2 bytes]
  456. * Stream ID [2 bytes]
  457. * Partial SHA-1 [4 bytes]
  458. * Length [2 bytes]
  459. * Relay payload [498 bytes]
  460. */
  461. #define CELL_PAYLOAD_SIZE 509
  462. #define CELL_NETWORK_SIZE 512
  463. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  464. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  465. /** Parsed onion routing cell. All communication from OP-to-OR, or from
  466. * OR-to-OR, is via cells. */
  467. typedef struct {
  468. uint16_t circ_id; /**< Circuit which received the cell. */
  469. unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  470. * or DESTROY. */
  471. unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  472. } cell_t;
  473. /** Beginning of a RELAY cell payload. */
  474. typedef struct {
  475. uint8_t command; /**< The end-to-end relay command. */
  476. uint16_t recognized; /**< Used to tell whether cell is for us. */
  477. uint16_t stream_id; /**< Which stream is this cell associated with? */
  478. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  479. uint16_t length; /**< How long is the payload body? */
  480. } relay_header_t;
  481. typedef struct buf_t buf_t;
  482. typedef struct socks_request_t socks_request_t;
  483. #define CONNECTION_MAGIC 0x7C3C304Eu
  484. /** Description of a connection to another host or process, and associated
  485. * data. */
  486. struct connection_t {
  487. uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
  488. uint8_t type; /**< What kind of connection is this? */
  489. uint8_t state; /**< Current state of this connection. */
  490. uint8_t purpose; /**< Only used for DIR types currently. */
  491. uint8_t wants_to_read; /**< Boolean: should we start reading again once
  492. * the bandwidth throttler allows it?
  493. */
  494. uint8_t wants_to_write; /**< Boolean: should we start writing again once
  495. * the bandwidth throttler allows reads?
  496. */
  497. int s; /**< Our socket; -1 if this connection is closed. */
  498. int poll_index; /* XXXX rename. */
  499. struct event *read_event; /**< libevent event structure. */
  500. struct event *write_event; /**< libevent event structure. */
  501. int marked_for_close; /**< Boolean: should we close this conn on the next
  502. * iteration of the main loop?
  503. */
  504. const char *marked_for_close_file; /**< For debugging: in which file were
  505. * we marked for close? */
  506. int hold_open_until_flushed; /**< Despite this connection's being marked
  507. * for close, do we flush it before closing it?
  508. */
  509. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  510. int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
  511. time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
  512. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  513. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  514. * outbuf? */
  515. time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
  516. time_t timestamp_created; /**< When was this connection_t created? */
  517. time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
  518. uint32_t addr; /**< IP of the other side of the connection; used to identify
  519. * routers, along with port. */
  520. uint16_t port; /**< If non-zero, port on the other end
  521. * of the connection. */
  522. char *address; /**< FQDN (or IP) of the guy on the other end.
  523. * strdup into this, because free_connection frees it.
  524. */
  525. crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
  526. * signing key. */
  527. char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
  528. char *nickname; /**< Nickname of OR on other side (if any). */
  529. /** Nickname of planned exit node -- to be used with .exit support. */
  530. char *chosen_exit_name;
  531. /* Used only by OR connections: */
  532. tor_tls *tls; /**< TLS connection state (OR only.) */
  533. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  534. * this connection? This is always in the
  535. * range 0..1<<15-1. (OR only.)*/
  536. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  537. int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
  538. int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
  539. * add 'bandwidth' to this, capping it at 10*bandwidth.
  540. * (OPEN ORs only)
  541. */
  542. circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
  543. * connection, which half of the space should
  544. * we use? */
  545. /* Used only by DIR and AP connections: */
  546. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  547. * querying for? (DIR/AP only) */
  548. /* Used only by edge connections: */
  549. size_t stream_size; /**< Used for debugging. */
  550. uint16_t stream_id;
  551. struct connection_t *next_stream; /**< Points to the next stream at this
  552. * edge, if any (Edge only). */
  553. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  554. * this conn exits at. (Edge only.) */
  555. int package_window; /**< How many more relay cells can i send into the
  556. * circuit? (Edge only.) */
  557. int deliver_window; /**< How many more relay cells can end at me? (Edge
  558. * only.) */
  559. int done_sending; /**< For half-open connections; not used currently. */
  560. int done_receiving; /**< For half-open connections; not used currently. */
  561. char has_sent_end; /**< For debugging: set once we've set the stream end,
  562. and check in circuit_about_to_close_connection(). */
  563. struct circuit_t *on_circuit; /**< DOCDOC */
  564. /* Used only by AP connections */
  565. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  566. * only.) */
  567. /** Quasi-global identifier for this connection; used for control.c */
  568. /* XXXX NM This can get re-used after 2**32 circuits. */
  569. uint32_t global_identifier;
  570. /* Used only by control connections */
  571. uint32_t event_mask;
  572. };
  573. typedef struct connection_t connection_t;
  574. #define ADDR_POLICY_ACCEPT 1
  575. #define ADDR_POLICY_REJECT 2
  576. /** A linked list of policy rules */
  577. typedef struct addr_policy_t {
  578. char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
  579. char *string; /**< String representation of this rule. */
  580. uint32_t addr; /**< Base address to accept or reject. */
  581. uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
  582. * a &amp; msk == <b>addr</b> &amp; msk . */
  583. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  584. uint16_t prt_max; /**< Highest port number to accept/reject. */
  585. struct addr_policy_t *next; /**< Next rule in list. */
  586. } addr_policy_t;
  587. /** Information about another onion router in the network. */
  588. typedef struct {
  589. char *signed_descriptor; /**< The original signed descriptor for this router*/
  590. char *address; /**< Location of OR: either a hostname or an IP address. */
  591. char *nickname; /**< Human-readable OR name. */
  592. uint32_t addr; /**< IPv4 address of OR, in host order. */
  593. uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
  594. uint16_t dir_port; /**< Port for HTTP directory connections. */
  595. time_t published_on; /**< When was the information in this routerinfo_t
  596. * published? */
  597. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  598. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  599. char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
  600. char *platform; /**< What software/operating system is this OR using? */
  601. /* link info */
  602. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  603. * bucket per second? */
  604. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  605. /** How many bytes/s is this router known to handle? */
  606. uint32_t bandwidthcapacity;
  607. addr_policy_t *exit_policy; /**< What streams will this OR permit
  608. * to exit? */
  609. long uptime; /**< How many seconds the router claims to have been up */
  610. /* local info */
  611. int is_running; /**< As far as we know, is this OR currently running? */
  612. time_t status_set_at; /**< When did we last update is_running? */
  613. int is_verified; /**< Has a trusted dirserver validated this OR? */
  614. smartlist_t *declared_family; /**< Nicknames of router which this router
  615. * claims are its family. */
  616. } routerinfo_t;
  617. /** Contents of a running-routers list */
  618. typedef struct running_routers_t {
  619. time_t published_on; /**< When was the list marked as published? */
  620. /** Which ORs are on the list? Entries may be prefixed with ! and $. */
  621. smartlist_t *running_routers;
  622. int is_running_routers_format; /**< Are we using the old entry format? */
  623. } running_routers_t;
  624. /** Contents of a directory of onion routers. */
  625. typedef struct {
  626. /** List of routerinfo_t */
  627. smartlist_t *routers;
  628. /** Which versions of tor are recommended by this directory? */
  629. char *software_versions;
  630. /** When was the most recent directory that contributed to this list
  631. * published?
  632. */
  633. time_t published_on;
  634. time_t running_routers_updated_on;
  635. /** What is the most recently received running_routers structure? */
  636. running_routers_t *running_routers;
  637. /** Which router is claimed to have signed it? */
  638. char *signing_router;
  639. } routerlist_t;
  640. #define CRYPT_PATH_MAGIC 0x70127012u
  641. /** Holds accounting information for a single step in the layered encryption
  642. * performed by a circuit. Used only at the client edge of a circuit. */
  643. struct crypt_path_t {
  644. uint32_t magic;
  645. /* crypto environments */
  646. /** Encryption key and counter for cells heading towards the OR at this
  647. * step. */
  648. crypto_cipher_env_t *f_crypto;
  649. /** Encryption key and counter for cells heading back from the OR at this
  650. * step. */
  651. crypto_cipher_env_t *b_crypto;
  652. /** Digest state for cells heading towards the OR at this step. */
  653. crypto_digest_env_t *f_digest; /* for integrity checking */
  654. /** Digest state for cells heading away from the OR at this step. */
  655. crypto_digest_env_t *b_digest;
  656. /** Current state of Diffie-Hellman key negotiation with the OR at this
  657. * step. */
  658. crypto_dh_env_t *handshake_state;
  659. /** Negotiated key material shared with the OR at this step. */
  660. char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
  661. /** IP4 address of the OR at this step. */
  662. uint32_t addr;
  663. /** Port of the OR at this step. */
  664. uint16_t port;
  665. /** Identity key digest of the OR at this step. */
  666. char identity_digest[DIGEST_LEN];
  667. /** Is the circuit built to this step? Must be one of:
  668. * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
  669. * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
  670. * and not received an EXTENDED/CREATED)
  671. * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
  672. uint8_t state;
  673. #define CPATH_STATE_CLOSED 0
  674. #define CPATH_STATE_AWAITING_KEYS 1
  675. #define CPATH_STATE_OPEN 2
  676. struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
  677. * (The list is circular, so the last node
  678. * links to the first.) */
  679. struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
  680. * circuit. */
  681. int package_window; /**< How many bytes are we allowed to originate ending
  682. * at this step? */
  683. int deliver_window; /**< How many bytes are we willing to deliver originating
  684. * at this step? */
  685. };
  686. #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
  687. #define DH_KEY_LEN DH_BYTES
  688. #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
  689. CIPHER_KEY_LEN+\
  690. DH_KEY_LEN)
  691. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
  692. #define REND_COOKIE_LEN DIGEST_LEN
  693. typedef struct crypt_path_t crypt_path_t;
  694. /** Information used to build a circuit. */
  695. typedef struct {
  696. /** Intended length of the final circuit. */
  697. int desired_path_len;
  698. /** Nickname of planned exit node. */
  699. char *chosen_exit_name;
  700. /** Identity of planned exit node. */
  701. char chosen_exit_digest[DIGEST_LEN];
  702. /** Whether every node in the circ must have adequate uptime. */
  703. int need_uptime;
  704. /** Whether every node in the circ must have adequate capacity. */
  705. int need_capacity;
  706. /** Whether the last hop was picked with exiting in mind. */
  707. int is_internal;
  708. /** The crypt_path_t to append after rendezvous: used for rendezvous. */
  709. struct crypt_path_t *pending_final_cpath;
  710. /** How many times has building a circuit for this task failed? */
  711. int failure_count;
  712. /** At what time should we give up on this task? */
  713. time_t expiry_time;
  714. } cpath_build_state_t;
  715. #define CIRCUIT_MAGIC 0x35315243u
  716. /** Struct for a path (circuit) through the onion routing network. */
  717. struct circuit_t {
  718. uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
  719. int marked_for_close; /**< Should we close this circuit at the end of the
  720. * main loop? */
  721. const char *marked_for_close_file; /**< For debugging: in which file was this
  722. * circuit marked for close? */
  723. /** The IPv4 address of the OR that is next in this circuit. */
  724. uint32_t n_addr;
  725. /** The port for the OR that is next in this circuit. */
  726. uint16_t n_port;
  727. /** The OR connection that is previous in this circuit. */
  728. connection_t *p_conn;
  729. /** The OR connection that is next in this circuit. */
  730. connection_t *n_conn;
  731. /** The identity hash of n_conn. */
  732. char n_conn_id_digest[DIGEST_LEN];
  733. /** Linked list of AP streams associated with this circuit. */
  734. connection_t *p_streams;
  735. /** Linked list of Exit streams associated with this circuit. */
  736. connection_t *n_streams;
  737. /** Linked list of Exit streams associated with this circuit that are
  738. * still being resolved. */
  739. connection_t *resolving_streams;
  740. /** The next stream_id that will be tried when we're attempting to
  741. * construct a new AP stream originating at this circuit. */
  742. uint16_t next_stream_id;
  743. /** How many relay data cells can we package (read from edge streams)
  744. * on this circuit before we receive a circuit-level sendme cell asking
  745. * for more? */
  746. int package_window;
  747. /** How many relay data cells will we deliver (write to edge streams)
  748. * on this circuit? When deliver_window gets low, we send some
  749. * circuit-level sendme cells to indicate that we're willing to accept
  750. * more. */
  751. int deliver_window;
  752. /** The circuit_id used in the previous (backward) hop of this circuit. */
  753. uint16_t p_circ_id;
  754. /** The circuit_id used in the next (forward) hop of this circuit. */
  755. uint16_t n_circ_id;
  756. /** The cipher used by intermediate hops for cells heading toward the
  757. * OP. */
  758. crypto_cipher_env_t *p_crypto;
  759. /** The cipher used by intermediate hops for cells heading away from
  760. * the OP. */
  761. crypto_cipher_env_t *n_crypto;
  762. /** The integrity-checking digest used by intermediate hops, for
  763. * cells packaged here and heading towards the OP.
  764. */
  765. crypto_digest_env_t *p_digest;
  766. /** The integrity-checking digest used by intermediate hops, for
  767. * cells packaged at the OP and arriving here.
  768. */
  769. crypto_digest_env_t *n_digest;
  770. /** Build state for this circuit. It includes the intended path
  771. * length, the chosen exit router, rendezvous information, etc.
  772. */
  773. cpath_build_state_t *build_state;
  774. /** The doubly-linked list of crypt_path_t entries, one per hop,
  775. * for this circuit. This includes ciphers for each hop,
  776. * integrity-checking digests for each hop, and package/delivery
  777. * windows for each hop.
  778. *
  779. * The cpath field is defined only when we are the circuit's origin.
  780. */
  781. crypt_path_t *cpath;
  782. /** For storage while passing to cpuworker, or while n_conn is pending. */
  783. char onionskin[ONIONSKIN_CHALLENGE_LEN];
  784. char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
  785. time_t timestamp_created; /**< When was this circuit created? */
  786. time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
  787. * circuit is clean. */
  788. uint8_t state; /**< Current status of this circuit. */
  789. uint8_t purpose; /**< Why are we creating this circuit? */
  790. /**
  791. * The rend_query field holds y portion of y.onion (nul-terminated)
  792. * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
  793. * for a hidden service, or is S_*.
  794. */
  795. char rend_query[REND_SERVICE_ID_LEN+1];
  796. /** The rend_pk_digest field holds a hash of location-hidden service's
  797. * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
  798. */
  799. char rend_pk_digest[DIGEST_LEN];
  800. /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
  801. * C_ESTABLISH_REND. Filled with zeroes otherwise.
  802. */
  803. char rend_cookie[REND_COOKIE_LEN];
  804. /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  805. * is not marked for close. */
  806. struct circuit_t *rend_splice;
  807. /** Quasi-global identifier for this circuit; used for control.c */
  808. /* XXXX NM This can get re-used after 2**32 circuits. */
  809. uint32_t global_identifier;
  810. struct circuit_t *next; /**< Next circuit in linked list. */
  811. };
  812. typedef struct circuit_t circuit_t;
  813. #define ALLOW_UNVERIFIED_ENTRY 1
  814. #define ALLOW_UNVERIFIED_EXIT 2
  815. #define ALLOW_UNVERIFIED_MIDDLE 4
  816. #define ALLOW_UNVERIFIED_RENDEZVOUS 8
  817. #define ALLOW_UNVERIFIED_INTRODUCTION 16
  818. typedef struct exit_redirect_t {
  819. uint32_t addr;
  820. uint32_t mask;
  821. uint16_t port_min;
  822. uint16_t port_max;
  823. int is_redirect;
  824. uint32_t addr_dest;
  825. uint16_t port_dest;
  826. } exit_redirect_t;
  827. /** Configuration options for a Tor process */
  828. typedef struct {
  829. /** What should the tor process actually do? */
  830. enum {
  831. CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
  832. } command;
  833. const char *command_arg; /**< Argument for command-line option. */
  834. struct config_line_t *OldLogOptions; /**< List of configuration lines
  835. * for logfiles, old style. */
  836. struct config_line_t *Logs; /**< New-style list of configuration lines
  837. * for logs */
  838. char *DebugLogFile; /**< Where to send verbose log messages. */
  839. char *DataDirectory; /**< OR only: where to store long-term data. */
  840. char *Nickname; /**< OR only: nickname of this onion router. */
  841. char *Address; /**< OR only: configured address for this onion router. */
  842. char *PidFile; /**< Where to store PID of Tor process. */
  843. char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
  844. * as exits. */
  845. char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
  846. * as entry points. */
  847. int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
  848. * stop building circuits? */
  849. int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
  850. * stop building circuits? */
  851. char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
  852. * use in circuits. */
  853. char *RendNodes; /**< Comma-separated list of nicknames used as introduction
  854. * points. */
  855. char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
  856. * as introduction points. */
  857. smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
  858. int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
  859. struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
  860. struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
  861. struct config_line_t *DirPolicy; /**< Lists of dir policy components */
  862. /** Addresses to bind for listening for SOCKS connections. */
  863. struct config_line_t *SocksBindAddress;
  864. /** Addresses to bind for listening for OR connections. */
  865. struct config_line_t *ORBindAddress;
  866. /** Addresses to bind for listening for directory connections. */
  867. struct config_line_t *DirBindAddress;
  868. /** Local address to bind outbound sockets */
  869. char *OutboundBindAddress;
  870. /** Directory server only: which versions of
  871. * Tor should we tell users to run? */
  872. struct config_line_t *RecommendedVersions;
  873. /** Whether dirservers refuse router descriptors with private IPs. */
  874. int DirAllowPrivateAddresses;
  875. char *User; /**< Name of user to run Tor as. */
  876. char *Group; /**< Name of group to run Tor as. */
  877. double PathlenCoinWeight; /**< Parameter used to configure average path
  878. * length (alpha in geometric distribution). */
  879. int ORPort; /**< Port to listen on for OR connections. */
  880. int SocksPort; /**< Port to listen on for SOCKS connections. */
  881. int ControlPort; /**< Port to listen on for control connections. */
  882. int DirPort; /**< Port to listen on for directory connections. */
  883. int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
  884. int ClientOnly; /**< Boolean: should we never evolve into a server role? */
  885. int ConnLimit; /**< Requested maximum number of simultaneous connections. */
  886. int _ConnLimit; /**< Actual maximum number of simultaneous connections. */
  887. int IgnoreVersion; /**< If true, run no matter what versions of Tor the
  888. * directory recommends. */
  889. int RunAsDaemon; /**< If true, run in the background. (Unix only) */
  890. int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
  891. smartlist_t *FirewallPorts; /**< Which ports our firewall allows (strings). */
  892. /** Application ports that require all nodes in circ to have sufficient uptime. */
  893. smartlist_t *LongLivedPorts;
  894. /** Should we try to reuse the same exit node for a given host */
  895. smartlist_t *TrackHostExits;
  896. int TrackHostExitsExpire; /**< Number of seconds until we expire an addressmap */
  897. struct config_line_t *AddressMap; /**< List of address map directives. */
  898. int DirFetchPeriod; /**< How often do we fetch new directories? */
  899. int DirPostPeriod; /**< How often do we post our server descriptor to the
  900. * authoritative directory servers? */
  901. int RendPostPeriod; /**< How often do we post each rendezvous service
  902. * descriptor? Remember to publish them independently. */
  903. int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
  904. int KeepalivePeriod; /**< How often do we send padding cells to keep
  905. * connections alive? */
  906. int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
  907. * to wait simultaneously before we start dropping
  908. * them? */
  909. int NewCircuitPeriod; /**< How long do we use a circuit before building
  910. * a new one? */
  911. int MaxCircuitDirtiness; /**< Never use circs that were first used more than
  912. this interval ago. */
  913. uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
  914. * use in a second? */
  915. uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
  916. * use in a second? */
  917. uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
  918. * tell people we have? */
  919. int NumCpus; /**< How many CPUs should we try to use? */
  920. int RunTesting; /**< If true, create testing circuits to measure how well the
  921. * other ORs are running. */
  922. struct config_line_t *RendConfigLines; /**< List of configuration lines
  923. * for rendezvous services. */
  924. char *ContactInfo; /**< Contact info to be published in the directory */
  925. char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
  926. uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
  927. uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
  928. char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
  929. uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
  930. uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
  931. struct config_line_t *DirServers; /**< List of configuration lines
  932. * for directory servers. */
  933. char *MyFamily; /**< Declared family for this OR. */
  934. struct config_line_t *NodeFamilies; /**< List of config lines for
  935. * node families */
  936. struct config_line_t *RedirectExit; /**< List of config lines for simple
  937. * addr/port redirection */
  938. smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
  939. int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
  940. * interval starts */
  941. char *AccountingStart; /** How long is the accounting interval, and when
  942. * does it start? */
  943. uint64_t AccountingMax; /**< How many bytes do we allow per accounting
  944. * interval before hibernation? 0 for "never
  945. * hibernate." */
  946. int _AccountingMaxKB; /**< How many KB do we allow per accounting
  947. * interval before hibernation? 0 for "never
  948. * hibernate." (Based on a deprecated option)*/
  949. char *HashedControlPassword; /**< Base64-encoded hash of a password for
  950. * the control system. */
  951. int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
  952. * the control system? */
  953. int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
  954. * circuits itself (0), or does it expect a controller
  955. * to cope? (1) */
  956. int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
  957. * long do we wait before exiting? */
  958. } or_options_t;
  959. #define MAX_SOCKS_REPLY_LEN 1024
  960. #define MAX_SOCKS_ADDR_LEN 256
  961. #define SOCKS_COMMAND_CONNECT 0x01
  962. #define SOCKS_COMMAND_RESOLVE 0xF0
  963. /** State of a SOCKS request from a user to an OP */
  964. struct socks_request_t {
  965. char socks_version; /**< Which version of SOCKS did the client use? */
  966. int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
  967. size_t replylen; /**< Length of <b>reply</b>. */
  968. char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
  969. * we want to specify our own socks reply,
  970. * rather than using the default socks4 or
  971. * socks5 socks reply. We use this for the
  972. * two-stage socks5 handshake.
  973. */
  974. int has_finished; /**< Has the SOCKS handshake finished? */
  975. char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
  976. uint16_t port; /**< What port did the client ask to connect to? */
  977. };
  978. /* all the function prototypes go here */
  979. /********************************* buffers.c ***************************/
  980. buf_t *buf_new(void);
  981. buf_t *buf_new_with_capacity(size_t size);
  982. void buf_free(buf_t *buf);
  983. void buf_clear(buf_t *buf);
  984. size_t buf_datalen(const buf_t *buf);
  985. size_t buf_capacity(const buf_t *buf);
  986. const char *_buf_peek_raw_buffer(const buf_t *buf);
  987. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
  988. int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
  989. int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
  990. int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
  991. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  992. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  993. int fetch_from_buf_http(buf_t *buf,
  994. char **headers_out, size_t max_headerlen,
  995. char **body_out, size_t *body_used, size_t max_bodylen);
  996. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  997. int fetch_from_buf_control(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
  998. char **body_out);
  999. void assert_buf_ok(buf_t *buf);
  1000. /********************************* circuitbuild.c **********************/
  1001. char *circuit_list_path(circuit_t *circ, int verbose);
  1002. void circuit_log_path(int severity, circuit_t *circ);
  1003. void circuit_rep_hist_note_result(circuit_t *circ);
  1004. void circuit_dump_by_conn(connection_t *conn, int severity);
  1005. circuit_t *circuit_init(uint8_t purpose, int need_uptime,
  1006. int need_capacity, int internal);
  1007. circuit_t *circuit_establish_circuit(uint8_t purpose, routerinfo_t *exit,
  1008. int need_uptime, int need_capacity, int internal);
  1009. int circuit_handle_first_hop(circuit_t *circ);
  1010. void circuit_n_conn_done(connection_t *or_conn, int status);
  1011. int circuit_send_next_onion_skin(circuit_t *circ);
  1012. void circuit_note_clock_jumped(int seconds_elapsed);
  1013. int circuit_extend(cell_t *cell, circuit_t *circ);
  1014. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
  1015. int circuit_finish_handshake(circuit_t *circ, char *reply);
  1016. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  1017. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  1018. int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
  1019. int *need_capacity);
  1020. int circuit_append_new_exit(circuit_t *circ, routerinfo_t *exit);
  1021. int circuit_extend_to_new_exit(circuit_t *circ, routerinfo_t *exit);
  1022. void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
  1023. /********************************* circuitlist.c ***********************/
  1024. extern const char *circuit_state_to_string[];
  1025. enum which_conn_changed_t { P_CONN_CHANGED=1, N_CONN_CHANGED=0 };
  1026. void circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
  1027. connection_t *conn,
  1028. enum which_conn_changed_t which);
  1029. void circuit_close_all_marked(void);
  1030. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
  1031. circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn);
  1032. circuit_t *circuit_get_by_edge_conn(connection_t *conn);
  1033. circuit_t *circuit_get_by_conn(connection_t *conn);
  1034. circuit_t *circuit_get_by_global_id(uint32_t id);
  1035. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
  1036. circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
  1037. const char *digest, uint8_t purpose);
  1038. circuit_t *circuit_get_rendezvous(const char *cookie);
  1039. circuit_t *circuit_get_clean_open(uint8_t purpose, int need_uptime,
  1040. int need_capacity, int internal);
  1041. void circuit_mark_all_unused_circs(void);
  1042. void _circuit_mark_for_close(circuit_t *circ, int line, const char *file);
  1043. #define circuit_mark_for_close(c) \
  1044. _circuit_mark_for_close((c), __LINE__, _SHORT_FILE_)
  1045. void assert_cpath_layer_ok(const crypt_path_t *cp);
  1046. void assert_circuit_ok(const circuit_t *c);
  1047. void circuit_free_all(void);
  1048. /********************************* circuituse.c ************************/
  1049. void circuit_expire_building(time_t now);
  1050. void circuit_remove_handled_ports(smartlist_t *needed_ports);
  1051. int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
  1052. void circuit_build_needed_circs(time_t now);
  1053. void circuit_detach_stream(circuit_t *circ, connection_t *conn);
  1054. void circuit_about_to_close_connection(connection_t *conn);
  1055. void circuit_has_opened(circuit_t *circ);
  1056. void circuit_build_failed(circuit_t *circ);
  1057. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname,
  1058. int need_uptime, int need_capacity, int is_internal);
  1059. circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
  1060. int need_uptime, int need_capacity, int is_internal);
  1061. void circuit_reset_failure_count(int timeout);
  1062. int connection_ap_handshake_attach_chosen_circuit(connection_t *conn,
  1063. circuit_t *circ);
  1064. int connection_ap_handshake_attach_circuit(connection_t *conn);
  1065. /********************************* command.c ***************************/
  1066. void command_process_cell(cell_t *cell, connection_t *conn);
  1067. extern unsigned long stats_n_padding_cells_processed;
  1068. extern unsigned long stats_n_create_cells_processed;
  1069. extern unsigned long stats_n_created_cells_processed;
  1070. extern unsigned long stats_n_relay_cells_processed;
  1071. extern unsigned long stats_n_destroy_cells_processed;
  1072. /********************************* config.c ***************************/
  1073. struct config_line_t {
  1074. char *key;
  1075. char *value;
  1076. struct config_line_t *next;
  1077. };
  1078. or_options_t *get_options(void);
  1079. void set_options(or_options_t *new_val);
  1080. int options_act(void);
  1081. void config_free_all(void);
  1082. int config_get_lines(char *string, struct config_line_t **result);
  1083. void config_free_lines(struct config_line_t *front);
  1084. int config_trial_assign(struct config_line_t *list, int reset);
  1085. int resolve_my_address(const char *address, uint32_t *addr);
  1086. void options_init(or_options_t *options);
  1087. int init_from_config(int argc, char **argv);
  1088. int config_init_logs(or_options_t *options, int validate_only);
  1089. int config_parse_addr_policy(struct config_line_t *cfg,
  1090. addr_policy_t **dest);
  1091. void addr_policy_free(addr_policy_t *p);
  1092. int config_option_is_recognized(const char *key);
  1093. struct config_line_t *config_get_assigned_option(or_options_t *options,
  1094. const char *key);
  1095. char *config_dump_options(or_options_t *options, int minimal);
  1096. int save_current_config(void);
  1097. /********************************* connection.c ***************************/
  1098. const char *conn_type_to_string(int type);
  1099. const char *conn_state_to_string(int type, int state);
  1100. connection_t *connection_new(int type);
  1101. void connection_unregister(connection_t *conn);
  1102. void connection_free(connection_t *conn);
  1103. void connection_free_all(void);
  1104. void connection_about_to_close_connection(connection_t *conn);
  1105. void connection_close_immediate(connection_t *conn);
  1106. void _connection_mark_for_close(connection_t *conn,int line, const char *file);
  1107. #define connection_mark_for_close(c) \
  1108. _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
  1109. void connection_expire_held_open(void);
  1110. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  1111. int retry_all_listeners(int force);
  1112. void connection_bucket_init(void);
  1113. void connection_bucket_refill(struct timeval *now);
  1114. int connection_handle_read(connection_t *conn);
  1115. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  1116. int connection_wants_to_flush(connection_t *conn);
  1117. int connection_outbuf_too_full(connection_t *conn);
  1118. int connection_handle_write(connection_t *conn);
  1119. void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
  1120. connection_t *connection_or_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  1121. connection_t *connection_get_by_identity_digest(const char *digest, int type);
  1122. connection_t *connection_get_by_global_id(uint32_t id);
  1123. connection_t *connection_get_by_type(int type);
  1124. connection_t *connection_get_by_type_state(int type, int state);
  1125. connection_t *connection_get_by_type_purpose(int type, int purpose);
  1126. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  1127. connection_t *connection_get_by_type_state_rendquery(int type, int state, const char *rendquery);
  1128. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  1129. #define connection_has_pending_tls_data(conn) \
  1130. ((conn)->type == CONN_TYPE_OR && \
  1131. (conn)->state == OR_CONN_STATE_OPEN && \
  1132. tor_tls_get_pending_bytes((conn)->tls))
  1133. int connection_is_listener(connection_t *conn);
  1134. int connection_state_is_open(connection_t *conn);
  1135. int connection_state_is_connecting(connection_t *conn);
  1136. int connection_send_destroy(uint16_t circ_id, connection_t *conn);
  1137. void assert_connection_ok(connection_t *conn, time_t now);
  1138. int connection_or_nonopen_was_started_here(connection_t *conn);
  1139. /********************************* connection_edge.c ***************************/
  1140. #define connection_mark_unattached_ap(conn, endreason) \
  1141. _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
  1142. void _connection_mark_unattached_ap(connection_t *conn, int endreason,
  1143. int line, const char *file);
  1144. int connection_edge_reached_eof(connection_t *conn);
  1145. int connection_edge_process_inbuf(connection_t *conn, int package_partial);
  1146. int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
  1147. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  1148. int connection_edge_end_errno(connection_t *conn, crypt_path_t *cpath_layer);
  1149. int connection_edge_finished_flushing(connection_t *conn);
  1150. int connection_edge_finished_connecting(connection_t *conn);
  1151. int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  1152. int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
  1153. int connection_ap_make_bridge(char *address, uint16_t port);
  1154. void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  1155. size_t replylen,
  1156. socks5_reply_status_t status);
  1157. void connection_ap_handshake_socks_resolved(connection_t *conn,
  1158. int answer_type,
  1159. size_t answer_len,
  1160. const char *answer);
  1161. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  1162. int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
  1163. void connection_exit_connect(connection_t *conn);
  1164. int connection_edge_is_rendezvous_stream(connection_t *conn);
  1165. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  1166. void connection_ap_expire_beginning(void);
  1167. void connection_ap_attach_pending(void);
  1168. int connection_ap_detach_retriable(connection_t *conn, circuit_t *circ);
  1169. void addressmap_init(void);
  1170. void addressmap_clean(time_t now);
  1171. void addressmap_clear_configured(void);
  1172. void addressmap_clear_transient(void);
  1173. void addressmap_free_all(void);
  1174. void addressmap_rewrite(char *address, size_t maxlen);
  1175. int addressmap_already_mapped(const char *address);
  1176. void addressmap_register(const char *address, char *new_address, time_t expires);
  1177. int client_dns_incr_failures(const char *address);
  1178. void client_dns_set_addressmap(const char *address, uint32_t val, const char *exitname);
  1179. const char *addressmap_register_virtual_address(int type, char *new_address);
  1180. void addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires);
  1181. void parse_socks_policy(void);
  1182. void free_socks_policy(void);
  1183. int socks_policy_permits_address(uint32_t addr);
  1184. void set_exit_redirects(smartlist_t *lst);
  1185. typedef enum hostname_type_t {
  1186. NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME
  1187. } hostname_type_t;
  1188. hostname_type_t parse_extended_hostname(char *address);
  1189. /********************************* connection_or.c ***************************/
  1190. int connection_or_reached_eof(connection_t *conn);
  1191. int connection_or_process_inbuf(connection_t *conn);
  1192. int connection_or_finished_flushing(connection_t *conn);
  1193. int connection_or_finished_connecting(connection_t *conn);
  1194. connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  1195. const char *id_digest);
  1196. int connection_tls_start_handshake(connection_t *conn, int receiving);
  1197. int connection_tls_continue_handshake(connection_t *conn);
  1198. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
  1199. void connection_or_update_nickname(connection_t *conn);
  1200. /********************************* control.c ***************************/
  1201. typedef enum circuit_status_event_t {
  1202. CIRC_EVENT_LAUNCHED = 0,
  1203. CIRC_EVENT_BUILT = 1,
  1204. CIRC_EVENT_EXTENDED = 2,
  1205. CIRC_EVENT_FAILED = 3,
  1206. CIRC_EVENT_CLOSED = 4,
  1207. } circuit_status_event_t;
  1208. typedef enum stream_status_event_t {
  1209. STREAM_EVENT_SENT_CONNECT = 0,
  1210. STREAM_EVENT_SENT_RESOLVE = 1,
  1211. STREAM_EVENT_SUCCEEDED = 2,
  1212. STREAM_EVENT_FAILED = 3,
  1213. STREAM_EVENT_CLOSED = 4,
  1214. STREAM_EVENT_NEW = 5,
  1215. STREAM_EVENT_NEW_RESOLVE = 6,
  1216. STREAM_EVENT_FAILED_RETRIABLE = 7
  1217. } stream_status_event_t;
  1218. typedef enum or_conn_status_event_t {
  1219. OR_CONN_EVENT_LAUNCHED = 0,
  1220. OR_CONN_EVENT_CONNECTED = 1,
  1221. OR_CONN_EVENT_FAILED = 2,
  1222. OR_CONN_EVENT_CLOSED = 3,
  1223. } or_conn_status_event_t;
  1224. void adjust_event_log_severity(void);
  1225. int connection_control_finished_flushing(connection_t *conn);
  1226. int connection_control_reached_eof(connection_t *conn);
  1227. int connection_control_process_inbuf(connection_t *conn);
  1228. int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
  1229. int control_event_stream_status(connection_t *conn, stream_status_event_t e);
  1230. int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
  1231. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  1232. void control_event_logmsg(int severity, const char *msg);
  1233. int control_event_descriptors_changed(smartlist_t *routers);
  1234. int init_cookie_authentication(int enabled);
  1235. int decode_hashed_password(char *buf, const char *hashed);
  1236. /********************************* cpuworker.c *****************************/
  1237. void cpu_init(void);
  1238. void cpuworkers_rotate(void);
  1239. int connection_cpu_finished_flushing(connection_t *conn);
  1240. int connection_cpu_reached_eof(connection_t *conn);
  1241. int connection_cpu_process_inbuf(connection_t *conn);
  1242. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  1243. void *task);
  1244. /********************************* directory.c ***************************/
  1245. int dir_policy_permits_address(uint32_t addr);
  1246. void directory_post_to_dirservers(uint8_t purpose, const char *payload,
  1247. size_t payload_len);
  1248. void directory_get_from_dirserver(uint8_t purpose, const char *resource,
  1249. int retry_if_no_servers);
  1250. void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
  1251. int private_connection, const char *resource,
  1252. const char *payload, size_t payload_len);
  1253. int parse_http_response(const char *headers, int *code, time_t *date,
  1254. int *compression, char **response);
  1255. int connection_dir_reached_eof(connection_t *conn);
  1256. int connection_dir_process_inbuf(connection_t *conn);
  1257. int connection_dir_finished_flushing(connection_t *conn);
  1258. int connection_dir_finished_connecting(connection_t *conn);
  1259. void connection_dir_connect_failed(connection_t *conn);
  1260. void parse_dir_policy(void);
  1261. void free_dir_policy(void);
  1262. /********************************* dirserv.c ***************************/
  1263. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  1264. int dirserv_parse_fingerprint_file(const char *fname);
  1265. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  1266. void dirserv_free_fingerprint_list(void);
  1267. const char *dirserv_get_nickname_by_digest(const char *digest);
  1268. int dirserv_add_descriptor(const char **desc, const char **msg);
  1269. int dirserv_load_from_directory_string(const char *dir);
  1270. void dirserv_free_descriptors(void);
  1271. int list_server_status(smartlist_t *routers,
  1272. char **running_routers_out, char **router_status_out);
  1273. void dirserv_remove_old_servers(int age);
  1274. int dirserv_dump_directory_to_string(char **dir_out,
  1275. crypto_pk_env_t *private_key);
  1276. void directory_set_dirty(void);
  1277. size_t dirserv_get_directory(const char **cp, int compress);
  1278. size_t dirserv_get_runningrouters(const char **rr, int compress);
  1279. void dirserv_set_cached_directory(const char *directory, time_t when,
  1280. int is_running_routers);
  1281. void dirserv_orconn_tls_done(const char *address,
  1282. uint16_t or_port,
  1283. const char *digest_rcvd,
  1284. const char *nickname);
  1285. void dirserv_free_all(void);
  1286. /********************************* dns.c ***************************/
  1287. void dns_init(void);
  1288. void dns_free_all(void);
  1289. int connection_dns_finished_flushing(connection_t *conn);
  1290. int connection_dns_reached_eof(connection_t *conn);
  1291. int connection_dns_process_inbuf(connection_t *conn);
  1292. void dnsworkers_rotate(void);
  1293. void connection_dns_remove(connection_t *conn);
  1294. void assert_connection_edge_not_dns_pending(connection_t *conn);
  1295. void assert_all_pending_dns_resolves_ok(void);
  1296. void dns_cancel_pending_resolve(char *question);
  1297. int dns_resolve(connection_t *exitconn);
  1298. /********************************* hibernate.c **********************/
  1299. int accounting_parse_options(or_options_t *options, int validate_only);
  1300. int accounting_is_enabled(or_options_t *options);
  1301. void configure_accounting(time_t now);
  1302. void accounting_run_housekeeping(time_t now);
  1303. void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
  1304. int accounting_record_bandwidth_usage(time_t now);
  1305. void hibernate_begin_shutdown(void);
  1306. int we_are_hibernating(void);
  1307. void consider_hibernation(time_t now);
  1308. /********************************* main.c ***************************/
  1309. int connection_add(connection_t *conn);
  1310. int connection_remove(connection_t *conn);
  1311. int connection_in_array(connection_t *conn);
  1312. void add_connection_to_closeable_list(connection_t *conn);
  1313. int connection_is_on_closeable_list(connection_t *conn);
  1314. void get_connection_array(connection_t ***array, int *n);
  1315. void connection_watch_events(connection_t *conn, short events);
  1316. int connection_is_reading(connection_t *conn);
  1317. void connection_stop_reading(connection_t *conn);
  1318. void connection_start_reading(connection_t *conn);
  1319. int connection_is_writing(connection_t *conn);
  1320. void connection_stop_writing(connection_t *conn);
  1321. void connection_start_writing(connection_t *conn);
  1322. void directory_all_unreachable(time_t now);
  1323. void directory_has_arrived(time_t now, char *identity_digest);
  1324. int control_signal_act(int the_signal);
  1325. void handle_signals(int is_parent);
  1326. void tor_cleanup(void);
  1327. int tor_main(int argc, char *argv[]);
  1328. /********************************* onion.c ***************************/
  1329. int onion_pending_add(circuit_t *circ);
  1330. circuit_t *onion_next_task(void);
  1331. void onion_pending_remove(circuit_t *circ);
  1332. int onion_skin_create(crypto_pk_env_t *router_key,
  1333. crypto_dh_env_t **handshake_state_out,
  1334. char *onion_skin_out);
  1335. int onion_skin_server_handshake(char *onion_skin,
  1336. crypto_pk_env_t *private_key,
  1337. crypto_pk_env_t *prev_private_key,
  1338. char *handshake_reply_out,
  1339. char *key_out,
  1340. size_t key_out_len);
  1341. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  1342. char *handshake_reply,
  1343. char *key_out,
  1344. size_t key_out_len);
  1345. void clear_pending_onions(void);
  1346. /********************************* relay.c ***************************/
  1347. extern unsigned long stats_n_relay_cells_relayed;
  1348. extern unsigned long stats_n_relay_cells_delivered;
  1349. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  1350. int cell_direction);
  1351. void relay_header_pack(char *dest, const relay_header_t *src);
  1352. void relay_header_unpack(relay_header_t *dest, const char *src);
  1353. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  1354. int relay_command, const char *payload,
  1355. size_t payload_len, crypt_path_t *cpath_layer);
  1356. int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
  1357. void connection_edge_consider_sending_sendme(connection_t *conn);
  1358. socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
  1359. int errno_to_end_reason(int e);
  1360. extern uint64_t stats_n_data_cells_packaged;
  1361. extern uint64_t stats_n_data_bytes_packaged;
  1362. extern uint64_t stats_n_data_cells_received;
  1363. extern uint64_t stats_n_data_bytes_received;
  1364. /********************************* rephist.c ***************************/
  1365. void rep_hist_init(void);
  1366. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  1367. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  1368. void rep_hist_note_disconnect(const char* nickname, time_t when);
  1369. void rep_hist_note_connection_died(const char* nickname, time_t when);
  1370. void rep_hist_note_extend_succeeded(const char *from_name,
  1371. const char *to_name);
  1372. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  1373. void rep_hist_dump_stats(time_t now, int severity);
  1374. void rep_hist_note_bytes_read(int num_bytes, time_t when);
  1375. void rep_hist_note_bytes_written(int num_bytes, time_t when);
  1376. int rep_hist_bandwidth_assess(void);
  1377. char *rep_hist_get_bandwidth_lines(void);
  1378. void rep_history_clean(time_t before);
  1379. void rep_hist_note_used_port(uint16_t port, time_t now);
  1380. smartlist_t *rep_hist_get_predicted_ports(time_t now);
  1381. void rep_hist_note_used_hidserv(time_t now, int need_uptime, int need_capacity);
  1382. int rep_hist_get_predicted_hidserv(time_t now, int *need_uptime, int *need_capacity);
  1383. void rep_hist_note_used_resolve(time_t now);
  1384. int rep_hist_get_predicted_resolve(time_t now);
  1385. void rep_hist_free_all(void);
  1386. /********************************* rendclient.c ***************************/
  1387. void rend_client_introcirc_has_opened(circuit_t *circ);
  1388. void rend_client_rendcirc_has_opened(circuit_t *circ);
  1389. int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
  1390. void rend_client_refetch_renddesc(const char *query);
  1391. int rend_client_remove_intro_point(char *failed_intro, const char *query);
  1392. int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
  1393. int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1394. void rend_client_desc_here(char *query);
  1395. char *rend_client_get_random_intro(char *query);
  1396. int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
  1397. /********************************* rendcommon.c ***************************/
  1398. typedef struct rend_service_descriptor_t {
  1399. crypto_pk_env_t *pk;
  1400. time_t timestamp;
  1401. int n_intro_points;
  1402. char **intro_points;
  1403. } rend_service_descriptor_t;
  1404. int rend_cmp_service_ids(const char *one, const char *two);
  1405. void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  1406. const char *payload);
  1407. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  1408. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  1409. crypto_pk_env_t *key,
  1410. char **str_out,
  1411. size_t *len_out);
  1412. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
  1413. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  1414. typedef struct rend_cache_entry_t {
  1415. size_t len; /* Length of desc */
  1416. time_t received; /* When did we get the descriptor? */
  1417. char *desc; /* Service descriptor */
  1418. rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
  1419. } rend_cache_entry_t;
  1420. void rend_cache_init(void);
  1421. void rend_cache_clean(void);
  1422. void rend_cache_free_all(void);
  1423. int rend_valid_service_id(const char *query);
  1424. int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
  1425. int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
  1426. int rend_cache_store(const char *desc, size_t desc_len);
  1427. /********************************* rendservice.c ***************************/
  1428. int rend_config_services(or_options_t *options, int validate_only);
  1429. int rend_service_load_keys(void);
  1430. void rend_services_init(void);
  1431. void rend_services_introduce(void);
  1432. void rend_consider_services_upload(time_t now);
  1433. void rend_service_intro_has_opened(circuit_t *circuit);
  1434. int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
  1435. void rend_service_rendezvous_has_opened(circuit_t *circuit);
  1436. int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
  1437. void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
  1438. int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
  1439. void rend_service_dump_stats(int severity);
  1440. void rend_service_free_all(void);
  1441. /********************************* rendmid.c *******************************/
  1442. int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
  1443. int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
  1444. int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1445. int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1446. /********************************* router.c ***************************/
  1447. typedef enum {
  1448. ADDR_POLICY_ACCEPTED=0,
  1449. ADDR_POLICY_REJECTED=-1,
  1450. ADDR_POLICY_PROBABLY_ACCEPTED=1,
  1451. ADDR_POLICY_PROBABLY_REJECTED=2
  1452. } addr_policy_result_t;
  1453. void set_onion_key(crypto_pk_env_t *k);
  1454. crypto_pk_env_t *get_onion_key(void);
  1455. crypto_pk_env_t *get_previous_onion_key(void);
  1456. time_t get_onion_key_set_at(void);
  1457. void set_identity_key(crypto_pk_env_t *k);
  1458. crypto_pk_env_t *get_identity_key(void);
  1459. int identity_key_is_set(void);
  1460. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  1461. void rotate_onion_key(void);
  1462. crypto_pk_env_t *init_key_from_file(const char *fname);
  1463. int init_keys(void);
  1464. int check_whether_orport_reachable(void);
  1465. int check_whether_dirport_reachable(void);
  1466. void consider_testing_reachability(void);
  1467. void router_orport_found_reachable(void);
  1468. void router_dirport_found_reachable(void);
  1469. void server_has_changed_ip(void);
  1470. void consider_publishable_server(time_t now, int force);
  1471. int authdir_mode(or_options_t *options);
  1472. int clique_mode(or_options_t *options);
  1473. int server_mode(or_options_t *options);
  1474. int advertised_server_mode(void);
  1475. int proxy_mode(or_options_t *options);
  1476. void router_retry_connections(void);
  1477. int router_is_clique_mode(routerinfo_t *router);
  1478. void router_upload_dir_desc_to_dirservers(int force);
  1479. void mark_my_descriptor_dirty(void);
  1480. int router_compare_to_my_exit_policy(connection_t *conn);
  1481. routerinfo_t *router_get_my_routerinfo(void);
  1482. const char *router_get_my_descriptor(void);
  1483. int router_is_me(routerinfo_t *router);
  1484. int router_rebuild_descriptor(int force);
  1485. int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  1486. crypto_pk_env_t *ident_key);
  1487. int is_legal_nickname(const char *s);
  1488. int is_legal_nickname_or_hexdigest(const char *s);
  1489. void router_free_all_keys(void);
  1490. /********************************* routerlist.c ***************************/
  1491. typedef struct trusted_dir_server_t {
  1492. char *address;
  1493. uint32_t addr;
  1494. uint16_t dir_port;
  1495. char digest[DIGEST_LEN];
  1496. int is_running;
  1497. } trusted_dir_server_t;
  1498. int router_reload_router_list(void);
  1499. void router_get_trusted_dir_servers(smartlist_t **outp);
  1500. routerinfo_t *router_pick_directory_server(int requireothers,
  1501. int fascistfirewall,
  1502. int for_running_routers,
  1503. int retry_if_no_servers);
  1504. trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
  1505. int fascistfirewall,
  1506. int retry_if_no_servers);
  1507. int all_trusted_directory_servers_down(void);
  1508. struct smartlist_t;
  1509. void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
  1510. void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
  1511. int router_nickname_is_in_list(routerinfo_t *router, const char *list);
  1512. routerinfo_t *routerlist_find_my_routerinfo(void);
  1513. int router_nickname_matches(routerinfo_t *router, const char *nickname);
  1514. int exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
  1515. int warn);
  1516. /** How many seconds a router must be up before we'll use it for
  1517. * reliability-critical node positions.
  1518. */
  1519. #define ROUTER_REQUIRED_MIN_UPTIME (24*3600) /* a day */
  1520. #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
  1521. int router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity);
  1522. routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
  1523. routerinfo_t *router_choose_random_node(const char *preferred,
  1524. const char *excluded,
  1525. struct smartlist_t *excludedsmartlist,
  1526. int need_uptime, int need_bandwidth,
  1527. int allow_unverified, int strict);
  1528. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  1529. routerinfo_t *router_get_by_nickname(const char *nickname);
  1530. routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
  1531. routerinfo_t *router_get_by_digest(const char *digest);
  1532. int router_digest_is_trusted_dir(const char *digest);
  1533. void router_get_routerlist(routerlist_t **prouterlist);
  1534. time_t routerlist_get_published_time(void);
  1535. void routerlist_free(routerlist_t *routerlist);
  1536. void routerinfo_free(routerinfo_t *router);
  1537. void routerlist_free_current(void);
  1538. void free_trusted_dir_servers(void);
  1539. routerinfo_t *routerinfo_copy(const routerinfo_t *router);
  1540. void router_mark_as_down(const char *digest);
  1541. void routerlist_remove_old_routers(int age);
  1542. int router_load_single_router(const char *s, const char **msg);
  1543. int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
  1544. int dir_is_recent, int dir_is_cached);
  1545. addr_policy_result_t router_compare_addr_to_addr_policy(uint32_t addr,
  1546. uint16_t port, addr_policy_t *policy);
  1547. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
  1548. int need_uptime);
  1549. int router_exit_policy_rejects_all(routerinfo_t *router);
  1550. void running_routers_free(running_routers_t *rr);
  1551. void routerlist_set_runningrouters(routerlist_t *list, running_routers_t *rr);
  1552. void routerlist_update_from_runningrouters(routerlist_t *list,
  1553. running_routers_t *rr);
  1554. int routers_update_status_from_entry(smartlist_t *routers,
  1555. time_t list_time,
  1556. const char *s,
  1557. int rr_format);
  1558. int router_update_status_from_smartlist(routerinfo_t *r,
  1559. time_t list_time,
  1560. smartlist_t *running_list,
  1561. int rr_format);
  1562. void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
  1563. void clear_trusted_dir_servers(void);
  1564. /********************************* routerparse.c ************************/
  1565. #define MAX_STATUS_TAG_LEN 32
  1566. /** Structure to hold parsed Tor versions. This is a little messier
  1567. * than we would like it to be, because we changed version schemes with 0.1.0.
  1568. *
  1569. * See version-spec.txt for the whole business.
  1570. */
  1571. typedef struct tor_version_t {
  1572. int major;
  1573. int minor;
  1574. int micro;
  1575. /** Release status. For version in the post-0.1 format, this is always
  1576. * VER_RELEASE. */
  1577. enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
  1578. int patchlevel;
  1579. /** CVS status. For version in the post-0.1 format, this is always
  1580. * IS_NOT_CVS */
  1581. enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
  1582. char status_tag[MAX_STATUS_TAG_LEN];
  1583. } tor_version_t;
  1584. int router_get_router_hash(const char *s, char *digest);
  1585. int router_get_dir_hash(const char *s, char *digest);
  1586. int router_get_runningrouters_hash(const char *s, char *digest);
  1587. int router_parse_list_from_string(const char **s,
  1588. routerlist_t **dest,
  1589. smartlist_t *good_nickname_list,
  1590. int rr_format,
  1591. time_t published);
  1592. int router_parse_routerlist_from_directory(const char *s,
  1593. routerlist_t **dest,
  1594. crypto_pk_env_t *pkey,
  1595. int check_version,
  1596. int write_to_cache);
  1597. running_routers_t *router_parse_runningrouters(const char *str,
  1598. int write_to_cache);
  1599. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
  1600. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  1601. addr_policy_t *router_parse_addr_policy_from_string(const char *s);
  1602. int check_software_version_against_directory(const char *directory,
  1603. int ignoreversion);
  1604. int tor_version_parse(const char *s, tor_version_t *out);
  1605. int tor_version_as_new_as(const char *platform, const char *cutoff);
  1606. int tor_version_compare(tor_version_t *a, tor_version_t *b);
  1607. void assert_addr_policy_ok(addr_policy_t *t);
  1608. #endif