or.h 80 KB

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