or.h 96 KB

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