or.h 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, 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 "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_UN_H
  56. #include <sys/un.h>
  57. #endif
  58. //#ifdef HAVE_SYS_TIME_H
  59. //#include <sys/time.h>
  60. //#endif
  61. #ifdef HAVE_SYS_STAT_H
  62. #include <sys/stat.h>
  63. #endif
  64. //#ifdef HAVE_NETINET_IN_H
  65. //#include <netinet/in.h>
  66. //#endif
  67. #ifdef HAVE_ARPA_INET_H
  68. #include <arpa/inet.h>
  69. #endif
  70. #ifdef HAVE_ERRNO_H
  71. #include <errno.h>
  72. #endif
  73. #ifdef HAVE_ASSERT_H
  74. #include <assert.h>
  75. #endif
  76. #ifdef HAVE_TIME_H
  77. #include <time.h>
  78. #endif
  79. /** Upper bound on maximum simultaneous connections; can be lowered by
  80. * config file. */
  81. #if defined(CYGWIN) || defined(__CYGWIN__)
  82. /* http://archives.seul.org/or/talk/Aug-2006/msg00210.html */
  83. #define MAXCONNECTIONS 3200
  84. #else
  85. /* very high by default. "nobody should need more than this..." */
  86. #define MAXCONNECTIONS 15000
  87. #endif
  88. #if 0
  89. #ifdef MS_WINDOWS
  90. /* No, we don't need to redefine FD_SETSIZE before including winsock:
  91. * we use libevent now, and libevent handles the select() stuff. Yes,
  92. * some documents imply that we need to redefine anyway if we're using
  93. * select() anywhere in our application or in anything it links to: these
  94. * documents are either the holy texts of a cargo cult of network
  95. * programmers, or more likely a simplification of what's going on for
  96. * people who haven't read winsock[2].h for themselves.
  97. */
  98. #if defined(_MSC_VER) && (_MSC_VER <= 1300)
  99. #include <winsock.h>
  100. #else
  101. #include <winsock2.h>
  102. #include <ws2tcpip.h>
  103. #endif
  104. #endif
  105. #endif
  106. #ifdef MS_WINDOWS
  107. #include <io.h>
  108. #include <process.h>
  109. #include <direct.h>
  110. #include <windows.h>
  111. #define snprintf _snprintf
  112. #endif
  113. #include "crypto.h"
  114. #include "tortls.h"
  115. #include "log.h"
  116. #include "compat.h"
  117. #include "container.h"
  118. #include "util.h"
  119. #include "torgzip.h"
  120. #include <event.h>
  121. /* These signals are defined to help control_signal_act work.
  122. */
  123. #ifndef SIGHUP
  124. #define SIGHUP 1
  125. #endif
  126. #ifndef SIGINT
  127. #define SIGINT 2
  128. #endif
  129. #ifndef SIGUSR1
  130. #define SIGUSR1 10
  131. #endif
  132. #ifndef SIGUSR2
  133. #define SIGUSR2 12
  134. #endif
  135. #ifndef SIGTERM
  136. #define SIGTERM 15
  137. #endif
  138. /* Controller signals start at a high number so we don't
  139. * conflict with system-defined signals. */
  140. #define SIGNEWNYM 129
  141. #define SIGCLEARDNSCACHE 130
  142. #if (SIZEOF_CELL_T != 0)
  143. /* On Irix, stdlib.h defines a cell_t type, so we need to make sure
  144. * that our stuff always calls cell_t something different. */
  145. #define cell_t tor_cell_t
  146. #endif
  147. /** Undefine this when it's time to stop generating v1 directories. */
  148. // #define FULL_V1_DIRECTORIES
  149. /** Undefine this when it's time to stop includeing bandwidth info in router
  150. * descriptors. */
  151. #define INCLUDE_BW_INFO_IN_ROUTERDESCS
  152. /** Length of longest allowable configured nickname. */
  153. #define MAX_NICKNAME_LEN 19
  154. /** Length of a router identity encoded as a hexadecimal digest, plus
  155. * possible dollar sign. */
  156. #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
  157. /** Maximum length of verbose router identifier: dollar sign, hex ID digest,
  158. * equal sign or tilde, nickname. */
  159. #define MAX_VERBOSE_NICKNAME_LEN (1+HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN)
  160. /** Maximum size, in bytes, for resized buffers. */
  161. #define MAX_BUF_SIZE ((1<<24)-1) /* 16MB-1 */
  162. /** Maximum size, in bytes, for any directory object that we've downloaded. */
  163. #define MAX_DIR_DL_SIZE MAX_BUF_SIZE
  164. /** For http parsing: Maximum number of bytes we'll accept in the headers
  165. * of an HTTP request or response. */
  166. #define MAX_HEADERS_SIZE 50000
  167. /** Maximum size, in bytes, for any directory object that we're accepting
  168. * as an upload. */
  169. #define MAX_DIR_UL_SIZE 500000
  170. /** How long do we keep DNS cache entries before purging them (regardless of
  171. * their TTL)? */
  172. #define MAX_DNS_ENTRY_AGE (30*60)
  173. /** How long do we cache/tell clients to cache DNS records when no TTL is
  174. * known? */
  175. #define DEFAULT_DNS_TTL (30*60)
  176. /** How long can a TTL be before we stop believing it? */
  177. #define MAX_DNS_TTL (3*60*60)
  178. /** How small can a TTL be before we stop believing it? */
  179. #define MIN_DNS_TTL (60)
  180. /** How often do we rotate onion keys? */
  181. #define MIN_ONION_KEY_LIFETIME (7*24*60*60)
  182. /** How often do we rotate TLS contexts? */
  183. #define MAX_SSL_KEY_LIFETIME (2*60*60)
  184. /** How old do we allow a router to get before removing it
  185. * from the router list? In seconds. */
  186. #define ROUTER_MAX_AGE (60*60*48)
  187. /** How old can a router get before we (as a server) will no longer
  188. * consider it live? In seconds. */
  189. #define ROUTER_MAX_AGE_TO_PUBLISH (60*60*20)
  190. /** How old do we let a saved descriptor get before force-removing it? */
  191. #define OLD_ROUTER_DESC_MAX_AGE (60*60*24*5)
  192. /** How old do we let a networkstatus get before ignoring it? */
  193. #define NETWORKSTATUS_MAX_AGE (60*60*24)
  194. /** Possible rules for generating circuit IDs on an OR connection. */
  195. typedef enum {
  196. CIRC_ID_TYPE_LOWER=0, /**< Pick from 0..1<<15-1. */
  197. CIRC_ID_TYPE_HIGHER=1, /**< Pick from 1<<15..1<<16-1. */
  198. /** The other side of a connection is an OP: never create circuits to it,
  199. * and let it use any circuit ID it wants. */
  200. CIRC_ID_TYPE_NEITHER=2
  201. } circ_id_type_t;
  202. #define _CONN_TYPE_MIN 3
  203. /** Type for sockets listening for OR connections. */
  204. #define CONN_TYPE_OR_LISTENER 3
  205. /** A bidirectional TLS connection transmitting a sequence of cells.
  206. * May be from an OR to an OR, or from an OP to an OR. */
  207. #define CONN_TYPE_OR 4
  208. /** A TCP connection from an onion router to a stream's destination. */
  209. #define CONN_TYPE_EXIT 5
  210. /** Type for sockets listening for SOCKS connections. */
  211. #define CONN_TYPE_AP_LISTENER 6
  212. /** A SOCKS proxy connection from the user application to the onion
  213. * proxy. */
  214. #define CONN_TYPE_AP 7
  215. /** Type for sockets listening for HTTP connections to the directory server. */
  216. #define CONN_TYPE_DIR_LISTENER 8
  217. /** Type for HTTP connections to the directory server. */
  218. #define CONN_TYPE_DIR 9
  219. /** Connection from the main process to a CPU worker process. */
  220. #define CONN_TYPE_CPUWORKER 10
  221. /** Type for listening for connections from user interface process. */
  222. #define CONN_TYPE_CONTROL_LISTENER 11
  223. /** Type for connections from user interface process. */
  224. #define CONN_TYPE_CONTROL 12
  225. /** Type for sockets listening for transparent connections redirected by pf or
  226. * netfilter. */
  227. #define CONN_TYPE_AP_TRANS_LISTENER 13
  228. /** Type for sockets listening for transparent connections redirected by
  229. * natd. */
  230. #define CONN_TYPE_AP_NATD_LISTENER 14
  231. /** Type for sockets listening for DNS requests. */
  232. #define CONN_TYPE_AP_DNS_LISTENER 15
  233. #define _CONN_TYPE_MAX 15
  234. /* !!!! If _CONN_TYPE_MAX is ever over 15, we must grow the type field in
  235. * connection_t. */
  236. #define CONN_IS_EDGE(x) \
  237. ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  238. /** State for any listener connection. */
  239. #define LISTENER_STATE_READY 0
  240. #define _CPUWORKER_STATE_MIN 1
  241. /** State for a connection to a cpuworker process that's idle. */
  242. #define CPUWORKER_STATE_IDLE 1
  243. /** State for a connection to a cpuworker process that's processing a
  244. * handshake. */
  245. #define CPUWORKER_STATE_BUSY_ONION 2
  246. #define _CPUWORKER_STATE_MAX 2
  247. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  248. #define _OR_CONN_STATE_MIN 1
  249. /** State for a connection to an OR: waiting for connect() to finish. */
  250. #define OR_CONN_STATE_CONNECTING 1
  251. /** State for a connection to an OR: waiting for proxy command to flush. */
  252. #define OR_CONN_STATE_PROXY_FLUSHING 2
  253. /** State for a connection to an OR: waiting for proxy response. */
  254. #define OR_CONN_STATE_PROXY_READING 3
  255. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  256. #define OR_CONN_STATE_HANDSHAKING 4
  257. /** State for a connection to an OR: Ready to send/receive cells. */
  258. #define OR_CONN_STATE_OPEN 5
  259. #define _OR_CONN_STATE_MAX 5
  260. #define _EXIT_CONN_STATE_MIN 1
  261. /** State for an exit connection: waiting for response from dns farm. */
  262. #define EXIT_CONN_STATE_RESOLVING 1
  263. /** State for an exit connection: waiting for connect() to finish. */
  264. #define EXIT_CONN_STATE_CONNECTING 2
  265. /** State for an exit connection: open and ready to transmit data. */
  266. #define EXIT_CONN_STATE_OPEN 3
  267. /** State for an exit connection: waiting to be removed. */
  268. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  269. #define _EXIT_CONN_STATE_MAX 4
  270. /* The AP state values must be disjoint from the EXIT state values. */
  271. #define _AP_CONN_STATE_MIN 5
  272. /** State for a SOCKS connection: waiting for SOCKS request. */
  273. #define AP_CONN_STATE_SOCKS_WAIT 5
  274. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  275. * rendezvous descriptor. */
  276. #define AP_CONN_STATE_RENDDESC_WAIT 6
  277. /** The controller will attach this connection to a circuit; it isn't our
  278. * job to do so. */
  279. #define AP_CONN_STATE_CONTROLLER_WAIT 7
  280. /** State for a SOCKS connection: waiting for a completed circuit. */
  281. #define AP_CONN_STATE_CIRCUIT_WAIT 8
  282. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  283. #define AP_CONN_STATE_CONNECT_WAIT 9
  284. /** State for a SOCKS connection: sent RESOLVE, waiting for RESOLVED. */
  285. #define AP_CONN_STATE_RESOLVE_WAIT 10
  286. /** State for a SOCKS connection: ready to send and receive. */
  287. #define AP_CONN_STATE_OPEN 11
  288. /** State for a transparent natd connection: waiting for original
  289. * destination. */
  290. #define AP_CONN_STATE_NATD_WAIT 12
  291. #define _AP_CONN_STATE_MAX 12
  292. /** True iff the AP_CONN_STATE_* value <b>s</b> means that the corresponding
  293. * edge connection is not attached to any circuit. */
  294. #define AP_CONN_STATE_IS_UNATTACHED(s) \
  295. ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)
  296. #define _DIR_CONN_STATE_MIN 1
  297. /** State for connection to directory server: waiting for connect(). */
  298. #define DIR_CONN_STATE_CONNECTING 1
  299. /** State for connection to directory server: sending HTTP request. */
  300. #define DIR_CONN_STATE_CLIENT_SENDING 2
  301. /** State for connection to directory server: reading HTTP response. */
  302. #define DIR_CONN_STATE_CLIENT_READING 3
  303. /** State for connection to directory server: happy and finished. */
  304. #define DIR_CONN_STATE_CLIENT_FINISHED 4
  305. /** State for connection at directory server: waiting for HTTP request. */
  306. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
  307. /** State for connection at directory server: sending HTTP response. */
  308. #define DIR_CONN_STATE_SERVER_WRITING 6
  309. #define _DIR_CONN_STATE_MAX 6
  310. /** True iff the purpose of <b>conn</b> means that it's a server-side
  311. * directory connection. */
  312. #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)
  313. #define _CONTROL_CONN_STATE_MIN 1
  314. /** State for a control connection: Authenticated and accepting v1 commands. */
  315. #define CONTROL_CONN_STATE_OPEN 1
  316. /** State for a control connection: Waiting for authentication; speaking
  317. * protocol v1. */
  318. #define CONTROL_CONN_STATE_NEEDAUTH 2
  319. #define _CONTROL_CONN_STATE_MAX 2
  320. #define _DIR_PURPOSE_MIN 1
  321. /** A connection to a directory server: download a directory. */
  322. #define DIR_PURPOSE_FETCH_DIR 1
  323. /** A connection to a directory server: download just the list
  324. * of running routers. */
  325. #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
  326. /** A connection to a directory server: download a rendezvous
  327. * descriptor. */
  328. #define DIR_PURPOSE_FETCH_RENDDESC 3
  329. /** A connection to a directory server: set after a rendezvous
  330. * descriptor is downloaded. */
  331. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
  332. /** A connection to a directory server: download one or more network-status
  333. * objects */
  334. #define DIR_PURPOSE_FETCH_NETWORKSTATUS 5
  335. /** A connection to a directory server: download one or more server
  336. * descriptors. */
  337. #define DIR_PURPOSE_FETCH_SERVERDESC 6
  338. /** A connection to a directory server: download one or more extra-info
  339. * documents. */
  340. #define DIR_PURPOSE_FETCH_EXTRAINFO 7
  341. /** A connection to a directory server: upload a server descriptor. */
  342. #define DIR_PURPOSE_UPLOAD_DIR 8
  343. /** A connection to a directory server: upload a rendezvous
  344. * descriptor. */
  345. #define DIR_PURPOSE_UPLOAD_RENDDESC 9
  346. /** A connection to a directory server: upload a v3 networkstatus vote. */
  347. #define DIR_PURPOSE_UPLOAD_VOTE 10
  348. /** A connection to a directory server: fetch a v3 networkstatus vote. */
  349. #define DIR_PURPOSE_FETCH_VOTE 11
  350. /** A connection to a directory server: upload a v3 consensus signature */
  351. #define DIR_PURPOSE_UPLOAD_SIGNATURES 12
  352. /** Purpose for connection at a directory server. */
  353. #define DIR_PURPOSE_SERVER 13
  354. #define _DIR_PURPOSE_MAX 13
  355. #define _EXIT_PURPOSE_MIN 1
  356. /** This exit stream wants to do an ordinary connect. */
  357. #define EXIT_PURPOSE_CONNECT 1
  358. /** This exit stream wants to do a resolve (either normal or reverse). */
  359. #define EXIT_PURPOSE_RESOLVE 2
  360. #define _EXIT_PURPOSE_MAX 2
  361. /* !!!! If any connection purpose is ever over over 15, we must grow the type
  362. * field in connection_t. */
  363. /** Circuit state: I'm the origin, still haven't done all my handshakes. */
  364. #define CIRCUIT_STATE_BUILDING 0
  365. /** Circuit state: Waiting to process the onionskin. */
  366. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  367. /** Circuit state: I'd like to deliver a create, but my n_conn is still
  368. * connecting. */
  369. #define CIRCUIT_STATE_OR_WAIT 2
  370. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  371. #define CIRCUIT_STATE_OPEN 3
  372. #define _CIRCUIT_PURPOSE_MIN 1
  373. /* these circuits were initiated elsewhere */
  374. #define _CIRCUIT_PURPOSE_OR_MIN 1
  375. /** OR-side circuit purpose: normal circuit, at OR. */
  376. #define CIRCUIT_PURPOSE_OR 1
  377. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  378. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  379. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  380. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  381. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  382. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  383. #define _CIRCUIT_PURPOSE_OR_MAX 4
  384. /* these circuits originate at this node */
  385. /* here's how circ client-side purposes work:
  386. * normal circuits are C_GENERAL.
  387. * circuits that are c_introducing are either on their way to
  388. * becoming open, or they are open and waiting for a
  389. * suitable rendcirc before they send the intro.
  390. * circuits that are c_introduce_ack_wait have sent the intro,
  391. * but haven't gotten a response yet.
  392. * circuits that are c_establish_rend are either on their way
  393. * to becoming open, or they are open and have sent the
  394. * establish_rendezvous cell but haven't received an ack.
  395. * circuits that are c_rend_ready are open and have received a
  396. * rend ack, but haven't heard from bob yet. if they have a
  397. * buildstate->pending_final_cpath then they're expecting a
  398. * cell from bob, else they're not.
  399. * circuits that are c_rend_ready_intro_acked are open, and
  400. * some intro circ has sent its intro and received an ack.
  401. * circuits that are c_rend_joined are open, have heard from
  402. * bob, and are talking to him.
  403. */
  404. /** Client-side circuit purpose: Normal circuit, with cpath. */
  405. #define CIRCUIT_PURPOSE_C_GENERAL 5
  406. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  407. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  408. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point,
  409. * waiting for ACK/NAK. */
  410. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  411. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  412. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  413. /** Client-side circuit purpose: at Alice, waiting for ack. */
  414. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  415. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  416. #define CIRCUIT_PURPOSE_C_REND_READY 10
  417. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  418. * has been acknowledged. */
  419. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  420. /** Client-side circuit purpose: at Alice, rendezvous established. */
  421. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  422. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  423. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  424. /** Hidden-service-side circuit purpose: at Bob, successfully established
  425. * intro. */
  426. #define CIRCUIT_PURPOSE_S_INTRO 14
  427. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  428. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  429. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  430. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  431. /** A testing circuit; not meant to be used for actual traffic. */
  432. #define CIRCUIT_PURPOSE_TESTING 17
  433. /** A controller made this circuit and Tor should not use it. */
  434. #define CIRCUIT_PURPOSE_CONTROLLER 18
  435. #define _CIRCUIT_PURPOSE_MAX 18
  436. /** True iff the circuit purpose <b>p</b> is for a circuit that
  437. * originated at this node. */
  438. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  439. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  440. /** How many circuits do we want simultaneously in-progress to handle
  441. * a given stream? */
  442. #define MIN_CIRCUITS_HANDLING_STREAM 2
  443. #define RELAY_COMMAND_BEGIN 1
  444. #define RELAY_COMMAND_DATA 2
  445. #define RELAY_COMMAND_END 3
  446. #define RELAY_COMMAND_CONNECTED 4
  447. #define RELAY_COMMAND_SENDME 5
  448. #define RELAY_COMMAND_EXTEND 6
  449. #define RELAY_COMMAND_EXTENDED 7
  450. #define RELAY_COMMAND_TRUNCATE 8
  451. #define RELAY_COMMAND_TRUNCATED 9
  452. #define RELAY_COMMAND_DROP 10
  453. #define RELAY_COMMAND_RESOLVE 11
  454. #define RELAY_COMMAND_RESOLVED 12
  455. #define RELAY_COMMAND_BEGIN_DIR 13
  456. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  457. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  458. #define RELAY_COMMAND_INTRODUCE1 34
  459. #define RELAY_COMMAND_INTRODUCE2 35
  460. #define RELAY_COMMAND_RENDEZVOUS1 36
  461. #define RELAY_COMMAND_RENDEZVOUS2 37
  462. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  463. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  464. #define RELAY_COMMAND_INTRODUCE_ACK 40
  465. /* Reasons why an OR connection is closed */
  466. #define END_OR_CONN_REASON_DONE 1
  467. #define END_OR_CONN_REASON_TCP_REFUSED 2
  468. #define END_OR_CONN_REASON_OR_IDENTITY 3
  469. #define END_OR_CONN_REASON_TLS_CONNRESET 4 /* tls connection reset by peer */
  470. #define END_OR_CONN_REASON_TLS_TIMEOUT 5
  471. #define END_OR_CONN_REASON_TLS_NO_ROUTE 6 /* no route to host/net */
  472. #define END_OR_CONN_REASON_TLS_IO_ERROR 7 /* tls read/write error */
  473. #define END_OR_CONN_REASON_TLS_MISC 8
  474. /* Reasons why we (or a remote OR) might close a stream. See tor-spec.txt for
  475. * documentation of these. */
  476. #define END_STREAM_REASON_MISC 1
  477. #define END_STREAM_REASON_RESOLVEFAILED 2
  478. #define END_STREAM_REASON_CONNECTREFUSED 3
  479. #define END_STREAM_REASON_EXITPOLICY 4
  480. #define END_STREAM_REASON_DESTROY 5
  481. #define END_STREAM_REASON_DONE 6
  482. #define END_STREAM_REASON_TIMEOUT 7
  483. /* 8 is unallocated for historical reasons. */
  484. #define END_STREAM_REASON_HIBERNATING 9
  485. #define END_STREAM_REASON_INTERNAL 10
  486. #define END_STREAM_REASON_RESOURCELIMIT 11
  487. #define END_STREAM_REASON_CONNRESET 12
  488. #define END_STREAM_REASON_TORPROTOCOL 13
  489. #define END_STREAM_REASON_NOTDIRECTORY 14
  490. /* These high-numbered end reasons are not part of the official spec,
  491. * and are not intended to be put in relay end cells. They are here
  492. * to be more informative when sending back socks replies to the
  493. * application. */
  494. /* XXXX 256 is no longer used; feel free to reuse it. */
  495. /** We were unable to attach the connection to any circuit at all. */
  496. /* XXXX the ways we use this one don't make a lot of sense. */
  497. #define END_STREAM_REASON_CANT_ATTACH 257
  498. /** We can't connect to any directories at all, so we killed our streams
  499. * before they can time out. */
  500. #define END_STREAM_REASON_NET_UNREACHABLE 258
  501. /** This is a SOCKS connection, and the client used (or misused) the SOCKS
  502. * protocol in a way we couldn't handle. */
  503. #define END_STREAM_REASON_SOCKSPROTOCOL 259
  504. /** This is a transparent proxy connection, but we can't extract the original
  505. * target address:port. */
  506. #define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
  507. /** This is a connection on the NATD port, and the destination IP:Port was
  508. * either ill-formed or out-of-range. */
  509. #define END_STREAM_REASON_INVALID_NATD_DEST 261
  510. /** Bitwise-and this value with endreason to mask out all flags. */
  511. #define END_STREAM_REASON_MASK 511
  512. /** Bitwise-or this with the argument to control_event_stream_status
  513. * to indicate that the reason came from an END cell. */
  514. #define END_STREAM_REASON_FLAG_REMOTE 512
  515. /** Bitwise-or this with the argument to control_event_stream_status
  516. * to indicate that we already sent a CLOSED stream event. */
  517. #define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED 1024
  518. /** Bitwise-or this with endreason to indicate that we already sent
  519. * a socks reply, and no further reply needs to be sent from
  520. * connection_mark_unattached_ap(). */
  521. #define END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED 2048
  522. /** Reason for remapping an AP connection's address: we have a cached
  523. * answer. */
  524. #define REMAP_STREAM_SOURCE_CACHE 1
  525. /** Reason for remapping an AP connection's address: the exit node told us an
  526. * answer. */
  527. #define REMAP_STREAM_SOURCE_EXIT 2
  528. /* 'type' values to use in RESOLVED cells. Specified in tor-spec.txt. */
  529. #define RESOLVED_TYPE_HOSTNAME 0
  530. #define RESOLVED_TYPE_IPV4 4
  531. #define RESOLVED_TYPE_IPV6 6
  532. #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
  533. #define RESOLVED_TYPE_ERROR 0xF1
  534. /* Negative reasons are internal: we never send them in a DESTROY or TRUNCATE
  535. * call; they only go to the controller for tracking */
  536. /** We couldn't build a path for this circuit. */
  537. #define END_CIRC_REASON_NOPATH -2
  538. /** Catch-all "other" reason for closing origin circuits. */
  539. #define END_CIRC_AT_ORIGIN -1
  540. /* Reasons why we (or a remote OR) might close a circuit. See tor-spec.txt for
  541. * documentation of these. */
  542. #define _END_CIRC_REASON_MIN 0
  543. #define END_CIRC_REASON_NONE 0
  544. #define END_CIRC_REASON_TORPROTOCOL 1
  545. #define END_CIRC_REASON_INTERNAL 2
  546. #define END_CIRC_REASON_REQUESTED 3
  547. #define END_CIRC_REASON_HIBERNATING 4
  548. #define END_CIRC_REASON_RESOURCELIMIT 5
  549. #define END_CIRC_REASON_CONNECTFAILED 6
  550. #define END_CIRC_REASON_OR_IDENTITY 7
  551. #define END_CIRC_REASON_OR_CONN_CLOSED 8
  552. #define END_CIRC_REASON_FINISHED 9
  553. #define END_CIRC_REASON_TIMEOUT 10
  554. #define END_CIRC_REASON_DESTROYED 11
  555. #define END_CIRC_REASON_NOSUCHSERVICE 12
  556. #define _END_CIRC_REASON_MAX 12
  557. /** Bitwise-OR this with the argument to circuit_mark_for_close() or
  558. * control_event_circuit_status() to indicate that the reason was
  559. * passed through from a destroy or truncate cell. */
  560. #define END_CIRC_REASON_FLAG_REMOTE 512
  561. /** Length of 'y' portion of 'y.onion' URL. */
  562. #define REND_SERVICE_ID_LEN 16
  563. #define CELL_DIRECTION_IN 1
  564. #define CELL_DIRECTION_OUT 2
  565. #ifdef TOR_PERF
  566. #define CIRCWINDOW_START 10000
  567. #define CIRCWINDOW_INCREMENT 1000
  568. #define STREAMWINDOW_START 5000
  569. #define STREAMWINDOW_INCREMENT 500
  570. #else
  571. /** Initial value for both sides of a circuit transmission window when the
  572. * circuit is initialized. Measured in cells. */
  573. #define CIRCWINDOW_START 1000
  574. /** Amount to increment a circuit window when we get a circuit SENDME. */
  575. #define CIRCWINDOW_INCREMENT 100
  576. /** Initial value on both sides of a stream transmission window when the
  577. * stream is initialized. Measured in cells. */
  578. #define STREAMWINDOW_START 500
  579. /** Amount to increment a stream window when we get a stream SENDME. */
  580. #define STREAMWINDOW_INCREMENT 50
  581. #endif
  582. /* cell commands */
  583. #define CELL_PADDING 0
  584. #define CELL_CREATE 1
  585. #define CELL_CREATED 2
  586. #define CELL_RELAY 3
  587. #define CELL_DESTROY 4
  588. #define CELL_CREATE_FAST 5
  589. #define CELL_CREATED_FAST 6
  590. /** How long to test reachability before complaining to the user. */
  591. #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
  592. /** Legal characters in a nickname. */
  593. #define LEGAL_NICKNAME_CHARACTERS \
  594. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  595. /** Name to use in client TLS certificates if no nickname is given. */
  596. #define DEFAULT_CLIENT_NICKNAME "client"
  597. /** Number of bytes in a SOCKS4 header. */
  598. #define SOCKS4_NETWORK_LEN 8
  599. /** Specified SOCKS5 status codes. */
  600. typedef enum {
  601. SOCKS5_SUCCEEDED = 0x00,
  602. SOCKS5_GENERAL_ERROR = 0x01,
  603. SOCKS5_NOT_ALLOWED = 0x02,
  604. SOCKS5_NET_UNREACHABLE = 0x03,
  605. SOCKS5_HOST_UNREACHABLE = 0x04,
  606. SOCKS5_CONNECTION_REFUSED = 0x05,
  607. SOCKS5_TTL_EXPIRED = 0x06,
  608. SOCKS5_COMMAND_NOT_SUPPORTED = 0x07,
  609. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
  610. } socks5_reply_status_t;
  611. /*
  612. * Relay payload:
  613. * Relay command [1 byte]
  614. * Recognized [2 bytes]
  615. * Stream ID [2 bytes]
  616. * Partial SHA-1 [4 bytes]
  617. * Length [2 bytes]
  618. * Relay payload [498 bytes]
  619. */
  620. /** Number of bytes in a cell, minus cell header. */
  621. #define CELL_PAYLOAD_SIZE 509
  622. /** Number of bytes in a cell transmitted over the network. */
  623. #define CELL_NETWORK_SIZE 512
  624. /** Number of bytes in a relay cell's header (not including general cell
  625. * header). */
  626. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  627. /** Largest number of bytes that can fit in a relay cell payload. */
  628. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  629. typedef struct cell_t cell_t;
  630. /** Parsed onion routing cell. All communication between nodes
  631. * is via cells. */
  632. struct cell_t {
  633. uint16_t circ_id; /**< Circuit which received the cell. */
  634. uint8_t command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  635. * or DESTROY. */
  636. char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  637. };
  638. typedef struct packed_cell_t packed_cell_t;
  639. /** A cell as packed for writing to the network. */
  640. struct packed_cell_t {
  641. struct packed_cell_t *next; /**< Next cell queued on this circuit. */
  642. char body[CELL_NETWORK_SIZE]; /**< Cell as packed for network. */
  643. };
  644. /** A queue of cells on a circuit, waiting to be added to the
  645. * or_connection_t's outbuf. */
  646. typedef struct cell_queue_t {
  647. packed_cell_t *head; /**< The first cell, or NULL if the queue is empty. */
  648. packed_cell_t *tail; /**< The last cell, or NULL if the queue is empty. */
  649. int n; /**< The number of cells in the queue. */
  650. } cell_queue_t;
  651. /** Beginning of a RELAY cell payload. */
  652. typedef struct {
  653. uint8_t command; /**< The end-to-end relay command. */
  654. uint16_t recognized; /**< Used to tell whether cell is for us. */
  655. uint16_t stream_id; /**< Which stream is this cell associated with? */
  656. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  657. uint16_t length; /**< How long is the payload body? */
  658. } relay_header_t;
  659. typedef struct buf_t buf_t;
  660. typedef struct socks_request_t socks_request_t;
  661. /* Values for connection_t.magic: used to make sure that downcasts (casts from
  662. * connection_t to foo_connection_t) are safe. */
  663. #define BASE_CONNECTION_MAGIC 0x7C3C304Eu
  664. #define OR_CONNECTION_MAGIC 0x7D31FF03u
  665. #define EDGE_CONNECTION_MAGIC 0xF0374013u
  666. #define DIR_CONNECTION_MAGIC 0x9988ffeeu
  667. #define CONTROL_CONNECTION_MAGIC 0x8abc765du
  668. /** Description of a connection to another host or process, and associated
  669. * data.
  670. *
  671. * A connection is named based on what it's connected to -- an "OR
  672. * connection" has a Tor node on the other end, an "exit
  673. * connection" has a website or other server on the other end, and an
  674. * "AP connection" has an application proxy (and thus a user) on the
  675. * other end.
  676. *
  677. * Every connection has a type and a state. Connections never change
  678. * their type, but can go through many state changes in their lifetime.
  679. *
  680. * Every connection has two associated input and output buffers.
  681. * Listeners don't use them. For non-listener connections, incoming
  682. * data is appended to conn->inbuf, and outgoing data is taken from
  683. * conn->outbuf. Connections differ primarily in the functions called
  684. * to fill and drain these buffers.
  685. */
  686. typedef struct connection_t {
  687. uint32_t magic; /**< For memory debugging: must equal one of
  688. * *_CONNECTION_MAGIC. */
  689. uint8_t state; /**< Current state of this connection. */
  690. uint8_t type:4; /**< What kind of connection is this? */
  691. uint8_t purpose:4; /**< Only used for DIR and EXIT types currently. */
  692. /* The next fields are all one-bit booleans. Some are only applicable to
  693. * connection subtypes, but we hold them here anyway, to save space.
  694. * (Currently, they all fit into a single byte. If they ever need more than
  695. * one byte, we can shave some bits off type, state, and purpose above, none
  696. * of which is ever over 31.)
  697. */
  698. unsigned read_blocked_on_bw:1; /**< Boolean: should we start reading again
  699. * once the bandwidth throttler allows it? */
  700. unsigned write_blocked_on_bw:1; /**< Boolean: should we start writing again
  701. * once the bandwidth throttler allows reads? */
  702. unsigned hold_open_until_flushed:1; /**< Despite this connection's being
  703. * marked for close, do we flush it
  704. * before closing it? */
  705. unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
  706. * conn? */
  707. unsigned int edge_has_sent_end:1; /**< For debugging; only used on edge
  708. * connections. Set once we've set the stream end,
  709. * and check in connection_about_to_close_connection().
  710. */
  711. /** Edge connections only: true if we've blocked writing until the
  712. * circuit has fewer queued cells. */
  713. unsigned int edge_blocked_on_circ:1;
  714. /** Used for OR conns that shouldn't get any new circs attached to them. */
  715. unsigned int or_is_obsolete:1;
  716. /** For AP connections only. If 1, and we fail to reach the chosen exit,
  717. * stop requiring it. */
  718. unsigned int chosen_exit_optional:1;
  719. /* For linked connections:
  720. */
  721. unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
  722. /** True iff we'd like to be notified about read events from the
  723. * linked conn. */
  724. unsigned int reading_from_linked_conn:1;
  725. /** True iff we're willing to write to the linked conn. */
  726. unsigned int writing_to_linked_conn:1;
  727. /** True iff we're currently able to read on the linked conn, and our
  728. * read_event should be made active with libevent. */
  729. unsigned int active_on_link:1;
  730. int s; /**< Our socket; -1 if this connection is closed, or has no
  731. * socket. */
  732. int conn_array_index; /**< Index into the global connection array. */
  733. struct event *read_event; /**< Libevent event structure. */
  734. struct event *write_event; /**< Libevent event structure. */
  735. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  736. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  737. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  738. * outbuf? */
  739. time_t timestamp_lastread; /**< When was the last time libevent said we could
  740. * read? */
  741. time_t timestamp_lastwritten; /**< When was the last time libevent said we
  742. * could write? */
  743. time_t timestamp_created; /**< When was this connection_t created? */
  744. /* XXXX020 make this ipv6-capable */
  745. int socket_family; /**< Address family of this connection's socket. Usually
  746. * AF_INET, but it can also be AF_UNIX, or in the future
  747. * AF_INET6 */
  748. uint32_t addr; /**< IP of the other side of the connection; used to identify
  749. * routers, along with port. */
  750. uint16_t port; /**< If non-zero, port on the other end
  751. * of the connection. */
  752. uint16_t marked_for_close; /**< Should we close this conn on the next
  753. * iteration of the main loop? (If true, holds
  754. * the line number where this connection was
  755. * marked.) */
  756. const char *marked_for_close_file; /**< For debugging: in which file were
  757. * we marked for close? */
  758. char *address; /**< FQDN (or IP) of the guy on the other end.
  759. * strdup into this, because free_connection frees it. */
  760. /** Annother connection that's connected to this one in lieu of a socket. */
  761. struct connection_t *linked_conn;
  762. /* XXXX020 move this into a subtype!!! */
  763. struct evdns_server_port *dns_server_port;
  764. } connection_t;
  765. /** Subtype of connection_t for an "OR connection" -- that is, one that speaks
  766. * cells over TLS. */
  767. typedef struct or_connection_t {
  768. connection_t _base;
  769. /** Hash of the public RSA key for the other side's identity key, or zeroes
  770. * if the other side hasn't shown us a valid identity key. */
  771. char identity_digest[DIGEST_LEN];
  772. char *nickname; /**< Nickname of OR on other side (if any). */
  773. tor_tls_t *tls; /**< TLS connection state. */
  774. int tls_error; /**< Last tor_tls error code. */
  775. /** When we last used this conn for any client traffic. If not
  776. * recent, we can rate limit it further. */
  777. time_t client_used;
  778. circ_id_type_t circ_id_type:2; /**< When we send CREATE cells along this
  779. * connection, which half of the space should
  780. * we use? */
  781. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  782. * this connection? This is always in the
  783. * range 0..1<<15-1. */
  784. time_t timestamp_lastempty; /**< When was the outbuf last completely empty?*/
  785. /* bandwidth* and read_bucket only used by ORs in OPEN state: */
  786. int bandwidthrate; /**< Bytes/s added to the bucket. (OPEN ORs only.) */
  787. int bandwidthburst; /**< Max bucket size for this conn. (OPEN ORs only.) */
  788. int read_bucket; /**< When this hits 0, stop receiving. Every second we
  789. * add 'bandwidthrate' to this, capping it at
  790. * bandwidthburst. (OPEN ORs only) */
  791. int n_circuits; /**< How many circuits use this connection as p_conn or
  792. * n_conn ? */
  793. /** Double-linked ring of circuits with queued cells waiting for room to
  794. * free up on this connection's outbuf. Every time we pull cells from a
  795. * circuit, we advance this pointer to the next circuit in the ring. */
  796. struct circuit_t *active_circuits;
  797. struct or_connection_t *next_with_same_id; /**< Next connection with same
  798. * identity digest as this one. */
  799. } or_connection_t;
  800. /** Subtype of connection_t for an "edge connection" -- that is, a socks (ap)
  801. * connection, or an exit. */
  802. typedef struct edge_connection_t {
  803. connection_t _base;
  804. struct edge_connection_t *next_stream; /**< Points to the next stream at this
  805. * edge, if any */
  806. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  807. * this conn exits at. */
  808. int package_window; /**< How many more relay cells can I send into the
  809. * circuit? */
  810. int deliver_window; /**< How many more relay cells can end at me? */
  811. /** Nickname of planned exit node -- used with .exit support. */
  812. char *chosen_exit_name;
  813. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  814. * only.) */
  815. struct circuit_t *on_circuit; /**< The circuit (if any) that this edge
  816. * connection is using. */
  817. uint32_t address_ttl; /**< TTL for address-to-addr mapping on exit
  818. * connection. Exit connections only. */
  819. uint16_t stream_id; /**< The stream ID used for this edge connection on its
  820. * circuit */
  821. /** The reason why this connection is closing; passed to the controller. */
  822. uint16_t end_reason;
  823. /** Quasi-global identifier for this connection; used for control.c */
  824. /* XXXX NM This can get re-used after 2**32 streams */
  825. uint32_t global_identifier;
  826. /** Bytes read since last call to control_event_stream_bandwidth_used() */
  827. uint32_t n_read;
  828. /** Bytes written since last call to control_event_stream_bandwidth_used() */
  829. uint32_t n_written;
  830. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  831. * querying for? (AP only) */
  832. /** Number of times we've reassigned this application connection to
  833. * a new circuit. We keep track because the timeout is longer if we've
  834. * already retried several times. */
  835. uint8_t num_socks_retries;
  836. /** True iff this connection is for a dns request only. */
  837. unsigned int is_dns_request : 1;
  838. /** If this is a DNSPort connection, this field holds the pending DNS
  839. * request that we're going to try to answer. */
  840. struct evdns_server_request *dns_server_request;
  841. } edge_connection_t;
  842. /** Subtype of connection_t for an "directory connection" -- that is, an HTTP
  843. * connection to retrieve or serve directory material. */
  844. typedef struct dir_connection_t {
  845. connection_t _base;
  846. char *requested_resource; /**< Which 'resource' did we ask the directory
  847. * for? */
  848. unsigned int dirconn_direct:1; /**< Is this dirconn direct, or via Tor? */
  849. /* Used only for server sides of some dir connections, to implement
  850. * "spooling" of directory material to the outbuf. Otherwise, we'd have
  851. * to append everything to the outbuf in one enormous chunk. */
  852. /** What exactly are we spooling right now? */
  853. enum {
  854. DIR_SPOOL_NONE=0, DIR_SPOOL_SERVER_BY_DIGEST, DIR_SPOOL_SERVER_BY_FP,
  855. DIR_SPOOL_EXTRA_BY_DIGEST, DIR_SPOOL_EXTRA_BY_FP,
  856. DIR_SPOOL_CACHED_DIR, DIR_SPOOL_NETWORKSTATUS
  857. } dir_spool_src : 3;
  858. /** If we're fetching descriptors, what router purpose shall we assign
  859. * to them? */
  860. uint8_t router_purpose;
  861. /** List of fingerprints for networkstatuses or desriptors to be spooled. */
  862. smartlist_t *fingerprint_stack;
  863. /** A cached_dir_t object that we're currently spooling out */
  864. struct cached_dir_t *cached_dir;
  865. /** The current offset into cached_dir. */
  866. off_t cached_dir_offset;
  867. /** The zlib object doing on-the-fly compression for spooled data. */
  868. tor_zlib_state_t *zlib_state;
  869. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  870. * querying for? */
  871. char identity_digest[DIGEST_LEN]; /**< Hash of the public RSA key for
  872. * the directory server's signing key. */
  873. } dir_connection_t;
  874. /** Subtype of connection_t for an connection to a controller. */
  875. typedef struct control_connection_t {
  876. connection_t _base;
  877. uint32_t event_mask; /**< Bitfield: which events does this controller
  878. * care about? */
  879. unsigned int use_long_names:1; /**< True if we should use long nicknames
  880. * on this (v1) connection. Only settable
  881. * via v1 controllers. */
  882. /** For control connections only. If set, we send extended info with control
  883. * events as appropriate. */
  884. unsigned int use_extended_events:1;
  885. /** True if we have sent a protocolinfo reply on this connection. */
  886. unsigned int have_sent_protocolinfo:1;
  887. uint32_t incoming_cmd_len;
  888. uint32_t incoming_cmd_cur_len;
  889. char *incoming_cmd;
  890. /* Used only by control v0 connections */
  891. uint16_t incoming_cmd_type;
  892. } control_connection_t;
  893. /** Cast a connection_t subtype pointer to a connection_t **/
  894. #define TO_CONN(c) (&(((c)->_base)))
  895. /** Helper macro: Given a pointer to to._base, of type from*, return &to. */
  896. #define DOWNCAST(to, ptr) ((to*)SUBTYPE_P(ptr, to, _base))
  897. /** Convert a connection_t* to an or_connection_t*; assert if the cast is
  898. * invalid. */
  899. static or_connection_t *TO_OR_CONN(connection_t *);
  900. /** Convert a connection_t* to a dir_connection_t*; assert if the cast is
  901. * invalid. */
  902. static dir_connection_t *TO_DIR_CONN(connection_t *);
  903. /** Convert a connection_t* to an edge_connection_t*; assert if the cast is
  904. * invalid. */
  905. static edge_connection_t *TO_EDGE_CONN(connection_t *);
  906. /** Convert a connection_t* to an control_connection_t*; assert if the cast is
  907. * invalid. */
  908. static control_connection_t *TO_CONTROL_CONN(connection_t *);
  909. static INLINE or_connection_t *TO_OR_CONN(connection_t *c)
  910. {
  911. tor_assert(c->magic == OR_CONNECTION_MAGIC);
  912. return DOWNCAST(or_connection_t, c);
  913. }
  914. static INLINE dir_connection_t *TO_DIR_CONN(connection_t *c)
  915. {
  916. tor_assert(c->magic == DIR_CONNECTION_MAGIC);
  917. return DOWNCAST(dir_connection_t, c);
  918. }
  919. static INLINE edge_connection_t *TO_EDGE_CONN(connection_t *c)
  920. {
  921. tor_assert(c->magic == EDGE_CONNECTION_MAGIC);
  922. return DOWNCAST(edge_connection_t, c);
  923. }
  924. static INLINE control_connection_t *TO_CONTROL_CONN(connection_t *c)
  925. {
  926. tor_assert(c->magic == CONTROL_CONNECTION_MAGIC);
  927. return DOWNCAST(control_connection_t, c);
  928. }
  929. typedef enum {
  930. ADDR_POLICY_ACCEPT=1,
  931. ADDR_POLICY_REJECT=2,
  932. } addr_policy_action_t;
  933. /** A linked list of policy rules */
  934. typedef struct addr_policy_t {
  935. addr_policy_action_t policy_type; /**< What to do when the policy matches.*/
  936. char *string; /**< String representation of this rule. */
  937. /* XXXX020 make this ipv6-capable */
  938. uint32_t addr; /**< Base address to accept or reject. */
  939. maskbits_t maskbits; /**< Accept/reject all addresses <b>a</b> such that the
  940. * first <b>maskbits</b> bits of <b>a</b> match
  941. * <b>addr</b>. */
  942. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  943. uint16_t prt_max; /**< Highest port number to accept/reject. */
  944. struct addr_policy_t *next; /**< Next rule in list. */
  945. } addr_policy_t;
  946. /** A cached_dir_t represents a cacheable directory object, along with its
  947. * compressed form. */
  948. typedef struct cached_dir_t {
  949. char *dir; /**< Contents of this object */
  950. char *dir_z; /**< Compressed contents of this object. */
  951. size_t dir_len; /**< Length of <b>dir</b> */
  952. size_t dir_z_len; /**< Length of <b>dir_z</b> */
  953. time_t published; /**< When was this object published */
  954. int refcnt; /**< Reference count for this cached_dir_t. */
  955. } cached_dir_t;
  956. /** Enum used to remember where a signed_descriptor_t is stored and how to
  957. * manage the memory for signed_descriptor_body. */
  958. typedef enum {
  959. /** The descriptor isn't stored on disk at all: the copy in memory is
  960. * canonical; the saved_offset field is meaningless. */
  961. SAVED_NOWHERE=0,
  962. /** The descriptor is stored in the cached_routers file: the
  963. * signed_descriptor_body is meaningless; the signed_descriptor_len and
  964. * saved_offset are used to index into the mmaped cache file. */
  965. SAVED_IN_CACHE,
  966. /** The descriptor is stored in the cached_routers.new file: the
  967. * signed_descriptor_body and saved_offset fields are both set. */
  968. /* FFFF (We could also mmap the file and grow the mmap as needed, or
  969. * lazy-load the descriptor text by using seek and read. We don't, for
  970. * now.)
  971. */
  972. SAVED_IN_JOURNAL
  973. } saved_location_t;
  974. /** Information about our plans for retrying downloads for a downloadable
  975. * object. */
  976. typedef struct download_status_t {
  977. time_t next_attempt_at; /**< When should we try downloading this descriptor
  978. * again? */
  979. uint8_t n_download_failures; /**< Number of failures trying to download the
  980. * most recent descriptor. */
  981. } download_status_t;
  982. /** Information need to cache an onion router's descriptor. */
  983. typedef struct signed_descriptor_t {
  984. /** Pointer to the raw server descriptor. Not necessarily NUL-terminated.
  985. * If saved_location is SAVED_IN_CACHE, this pointer is null. */
  986. char *signed_descriptor_body;
  987. /** Length of the server descriptor. */
  988. size_t signed_descriptor_len;
  989. /** Digest of the server descriptor, computed as specified in dir-spec.txt */
  990. char signed_descriptor_digest[DIGEST_LEN];
  991. /** Identity digest of the router. */
  992. char identity_digest[DIGEST_LEN];
  993. /** Declared publication time of the descriptor */
  994. time_t published_on;
  995. /** For routerdescs only: digest of the corresponding extrainfo. */
  996. char extra_info_digest[DIGEST_LEN];
  997. /** For routerdescs only: Status of downloading the corresponding
  998. * extrainfo. */
  999. download_status_t ei_dl_status;
  1000. /** Where is the descriptor saved? */
  1001. saved_location_t saved_location ;
  1002. /** If saved_location is SAVED_IN_CACHE or SAVED_IN_JOURNAL, the offset of
  1003. * this descriptor in the corresponding file. */
  1004. off_t saved_offset;
  1005. /* If true, we do not ever try to save this object in the cache. */
  1006. unsigned int do_not_cache : 1;
  1007. /* If true, this item is meant to represent an extrainfo. */
  1008. unsigned int is_extrainfo : 1;
  1009. } signed_descriptor_t;
  1010. /** Information about another onion router in the network. */
  1011. typedef struct {
  1012. signed_descriptor_t cache_info;
  1013. char *address; /**< Location of OR: either a hostname or an IP address. */
  1014. char *nickname; /**< Human-readable OR name. */
  1015. uint32_t addr; /**< IPv4 address of OR, in host order. */
  1016. uint16_t or_port; /**< Port for TLS connections. */
  1017. uint16_t dir_port; /**< Port for HTTP directory connections. */
  1018. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  1019. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  1020. char *platform; /**< What software/operating system is this OR using? */
  1021. /* link info */
  1022. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  1023. * bucket per second? */
  1024. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  1025. /** How many bytes/s is this router known to handle? */
  1026. uint32_t bandwidthcapacity;
  1027. addr_policy_t *exit_policy; /**< What streams will this OR permit
  1028. * to exit? */
  1029. long uptime; /**< How many seconds the router claims to have been up */
  1030. smartlist_t *declared_family; /**< Nicknames of router which this router
  1031. * claims are its family. */
  1032. char *contact_info; /**< Declared contact info for this router. */
  1033. unsigned int is_hibernating:1; /**< Whether the router claims to be
  1034. * hibernating */
  1035. unsigned int has_old_dnsworkers:1; /**< Whether the router is using
  1036. * dnsworker code. */
  1037. unsigned int caches_extra_info:1; /**< Whether the router caches and serves
  1038. * extrainfo documents. */
  1039. /* local info */
  1040. unsigned int is_running:1; /**< As far as we know, is this OR currently
  1041. * running? */
  1042. unsigned int is_valid:1; /**< Has a trusted dirserver validated this OR?
  1043. * (For Authdir: Have we validated this OR?)
  1044. */
  1045. unsigned int is_named:1; /**< Do we believe the nickname that this OR gives
  1046. * us? */
  1047. unsigned int is_fast:1; /** Do we think this is a fast OR? */
  1048. unsigned int is_stable:1; /** Do we think this is a stable OR? */
  1049. unsigned int is_possible_guard:1; /**< Do we think this is an OK guard? */
  1050. unsigned int is_exit:1; /**< Do we think this is an OK exit? */
  1051. unsigned int is_bad_exit:1; /**< Do we think this exit is censored, borked,
  1052. * or otherwise nasty? */
  1053. /** Tor can use this router for general positions in circuits. */
  1054. #define ROUTER_PURPOSE_GENERAL 0
  1055. /** Tor should avoid using this router for circuit-building. */
  1056. #define ROUTER_PURPOSE_CONTROLLER 1
  1057. /** Tor should use this router only for bridge positions in circuits. */
  1058. #define ROUTER_PURPOSE_BRIDGE 2
  1059. uint8_t purpose; /** What positions in a circuit is this router good for? */
  1060. /* The below items are used only by authdirservers for
  1061. * reachability testing. */
  1062. /** When was the last time we could reach this OR? */
  1063. time_t last_reachable;
  1064. /** When did we start testing reachability for this OR? */
  1065. time_t testing_since;
  1066. /** How many times has a descriptor been posted and we believed
  1067. * this router to be unreachable? We only actually warn on the third. */
  1068. int num_unreachable_notifications;
  1069. /** What position is this descriptor within routerlist->routers? -1 for
  1070. * none. */
  1071. int routerlist_index;
  1072. } routerinfo_t;
  1073. /** DOCDOC */
  1074. #define EXTRAINFO_PURPOSE_GENERAL -1
  1075. /** Information needed to keep and cache a signed extra-info document. */
  1076. typedef struct extrainfo_t {
  1077. signed_descriptor_t cache_info;
  1078. /** The router's nickname. */
  1079. char nickname[MAX_NICKNAME_LEN+1];
  1080. /** True iff we found the right key for this extra-info, verified the
  1081. * signature, and found it to be bad. */
  1082. unsigned int bad_sig : 1;
  1083. /** If present, we didn't have the right key to verify this extra-info,
  1084. * so this is a copy of the signature in the document. */
  1085. char *pending_sig;
  1086. /** Length of pending_sig. */
  1087. size_t pending_sig_len;
  1088. } extrainfo_t;
  1089. /** Contents of a single router entry in a network status object.
  1090. */
  1091. typedef struct routerstatus_t {
  1092. time_t published_on; /**< When was this router published? */
  1093. char nickname[MAX_NICKNAME_LEN+1]; /**< The nickname this router says it
  1094. * has. */
  1095. char identity_digest[DIGEST_LEN]; /**< Digest of the router's identity
  1096. * key. */
  1097. char descriptor_digest[DIGEST_LEN]; /**< Digest of the router's most recent
  1098. * descriptor. */
  1099. uint32_t addr; /**< IPv4 address for this router. */
  1100. uint16_t or_port; /**< OR port for this router. */
  1101. uint16_t dir_port; /**< Directory port for this router. */
  1102. unsigned int is_authority:1; /**< True iff this router is an authority. */
  1103. unsigned int is_exit:1; /**< True iff this router is a good exit. */
  1104. unsigned int is_stable:1; /**< True iff this router stays up a long time. */
  1105. unsigned int is_fast:1; /**< True iff this router has good bandwidth. */
  1106. unsigned int is_running:1; /**< True iff this router is up. */
  1107. unsigned int is_named:1; /**< True iff "nickname" belongs to this router. */
  1108. unsigned int is_valid:1; /**< True iff this router is validated. */
  1109. unsigned int is_v2_dir:1; /**< True iff this router can serve directory
  1110. * information with v2 of the directory
  1111. * protocol. (All directory caches cache v1
  1112. * directories.) */
  1113. unsigned int is_possible_guard:1; /**< True iff this router would be a good
  1114. * choice as an entry guard. */
  1115. unsigned int is_bad_exit:1; /**< True iff this node is a bad choice for
  1116. * an exit node. */
  1117. unsigned int is_bad_directory:1; /**< Do we think this directory is junky,
  1118. * underpowered, or otherwise useless? */
  1119. /** True iff we know version info for this router. (i.e., a "v" entry was
  1120. * included.) We'll replace all these with a big tor_version_t or a char[]
  1121. * if the number of traits we care about ever becomes incredibly big. */
  1122. unsigned int version_known:1;
  1123. /** True iff this router is a version that supports BEGIN_DIR cells. */
  1124. unsigned int version_supports_begindir:1;
  1125. /** True iff this router is a version that we can post extrainfo docs to. */
  1126. unsigned int version_supports_extrainfo_upload:1;
  1127. /** True if we, as a directory mirror, want to download the corresponding
  1128. * routerinfo from the authority who gave us this routerstatus. (That is,
  1129. * if we don't have the routerinfo, and if we haven't already tried to get it
  1130. * from this authority.)
  1131. */
  1132. unsigned int need_to_mirror:1;
  1133. } routerstatus_t;
  1134. /** Our "local" or combined view of the info from all networkstatus objects
  1135. * about a single router. */
  1136. typedef struct local_routerstatus_t {
  1137. /** What do we believe to be the case about this router? In this field,
  1138. * descriptor_digest represents the descriptor we would most like to use for
  1139. * this router. */
  1140. routerstatus_t status;
  1141. time_t last_dir_503_at; /**< When did this router last tell us that it
  1142. * was too busy to serve directory info? */
  1143. download_status_t dl_status;
  1144. unsigned int name_lookup_warned:1; /**< Have we warned the user for referring
  1145. * to this (unnamed) router by nickname?
  1146. */
  1147. } local_routerstatus_t;
  1148. /** How many times will we try to download a router's descriptor before giving
  1149. * up? */
  1150. #define MAX_ROUTERDESC_DOWNLOAD_FAILURES 8
  1151. /** Contents of a v2 (non-consensus, non-vote) network status object. */
  1152. typedef struct networkstatus_t {
  1153. /** When did we receive the network-status document? */
  1154. time_t received_on;
  1155. /** What was the digest of the document? */
  1156. char networkstatus_digest[DIGEST_LEN];
  1157. unsigned int is_recent; /**< Is this recent enough to influence running
  1158. * status? */
  1159. /* These fields come from the actual network-status document.*/
  1160. time_t published_on; /**< Declared publication date. */
  1161. char *source_address; /**< Canonical directory server hostname. */
  1162. uint32_t source_addr; /**< Canonical directory server IP. */
  1163. uint16_t source_dirport; /**< Canonical directory server dirport. */
  1164. unsigned int binds_names:1; /**< True iff this directory server binds
  1165. * names. */
  1166. unsigned int recommends_versions:1; /**< True iff this directory server
  1167. * recommends client and server software
  1168. * versions. */
  1169. unsigned int lists_bad_exits:1; /**< True iff this directory server marks
  1170. * malfunctioning exits as bad. */
  1171. /** True iff this directory server marks malfunctioning directories as
  1172. * bad. */
  1173. unsigned int lists_bad_directories:1;
  1174. char identity_digest[DIGEST_LEN]; /**< Digest of signing key. */
  1175. char *contact; /**< How to contact directory admin? (may be NULL). */
  1176. crypto_pk_env_t *signing_key; /**< Key used to sign this directory. */
  1177. char *client_versions; /**< comma-separated list of recommended client
  1178. * versions. */
  1179. char *server_versions; /**< comma-separated list of recommended server
  1180. * versions. */
  1181. smartlist_t *entries; /**< List of routerstatus_t*. This list is kept
  1182. * sorted by identity_digest. */
  1183. } networkstatus_t;
  1184. /** DOCDOC */
  1185. typedef struct vote_routerstatus_t {
  1186. routerstatus_t status;
  1187. uint64_t flags;
  1188. char *version;
  1189. } vote_routerstatus_t;
  1190. /* DOCDOC */
  1191. typedef struct networkstatus_voter_info_t {
  1192. char *nickname;
  1193. char identity_digest[DIGEST_LEN];
  1194. char *address;
  1195. uint32_t addr;
  1196. uint16_t dir_port;
  1197. uint16_t or_port;
  1198. char *contact;
  1199. char vote_digest[DIGEST_LEN];
  1200. char signing_key_digest[DIGEST_LEN]; /* This part is _not_ signed. */
  1201. char *signature;
  1202. int signature_len;
  1203. unsigned int bad_signature : 1;
  1204. unsigned int good_signature : 1;
  1205. } networkstatus_voter_info_t;
  1206. /*XXXX020 rename to networkstatus_t once it works. */
  1207. /** DOCDOC is vote or consensus. */
  1208. typedef struct networkstatus_vote_t {
  1209. int is_vote;
  1210. time_t published; /* vote only. */
  1211. time_t valid_after;
  1212. time_t fresh_until;
  1213. time_t valid_until;
  1214. int vote_seconds;
  1215. int dist_seconds;
  1216. char *client_versions;
  1217. char *server_versions;
  1218. smartlist_t *known_flags;
  1219. smartlist_t *voters; /* list of networkstatus_voter_info_t */
  1220. struct authority_cert_t *cert; /* vote only. */
  1221. char networkstatus_digest[DIGEST_LEN];
  1222. smartlist_t *routerstatus_list; /* holds vote_routerstatus_t if is_vote,
  1223. * otherwise just routerstatus_t. */
  1224. } networkstatus_vote_t;
  1225. /* XXXX020 merge with networkstatus_vote_t ?? */
  1226. /** DOCDOC */
  1227. typedef struct ns_detached_signatures_t {
  1228. time_t valid_after;
  1229. time_t fresh_until;
  1230. time_t valid_until;
  1231. char networkstatus_digest[DIGEST_LEN];
  1232. smartlist_t *signatures; /* list of networkstatus_voter_info_t */
  1233. } ns_detached_signatures_t;
  1234. /** Contents of a directory of onion routers. */
  1235. typedef struct {
  1236. /** Map from server identity digest to a member of routers. */
  1237. struct digest_ri_map_t *identity_map;
  1238. /** Map from server descriptor digest to a signed_descriptor_t from
  1239. * routers or old_routers. */
  1240. struct digest_sd_map_t *desc_digest_map;
  1241. /** Map from extra-info digest to an extrainfo_t. Only exists for
  1242. * routers in routers or old_routers. */
  1243. struct digest_ei_map_t *extra_info_map;
  1244. /** Map from extra-info digests to a signed_descriptor_t for a router
  1245. * descriptor having that extra-info digest. Only exists for
  1246. * routers in routers or old_routers. */
  1247. struct digest_sd_map_t *desc_by_eid_map;
  1248. /** List of routerinfo_t for all currently live routers we know. */
  1249. smartlist_t *routers;
  1250. /** List of signed_descriptor_t for older router descriptors we're
  1251. * caching. */
  1252. smartlist_t *old_routers;
  1253. /** Mmaped file holding server descriptors. If present, any router whose
  1254. * cache_info.saved_location == SAVED_IN_CACHE is stored in this file
  1255. * starting at cache_info.saved_offset */
  1256. tor_mmap_t *mmap_descriptors;
  1257. /** Mmaped file holding extra-info documents. */
  1258. tor_mmap_t *mmap_extrainfo;
  1259. } routerlist_t;
  1260. /** Information on router used when extending a circuit. We don't need a
  1261. * full routerinfo_t to extend: we only need addr:port:keyid to build an OR
  1262. * connection, and onion_key to create the onionskin. Note that for onehop
  1263. * general-purpose tunnels, the onion_key is NULL. */
  1264. typedef struct extend_info_t {
  1265. char nickname[MAX_HEX_NICKNAME_LEN+1]; /**< This router's nickname for
  1266. * display. */
  1267. char identity_digest[DIGEST_LEN]; /**< Hash of this router's identity key. */
  1268. uint16_t port; /**< OR port. */
  1269. // uint8_t router_purpose; /**< General, controller, or bridge. */
  1270. uint32_t addr; /**< IP address in host order. */
  1271. crypto_pk_env_t *onion_key; /**< Current onionskin key. */
  1272. } extend_info_t;
  1273. /** Certificate for v3 directory protocol: binds long-term authority identity
  1274. * keys to medium-term authority signing keys. */
  1275. typedef struct authority_cert_t {
  1276. signed_descriptor_t cache_info;
  1277. crypto_pk_env_t *identity_key;
  1278. crypto_pk_env_t *signing_key;
  1279. char signing_key_digest[DIGEST_LEN];
  1280. time_t expires;
  1281. } authority_cert_t;
  1282. /** Bitfield enum type listing types of directory authority/directory
  1283. * server. */
  1284. typedef enum {
  1285. NO_AUTHORITY = 0,
  1286. V1_AUTHORITY = 1 << 0,
  1287. V2_AUTHORITY = 1 << 1,
  1288. V3_AUTHORITY = 1 << 2,
  1289. HIDSERV_AUTHORITY = 1 << 3,
  1290. BRIDGE_AUTHORITY = 1 << 4,
  1291. EXTRAINFO_CACHE = 1 << 5, /* not precisely an authority type. */
  1292. } authority_type_t;
  1293. #define CRYPT_PATH_MAGIC 0x70127012u
  1294. /** Holds accounting information for a single step in the layered encryption
  1295. * performed by a circuit. Used only at the client edge of a circuit. */
  1296. typedef struct crypt_path_t {
  1297. uint32_t magic;
  1298. /* crypto environments */
  1299. /** Encryption key and counter for cells heading towards the OR at this
  1300. * step. */
  1301. crypto_cipher_env_t *f_crypto;
  1302. /** Encryption key and counter for cells heading back from the OR at this
  1303. * step. */
  1304. crypto_cipher_env_t *b_crypto;
  1305. /** Digest state for cells heading towards the OR at this step. */
  1306. crypto_digest_env_t *f_digest; /* for integrity checking */
  1307. /** Digest state for cells heading away from the OR at this step. */
  1308. crypto_digest_env_t *b_digest;
  1309. /** Current state of Diffie-Hellman key negotiation with the OR at this
  1310. * step. */
  1311. crypto_dh_env_t *dh_handshake_state;
  1312. /** Current state of 'fast' (non-PK) key negotiation with the OR at this
  1313. * step. Used to save CPU when TLS is already providing all the
  1314. * authentication, secrecy, and integrity we need, and we're already
  1315. * distinguishable from an OR.
  1316. */
  1317. char fast_handshake_state[DIGEST_LEN];
  1318. /** Negotiated key material shared with the OR at this step. */
  1319. char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
  1320. /** Information to extend to the OR at this step. */
  1321. extend_info_t *extend_info;
  1322. /** Is the circuit built to this step? Must be one of:
  1323. * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
  1324. * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
  1325. * and not received an EXTENDED/CREATED)
  1326. * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
  1327. uint8_t state;
  1328. #define CPATH_STATE_CLOSED 0
  1329. #define CPATH_STATE_AWAITING_KEYS 1
  1330. #define CPATH_STATE_OPEN 2
  1331. struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
  1332. * (The list is circular, so the last node
  1333. * links to the first.) */
  1334. struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
  1335. * circuit. */
  1336. int package_window; /**< How many bytes are we allowed to originate ending
  1337. * at this step? */
  1338. int deliver_window; /**< How many bytes are we willing to deliver originating
  1339. * at this step? */
  1340. } crypt_path_t;
  1341. #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
  1342. #define DH_KEY_LEN DH_BYTES
  1343. #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
  1344. CIPHER_KEY_LEN+\
  1345. DH_KEY_LEN)
  1346. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
  1347. #define REND_COOKIE_LEN DIGEST_LEN
  1348. /** Information used to build a circuit. */
  1349. typedef struct {
  1350. /** Intended length of the final circuit. */
  1351. int desired_path_len;
  1352. /** How to extend to the planned exit node. */
  1353. extend_info_t *chosen_exit;
  1354. /** Whether every node in the circ must have adequate uptime. */
  1355. int need_uptime;
  1356. /** Whether every node in the circ must have adequate capacity. */
  1357. int need_capacity;
  1358. /** Whether the last hop was picked with exiting in mind. */
  1359. int is_internal;
  1360. /** Did we pick this as a one-hop tunnel (not safe for other conns)?
  1361. * These are for encrypted connections that exit to this router, not
  1362. * for arbitrary exits from the circuit. */
  1363. int onehop_tunnel;
  1364. /** The crypt_path_t to append after rendezvous: used for rendezvous. */
  1365. crypt_path_t *pending_final_cpath;
  1366. /** How many times has building a circuit for this task failed? */
  1367. int failure_count;
  1368. /** At what time should we give up on this task? */
  1369. time_t expiry_time;
  1370. } cpath_build_state_t;
  1371. #define ORIGIN_CIRCUIT_MAGIC 0x35315243u
  1372. #define OR_CIRCUIT_MAGIC 0x98ABC04Fu
  1373. typedef uint16_t circid_t;
  1374. /**
  1375. * A circuit is a path over the onion routing
  1376. * network. Applications can connect to one end of the circuit, and can
  1377. * create exit connections at the other end of the circuit. AP and exit
  1378. * connections have only one circuit associated with them (and thus these
  1379. * connection types are closed when the circuit is closed), whereas
  1380. * OR connections multiplex many circuits at once, and stay standing even
  1381. * when there are no circuits running over them.
  1382. *
  1383. * A circuit_t structure can fill one of two roles. First, a or_circuit_t
  1384. * links two connections together: either an edge connection and an OR
  1385. * connection, or two OR connections. (When joined to an OR connection, a
  1386. * circuit_t affects only cells sent to a particular circID on that
  1387. * connection. When joined to an edge connection, a circuit_t affects all
  1388. * data.)
  1389. * Second, an origin_circuit_t holds the cipher keys and state for sending data
  1390. * along a given circuit. At the OP, it has a sequence of ciphers, each
  1391. * of which is shared with a single OR along the circuit. Separate
  1392. * ciphers are used for data going "forward" (away from the OP) and
  1393. * "backward" (towards the OP). At the OR, a circuit has only two stream
  1394. * ciphers: one for data going forward, and one for data going backward.
  1395. */
  1396. typedef struct circuit_t {
  1397. uint32_t magic; /**< For memory and type debugging: must equal
  1398. * ORIGIN_CIRCUIT_MAGIC or OR_CIRCUIT_MAGIC. */
  1399. /** Queue of cells waiting to be transmitted on n_conn. */
  1400. cell_queue_t n_conn_cells;
  1401. /** The OR connection that is next in this circuit. */
  1402. or_connection_t *n_conn;
  1403. /** The identity hash of n_conn. */
  1404. char n_conn_id_digest[DIGEST_LEN];
  1405. /** The circuit_id used in the next (forward) hop of this circuit. */
  1406. uint16_t n_circ_id;
  1407. /** The port for the OR that is next in this circuit. */
  1408. uint16_t n_port;
  1409. /** The IPv4 address of the OR that is next in this circuit. */
  1410. uint32_t n_addr;
  1411. /** True iff we are waiting for n_conn_cells to become less full before
  1412. * allowing p_streams to add any more cells. (Origin circuit only.) */
  1413. unsigned int streams_blocked_on_n_conn : 1;
  1414. /** True iff we are waiting for p_conn_cells to become less full before
  1415. * allowing n_streams to add any more cells. (OR circuit only.) */
  1416. unsigned int streams_blocked_on_p_conn : 1;
  1417. uint8_t state; /**< Current status of this circuit. */
  1418. uint8_t purpose; /**< Why are we creating this circuit? */
  1419. /** How many relay data cells can we package (read from edge streams)
  1420. * on this circuit before we receive a circuit-level sendme cell asking
  1421. * for more? */
  1422. int package_window;
  1423. /** How many relay data cells will we deliver (write to edge streams)
  1424. * on this circuit? When deliver_window gets low, we send some
  1425. * circuit-level sendme cells to indicate that we're willing to accept
  1426. * more. */
  1427. int deliver_window;
  1428. /** For storage while passing to cpuworker (state
  1429. * CIRCUIT_STATE_ONIONSKIN_PENDING), or while n_conn is pending
  1430. * (state CIRCUIT_STATE_OR_WAIT). When defined, it is always
  1431. * length ONIONSKIN_CHALLENGE_LEN. */
  1432. char *onionskin;
  1433. time_t timestamp_created; /**< When was this circuit created? */
  1434. time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
  1435. * circuit is clean. */
  1436. uint16_t marked_for_close; /**< Should we close this circuit at the end of
  1437. * the main loop? (If true, holds the line number
  1438. * where this circuit was marked.) */
  1439. const char *marked_for_close_file; /**< For debugging: in which file was this
  1440. * circuit marked for close? */
  1441. /** Next circuit in the doubly-linked ring of circuits waiting to add
  1442. * cells to n_conn. NULL if we have no cells pending, or if we're not
  1443. * linked to an OR connection. */
  1444. struct circuit_t *next_active_on_n_conn;
  1445. /** Previous circuit in the doubly-linked ring of circuits waiting to add
  1446. * cells to n_conn. NULL if we have no cells pending, or if we're not
  1447. * linked to an OR connection. */
  1448. struct circuit_t *prev_active_on_n_conn;
  1449. struct circuit_t *next; /**< Next circuit in linked list of all circuits. */
  1450. } circuit_t;
  1451. /** An origin_circuit_t holds data necessary to build and use a circuit.
  1452. */
  1453. typedef struct origin_circuit_t {
  1454. circuit_t _base;
  1455. /** Linked list of AP streams (or EXIT streams if hidden service)
  1456. * associated with this circuit. */
  1457. edge_connection_t *p_streams;
  1458. /** Build state for this circuit. It includes the intended path
  1459. * length, the chosen exit router, rendezvous information, etc.
  1460. */
  1461. cpath_build_state_t *build_state;
  1462. /** The doubly-linked list of crypt_path_t entries, one per hop,
  1463. * for this circuit. This includes ciphers for each hop,
  1464. * integrity-checking digests for each hop, and package/delivery
  1465. * windows for each hop.
  1466. */
  1467. crypt_path_t *cpath;
  1468. /** The rend_pk_digest field holds a hash of location-hidden service's
  1469. * PK if purpose is S_ESTABLISH_INTRO or S_RENDEZVOUSING.
  1470. */
  1471. char rend_pk_digest[DIGEST_LEN];
  1472. /** Holds rendezvous cookie if purpose is C_ESTABLISH_REND. Filled with
  1473. * zeroes otherwise.
  1474. */
  1475. char rend_cookie[REND_COOKIE_LEN];
  1476. /**
  1477. * The rend_query field holds the y portion of y.onion (nul-terminated)
  1478. * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
  1479. * for a hidden service, or is S_*.
  1480. */
  1481. char rend_query[REND_SERVICE_ID_LEN+1];
  1482. /** The next stream_id that will be tried when we're attempting to
  1483. * construct a new AP stream originating at this circuit. */
  1484. uint16_t next_stream_id;
  1485. /** Quasi-global identifier for this circuit; used for control.c */
  1486. /* XXXX NM This can get re-used after 2**32 circuits. */
  1487. uint32_t global_identifier;
  1488. } origin_circuit_t;
  1489. /** An or_circuit_t holds information needed to implement a circuit at an
  1490. * OR. */
  1491. typedef struct or_circuit_t {
  1492. circuit_t _base;
  1493. /** Next circuit in the doubly-linked ring of circuits waiting to add
  1494. * cells to p_conn. NULL if we have no cells pending, or if we're not
  1495. * linked to an OR connection. */
  1496. struct circuit_t *next_active_on_p_conn;
  1497. /** Previous circuit in the doubly-linked ring of circuits waiting to add
  1498. * cells to p_conn. NULL if we have no cells pending, or if we're not
  1499. * linked to an OR connection. */
  1500. struct circuit_t *prev_active_on_p_conn;
  1501. /** The circuit_id used in the previous (backward) hop of this circuit. */
  1502. circid_t p_circ_id;
  1503. /** Queue of cells waiting to be transmitted on p_conn. */
  1504. cell_queue_t p_conn_cells;
  1505. /** The OR connection that is previous in this circuit. */
  1506. or_connection_t *p_conn;
  1507. /** Linked list of Exit streams associated with this circuit. */
  1508. edge_connection_t *n_streams;
  1509. /** Linked list of Exit streams associated with this circuit that are
  1510. * still being resolved. */
  1511. edge_connection_t *resolving_streams;
  1512. /** The cipher used by intermediate hops for cells heading toward the
  1513. * OP. */
  1514. crypto_cipher_env_t *p_crypto;
  1515. /** The cipher used by intermediate hops for cells heading away from
  1516. * the OP. */
  1517. crypto_cipher_env_t *n_crypto;
  1518. /** The integrity-checking digest used by intermediate hops, for
  1519. * cells packaged here and heading towards the OP.
  1520. */
  1521. crypto_digest_env_t *p_digest;
  1522. /** The integrity-checking digest used by intermediate hops, for
  1523. * cells packaged at the OP and arriving here.
  1524. */
  1525. crypto_digest_env_t *n_digest;
  1526. /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  1527. * is not marked for close. */
  1528. struct or_circuit_t *rend_splice;
  1529. #if REND_COOKIE_LEN >= DIGEST_LEN
  1530. #define REND_TOKEN_LEN REND_COOKIE_LEN
  1531. #else
  1532. #define REND_TOKEN_LEN DIGEST_LEN
  1533. #endif
  1534. /** A hash of location-hidden service's PK if purpose is INTRO_POINT, or a
  1535. * rendezvous cookie if purpose is REND_POINT_WAITING. Filled with zeroes
  1536. * otherwise.
  1537. * ???? move to a subtype or adjunct structure? Wastes 20 bytes. -NM
  1538. */
  1539. char rend_token[REND_TOKEN_LEN];
  1540. /* ???? move to a subtype or adjunct structure? Wastes 20 bytes -NM */
  1541. char handshake_digest[DIGEST_LEN]; /**< Stores KH for the handshake. */
  1542. /** True iff this circuit was made with a CREATE_FAST cell. */
  1543. unsigned int is_first_hop : 1;
  1544. } or_circuit_t;
  1545. /** Convert a circuit subtype to a circuit_t.*/
  1546. #define TO_CIRCUIT(x) (&((x)->_base))
  1547. /** Convert a circuit_t* to a pointer to the enclosing or_circuit_t. Asserts
  1548. * if the cast is impossible. */
  1549. static or_circuit_t *TO_OR_CIRCUIT(circuit_t *);
  1550. /** Convert a circuit_t* to a pointer to the enclosing origin_circuit_t.
  1551. * Asserts if the cast is impossible. */
  1552. static origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *);
  1553. static INLINE or_circuit_t *TO_OR_CIRCUIT(circuit_t *x)
  1554. {
  1555. tor_assert(x->magic == OR_CIRCUIT_MAGIC);
  1556. return DOWNCAST(or_circuit_t, x);
  1557. }
  1558. static INLINE origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *x)
  1559. {
  1560. tor_assert(x->magic == ORIGIN_CIRCUIT_MAGIC);
  1561. return DOWNCAST(origin_circuit_t, x);
  1562. }
  1563. #define ALLOW_INVALID_ENTRY 1
  1564. #define ALLOW_INVALID_EXIT 2
  1565. #define ALLOW_INVALID_MIDDLE 4
  1566. #define ALLOW_INVALID_RENDEZVOUS 8
  1567. #define ALLOW_INVALID_INTRODUCTION 16
  1568. /** An entry specifying a set of addresses and ports that should be remapped
  1569. * to another address and port before exiting this exit node. */
  1570. typedef struct exit_redirect_t {
  1571. /* XXXX020 make this whole mess ipv6-capable. (Does anybody use it? */
  1572. uint32_t addr;
  1573. uint16_t port_min;
  1574. uint16_t port_max;
  1575. maskbits_t maskbits;
  1576. uint32_t addr_dest;
  1577. uint16_t port_dest;
  1578. unsigned is_redirect:1;
  1579. } exit_redirect_t;
  1580. /* limits for TCP send and recv buffer size used for constrained sockets */
  1581. #define MIN_CONSTRAINED_TCP_BUFFER 2048
  1582. #define MAX_CONSTRAINED_TCP_BUFFER 262144 /* 256k */
  1583. /** A linked list of lines in a config file. */
  1584. typedef struct config_line_t {
  1585. char *key;
  1586. char *value;
  1587. struct config_line_t *next;
  1588. } config_line_t;
  1589. /** Configuration options for a Tor process. */
  1590. typedef struct {
  1591. uint32_t _magic;
  1592. /** What should the tor process actually do? */
  1593. enum {
  1594. CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
  1595. CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS
  1596. } command;
  1597. const char *command_arg; /**< Argument for command-line option. */
  1598. config_line_t *Logs; /**< New-style list of configuration lines
  1599. * for logs */
  1600. char *DebugLogFile; /**< Where to send verbose log messages. */
  1601. char *DataDirectory; /**< OR only: where to store long-term data. */
  1602. char *Nickname; /**< OR only: nickname of this onion router. */
  1603. char *Address; /**< OR only: configured address for this onion router. */
  1604. char *PidFile; /**< Where to store PID of Tor process. */
  1605. char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
  1606. * as exits. */
  1607. char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
  1608. * as entry points. */
  1609. int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
  1610. * stop building circuits? */
  1611. int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
  1612. * stop building circuits? */
  1613. char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
  1614. * use in circuits. */
  1615. char *RendNodes; /**< Comma-separated list of nicknames used as introduction
  1616. * points. */
  1617. char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
  1618. * as introduction points. */
  1619. /** List of "entry", "middle", "exit", "introduction", "rendezvous". */
  1620. smartlist_t *AllowInvalidNodes;
  1621. int _AllowInvalid; /**< Bitmask; derived from AllowInvalidNodes. */
  1622. config_line_t *ExitPolicy; /**< Lists of exit policy components. */
  1623. int ExitPolicyRejectPrivate; /**< Should we not exit to local addresses? */
  1624. config_line_t *SocksPolicy; /**< Lists of socks policy components */
  1625. config_line_t *DirPolicy; /**< Lists of dir policy components */
  1626. /** Addresses to bind for listening for SOCKS connections. */
  1627. config_line_t *SocksListenAddress;
  1628. /** Addresses to bind for listening for transparent pf/nefilter
  1629. * connections. */
  1630. config_line_t *TransListenAddress;
  1631. /** Addresses to bind for listening for transparent natd connections */
  1632. config_line_t *NatdListenAddress;
  1633. /** Addresses to bind for listening for SOCKS connections. */
  1634. config_line_t *DNSListenAddress;
  1635. /** Addresses to bind for listening for OR connections. */
  1636. config_line_t *ORListenAddress;
  1637. /** Addresses to bind for listening for directory connections. */
  1638. config_line_t *DirListenAddress;
  1639. /** Addresses to bind for listening for control connections. */
  1640. config_line_t *ControlListenAddress;
  1641. /** Local address to bind outbound sockets */
  1642. char *OutboundBindAddress;
  1643. /** Directory server only: which versions of
  1644. * Tor should we tell users to run? */
  1645. config_line_t *RecommendedVersions;
  1646. config_line_t *RecommendedClientVersions;
  1647. config_line_t *RecommendedServerVersions;
  1648. /** Whether dirservers refuse router descriptors with private IPs. */
  1649. int DirAllowPrivateAddresses;
  1650. char *User; /**< Name of user to run Tor as. */
  1651. char *Group; /**< Name of group to run Tor as. */
  1652. int ORPort; /**< Port to listen on for OR connections. */
  1653. int SocksPort; /**< Port to listen on for SOCKS connections. */
  1654. /** Port to listen on for transparent pf/netfilter connections. */
  1655. int TransPort;
  1656. int NatdPort; /**< Port to listen on for transparent natd connections. */
  1657. int ControlPort; /**< Port to listen on for control connections. */
  1658. config_line_t *ControlSocket; /**< List of Unix Domain Sockets to listen on
  1659. * for control connections. */
  1660. int DirPort; /**< Port to listen on for directory connections. */
  1661. int DNSPort; /**< Port to listen on for DNS requests. */
  1662. int AssumeReachable; /**< Whether to publish our descriptor regardless. */
  1663. int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
  1664. int V1AuthoritativeDir; /**< Boolean: is this an authoritative directory
  1665. * for version 1 directories? */
  1666. int V2AuthoritativeDir; /**< Boolean: is this an authoritative directory
  1667. * for version 2 directories? */
  1668. int V3AuthoritativeDir; /**< Boolean: is this an authoritative directory
  1669. * for version 3 directories? */
  1670. int HSAuthoritativeDir; /**< Boolean: does this an authoritative directory
  1671. * handle hidden service requests? */
  1672. int HSAuthorityRecordStats; /**< Boolean: does this HS authoritative
  1673. * directory record statistics? */
  1674. int NamingAuthoritativeDir; /**< Boolean: is this an authoritative directory
  1675. * that's willing to bind names? */
  1676. int VersioningAuthoritativeDir; /**< Boolean: is this an authoritative
  1677. * directory that's willing to recommend
  1678. * versions? */
  1679. int BridgeAuthoritativeDir; /**< Boolean: is this an authoritative directory
  1680. * that aggregates bridge descriptors? */
  1681. int UseBridges; /**< Boolean: should we start all circuits with a bridge? */
  1682. config_line_t *Bridges; /**< List of bootstrap bridge addresses. */
  1683. /** Boolean: if we know the bridge's digest, should we get new
  1684. * descriptors from the bridge authorities or from the bridge itself? */
  1685. int UpdateBridgesFromAuthority;
  1686. int AvoidDiskWrites; /**< Boolean: should we never cache things to disk?
  1687. * Not used yet. */
  1688. int ClientOnly; /**< Boolean: should we never evolve into a server role? */
  1689. /** Boolean: should we never publish a descriptor? Deprecated. */
  1690. int NoPublish;
  1691. /** To what authority types do we publish our descriptor? Choices are
  1692. * "v1", "v2", "bridge", or "". */
  1693. smartlist_t *PublishServerDescriptor;
  1694. /** An authority type, derived from PublishServerDescriptor. */
  1695. authority_type_t _PublishServerDescriptor;
  1696. /** Boolean: do we publish hidden service descriptors to the HS auths? */
  1697. int PublishHidServDescriptors;
  1698. int FetchServerDescriptors; /**< Do we fetch server descriptors as normal? */
  1699. int FetchHidServDescriptors; /** and hidden service descriptors? */
  1700. int FetchUselessDescriptors; /**< Do we fetch non-running descriptors too? */
  1701. int AllDirActionsPrivate; /**< Should every directory action be sent
  1702. * through a Tor circuit? */
  1703. int ConnLimit; /**< Demanded minimum number of simultaneous connections. */
  1704. int _ConnLimit; /**< Maximum allowed number of simultaneous connections. */
  1705. int RunAsDaemon; /**< If true, run in the background. (Unix only) */
  1706. int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
  1707. smartlist_t *FirewallPorts; /**< Which ports our firewall allows
  1708. * (strings). */
  1709. config_line_t *ReachableAddresses; /**< IP:ports our firewall allows. */
  1710. config_line_t *ReachableORAddresses; /**< IP:ports for OR conns. */
  1711. config_line_t *ReachableDirAddresses; /**< IP:ports for Dir conns. */
  1712. int ConstrainedSockets; /**< Shrink xmit and recv socket buffers. */
  1713. uint64_t ConstrainedSockSize; /**< Size of constrained buffers. */
  1714. /** Application ports that require all nodes in circ to have sufficient
  1715. * uptime. */
  1716. smartlist_t *LongLivedPorts;
  1717. /** Should we try to reuse the same exit node for a given host */
  1718. smartlist_t *TrackHostExits;
  1719. int TrackHostExitsExpire; /**< Number of seconds until we expire an
  1720. * addressmap */
  1721. config_line_t *AddressMap; /**< List of address map directives. */
  1722. int AutomapHostsOnResolve; /**< If true, when we get a resolve request for a
  1723. * hostname ending with one of the suffixes in
  1724. * <b>AutomapHostsSuffixes</b>, map it to a
  1725. * virtual address. */
  1726. smartlist_t *AutomapHostsSuffixes; /**< List of suffixes for
  1727. * <b>AutomapHostsOnResolve</b>. */
  1728. int RendPostPeriod; /**< How often do we post each rendezvous service
  1729. * descriptor? Remember to publish them independently. */
  1730. int KeepalivePeriod; /**< How often do we send padding cells to keep
  1731. * connections alive? */
  1732. int SocksTimeout; /**< How long do we let a socks connection wait
  1733. * unattached before we fail it? */
  1734. int CircuitBuildTimeout; /**< Cull non-open circuits that were born
  1735. * at least this many seconds ago. */
  1736. int CircuitIdleTimeout; /**< Cull open clean circuits that were born
  1737. * at least this many seconds ago. */
  1738. int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
  1739. * to wait simultaneously before we start dropping
  1740. * them? */
  1741. int NewCircuitPeriod; /**< How long do we use a circuit before building
  1742. * a new one? */
  1743. int MaxCircuitDirtiness; /**< Never use circs that were first used more than
  1744. this interval ago. */
  1745. uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing
  1746. * to use in a second? */
  1747. uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing
  1748. * to use in a second? */
  1749. uint64_t MaxAdvertisedBandwidth; /**< How much bandwidth are we willing to
  1750. * tell people we have? */
  1751. uint64_t RelayBandwidthRate; /**< How much bandwidth, on average, are we
  1752. * willing to use for all relayed conns? */
  1753. uint64_t RelayBandwidthBurst; /**< How much bandwidth, at maximum, will we
  1754. * use in a second for all relayed conns? */
  1755. int NumCpus; /**< How many CPUs should we try to use? */
  1756. int RunTesting; /**< If true, create testing circuits to measure how well the
  1757. * other ORs are running. */
  1758. char *TestVia; /**< When reachability testing, use these as middle hop. */
  1759. config_line_t *RendConfigLines; /**< List of configuration lines
  1760. * for rendezvous services. */
  1761. char *ContactInfo; /**< Contact info to be published in the directory */
  1762. char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
  1763. uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
  1764. uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
  1765. char *HttpProxyAuthenticator; /**< username:password string, if any */
  1766. char *HttpsProxy; /**< hostname[:port] to use as https proxy, if any */
  1767. uint32_t HttpsProxyAddr; /**< Parsed IPv4 addr for https proxy, if any */
  1768. uint16_t HttpsProxyPort; /**< Parsed port for https proxy, if any */
  1769. char *HttpsProxyAuthenticator; /**< username:password string, if any */
  1770. config_line_t *DirServers; /**< List of configuration lines
  1771. * for directory servers. */
  1772. char *MyFamily; /**< Declared family for this OR. */
  1773. config_line_t *NodeFamilies; /**< List of config lines for
  1774. * node families */
  1775. config_line_t *RedirectExit; /**< List of config lines for simple
  1776. * addr/port redirection */
  1777. smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
  1778. config_line_t *AuthDirBadExit; /**< Address policy for descriptors to
  1779. * mark as bad exits. */
  1780. config_line_t *AuthDirReject; /**< Address policy for descriptors to
  1781. * reject. */
  1782. config_line_t *AuthDirInvalid; /**< Address policy for descriptors to
  1783. * never mark as valid. */
  1784. int AuthDirListBadExits; /**< True iff we should list bad exits,
  1785. * and vote for all other exits as good. */
  1786. int AuthDirRejectUnlisted; /**< Boolean: do we reject all routers that
  1787. * aren't named in our fingerprint file? */
  1788. char *AccountingStart; /**< How long is the accounting interval, and when
  1789. * does it start? */
  1790. uint64_t AccountingMax; /**< How many bytes do we allow per accounting
  1791. * interval before hibernation? 0 for "never
  1792. * hibernate." */
  1793. char *HashedControlPassword; /**< Base64-encoded hash of a password for
  1794. * the control system. */
  1795. int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
  1796. * the control system? */
  1797. char *CookieAuthFile; /**< Location of a cookie authentication file. */
  1798. int CookieAuthFileGroupReadable; /**< Boolean: Is the CookieAuthFile g+r? */
  1799. int LeaveStreamsUnattached; /**< Boolean: Does Tor attach new streams to
  1800. * circuits itself (0), or does it expect a controller
  1801. * to cope? (1) */
  1802. int DisablePredictedCircuits; /**< Boolean: does Tor preemptively
  1803. * make circuits in the background (0),
  1804. * or not (1)? */
  1805. int ShutdownWaitLength; /**< When we get a SIGINT and we're a server, how
  1806. * long do we wait before exiting? */
  1807. int SafeLogging; /**< Boolean: are we allowed to log sensitive strings
  1808. * such as addresses (0), or do we scrub them first (1)? */
  1809. int SafeSocks; /**< Boolean: should we outright refuse application
  1810. * connections that use socks4 or socks5-with-local-dns? */
  1811. #define LOG_PROTOCOL_WARN (get_options()->ProtocolWarnings ? \
  1812. LOG_WARN : LOG_INFO)
  1813. int ProtocolWarnings; /**< Boolean: when other parties screw up the Tor
  1814. * protocol, is it a warn or an info in our logs? */
  1815. int TestSocks; /**< Boolean: when we get a socks connection, do we loudly
  1816. * log whether it was DNS-leaking or not? */
  1817. int HardwareAccel; /**< Boolean: Should we enable OpenSSL hardware
  1818. * acceleration where available? */
  1819. int UseEntryGuards; /**< Boolean: Do we try to enter from a smallish number
  1820. * of fixed nodes? */
  1821. int NumEntryGuards; /**< How many entry guards do we try to establish? */
  1822. int RephistTrackTime; /**< How many seconds do we keep rephist info? */
  1823. int FastFirstHopPK; /**< If Tor believes it is safe, should we save a third
  1824. * of our PK time by sending CREATE_FAST cells? */
  1825. addr_policy_t *reachable_addr_policy; /**< Parsed from ReachableAddresses */
  1826. char *VirtualAddrNetwork; /**< Address and mask to hand out for virtual
  1827. * MAPADDRESS requests. */
  1828. int ServerDNSSearchDomains; /**< Boolean: If set, we don't force exit
  1829. * addresses to be FQDNs, but rather search for them in
  1830. * the local domains. */
  1831. int ServerDNSDetectHijacking; /**< Boolean: If true, check for DNS failure
  1832. * hijacking. */
  1833. char *ServerDNSResolvConfFile; /**< If provided, we configure our internal
  1834. * resolver from the file here rather than from
  1835. * /etc/resolv.conf (Unix) or the registry (Windows). */
  1836. smartlist_t *ServerDNSTestAddresses; /**< A list of addresses that definitely
  1837. * should be resolveable. Used for
  1838. * testing our DNS server. */
  1839. int EnforceDistinctSubnets; /**< If true, don't allow multiple routers in the
  1840. * same network zone in the same circuit. */
  1841. int TunnelDirConns; /**< If true, use BEGIN_DIR rather than BEGIN when
  1842. * possible. */
  1843. int PreferTunneledDirConns; /**< If true, avoid dirservers that don't
  1844. * support BEGIN_DIR, when possible. */
  1845. int AllowNonRFC953Hostnames; /**< If true, we allow connections to hostnames
  1846. * with weird characters. */
  1847. /** If true, we try resolving hostnames with weird characters. */
  1848. int ServerDNSAllowNonRFC953Hostnames;
  1849. /** If true, we try to download extra-info documents (and we serve them,
  1850. * if we are a cache). For authorities, this is always true. */
  1851. int DownloadExtraInfo;
  1852. /** The length of time that we think a consensus should be fresh. */
  1853. int V3AuthVotingInterval;
  1854. /** The length of time we think it will take to distribute votes */
  1855. int V3AuthVoteDelay;
  1856. /** The length of time we think it will take to distribute signatures */
  1857. int V3AuthDistDelay;
  1858. /** The number of intervals we think a consensus should be valid. */
  1859. int V3AuthNIntervalsValid;
  1860. } or_options_t;
  1861. /** Persistent state for an onion router, as saved to disk. */
  1862. typedef struct {
  1863. uint32_t _magic;
  1864. /** The time at which we next plan to write the state to the disk. Equal to
  1865. * TIME_MAX if there are no saveable changes, 0 if there are changes that
  1866. * should be saved right away. */
  1867. time_t next_write;
  1868. /** When was the state last written to disk? */
  1869. time_t LastWritten;
  1870. /** Fields for accounting bandwidth use. */
  1871. time_t AccountingIntervalStart;
  1872. uint64_t AccountingBytesReadInInterval;
  1873. uint64_t AccountingBytesWrittenInInterval;
  1874. int AccountingSecondsActive;
  1875. uint64_t AccountingExpectedUsage;
  1876. /** A list of Entry Guard-related configuration lines. */
  1877. config_line_t *EntryGuards;
  1878. /** These fields hold information on the history of bandwidth usage for
  1879. * servers. The "Ends" fields hold the time when we last updated the
  1880. * bandwidth usage. The "Interval" fields hold the granularity, in seconds,
  1881. * of the entries of Values. The "Values" lists hold decimal string
  1882. * representations of the number of bytes read or written in each
  1883. * interval. */
  1884. time_t BWHistoryReadEnds;
  1885. int BWHistoryReadInterval;
  1886. smartlist_t *BWHistoryReadValues;
  1887. time_t BWHistoryWriteEnds;
  1888. int BWHistoryWriteInterval;
  1889. smartlist_t *BWHistoryWriteValues;
  1890. /** What version of Tor wrote this state file? */
  1891. char *TorVersion;
  1892. /** Holds any unrecognized values we found in the state file, in the order
  1893. * in which we found them. */
  1894. config_line_t *ExtraLines;
  1895. /** When did we last rotate our onion key? "0" for 'no idea'. */
  1896. time_t LastRotatedOnionKey;
  1897. } or_state_t;
  1898. /** Change the next_write time of <b>state</b> to <b>when</b>, unless the
  1899. * state is already scheduled to be written to disk earlier than <b>when</b>.
  1900. */
  1901. static INLINE void or_state_mark_dirty(or_state_t *state, time_t when)
  1902. {
  1903. if (state->next_write > when)
  1904. state->next_write = when;
  1905. }
  1906. #define MAX_SOCKS_REPLY_LEN 1024
  1907. #define MAX_SOCKS_ADDR_LEN 256
  1908. /** Please open a TCP connection to this addr:port. */
  1909. #define SOCKS_COMMAND_CONNECT 0x01
  1910. /** Please turn this FQDN into an IP address, privately. */
  1911. #define SOCKS_COMMAND_RESOLVE 0xF0
  1912. /** Please turn this IP address into an FQDN, privately. */
  1913. #define SOCKS_COMMAND_RESOLVE_PTR 0xF1
  1914. /** Please open an encrypted direct TCP connection to the directory port
  1915. * of the Tor server specified by address:port. (In this case address:port
  1916. * specifies the ORPort of the server.) */
  1917. #define SOCKS_COMMAND_CONNECT_DIR 0xF2
  1918. #define SOCKS_COMMAND_IS_CONNECT(c) ((c)==SOCKS_COMMAND_CONNECT || \
  1919. (c)==SOCKS_COMMAND_CONNECT_DIR)
  1920. #define SOCKS_COMMAND_IS_RESOLVE(c) ((c)==SOCKS_COMMAND_RESOLVE || \
  1921. (c)==SOCKS_COMMAND_RESOLVE_PTR)
  1922. /** State of a SOCKS request from a user to an OP. Also used to encode other
  1923. * information for non-socks user request (such as those on TransPort and
  1924. * DNSPort) */
  1925. struct socks_request_t {
  1926. /** Which version of SOCKS did the client use? One of "0, 4, 5" -- where
  1927. * 0 means that no socks handshake ever took place, and this is just a
  1928. * stub connection (e.g. see connection_ap_make_link()). */
  1929. char socks_version;
  1930. int command; /**< What is this stream's goal? One from the above list. */
  1931. size_t replylen; /**< Length of <b>reply</b>. */
  1932. char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
  1933. * we want to specify our own socks reply,
  1934. * rather than using the default socks4 or
  1935. * socks5 socks reply. We use this for the
  1936. * two-stage socks5 handshake.
  1937. */
  1938. char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to
  1939. connect to/resolve? */
  1940. uint16_t port; /**< What port did the client ask to connect to? */
  1941. unsigned has_finished : 1; /**< Has the SOCKS handshake finished? Used to
  1942. * make sure we send back a socks reply for
  1943. * every connection. */
  1944. };
  1945. /* all the function prototypes go here */
  1946. /********************************* buffers.c ***************************/
  1947. buf_t *buf_new(void);
  1948. buf_t *buf_new_with_capacity(size_t size);
  1949. void buf_free(buf_t *buf);
  1950. void buf_clear(buf_t *buf);
  1951. void buf_shrink(buf_t *buf);
  1952. void buf_shrink_freelists(int free_all);
  1953. void buf_dump_freelist_sizes(int severity);
  1954. size_t buf_datalen(const buf_t *buf);
  1955. size_t buf_capacity(const buf_t *buf);
  1956. const char *_buf_peek_raw_buffer(const buf_t *buf);
  1957. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
  1958. int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
  1959. int flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen);
  1960. int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
  1961. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  1962. int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  1963. const char *data, size_t data_len, int done);
  1964. int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
  1965. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  1966. int fetch_from_buf_http(buf_t *buf,
  1967. char **headers_out, size_t max_headerlen,
  1968. char **body_out, size_t *body_used, size_t max_bodylen,
  1969. int force_complete);
  1970. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  1971. int log_sockstype, int safe_socks);
  1972. int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
  1973. int fetch_from_buf_line_lf(buf_t *buf, char *data_out, size_t *data_len);
  1974. int peek_buf_has_control0_command(buf_t *buf);
  1975. void assert_buf_ok(buf_t *buf);
  1976. /********************************* circuitbuild.c **********************/
  1977. char *circuit_list_path(origin_circuit_t *circ, int verbose);
  1978. char *circuit_list_path_for_controller(origin_circuit_t *circ);
  1979. void circuit_log_path(int severity, unsigned int domain,
  1980. origin_circuit_t *circ);
  1981. void circuit_rep_hist_note_result(origin_circuit_t *circ);
  1982. origin_circuit_t *origin_circuit_init(uint8_t purpose, int onehop_tunnel,
  1983. int need_uptime,
  1984. int need_capacity, int internal);
  1985. origin_circuit_t *circuit_establish_circuit(uint8_t purpose,
  1986. int onehop_tunnel, extend_info_t *exit,
  1987. int need_uptime, int need_capacity,
  1988. int internal);
  1989. int circuit_handle_first_hop(origin_circuit_t *circ);
  1990. void circuit_n_conn_done(or_connection_t *or_conn, int status);
  1991. int inform_testing_reachability(void);
  1992. int circuit_send_next_onion_skin(origin_circuit_t *circ);
  1993. void circuit_note_clock_jumped(int seconds_elapsed);
  1994. int circuit_extend(cell_t *cell, circuit_t *circ);
  1995. int circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data,
  1996. int reverse);
  1997. int circuit_finish_handshake(origin_circuit_t *circ, uint8_t cell_type,
  1998. const char *reply);
  1999. int circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer);
  2000. int onionskin_answer(or_circuit_t *circ, uint8_t cell_type,
  2001. const char *payload, const char *keys);
  2002. int circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
  2003. int *need_capacity);
  2004. int circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *info);
  2005. int circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *info);
  2006. void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
  2007. extend_info_t *extend_info_alloc(const char *nickname, const char *digest,
  2008. crypto_pk_env_t *onion_key,
  2009. uint32_t addr, uint16_t port);
  2010. extend_info_t *extend_info_from_router(routerinfo_t *r);
  2011. //extend_info_t *extend_info_from_routerstatus(routerstatus_t *s);
  2012. extend_info_t *extend_info_dup(extend_info_t *info);
  2013. void extend_info_free(extend_info_t *info);
  2014. routerinfo_t *build_state_get_exit_router(cpath_build_state_t *state);
  2015. const char *build_state_get_exit_nickname(cpath_build_state_t *state);
  2016. void entry_guards_compute_status(void);
  2017. int entry_guard_register_connect_status(const char *digest, int succeeded,
  2018. time_t now);
  2019. void entry_nodes_should_be_added(void);
  2020. routerinfo_t *choose_random_entry(cpath_build_state_t *state);
  2021. int entry_guards_parse_state(or_state_t *state, int set, char **msg);
  2022. void entry_guards_update_state(or_state_t *state);
  2023. int getinfo_helper_entry_guards(control_connection_t *conn,
  2024. const char *question, char **answer);
  2025. void clear_bridge_list(void);
  2026. int routerinfo_is_a_configured_bridge(routerinfo_t *ri);
  2027. void bridge_add_from_config(uint32_t addr, uint16_t port, char *digest);
  2028. void fetch_bridge_descriptors(time_t now);
  2029. void learned_bridge_descriptor(routerinfo_t *ri);
  2030. int any_bridge_descriptors_known(void);
  2031. int bridges_should_be_retried(void);
  2032. void bridges_retry_all(void);
  2033. void entry_guards_free_all(void);
  2034. /********************************* circuitlist.c ***********************/
  2035. circuit_t * _circuit_get_global_list(void);
  2036. const char *circuit_state_to_string(int state);
  2037. void circuit_dump_by_conn(connection_t *conn, int severity);
  2038. void circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
  2039. or_connection_t *conn);
  2040. void circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
  2041. or_connection_t *conn);
  2042. void circuit_set_state(circuit_t *circ, int state);
  2043. void circuit_close_all_marked(void);
  2044. origin_circuit_t *origin_circuit_new(void);
  2045. or_circuit_t *or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn);
  2046. circuit_t *circuit_get_by_circid_orconn(uint16_t circ_id,
  2047. or_connection_t *conn);
  2048. circuit_t *circuit_get_by_edge_conn(edge_connection_t *conn);
  2049. void circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason);
  2050. origin_circuit_t *circuit_get_by_global_id(uint32_t id);
  2051. origin_circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query,
  2052. uint8_t purpose);
  2053. origin_circuit_t *circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  2054. const char *digest, uint8_t purpose);
  2055. or_circuit_t *circuit_get_rendezvous(const char *cookie);
  2056. or_circuit_t *circuit_get_intro_point(const char *digest);
  2057. origin_circuit_t *circuit_find_to_cannibalize(uint8_t purpose,
  2058. extend_info_t *info,
  2059. int need_uptime,
  2060. int need_capacity, int internal);
  2061. void circuit_mark_all_unused_circs(void);
  2062. void circuit_expire_all_dirty_circs(void);
  2063. void _circuit_mark_for_close(circuit_t *circ, int reason,
  2064. int line, const char *file);
  2065. int circuit_get_cpath_len(origin_circuit_t *circ);
  2066. crypt_path_t *circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum);
  2067. void circuit_get_all_pending_on_or_conn(smartlist_t *out,
  2068. or_connection_t *or_conn);
  2069. int circuit_count_pending_on_or_conn(or_connection_t *or_conn);
  2070. #define circuit_mark_for_close(c, reason) \
  2071. _circuit_mark_for_close((c), (reason), __LINE__, _SHORT_FILE_)
  2072. void assert_cpath_layer_ok(const crypt_path_t *cp);
  2073. void assert_circuit_ok(const circuit_t *c);
  2074. void circuit_free_all(void);
  2075. /********************************* circuituse.c ************************/
  2076. void circuit_expire_building(time_t now);
  2077. void circuit_remove_handled_ports(smartlist_t *needed_ports);
  2078. int circuit_stream_is_being_handled(edge_connection_t *conn, uint16_t port,
  2079. int min);
  2080. void circuit_build_needed_circs(time_t now);
  2081. void circuit_detach_stream(circuit_t *circ, edge_connection_t *conn);
  2082. void reset_bandwidth_test(void);
  2083. int circuit_enough_testing_circs(void);
  2084. void circuit_has_opened(origin_circuit_t *circ);
  2085. void circuit_build_failed(origin_circuit_t *circ);
  2086. origin_circuit_t *circuit_launch_by_nickname(uint8_t purpose,
  2087. int onehop_tunnel,
  2088. const char *exit_nickname,
  2089. int need_uptime, int need_capacity,
  2090. int is_internal);
  2091. origin_circuit_t *circuit_launch_by_extend_info(uint8_t purpose,
  2092. int onehop_tunnel,
  2093. extend_info_t *info,
  2094. int need_uptime, int need_capacity,
  2095. int is_internal);
  2096. origin_circuit_t *circuit_launch_by_router(uint8_t purpose,
  2097. int onehop_tunnel,
  2098. routerinfo_t *exit,
  2099. int need_uptime, int need_capacity,
  2100. int is_internal);
  2101. void circuit_reset_failure_count(int timeout);
  2102. int connection_ap_handshake_attach_chosen_circuit(edge_connection_t *conn,
  2103. origin_circuit_t *circ,
  2104. crypt_path_t *cpath);
  2105. int connection_ap_handshake_attach_circuit(edge_connection_t *conn);
  2106. /********************************* command.c ***************************/
  2107. void command_process_cell(cell_t *cell, or_connection_t *conn);
  2108. extern uint64_t stats_n_padding_cells_processed;
  2109. extern uint64_t stats_n_create_cells_processed;
  2110. extern uint64_t stats_n_created_cells_processed;
  2111. extern uint64_t stats_n_relay_cells_processed;
  2112. extern uint64_t stats_n_destroy_cells_processed;
  2113. /********************************* config.c ***************************/
  2114. or_options_t *get_options(void);
  2115. int set_options(or_options_t *new_val, char **msg);
  2116. void config_free_all(void);
  2117. const char *safe_str(const char *address);
  2118. const char *escaped_safe_str(const char *address);
  2119. const char *get_version(void);
  2120. int config_get_lines(char *string, config_line_t **result);
  2121. void config_free_lines(config_line_t *front);
  2122. int options_trial_assign(config_line_t *list, int use_defaults,
  2123. int clear_first, char **msg);
  2124. int resolve_my_address(int warn_severity, or_options_t *options,
  2125. uint32_t *addr, char **hostname_out);
  2126. int is_local_IP(uint32_t ip) ATTR_PURE;
  2127. void options_init(or_options_t *options);
  2128. int options_init_from_torrc(int argc, char **argv);
  2129. int options_init_logs(or_options_t *options, int validate_only);
  2130. int option_is_recognized(const char *key);
  2131. const char *option_get_canonical_name(const char *key);
  2132. config_line_t *option_get_assignment(or_options_t *options,
  2133. const char *key);
  2134. char *options_dump(or_options_t *options, int minimal);
  2135. int options_save_current(void);
  2136. const char *get_torrc_fname(void);
  2137. or_state_t *get_or_state(void);
  2138. int or_state_load(void);
  2139. int or_state_save(time_t now);
  2140. int getinfo_helper_config(control_connection_t *conn,
  2141. const char *question, char **answer);
  2142. #ifdef CONFIG_PRIVATE
  2143. /* Used only by config.c and test.c */
  2144. or_options_t *options_new(void);
  2145. #endif
  2146. /********************************* connection.c ***************************/
  2147. const char *conn_type_to_string(int type);
  2148. const char *conn_state_to_string(int type, int state);
  2149. connection_t *connection_new(int type, int socket_family);
  2150. void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
  2151. void connection_unregister_events(connection_t *conn);
  2152. void connection_free(connection_t *conn);
  2153. void connection_free_all(void);
  2154. void connection_about_to_close_connection(connection_t *conn);
  2155. void connection_close_immediate(connection_t *conn);
  2156. void _connection_mark_for_close(connection_t *conn,int line, const char *file);
  2157. #define connection_mark_for_close(c) \
  2158. _connection_mark_for_close((c), __LINE__, _SHORT_FILE_)
  2159. void connection_expire_held_open(void);
  2160. int connection_connect(connection_t *conn, const char *address, uint32_t addr,
  2161. uint16_t port);
  2162. int retry_all_listeners(smartlist_t *replaced_conns,
  2163. smartlist_t *new_conns);
  2164. int connection_bucket_write_limit(connection_t *conn, time_t now);
  2165. int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
  2166. void connection_bucket_init(void);
  2167. void connection_bucket_refill(int seconds_elapsed, time_t now);
  2168. int connection_handle_read(connection_t *conn);
  2169. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  2170. int connection_wants_to_flush(connection_t *conn);
  2171. int connection_outbuf_too_full(connection_t *conn);
  2172. int connection_handle_write(connection_t *conn, int force);
  2173. void _connection_write_to_buf_impl(const char *string, size_t len,
  2174. connection_t *conn, int zlib);
  2175. static void connection_write_to_buf(const char *string, size_t len,
  2176. connection_t *conn);
  2177. static void connection_write_to_buf_zlib(const char *string, size_t len,
  2178. dir_connection_t *conn, int done);
  2179. static INLINE void
  2180. connection_write_to_buf(const char *string, size_t len, connection_t *conn)
  2181. {
  2182. _connection_write_to_buf_impl(string, len, conn, 0);
  2183. }
  2184. static INLINE void
  2185. connection_write_to_buf_zlib(const char *string, size_t len,
  2186. dir_connection_t *conn, int done)
  2187. {
  2188. _connection_write_to_buf_impl(string, len, TO_CONN(conn), done ? -1 : 1);
  2189. }
  2190. or_connection_t *connection_or_exact_get_by_addr_port(uint32_t addr,
  2191. uint16_t port);
  2192. edge_connection_t *connection_get_by_global_id(uint32_t id);
  2193. connection_t *connection_get_by_type(int type);
  2194. connection_t *connection_get_by_type_purpose(int type, int purpose);
  2195. connection_t *connection_get_by_type_addr_port_purpose(int type, uint32_t addr,
  2196. uint16_t port, int purpose);
  2197. connection_t *connection_get_by_type_state(int type, int state);
  2198. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  2199. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  2200. const char *rendquery);
  2201. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  2202. int connection_is_listener(connection_t *conn);
  2203. int connection_state_is_open(connection_t *conn);
  2204. int connection_state_is_connecting(connection_t *conn);
  2205. char *alloc_http_authenticator(const char *authenticator);
  2206. void assert_connection_ok(connection_t *conn, time_t now);
  2207. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  2208. void connection_dump_buffer_mem_stats(int severity);
  2209. /********************************* connection_edge.c *************************/
  2210. #define connection_mark_unattached_ap(conn, endreason) \
  2211. _connection_mark_unattached_ap((conn), (endreason), __LINE__, _SHORT_FILE_)
  2212. void _connection_mark_unattached_ap(edge_connection_t *conn, int endreason,
  2213. int line, const char *file);
  2214. int connection_edge_reached_eof(edge_connection_t *conn);
  2215. int connection_edge_process_inbuf(edge_connection_t *conn,
  2216. int package_partial);
  2217. int connection_edge_destroy(uint16_t circ_id, edge_connection_t *conn);
  2218. int connection_edge_end(edge_connection_t *conn, char reason);
  2219. int connection_edge_end_errno(edge_connection_t *conn);
  2220. int connection_edge_finished_flushing(edge_connection_t *conn);
  2221. int connection_edge_finished_connecting(edge_connection_t *conn);
  2222. int connection_ap_handshake_send_begin(edge_connection_t *ap_conn);
  2223. int connection_ap_handshake_send_resolve(edge_connection_t *ap_conn);
  2224. edge_connection_t *connection_ap_make_link(char *address, uint16_t port,
  2225. const char *digest, int command);
  2226. void connection_ap_handshake_socks_reply(edge_connection_t *conn, char *reply,
  2227. size_t replylen,
  2228. int endreason);
  2229. void connection_ap_handshake_socks_resolved(edge_connection_t *conn,
  2230. int answer_type,
  2231. size_t answer_len,
  2232. const char *answer,
  2233. int ttl,
  2234. time_t expires);
  2235. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  2236. int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ);
  2237. void connection_exit_connect(edge_connection_t *conn);
  2238. int connection_edge_is_rendezvous_stream(edge_connection_t *conn);
  2239. int connection_ap_can_use_exit(edge_connection_t *conn, routerinfo_t *exit);
  2240. void connection_ap_expire_beginning(void);
  2241. void connection_ap_attach_pending(void);
  2242. void circuit_discard_optional_exit_enclaves(extend_info_t *info);
  2243. int connection_ap_detach_retriable(edge_connection_t *conn,
  2244. origin_circuit_t *circ,
  2245. int reason);
  2246. int connection_ap_process_transparent(edge_connection_t *conn);
  2247. int address_is_invalid_destination(const char *address, int client);
  2248. void addressmap_init(void);
  2249. void addressmap_clean(time_t now);
  2250. void addressmap_clear_configured(void);
  2251. void addressmap_clear_transient(void);
  2252. void addressmap_free_all(void);
  2253. int addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out);
  2254. int addressmap_have_mapping(const char *address);
  2255. void addressmap_register(const char *address, char *new_address,
  2256. time_t expires);
  2257. int parse_virtual_addr_network(const char *val, int validate_only,
  2258. char **msg);
  2259. int client_dns_incr_failures(const char *address);
  2260. void client_dns_clear_failures(const char *address);
  2261. void client_dns_set_addressmap(const char *address, uint32_t val,
  2262. const char *exitname, int ttl);
  2263. int address_is_in_virtual_range(const char *addr);
  2264. const char *addressmap_register_virtual_address(int type, char *new_address);
  2265. void addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
  2266. time_t max_expires, int want_expiry);
  2267. int connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn,
  2268. origin_circuit_t *circ,
  2269. crypt_path_t *cpath);
  2270. int hostname_is_noconnect_address(const char *address);
  2271. void set_exit_redirects(smartlist_t *lst);
  2272. typedef enum hostname_type_t {
  2273. NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME, BAD_HOSTNAME
  2274. } hostname_type_t;
  2275. hostname_type_t parse_extended_hostname(char *address);
  2276. /********************************* connection_or.c ***************************/
  2277. void connection_or_remove_from_identity_map(or_connection_t *conn);
  2278. void connection_or_clear_identity_map(void);
  2279. or_connection_t *connection_or_get_by_identity_digest(const char *digest);
  2280. int connection_or_reached_eof(or_connection_t *conn);
  2281. int connection_or_process_inbuf(or_connection_t *conn);
  2282. int connection_or_flushed_some(or_connection_t *conn);
  2283. int connection_or_finished_flushing(or_connection_t *conn);
  2284. int connection_or_finished_connecting(or_connection_t *conn);
  2285. or_connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  2286. const char *id_digest);
  2287. int connection_tls_start_handshake(or_connection_t *conn, int receiving);
  2288. int connection_tls_continue_handshake(or_connection_t *conn);
  2289. void connection_or_write_cell_to_buf(const cell_t *cell,
  2290. or_connection_t *conn);
  2291. int connection_or_send_destroy(uint16_t circ_id, or_connection_t *conn,
  2292. int reason);
  2293. void cell_pack(packed_cell_t *dest, const cell_t *src);
  2294. /********************************* control.c ***************************/
  2295. typedef enum circuit_status_event_t {
  2296. CIRC_EVENT_LAUNCHED = 0,
  2297. CIRC_EVENT_BUILT = 1,
  2298. CIRC_EVENT_EXTENDED = 2,
  2299. CIRC_EVENT_FAILED = 3,
  2300. CIRC_EVENT_CLOSED = 4,
  2301. } circuit_status_event_t;
  2302. typedef enum stream_status_event_t {
  2303. STREAM_EVENT_SENT_CONNECT = 0,
  2304. STREAM_EVENT_SENT_RESOLVE = 1,
  2305. STREAM_EVENT_SUCCEEDED = 2,
  2306. STREAM_EVENT_FAILED = 3,
  2307. STREAM_EVENT_CLOSED = 4,
  2308. STREAM_EVENT_NEW = 5,
  2309. STREAM_EVENT_NEW_RESOLVE = 6,
  2310. STREAM_EVENT_FAILED_RETRIABLE = 7,
  2311. STREAM_EVENT_REMAP = 8
  2312. } stream_status_event_t;
  2313. typedef enum or_conn_status_event_t {
  2314. OR_CONN_EVENT_LAUNCHED = 0,
  2315. OR_CONN_EVENT_CONNECTED = 1,
  2316. OR_CONN_EVENT_FAILED = 2,
  2317. OR_CONN_EVENT_CLOSED = 3,
  2318. OR_CONN_EVENT_NEW = 4,
  2319. } or_conn_status_event_t;
  2320. void control_update_global_event_mask(void);
  2321. void control_adjust_event_log_severity(void);
  2322. /** Execute the statement <b>stmt</b>, which may log events concerning the
  2323. * connection <b>conn</b>. To prevent infinite loops, disable log messages
  2324. * being sent to controllers if <b>conn</b> is a control connection.
  2325. *
  2326. * Stmt must not contain any return or goto statements.
  2327. */
  2328. #define CONN_LOG_PROTECT(conn, stmt) \
  2329. STMT_BEGIN \
  2330. int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
  2331. if (_log_conn_is_control) \
  2332. disable_control_logging(); \
  2333. STMT_BEGIN stmt; STMT_END; \
  2334. if (_log_conn_is_control) \
  2335. enable_control_logging(); \
  2336. STMT_END
  2337. /** Log information about the connection <b>conn</b>, protecting it as with
  2338. * CONN_LOG_PROTECT. Example:
  2339. *
  2340. * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
  2341. **/
  2342. #define LOG_FN_CONN(conn, args) \
  2343. CONN_LOG_PROTECT(conn, log_fn args)
  2344. int connection_control_finished_flushing(control_connection_t *conn);
  2345. int connection_control_reached_eof(control_connection_t *conn);
  2346. int connection_control_process_inbuf(control_connection_t *conn);
  2347. #define EVENT_AUTHDIR_NEWDESCS 0x000D
  2348. int control_event_is_interesting(int event);
  2349. int control_event_circuit_status(origin_circuit_t *circ,
  2350. circuit_status_event_t e, int reason);
  2351. int control_event_stream_status(edge_connection_t *conn,
  2352. stream_status_event_t e,
  2353. int reason);
  2354. int control_tls_error_to_reason(int e);
  2355. int control_event_or_conn_status(or_connection_t *conn,
  2356. or_conn_status_event_t e, int reason);
  2357. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  2358. int control_event_stream_bandwidth_used(void);
  2359. void control_event_logmsg(int severity, unsigned int domain, const char *msg);
  2360. int control_event_descriptors_changed(smartlist_t *routers);
  2361. int control_event_address_mapped(const char *from, const char *to,
  2362. time_t expires, const char *error);
  2363. int control_event_or_authdir_new_descriptor(const char *action,
  2364. const char *desc,
  2365. size_t desclen,
  2366. const char *msg);
  2367. int control_event_my_descriptor_changed(void);
  2368. int control_event_networkstatus_changed(smartlist_t *statuses);
  2369. int control_event_networkstatus_changed_single(local_routerstatus_t *rs);
  2370. int control_event_general_status(int severity, const char *format, ...)
  2371. CHECK_PRINTF(2,3);
  2372. int control_event_client_status(int severity, const char *format, ...)
  2373. CHECK_PRINTF(2,3);
  2374. int control_event_server_status(int severity, const char *format, ...)
  2375. CHECK_PRINTF(2,3);
  2376. int control_event_guard(const char *nickname, const char *digest,
  2377. const char *status);
  2378. int init_cookie_authentication(int enabled);
  2379. int decode_hashed_password(char *buf, const char *hashed);
  2380. void disable_control_logging(void);
  2381. void enable_control_logging(void);
  2382. #ifdef CONTROL_PRIVATE
  2383. /* Used only by control.c and test.c */
  2384. size_t write_escaped_data(const char *data, size_t len,
  2385. int translate_newlines, char **out);
  2386. size_t read_escaped_data(const char *data, size_t len,
  2387. int translate_newlines, char **out);
  2388. #endif
  2389. /********************************* cpuworker.c *****************************/
  2390. void cpu_init(void);
  2391. void cpuworkers_rotate(void);
  2392. int connection_cpu_finished_flushing(connection_t *conn);
  2393. int connection_cpu_reached_eof(connection_t *conn);
  2394. int connection_cpu_process_inbuf(connection_t *conn);
  2395. int assign_to_cpuworker(connection_t *cpuworker, uint8_t question_type,
  2396. void *task);
  2397. /********************************* directory.c ***************************/
  2398. char *authority_type_to_string(authority_type_t auth);
  2399. void directory_post_to_dirservers(uint8_t dir_purpose, uint8_t router_purpose,
  2400. authority_type_t type, const char *payload,
  2401. size_t payload_len, size_t extrainfo_len);
  2402. void directory_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  2403. const char *resource,
  2404. int retry_if_no_servers);
  2405. void directory_initiate_command_routerstatus(routerstatus_t *status,
  2406. uint8_t dir_purpose,
  2407. uint8_t router_purpose,
  2408. int anonymized_connection,
  2409. const char *resource,
  2410. const char *payload,
  2411. size_t payload_len);
  2412. int parse_http_response(const char *headers, int *code, time_t *date,
  2413. compress_method_t *compression, char **response);
  2414. int connection_dir_reached_eof(dir_connection_t *conn);
  2415. int connection_dir_process_inbuf(dir_connection_t *conn);
  2416. int connection_dir_finished_flushing(dir_connection_t *conn);
  2417. int connection_dir_finished_connecting(dir_connection_t *conn);
  2418. void connection_dir_request_failed(dir_connection_t *conn);
  2419. void directory_initiate_command(const char *address, uint32_t addr,
  2420. uint16_t or_port, uint16_t dir_port,
  2421. int supports_begindir, const char *digest,
  2422. uint8_t dir_purpose, uint8_t router_purpose,
  2423. int anonymized_connection,
  2424. const char *resource,
  2425. const char *payload, size_t payload_len);
  2426. int dir_split_resource_into_fingerprints(const char *resource,
  2427. smartlist_t *fp_out, int *compresseed_out,
  2428. int decode_hex, int sort_uniq);
  2429. char *directory_dump_request_log(void);
  2430. int router_supports_extrainfo(const char *identity_digest, int is_authority);
  2431. /********************************* dirserv.c ***************************/
  2432. #define UNNAMED_ROUTER_NICKNAME "Unnamed"
  2433. int connection_dirserv_flushed_some(dir_connection_t *conn);
  2434. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  2435. int dirserv_load_fingerprint_file(void);
  2436. void dirserv_free_fingerprint_list(void);
  2437. const char *dirserv_get_nickname_by_digest(const char *digest);
  2438. int dirserv_add_multiple_descriptors(const char *desc, uint8_t purpose,
  2439. const char **msg);
  2440. int dirserv_add_descriptor(routerinfo_t *ri, const char **msg);
  2441. int getinfo_helper_dirserv_unregistered(control_connection_t *conn,
  2442. const char *question, char **answer);
  2443. void dirserv_free_descriptors(void);
  2444. int dirserv_thinks_router_is_blatantly_unreachable(routerinfo_t *router,
  2445. time_t now);
  2446. int list_server_status(smartlist_t *routers, char **router_status_out,
  2447. int for_controller);
  2448. int dirserv_dump_directory_to_string(char **dir_out,
  2449. crypto_pk_env_t *private_key,
  2450. int complete);
  2451. void directory_set_dirty(void);
  2452. cached_dir_t *dirserv_get_directory(void);
  2453. cached_dir_t *dirserv_get_runningrouters(void);
  2454. cached_dir_t *dirserv_get_consensus(void);
  2455. void dirserv_set_cached_directory(const char *directory, time_t when,
  2456. int is_running_routers);
  2457. void dirserv_set_cached_networkstatus_v2(const char *directory,
  2458. const char *identity,
  2459. time_t published);
  2460. void dirserv_set_cached_networkstatus_v3(const char *consensus,
  2461. time_t published);
  2462. void dirserv_clear_old_networkstatuses(time_t cutoff);
  2463. void dirserv_clear_old_v1_info(time_t now);
  2464. void dirserv_get_networkstatus_v2(smartlist_t *result, const char *key);
  2465. void dirserv_get_networkstatus_v2_fingerprints(smartlist_t *result,
  2466. const char *key);
  2467. int dirserv_get_routerdesc_fingerprints(smartlist_t *fps_out, const char *key,
  2468. const char **msg);
  2469. int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
  2470. const char **msg);
  2471. void dirserv_orconn_tls_done(const char *address,
  2472. uint16_t or_port,
  2473. const char *digest_rcvd,
  2474. int as_advertised);
  2475. void dirserv_test_reachability(time_t now, int try_all);
  2476. int authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
  2477. int complain);
  2478. int dirserv_would_reject_router(routerstatus_t *rs);
  2479. int dirserv_remove_old_statuses(smartlist_t *fps, time_t cutoff);
  2480. int dirserv_have_any_serverdesc(smartlist_t *fps, int spool_src);
  2481. size_t dirserv_estimate_data_size(smartlist_t *fps, int is_serverdescs,
  2482. int compressed);
  2483. int routerstatus_format_entry(char *buf, size_t buf_len,
  2484. routerstatus_t *rs, const char *platform,
  2485. int first_line_only);
  2486. void dirserv_free_all(void);
  2487. void cached_dir_decref(cached_dir_t *d);
  2488. cached_dir_t *new_cached_dir(char *s, time_t published);
  2489. cached_dir_t *generate_v3_networkstatus(void);
  2490. #ifdef DIRSERV_PRIVATE
  2491. char *
  2492. format_networkstatus_vote(crypto_pk_env_t *private_key,
  2493. networkstatus_vote_t *v3_ns);
  2494. #endif
  2495. /********************************* dirvote.c ************************/
  2496. /* XXXX020 enforce */
  2497. /* XXXX020 document in dir-spec.txt */
  2498. /*DOCDOC*/
  2499. #define MIN_VOTE_SECONDS 20
  2500. #define MIN_DIST_SECONDS 20
  2501. #define MIN_VOTE_INTERVAL 300
  2502. void dirvote_free_all(void);
  2503. /* vote manipulation */
  2504. void networkstatus_vote_free(networkstatus_vote_t *ns);
  2505. char *networkstatus_compute_consensus(smartlist_t *votes,
  2506. int total_authorities,
  2507. crypto_pk_env_t *identity_key,
  2508. crypto_pk_env_t *signing_key);
  2509. networkstatus_voter_info_t *networkstatus_get_voter_by_id(
  2510. networkstatus_vote_t *vote,
  2511. const char *identity);
  2512. int networkstatus_check_consensus_signature(networkstatus_vote_t *consensus);
  2513. int networkstatus_add_consensus_signatures(networkstatus_vote_t *target,
  2514. networkstatus_vote_t *src,
  2515. char **new_signatures_out,
  2516. int *regenerate_out);
  2517. int networkstatus_add_detached_signatures(networkstatus_vote_t *target,
  2518. ns_detached_signatures_t *sigs,
  2519. char **new_signatures_out,
  2520. int *regenerate_out);
  2521. char *networkstatus_get_detached_signatures(networkstatus_vote_t *consensus);
  2522. void ns_detached_signatures_free(ns_detached_signatures_t *s);
  2523. /* cert manipulation */
  2524. void authority_cert_free(authority_cert_t *cert);
  2525. authority_cert_t *authority_cert_dup(authority_cert_t *cert);
  2526. /** DOCDOC */
  2527. typedef struct vote_timing_t {
  2528. int vote_interval;
  2529. int n_intervals_valid;
  2530. int vote_delay;
  2531. int dist_delay;
  2532. } vote_timing_t;
  2533. /* vote scheduling */
  2534. void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
  2535. time_t dirvote_get_start_of_next_interval(time_t now, int interval);
  2536. void dirvote_recalculate_timing(time_t now);
  2537. void dirvote_act(time_t now);
  2538. /* invoked on timers and by outside triggers. */
  2539. void dirvote_perform_vote(void);
  2540. void dirvote_clear_pending_votes(void);
  2541. struct pending_vote_t * dirvote_add_vote(const char *vote_body,
  2542. const char **msg_out,
  2543. int *status_out);
  2544. int dirvote_compute_consensus(void);
  2545. int dirvote_add_signatures(const char *detached_signatures_body);
  2546. int dirvote_publish_consensus(void);
  2547. /* Item access */
  2548. const char *dirvote_get_pending_consensus(void);
  2549. const char *dirvote_get_pending_detached_signatures(void);
  2550. const cached_dir_t *dirvote_get_vote(const char *id);
  2551. #ifdef DIRVOTE_PRIVATE
  2552. time_t median_time(smartlist_t *times);
  2553. int median_int(smartlist_t *times);
  2554. int networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  2555. networkstatus_voter_info_t *voter,
  2556. authority_cert_t *cert);
  2557. #endif
  2558. /********************************* dns.c ***************************/
  2559. int dns_init(void);
  2560. void dns_free_all(void);
  2561. uint32_t dns_clip_ttl(uint32_t ttl);
  2562. int dns_reset(void);
  2563. void connection_dns_remove(edge_connection_t *conn);
  2564. void assert_connection_edge_not_dns_pending(edge_connection_t *conn);
  2565. void assert_all_pending_dns_resolves_ok(void);
  2566. void dns_cancel_pending_resolve(const char *question);
  2567. int dns_resolve(edge_connection_t *exitconn);
  2568. void dns_launch_correctness_checks(void);
  2569. int dns_seems_to_be_broken(void);
  2570. void dns_reset_correctness_checks(void);
  2571. /********************************* dnsserv.c ************************/
  2572. void dnsserv_configure_listener(connection_t *conn);
  2573. void dnsserv_close_listener(connection_t *conn);
  2574. void dnsserv_resolved(edge_connection_t *conn,
  2575. int answer_type,
  2576. size_t answer_len,
  2577. const char *answer,
  2578. int ttl);
  2579. void dnsserv_reject_request(edge_connection_t *conn);
  2580. void dnsserv_launch_request(const char *name, int is_reverse);
  2581. /********************************* hibernate.c **********************/
  2582. int accounting_parse_options(or_options_t *options, int validate_only);
  2583. int accounting_is_enabled(or_options_t *options);
  2584. void configure_accounting(time_t now);
  2585. void accounting_run_housekeeping(time_t now);
  2586. void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
  2587. int accounting_record_bandwidth_usage(time_t now, or_state_t *state);
  2588. void hibernate_begin_shutdown(void);
  2589. int we_are_hibernating(void);
  2590. void consider_hibernation(time_t now);
  2591. int getinfo_helper_accounting(control_connection_t *conn,
  2592. const char *question, char **answer);
  2593. void accounting_set_bandwidth_usage_from_state(or_state_t *state);
  2594. /********************************* main.c ***************************/
  2595. extern int has_completed_circuit;
  2596. int connection_add(connection_t *conn);
  2597. int connection_remove(connection_t *conn);
  2598. int connection_in_array(connection_t *conn);
  2599. void add_connection_to_closeable_list(connection_t *conn);
  2600. int connection_is_on_closeable_list(connection_t *conn);
  2601. smartlist_t *get_connection_array(void);
  2602. void connection_watch_events(connection_t *conn, short events);
  2603. int connection_is_reading(connection_t *conn);
  2604. void connection_stop_reading(connection_t *conn);
  2605. void connection_start_reading(connection_t *conn);
  2606. int connection_is_writing(connection_t *conn);
  2607. void connection_stop_writing(connection_t *conn);
  2608. void connection_start_writing(connection_t *conn);
  2609. void connection_stop_reading_from_linked_conn(connection_t *conn);
  2610. void directory_all_unreachable(time_t now);
  2611. void directory_info_has_arrived(time_t now, int from_cache);
  2612. void ip_address_changed(int at_interface);
  2613. void dns_servers_relaunch_checks(void);
  2614. void control_signal_act(int the_signal);
  2615. void handle_signals(int is_parent);
  2616. void tor_cleanup(void);
  2617. void tor_free_all(int postfork);
  2618. int tor_main(int argc, char *argv[]);
  2619. /********************************* onion.c ***************************/
  2620. int onion_pending_add(or_circuit_t *circ);
  2621. or_circuit_t *onion_next_task(void);
  2622. void onion_pending_remove(or_circuit_t *circ);
  2623. int onion_skin_create(crypto_pk_env_t *router_key,
  2624. crypto_dh_env_t **handshake_state_out,
  2625. char *onion_skin_out);
  2626. int onion_skin_server_handshake(const char *onion_skin,
  2627. crypto_pk_env_t *private_key,
  2628. crypto_pk_env_t *prev_private_key,
  2629. char *handshake_reply_out,
  2630. char *key_out,
  2631. size_t key_out_len);
  2632. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  2633. const char *handshake_reply,
  2634. char *key_out,
  2635. size_t key_out_len);
  2636. int fast_server_handshake(const char *key_in,
  2637. char *handshake_reply_out,
  2638. char *key_out,
  2639. size_t key_out_len);
  2640. int fast_client_handshake(const char *handshake_state,
  2641. const char *handshake_reply_out,
  2642. char *key_out,
  2643. size_t key_out_len);
  2644. void clear_pending_onions(void);
  2645. /********************************* policies.c ************************/
  2646. /* (length of "accept 255.255.255.255/255.255.255.255:65535-65535\n" plus a
  2647. * nul.)
  2648. */
  2649. #define POLICY_BUF_LEN 52
  2650. typedef enum {
  2651. ADDR_POLICY_ACCEPTED=0,
  2652. ADDR_POLICY_REJECTED=-1,
  2653. ADDR_POLICY_PROBABLY_ACCEPTED=1,
  2654. ADDR_POLICY_PROBABLY_REJECTED=2
  2655. } addr_policy_result_t;
  2656. int firewall_is_fascist_or(void);
  2657. int fascist_firewall_allows_address_or(uint32_t addr, uint16_t port);
  2658. int fascist_firewall_allows_address_dir(uint32_t addr, uint16_t port);
  2659. int dir_policy_permits_address(uint32_t addr);
  2660. int socks_policy_permits_address(uint32_t addr);
  2661. int authdir_policy_permits_address(uint32_t addr, uint16_t port);
  2662. int authdir_policy_valid_address(uint32_t addr, uint16_t port);
  2663. int authdir_policy_badexit_address(uint32_t addr, uint16_t port);
  2664. int validate_addr_policies(or_options_t *options, char **msg);
  2665. void policies_parse_from_options(or_options_t *options);
  2666. int cmp_addr_policies(addr_policy_t *a, addr_policy_t *b);
  2667. addr_policy_result_t compare_addr_to_addr_policy(uint32_t addr,
  2668. uint16_t port, addr_policy_t *policy);
  2669. int policies_parse_exit_policy(config_line_t *cfg,
  2670. addr_policy_t **dest,
  2671. int rejectprivate);
  2672. int exit_policy_is_general_exit(addr_policy_t *policy);
  2673. int policy_is_reject_star(addr_policy_t *policy);
  2674. int getinfo_helper_policies(control_connection_t *conn,
  2675. const char *question, char **answer);
  2676. int policy_write_item(char *buf, size_t buflen, addr_policy_t *policy);
  2677. void addr_policy_free(addr_policy_t *p);
  2678. void policies_free_all(void);
  2679. /********************************* relay.c ***************************/
  2680. extern uint64_t stats_n_relay_cells_relayed;
  2681. extern uint64_t stats_n_relay_cells_delivered;
  2682. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  2683. int cell_direction);
  2684. void relay_header_pack(char *dest, const relay_header_t *src);
  2685. void relay_header_unpack(relay_header_t *dest, const char *src);
  2686. int relay_send_command_from_edge(uint16_t stream_id, circuit_t *circ,
  2687. int relay_command, const char *payload,
  2688. size_t payload_len, crypt_path_t *cpath_layer);
  2689. int connection_edge_send_command(edge_connection_t *fromconn,
  2690. int relay_command, const char *payload,
  2691. size_t payload_len);
  2692. int connection_edge_package_raw_inbuf(edge_connection_t *conn,
  2693. int package_partial);
  2694. void connection_edge_consider_sending_sendme(edge_connection_t *conn);
  2695. socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
  2696. int errno_to_end_reason(int e);
  2697. extern uint64_t stats_n_data_cells_packaged;
  2698. extern uint64_t stats_n_data_bytes_packaged;
  2699. extern uint64_t stats_n_data_cells_received;
  2700. extern uint64_t stats_n_data_bytes_received;
  2701. void init_cell_pool(void);
  2702. void free_cell_pool(void);
  2703. void clean_cell_pool(void);
  2704. void dump_cell_pool_usage(int severity);
  2705. void cell_queue_clear(cell_queue_t *queue);
  2706. void cell_queue_append(cell_queue_t *queue, packed_cell_t *cell);
  2707. void cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell);
  2708. void append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
  2709. cell_t *cell, int direction);
  2710. void connection_or_unlink_all_active_circs(or_connection_t *conn);
  2711. int connection_or_flush_from_first_active_circuit(or_connection_t *conn,
  2712. int max);
  2713. void assert_active_circuits_ok(or_connection_t *orconn);
  2714. void make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn);
  2715. void make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn);
  2716. void circuit_clear_cell_queue(circuit_t *circ, or_connection_t *orconn);
  2717. /********************************* rephist.c ***************************/
  2718. void rep_hist_init(void);
  2719. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  2720. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  2721. void rep_hist_note_disconnect(const char* nickname, time_t when);
  2722. void rep_hist_note_connection_died(const char* nickname, time_t when);
  2723. void rep_hist_note_extend_succeeded(const char *from_name,
  2724. const char *to_name);
  2725. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  2726. void rep_hist_dump_stats(time_t now, int severity);
  2727. void rep_hist_note_bytes_read(int num_bytes, time_t when);
  2728. void rep_hist_note_bytes_written(int num_bytes, time_t when);
  2729. int rep_hist_bandwidth_assess(void);
  2730. char *rep_hist_get_bandwidth_lines(int for_extrainfo);
  2731. void rep_hist_update_state(or_state_t *state);
  2732. int rep_hist_load_state(or_state_t *state, char **err);
  2733. void rep_history_clean(time_t before);
  2734. void rep_hist_note_router_reachable(const char *id, time_t when);
  2735. void rep_hist_note_router_unreachable(const char *id, time_t when);
  2736. int rep_hist_record_mtbf_data(const char *filename);
  2737. int rep_hist_load_mtbf_data(const char *filename, time_t now);
  2738. time_t rep_hist_downrate_old_runs(time_t now);
  2739. double rep_hist_get_stability(const char *id, time_t when);
  2740. void rep_hist_note_used_port(uint16_t port, time_t now);
  2741. smartlist_t *rep_hist_get_predicted_ports(time_t now);
  2742. void rep_hist_note_used_resolve(time_t now);
  2743. void rep_hist_note_used_internal(time_t now, int need_uptime,
  2744. int need_capacity);
  2745. int rep_hist_get_predicted_internal(time_t now, int *need_uptime,
  2746. int *need_capacity);
  2747. int any_predicted_circuits(time_t now);
  2748. int rep_hist_circbuilding_dormant(time_t now);
  2749. /** Possible public/private key operations in Tor: used to keep track of where
  2750. * we're spending our time. */
  2751. typedef enum {
  2752. SIGN_DIR, SIGN_RTR,
  2753. VERIFY_DIR, VERIFY_RTR,
  2754. ENC_ONIONSKIN, DEC_ONIONSKIN,
  2755. TLS_HANDSHAKE_C, TLS_HANDSHAKE_S,
  2756. REND_CLIENT, REND_MID, REND_SERVER,
  2757. } pk_op_t;
  2758. void note_crypto_pk_op(pk_op_t operation);
  2759. void dump_pk_ops(int severity);
  2760. void rep_hist_free_all(void);
  2761. /* for hidden service usage statistics */
  2762. void hs_usage_note_publish_total(const char *service_id, time_t now);
  2763. void hs_usage_note_publish_novel(const char *service_id, time_t now);
  2764. void hs_usage_note_fetch_total(const char *service_id, time_t now);
  2765. void hs_usage_note_fetch_successful(const char *service_id, time_t now);
  2766. void hs_usage_write_statistics_to_file(time_t now);
  2767. void hs_usage_free_all(void);
  2768. /********************************* rendclient.c ***************************/
  2769. void rend_client_introcirc_has_opened(origin_circuit_t *circ);
  2770. void rend_client_rendcirc_has_opened(origin_circuit_t *circ);
  2771. int rend_client_introduction_acked(origin_circuit_t *circ, const char *request,
  2772. size_t request_len);
  2773. void rend_client_refetch_renddesc(const char *query);
  2774. int rend_client_remove_intro_point(extend_info_t *failed_intro,
  2775. const char *query);
  2776. int rend_client_rendezvous_acked(origin_circuit_t *circ, const char *request,
  2777. size_t request_len);
  2778. int rend_client_receive_rendezvous(origin_circuit_t *circ, const char *request,
  2779. size_t request_len);
  2780. void rend_client_desc_here(const char *query);
  2781. extend_info_t *rend_client_get_random_intro(const char *query);
  2782. int rend_client_send_introduction(origin_circuit_t *introcirc,
  2783. origin_circuit_t *rendcirc);
  2784. /********************************* rendcommon.c ***************************/
  2785. /** Information used to connect to a hidden service. */
  2786. typedef struct rend_service_descriptor_t {
  2787. crypto_pk_env_t *pk; /**< This service's public key. */
  2788. int version; /**< 0 or 1. */
  2789. time_t timestamp; /**< Time when the descriptor was generated. */
  2790. uint16_t protocols; /**< Bitmask: which rendezvous protocols are supported?
  2791. * (We allow bits '0', '1', and '2' to be set.) */
  2792. int n_intro_points; /**< Number of introduction points. */
  2793. /** Array of n_intro_points elements for this service's introduction points'
  2794. * nicknames. Elements are removed from this array if introduction attempts
  2795. * fail. */
  2796. char **intro_points;
  2797. /** Array of n_intro_points elements for this service's introduction points'
  2798. * extend_infos, or NULL if this descriptor is V0. Elements are removed
  2799. * from this array if introduction attempts fail. If this array is present,
  2800. * its elements correspond to the elements of intro_points. */
  2801. extend_info_t **intro_point_extend_info;
  2802. } rend_service_descriptor_t;
  2803. int rend_cmp_service_ids(const char *one, const char *two);
  2804. void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  2805. const char *payload);
  2806. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  2807. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  2808. int version,
  2809. crypto_pk_env_t *key,
  2810. char **str_out,
  2811. size_t *len_out);
  2812. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str,
  2813. size_t len);
  2814. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  2815. /** A cached rendezvous descriptor. */
  2816. typedef struct rend_cache_entry_t {
  2817. size_t len; /**< Length of <b>desc</b> */
  2818. time_t received; /**< When was the descriptor received? */
  2819. char *desc; /**< Service descriptor */
  2820. rend_service_descriptor_t *parsed; /**< Parsed value of 'desc' */
  2821. } rend_cache_entry_t;
  2822. void rend_cache_init(void);
  2823. void rend_cache_clean(void);
  2824. void rend_cache_free_all(void);
  2825. int rend_valid_service_id(const char *query);
  2826. int rend_cache_lookup_desc(const char *query, int version, const char **desc,
  2827. size_t *desc_len);
  2828. int rend_cache_lookup_entry(const char *query, int version,
  2829. rend_cache_entry_t **entry_out);
  2830. int rend_cache_store(const char *desc, size_t desc_len, int published);
  2831. int rend_cache_size(void);
  2832. /********************************* rendservice.c ***************************/
  2833. int num_rend_services(void);
  2834. int rend_config_services(or_options_t *options, int validate_only);
  2835. int rend_service_load_keys(void);
  2836. void rend_services_init(void);
  2837. void rend_services_introduce(void);
  2838. void rend_consider_services_upload(time_t now);
  2839. void rend_service_intro_has_opened(origin_circuit_t *circuit);
  2840. int rend_service_intro_established(origin_circuit_t *circuit,
  2841. const char *request,
  2842. size_t request_len);
  2843. void rend_service_rendezvous_has_opened(origin_circuit_t *circuit);
  2844. int rend_service_introduce(origin_circuit_t *circuit, const char *request,
  2845. size_t request_len);
  2846. void rend_service_relaunch_rendezvous(origin_circuit_t *oldcirc);
  2847. int rend_service_set_connection_addr_port(edge_connection_t *conn,
  2848. origin_circuit_t *circ);
  2849. void rend_service_dump_stats(int severity);
  2850. void rend_service_free_all(void);
  2851. /********************************* rendmid.c *******************************/
  2852. int rend_mid_establish_intro(or_circuit_t *circ, const char *request,
  2853. size_t request_len);
  2854. int rend_mid_introduce(or_circuit_t *circ, const char *request,
  2855. size_t request_len);
  2856. int rend_mid_establish_rendezvous(or_circuit_t *circ, const char *request,
  2857. size_t request_len);
  2858. int rend_mid_rendezvous(or_circuit_t *circ, const char *request,
  2859. size_t request_len);
  2860. /********************************* router.c ***************************/
  2861. crypto_pk_env_t *get_onion_key(void);
  2862. time_t get_onion_key_set_at(void);
  2863. void set_identity_key(crypto_pk_env_t *k);
  2864. crypto_pk_env_t *get_identity_key(void);
  2865. int identity_key_is_set(void);
  2866. authority_cert_t *get_my_v3_authority_cert(void);
  2867. crypto_pk_env_t *get_my_v3_authority_signing_key(void);
  2868. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  2869. void rotate_onion_key(void);
  2870. crypto_pk_env_t *init_key_from_file(const char *fname, int generate,
  2871. int severity);
  2872. int init_keys(void);
  2873. int check_whether_orport_reachable(void);
  2874. int check_whether_dirport_reachable(void);
  2875. void consider_testing_reachability(int test_or, int test_dir);
  2876. void router_orport_found_reachable(void);
  2877. void router_dirport_found_reachable(void);
  2878. void router_perform_bandwidth_test(int num_circs, time_t now);
  2879. int authdir_mode(or_options_t *options);
  2880. int authdir_mode_v1(or_options_t *options);
  2881. int authdir_mode_v2(or_options_t *options);
  2882. int authdir_mode_v3(or_options_t *options);
  2883. int authdir_mode_handles_descs(or_options_t *options);
  2884. int authdir_mode_publishes_statuses(or_options_t *options);
  2885. int authdir_mode_tests_reachability(or_options_t *options);
  2886. int authdir_mode_bridge(or_options_t *options);
  2887. int clique_mode(or_options_t *options);
  2888. int server_mode(or_options_t *options);
  2889. int advertised_server_mode(void);
  2890. int proxy_mode(or_options_t *options);
  2891. void consider_publishable_server(int force);
  2892. int router_is_clique_mode(routerinfo_t *router);
  2893. void router_upload_dir_desc_to_dirservers(int force);
  2894. void mark_my_descriptor_dirty_if_older_than(time_t when);
  2895. void mark_my_descriptor_dirty(void);
  2896. void check_descriptor_bandwidth_changed(time_t now);
  2897. void check_descriptor_ipaddress_changed(time_t now);
  2898. void router_new_address_suggestion(const char *suggestion);
  2899. int router_compare_to_my_exit_policy(edge_connection_t *conn);
  2900. routerinfo_t *router_get_my_routerinfo(void);
  2901. extrainfo_t *router_get_my_extrainfo(void);
  2902. const char *router_get_my_descriptor(void);
  2903. int router_digest_is_me(const char *digest);
  2904. int router_is_me(routerinfo_t *router);
  2905. int router_fingerprint_is_me(const char *fp);
  2906. int router_pick_published_address(or_options_t *options, uint32_t *addr);
  2907. int router_rebuild_descriptor(int force);
  2908. int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  2909. crypto_pk_env_t *ident_key);
  2910. int extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo,
  2911. crypto_pk_env_t *ident_key);
  2912. int is_legal_nickname(const char *s);
  2913. int is_legal_nickname_or_hexdigest(const char *s);
  2914. int is_legal_hexdigest(const char *s);
  2915. void router_get_verbose_nickname(char *buf, routerinfo_t *router);
  2916. void router_reset_warnings(void);
  2917. void router_reset_reachability(void);
  2918. void router_free_all(void);
  2919. #ifdef ROUTER_PRIVATE
  2920. /* Used only by router.c and test.c */
  2921. void get_platform_str(char *platform, size_t len);
  2922. #endif
  2923. /********************************* routerlist.c ***************************/
  2924. /** Represents information about a single trusted directory server. */
  2925. typedef struct trusted_dir_server_t {
  2926. char *description;
  2927. char *nickname;
  2928. char *address; /**< Hostname. */
  2929. uint32_t addr; /**< IPv4 address. */
  2930. uint16_t dir_port; /**< Directory port. */
  2931. uint16_t or_port; /**< OR port: Used for tunneling connections. */
  2932. char digest[DIGEST_LEN]; /**< Digest of identity key. */
  2933. char v3_identity_digest[DIGEST_LEN]; /**< Digest of v3 (authority only,
  2934. * high-security) identity key. */
  2935. unsigned int is_running:1; /**< True iff we think this server is running. */
  2936. /** True iff this server has accepted the most recent server descriptor
  2937. * we tried to upload to it. */
  2938. unsigned int has_accepted_serverdesc:1;
  2939. /** What kind of authority is this? (Bitfield.) */
  2940. authority_type_t type;
  2941. smartlist_t *v3_certs; /**< V3 key certificates for this authority */
  2942. int n_networkstatus_failures; /**< How many times have we asked for this
  2943. * server's network-status unsuccessfully? */
  2944. local_routerstatus_t fake_status; /**< Used when we need to pass this trusted
  2945. * dir_server_t to directory_initiate_command_*
  2946. * as a routerstatus_t. Not updated by the
  2947. * router-status management code!
  2948. **/
  2949. } trusted_dir_server_t;
  2950. int router_reload_router_list(void);
  2951. int get_n_authorities(authority_type_t type);
  2952. int router_reload_networkstatus(void);
  2953. smartlist_t *router_get_trusted_dir_servers(void);
  2954. routerstatus_t *router_pick_directory_server(int requireother,
  2955. int fascistfirewall,
  2956. authority_type_t type,
  2957. int retry_if_no_servers);
  2958. routerstatus_t *router_pick_trusteddirserver(authority_type_t type,
  2959. int requireother,
  2960. int fascistfirewall,
  2961. int retry_if_no_servers);
  2962. trusted_dir_server_t *router_get_trusteddirserver_by_digest(
  2963. const char *digest);
  2964. trusted_dir_server_t *trusteddirserver_get_by_v3_auth_digest(
  2965. const char *digest);
  2966. authority_cert_t *authority_cert_get_newest_by_id(const char *id_digest);
  2967. authority_cert_t *authority_cert_get_by_sk_digest(const char *sk_digest);
  2968. authority_cert_t *authority_cert_get_by_digests(const char *id_digest,
  2969. const char *sk_digest);
  2970. void routerlist_add_family(smartlist_t *sl, routerinfo_t *router);
  2971. void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
  2972. int must_be_running);
  2973. int router_nickname_is_in_list(routerinfo_t *router, const char *list);
  2974. routerinfo_t *routerlist_find_my_routerinfo(void);
  2975. routerinfo_t *router_find_exact_exit_enclave(const char *address,
  2976. uint16_t port);
  2977. #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
  2978. #define ROUTER_MAX_DECLARED_BANDWIDTH INT32_MAX
  2979. int router_is_unreliable(routerinfo_t *router, int need_uptime,
  2980. int need_capacity, int need_guard);
  2981. uint32_t router_get_advertised_bandwidth(routerinfo_t *router);
  2982. routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl, int for_exit);
  2983. routerstatus_t *routerstatus_sl_choose_by_bandwidth(smartlist_t *sl);
  2984. routerinfo_t *router_choose_random_node(const char *preferred,
  2985. const char *excluded,
  2986. smartlist_t *excludedsmartlist,
  2987. int need_uptime, int need_bandwidth,
  2988. int need_guard,
  2989. int allow_invalid, int strict,
  2990. int weight_for_exit);
  2991. routerinfo_t *router_get_by_nickname(const char *nickname,
  2992. int warn_if_unnamed);
  2993. int hexdigest_to_digest(const char *hexdigest, char *digest);
  2994. routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
  2995. routerinfo_t *router_get_by_digest(const char *digest);
  2996. signed_descriptor_t *router_get_by_descriptor_digest(const char *digest);
  2997. signed_descriptor_t *router_get_by_extrainfo_digest(const char *digest);
  2998. signed_descriptor_t *extrainfo_get_by_descriptor_digest(const char *digest);
  2999. const char *signed_descriptor_get_body(signed_descriptor_t *desc);
  3000. int router_digest_version_as_new_as(const char *digest, const char *cutoff);
  3001. int router_digest_is_trusted_dir(const char *digest);
  3002. routerlist_t *router_get_routerlist(void);
  3003. void routerlist_reset_warnings(void);
  3004. void routerlist_free(routerlist_t *routerlist);
  3005. void dump_routerlist_mem_usage(int severity);
  3006. void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old);
  3007. void routerinfo_free(routerinfo_t *router);
  3008. void extrainfo_free(extrainfo_t *extrainfo);
  3009. void routerstatus_free(routerstatus_t *routerstatus);
  3010. void networkstatus_free(networkstatus_t *networkstatus);
  3011. void routerlist_free_all(void);
  3012. void router_set_status(const char *digest, int up);
  3013. void routerlist_remove_old_routers(void);
  3014. void networkstatus_list_clean(time_t now);
  3015. int router_add_to_routerlist(routerinfo_t *router, const char **msg,
  3016. int from_cache, int from_fetch);
  3017. void router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg,
  3018. int from_cache, int from_fetch);
  3019. int router_load_single_router(const char *s, uint8_t purpose,
  3020. const char **msg);
  3021. void router_load_routers_from_string(const char *s, const char *eos,
  3022. saved_location_t saved_location,
  3023. smartlist_t *requested_fingerprints,
  3024. uint8_t purpose);
  3025. void router_load_extrainfo_from_string(const char *s, const char *eos,
  3026. saved_location_t saved_location,
  3027. smartlist_t *requested_fps);
  3028. typedef enum {
  3029. NS_FROM_CACHE, NS_FROM_DIR_BY_FP, NS_FROM_DIR_ALL, NS_GENERATED
  3030. } networkstatus_source_t;
  3031. int router_set_networkstatus(const char *s, time_t arrived_at,
  3032. networkstatus_source_t source,
  3033. smartlist_t *requested_fingerprints);
  3034. char *networkstatus_get_cache_filename(const char *identity_digest);
  3035. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
  3036. int need_uptime);
  3037. int router_exit_policy_rejects_all(routerinfo_t *router);
  3038. void add_trusted_dir_server(const char *nickname, const char *address,
  3039. uint16_t dir_port, uint16_t or_port,
  3040. const char *digest,
  3041. const char *v3_auth_digest,
  3042. authority_type_t type);
  3043. void clear_trusted_dir_servers(void);
  3044. int any_trusted_dir_is_v1_authority(void);
  3045. networkstatus_t *networkstatus_get_by_digest(const char *digest);
  3046. local_routerstatus_t *router_get_combined_status_by_digest(const char *digest);
  3047. local_routerstatus_t *router_get_combined_status_by_descriptor_digest(
  3048. const char *digest);
  3049. /* for consensuses. */
  3050. int router_reload_consensus_networkstatus(void);
  3051. networkstatus_vote_t *networkstatus_get_latest_consensus(void);
  3052. networkstatus_vote_t *networkstatus_get_live_consensus(time_t now);
  3053. int networkstatus_set_current_consensus(const char *consensus, int from_cache);
  3054. //routerstatus_t *routerstatus_get_by_hexdigest(const char *hexdigest);
  3055. int should_delay_dir_fetches(or_options_t *options);
  3056. void update_networkstatus_downloads(time_t now);
  3057. void routerlist_retry_directory_downloads(time_t now);
  3058. void update_router_descriptor_downloads(time_t now);
  3059. void update_extrainfo_downloads(time_t now);
  3060. void routers_update_all_from_networkstatus(time_t now);
  3061. void routers_update_status_from_networkstatus(smartlist_t *routers,
  3062. int reset_failures);
  3063. smartlist_t *router_list_superseded(void);
  3064. int router_have_minimum_dir_info(void);
  3065. void networkstatus_list_update_recent(time_t now);
  3066. void router_reset_descriptor_download_failures(void);
  3067. void router_reset_status_download_failures(void);
  3068. int router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2);
  3069. int routerinfo_incompatible_with_extrainfo(routerinfo_t *ri, extrainfo_t *ei,
  3070. const char **msg);
  3071. const char *esc_router_info(routerinfo_t *router);
  3072. char *networkstatus_getinfo_helper_single(routerstatus_t *rs);
  3073. int getinfo_helper_networkstatus(control_connection_t *conn,
  3074. const char *question, char **answer);
  3075. void routerlist_assert_ok(routerlist_t *rl);
  3076. void routerlist_check_bug_417(void);
  3077. int trusted_dirs_reload_certs(void);
  3078. int trusted_dirs_load_certs_from_string(const char *contents, int from_store);
  3079. void trusted_dirs_flush_certs_to_disk(void);
  3080. /********************************* routerparse.c ************************/
  3081. #define MAX_STATUS_TAG_LEN 32
  3082. /** Structure to hold parsed Tor versions. This is a little messier
  3083. * than we would like it to be, because we changed version schemes with 0.1.0.
  3084. *
  3085. * See version-spec.txt for the whole business.
  3086. */
  3087. typedef struct tor_version_t {
  3088. int major;
  3089. int minor;
  3090. int micro;
  3091. /** Release status. For version in the post-0.1 format, this is always
  3092. * VER_RELEASE. */
  3093. enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
  3094. int patchlevel;
  3095. char status_tag[MAX_STATUS_TAG_LEN];
  3096. int svn_revision;
  3097. } tor_version_t;
  3098. typedef enum version_status_t {
  3099. VS_RECOMMENDED=0, /**< This version is listed as recommended. */
  3100. VS_OLD=1, /**< This version is older than any recommended version. */
  3101. VS_NEW=2, /**< This version is newer than any recommended version. */
  3102. VS_NEW_IN_SERIES=3, /**< This version is newer than any recommended version
  3103. * in its series, but later recommended versions exist.
  3104. */
  3105. VS_UNRECOMMENDED=4 /**< This version is not recommended (general case). */
  3106. } version_status_t;
  3107. typedef struct combined_version_status_t {
  3108. /** How many networkstatuses claim to know about versions? */
  3109. int n_versioning;
  3110. /** What do the majority of networkstatuses believe about this version? */
  3111. version_status_t consensus;
  3112. /** How many networkstatuses constitute the majority? */
  3113. int n_concurring;
  3114. } combined_version_status_t;
  3115. char *compute_recommended_versions(time_t now, int client,
  3116. const char *my_version,
  3117. combined_version_status_t *status_out);
  3118. int router_get_router_hash(const char *s, char *digest);
  3119. int router_get_dir_hash(const char *s, char *digest);
  3120. int router_get_runningrouters_hash(const char *s, char *digest);
  3121. int router_get_networkstatus_v2_hash(const char *s, char *digest);
  3122. int router_get_networkstatus_v3_hash(const char *s, char *digest);
  3123. int router_get_extrainfo_hash(const char *s, char *digest);
  3124. int router_append_dirobj_signature(char *buf, size_t buf_len,
  3125. const char *digest,
  3126. crypto_pk_env_t *private_key);
  3127. int router_parse_list_from_string(const char **s, const char *eos,
  3128. smartlist_t *dest,
  3129. saved_location_t saved_location,
  3130. int is_extrainfo);
  3131. int router_parse_routerlist_from_directory(const char *s,
  3132. routerlist_t **dest,
  3133. crypto_pk_env_t *pkey,
  3134. int check_version,
  3135. int write_to_cache);
  3136. int router_parse_runningrouters(const char *str);
  3137. int router_parse_directory(const char *str);
  3138. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end,
  3139. int cache_copy);
  3140. extrainfo_t *extrainfo_parse_entry_from_string(const char *s, const char *end,
  3141. int cache_copy, struct digest_ri_map_t *routermap);
  3142. addr_policy_t *router_parse_addr_policy_from_string(const char *s,
  3143. int assume_action);
  3144. version_status_t tor_version_is_obsolete(const char *myversion,
  3145. const char *versionlist);
  3146. version_status_t version_status_join(version_status_t a, version_status_t b);
  3147. int tor_version_parse(const char *s, tor_version_t *out);
  3148. int tor_version_as_new_as(const char *platform, const char *cutoff);
  3149. int tor_version_compare(tor_version_t *a, tor_version_t *b);
  3150. void sort_version_list(smartlist_t *lst, int remove_duplicates);
  3151. void assert_addr_policy_ok(addr_policy_t *t);
  3152. void dump_distinct_digest_count(int severity);
  3153. networkstatus_t *networkstatus_parse_from_string(const char *s);
  3154. networkstatus_vote_t *networkstatus_parse_vote_from_string(const char *s,
  3155. int is_vote);
  3156. ns_detached_signatures_t *networkstatus_parse_detached_signatures(
  3157. const char *s, const char *eos);
  3158. authority_cert_t *authority_cert_parse_from_string(const char *s,
  3159. const char **end_of_string);
  3160. #endif