or.h 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file or.h
  8. * \brief Master header file for Tor-specific functionality.
  9. **/
  10. #ifndef __OR_H
  11. #define __OR_H
  12. #define OR_H_ID "$Id$"
  13. #include "orconfig.h"
  14. #ifdef MS_WINDOWS
  15. #define WIN32_WINNT 0x400
  16. #define _WIN32_WINNT 0x400
  17. #define WIN32_LEAN_AND_MEAN
  18. #endif
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #ifdef HAVE_SIGNAL_H
  28. #include <signal.h>
  29. #endif
  30. #ifdef HAVE_NETDB_H
  31. #include <netdb.h>
  32. #endif
  33. #ifdef HAVE_CTYPE_H
  34. #include <ctype.h>
  35. #endif
  36. #ifdef HAVE_SYS_PARAM_H
  37. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  38. #endif
  39. #include "../common/torint.h"
  40. #ifdef HAVE_SYS_WAIT_H
  41. #include <sys/wait.h>
  42. #endif
  43. #ifdef HAVE_SYS_FCNTL_H
  44. #include <sys/fcntl.h>
  45. #endif
  46. #ifdef HAVE_FCNTL_H
  47. #include <fcntl.h>
  48. #endif
  49. #ifdef HAVE_SYS_IOCTL_H
  50. #include <sys/ioctl.h>
  51. #endif
  52. #ifdef HAVE_SYS_SOCKET_H
  53. #include <sys/socket.h>
  54. #endif
  55. #ifdef HAVE_SYS_TIME_H
  56. #include <sys/time.h>
  57. #endif
  58. #ifdef HAVE_SYS_STAT_H
  59. #include <sys/stat.h>
  60. #endif
  61. #ifdef HAVE_NETINET_IN_H
  62. #include <netinet/in.h>
  63. #endif
  64. #ifdef HAVE_ARPA_INET_H
  65. #include <arpa/inet.h>
  66. #endif
  67. #ifdef HAVE_ERRNO_H
  68. #include <errno.h>
  69. #endif
  70. #ifdef HAVE_ASSERT_H
  71. #include <assert.h>
  72. #endif
  73. #ifdef HAVE_TIME_H
  74. #include <time.h>
  75. #endif
  76. /** Upper bound on maximum simultaneous connections; can be lowered by
  77. * config file. */
  78. #ifdef CYGWIN
  79. /* http://archives.seul.org/or/talk/Aug-2006/msg00210.html */
  80. #define MAXCONNECTIONS 3200
  81. #else
  82. /* very high by default. "nobody should need more than this..." */
  83. #define MAXCONNECTIONS 15000
  84. #endif
  85. #ifdef MS_WINDOWS
  86. /* No, we don't need to redefine FD_SETSIZE before including winsock:
  87. * we use libevent now, and libevent handles the select() stuff. Yes,
  88. * some documents imply that we need to redefine anyway if we're using
  89. * select() anywhere in our application or in anything it links to: these
  90. * documents are either the holy texts of a cargo cult of network
  91. * programmers, or more likely a simplification of what's going on for
  92. * people who haven't read winsock[2].c for themselves.
  93. */
  94. #if (_MSC_VER <= 1300)
  95. #include <winsock.h>
  96. #else
  97. #include <winsock2.h>
  98. #include <ws2tcpip.h>
  99. #endif
  100. #endif
  101. #ifdef MS_WINDOWS
  102. #include <io.h>
  103. #include <process.h>
  104. #include <direct.h>
  105. #include <windows.h>
  106. #define snprintf _snprintf
  107. #endif
  108. #ifdef HAVE_EVENT_H
  109. #include <event.h>
  110. #else
  111. #error "Tor requires libevent to build."
  112. #endif
  113. #include "../common/crypto.h"
  114. #include "../common/tortls.h"
  115. #include "../common/log.h"
  116. #include "../common/compat.h"
  117. #include "../common/container.h"
  118. #include "../common/util.h"
  119. #include "../common/torgzip.h"
  120. /* These signals are defined to help control_signal_act work.
  121. * XXXX Move into compat.h ?
  122. */
  123. #ifndef SIGHUP
  124. #define SIGHUP 1
  125. #endif
  126. #ifndef SIGINT
  127. #define SIGINT 2
  128. #endif
  129. #ifndef SIGUSR1
  130. #define SIGUSR1 10
  131. #endif
  132. #ifndef SIGUSR2
  133. #define SIGUSR2 12
  134. #endif
  135. #ifndef SIGTERM
  136. #define SIGTERM 15
  137. #endif
  138. /* Controller signals start at a high number so we don't
  139. * conflict with system-defined signals. */
  140. #define SIGNEWNYM 129
  141. #if (SIZEOF_CELL_T != 0)
  142. /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
  143. * that our stuff always calls cell_t something different. */
  144. #define cell_t tor_cell_t
  145. #endif
  146. #define MAX_NICKNAME_LEN 19
  147. /* Hex digest plus dollar sign. */
  148. #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
  149. /** Maximum size, in bytes, for resized buffers. */
  150. #define MAX_BUF_SIZE ((1<<24)-1)
  151. #define MAX_DIR_SIZE MAX_BUF_SIZE
  152. /* For http parsing */
  153. #define MAX_HEADERS_SIZE 50000
  154. #define MAX_BODY_SIZE 500000
  155. /** How long do we keep DNS cache entries before purging them (regardless of
  156. * their TTL)? */
  157. #define MAX_DNS_ENTRY_AGE (30*60)
  158. #define DEFAULT_DNS_TTL (30*60)
  159. /** How long can a TTL be before we stop believing it? */
  160. #define MAX_DNS_TTL (3*60*60)
  161. /** How small can a TTL be before we stop believing it? */
  162. #define MIN_DNS_TTL (60)
  163. /** How often do we rotate onion keys? */
  164. #define MIN_ONION_KEY_LIFETIME (7*24*60*60)
  165. /** How often do we rotate TLS contexts? */
  166. #define MAX_SSL_KEY_LIFETIME (2*60*60)
  167. /** How old do we allow a router to get before removing it
  168. * from the router list? In seconds. */
  169. #define ROUTER_MAX_AGE (60*60*48)
  170. /** How old can a router get before we (as a server) will no longer
  171. * consider it live? In seconds. */
  172. #define ROUTER_MAX_AGE_TO_PUBLISH (60*60*20)
  173. /** How old do we let a saved descriptor get before removing it? */
  174. #define OLD_ROUTER_DESC_MAX_AGE (60*60*60)
  175. /** How old do we let a networkstatus get before ignoring it? */
  176. #define NETWORKSTATUS_MAX_AGE (60*60*24)
  177. typedef enum {
  178. CIRC_ID_TYPE_LOWER=0,
  179. CIRC_ID_TYPE_HIGHER=1
  180. } circ_id_type_t;
  181. #define _CONN_TYPE_MIN 3
  182. /** Type for sockets listening for OR connections. */
  183. #define CONN_TYPE_OR_LISTENER 3
  184. /** A bidirectional TLS connection transmitting a sequence of cells.
  185. * May be from an OR to an OR, or from an OP to an OR. */
  186. #define CONN_TYPE_OR 4
  187. /** A TCP connection from an onion router to a stream's destination. */
  188. #define CONN_TYPE_EXIT 5
  189. /** Type for sockets listening for SOCKS connections. */
  190. #define CONN_TYPE_AP_LISTENER 6
  191. /** A SOCKS proxy connection from the user application to the onion
  192. * proxy. */
  193. #define CONN_TYPE_AP 7
  194. /** Type for sockets listening for HTTP connections to the directory server. */
  195. #define CONN_TYPE_DIR_LISTENER 8
  196. /** Type for HTTP connections to the directory server. */
  197. #define CONN_TYPE_DIR 9
  198. /** Connection from the main process to a DNS worker process. */
  199. #define CONN_TYPE_DNSWORKER 10
  200. /** Connection from the main process to a CPU worker process. */
  201. #define CONN_TYPE_CPUWORKER 11
  202. /** Type for listening for connections from user interface process. */
  203. #define CONN_TYPE_CONTROL_LISTENER 12
  204. /** Type for connections from user interface process. */
  205. #define CONN_TYPE_CONTROL 13
  206. /** Type for sockets listening for transparent proxy connections. */
  207. #define CONN_TYPE_AP_TRANS_LISTENER 14
  208. #define _CONN_TYPE_MAX 14
  209. #define CONN_IS_EDGE(x) \
  210. ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  211. /** State for any listener connection. */
  212. #define LISTENER_STATE_READY 0
  213. #define _DNSWORKER_STATE_MIN 1
  214. /** State for a connection to a dnsworker process that's idle. */
  215. #define DNSWORKER_STATE_IDLE 1
  216. /** State for a connection to a dnsworker process that's resolving a
  217. * hostname. */
  218. #define DNSWORKER_STATE_BUSY 2
  219. #define _DNSWORKER_STATE_MAX 2
  220. #define _CPUWORKER_STATE_MIN 1
  221. /** State for a connection to a cpuworker process that's idle. */
  222. #define CPUWORKER_STATE_IDLE 1
  223. /** State for a connection to a cpuworker process that's processing a
  224. * handshake. */
  225. #define CPUWORKER_STATE_BUSY_ONION 2
  226. #define _CPUWORKER_STATE_MAX 2
  227. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  228. #define _OR_CONN_STATE_MIN 1
  229. /** State for a connection to an OR: waiting for connect() to finish. */
  230. #define OR_CONN_STATE_CONNECTING 1
  231. /** State for a connection to an OR: waiting for proxy command to flush. */
  232. #define OR_CONN_STATE_PROXY_FLUSHING 2
  233. /** State for a connection to an OR: waiting for proxy response. */
  234. #define OR_CONN_STATE_PROXY_READING 3
  235. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  236. #define OR_CONN_STATE_HANDSHAKING 4
  237. /** State for a connection to an OR: Ready to send/receive cells. */
  238. #define OR_CONN_STATE_OPEN 5
  239. #define _OR_CONN_STATE_MAX 5
  240. #define _EXIT_CONN_STATE_MIN 1
  241. /** State for an exit connection: waiting for response from dns farm. */
  242. #define EXIT_CONN_STATE_RESOLVING 1
  243. /** State for an exit connection: waiting for connect() to finish. */
  244. #define EXIT_CONN_STATE_CONNECTING 2
  245. /** State for an exit connection: open and ready to transmit data. */
  246. #define EXIT_CONN_STATE_OPEN 3
  247. /** State for an exit connection: waiting to be removed. */
  248. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  249. #define _EXIT_CONN_STATE_MAX 4
  250. /* the AP state values must be disjoint from the EXIT state values */
  251. #define _AP_CONN_STATE_MIN 5
  252. /** State for a SOCKS connection: waiting for SOCKS request. */
  253. #define AP_CONN_STATE_SOCKS_WAIT 5
  254. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  255. * rendezvous descriptor. */
  256. #define AP_CONN_STATE_RENDDESC_WAIT 6
  257. /** The controller will attach this connection to a circuit; it isn't our
  258. * job to do so. */
  259. #define AP_CONN_STATE_CONTROLLER_WAIT 7
  260. /** State for a SOCKS connection: waiting for a completed circuit. */
  261. #define AP_CONN_STATE_CIRCUIT_WAIT 8
  262. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  263. #define AP_CONN_STATE_CONNECT_WAIT 9
  264. /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
  265. #define AP_CONN_STATE_RESOLVE_WAIT 10
  266. /** State for a SOCKS connection: ready to send and receive. */
  267. #define AP_CONN_STATE_OPEN 11
  268. /** State for a transparent proxy connection: waiting for original
  269. * destination. */
  270. #define AP_CONN_STATE_ORIGDST_WAIT 12
  271. #define _AP_CONN_STATE_MAX 12
  272. #define _DIR_CONN_STATE_MIN 1
  273. /** State for connection to directory server: waiting for connect(). */
  274. #define DIR_CONN_STATE_CONNECTING 1
  275. /** State for connection to directory server: sending HTTP request. */
  276. #define DIR_CONN_STATE_CLIENT_SENDING 2
  277. /** State for connection to directory server: reading HTTP response. */
  278. #define DIR_CONN_STATE_CLIENT_READING 3
  279. /** State for connection to directory server: happy and finished. */
  280. #define DIR_CONN_STATE_CLIENT_FINISHED 4
  281. /** State for connection at directory server: waiting for HTTP request. */
  282. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
  283. /** State for connection at directory server: sending HTTP response. */
  284. #define DIR_CONN_STATE_SERVER_WRITING 6
  285. #define _DIR_CONN_STATE_MAX 6
  286. #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)
  287. #define _CONTROL_CONN_STATE_MIN 1
  288. #define CONTROL_CONN_STATE_OPEN_V0 1
  289. #define CONTROL_CONN_STATE_OPEN_V1 2
  290. #define CONTROL_CONN_STATE_NEEDAUTH_V0 3
  291. #define CONTROL_CONN_STATE_NEEDAUTH_V1 4
  292. #define _CONTROL_CONN_STATE_MAX 4
  293. #define _DIR_PURPOSE_MIN 1
  294. /** A connection to a directory server: download a directory. */
  295. #define DIR_PURPOSE_FETCH_DIR 1
  296. /** A connection to a directory server: download just the list
  297. * of running routers. */
  298. #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
  299. /** A connection to a directory server: download a rendezvous
  300. * descriptor. */
  301. #define DIR_PURPOSE_FETCH_RENDDESC 3
  302. /** A connection to a directory server: set after a rendezvous
  303. * descriptor is downloaded. */
  304. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
  305. /** A connection to a directory server: download one or more network-status
  306. * objects */
  307. #define DIR_PURPOSE_FETCH_NETWORKSTATUS 5
  308. /** A connection to a directory server: download one or more server
  309. * descriptors. */
  310. #define DIR_PURPOSE_FETCH_SERVERDESC 6
  311. /** A connection to a directory server: upload a server descriptor. */
  312. #define DIR_PURPOSE_UPLOAD_DIR 7
  313. /** A connection to a directory server: upload a rendezvous
  314. * descriptor. */
  315. #define DIR_PURPOSE_UPLOAD_RENDDESC 8
  316. /** Purpose for connection at a directory server. */
  317. #define DIR_PURPOSE_SERVER 9
  318. #define _DIR_PURPOSE_MAX 9
  319. #define _EXIT_PURPOSE_MIN 1
  320. /** This exit stream wants to do an ordinary connect. */
  321. #define EXIT_PURPOSE_CONNECT 1
  322. /** This exit stream wants to do a resolve (either normal or reverse). */
  323. #define EXIT_PURPOSE_RESOLVE 2
  324. #define _EXIT_PURPOSE_MAX 2
  325. /** Circuit state: I'm the origin, still haven't done all my handshakes. */
  326. #define CIRCUIT_STATE_BUILDING 0
  327. /** Circuit state: Waiting to process the onionskin. */
  328. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  329. /** Circuit state: I'd like to deliver a create, but my n_conn is still
  330. * connecting. */
  331. #define CIRCUIT_STATE_OR_WAIT 2
  332. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  333. #define CIRCUIT_STATE_OPEN 3
  334. #define _CIRCUIT_PURPOSE_MIN 1
  335. /* these circuits were initiated elsewhere */
  336. #define _CIRCUIT_PURPOSE_OR_MIN 1
  337. /** OR-side circuit purpose: normal circuit, at OR. */
  338. #define CIRCUIT_PURPOSE_OR 1
  339. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  340. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  341. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  342. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  343. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  344. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  345. #define _CIRCUIT_PURPOSE_OR_MAX 4
  346. /* these circuits originate at this node */
  347. /* here's how circ client-side purposes work:
  348. * normal circuits are C_GENERAL.
  349. * circuits that are c_introducing are either on their way to
  350. * becoming open, or they are open and waiting for a
  351. * suitable rendcirc before they send the intro.
  352. * circuits that are c_introduce_ack_wait have sent the intro,
  353. * but haven't gotten a response yet.
  354. * circuits that are c_establish_rend are either on their way
  355. * to becoming open, or they are open and have sent the
  356. * establish_rendezvous cell but haven't received an ack.
  357. * circuits that are c_rend_ready are open and have received a
  358. * rend ack, but haven't heard from bob yet. if they have a
  359. * buildstate->pending_final_cpath then they're expecting a
  360. * cell from bob, else they're not.
  361. * circuits that are c_rend_ready_intro_acked are open, and
  362. * some intro circ has sent its intro and received an ack.
  363. * circuits that are c_rend_joined are open, have heard from
  364. * bob, and are talking to him.
  365. */
  366. /** Client-side circuit purpose: Normal circuit, with cpath. */
  367. #define CIRCUIT_PURPOSE_C_GENERAL 5
  368. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  369. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  370. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point,
  371. * waiting for ACK/NAK. */
  372. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  373. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  374. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  375. /** Client-side circuit purpose: at Alice, waiting for ack. */
  376. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  377. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  378. #define CIRCUIT_PURPOSE_C_REND_READY 10
  379. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  380. * has been acknowledged. */
  381. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  382. /** Client-side circuit purpose: at Alice, rendezvous established. */
  383. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  384. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  385. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  386. /** Hidden-service-side circuit purpose: at Bob, successfully established
  387. * intro. */
  388. #define CIRCUIT_PURPOSE_S_INTRO 14
  389. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  390. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  391. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  392. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  393. /** A testing circuit; not meant to be used for actual traffic. */
  394. #define CIRCUIT_PURPOSE_TESTING 17
  395. /** A controller made this circuit and Tor should not use it. */
  396. #define CIRCUIT_PURPOSE_CONTROLLER 18
  397. #define _CIRCUIT_PURPOSE_MAX 18
  398. /** True iff the circuit purpose <b>p</b> is for a circuit that
  399. * originated at this node. */
  400. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  401. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  402. #define RELAY_COMMAND_BEGIN 1
  403. #define RELAY_COMMAND_DATA 2
  404. #define RELAY_COMMAND_END 3
  405. #define RELAY_COMMAND_CONNECTED 4
  406. #define RELAY_COMMAND_SENDME 5
  407. #define RELAY_COMMAND_EXTEND 6
  408. #define RELAY_COMMAND_EXTENDED 7
  409. #define RELAY_COMMAND_TRUNCATE 8
  410. #define RELAY_COMMAND_TRUNCATED 9
  411. #define RELAY_COMMAND_DROP 10
  412. #define RELAY_COMMAND_RESOLVE 11
  413. #define RELAY_COMMAND_RESOLVED 12
  414. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  415. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  416. #define RELAY_COMMAND_INTRODUCE1 34
  417. #define RELAY_COMMAND_INTRODUCE2 35
  418. #define RELAY_COMMAND_RENDEZVOUS1 36
  419. #define RELAY_COMMAND_RENDEZVOUS2 37
  420. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  421. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  422. #define RELAY_COMMAND_INTRODUCE_ACK 40
  423. #define END_STREAM_REASON_MISC 1
  424. #define END_STREAM_REASON_RESOLVEFAILED 2
  425. #define END_STREAM_REASON_CONNECTREFUSED 3
  426. #define END_STREAM_REASON_EXITPOLICY 4
  427. #define END_STREAM_REASON_DESTROY 5
  428. #define END_STREAM_REASON_DONE 6
  429. #define END_STREAM_REASON_TIMEOUT 7
  430. /* 8 is unallocated. */
  431. #define END_STREAM_REASON_HIBERNATING 9
  432. #define END_STREAM_REASON_INTERNAL 10
  433. #define END_STREAM_REASON_RESOURCELIMIT 11
  434. #define END_STREAM_REASON_CONNRESET 12
  435. #define END_STREAM_REASON_TORPROTOCOL 13
  436. /* These high-numbered end reasons are not part of the official spec,
  437. * and are not intended to be put in relay end cells. They are here
  438. * to be more informative when sending back socks replies to the
  439. * application. */
  440. #define END_STREAM_REASON_ALREADY_SOCKS_REPLIED 256
  441. #define END_STREAM_REASON_CANT_ATTACH 257
  442. #define END_STREAM_REASON_NET_UNREACHABLE 258
  443. #define RESOLVED_TYPE_HOSTNAME 0
  444. #define RESOLVED_TYPE_IPV4 4
  445. #define RESOLVED_TYPE_IPV6 6
  446. #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
  447. #define RESOLVED_TYPE_ERROR 0xF1
  448. /* XXX We should document the meaning of these. */
  449. #define END_CIRC_AT_ORIGIN -1
  450. #define _END_CIRC_REASON_MIN 0
  451. #define END_CIRC_REASON_NONE 0
  452. #define END_CIRC_REASON_TORPROTOCOL 1
  453. #define END_CIRC_REASON_INTERNAL 2
  454. #define END_CIRC_REASON_REQUESTED 3
  455. #define END_CIRC_REASON_HIBERNATING 4
  456. #define END_CIRC_REASON_RESOURCELIMIT 5
  457. #define END_CIRC_REASON_CONNECTFAILED 6
  458. #define END_CIRC_REASON_OR_IDENTITY 7
  459. #define END_CIRC_REASON_OR_CONN_CLOSED 8
  460. #define _END_CIRC_REASON_MAX 8
  461. /** Length of 'y' portion of 'y.onion' URL. */
  462. #define REND_SERVICE_ID_LEN 16
  463. #define CELL_DIRECTION_IN 1
  464. #define CELL_DIRECTION_OUT 2
  465. #ifdef TOR_PERF
  466. #define CIRCWINDOW_START 10000
  467. #define CIRCWINDOW_INCREMENT 1000
  468. #define STREAMWINDOW_START 5000
  469. #define STREAMWINDOW_INCREMENT 500
  470. #else
  471. #define CIRCWINDOW_START 1000
  472. #define CIRCWINDOW_INCREMENT 100
  473. #define STREAMWINDOW_START 500
  474. #define STREAMWINDOW_INCREMENT 50
  475. #endif
  476. /* cell commands */
  477. #define CELL_PADDING 0
  478. #define CELL_CREATE 1
  479. #define CELL_CREATED 2
  480. #define CELL_RELAY 3
  481. #define CELL_DESTROY 4
  482. #define CELL_CREATE_FAST 5
  483. #define CELL_CREATED_FAST 6
  484. /** How long to test reachability before complaining to the user. */
  485. #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
  486. /* legal characters in a nickname */
  487. #define LEGAL_NICKNAME_CHARACTERS \
  488. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  489. /** Name to use in client TLS certificates if no nickname is given.*/
  490. #define DEFAULT_CLIENT_NICKNAME "client"
  491. #define SOCKS4_NETWORK_LEN 8
  492. typedef enum {
  493. SOCKS5_SUCCEEDED = 0x00,
  494. SOCKS5_GENERAL_ERROR = 0x01,
  495. SOCKS5_NOT_ALLOWED = 0x02,
  496. SOCKS5_NET_UNREACHABLE = 0x03,
  497. SOCKS5_HOST_UNREACHABLE = 0x04,
  498. SOCKS5_CONNECTION_REFUSED = 0x05,
  499. SOCKS5_TTL_EXPIRED = 0x06,
  500. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  501. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  502. } socks5_reply_status_t;
  503. /*
  504. * Relay payload:
  505. * Relay command [1 byte]
  506. * Recognized [2 bytes]
  507. * Stream ID [2 bytes]
  508. * Partial SHA-1 [4 bytes]
  509. * Length [2 bytes]
  510. * Relay payload [498 bytes]
  511. */
  512. #define CELL_PAYLOAD_SIZE 509
  513. #define CELL_NETWORK_SIZE 512
  514. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  515. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  516. /** Parsed onion routing cell. All communication between nodes
  517. * is via cells. */
  518. typedef struct {
  519. uint16_t circ_id; /**< Circuit which received the cell. */
  520. uint8_t command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  521. * or DESTROY. */
  522. char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  523. } cell_t;
  524. /** Beginning of a RELAY cell payload. */
  525. typedef struct {
  526. uint8_t command; /**< The end-to-end relay command. */
  527. uint16_t recognized; /**< Used to tell whether cell is for us. */
  528. uint16_t stream_id; /**< Which stream is this cell associated with? */
  529. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  530. uint16_t length; /**< How long is the payload body? */
  531. } relay_header_t;
  532. typedef struct buf_t buf_t;
  533. typedef struct socks_request_t socks_request_t;
  534. #define BASE_CONNECTION_MAGIC 0x7C3C304Eu
  535. #define OR_CONNECTION_MAGIC 0x7D31FF03u
  536. #define EDGE_CONNECTION_MAGIC 0xF0374013u
  537. #define DIR_CONNECTION_MAGIC 0x9988ffeeu
  538. #define CONTROL_CONNECTION_MAGIC 0x8abc765du
  539. /** Description of a connection to another host or process, and associated
  540. * data.
  541. *
  542. * A connection is named based on what it's connected to -- an "OR
  543. * connection" has a Tor node on the other end, an "exit
  544. * connection" has a website or other server on the other end, and an
  545. * "AP connection" has an application proxy (and thus a user) on the
  546. * other end.
  547. *
  548. * Every connection has a type and a state. Connections never change
  549. * their type, but can go through many state changes in their lifetime.
  550. *
  551. * Every connection has two associated input and output buffers.
  552. * Listeners don't use them. For non-listener connections, incoming
  553. * data is appended to conn->inbuf, and outgoing data is taken from
  554. * conn->outbuf. Connections differ primarily in the functions called
  555. * to fill and drain these buffers.
  556. */
  557. typedef struct connection_t {
  558. uint32_t magic; /**< For memory debugging: must equal one of
  559. * *_CONNECTION_MAGIC. */
  560. uint8_t type; /**< What kind of connection is this? */
  561. uint8_t state; /**< Current state of this connection. */
  562. uint8_t purpose; /**< Only used for DIR and EXIT types currently. */
  563. /* The next fields are all one-bit booleans. Some are only applicable
  564. * to connection subtypes, but we hold them here anyway, to save space.
  565. * (Currently, they all fit into a single byte.) */
  566. unsigned wants_to_read:1; /**< Boolean: should we start reading again once
  567. * the bandwidth throttler allows it? */
  568. unsigned wants_to_write:1; /**< Boolean: should we start writing again once
  569. * the bandwidth throttler allows reads? */
  570. unsigned hold_open_until_flushed:1; /**< Despite this connection's being
  571. * marked for close, do we flush it
  572. * before closing it? */
  573. unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
  574. * conn? */
  575. unsigned edge_has_sent_end:1; /**< For debugging; only used on edge
  576. * connections. Set once we've set the stream end,
  577. * and check in circuit_about_to_close_connection(). */
  578. /** For control connections only. If set, we send extended info with control
  579. * events as appropriate. */
  580. unsigned int control_events_are_extended:1;
  581. /** Used for OR conns that shouldn't get any new circs attached to them. */
  582. unsigned int or_is_obsolete:1;
  583. /** For AP connections only. If 1, and we fail to reach the chosen exit,
  584. * stop requiring it. */
  585. unsigned int chosen_exit_optional:1;
  586. int s; /**< Our socket; -1 if this connection is closed. */
  587. int conn_array_index; /**< Index into the global connection array. */
  588. struct event *read_event; /**< Libevent event structure. */
  589. struct event *write_event; /**< Libevent event structure. */
  590. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  591. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  592. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  593. * outbuf? */
  594. time_t timestamp_lastread; /**< When was the last time libevent said we could
  595. * read? */
  596. time_t timestamp_lastwritten; /**< When was the last time libevent said we
  597. * could write? */
  598. time_t timestamp_created; /**< When was this connection_t created? */
  599. uint32_t addr; /**< IP of the other side of the connection; used to identify
  600. * routers, along with port. */
  601. uint16_t port; /**< If non-zero, port on the other end
  602. * of the connection. */
  603. uint16_t marked_for_close; /**< Should we close this conn on the next
  604. * iteration of the main loop? (If true, holds
  605. * the line number where this connection was
  606. * marked.) */
  607. const char *marked_for_close_file; /**< For debugging: in which file were
  608. * we marked for close? */
  609. char *address; /**< FQDN (or IP) of the guy on the other end.
  610. * strdup into this, because free_connection frees it. */
  611. } connection_t;
  612. /** Subtype of connection_t for an "OR connection" -- that is, one that speaks
  613. * cells over TLS. */
  614. typedef struct or_connection_t {
  615. connection_t _base;
  616. char identity_digest[DIGEST_LEN]; /**< Hash of the public RSA key for
  617. * the other side's signing key. */
  618. char *nickname; /**< Nickname of OR on other side (if any). */
  619. tor_tls_t *tls; /**< TLS connection state */
  620. time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
  621. /* bandwidth* and receiver_bucket only used by ORs in OPEN state: */
  622. int bandwidthrate; /**< Bytes/s added to the bucket. (OPEN ORs only.) */
  623. int bandwidthburst; /**< Max bucket size for this conn. (OPEN ORs only.) */
  624. int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
  625. * add 'bandwidthrate' to this, capping it at
  626. * bandwidthburst. (OPEN ORs only) */
  627. circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
  628. * connection, which half of the space should
  629. * we use? */
  630. int n_circuits; /**< How many circuits use this connection as p_conn or
  631. * n_conn ? */
  632. struct or_connection_t *next_with_same_id; /**< Next connection with same
  633. * identity digest as this one. */
  634. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  635. * this connection? This is always in the
  636. * range 0..1<<15-1. */
  637. } or_connection_t;
  638. /** Subtype of connection_t for an "edge connection" -- that is, a socks (ap)
  639. * connection, or an exit. */
  640. typedef struct edge_connection_t {
  641. connection_t _base;
  642. struct edge_connection_t *next_stream; /**< Points to the next stream at this
  643. * edge, if any */
  644. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  645. * this conn exits at. */
  646. int package_window; /**< How many more relay cells can I send into the
  647. * circuit? */
  648. int deliver_window; /**< How many more relay cells can end at me? */
  649. /** Nickname of planned exit node -- used with .exit support. */
  650. char *chosen_exit_name;
  651. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  652. * only.) */
  653. struct circuit_t *on_circuit; /**< The circuit (if any) that this edge
  654. * connection is using. */
  655. uint32_t address_ttl; /**< TTL for address-to-addr mapping on exit
  656. * connection. Exit connections only. */
  657. uint16_t stream_id; /**< The stream ID used for this edge connection on its
  658. * circuit */
  659. /** Quasi-global identifier for this connection; used for control.c */
  660. /* XXXX NM This can get re-used after 2**32 streams */
  661. uint32_t global_identifier;
  662. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  663. * querying for? (AP only) */
  664. /** Number of times we've reassigned this application connection to
  665. * a new circuit. We keep track because the timeout is longer if we've
  666. * already retried several times. */
  667. uint8_t num_socks_retries;
  668. } edge_connection_t;
  669. /** Subtype of connection_t for an "directory connection" -- that is, an HTTP
  670. * connection to retrieve or serve directory material. */
  671. typedef struct dir_connection_t {
  672. connection_t _base;
  673. char *requested_resource; /**< Which 'resource' did we ask the directory
  674. * for? */
  675. unsigned int dirconn_direct:1; /**< Is this dirconn direct, or via Tor? */
  676. /* Used only for server sides of some dir connections, to implement
  677. * "spooling" of directory material to the outbuf. Otherwise, we'd have
  678. * to append everything to the outbuf in one enormous chunk. */
  679. enum {
  680. DIR_SPOOL_NONE=0, DIR_SPOOL_SERVER_BY_DIGEST, DIR_SPOOL_SERVER_BY_FP,
  681. DIR_SPOOL_CACHED_DIR, DIR_SPOOL_NETWORKSTATUS
  682. } dir_spool_src;
  683. smartlist_t *fingerprint_stack;
  684. struct cached_dir_t *cached_dir;
  685. off_t cached_dir_offset;
  686. tor_zlib_state_t *zlib_state;
  687. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  688. * querying for? */
  689. char identity_digest[DIGEST_LEN]; /**< Hash of the public RSA key for
  690. * the directory server's signing key. */
  691. } dir_connection_t;
  692. /** Subtype of connection_t for an connection to a controller. */
  693. typedef struct control_connection_t {
  694. connection_t _base;
  695. uint32_t event_mask; /**< Bitfield: which events does this controller
  696. * care about? */
  697. uint32_t incoming_cmd_len;
  698. uint32_t incoming_cmd_cur_len;
  699. char *incoming_cmd;
  700. /* Used only by control v0 connections */
  701. uint16_t incoming_cmd_type;
  702. } control_connection_t;
  703. /** Cast a connection_t subtype pointer to a connection_t **/
  704. #define TO_CONN(c) &(((c)->_base))
  705. /** Helper macro: Given a pointer to to._base, of type from*, return &to. */
  706. #define DOWNCAST(to, ptr) \
  707. (to*) (((char*)(ptr)) - STRUCT_OFFSET(to, _base))
  708. /** Convert a connection_t* to an or_connection_t*; assert if the cast is
  709. * invalid. */
  710. static or_connection_t *TO_OR_CONN(connection_t *);
  711. /** Convert a connection_t* to a dir_connection_t*; assert if the cast is
  712. * invalid. */
  713. static dir_connection_t *TO_DIR_CONN(connection_t *);
  714. /** Convert a connection_t* to an edge_connection_t*; assert if the cast is
  715. * invalid. */
  716. static edge_connection_t *TO_EDGE_CONN(connection_t *);
  717. /** Convert a connection_t* to an control_connection_t*; assert if the cast is
  718. * invalid. */
  719. static control_connection_t *TO_CONTROL_CONN(connection_t *);
  720. static INLINE or_connection_t *TO_OR_CONN(connection_t *c)
  721. {
  722. tor_assert(c->magic == OR_CONNECTION_MAGIC);
  723. return DOWNCAST(or_connection_t, c);
  724. }
  725. static INLINE dir_connection_t *TO_DIR_CONN(connection_t *c)
  726. {
  727. tor_assert(c->magic == DIR_CONNECTION_MAGIC);
  728. return DOWNCAST(dir_connection_t, c);
  729. }
  730. static INLINE edge_connection_t *TO_EDGE_CONN(connection_t *c)
  731. {
  732. tor_assert(c->magic == EDGE_CONNECTION_MAGIC);
  733. return DOWNCAST(edge_connection_t, c);
  734. }
  735. static INLINE control_connection_t *TO_CONTROL_CONN(connection_t *c)
  736. {
  737. tor_assert(c->magic == CONTROL_CONNECTION_MAGIC);
  738. return DOWNCAST(control_connection_t, c);
  739. }
  740. typedef enum {
  741. ADDR_POLICY_ACCEPT=1,
  742. ADDR_POLICY_REJECT=2,
  743. } addr_policy_action_t;
  744. /** A linked list of policy rules */
  745. typedef struct addr_policy_t {
  746. addr_policy_action_t policy_type; /**< What to do when the policy matches.*/
  747. char *string; /**< String representation of this rule. */
  748. uint32_t addr; /**< Base address to accept or reject. */
  749. uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
  750. * a &amp; msk == <b>addr</b> &amp; msk . */
  751. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  752. uint16_t prt_max; /**< Highest port number to accept/reject. */
  753. struct addr_policy_t *next; /**< Next rule in list. */
  754. } addr_policy_t;
  755. /** A cached_dir_t represents a cacheable directory object, along with its
  756. * compressed form. */
  757. typedef struct cached_dir_t {
  758. char *dir; /**< Contents of this object */
  759. char *dir_z; /**< Compressed contents of this object. */
  760. size_t dir_len; /**< Length of <b>dir</b> */
  761. size_t dir_z_len; /**< Length of <b>dir_z</b> */
  762. time_t published; /**< When was this object published */
  763. int refcnt; /**< Reference count for this cached_dir_t. */
  764. } cached_dir_t;
  765. typedef enum {
  766. SAVED_NOWHERE=0, SAVED_IN_CACHE, SAVED_IN_JOURNAL
  767. } saved_location_t;
  768. /** Information need to cache an onion router's descriptor. */
  769. typedef struct signed_descriptor_t {
  770. char *signed_descriptor_body;
  771. size_t signed_descriptor_len;
  772. char signed_descriptor_digest[DIGEST_LEN];
  773. char identity_digest[DIGEST_LEN];
  774. time_t published_on;
  775. saved_location_t saved_location;
  776. off_t saved_offset;
  777. } signed_descriptor_t;
  778. /** Information about another onion router in the network. */
  779. typedef struct {
  780. signed_descriptor_t cache_info;
  781. char *address; /**< Location of OR: either a hostname or an IP address. */
  782. char *nickname; /**< Human-readable OR name. */
  783. uint32_t addr; /**< IPv4 address of OR, in host order. */
  784. uint16_t or_port; /**< Port for TLS connections. */
  785. uint16_t dir_port; /**< Port for HTTP directory connections. */
  786. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  787. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  788. char *platform; /**< What software/operating system is this OR using? */
  789. /* link info */
  790. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  791. * bucket per second? */
  792. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  793. /** How many bytes/s is this router known to handle? */
  794. uint32_t bandwidthcapacity;
  795. addr_policy_t *exit_policy; /**< What streams will this OR permit
  796. * to exit? */
  797. long uptime; /**< How many seconds the router claims to have been up */
  798. smartlist_t *declared_family; /**< Nicknames of router which this router
  799. * claims are its family. */
  800. char *contact_info; /**< Declared contact info for this router. */
  801. unsigned int is_hibernating:1; /**< Whether the router claims to be
  802. * hibernating */
  803. unsigned int has_old_dnsworkers:1; /**< Whether the router is using
  804. * dnsworker code. */
  805. /* local info */
  806. unsigned int is_running:1; /**< As far as we know, is this OR currently
  807. * running? */
  808. unsigned int is_valid:1; /**< Has a trusted dirserver validated this OR?
  809. * (For Authdir: Have we validated this OR?)
  810. */
  811. unsigned int is_named:1; /**< Do we believe the nickname that this OR gives
  812. * us? */
  813. unsigned int is_fast:1; /** Do we think this is a fast OR? */
  814. unsigned int is_stable:1; /** Do we think this is a stable OR? */
  815. unsigned int is_possible_guard:1; /**< Do we think this is an OK guard? */
  816. /** Tor can use this desc for circuit-building. */
  817. #define ROUTER_PURPOSE_GENERAL 0
  818. /** Tor should avoid using this desc for circuit-building. */
  819. #define ROUTER_PURPOSE_CONTROLLER 1
  820. uint8_t purpose; /** Should Tor use this desc for circuit-building? */
  821. /* The below items are used only by authdirservers for
  822. * reachability testing. */
  823. /** When was the last time we could reach this OR? */
  824. time_t last_reachable;
  825. /** When did we start testing reachability for this OR? */
  826. time_t testing_since;
  827. /** How many times has a descriptor been posted and we believed
  828. * this router to be unreachable? We only actually warn on the third. */
  829. int num_unreachable_notifications;
  830. /** What position is this descriptor within routerlist->routers? -1 for
  831. * none. */
  832. int routerlist_index;
  833. } routerinfo_t;
  834. /** Contents of a single router entry in a network status object.
  835. */
  836. typedef struct routerstatus_t {
  837. time_t published_on; /**< When was this router published? */
  838. char nickname[MAX_NICKNAME_LEN+1]; /**< The nickname this router says it
  839. * has. */
  840. char identity_digest[DIGEST_LEN]; /**< Digest of the router's identity
  841. * key. */
  842. char descriptor_digest[DIGEST_LEN]; /**< Digest of the router's most recent
  843. * descriptor. */
  844. uint32_t addr; /**< IPv4 address for this router. */
  845. uint16_t or_port; /**< OR port for this router. */
  846. uint16_t dir_port; /**< Directory port for this router. */
  847. unsigned int is_exit:1; /**< True iff this router is a good exit. */
  848. unsigned int is_stable:1; /**< True iff this router stays up a long time. */
  849. unsigned int is_fast:1; /**< True iff this router has good bandwidth. */
  850. unsigned int is_running:1; /**< True iff this router is up. */
  851. unsigned int is_named:1; /**< True iff "nickname" belongs to this router. */
  852. unsigned int is_valid:1; /**< True iff this router is validated. */
  853. unsigned int is_v2_dir:1; /**< True iff this router can serve directory
  854. * information with v2 of the directory
  855. * protocol. (All directory caches cache v1
  856. * directories.) */
  857. unsigned int is_possible_guard:1; /**< True iff this router would be a good
  858. * choice as an entry guard. */
  859. /** True if we, as a directory mirror, want to download the corresponding
  860. * routerinfo from the authority who gave us this routerstatus. (That is,
  861. * if we don't have the routerinfo, and if we haven't already tried to get it
  862. * from this authority.)
  863. */
  864. unsigned int need_to_mirror:1;
  865. } routerstatus_t;
  866. /** Our "local" or combined view of the info from all networkstatus objects
  867. * about a single router. */
  868. typedef struct local_routerstatus_t {
  869. /** What do we believe to be the case about this router? In this field,
  870. * descriptor_digest represents the descriptor we would most like to use for
  871. * this router. */
  872. routerstatus_t status;
  873. time_t next_attempt_at; /**< When should we try this descriptor again? */
  874. uint8_t n_download_failures; /**< Number of failures trying to download the
  875. * most recent descriptor. */
  876. unsigned int name_lookup_warned:1; /**< Have we warned the user for referring
  877. * to this (unnamed) router by nickname?
  878. */
  879. } local_routerstatus_t;
  880. /** How many times will we try to download a router's descriptor before giving
  881. * up? */
  882. #define MAX_ROUTERDESC_DOWNLOAD_FAILURES 8
  883. /** Contents of a (v2 or later) network status object. */
  884. typedef struct networkstatus_t {
  885. /** When did we receive the network-status document? */
  886. time_t received_on;
  887. /** What was the digest of the document? */
  888. char networkstatus_digest[DIGEST_LEN];
  889. unsigned int is_recent; /**< Is this recent enough to influence running
  890. * status? */
  891. /* These fields come from the actual network-status document.*/
  892. time_t published_on; /**< Declared publication date. */
  893. char *source_address; /**< Canonical directory server hostname. */
  894. uint32_t source_addr; /**< Canonical directory server IP. */
  895. uint16_t source_dirport; /**< Canonical directory server dirport. */
  896. char identity_digest[DIGEST_LEN]; /**< Digest of signing key. */
  897. char *contact; /**< How to contact directory admin? (may be NULL). */
  898. crypto_pk_env_t *signing_key; /**< Key used to sign this directory. */
  899. char *client_versions; /**< comma-separated list of recommended client
  900. * versions. */
  901. char *server_versions; /**< comma-separated list of recommended server
  902. * versions. */
  903. unsigned int binds_names:1; /**< True iff this directory server binds
  904. * names. */
  905. unsigned int recommends_versions:1; /**< True iff this directory server
  906. * recommends client and server software
  907. * versions. */
  908. smartlist_t *entries; /**< List of routerstatus_t*. This list is kept
  909. * sorted by identity_digest. */
  910. } networkstatus_t;
  911. /** Contents of a directory of onion routers. */
  912. typedef struct {
  913. /** Map from server identity digest to a member of routers. */
  914. digestmap_t *identity_map;
  915. /** Map from server descriptor digest to a signed_descriptor_t from
  916. * routers or old_routers. */
  917. digestmap_t *desc_digest_map;
  918. /** List of routerinfo_t for all currently live routers we know. */
  919. smartlist_t *routers;
  920. /** List of signed_descriptor_t for older router descriptors we're
  921. * caching. */
  922. smartlist_t *old_routers;
  923. /** DOCDOC */
  924. tor_mmap_t *mmap_descriptors;
  925. } routerlist_t;
  926. /** Information on router used when extending a circuit. (We don't need a
  927. * full routerinfo_t to extend: we only need addr:port:keyid to build an OR
  928. * connection, and onion_key to create the onionskin.) */
  929. typedef struct extend_info_t {
  930. char nickname[MAX_HEX_NICKNAME_LEN+1]; /**< This router's nickname for
  931. * display. */
  932. char identity_digest[DIGEST_LEN]; /**< Hash of this router's identity key. */
  933. uint32_t addr; /**< IP address in host order. */
  934. uint16_t port; /**< OR port. */
  935. crypto_pk_env_t *onion_key; /**< Current onionskin key. */
  936. } extend_info_t;
  937. #define CRYPT_PATH_MAGIC 0x70127012u
  938. /** Holds accounting information for a single step in the layered encryption
  939. * performed by a circuit. Used only at the client edge of a circuit. */
  940. typedef struct crypt_path_t {
  941. uint32_t magic;
  942. /* crypto environments */
  943. /** Encryption key and counter for cells heading towards the OR at this
  944. * step. */
  945. crypto_cipher_env_t *f_crypto;
  946. /** Encryption key and counter for cells heading back from the OR at this
  947. * step. */
  948. crypto_cipher_env_t *b_crypto;
  949. /** Digest state for cells heading towards the OR at this step. */
  950. crypto_digest_env_t *f_digest; /* for integrity checking */
  951. /** Digest state for cells heading away from the OR at this step. */
  952. crypto_digest_env_t *b_digest;
  953. /** Current state of Diffie-Hellman key negotiation with the OR at this
  954. * step. */
  955. crypto_dh_env_t *dh_handshake_state;
  956. /** Current state of 'fast' (non-PK) key negotiation with the OR at this
  957. * step. Used to save CPU when TLS is already providing all the
  958. * authentication, secrecy, and integrity we need, and we're already
  959. * distinguishable from an OR.
  960. */
  961. char fast_handshake_state[DIGEST_LEN];
  962. /** Negotiated key material shared with the OR at this step. */
  963. char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
  964. /** Information to extend to the OR at this step. */
  965. extend_info_t *extend_info;
  966. /** Is the circuit built to this step? Must be one of:
  967. * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
  968. * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
  969. * and not received an EXTENDED/CREATED)
  970. * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
  971. uint8_t state;
  972. #define CPATH_STATE_CLOSED 0
  973. #define CPATH_STATE_AWAITING_KEYS 1
  974. #define CPATH_STATE_OPEN 2
  975. struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
  976. * (The list is circular, so the last node
  977. * links to the first.) */
  978. struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
  979. * circuit. */
  980. int package_window; /**< How many bytes are we allowed to originate ending
  981. * at this step? */
  982. int deliver_window; /**< How many bytes are we willing to deliver originating
  983. * at this step? */
  984. } crypt_path_t;
  985. #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
  986. #define DH_KEY_LEN DH_BYTES
  987. #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
  988. CIPHER_KEY_LEN+\
  989. DH_KEY_LEN)
  990. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
  991. #define REND_COOKIE_LEN DIGEST_LEN
  992. /** Information used to build a circuit. */
  993. typedef struct {
  994. /** Intended length of the final circuit. */
  995. int desired_path_len;
  996. /** How to extend to the planned exit node. */
  997. extend_info_t *chosen_exit;
  998. /** Whether every node in the circ must have adequate uptime. */
  999. int need_uptime;
  1000. /** Whether every node in the circ must have adequate capacity. */
  1001. int need_capacity;
  1002. /** Whether the last hop was picked with exiting in mind. */
  1003. int is_internal;
  1004. /** The crypt_path_t to append after rendezvous: used for rendezvous. */
  1005. crypt_path_t *pending_final_cpath;
  1006. /** How many times has building a circuit for this task failed? */
  1007. int failure_count;
  1008. /** At what time should we give up on this task? */
  1009. time_t expiry_time;
  1010. } cpath_build_state_t;
  1011. #define ORIGIN_CIRCUIT_MAGIC 0x35315243u
  1012. #define OR_CIRCUIT_MAGIC 0x98ABC04Fu
  1013. typedef uint16_t circid_t;
  1014. /**
  1015. * A circuit is a path over the onion routing
  1016. * network. Applications can connect to one end of the circuit, and can
  1017. * create exit connections at the other end of the circuit. AP and exit
  1018. * connections have only one circuit associated with them (and thus these
  1019. * connection types are closed when the circuit is closed), whereas
  1020. * OR connections multiplex many circuits at once, and stay standing even
  1021. * when there are no circuits running over them.
  1022. *
  1023. * A circuit_t structure cann fill one of two roles. First, a or_circuit_t
  1024. * links two connections together: either an edge connection and an OR
  1025. * connection, or two OR connections. (When joined to an OR connection, a
  1026. * circuit_t affects only cells sent to a particular circID on that
  1027. * connection. When joined to an edge connection, a circuit_t affects all
  1028. * data.)
  1029. * Second, an origin_circuit_t holds the cipher keys and state for sending data
  1030. * along a given circuit. At the OP, it has a sequence of ciphers, each
  1031. * of which is shared with a single OR along the circuit. Separate
  1032. * ciphers are used for data going "forward" (away from the OP) and
  1033. * "backward" (towards the OP). At the OR, a circuit has only two stream
  1034. * ciphers: one for data going forward, and one for data going backward.
  1035. */
  1036. typedef struct circuit_t {
  1037. uint32_t magic; /**< For memory and type debugging: must equal
  1038. * ORIGIN_CIRCUIT_MAGIC or OR_CIRCUIT_MAGIC. */
  1039. /** The OR connection that is next in this circuit. */
  1040. or_connection_t *n_conn;
  1041. /** The identity hash of n_conn. */
  1042. char n_conn_id_digest[DIGEST_LEN];
  1043. /** The circuit_id used in the next (forward) hop of this circuit. */
  1044. uint16_t n_circ_id;
  1045. /** The port for the OR that is next in this circuit. */
  1046. uint16_t n_port;
  1047. /** The IPv4 address of the OR that is next in this circuit. */
  1048. uint32_t n_addr;
  1049. /** How many relay data cells can we package (read from edge streams)
  1050. * on this circuit before we receive a circuit-level sendme cell asking
  1051. * for more? */
  1052. int package_window;
  1053. /** How many relay data cells will we deliver (write to edge streams)
  1054. * on this circuit? When deliver_window gets low, we send some
  1055. * circuit-level sendme cells to indicate that we're willing to accept
  1056. * more. */
  1057. int deliver_window;
  1058. /** For storage while passing to cpuworker (state
  1059. * CIRCUIT_STATE_ONIONSKIN_PENDING), or while n_conn is pending
  1060. * (state CIRCUIT_STATE_OR_WAIT). When defined, it is always
  1061. * length ONIONSKIN_CHALLENGE_LEN. */
  1062. char *onionskin;
  1063. time_t timestamp_created; /**< When was this circuit created? */
  1064. time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
  1065. * circuit is clean. */
  1066. uint8_t state; /**< Current status of this circuit. */
  1067. uint8_t purpose; /**< Why are we creating this circuit? */
  1068. uint16_t marked_for_close; /**< Should we close this circuit at the end of
  1069. * the main loop? (If true, holds the line number
  1070. * where this circuit was marked.) */
  1071. const char *marked_for_close_file; /**< For debugging: in which file was this
  1072. * circuit marked for close? */
  1073. struct circuit_t *next; /**< Next circuit in linked list. */
  1074. } circuit_t;
  1075. /** An origin_circuit_t holds data necessary to build and use a circuit.
  1076. */
  1077. typedef struct origin_circuit_t {
  1078. circuit_t _base;
  1079. /** Linked list of AP streams (or EXIT streams if hidden service)
  1080. * associated with this circuit. */
  1081. edge_connection_t *p_streams;
  1082. /** Build state for this circuit. It includes the intended path
  1083. * length, the chosen exit router, rendezvous information, etc.
  1084. */
  1085. cpath_build_state_t *build_state;
  1086. /** The doubly-linked list of crypt_path_t entries, one per hop,
  1087. * for this circuit. This includes ciphers for each hop,
  1088. * integrity-checking digests for each hop, and package/delivery
  1089. * windows for each hop.
  1090. *
  1091. * The cpath field is defined only when we are the circuit's origin.
  1092. */
  1093. crypt_path_t *cpath;
  1094. /** The rend_pk_digest field holds a hash of location-hidden service's
  1095. * PK if purpose is S_ESTABLISH_INTRO or S_RENDEZVOUSING.
  1096. */
  1097. char rend_pk_digest[DIGEST_LEN];
  1098. /** Holds rendezvous cookie if purpose is C_ESTABLISH_REND. Filled with
  1099. * zeroes otherwise.
  1100. */
  1101. char rend_cookie[REND_COOKIE_LEN];
  1102. /**
  1103. * The rend_query field holds the y portion of y.onion (nul-terminated)
  1104. * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
  1105. * for a hidden service, or is S_*.
  1106. */
  1107. char rend_query[REND_SERVICE_ID_LEN+1];
  1108. /** The next stream_id that will be tried when we're attempting to
  1109. * construct a new AP stream originating at this circuit. */
  1110. uint16_t next_stream_id;
  1111. /** Quasi-global identifier for this circuit; used for control.c */
  1112. /* XXXX NM This can get re-used after 2**32 circuits. */
  1113. uint32_t global_identifier;
  1114. } origin_circuit_t;
  1115. /** An or_circuit_t holds information needed to implement a circuit at an
  1116. * OR. */
  1117. typedef struct or_circuit_t {
  1118. circuit_t _base;
  1119. /** The circuit_id used in the previous (backward) hop of this circuit. */
  1120. circid_t p_circ_id;
  1121. /** The OR connection that is previous in this circuit. */
  1122. or_connection_t *p_conn;
  1123. /** Linked list of Exit streams associated with this circuit. */
  1124. edge_connection_t *n_streams;
  1125. /** Linked list of Exit streams associated with this circuit that are
  1126. * still being resolved. */
  1127. edge_connection_t *resolving_streams;
  1128. /** The cipher used by intermediate hops for cells heading toward the
  1129. * OP. */
  1130. crypto_cipher_env_t *p_crypto;
  1131. /** The cipher used by intermediate hops for cells heading away from
  1132. * the OP. */
  1133. crypto_cipher_env_t *n_crypto;
  1134. /** The integrity-checking digest used by intermediate hops, for
  1135. * cells packaged here and heading towards the OP.
  1136. */
  1137. crypto_digest_env_t *p_digest;
  1138. /** The integrity-checking digest used by intermediate hops, for
  1139. * cells packaged at the OP and arriving here.
  1140. */
  1141. crypto_digest_env_t *n_digest;
  1142. /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  1143. * is not marked for close. */
  1144. struct or_circuit_t *rend_splice;
  1145. #if REND_COOKIE_LEN >= DIGEST_LEN
  1146. #define REND_TOKEN_LEN REND_COOKIE_LEN
  1147. #else
  1148. #define REND_TOKEN_LEN DIGEST_LEN
  1149. #endif
  1150. /** A hash of location-hidden service's PK if purpose is INTRO_POINT, or a
  1151. * rendezvous cookie if purpose is REND_POINT_WAITING. Filled with zeroes
  1152. * otherwise.
  1153. */
  1154. char rend_token[REND_TOKEN_LEN];
  1155. char handshake_digest[DIGEST_LEN]; /**< Stores KH for the handshake. */
  1156. } or_circuit_t;
  1157. /** Convert a circuit subtype to a circuit_t.*/
  1158. #define TO_CIRCUIT(x) (&((x)->_base))
  1159. /** Convert a circuit_t* to a pointer to the enclosing or_circuit_t. Asserts
  1160. * if the cast is impossible. */
  1161. static or_circuit_t *TO_OR_CIRCUIT(circuit_t *);
  1162. /** Convert a circuit_t* to a pointer to the enclosing origin_circuit_t.
  1163. * Asserts if the cast is impossible. */
  1164. static origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *);
  1165. static INLINE or_circuit_t *TO_OR_CIRCUIT(circuit_t *x)
  1166. {
  1167. tor_assert(x->magic == OR_CIRCUIT_MAGIC);
  1168. //return (or_circuit_t*) (((char*)x) - STRUCT_OFFSET(or_circuit_t, _base));
  1169. return DOWNCAST(or_circuit_t, x);
  1170. }
  1171. static INLINE origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *x)
  1172. {
  1173. tor_assert(x->magic == ORIGIN_CIRCUIT_MAGIC);
  1174. //return (origin_circuit_t*)
  1175. // (((char*)x) - STRUCT_OFFSET(origin_circuit_t, _base));
  1176. return DOWNCAST(origin_circuit_t, x);
  1177. }
  1178. #define ALLOW_INVALID_ENTRY 1
  1179. #define ALLOW_INVALID_EXIT 2
  1180. #define ALLOW_INVALID_MIDDLE 4
  1181. #define ALLOW_INVALID_RENDEZVOUS 8
  1182. #define ALLOW_INVALID_INTRODUCTION 16
  1183. /** An entry specifying a set of addresses and ports that should be remapped
  1184. * to another address and port before exiting this exit node. */
  1185. typedef struct exit_redirect_t {
  1186. uint32_t addr;
  1187. uint32_t mask;
  1188. uint16_t port_min;
  1189. uint16_t port_max;
  1190. uint32_t addr_dest;
  1191. uint16_t port_dest;
  1192. unsigned is_redirect:1;
  1193. } exit_redirect_t;
  1194. /** A linked list of lines in a config file. */
  1195. typedef struct config_line_t {
  1196. char *key;
  1197. char *value;
  1198. struct config_line_t *next;
  1199. } config_line_t;
  1200. /** Configuration options for a Tor process. */
  1201. typedef struct {
  1202. uint32_t _magic;
  1203. /** What should the tor process actually do? */
  1204. enum {
  1205. CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
  1206. CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS
  1207. } command;
  1208. const char *command_arg; /**< Argument for command-line option. */
  1209. config_line_t *OldLogOptions; /**< List of configuration lines
  1210. * for logfiles, old style. */
  1211. config_line_t *Logs; /**< New-style list of configuration lines
  1212. * for logs */
  1213. char *DebugLogFile; /**< Where to send verbose log messages. */
  1214. char *DataDirectory; /**< OR only: where to store long-term data. */
  1215. char *Nickname; /**< OR only: nickname of this onion router. */
  1216. char *Address; /**< OR only: configured address for this onion router. */
  1217. char *PidFile; /**< Where to store PID of Tor process. */
  1218. char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
  1219. * as exits. */
  1220. char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
  1221. * as entry points. */
  1222. int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
  1223. * stop building circuits? */
  1224. int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
  1225. * stop building circuits? */
  1226. char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
  1227. * use in circuits. */
  1228. char *RendNodes; /**< Comma-separated list of nicknames used as introduction
  1229. * points. */
  1230. char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
  1231. * as introduction points. */
  1232. smartlist_t *AllowInvalidNodes; /**< List of "entry", "middle", "exit" */
  1233. int _AllowInvalid; /**< Bitmask; derived from AllowInvalidNodes; */
  1234. config_line_t *ExitPolicy; /**< Lists of exit policy components. */
  1235. int ExitPolicyRejectPrivate; /**< Should we not exit to local addresses? */
  1236. config_line_t *SocksPolicy; /**< Lists of socks policy components */
  1237. config_line_t *DirPolicy; /**< Lists of dir policy components */
  1238. /** Addresses to bind for listening for SOCKS connections. */
  1239. config_line_t *SocksListenAddress;
  1240. /** Addresses to bind for listening for transparent connections. */
  1241. config_line_t *TransListenAddress;
  1242. /** Addresses to bind for listening for OR connections. */
  1243. config_line_t *ORListenAddress;
  1244. /** Addresses to bind for listening for directory connections. */
  1245. config_line_t *DirListenAddress;
  1246. /** Addresses to bind for listening for control connections. */
  1247. config_line_t *ControlListenAddress;
  1248. /** Local address to bind outbound sockets */
  1249. char *OutboundBindAddress;
  1250. /** Directory server only: which versions of
  1251. * Tor should we tell users to run? */
  1252. config_line_t *RecommendedVersions;
  1253. config_line_t *RecommendedClientVersions;
  1254. config_line_t *RecommendedServerVersions;
  1255. /** Whether dirservers refuse router descriptors with private IPs. */
  1256. int DirAllowPrivateAddresses;
  1257. char *User; /**< Name of user to run Tor as. */
  1258. char *Group; /**< Name of group to run Tor as. */
  1259. double PathlenCoinWeight; /**< Parameter used to configure average path
  1260. * length (alpha in geometric distribution). */
  1261. int ORPort; /**< Port to listen on for OR connections. */
  1262. int SocksPort; /**< Port to listen on for SOCKS connections. */
  1263. int TransPort; /**< Port to listen on for transparent connections. */
  1264. int ControlPort; /**< Port to listen on for control connections. */
  1265. int DirPort; /**< Port to listen on for directory connections. */
  1266. int AssumeReachable; /**< Whether to publish our descriptor regardless. */
  1267. int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
  1268. int V1AuthoritativeDir; /**< Boolean: is this an authoritative directory?
  1269. * for version 1 directories? */
  1270. int NamingAuthoritativeDir; /**< Boolean: is this an authoritative directory
  1271. * that's willing to bind names? */
  1272. int VersioningAuthoritativeDir; /**< Boolean: is this an authoritative
  1273. * directory that's willing to recommend
  1274. * versions? */
  1275. int AvoidDiskWrites; /**< Boolean: should we never cache things to disk?
  1276. * Not used yet. */
  1277. int ClientOnly; /**< Boolean: should we never evolve into a server role? */
  1278. int NoPublish; /**< Boolean: should we never publish a descriptor? */
  1279. int PublishServerDescriptor; /**< Do we publish our descriptor as normal? */
  1280. int PublishHidServDescriptors; /**< and our hidden service descriptors? */
  1281. int FetchServerDescriptors; /**< Do we fetch server descriptors as normal? */
  1282. int FetchHidServDescriptors; /** and hidden service descriptors? */
  1283. int FetchUselessDescriptors; /**< Do we fetch non-running descriptors too? */
  1284. int AllDirActionsPrivate; /**< Should every directory action be sent
  1285. * through a Tor circuit? */
  1286. int ConnLimit; /**< Demanded minimum number of simultaneous connections. */
  1287. int _ConnLimit; /**< Maximum allowed number of simultaneous connections. */
  1288. int RunAsDaemon; /**< If true, run in the background. (Unix only) */
  1289. int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
  1290. smartlist_t *FirewallPorts; /**< Which ports our firewall allows
  1291. * (strings). */
  1292. config_line_t *ReachableAddresses; /**< IP:ports our firewall allows. */
  1293. config_line_t *ReachableORAddresses; /**< IP:ports for OR conns. */
  1294. config_line_t *ReachableDirAddresses; /**< IP:ports for Dir conns. */
  1295. /** Application ports that require all nodes in circ to have sufficient
  1296. * uptime. */
  1297. smartlist_t *LongLivedPorts;
  1298. /** Should we try to reuse the same exit node for a given host */
  1299. smartlist_t *TrackHostExits;
  1300. int TrackHostExitsExpire; /**< Number of seconds until we expire an
  1301. * addressmap */
  1302. config_line_t *AddressMap; /**< List of address map directives. */
  1303. int RendPostPeriod; /**< How often do we post each rendezvous service
  1304. * descriptor? Remember to publish them independently. */
  1305. int KeepalivePeriod; /**< How often do we send padding cells to keep
  1306. * connections alive? */
  1307. int SocksTimeout; /**< How long do we let a socks connection wait
  1308. * unattached before we fail it? */
  1309. int CircuitBuildTimeout; /**< Cull non-open circuits that were born
  1310. * at least this many seconds ago. */
  1311. int CircuitIdleTimeout; /**< Cull open clean circuits that were born
  1312. * at least this many seconds ago. */
  1313. int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
  1314. * to wait simultaneously before we start dropping
  1315. * them? */
  1316. int NewCircuitPeriod; /**< How long do we use a circuit before building
  1317. * a new one? */
  1318. int MaxCircuitDirtiness; /**< Never use circs that were first used more than
  1319. this interval ago. */
  1320. uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing
  1321. * to use in a second? */
  1322. uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing
  1323. * to use in a second? */
  1324. uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
  1325. * tell people we have? */
  1326. uint64_t RelayBandwidthRate; /**< How much bandwidth, on average, are we
  1327. * willing to use for all relayed conns? */
  1328. uint64_t RelayBandwidthBurst; /**< How much bandwidth, at maximum, will we
  1329. * use in a second for all relayed conns? */
  1330. int NumCpus; /**< How many CPUs should we try to use? */
  1331. int RunTesting; /**< If true, create testing circuits to measure how well the
  1332. * other ORs are running. */
  1333. char *TestVia; /**< When reachability testing, use these as middle hop. */
  1334. config_line_t *RendConfigLines; /**< List of configuration lines
  1335. * for rendezvous services. */
  1336. char *ContactInfo; /**< Contact info to be published in the directory */
  1337. char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
  1338. uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
  1339. uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
  1340. char *HttpProxyAuthenticator; /**< username:password string, if any */
  1341. char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
  1342. uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
  1343. uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
  1344. char *HttpsProxyAuthenticator; /**< username:password string, if any */
  1345. config_line_t *DirServers; /**< List of configuration lines
  1346. * for directory servers. */
  1347. char *MyFamily; /**< Declared family for this OR. */
  1348. config_line_t *NodeFamilies; /**< List of config lines for
  1349. * node families */
  1350. config_line_t *RedirectExit; /**< List of config lines for simple
  1351. * addr/port redirection */
  1352. smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
  1353. config_line_t *AuthDirReject; /**< Address policy for descriptors to
  1354. * reject. */
  1355. config_line_t *AuthDirInvalid; /**< Address policy for descriptors to
  1356. * never mark as valid. */
  1357. int AuthDirRejectUnlisted; /**< Boolean: do we reject all routers that
  1358. * aren't named in our fingerprint file? */
  1359. char *AccountingStart; /**< How long is the accounting interval, and when
  1360. * does it start? */
  1361. uint64_t AccountingMax; /**< How many bytes do we allow per accounting
  1362. * interval before hibernation? 0 for "never
  1363. * hibernate." */
  1364. int _AccountingMaxKB; /**< How many KB do we allow per accounting
  1365. * interval before hibernation? 0 for "never
  1366. * hibernate." (Based on a deprecated option)*/
  1367. char *HashedControlPassword; /**< Base64-encoded hash of a password for
  1368. * the control system. */
  1369. int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
  1370. * the control system? */
  1371. int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
  1372. * circuits itself (0), or does it expect a controller
  1373. * to cope? (1) */
  1374. int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
  1375. * long do we wait before exiting? */
  1376. int SafeLogging; /**< Boolean: are we allowed to log sensitive strings
  1377. * such as addresses (0), or do we scrub them first (1)? */
  1378. int SafeSocks; /**< Boolean: should we outright refuse application
  1379. * connections that use socks4 or socks5-with-local-dns? */
  1380. #define LOG_PROTOCOL_WARN (get_options()->ProtocolWarnings ? \
  1381. LOG_WARN : LOG_INFO)
  1382. int ProtocolWarnings; /**< Boolean: when other parties screw up the Tor
  1383. * protocol, is it a warn or an info in our logs? */
  1384. int TestSocks; /**< Boolean: when we get a socks connection, do we loudly
  1385. * log whether it was DNS-leaking or not? */
  1386. int HardwareAccel; /**< Boolean: Should we enable OpenSSL hardware
  1387. * acceleration where available? */
  1388. int UseEntryGuards; /**< Boolean: Do we try to enter from a smallish number
  1389. * of fixed nodes? */
  1390. int NumEntryGuards; /**< How many entry guards do we try to establish? */
  1391. int RephistTrackTime; /**< How many seconds do we keep rephist info? */
  1392. int FastFirstHopPK; /**< If Tor believes it is safe, should we save a third
  1393. * of our PK time by sending CREATE_FAST cells? */
  1394. addr_policy_t *reachable_addr_policy; /**< Parsed from ReachableAddresses */
  1395. char *VirtualAddrNetwork; /**< Address and mask to hand out for virtual
  1396. * MAPADDRESS requests. */
  1397. int ServerDNSSearchDomains; /**< Boolean: If set, we don't force exit
  1398. * addresses to be FQDNs, but rather search for them in
  1399. * the local domains. */
  1400. int ServerDNSDetectHijacking; /**< Boolean: If true, check for DNS failure
  1401. * hijacking. */
  1402. char *ServerDNSResolvConfFile; /**< If provided, we configure our internal
  1403. * resolver from the file here rather than from
  1404. * /etc/resolv.conf (Unix) or the registry (Windows). */
  1405. int EnforceDistinctSubnets; /** If true, don't allow multiple routers in the
  1406. * same network zone in the same circuit. */
  1407. } or_options_t;
  1408. /** Persistent state for an onion router, as saved to disk. */
  1409. typedef struct {
  1410. uint32_t _magic;
  1411. int dirty;
  1412. /* XXXX These options aren't actually attached to anything yet. */
  1413. time_t LastWritten;
  1414. time_t AccountingIntervalStart;
  1415. uint64_t AccountingBytesReadInInterval;
  1416. uint64_t AccountingBytesWrittenInInterval;
  1417. int AccountingSecondsActive;
  1418. uint64_t AccountingExpectedUsage;
  1419. config_line_t *EntryGuards;
  1420. time_t BWHistoryReadEnds;
  1421. int BWHistoryReadInterval;
  1422. smartlist_t *BWHistoryReadValues;
  1423. time_t BWHistoryWriteEnds;
  1424. int BWHistoryWriteInterval;
  1425. smartlist_t *BWHistoryWriteValues;
  1426. char *TorVersion;
  1427. config_line_t *ExtraLines;
  1428. } or_state_t;
  1429. #define MAX_SOCKS_REPLY_LEN 1024
  1430. #define MAX_SOCKS_ADDR_LEN 256
  1431. #define SOCKS_COMMAND_CONNECT 0x01
  1432. #define SOCKS_COMMAND_RESOLVE 0xF0
  1433. #define SOCKS_COMMAND_RESOLVE_PTR 0xF1
  1434. /** State of a SOCKS request from a user to an OP */
  1435. struct socks_request_t {
  1436. char socks_version; /**< Which version of SOCKS did the client use? */
  1437. int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
  1438. size_t replylen; /**< Length of <b>reply</b>. */
  1439. char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
  1440. * we want to specify our own socks reply,
  1441. * rather than using the default socks4 or
  1442. * socks5 socks reply. We use this for the
  1443. * two-stage socks5 handshake.
  1444. */
  1445. int has_finished; /**< Has the SOCKS handshake finished? */
  1446. char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to
  1447. connect to? */
  1448. uint16_t port; /**< What port did the client ask to connect to? */
  1449. };
  1450. /* all the function prototypes go here */
  1451. /********************************* buffers.c ***************************/
  1452. buf_t *buf_new(void);
  1453. buf_t *buf_new_with_capacity(size_t size);
  1454. void buf_free(buf_t *buf);
  1455. void buf_clear(buf_t *buf);
  1456. void buf_shrink(buf_t *buf);
  1457. size_t buf_datalen(const buf_t *buf);
  1458. size_t buf_capacity(const buf_t *buf);
  1459. const char *_buf_peek_raw_buffer(const buf_t *buf);
  1460. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
  1461. int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
  1462. int flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen);
  1463. int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
  1464. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  1465. int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  1466. const char *data, size_t data_len, int done);
  1467. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  1468. int fetch_from_buf_http(buf_t *buf,
  1469. char **headers_out, size_t max_headerlen,
  1470. char **body_out, size_t *body_used, size_t max_bodylen,
  1471. int force_complete);
  1472. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  1473. int log_sockstype, int safe_socks);
  1474. int fetch_from_buf_control0(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
  1475. char **body_out, int check_for_v1);
  1476. int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
  1477. void assert_buf_ok(buf_t *buf);
  1478. /********************************* circuitbuild.c **********************/
  1479. char *circuit_list_path(origin_circuit_t *circ, int verbose);
  1480. void circuit_log_path(int severity, unsigned int domain,
  1481. origin_circuit_t *circ);
  1482. void circuit_rep_hist_note_result(origin_circuit_t *circ);
  1483. origin_circuit_t *origin_circuit_init(uint8_t purpose, int need_uptime,
  1484. int need_capacity, int internal);
  1485. origin_circuit_t *circuit_establish_circuit(uint8_t purpose,
  1486. extend_info_t *exit,
  1487. int need_uptime, int need_capacity,
  1488. int internal);
  1489. int circuit_handle_first_hop(origin_circuit_t *circ);
  1490. void circuit_n_conn_done(or_connection_t *or_conn, int status);
  1491. int inform_testing_reachability(void);
  1492. int circuit_send_next_onion_skin(origin_circuit_t *circ);
  1493. void circuit_note_clock_jumped(int seconds_elapsed);
  1494. int circuit_extend(cell_t *cell, circuit_t *circ);
  1495. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data,
  1496. int reverse);
  1497. int circuit_finish_handshake(origin_circuit_t *circ, uint8_t cell_type,
  1498. char *reply);
  1499. int circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer);
  1500. int onionskin_answer(or_circuit_t *circ, uint8_t cell_type, char *payload,
  1501. char *keys);
  1502. int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
  1503. int *need_capacity);
  1504. int circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *info);
  1505. int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *info);
  1506. void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
  1507. extend_info_t *extend_info_from_router(routerinfo_t *r);
  1508. extend_info_t *extend_info_dup(extend_info_t *info);
  1509. void extend_info_free(extend_info_t *info);
  1510. routerinfo_t *build_state_get_exit_router(cpath_build_state_t *state);
  1511. const char *build_state_get_exit_nickname(cpath_build_state_t *state);
  1512. void entry_guards_compute_status(void);
  1513. int entry_guard_register_connect_status(const char *digest, int succeeded);
  1514. void entry_nodes_should_be_added(void);
  1515. void entry_guards_prepend_from_config(void);
  1516. void entry_guards_update_state(or_state_t *state);
  1517. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  1518. int entry_guards_getinfo(const char *question, char **answer);
  1519. void entry_guards_free_all(void);
  1520. /********************************* circuitlist.c ***********************/
  1521. circuit_t * _circuit_get_global_list(void);
  1522. const char *circuit_state_to_string(int state);
  1523. void circuit_dump_by_conn(connection_t *conn, int severity);
  1524. void circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
  1525. or_connection_t *conn);
  1526. void circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
  1527. or_connection_t *conn);
  1528. void circuit_set_state(circuit_t *circ, int state);
  1529. void circuit_close_all_marked(void);
  1530. origin_circuit_t *origin_circuit_new(void);
  1531. or_circuit_t *or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn);
  1532. circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id,
  1533. or_connection_t *conn);
  1534. int circuit_id_used_on_conn(uint16_t circ_id, or_connection_t *conn);
  1535. circuit_t *circuit_get_by_edge_conn(edge_connection_t *conn);
  1536. void circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason);
  1537. origin_circuit_t *circuit_get_by_global_id(uint32_t id);
  1538. origin_circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query,
  1539. uint8_t purpose);
  1540. origin_circuit_t *circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1541. const char *digest, uint8_t purpose);
  1542. or_circuit_t *circuit_get_rendezvous(const char *cookie);
  1543. or_circuit_t *circuit_get_intro_point(const char *digest);
  1544. origin_circuit_t *circuit_find_to_cannibalize(uint8_t purpose,
  1545. extend_info_t *info,
  1546. int need_uptime,
  1547. int need_capacity, int internal);
  1548. void circuit_mark_all_unused_circs(void);
  1549. void circuit_expire_all_dirty_circs(void);
  1550. void _circuit_mark_for_close(circuit_t *circ, int reason,
  1551. int line, const char *file);
  1552. #define circuit_mark_for_close(c, reason) \
  1553. _circuit_mark_for_close((c), (reason), __LINE__, _SHORT_FILE_)
  1554. void assert_cpath_layer_ok(const crypt_path_t *cp);
  1555. void assert_circuit_ok(const circuit_t *c);
  1556. void circuit_free_all(void);
  1557. /********************************* circuituse.c ************************/
  1558. void circuit_expire_building(time_t now);
  1559. void circuit_remove_handled_ports(smartlist_t *needed_ports);
  1560. int circuit_stream_is_being_handled(edge_connection_t *conn, uint16_t port,
  1561. int min);
  1562. void circuit_build_needed_circs(time_t now);
  1563. void circuit_detach_stream(circuit_t *circ, edge_connection_t *conn);
  1564. void circuit_about_to_close_connection(connection_t *conn);
  1565. void reset_bandwidth_test(void);
  1566. int circuit_enough_testing_circs(void);
  1567. void circuit_has_opened(origin_circuit_t *circ);
  1568. void circuit_build_failed(origin_circuit_t *circ);
  1569. origin_circuit_t *circuit_launch_by_nickname(uint8_t purpose,
  1570. const char *exit_nickname,
  1571. int need_uptime, int need_capacity,
  1572. int is_internal);
  1573. origin_circuit_t *circuit_launch_by_extend_info(uint8_t purpose,
  1574. extend_info_t *info,
  1575. int need_uptime, int need_capacity,
  1576. int is_internal);
  1577. origin_circuit_t *circuit_launch_by_router(uint8_t purpose, routerinfo_t *exit,
  1578. int need_uptime, int need_capacity,
  1579. int is_internal);
  1580. void circuit_reset_failure_count(int timeout);
  1581. int connection_ap_handshake_attach_chosen_circuit(edge_connection_t *conn,
  1582. origin_circuit_t *circ);
  1583. int connection_ap_handshake_attach_circuit(edge_connection_t *conn);
  1584. /********************************* command.c ***************************/
  1585. void command_process_cell(cell_t *cell, or_connection_t *conn);
  1586. extern uint64_t stats_n_padding_cells_processed;
  1587. extern uint64_t stats_n_create_cells_processed;
  1588. extern uint64_t stats_n_created_cells_processed;
  1589. extern uint64_t stats_n_relay_cells_processed;
  1590. extern uint64_t stats_n_destroy_cells_processed;
  1591. /********************************* config.c ***************************/
  1592. or_options_t *get_options(void);
  1593. int set_options(or_options_t *new_val, char **msg);
  1594. void config_free_all(void);
  1595. const char *safe_str(const char *address);
  1596. const char *escaped_safe_str(const char *address);
  1597. int config_get_lines(char *string, config_line_t **result);
  1598. void config_free_lines(config_line_t *front);
  1599. int options_trial_assign(config_line_t *list, int use_defaults,
  1600. int clear_first, char **msg);
  1601. int resolve_my_address(int warn_severity, or_options_t *options,
  1602. uint32_t *addr, char **hostname_out);
  1603. void options_init(or_options_t *options);
  1604. int options_init_from_torrc(int argc, char **argv);
  1605. int options_init_logs(or_options_t *options, int validate_only);
  1606. int option_is_recognized(const char *key);
  1607. const char *option_get_canonical_name(const char *key);
  1608. config_line_t *option_get_assignment(or_options_t *options,
  1609. const char *key);
  1610. char *options_dump(or_options_t *options, int minimal);
  1611. int options_save_current(void);
  1612. const char *get_torrc_fname(void);
  1613. or_state_t *get_or_state(void);
  1614. int or_state_load(void);
  1615. int or_state_save(void);
  1616. int config_getinfo_helper(const char *question, char **answer);
  1617. /********************************* connection.c ***************************/
  1618. const char *conn_type_to_string(int type);
  1619. const char *conn_state_to_string(int type, int state);
  1620. connection_t *connection_new(int type);
  1621. void connection_unregister(connection_t *conn);
  1622. void connection_free(connection_t *conn);
  1623. void connection_free_all(void);
  1624. void connection_about_to_close_connection(connection_t *conn);
  1625. void connection_close_immediate(connection_t *conn);
  1626. void _connection_mark_for_close(connection_t *conn,int line, const char *file);
  1627. #define connection_mark_for_close(c) \
  1628. _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
  1629. void connection_expire_held_open(void);
  1630. int connection_connect(connection_t *conn, char *address, uint32_t addr,
  1631. uint16_t port);
  1632. int retry_all_listeners(int force, smartlist_t *replaced_conns,
  1633. smartlist_t *new_conns);
  1634. int connection_bucket_write_limit(connection_t *conn);
  1635. int global_write_bucket_empty(void);
  1636. void connection_bucket_init(void);
  1637. void connection_bucket_refill(struct timeval *now);
  1638. int connection_handle_read(connection_t *conn);
  1639. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  1640. int connection_wants_to_flush(connection_t *conn);
  1641. int connection_outbuf_too_full(connection_t *conn);
  1642. int connection_handle_write(connection_t *conn);
  1643. void _connection_controller_force_write(control_connection_t *conn);
  1644. void connection_write_to_buf(const char *string, size_t len,
  1645. connection_t *conn);
  1646. void connection_write_to_buf_zlib(dir_connection_t *conn,
  1647. const char *data, size_t data_len,
  1648. int done);
  1649. or_connection_t *connection_or_exact_get_by_addr_port(uint32_t addr,
  1650. uint16_t port);
  1651. edge_connection_t *connection_get_by_global_id(uint32_t id);
  1652. connection_t *connection_get_by_type(int type);
  1653. connection_t *connection_get_by_type_purpose(int type, int purpose);
  1654. connection_t *connection_get_by_type_addr_port_purpose(int type, uint32_t addr,
  1655. uint16_t port, int purpose);
  1656. connection_t *connection_get_by_type_state(int type, int state);
  1657. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  1658. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  1659. const char *rendquery);
  1660. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  1661. int connection_is_listener(connection_t *conn);
  1662. int connection_state_is_open(connection_t *conn);
  1663. int connection_state_is_connecting(connection_t *conn);
  1664. char *alloc_http_authenticator(const char *authenticator);
  1665. void assert_connection_ok(connection_t *conn, time_t now);
  1666. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  1667. /********************************* connection_edge.c *************************/
  1668. #define connection_mark_unattached_ap(conn, endreason) \
  1669. _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
  1670. void _connection_mark_unattached_ap(edge_connection_t *conn, int endreason,
  1671. int line, const char *file);
  1672. int connection_edge_reached_eof(edge_connection_t *conn);
  1673. int connection_edge_process_inbuf(edge_connection_t *conn,
  1674. int package_partial);
  1675. int connection_edge_destroy(uint16_t circ_id, edge_connection_t *conn);
  1676. int connection_edge_end(edge_connection_t *conn, char reason,
  1677. crypt_path_t *cpath_layer);
  1678. int connection_edge_end_errno(edge_connection_t *conn,
  1679. crypt_path_t *cpath_layer);
  1680. int connection_edge_finished_flushing(edge_connection_t *conn);
  1681. int connection_edge_finished_connecting(edge_connection_t *conn);
  1682. int connection_ap_handshake_send_begin(edge_connection_t *ap_conn,
  1683. origin_circuit_t *circ);
  1684. int connection_ap_handshake_send_resolve(edge_connection_t *ap_conn,
  1685. origin_circuit_t *circ);
  1686. int connection_ap_make_bridge(char *address, uint16_t port);
  1687. void connection_ap_handshake_socks_reply(edge_connection_t *conn, char *reply,
  1688. size_t replylen,
  1689. socks5_reply_status_t status);
  1690. void connection_ap_handshake_socks_resolved(edge_connection_t *conn,
  1691. int answer_type,
  1692. size_t answer_len,
  1693. const char *answer,
  1694. int ttl);
  1695. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  1696. int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ);
  1697. void connection_exit_connect(edge_connection_t *conn);
  1698. int connection_edge_is_rendezvous_stream(edge_connection_t *conn);
  1699. int connection_ap_can_use_exit(edge_connection_t *conn, routerinfo_t *exit);
  1700. void connection_ap_expire_beginning(void);
  1701. void connection_ap_attach_pending(void);
  1702. int connection_ap_detach_retriable(edge_connection_t *conn,
  1703. origin_circuit_t *circ);
  1704. void addressmap_init(void);
  1705. void addressmap_clean(time_t now);
  1706. void addressmap_clear_configured(void);
  1707. void addressmap_clear_transient(void);
  1708. void addressmap_free_all(void);
  1709. void addressmap_rewrite(char *address, size_t maxlen);
  1710. int addressmap_have_mapping(const char *address);
  1711. void addressmap_register(const char *address, char *new_address,
  1712. time_t expires);
  1713. int parse_virtual_addr_network(const char *val, int validate_only,
  1714. char **msg);
  1715. int client_dns_incr_failures(const char *address);
  1716. void client_dns_clear_failures(const char *address);
  1717. void client_dns_set_addressmap(const char *address, uint32_t val,
  1718. const char *exitname, int ttl);
  1719. int address_is_in_virtual_range(const char *addr);
  1720. const char *addressmap_register_virtual_address(int type, char *new_address);
  1721. void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
  1722. time_t max_expires);
  1723. int connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn,
  1724. origin_circuit_t *circ);
  1725. void set_exit_redirects(smartlist_t *lst);
  1726. typedef enum hostname_type_t {
  1727. NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME, BAD_HOSTNAME
  1728. } hostname_type_t;
  1729. hostname_type_t parse_extended_hostname(char *address);
  1730. /********************************* connection_or.c ***************************/
  1731. void connection_or_remove_from_identity_map(or_connection_t *conn);
  1732. void connection_or_clear_identity_map(void);
  1733. or_connection_t *connection_or_get_by_identity_digest(const char *digest);
  1734. int connection_or_reached_eof(or_connection_t *conn);
  1735. int connection_or_process_inbuf(or_connection_t *conn);
  1736. int connection_or_finished_flushing(or_connection_t *conn);
  1737. int connection_or_finished_connecting(or_connection_t *conn);
  1738. or_connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  1739. const char *id_digest);
  1740. int connection_tls_start_handshake(or_connection_t *conn, int receiving);
  1741. int connection_tls_continue_handshake(or_connection_t *conn);
  1742. void connection_or_write_cell_to_buf(const cell_t *cell,
  1743. or_connection_t *conn);
  1744. int connection_or_send_destroy(uint16_t circ_id, or_connection_t *conn,
  1745. int reason);
  1746. /********************************* control.c ***************************/
  1747. typedef enum circuit_status_event_t {
  1748. CIRC_EVENT_LAUNCHED = 0,
  1749. CIRC_EVENT_BUILT = 1,
  1750. CIRC_EVENT_EXTENDED = 2,
  1751. CIRC_EVENT_FAILED = 3,
  1752. CIRC_EVENT_CLOSED = 4,
  1753. } circuit_status_event_t;
  1754. typedef enum stream_status_event_t {
  1755. STREAM_EVENT_SENT_CONNECT = 0,
  1756. STREAM_EVENT_SENT_RESOLVE = 1,
  1757. STREAM_EVENT_SUCCEEDED = 2,
  1758. STREAM_EVENT_FAILED = 3,
  1759. STREAM_EVENT_CLOSED = 4,
  1760. STREAM_EVENT_NEW = 5,
  1761. STREAM_EVENT_NEW_RESOLVE = 6,
  1762. STREAM_EVENT_FAILED_RETRIABLE = 7
  1763. } stream_status_event_t;
  1764. typedef enum or_conn_status_event_t {
  1765. OR_CONN_EVENT_LAUNCHED = 0,
  1766. OR_CONN_EVENT_CONNECTED = 1,
  1767. OR_CONN_EVENT_FAILED = 2,
  1768. OR_CONN_EVENT_CLOSED = 3,
  1769. OR_CONN_EVENT_NEW = 4,
  1770. } or_conn_status_event_t;
  1771. void control_update_global_event_mask(void);
  1772. void control_adjust_event_log_severity(void);
  1773. /** Execute the statement <b>stmt</b>, which may log events concerning the
  1774. * connection <b>conn</b>. To prevent infinite loops, disable log messages
  1775. * being sent to controllers if <b>conn</b> is a control connection.
  1776. *
  1777. * Stmt must not contain any return or goto statements.
  1778. */
  1779. #define CONN_LOG_PROTECT(conn, stmt) \
  1780. do { \
  1781. int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
  1782. if (_log_conn_is_control) \
  1783. disable_control_logging(); \
  1784. do {stmt;} while (0); \
  1785. if (_log_conn_is_control) \
  1786. enable_control_logging(); \
  1787. } while (0)
  1788. /** Log information about the connection <b>conn</b>, protecting it as with
  1789. * CONN_LOG_PROTECT. Example:
  1790. *
  1791. * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
  1792. **/
  1793. #define LOG_FN_CONN(conn, args) \
  1794. CONN_LOG_PROTECT(conn, log_fn args)
  1795. int connection_control_finished_flushing(control_connection_t *conn);
  1796. int connection_control_reached_eof(control_connection_t *conn);
  1797. int connection_control_process_inbuf(control_connection_t *conn);
  1798. int control_event_circuit_status(origin_circuit_t *circ,
  1799. circuit_status_event_t e);
  1800. int control_event_stream_status(edge_connection_t *conn,
  1801. stream_status_event_t e);
  1802. int control_event_or_conn_status(or_connection_t *conn,
  1803. or_conn_status_event_t e);
  1804. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  1805. void control_event_logmsg(int severity, unsigned int domain, const char *msg);
  1806. int control_event_descriptors_changed(smartlist_t *routers);
  1807. int control_event_address_mapped(const char *from, const char *to,
  1808. time_t expires);
  1809. int control_event_or_authdir_new_descriptor(const char *action,
  1810. const char *descriptor,
  1811. const char *msg);
  1812. int init_cookie_authentication(int enabled);
  1813. int decode_hashed_password(char *buf, const char *hashed);
  1814. void disable_control_logging(void);
  1815. void enable_control_logging(void);
  1816. /********************************* cpuworker.c *****************************/
  1817. void cpu_init(void);
  1818. void cpuworkers_rotate(void);
  1819. int connection_cpu_finished_flushing(connection_t *conn);
  1820. int connection_cpu_reached_eof(connection_t *conn);
  1821. int connection_cpu_process_inbuf(connection_t *conn);
  1822. int assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
  1823. void *task);
  1824. /********************************* directory.c ***************************/
  1825. void directory_post_to_dirservers(uint8_t purpose, const char *payload,
  1826. size_t payload_len);
  1827. void directory_get_from_dirserver(uint8_t purpose, const char *resource,
  1828. int retry_if_no_servers);
  1829. void directory_initiate_command_router(routerinfo_t *router, uint8_t purpose,
  1830. int private_connection,
  1831. const char *resource,
  1832. const char *payload,
  1833. size_t payload_len);
  1834. void directory_initiate_command_routerstatus(routerstatus_t *status,
  1835. uint8_t purpose,
  1836. int private_connection,
  1837. const char *resource,
  1838. const char *payload,
  1839. size_t payload_len);
  1840. int parse_http_response(const char *headers, int *code, time_t *date,
  1841. int *compression, char **response);
  1842. int connection_dir_reached_eof(dir_connection_t *conn);
  1843. int connection_dir_process_inbuf(dir_connection_t *conn);
  1844. int connection_dir_finished_flushing(dir_connection_t *conn);
  1845. int connection_dir_finished_connecting(dir_connection_t *conn);
  1846. void connection_dir_request_failed(dir_connection_t *conn);
  1847. int dir_split_resource_into_fingerprints(const char *resource,
  1848. smartlist_t *fp_out, int *compresseed_out,
  1849. int decode_hex, int sort_uniq);
  1850. char *directory_dump_request_log(void);
  1851. /********************************* dirserv.c ***************************/
  1852. int connection_dirserv_flushed_some(dir_connection_t *conn);
  1853. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  1854. int dirserv_load_fingerprint_file(void);
  1855. void dirserv_free_fingerprint_list(void);
  1856. const char *dirserv_get_nickname_by_digest(const char *digest);
  1857. int dirserv_add_descriptor(const char *desc, const char **msg);
  1858. char *dirserver_getinfo_unregistered(const char *question);
  1859. void dirserv_free_descriptors(void);
  1860. int dirserv_thinks_router_is_blatantly_unreachable(routerinfo_t *router,
  1861. time_t now);
  1862. int list_server_status(smartlist_t *routers, char **router_status_out);
  1863. int dirserv_dump_directory_to_string(char **dir_out,
  1864. crypto_pk_env_t *private_key,
  1865. int complete);
  1866. void directory_set_dirty(void);
  1867. cached_dir_t *dirserv_get_directory(void);
  1868. size_t dirserv_get_runningrouters(const char **rr, int compress);
  1869. void dirserv_set_cached_directory(const char *directory, time_t when,
  1870. int is_running_routers);
  1871. void dirserv_set_cached_networkstatus_v2(const char *directory,
  1872. const char *identity,
  1873. time_t published);
  1874. void dirserv_get_networkstatus_v2(smartlist_t *result, const char *key);
  1875. void dirserv_get_networkstatus_v2_fingerprints(smartlist_t *result,
  1876. const char *key);
  1877. int dirserv_get_routerdesc_fingerprints(smartlist_t *fps_out, const char *key,
  1878. const char **msg);
  1879. int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
  1880. const char **msg);
  1881. void dirserv_orconn_tls_done(const char *address,
  1882. uint16_t or_port,
  1883. const char *digest_rcvd,
  1884. const char *nickname,
  1885. int as_advertised);
  1886. void dirserv_test_reachability(int try_all);
  1887. int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  1888. int complain);
  1889. int dirserv_would_reject_router(routerstatus_t *rs);
  1890. void dirserv_free_all(void);
  1891. void cached_dir_decref(cached_dir_t *d);
  1892. /********************************* dns.c ***************************/
  1893. int dns_init(void);
  1894. void dns_free_all(void);
  1895. uint32_t dns_clip_ttl(uint32_t ttl);
  1896. int connection_dns_finished_flushing(connection_t *conn);
  1897. int connection_dns_reached_eof(connection_t *conn);
  1898. int connection_dns_process_inbuf(connection_t *conn);
  1899. void dns_reset(void);
  1900. void connection_dns_remove(edge_connection_t *conn);
  1901. void assert_connection_edge_not_dns_pending(edge_connection_t *conn);
  1902. void assert_all_pending_dns_resolves_ok(void);
  1903. void dns_cancel_pending_resolve(const char *question);
  1904. int dns_resolve(edge_connection_t *exitconn, or_circuit_t *circ);
  1905. void dns_launch_wildcard_checks(void);
  1906. /********************************* hibernate.c **********************/
  1907. int accounting_parse_options(or_options_t *options, int validate_only);
  1908. int accounting_is_enabled(or_options_t *options);
  1909. void configure_accounting(time_t now);
  1910. void accounting_run_housekeeping(time_t now);
  1911. void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
  1912. int accounting_record_bandwidth_usage(time_t now);
  1913. void hibernate_begin_shutdown(void);
  1914. int we_are_hibernating(void);
  1915. void consider_hibernation(time_t now);
  1916. int accounting_getinfo_helper(const char *question, char **answer);
  1917. void accounting_set_bandwidth_usage_from_state(or_state_t *state);
  1918. /********************************* main.c ***************************/
  1919. extern int has_completed_circuit;
  1920. int connection_add(connection_t *conn);
  1921. int connection_remove(connection_t *conn);
  1922. int connection_in_array(connection_t *conn);
  1923. void add_connection_to_closeable_list(connection_t *conn);
  1924. int connection_is_on_closeable_list(connection_t *conn);
  1925. void get_connection_array(connection_t ***array, int *n);
  1926. void connection_watch_events(connection_t *conn, short events);
  1927. int connection_is_reading(connection_t *conn);
  1928. void connection_stop_reading(connection_t *conn);
  1929. void connection_start_reading(connection_t *conn);
  1930. int connection_is_writing(connection_t *conn);
  1931. void connection_stop_writing(connection_t *conn);
  1932. void connection_start_writing(connection_t *conn);
  1933. void directory_all_unreachable(time_t now);
  1934. void directory_info_has_arrived(time_t now, int from_cache);
  1935. int control_signal_check(int the_signal);
  1936. void control_signal_act(int the_signal);
  1937. void handle_signals(int is_parent);
  1938. void tor_cleanup(void);
  1939. void tor_free_all(int postfork);
  1940. int tor_main(int argc, char *argv[]);
  1941. /********************************* onion.c ***************************/
  1942. int onion_pending_add(or_circuit_t *circ);
  1943. or_circuit_t *onion_next_task(void);
  1944. void onion_pending_remove(or_circuit_t *circ);
  1945. int onion_skin_create(crypto_pk_env_t *router_key,
  1946. crypto_dh_env_t **handshake_state_out,
  1947. char *onion_skin_out);
  1948. int onion_skin_server_handshake(const char *onion_skin,
  1949. crypto_pk_env_t *private_key,
  1950. crypto_pk_env_t *prev_private_key,
  1951. char *handshake_reply_out,
  1952. char *key_out,
  1953. size_t key_out_len);
  1954. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  1955. const char *handshake_reply,
  1956. char *key_out,
  1957. size_t key_out_len);
  1958. int fast_server_handshake(const char *key_in,
  1959. char *handshake_reply_out,
  1960. char *key_out,
  1961. size_t key_out_len);
  1962. int fast_client_handshake(const char *handshake_state,
  1963. const char *handshake_reply_out,
  1964. char *key_out,
  1965. size_t key_out_len);
  1966. void clear_pending_onions(void);
  1967. /********************************* policies.c ************************/
  1968. typedef enum {
  1969. ADDR_POLICY_ACCEPTED=0,
  1970. ADDR_POLICY_REJECTED=-1,
  1971. ADDR_POLICY_PROBABLY_ACCEPTED=1,
  1972. ADDR_POLICY_PROBABLY_REJECTED=2
  1973. } addr_policy_result_t;
  1974. int firewall_is_fascist_or(void);
  1975. int fascist_firewall_allows_address_or(uint32_t addr, uint16_t port);
  1976. int fascist_firewall_allows_address_dir(uint32_t addr, uint16_t port);
  1977. int dir_policy_permits_address(uint32_t addr);
  1978. int socks_policy_permits_address(uint32_t addr);
  1979. int authdir_policy_permits_address(uint32_t addr, uint16_t port);
  1980. int authdir_policy_valid_address(uint32_t addr, uint16_t port);
  1981. int validate_addr_policies(or_options_t *options, char **msg);
  1982. void policies_parse_from_options(or_options_t *options);
  1983. int cmp_addr_policies(addr_policy_t *a, addr_policy_t *b);
  1984. addr_policy_result_t compare_addr_to_addr_policy(uint32_t addr,
  1985. uint16_t port, addr_policy_t *policy);
  1986. int policies_parse_exit_policy(config_line_t *cfg,
  1987. addr_policy_t **dest,
  1988. int rejectprivate);
  1989. int exit_policy_is_general_exit(addr_policy_t *policy);
  1990. int policy_is_reject_star(addr_policy_t *policy);
  1991. int policies_getinfo_helper(const char *question, char **answer);
  1992. void addr_policy_free(addr_policy_t *p);
  1993. void policies_free_all(void);
  1994. /********************************* relay.c ***************************/
  1995. extern uint64_t stats_n_relay_cells_relayed;
  1996. extern uint64_t stats_n_relay_cells_delivered;
  1997. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  1998. int cell_direction);
  1999. void relay_header_pack(char *dest, const relay_header_t *src);
  2000. void relay_header_unpack(relay_header_t *dest, const char *src);
  2001. int connection_edge_send_command(edge_connection_t *fromconn, circuit_t *circ,
  2002. int relay_command, const char *payload,
  2003. size_t payload_len,
  2004. crypt_path_t *cpath_layer);
  2005. int connection_edge_package_raw_inbuf(edge_connection_t *conn,
  2006. int package_partial);
  2007. void connection_edge_consider_sending_sendme(edge_connection_t *conn);
  2008. socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
  2009. int errno_to_end_reason(int e);
  2010. extern uint64_t stats_n_data_cells_packaged;
  2011. extern uint64_t stats_n_data_bytes_packaged;
  2012. extern uint64_t stats_n_data_cells_received;
  2013. extern uint64_t stats_n_data_bytes_received;
  2014. /********************************* rephist.c ***************************/
  2015. void rep_hist_init(void);
  2016. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  2017. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  2018. void rep_hist_note_disconnect(const char* nickname, time_t when);
  2019. void rep_hist_note_connection_died(const char* nickname, time_t when);
  2020. void rep_hist_note_extend_succeeded(const char *from_name,
  2021. const char *to_name);
  2022. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  2023. void rep_hist_dump_stats(time_t now, int severity);
  2024. void rep_hist_note_bytes_read(int num_bytes, time_t when);
  2025. void rep_hist_note_bytes_written(int num_bytes, time_t when);
  2026. int rep_hist_bandwidth_assess(void);
  2027. char *rep_hist_get_bandwidth_lines(void);
  2028. void rep_hist_update_state(or_state_t *state);
  2029. int rep_hist_load_state(or_state_t *state, char **err);
  2030. void rep_history_clean(time_t before);
  2031. void rep_hist_note_used_port(uint16_t port, time_t now);
  2032. smartlist_t *rep_hist_get_predicted_ports(time_t now);
  2033. void rep_hist_note_used_resolve(time_t now);
  2034. void rep_hist_note_used_internal(time_t now, int need_uptime,
  2035. int need_capacity);
  2036. int rep_hist_get_predicted_internal(time_t now, int *need_uptime,
  2037. int *need_capacity);
  2038. int any_predicted_circuits(time_t now);
  2039. int rep_hist_circbuilding_dormant(time_t now);
  2040. void rep_hist_free_all(void);
  2041. /********************************* rendclient.c ***************************/
  2042. void rend_client_introcirc_has_opened(origin_circuit_t *circ);
  2043. void rend_client_rendcirc_has_opened(origin_circuit_t *circ);
  2044. int rend_client_introduction_acked(origin_circuit_t *circ, const char *request,
  2045. size_t request_len);
  2046. void rend_client_refetch_renddesc(const char *query);
  2047. int rend_client_remove_intro_point(extend_info_t *failed_intro,
  2048. const char *query);
  2049. int rend_client_rendezvous_acked(origin_circuit_t *circ, const char *request,
  2050. size_t request_len);
  2051. int rend_client_receive_rendezvous(origin_circuit_t *circ, const char *request,
  2052. size_t request_len);
  2053. void rend_client_desc_here(const char *query);
  2054. extend_info_t *rend_client_get_random_intro(const char *query);
  2055. int rend_client_send_introduction(origin_circuit_t *introcirc,
  2056. origin_circuit_t *rendcirc);
  2057. /********************************* rendcommon.c ***************************/
  2058. /** Information used to connect to a hidden service. */
  2059. typedef struct rend_service_descriptor_t {
  2060. crypto_pk_env_t *pk; /**< This service's public key. */
  2061. int version; /**< 0 or 1. */
  2062. time_t timestamp; /**< Time when the descriptor was generated. */
  2063. uint16_t protocols; /**< Bitmask: which rendezvous protocols are supported?
  2064. * (We allow bits '0', '1', and '2' to be set.) */
  2065. int n_intro_points; /**< Number of introduction points. */
  2066. /** Array of n_intro_points elements for this service's introduction points'
  2067. * nicknames. Elements are removed from this array if introduction attempts
  2068. * fail. */
  2069. char **intro_points;
  2070. /** Array of n_intro_points elements for this service's introduction points'
  2071. * extend_infos, or NULL if this descriptor is V0. Elements are removed
  2072. * from this array if introduction attempts fail. If this array is present,
  2073. * its elements correspond to the elements of intro_points. */
  2074. extend_info_t **intro_point_extend_info;
  2075. } rend_service_descriptor_t;
  2076. int rend_cmp_service_ids(const char *one, const char *two);
  2077. void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  2078. const char *payload);
  2079. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  2080. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  2081. int version,
  2082. crypto_pk_env_t *key,
  2083. char **str_out,
  2084. size_t *len_out);
  2085. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str,
  2086. size_t len);
  2087. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  2088. /** A cached rendezvous descriptor. */
  2089. typedef struct rend_cache_entry_t {
  2090. size_t len; /** Length of <b>desc</b> */
  2091. time_t received; /** When was the descriptor received? */
  2092. char *desc; /** Service descriptor */
  2093. rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
  2094. } rend_cache_entry_t;
  2095. void rend_cache_init(void);
  2096. void rend_cache_clean(void);
  2097. void rend_cache_free_all(void);
  2098. int rend_valid_service_id(const char *query);
  2099. int rend_cache_lookup_desc(const char *query, int version, const char **desc,
  2100. size_t *desc_len);
  2101. int rend_cache_lookup_entry(const char *query, int version,
  2102. rend_cache_entry_t **entry_out);
  2103. int rend_cache_store(const char *desc, size_t desc_len);
  2104. /********************************* rendservice.c ***************************/
  2105. int num_rend_services(void);
  2106. int rend_config_services(or_options_t *options, int validate_only);
  2107. int rend_service_load_keys(void);
  2108. void rend_services_init(void);
  2109. void rend_services_introduce(void);
  2110. void rend_consider_services_upload(time_t now);
  2111. void rend_service_intro_has_opened(origin_circuit_t *circuit);
  2112. int rend_service_intro_established(origin_circuit_t *circuit,
  2113. const char *request,
  2114. size_t request_len);
  2115. void rend_service_rendezvous_has_opened(origin_circuit_t *circuit);
  2116. int rend_service_introduce(origin_circuit_t *circuit, const char *request,
  2117. size_t request_len);
  2118. void rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc);
  2119. int rend_service_set_connection_addr_port(edge_connection_t *conn,
  2120. origin_circuit_t *circ);
  2121. void rend_service_dump_stats(int severity);
  2122. void rend_service_free_all(void);
  2123. /********************************* rendmid.c *******************************/
  2124. int rend_mid_establish_intro(or_circuit_t *circ, const char *request,
  2125. size_t request_len);
  2126. int rend_mid_introduce(or_circuit_t *circ, const char *request,
  2127. size_t request_len);
  2128. int rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
  2129. size_t request_len);
  2130. int rend_mid_rendezvous(or_circuit_t *circ, const char *request,
  2131. size_t request_len);
  2132. /********************************* router.c ***************************/
  2133. void set_onion_key(crypto_pk_env_t *k);
  2134. crypto_pk_env_t *get_onion_key(void);
  2135. time_t get_onion_key_set_at(void);
  2136. void set_identity_key(crypto_pk_env_t *k);
  2137. crypto_pk_env_t *get_identity_key(void);
  2138. int identity_key_is_set(void);
  2139. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  2140. void rotate_onion_key(void);
  2141. crypto_pk_env_t *init_key_from_file(const char *fname);
  2142. int init_keys(void);
  2143. int check_whether_orport_reachable(void);
  2144. int check_whether_dirport_reachable(void);
  2145. void consider_testing_reachability(int test_or, int test_dir);
  2146. void router_orport_found_reachable(void);
  2147. void router_dirport_found_reachable(void);
  2148. void server_has_changed_ip(void);
  2149. void router_perform_bandwidth_test(int num_circs, time_t now);
  2150. int authdir_mode(or_options_t *options);
  2151. int clique_mode(or_options_t *options);
  2152. int server_mode(or_options_t *options);
  2153. int advertised_server_mode(void);
  2154. int proxy_mode(or_options_t *options);
  2155. void consider_publishable_server(int force);
  2156. int router_is_clique_mode(routerinfo_t *router);
  2157. void router_upload_dir_desc_to_dirservers(int force);
  2158. void mark_my_descriptor_dirty_if_older_than(time_t when);
  2159. void mark_my_descriptor_dirty(void);
  2160. void check_descriptor_bandwidth_changed(time_t now);
  2161. void check_descriptor_ipaddress_changed(time_t now);
  2162. void router_new_address_suggestion(const char *suggestion);
  2163. int router_compare_to_my_exit_policy(edge_connection_t *conn);
  2164. routerinfo_t *router_get_my_routerinfo(void);
  2165. const char *router_get_my_descriptor(void);
  2166. int router_digest_is_me(const char *digest);
  2167. int router_is_me(routerinfo_t *router);
  2168. int router_fingerprint_is_me(const char *fp);
  2169. int router_pick_published_address(or_options_t *options, uint32_t *addr);
  2170. int router_rebuild_descriptor(int force);
  2171. int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  2172. crypto_pk_env_t *ident_key);
  2173. int is_legal_nickname(const char *s);
  2174. int is_legal_nickname_or_hexdigest(const char *s);
  2175. int is_legal_hexdigest(const char *s);
  2176. void router_reset_warnings(void);
  2177. void router_free_all(void);
  2178. /********************************* routerlist.c ***************************/
  2179. /** Represents information about a single trusted directory server. */
  2180. typedef struct trusted_dir_server_t {
  2181. char *description;
  2182. char *nickname;
  2183. char *address; /**< Hostname */
  2184. uint32_t addr; /**< IPv4 address */
  2185. uint16_t dir_port; /**< Directory port */
  2186. char digest[DIGEST_LEN]; /**< Digest of identity key */
  2187. unsigned int is_running:1; /**< True iff we think this server is running. */
  2188. /** True iff this server is an authority for the older ("v1") directory
  2189. * protocol. */
  2190. unsigned int is_v1_authority:1;
  2191. /** True iff this server is an authority for the newer ("v2") directory
  2192. * protocol. */
  2193. unsigned int is_v2_authority:1;
  2194. /** True iff this server is an authority for hidden services */
  2195. unsigned int is_hidserv_authority:1;
  2196. int n_networkstatus_failures; /**< How many times have we asked for this
  2197. * server's network-status unsuccessfully? */
  2198. routerstatus_t fake_status; /**< Used when we need to pass this trusted
  2199. * dir_server_t to directory_initiate_command_*
  2200. * as a routerstatus_t. Not updated by the
  2201. * router-status management code!
  2202. **/
  2203. } trusted_dir_server_t;
  2204. int router_reload_router_list(void);
  2205. int router_reload_networkstatus(void);
  2206. smartlist_t *router_get_trusted_dir_servers(void);
  2207. routerstatus_t *router_pick_directory_server(int requireother,
  2208. int fascistfirewall,
  2209. int for_v2_directory,
  2210. int retry_if_no_servers);
  2211. typedef enum {
  2212. V1_AUTHORITY, V2_AUTHORITY, HIDSERV_AUTHORITY,
  2213. } authority_type_t;
  2214. routerstatus_t *router_pick_trusteddirserver(authority_type_t type,
  2215. int requireother,
  2216. int fascistfirewall,
  2217. int retry_if_no_servers);
  2218. trusted_dir_server_t *router_get_trusteddirserver_by_digest(
  2219. const char *digest);
  2220. void routerlist_add_family(smartlist_t *sl, routerinfo_t *router);
  2221. void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
  2222. int must_be_running,
  2223. int warn_if_down, int warn_if_unnamed);
  2224. int router_nickname_is_in_list(routerinfo_t *router, const char *list);
  2225. routerinfo_t *routerlist_find_my_routerinfo(void);
  2226. routerinfo_t *router_find_exact_exit_enclave(const char *address,
  2227. uint16_t port);
  2228. #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
  2229. int router_is_unreliable(routerinfo_t *router, int need_uptime,
  2230. int need_capacity, int need_guard);
  2231. uint32_t router_get_advertised_bandwidth(routerinfo_t *router);
  2232. routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
  2233. routerinfo_t *router_choose_random_node(const char *preferred,
  2234. const char *excluded,
  2235. smartlist_t *excludedsmartlist,
  2236. int need_uptime, int need_bandwidth,
  2237. int need_guard,
  2238. int allow_invalid, int strict);
  2239. routerinfo_t *router_get_by_nickname(const char *nickname,
  2240. int warn_if_unnamed);
  2241. routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
  2242. routerinfo_t *router_get_by_digest(const char *digest);
  2243. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  2244. const char *signed_descriptor_get_body(signed_descriptor_t *desc);
  2245. int router_digest_version_as_new_as(const char *digest, const char *cutoff);
  2246. int router_digest_is_trusted_dir(const char *digest);
  2247. routerlist_t *router_get_routerlist(void);
  2248. void routerlist_reset_warnings(void);
  2249. void routerlist_free(routerlist_t *routerlist);
  2250. void dump_routerlist_mem_usage(int severity);
  2251. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int idx,
  2252. int make_old);
  2253. void routerinfo_free(routerinfo_t *router);
  2254. void routerstatus_free(routerstatus_t *routerstatus);
  2255. void networkstatus_free(networkstatus_t *networkstatus);
  2256. void routerlist_free_all(void);
  2257. routerinfo_t *routerinfo_copy(const routerinfo_t *router);
  2258. void router_set_status(const char *digest, int up);
  2259. void routerlist_remove_old_routers(void);
  2260. void networkstatus_list_clean(time_t now);
  2261. int router_add_to_routerlist(routerinfo_t *router, const char **msg,
  2262. int from_cache, int from_fetch);
  2263. int router_load_single_router(const char *s, uint8_t purpose,
  2264. const char **msg);
  2265. void router_load_routers_from_string(const char *s,
  2266. saved_location_t saved_location,
  2267. smartlist_t *requested_fingerprints);
  2268. typedef enum {
  2269. NS_FROM_CACHE, NS_FROM_DIR_BY_FP, NS_FROM_DIR_ALL, NS_GENERATED
  2270. } networkstatus_source_t;
  2271. int router_set_networkstatus(const char *s, time_t arrived_at,
  2272. networkstatus_source_t source,
  2273. smartlist_t *requested_fingerprints);
  2274. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
  2275. int need_uptime);
  2276. int router_exit_policy_rejects_all(routerinfo_t *router);
  2277. void add_trusted_dir_server(const char *nickname,
  2278. const char *address, uint16_t port,
  2279. const char *digest, int is_v1_authority,
  2280. int is_v2_authority, int is_hidserv_authority);
  2281. void clear_trusted_dir_servers(void);
  2282. int any_trusted_dir_is_v1_authority(void);
  2283. networkstatus_t *networkstatus_get_by_digest(const char *digest);
  2284. local_routerstatus_t *router_get_combined_status_by_digest(const char *digest);
  2285. void update_networkstatus_downloads(time_t now);
  2286. void update_router_descriptor_downloads(time_t now);
  2287. void routers_update_all_from_networkstatus(void);
  2288. void routers_update_status_from_networkstatus(smartlist_t *routers,
  2289. int reset_failures);
  2290. smartlist_t *router_list_superseded(void);
  2291. int router_have_minimum_dir_info(void);
  2292. void networkstatus_list_update_recent(time_t now);
  2293. void router_reset_descriptor_download_failures(void);
  2294. void router_reset_status_download_failures(void);
  2295. int router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2);
  2296. const char *esc_router_info(routerinfo_t *router);
  2297. /********************************* routerparse.c ************************/
  2298. #define MAX_STATUS_TAG_LEN 32
  2299. /** Structure to hold parsed Tor versions. This is a little messier
  2300. * than we would like it to be, because we changed version schemes with 0.1.0.
  2301. *
  2302. * See version-spec.txt for the whole business.
  2303. */
  2304. typedef struct tor_version_t {
  2305. int major;
  2306. int minor;
  2307. int micro;
  2308. /** Release status. For version in the post-0.1 format, this is always
  2309. * VER_RELEASE. */
  2310. enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
  2311. int patchlevel;
  2312. char status_tag[MAX_STATUS_TAG_LEN];
  2313. } tor_version_t;
  2314. typedef enum version_status_t {
  2315. VS_RECOMMENDED=0, /**< This version is listed as recommended. */
  2316. VS_OLD=1, /**< This version is older than any recommended version. */
  2317. VS_NEW=2, /**< This version is newer than any recommended version. */
  2318. VS_NEW_IN_SERIES=3, /**< This version is newer than any recommended version
  2319. * in its series, and such recommended versions exist. */
  2320. VS_UNRECOMMENDED=4 /**< This version is not recommended (general case) */
  2321. } version_status_t;
  2322. int router_get_router_hash(const char *s, char *digest);
  2323. int router_get_dir_hash(const char *s, char *digest);
  2324. int router_get_runningrouters_hash(const char *s, char *digest);
  2325. int router_get_networkstatus_v2_hash(const char *s, char *digest);
  2326. int router_append_dirobj_signature(char *buf, size_t buf_len,
  2327. const char *digest,
  2328. crypto_pk_env_t *private_key);
  2329. int router_parse_list_from_string(const char **s,
  2330. smartlist_t *dest,
  2331. saved_location_t saved_location);
  2332. int router_parse_routerlist_from_directory(const char *s,
  2333. routerlist_t **dest,
  2334. crypto_pk_env_t *pkey,
  2335. int check_version,
  2336. int write_to_cache);
  2337. int router_parse_runningrouters(const char *str);
  2338. int router_parse_directory(const char *str);
  2339. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end,
  2340. int cache_copy);
  2341. addr_policy_t *router_parse_addr_policy_from_string(const char *s,
  2342. int assume_action);
  2343. version_status_t tor_version_is_obsolete(const char *myversion,
  2344. const char *versionlist);
  2345. version_status_t version_status_join(version_status_t a, version_status_t b);
  2346. int tor_version_parse(const char *s, tor_version_t *out);
  2347. int tor_version_as_new_as(const char *platform, const char *cutoff);
  2348. int tor_version_compare(tor_version_t *a, tor_version_t *b);
  2349. void sort_version_list(smartlist_t *lst, int remove_duplicates);
  2350. void assert_addr_policy_ok(addr_policy_t *t);
  2351. networkstatus_t *networkstatus_parse_from_string(const char *s);
  2352. #endif