or.h 116 KB

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