or.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file or.h
  8. *
  9. * \brief Master header file for Tor-specific functionality.
  10. */
  11. #ifndef __OR_H
  12. #define __OR_H
  13. #define OR_H_ID "$Id$"
  14. #include "orconfig.h"
  15. #ifdef MS_WINDOWS
  16. #define WIN32_WINNT 0x400
  17. #define _WIN32_WINNT 0x400
  18. #define WIN32_LEAN_AND_MEAN
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #ifdef HAVE_SIGNAL_H
  30. #include <signal.h>
  31. #endif
  32. #ifdef HAVE_NETDB_H
  33. #include <netdb.h>
  34. #endif
  35. #ifdef HAVE_CTYPE_H
  36. #include <ctype.h>
  37. #endif
  38. #include "../common/torint.h"
  39. #include "../common/fakepoll.h"
  40. #ifdef HAVE_INTTYPES_H
  41. #include <inttypes.h>
  42. #endif
  43. #ifdef HAVE_SYS_PARAM_H
  44. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  45. #endif
  46. #ifdef HAVE_SYS_LIMITS_H
  47. #include <sys/limits.h>
  48. #endif
  49. #ifdef HAVE_MACHINE_LIMITS_H
  50. #ifndef __FreeBSD__
  51. /* FreeBSD has a bug where it complains that this file is obsolete,
  52. and I should migrate to using sys/limits. It complains even when
  53. I include both. */
  54. #include <machine/limits.h>
  55. #endif
  56. #endif
  57. #ifdef HAVE_SYS_TYPES_H
  58. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  59. #endif
  60. #ifdef HAVE_SYS_WAIT_H
  61. #include <sys/wait.h>
  62. #endif
  63. #ifdef HAVE_SYS_FCNTL_H
  64. #include <sys/fcntl.h>
  65. #endif
  66. #ifdef HAVE_FCNTL_H
  67. #include <fcntl.h>
  68. #endif
  69. #ifdef HAVE_SYS_IOCTL_H
  70. #include <sys/ioctl.h>
  71. #endif
  72. #ifdef HAVE_SYS_SOCKET_H
  73. #include <sys/socket.h>
  74. #endif
  75. #ifdef HAVE_SYS_TIME_H
  76. #include <sys/time.h>
  77. #endif
  78. #ifdef HAVE_SYS_STAT_H
  79. #include <sys/stat.h>
  80. #endif
  81. #ifdef HAVE_NETINET_IN_H
  82. #include <netinet/in.h>
  83. #endif
  84. #ifdef HAVE_ARPA_INET_H
  85. #include <arpa/inet.h>
  86. #endif
  87. #ifdef HAVE_ERRNO_H
  88. #include <errno.h>
  89. #endif
  90. #ifdef HAVE_ASSERT_H
  91. #include <assert.h>
  92. #endif
  93. #ifdef HAVE_TIME_H
  94. #include <time.h>
  95. #endif
  96. #ifdef MS_WINDOWS
  97. #if (_MSC_VER <= 1300)
  98. #include <winsock.h>
  99. #else
  100. #include <winsock2.h>
  101. #include <ws2tcpip.h>
  102. #endif
  103. #endif
  104. #ifdef MS_WINDOWS
  105. #include <io.h>
  106. #include <process.h>
  107. #include <direct.h>
  108. #include <windows.h>
  109. #define snprintf _snprintf
  110. #endif
  111. #include "../common/crypto.h"
  112. #include "../common/tortls.h"
  113. #include "../common/log.h"
  114. #include "../common/compat.h"
  115. #include "../common/container.h"
  116. #include "../common/util.h"
  117. #include "../common/torgzip.h"
  118. /** Upper bound on maximum simultaneous connections; can be lowered by
  119. * config file. */
  120. #define MAXCONNECTIONS 10000
  121. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  122. #define MAX_NICKNAME_LEN 19
  123. /* Hex digest plus dollar sign. */
  124. #define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
  125. #define MAX_DIR_SIZE 500000
  126. #ifdef TOR_PERF
  127. /** How long do we keep DNS cache entries before purging them? */
  128. #define MAX_DNS_ENTRY_AGE (150*60)
  129. #else
  130. #define MAX_DNS_ENTRY_AGE (15*60)
  131. #endif
  132. /** How often do we rotate onion keys? */
  133. #define OLD_MIN_ONION_KEY_LIFETIME (12*60*60) /* twice a day */
  134. #define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
  135. /** How often do we rotate TLS contexts? */
  136. #define MAX_SSL_KEY_LIFETIME (120*60)
  137. /** How old do we allow a router to get before removing it, either
  138. * from the descriptor list (for dirservers) or the router list (for others)?
  139. * In seconds. */
  140. #define ROUTER_MAX_AGE (60*60*24)
  141. typedef enum {
  142. CIRC_ID_TYPE_LOWER=0,
  143. CIRC_ID_TYPE_HIGHER=1
  144. } circ_id_type_t;
  145. #define _CONN_TYPE_MIN 3
  146. /** Type for sockets listening for OR connections. */
  147. #define CONN_TYPE_OR_LISTENER 3
  148. /** Type for OR-to-OR or OP-to-OR connections. */
  149. #define CONN_TYPE_OR 4
  150. /** Type for connections from final OR to chosen destination. */
  151. #define CONN_TYPE_EXIT 5
  152. /** Type for sockets listening for SOCKS connections. */
  153. #define CONN_TYPE_AP_LISTENER 6
  154. /** Type for SOCKS connections to OP. */
  155. #define CONN_TYPE_AP 7
  156. /** Type for sockets listening for HTTP connections to the directory server. */
  157. #define CONN_TYPE_DIR_LISTENER 8
  158. /** Type for HTTP connections to the directory server. */
  159. #define CONN_TYPE_DIR 9
  160. /** Type for connections to local dnsworker processes. */
  161. #define CONN_TYPE_DNSWORKER 10
  162. /** Type for connections to local cpuworker processes. */
  163. #define CONN_TYPE_CPUWORKER 11
  164. /** Type for listenting for connections from user interface process */
  165. #define CONN_TYPE_CONTROL_LISTENER 12
  166. /** Type for connections from user interface process */
  167. #define CONN_TYPE_CONTROL 13
  168. #define _CONN_TYPE_MAX 13
  169. #define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  170. /** State for any listener connection. */
  171. #define LISTENER_STATE_READY 0
  172. #define _DNSWORKER_STATE_MIN 1
  173. /** State for a connection to a dnsworker process that's idle. */
  174. #define DNSWORKER_STATE_IDLE 1
  175. /** State for a connection to a dnsworker process that's resolving a hostname. */
  176. #define DNSWORKER_STATE_BUSY 2
  177. #define _DNSWORKER_STATE_MAX 2
  178. #define _CPUWORKER_STATE_MIN 1
  179. /** State for a connection to a cpuworker process that's idle. */
  180. #define CPUWORKER_STATE_IDLE 1
  181. /** State for a connection to a cpuworker process that's processing a
  182. * handshake. */
  183. #define CPUWORKER_STATE_BUSY_ONION 2
  184. #define _CPUWORKER_STATE_MAX 2
  185. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  186. #define _OR_CONN_STATE_MIN 1
  187. /** State for a connection to an OR: waiting for connect() to finish. */
  188. #define OR_CONN_STATE_CONNECTING 1
  189. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  190. #define OR_CONN_STATE_HANDSHAKING 2
  191. /** State for a connection to an OR: Ready to send/receive cells. */
  192. #define OR_CONN_STATE_OPEN 3
  193. #define _OR_CONN_STATE_MAX 3
  194. #define _EXIT_CONN_STATE_MIN 1
  195. /** State for an exit connection: waiting for response from dns farm. */
  196. #define EXIT_CONN_STATE_RESOLVING 1
  197. /** State for an exit connection: waiting for connect() to finish. */
  198. #define EXIT_CONN_STATE_CONNECTING 2
  199. /** State for an exit connection: open and ready to transmit data. */
  200. #define EXIT_CONN_STATE_OPEN 3
  201. /** State for an exit connection: waiting to be removed. */
  202. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  203. #define _EXIT_CONN_STATE_MAX 4
  204. #if 0
  205. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  206. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  207. #endif
  208. /* the AP state values must be disjoint from the EXIT state values */
  209. #define _AP_CONN_STATE_MIN 5
  210. /** State for a SOCKS connection: waiting for SOCKS request. */
  211. #define AP_CONN_STATE_SOCKS_WAIT 5
  212. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  213. * rendezvous rescriptor. */
  214. #define AP_CONN_STATE_RENDDESC_WAIT 6
  215. /** State for a SOCKS connection: waiting for a completed circuit. */
  216. #define AP_CONN_STATE_CIRCUIT_WAIT 7
  217. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  218. #define AP_CONN_STATE_CONNECT_WAIT 8
  219. /** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
  220. #define AP_CONN_STATE_RESOLVE_WAIT 9
  221. /** State for a SOCKS connection: ready to send and receive. */
  222. #define AP_CONN_STATE_OPEN 10
  223. #define _AP_CONN_STATE_MAX 10
  224. #define _DIR_CONN_STATE_MIN 1
  225. /** State for connection to directory server: waiting for connect(). */
  226. #define DIR_CONN_STATE_CONNECTING 1
  227. /** State for connection to directory server: sending HTTP request. */
  228. #define DIR_CONN_STATE_CLIENT_SENDING 2
  229. /** State for connection to directory server: reading HTTP response. */
  230. #define DIR_CONN_STATE_CLIENT_READING 3
  231. /** State for connection at directory server: waiting for HTTP request. */
  232. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
  233. /** State for connection at directory server: sending HTTP response. */
  234. #define DIR_CONN_STATE_SERVER_WRITING 5
  235. #define _DIR_CONN_STATE_MAX 5
  236. #define _CONTROL_CONN_STATE_MIN 1
  237. #define CONTROL_CONN_STATE_OPEN 1
  238. #define CONTROL_CONN_STATE_NEEDAUTH 2
  239. #define _CONTROL_CONN_STATE_MAX 2
  240. #define _DIR_PURPOSE_MIN 1
  241. /** Purpose for connection to directory server: download a directory. */
  242. #define DIR_PURPOSE_FETCH_DIR 1
  243. /** Purpose for connection to directory server: download just the list
  244. * of running routers. */
  245. #define DIR_PURPOSE_FETCH_RUNNING_LIST 2
  246. /** Purpose for connection to directory server: download a rendezvous
  247. * descriptor. */
  248. #define DIR_PURPOSE_FETCH_RENDDESC 3
  249. /** Purpose for connection to directory server: set after a rendezvous
  250. * descriptor is downloaded. */
  251. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
  252. /** Purpose for connection to directory server: upload a server descriptor. */
  253. #define DIR_PURPOSE_UPLOAD_DIR 5
  254. /** Purpose for connection to directory server: upload a rendezvous
  255. * descriptor. */
  256. #define DIR_PURPOSE_UPLOAD_RENDDESC 6
  257. /** Purpose for connection at a directory server. */
  258. #define DIR_PURPOSE_SERVER 7
  259. #define _DIR_PURPOSE_MAX 7
  260. #define _EXIT_PURPOSE_MIN 1
  261. #define EXIT_PURPOSE_CONNECT 1
  262. #define EXIT_PURPOSE_RESOLVE 2
  263. #define _EXIT_PURPOSE_MAX 2
  264. /** Circuit state: I'm the OP, still haven't done all my handshakes. */
  265. #define CIRCUIT_STATE_BUILDING 0
  266. /** Circuit state: Waiting to process the onionskin. */
  267. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  268. /** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
  269. #define CIRCUIT_STATE_OR_WAIT 2
  270. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  271. #define CIRCUIT_STATE_OPEN 3
  272. #define _CIRCUIT_PURPOSE_MIN 1
  273. /* these circuits were initiated elsewhere */
  274. #define _CIRCUIT_PURPOSE_OR_MIN 1
  275. /** OR-side circuit purpose: normal circuit, at OR. */
  276. #define CIRCUIT_PURPOSE_OR 1
  277. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  278. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  279. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  280. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  281. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  282. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  283. #define _CIRCUIT_PURPOSE_OR_MAX 4
  284. /* these circuits originate at this node */
  285. /* here's how circ client-side purposes work:
  286. * normal circuits are C_GENERAL.
  287. * circuits that are c_introducing are either on their way to
  288. * becoming open, or they are open and waiting for a
  289. * suitable rendcirc before they send the intro.
  290. * circuits that are c_introduce_ack_wait have sent the intro,
  291. * but haven't gotten a response yet.
  292. * circuits that are c_establish_rend are either on their way
  293. * to becoming open, or they are open and have sent the
  294. * establish_rendezvous cell but haven't received an ack.
  295. * circuits that are c_rend_ready are open and have received a
  296. * rend ack, but haven't heard from bob yet. if they have a
  297. * buildstate->pending_final_cpath then they're expecting a
  298. * cell from bob, else they're not.
  299. * circuits that are c_rend_ready_intro_acked are open, and
  300. * some intro circ has sent its intro and received an ack.
  301. * circuits that are c_rend_joined are open, have heard from
  302. * bob, and are talking to him.
  303. */
  304. /** Client-side circuit purpose: Normal circuit, with cpath. */
  305. #define CIRCUIT_PURPOSE_C_GENERAL 5
  306. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  307. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  308. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
  309. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  310. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  311. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  312. /** Client-side circuit purpose: at Alice, waiting for ack. */
  313. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  314. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  315. #define CIRCUIT_PURPOSE_C_REND_READY 10
  316. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  317. * has been acknowledged. */
  318. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  319. /** Client-side circuit purpose: at Alice, rendezvous established. */
  320. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  321. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  322. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  323. /** Hidden-service-side circuit purpose: at Bob, successfully established
  324. * intro. */
  325. #define CIRCUIT_PURPOSE_S_INTRO 14
  326. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  327. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  328. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  329. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  330. #define _CIRCUIT_PURPOSE_MAX 16
  331. /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
  332. * that this OP has originated. */
  333. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  334. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  335. #define RELAY_COMMAND_BEGIN 1
  336. #define RELAY_COMMAND_DATA 2
  337. #define RELAY_COMMAND_END 3
  338. #define RELAY_COMMAND_CONNECTED 4
  339. #define RELAY_COMMAND_SENDME 5
  340. #define RELAY_COMMAND_EXTEND 6
  341. #define RELAY_COMMAND_EXTENDED 7
  342. #define RELAY_COMMAND_TRUNCATE 8
  343. #define RELAY_COMMAND_TRUNCATED 9
  344. #define RELAY_COMMAND_DROP 10
  345. #define RELAY_COMMAND_RESOLVE 11
  346. #define RELAY_COMMAND_RESOLVED 12
  347. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  348. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  349. #define RELAY_COMMAND_INTRODUCE1 34
  350. #define RELAY_COMMAND_INTRODUCE2 35
  351. #define RELAY_COMMAND_RENDEZVOUS1 36
  352. #define RELAY_COMMAND_RENDEZVOUS2 37
  353. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  354. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  355. #define RELAY_COMMAND_INTRODUCE_ACK 40
  356. #define _MIN_END_STREAM_REASON 1
  357. #define END_STREAM_REASON_MISC 1
  358. #define END_STREAM_REASON_RESOLVEFAILED 2
  359. #define END_STREAM_REASON_CONNECTFAILED 3
  360. #define END_STREAM_REASON_EXITPOLICY 4
  361. #define END_STREAM_REASON_DESTROY 5
  362. #define END_STREAM_REASON_DONE 6
  363. #define END_STREAM_REASON_TIMEOUT 7
  364. #define _MAX_END_STREAM_REASON 7
  365. #define RESOLVED_TYPE_IPV4 4
  366. #define RESOLVED_TYPE_IPV6 6
  367. #define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
  368. #define RESOLVED_TYPE_ERROR 0xF1
  369. /** Length of 'y' portion of 'y.onion' URL. */
  370. #define REND_SERVICE_ID_LEN 16
  371. #define CELL_DIRECTION_IN 1
  372. #define CELL_DIRECTION_OUT 2
  373. #ifdef TOR_PERF
  374. #define CIRCWINDOW_START 10000
  375. #define CIRCWINDOW_INCREMENT 1000
  376. #define STREAMWINDOW_START 5000
  377. #define STREAMWINDOW_INCREMENT 500
  378. #else
  379. #define CIRCWINDOW_START 1000
  380. #define CIRCWINDOW_INCREMENT 100
  381. #define STREAMWINDOW_START 500
  382. #define STREAMWINDOW_INCREMENT 50
  383. #endif
  384. /* cell commands */
  385. #define CELL_PADDING 0
  386. #define CELL_CREATE 1
  387. #define CELL_CREATED 2
  388. #define CELL_RELAY 3
  389. #define CELL_DESTROY 4
  390. /* people behind fascist firewalls use only these ports */
  391. #define REQUIRED_FIREWALL_DIRPORT 80
  392. #define REQUIRED_FIREWALL_ORPORT 443
  393. /* legal characters in a nickname */
  394. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  395. /** Name to use in client TLS certificates if no nickname is given.*/
  396. #define DEFAULT_CLIENT_NICKNAME "client"
  397. #define SOCKS4_NETWORK_LEN 8
  398. /*
  399. * Relay payload:
  400. * Relay command [1 byte]
  401. * Recognized [2 bytes]
  402. * Stream ID [2 bytes]
  403. * Partial SHA-1 [4 bytes]
  404. * Length [2 bytes]
  405. * Relay payload [498 bytes]
  406. */
  407. #define CELL_PAYLOAD_SIZE 509
  408. #define CELL_NETWORK_SIZE 512
  409. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  410. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  411. /** Parsed onion routing cell. All communication from OP-to-OR, or from
  412. * OR-to-OR, is via cells. */
  413. typedef struct {
  414. uint16_t circ_id; /**< Circuit which received the cell. */
  415. unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  416. * or DESTROY. */
  417. unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  418. } cell_t;
  419. /** Beginning of a RELAY cell payload. */
  420. typedef struct {
  421. uint8_t command; /**< The end-to-end relay command. */
  422. uint16_t recognized; /**< Used to tell whether cell is for us. */
  423. uint16_t stream_id; /**< Which stream is this cell associated with? */
  424. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  425. uint16_t length; /**< How long is the payload body? */
  426. } relay_header_t;
  427. typedef struct buf_t buf_t;
  428. typedef struct socks_request_t socks_request_t;
  429. #define CONNECTION_MAGIC 0x7C3C304Eu
  430. /** Description of a connection to another host or process, and associated
  431. * data. */
  432. struct connection_t {
  433. uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
  434. uint8_t type; /**< What kind of connection is this? */
  435. uint8_t state; /**< Current state of this connection. */
  436. uint8_t purpose; /**< Only used for DIR types currently. */
  437. uint8_t wants_to_read; /**< Boolean: should we start reading again once
  438. * the bandwidth throttler allows it?
  439. */
  440. uint8_t wants_to_write; /**< Boolean: should we start writing again once
  441. * the bandwidth throttler allows reads?
  442. */
  443. int s; /**< Our socket; -1 if this connection is closed. */
  444. int poll_index; /**< Index of this conn into the poll_array. */
  445. int marked_for_close; /**< Boolean: should we close this conn on the next
  446. * iteration of the main loop?
  447. */
  448. const char *marked_for_close_file; /**< For debugging: in which file were
  449. * we marked for close? */
  450. int hold_open_until_flushed; /**< Despite this connection's being marked
  451. * for close, do we flush it before closing it?
  452. */
  453. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  454. int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
  455. time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
  456. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  457. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  458. * outbuf? */
  459. time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
  460. time_t timestamp_created; /**< When was this connection_t created? */
  461. uint32_t addr; /**< IP of the other side of the connection; used to identify
  462. * routers, along with port. */
  463. uint16_t port; /**< If non-zero, port on the other end
  464. * of the connection. */
  465. char *address; /**< FQDN (or IP) of the guy on the other end.
  466. * strdup into this, because free_connection frees it.
  467. */
  468. crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
  469. * signing key. */
  470. char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
  471. char *nickname; /**< Nickname of OR on other side (if any). */
  472. /** Nickname of planned exit node -- to be used with .exit support. */
  473. char *chosen_exit_name;
  474. /* Used only by OR connections: */
  475. tor_tls *tls; /**< TLS connection state (OR only.) */
  476. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  477. * this connection? This is always in the
  478. * range 0..1<<15-1. (OR only.)*/
  479. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  480. int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
  481. int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
  482. * add 'bandwidth' to this, capping it at 10*bandwidth.
  483. * (OPEN ORs only)
  484. */
  485. circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
  486. * connection, which half of the space should
  487. * we use? */
  488. /* Used only by DIR and AP connections: */
  489. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  490. * querying for? (DIR/AP only) */
  491. /* Used only by edge connections: */
  492. size_t stream_size; /**< Used for debugging. */
  493. uint16_t stream_id;
  494. struct connection_t *next_stream; /**< Points to the next stream at this
  495. * edge, if any (Edge only). */
  496. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  497. * this conn exits at. (Edge only.) */
  498. int package_window; /**< How many more relay cells can i send into the
  499. * circuit? (Edge only.) */
  500. int deliver_window; /**< How many more relay cells can end at me? (Edge
  501. * only.) */
  502. int done_sending; /**< For half-open connections; not used currently. */
  503. int done_receiving; /**< For half-open connections; not used currently. */
  504. char has_sent_end; /**< For debugging: set once we've set the stream end,
  505. and check in circuit_about_to_close_connection(). */
  506. /* Used only by AP connections */
  507. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  508. * only.) */
  509. /* Used only by control connections */
  510. uint32_t event_mask;
  511. };
  512. typedef struct connection_t connection_t;
  513. #define ADDR_POLICY_ACCEPT 1
  514. #define ADDR_POLICY_REJECT 2
  515. /** A linked list of policy rules */
  516. typedef struct addr_policy_t {
  517. char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
  518. char *string; /**< String representation of this rule. */
  519. uint32_t addr; /**< Base address to accept or reject. */
  520. uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
  521. * a &amp; msk == <b>addr</b> &amp; msk . */
  522. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  523. uint16_t prt_max; /**< Highest port number to accept/reject. */
  524. struct addr_policy_t *next; /**< Next rule in list. */
  525. } addr_policy_t;
  526. /** Information about another onion router in the network. */
  527. typedef struct {
  528. char *address; /**< Location of OR: either a hostname or an IP address. */
  529. char *nickname; /**< Human-readable OR name. */
  530. uint32_t addr; /**< IPv4 address of OR, in host order. */
  531. uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
  532. uint16_t socks_port; /**< Port for SOCKS connections. */
  533. uint16_t dir_port; /**< Port for HTTP directory connections. */
  534. time_t published_on; /**< When was the information in this routerinfo_t
  535. * published? */
  536. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  537. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  538. char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
  539. char *platform; /**< What software/operating system is this OR using? */
  540. /* link info */
  541. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  542. * bucket per second? */
  543. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  544. /** How many bytes/s is this router known to handle? */
  545. uint32_t bandwidthcapacity;
  546. addr_policy_t *exit_policy; /**< What streams will this OR permit
  547. * to exit? */
  548. long uptime; /**< How many seconds the router claims to have been up */
  549. /* local info */
  550. int is_running; /**< As far as we know, is this OR currently running? */
  551. time_t status_set_at; /**< When did we last update is_running? */
  552. int is_verified; /**< Has a trusted dirserver validated this OR? */
  553. smartlist_t *declared_family; /**< Nicknames of router which this router
  554. * claims are its family. */
  555. } routerinfo_t;
  556. /** Contents of a directory of onion routers. */
  557. typedef struct {
  558. /** List of routerinfo_t */
  559. smartlist_t *routers;
  560. /** Which versions of tor are recommended by this directory? */
  561. char *software_versions;
  562. /** When was the most recent directory that contributed to this list
  563. * published?
  564. */
  565. time_t published_on;
  566. time_t running_routers_updated_on;
  567. /** Which router is claimed to have signed it? */
  568. char *signing_router;
  569. } routerlist_t;
  570. /** Contents of a running-routers list */
  571. typedef struct running_routers_t {
  572. time_t published_on; /**< When was the list marked as published? */
  573. /** Which ORs are on the list? Entries may be prefixed with ! and $. */
  574. smartlist_t *running_routers;
  575. int is_running_routers_format; /**< Are we using the old entry format? */
  576. } running_routers_t;
  577. /** Holds accounting information for a single step in the layered encryption
  578. * performed by a circuit. Used only at the client edge of a circuit. */
  579. struct crypt_path_t {
  580. /* crypto environments */
  581. /** Encryption key and counter for cells heading towards the OR at this
  582. * step. */
  583. crypto_cipher_env_t *f_crypto;
  584. /** Encryption key and counter for cells heading back from the OR at this
  585. * step. */
  586. crypto_cipher_env_t *b_crypto;
  587. /** Digest state for cells heading towards the OR at this step. */
  588. crypto_digest_env_t *f_digest; /* for integrity checking */
  589. /** Digest state for cells heading away from the OR at this step. */
  590. crypto_digest_env_t *b_digest;
  591. /** Current state of Diffie-Hellman key negotiation with the OR at this
  592. * step. */
  593. crypto_dh_env_t *handshake_state;
  594. /** Negotiated key material shared with the OR at this step. */
  595. char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
  596. /** IP4 address of the OR at this step. */
  597. uint32_t addr;
  598. /** Port of the OR at this step. */
  599. uint16_t port;
  600. /** Identity key digest of the OR at this step. */
  601. char identity_digest[DIGEST_LEN];
  602. /** Is the circuit built to this step? Must be one of:
  603. * - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
  604. * - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
  605. * and not received an EXTENDED/CREATED)
  606. * - CPATH_STATE_OPEN (The circuit has been extended to this step) */
  607. uint8_t state;
  608. #define CPATH_STATE_CLOSED 0
  609. #define CPATH_STATE_AWAITING_KEYS 1
  610. #define CPATH_STATE_OPEN 2
  611. struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
  612. * (The list is circular, so the last node
  613. * links to the first.) */
  614. struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
  615. * circuit. */
  616. int package_window; /**< How many bytes are we allowed to originate ending
  617. * at this step? */
  618. int deliver_window; /**< How many bytes are we willing to deliver originating
  619. * at this step? */
  620. };
  621. #define CPATH_KEY_MATERIAL_LEN (20*2+16*2)
  622. #define DH_KEY_LEN DH_BYTES
  623. #define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
  624. CIPHER_KEY_LEN+\
  625. DH_KEY_LEN)
  626. #define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
  627. #define REND_COOKIE_LEN DIGEST_LEN
  628. typedef struct crypt_path_t crypt_path_t;
  629. /** Information used to build a circuit. */
  630. typedef struct {
  631. /** Intended length of the final circuit. */
  632. int desired_path_len;
  633. /** Nickname of planned exit node. */
  634. char *chosen_exit_name;
  635. /** Identity of planned exit node. */
  636. char chosen_exit_digest[DIGEST_LEN];
  637. /** The crypt_path_t to append after rendezvous: used for rendezvous. */
  638. struct crypt_path_t *pending_final_cpath;
  639. /** How many times has building a circuit for this task failed? */
  640. int failure_count;
  641. } cpath_build_state_t;
  642. #define CIRCUIT_MAGIC 0x35315243u
  643. /** Struct for a path (circuit) through the onion routing network. */
  644. struct circuit_t {
  645. uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
  646. int marked_for_close; /**< Should we close this circuit at the end of the
  647. * main loop? */
  648. const char *marked_for_close_file; /**< For debugging: in which file was this
  649. * circuit marked for close? */
  650. /** The IPv4 address of the OR that is next in this circuit. */
  651. uint32_t n_addr;
  652. /** The port for the OR that is next in this circuit. */
  653. uint16_t n_port;
  654. /** The OR connection that is previous in this circuit. */
  655. connection_t *p_conn;
  656. /** The OR connection that is next in this circuit. */
  657. connection_t *n_conn;
  658. /** The identity hash of n_conn. */
  659. char n_conn_id_digest[DIGEST_LEN];
  660. /** Linked list of AP streams associated with this circuit. */
  661. connection_t *p_streams;
  662. /** Linked list of Exit streams associated with this circuit. */
  663. connection_t *n_streams;
  664. /** Linked list of Exit streams associated with this circuit that are
  665. * still being resolved. */
  666. connection_t *resolving_streams;
  667. /** The next stream_id that will be tried when we're attempting to
  668. * construct a new AP stream originating at this circuit. */
  669. uint16_t next_stream_id;
  670. /** How many relay data cells can we package (read from edge streams)
  671. * on this circuit before we receive a circuit-level sendme cell asking
  672. * for more? */
  673. int package_window;
  674. /** How many relay data cells will we deliver (write to edge streams)
  675. * on this circuit? When deliver_window gets low, we send some
  676. * circuit-level sendme cells to indicate that we're willing to accept
  677. * more. */
  678. int deliver_window;
  679. /** The circuit_id used in the previous (backward) hop of this circuit. */
  680. uint16_t p_circ_id;
  681. /** The circuit_id used in the next (forward) hop of this circuit. */
  682. uint16_t n_circ_id;
  683. /** The cipher used by intermediate hops for cells heading toward the
  684. * OP. */
  685. crypto_cipher_env_t *p_crypto;
  686. /** The cipher used by intermediate hops for cells heading away from
  687. * the OP. */
  688. crypto_cipher_env_t *n_crypto;
  689. /** The integrity-checking digest used by intermediate hops, for
  690. * cells packaged here and heading towards the OP.
  691. */
  692. crypto_digest_env_t *p_digest;
  693. /** The integrity-checking digest used by intermediate hops, for
  694. * cells packaged at the OP and arriving here.
  695. */
  696. crypto_digest_env_t *n_digest;
  697. /** Build state for this circuit. It includes the intended path
  698. * length, the chosen exit router, rendezvous information, etc.
  699. */
  700. cpath_build_state_t *build_state;
  701. /** The doubly-linked list of crypt_path_t entries, one per hop,
  702. * for this circuit. This includes ciphers for each hop,
  703. * integrity-checking digests for each hop, and package/delivery
  704. * windows for each hop.
  705. *
  706. * The cpath field is defined only when we are the circuit's origin.
  707. */
  708. crypt_path_t *cpath;
  709. /** For storage while passing to cpuworker, or while n_conn is pending. */
  710. char onionskin[ONIONSKIN_CHALLENGE_LEN];
  711. char handshake_digest[DIGEST_LEN]; /**< Stores KH for intermediate hops. */
  712. time_t timestamp_created; /**< When was this circuit created? */
  713. time_t timestamp_dirty; /**< When the circuit was first used, or 0 if the
  714. * circuit is clean. */
  715. uint8_t state; /**< Current status of this circuit. */
  716. uint8_t purpose; /**< Why are we creating this circuit? */
  717. /**
  718. * The rend_query field holds y portion of y.onion (nul-terminated)
  719. * if purpose is C_INTRODUCING or C_ESTABLISH_REND, or is a C_GENERAL
  720. * for a hidden service, or is S_*.
  721. */
  722. char rend_query[REND_SERVICE_ID_LEN+1];
  723. /** The rend_pk_digest field holds a hash of location-hidden service's
  724. * PK if purpose is INTRO_POINT or S_ESTABLISH_INTRO or S_RENDEZVOUSING.
  725. */
  726. char rend_pk_digest[DIGEST_LEN];
  727. /** Holds rendezvous cookie if purpose is REND_POINT_WAITING or
  728. * C_ESTABLISH_REND. Filled with zeroes otherwise.
  729. */
  730. char rend_cookie[REND_COOKIE_LEN];
  731. /** Points to spliced circuit if purpose is REND_ESTABLISHED, and circuit
  732. * is not marked for close. */
  733. struct circuit_t *rend_splice;
  734. /** Quasi-global identifier for this circuit; used for control.c */
  735. /* XXXX009 NM This can get re-used after 2**32 circuits. */
  736. uint32_t global_identifier;
  737. struct circuit_t *next; /**< Next circuit in linked list. */
  738. };
  739. typedef struct circuit_t circuit_t;
  740. #define ALLOW_UNVERIFIED_ENTRY 1
  741. #define ALLOW_UNVERIFIED_EXIT 2
  742. #define ALLOW_UNVERIFIED_MIDDLE 4
  743. #define ALLOW_UNVERIFIED_RENDEZVOUS 8
  744. #define ALLOW_UNVERIFIED_INTRODUCTION 16
  745. typedef struct exit_redirect_t {
  746. uint32_t addr;
  747. uint32_t mask;
  748. uint16_t port_min;
  749. uint16_t port_max;
  750. int is_redirect;
  751. uint32_t addr_dest;
  752. uint16_t port_dest;
  753. } exit_redirect_t;
  754. /** Configuration options for a Tor process */
  755. typedef struct {
  756. /** What should the tor process actually do? */
  757. enum {
  758. CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
  759. } command;
  760. const char *command_arg; /**< Argument for command-line option. */
  761. struct config_line_t *OldLogOptions; /**< List of configuration lines
  762. * for logfiles, old style. */
  763. struct config_line_t *Logs; /**< New-style list of configuration lines
  764. * for logs */
  765. char *DebugLogFile; /**< Where to send verbose log messages. */
  766. char *DataDirectory; /**< OR only: where to store long-term data. */
  767. char *Nickname; /**< OR only: nickname of this onion router. */
  768. char *Address; /**< OR only: configured address for this onion router. */
  769. char *PidFile; /**< Where to store PID of Tor process. */
  770. char *ExitNodes; /**< Comma-separated list of nicknames of ORs to consider
  771. * as exits. */
  772. char *EntryNodes; /**< Comma-separated list of nicknames of ORs to consider
  773. * as entry points. */
  774. int StrictExitNodes; /**< Boolean: When none of our ExitNodes are up, do we
  775. * stop building circuits? */
  776. int StrictEntryNodes; /**< Boolean: When none of our EntryNodes are up, do we
  777. * stop building circuits? */
  778. char *ExcludeNodes; /**< Comma-separated list of nicknames of ORs not to
  779. * use in circuits. */
  780. char *RendNodes; /**< Comma-separated list of nicknames used as introduction
  781. * points. */
  782. char *RendExcludeNodes; /**< Comma-separated list of nicknames not to use
  783. * as introduction points. */
  784. smartlist_t *AllowUnverifiedNodes; /**< List of "entry", "middle", "exit" */
  785. int _AllowUnverified; /**< Bitmask; derived from AllowUnverifiedNodes; */
  786. struct config_line_t *ExitPolicy; /**< Lists of exit policy components. */
  787. struct config_line_t *SocksPolicy; /**< Lists of socks policy components */
  788. struct config_line_t *DirPolicy; /**< Lists of dir policy components */
  789. /** Addresses to bind for listening for SOCKS connections. */
  790. struct config_line_t *SocksBindAddress;
  791. /** Addresses to bind for listening for OR connections. */
  792. struct config_line_t *ORBindAddress;
  793. /** Addresses to bind for listening for directory connections. */
  794. struct config_line_t *DirBindAddress;
  795. /** Local address to bind outbound sockets */
  796. char *OutboundBindAddress;
  797. struct config_line_t *RecommendedVersions;
  798. /**< Directory server only: which versions of
  799. * Tor should we tell users to run? */
  800. char *User; /**< Name of user to run Tor as. */
  801. char *Group; /**< Name of group to run Tor as. */
  802. double PathlenCoinWeight; /**< Parameter used to configure average path
  803. * length (alpha in geometric distribution). */
  804. int ORPort; /**< Port to listen on for OR connections. */
  805. int SocksPort; /**< Port to listen on for SOCKS connections. */
  806. int ControlPort; /**< Port to listen on for control connections. */
  807. int DirPort; /**< Port to listen on for directory connections. */
  808. int AuthoritativeDir; /**< Boolean: is this an authoritative directory? */
  809. int ClientOnly; /**< Boolean: should we never evolve into a server role? */
  810. int MaxConn; /**< Maximum number of simultaneous connections. */
  811. int IgnoreVersion; /**< If true, run no matter what versions of Tor the
  812. * directory recommends. */
  813. int RunAsDaemon; /**< If true, run in the background. (Unix only) */
  814. int FascistFirewall; /**< Whether to prefer ORs reachable on open ports. */
  815. smartlist_t *FirewallPorts; /** Which ports our firewall allows. */
  816. int DirFetchPeriod; /**< How often do we fetch new directories? */
  817. int DirPostPeriod; /**< How often do we post our server descriptor to the
  818. * authoritative directory servers? */
  819. int RendPostPeriod; /**< How often do we post each rendezvous service
  820. * descriptor? Remember to publish them independently. */
  821. int StatusFetchPeriod; /**< How often do we fetch running-routers lists? */
  822. int KeepalivePeriod; /**< How often do we send padding cells to keep
  823. * connections alive? */
  824. int MaxOnionsPending; /**< How many circuit CREATE requests do we allow
  825. * to wait simultaneously before we start dropping
  826. * them? */
  827. int NewCircuitPeriod; /**< How long do we use a circuit before building
  828. * a new one? */
  829. uint64_t BandwidthRate; /**< How much bandwidth, on average, are we willing to
  830. * use in a second? */
  831. uint64_t BandwidthBurst; /**< How much bandwidth, at maximum, are we willing to
  832. * use in a second? */
  833. int NumCpus; /**< How many CPUs should we try to use? */
  834. int RunTesting; /**< If true, create testing circuits to measure how well the
  835. * other ORs are running. */
  836. struct config_line_t *RendConfigLines; /**< List of configuration lines
  837. * for rendezvous services. */
  838. char *ContactInfo; /**< Contact info to be published in the directory */
  839. char *HttpProxy; /**< hostname[:port] to use as http proxy, if any */
  840. uint32_t HttpProxyAddr; /**< Parsed IPv4 addr for http proxy, if any */
  841. uint16_t HttpProxyPort; /**< Parsed port for http proxy, if any */
  842. struct config_line_t *DirServers; /**< List of configuration lines
  843. * for directory servers. */
  844. char *MyFamily; /**< Declared family for this OR. */
  845. struct config_line_t *NodeFamilies; /**< List of config lines for
  846. * node families */
  847. struct config_line_t *RedirectExit; /**< List of config lines for simple
  848. * addr/port redirection */
  849. smartlist_t *RedirectExitList; /**< List of exit_redirect_t */
  850. int _MonthlyAccountingStart; /**< Deprecated: day of month when accounting
  851. * interval starts */
  852. char *AccountingStart; /** How long is the accounting interval, and when
  853. * does it start? */
  854. uint64_t AccountingMax; /**< How many bytes do we allow per accounting
  855. * interval before hibernation? 0 for "never
  856. * hibernate." */
  857. int _AccountingMaxKB; /**< How many KB do we allow per accounting
  858. * interval before hibernation? 0 for "never
  859. * hibernate." (Based on a deprecated option)*/
  860. char *HashedControlPassword; /**< Base64-encoded hash of a password for
  861. * the control system. */
  862. int CookieAuthentication; /**< Boolean: do we enable cookie-based auth for
  863. * the control system? */
  864. } or_options_t;
  865. #define MAX_SOCKS_REPLY_LEN 1024
  866. #define MAX_SOCKS_ADDR_LEN 256
  867. #define SOCKS_COMMAND_CONNECT 0x01
  868. #define SOCKS_COMMAND_RESOLVE 0xF0
  869. /** State of a SOCKS request from a user to an OP */
  870. struct socks_request_t {
  871. char socks_version; /**< Which version of SOCKS did the client use? */
  872. int command; /**< What has the user requested? One of CONNECT or RESOLVE. */
  873. size_t replylen; /**< Length of <b>reply</b>. */
  874. char reply[MAX_SOCKS_REPLY_LEN]; /**< Write an entry into this string if
  875. * we want to specify our own socks reply,
  876. * rather than using the default socks4 or
  877. * socks5 socks reply. We use this for the
  878. * two-stage socks5 handshake.
  879. */
  880. int has_finished; /**< Has the SOCKS handshake finished? */
  881. char address[MAX_SOCKS_ADDR_LEN]; /**< What address did the client ask to connect to? */
  882. uint16_t port; /**< What port did the client ask to connect to? */
  883. };
  884. /* all the function prototypes go here */
  885. /********************************* buffers.c ***************************/
  886. buf_t *buf_new(void);
  887. buf_t *buf_new_with_capacity(size_t size);
  888. void buf_free(buf_t *buf);
  889. void buf_clear(buf_t *buf);
  890. size_t buf_datalen(const buf_t *buf);
  891. size_t buf_capacity(const buf_t *buf);
  892. const char *_buf_peek_raw_buffer(const buf_t *buf);
  893. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof);
  894. int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf);
  895. int flush_buf(int s, buf_t *buf, size_t *buf_flushlen);
  896. int flush_buf_tls(tor_tls *tls, buf_t *buf, size_t *buf_flushlen);
  897. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  898. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  899. int fetch_from_buf_http(buf_t *buf,
  900. char **headers_out, size_t max_headerlen,
  901. char **body_out, size_t *body_used, size_t max_bodylen);
  902. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
  903. int fetch_from_buf_control(buf_t *buf, uint16_t *len_out, uint16_t *type_out,
  904. char **body_out);
  905. void assert_buf_ok(buf_t *buf);
  906. /********************************* circuitbuild.c **********************/
  907. char *circuit_list_path(circuit_t *circ, int verbose);
  908. void circuit_log_path(int severity, circuit_t *circ);
  909. void circuit_rep_hist_note_result(circuit_t *circ);
  910. void circuit_dump_by_conn(connection_t *conn, int severity);
  911. circuit_t *circuit_establish_circuit(uint8_t purpose,
  912. const char *exit_digest);
  913. void circuit_n_conn_done(connection_t *or_conn, int status);
  914. int circuit_send_next_onion_skin(circuit_t *circ);
  915. int circuit_extend(cell_t *cell, circuit_t *circ);
  916. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse);
  917. int circuit_finish_handshake(circuit_t *circ, char *reply);
  918. int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
  919. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
  920. int circuit_all_predicted_ports_handled(time_t now);
  921. void onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop);
  922. /********************************* circuitlist.c ***********************/
  923. extern const char *circuit_state_to_string[];
  924. void circuit_close_all_marked(void);
  925. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
  926. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn);
  927. circuit_t *circuit_get_by_conn(connection_t *conn);
  928. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose);
  929. circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
  930. const char *digest, uint8_t purpose);
  931. circuit_t *circuit_get_rendezvous(const char *cookie);
  932. int circuit_count_building(uint8_t purpose);
  933. circuit_t *circuit_get_youngest_clean_open(uint8_t purpose);
  934. int _circuit_mark_for_close(circuit_t *circ);
  935. #define circuit_mark_for_close(c) \
  936. do { \
  937. if (_circuit_mark_for_close(c)<0) { \
  938. log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d (first at %s:%d)", \
  939. _SHORT_FILE_,__LINE__, \
  940. c->marked_for_close_file,c->marked_for_close); \
  941. } else { \
  942. c->marked_for_close_file = _SHORT_FILE_; \
  943. c->marked_for_close = __LINE__; \
  944. } \
  945. } while (0)
  946. void assert_cpath_layer_ok(const crypt_path_t *cp);
  947. void assert_circuit_ok(const circuit_t *c);
  948. /********************************* circuituse.c ************************/
  949. void circuit_expire_building(time_t now);
  950. void circuit_remove_handled_ports(smartlist_t *needed_ports);
  951. int circuit_stream_is_being_handled(connection_t *conn, uint16_t port, int min);
  952. void circuit_build_needed_circs(time_t now);
  953. void circuit_detach_stream(circuit_t *circ, connection_t *conn);
  954. void circuit_about_to_close_connection(connection_t *conn);
  955. void circuit_has_opened(circuit_t *circ);
  956. void circuit_build_failed(circuit_t *circ);
  957. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname);
  958. circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest);
  959. void circuit_reset_failure_count(int timeout);
  960. int connection_ap_handshake_attach_circuit(connection_t *conn);
  961. /********************************* command.c ***************************/
  962. void command_process_cell(cell_t *cell, connection_t *conn);
  963. extern unsigned long stats_n_padding_cells_processed;
  964. extern unsigned long stats_n_create_cells_processed;
  965. extern unsigned long stats_n_created_cells_processed;
  966. extern unsigned long stats_n_relay_cells_processed;
  967. extern unsigned long stats_n_destroy_cells_processed;
  968. /********************************* config.c ***************************/
  969. struct config_line_t {
  970. char *key;
  971. char *value;
  972. struct config_line_t *next;
  973. };
  974. or_options_t *get_options(void);
  975. void set_options(or_options_t *new_val);
  976. int options_act(void);
  977. int config_get_lines(char *string, struct config_line_t **result);
  978. void config_free_lines(struct config_line_t *front);
  979. int config_trial_assign(struct config_line_t *list, int reset);
  980. int resolve_my_address(const char *address, uint32_t *addr);
  981. void options_init(or_options_t *options);
  982. int init_from_config(int argc, char **argv);
  983. int config_init_logs(or_options_t *options, int validate_only);
  984. int config_parse_addr_policy(struct config_line_t *cfg,
  985. addr_policy_t **dest);
  986. void addr_policy_free(addr_policy_t *p);
  987. int config_option_is_recognized(const char *key);
  988. struct config_line_t *config_get_assigned_option(or_options_t *options,
  989. const char *key);
  990. struct config_line_t *config_line_prepend(struct config_line_t *front,
  991. const char *key, const char *val);
  992. char *config_dump_options(or_options_t *options, int minimal);
  993. int save_current_config(void);
  994. /********************************* connection.c ***************************/
  995. #define CONN_TYPE_TO_STRING(t) (((t) < _CONN_TYPE_MIN || (t) > _CONN_TYPE_MAX) ? \
  996. "Unknown" : conn_type_to_string[(t)])
  997. extern const char *conn_type_to_string[];
  998. extern const char *conn_state_to_string[][_CONN_TYPE_MAX+1];
  999. connection_t *connection_new(int type);
  1000. void connection_free(connection_t *conn);
  1001. void connection_free_all(void);
  1002. void connection_about_to_close_connection(connection_t *conn);
  1003. void connection_close_immediate(connection_t *conn);
  1004. int _connection_mark_for_close(connection_t *conn);
  1005. #define connection_mark_for_close(c) \
  1006. do { \
  1007. if (_connection_mark_for_close(c)<0) { \
  1008. log(LOG_WARN,"Duplicate call to connection_mark_for_close at %s:%d (first at %s:%d)", \
  1009. _SHORT_FILE_,__LINE__, \
  1010. c->marked_for_close_file,c->marked_for_close); \
  1011. } else { \
  1012. c->marked_for_close_file = _SHORT_FILE_; \
  1013. c->marked_for_close = __LINE__; \
  1014. } \
  1015. } while (0)
  1016. void connection_expire_held_open(void);
  1017. int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
  1018. int retry_all_listeners(int force);
  1019. void connection_bucket_init(void);
  1020. void connection_bucket_refill(struct timeval *now);
  1021. int connection_handle_read(connection_t *conn);
  1022. int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
  1023. int connection_wants_to_flush(connection_t *conn);
  1024. int connection_outbuf_too_full(connection_t *conn);
  1025. int connection_handle_write(connection_t *conn);
  1026. void connection_write_to_buf(const char *string, size_t len, connection_t *conn);
  1027. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
  1028. connection_t *connection_get_by_identity_digest(const char *digest, int type);
  1029. connection_t *connection_get_by_type(int type);
  1030. connection_t *connection_get_by_type_state(int type, int state);
  1031. connection_t *connection_get_by_type_state_lastwritten(int type, int state);
  1032. connection_t *connection_get_by_type_rendquery(int type, const char *rendquery);
  1033. #define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
  1034. #define connection_has_pending_tls_data(conn) \
  1035. ((conn)->type == CONN_TYPE_OR && \
  1036. (conn)->state == OR_CONN_STATE_OPEN && \
  1037. tor_tls_get_pending_bytes((conn)->tls))
  1038. int connection_is_listener(connection_t *conn);
  1039. int connection_state_is_open(connection_t *conn);
  1040. int connection_state_is_connecting(connection_t *conn);
  1041. int connection_send_destroy(uint16_t circ_id, connection_t *conn);
  1042. void assert_connection_ok(connection_t *conn, time_t now);
  1043. int connection_or_nonopen_was_started_here(connection_t *conn);
  1044. /********************************* connection_edge.c ***************************/
  1045. int connection_edge_reached_eof(connection_t *conn);
  1046. int connection_edge_process_inbuf(connection_t *conn, int package_partial);
  1047. int connection_edge_destroy(uint16_t circ_id, connection_t *conn);
  1048. int connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
  1049. int connection_edge_finished_flushing(connection_t *conn);
  1050. int connection_edge_finished_connecting(connection_t *conn);
  1051. int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
  1052. int connection_ap_handshake_send_resolve(connection_t *ap_conn, circuit_t *circ);
  1053. int connection_ap_make_bridge(char *address, uint16_t port);
  1054. void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  1055. size_t replylen, int status);
  1056. void connection_ap_handshake_socks_resolved(connection_t *conn,
  1057. int answer_type,
  1058. size_t answer_len,
  1059. const char *answer);
  1060. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  1061. int connection_exit_begin_resolve(cell_t *cell, circuit_t *circ);
  1062. void connection_exit_connect(connection_t *conn);
  1063. int connection_edge_is_rendezvous_stream(connection_t *conn);
  1064. int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
  1065. void connection_ap_expire_beginning(void);
  1066. void connection_ap_attach_pending(void);
  1067. void parse_socks_policy(void);
  1068. int socks_policy_permits_address(uint32_t addr);
  1069. void client_dns_init(void);
  1070. uint32_t client_dns_lookup_entry(const char *address);
  1071. int client_dns_incr_failures(const char *address);
  1072. void client_dns_set_entry(const char *address, uint32_t val);
  1073. void client_dns_clean(void);
  1074. void set_exit_redirects(smartlist_t *lst);
  1075. typedef enum hostname_type_t {
  1076. NORMAL_HOSTNAME, ONION_HOSTNAME, EXIT_HOSTNAME
  1077. } hostname_type_t;
  1078. hostname_type_t parse_extended_hostname(char *address);
  1079. /********************************* connection_or.c ***************************/
  1080. int connection_or_reached_eof(connection_t *conn);
  1081. int connection_or_process_inbuf(connection_t *conn);
  1082. int connection_or_finished_flushing(connection_t *conn);
  1083. int connection_or_finished_connecting(connection_t *conn);
  1084. connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  1085. const char *id_digest);
  1086. int connection_tls_start_handshake(connection_t *conn, int receiving);
  1087. int connection_tls_continue_handshake(connection_t *conn);
  1088. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn);
  1089. void connection_or_update_nickname(connection_t *conn);
  1090. /********************************* control.c ***************************/
  1091. typedef enum circuit_status_event_t {
  1092. CIRC_EVENT_LAUNCHED = 0,
  1093. CIRC_EVENT_BUILT = 1,
  1094. CIRC_EVENT_EXTENDED = 2,
  1095. CIRC_EVENT_FAILED = 3,
  1096. CIRC_EVENT_CLOSED = 4,
  1097. } circuit_status_event_t;
  1098. typedef enum stream_status_event_t {
  1099. STREAM_EVENT_SENT_CONNECT = 0,
  1100. STREAM_EVENT_SENT_RESOLVE = 1,
  1101. STREAM_EVENT_SUCCEEDED = 2,
  1102. STREAM_EVENT_FAILED = 3,
  1103. STREAM_EVENT_CLOSED = 4
  1104. } stream_status_event_t;
  1105. typedef enum or_conn_status_event_t {
  1106. OR_CONN_EVENT_LAUNCHED = 0,
  1107. OR_CONN_EVENT_CONNECTED = 1,
  1108. OR_CONN_EVENT_FAILED = 2,
  1109. OR_CONN_EVENT_CLOSED = 3,
  1110. } or_conn_status_event_t;
  1111. int connection_control_finished_flushing(connection_t *conn);
  1112. int connection_control_reached_eof(connection_t *conn);
  1113. int connection_control_process_inbuf(connection_t *conn);
  1114. int control_event_circuit_status(circuit_t *circ, circuit_status_event_t e);
  1115. int control_event_stream_status(connection_t *conn, stream_status_event_t e);
  1116. int control_event_or_conn_status(connection_t *conn, or_conn_status_event_t e);
  1117. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  1118. void control_event_logmsg(int severity, const char *msg);
  1119. int init_cookie_authentication(int enabled);
  1120. int decode_hashed_password(char *buf, const char *hashed);
  1121. /********************************* cpuworker.c *****************************/
  1122. void cpu_init(void);
  1123. void cpuworkers_rotate(void);
  1124. int connection_cpu_finished_flushing(connection_t *conn);
  1125. int connection_cpu_reached_eof(connection_t *conn);
  1126. int connection_cpu_process_inbuf(connection_t *conn);
  1127. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  1128. void *task);
  1129. /********************************* directory.c ***************************/
  1130. int dir_policy_permits_address(uint32_t addr);
  1131. void directory_post_to_dirservers(uint8_t purpose, const char *payload,
  1132. size_t payload_len);
  1133. void directory_get_from_dirserver(uint8_t purpose, const char *resource,
  1134. int retry_if_no_servers);
  1135. int connection_dir_reached_eof(connection_t *conn);
  1136. int connection_dir_process_inbuf(connection_t *conn);
  1137. int connection_dir_finished_flushing(connection_t *conn);
  1138. int connection_dir_finished_connecting(connection_t *conn);
  1139. int connection_dir_connect_failed(connection_t *conn);
  1140. void parse_dir_policy(void);
  1141. /********************************* dirserv.c ***************************/
  1142. int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
  1143. int dirserv_parse_fingerprint_file(const char *fname);
  1144. int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
  1145. void dirserv_free_fingerprint_list(void);
  1146. const char *dirserv_get_nickname_by_digest(const char *digest);
  1147. int dirserv_add_descriptor(const char **desc);
  1148. int dirserv_load_from_directory_string(const char *dir);
  1149. void dirserv_free_descriptors(void);
  1150. void dirserv_remove_old_servers(int age);
  1151. int dirserv_dump_directory_to_string(char **dir_out,
  1152. crypto_pk_env_t *private_key);
  1153. void directory_set_dirty(void);
  1154. size_t dirserv_get_directory(const char **cp, int compress);
  1155. size_t dirserv_get_runningrouters(const char **rr, int compress);
  1156. void dirserv_set_cached_directory(const char *directory, time_t when,
  1157. int is_running_routers);
  1158. /********************************* dns.c ***************************/
  1159. void dns_init(void);
  1160. int connection_dns_finished_flushing(connection_t *conn);
  1161. int connection_dns_reached_eof(connection_t *conn);
  1162. int connection_dns_process_inbuf(connection_t *conn);
  1163. void dnsworkers_rotate(void);
  1164. void connection_dns_remove(connection_t *conn);
  1165. void assert_connection_edge_not_dns_pending(connection_t *conn);
  1166. void assert_all_pending_dns_resolves_ok(void);
  1167. void dns_cancel_pending_resolve(char *question);
  1168. int dns_resolve(connection_t *exitconn);
  1169. /********************************* hibernate.c **********************/
  1170. int accounting_parse_options(or_options_t *options, int validate_only);
  1171. int accounting_is_enabled(or_options_t *options);
  1172. void configure_accounting(time_t now);
  1173. void accounting_run_housekeeping(time_t now);
  1174. void accounting_add_bytes(size_t n_read, size_t n_written, int seconds);
  1175. int accounting_record_bandwidth_usage(time_t now);
  1176. void hibernate_begin_shutdown(void);
  1177. int we_are_hibernating(void);
  1178. void consider_hibernation(time_t now);
  1179. /********************************* main.c ***************************/
  1180. int connection_add(connection_t *conn);
  1181. int connection_remove(connection_t *conn);
  1182. int connection_in_array(connection_t *conn);
  1183. void get_connection_array(connection_t ***array, int *n);
  1184. void connection_watch_events(connection_t *conn, short events);
  1185. int connection_is_reading(connection_t *conn);
  1186. void connection_stop_reading(connection_t *conn);
  1187. void connection_start_reading(connection_t *conn);
  1188. int connection_is_writing(connection_t *conn);
  1189. void connection_stop_writing(connection_t *conn);
  1190. void connection_start_writing(connection_t *conn);
  1191. void directory_has_arrived(time_t now);
  1192. int authdir_mode(or_options_t *options);
  1193. int clique_mode(or_options_t *options);
  1194. int server_mode(or_options_t *options);
  1195. int advertised_server_mode(void);
  1196. int proxy_mode(or_options_t *options);
  1197. void handle_signals(int is_parent);
  1198. void tor_cleanup(void);
  1199. int tor_main(int argc, char *argv[]);
  1200. /********************************* onion.c ***************************/
  1201. int onion_pending_add(circuit_t *circ);
  1202. circuit_t *onion_next_task(void);
  1203. void onion_pending_remove(circuit_t *circ);
  1204. int onion_skin_create(crypto_pk_env_t *router_key,
  1205. crypto_dh_env_t **handshake_state_out,
  1206. char *onion_skin_out);
  1207. int onion_skin_server_handshake(char *onion_skin,
  1208. crypto_pk_env_t *private_key,
  1209. crypto_pk_env_t *prev_private_key,
  1210. char *handshake_reply_out,
  1211. char *key_out,
  1212. size_t key_out_len);
  1213. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  1214. char *handshake_reply,
  1215. char *key_out,
  1216. size_t key_out_len);
  1217. /********************************* relay.c ***************************/
  1218. extern unsigned long stats_n_relay_cells_relayed;
  1219. extern unsigned long stats_n_relay_cells_delivered;
  1220. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  1221. int cell_direction);
  1222. void relay_header_pack(char *dest, const relay_header_t *src);
  1223. void relay_header_unpack(relay_header_t *dest, const char *src);
  1224. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  1225. int relay_command, const char *payload,
  1226. size_t payload_len, crypt_path_t *cpath_layer);
  1227. int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
  1228. void connection_edge_consider_sending_sendme(connection_t *conn);
  1229. extern uint64_t stats_n_data_cells_packaged;
  1230. extern uint64_t stats_n_data_bytes_packaged;
  1231. extern uint64_t stats_n_data_cells_received;
  1232. extern uint64_t stats_n_data_bytes_received;
  1233. /********************************* rephist.c ***************************/
  1234. void rep_hist_init(void);
  1235. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  1236. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  1237. void rep_hist_note_disconnect(const char* nickname, time_t when);
  1238. void rep_hist_note_connection_died(const char* nickname, time_t when);
  1239. void rep_hist_note_extend_succeeded(const char *from_name,
  1240. const char *to_name);
  1241. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  1242. void rep_hist_dump_stats(time_t now, int severity);
  1243. void rep_hist_note_bytes_read(int num_bytes, time_t when);
  1244. void rep_hist_note_bytes_written(int num_bytes, time_t when);
  1245. int rep_hist_bandwidth_assess(void);
  1246. char *rep_hist_get_bandwidth_lines(void);
  1247. void rep_history_clean(time_t before);
  1248. void rep_hist_note_used_port(uint16_t port, time_t now);
  1249. smartlist_t *rep_hist_get_predicted_ports(time_t now);
  1250. /********************************* rendclient.c ***************************/
  1251. void rend_client_introcirc_has_opened(circuit_t *circ);
  1252. void rend_client_rendcirc_has_opened(circuit_t *circ);
  1253. int rend_client_introduction_acked(circuit_t *circ, const char *request, size_t request_len);
  1254. void rend_client_refetch_renddesc(const char *query);
  1255. int rend_client_remove_intro_point(char *failed_intro, const char *query);
  1256. int rend_client_rendezvous_acked(circuit_t *circ, const char *request, size_t request_len);
  1257. int rend_client_receive_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1258. void rend_client_desc_fetched(char *query, int status);
  1259. char *rend_client_get_random_intro(char *query);
  1260. int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
  1261. /********************************* rendcommon.c ***************************/
  1262. typedef struct rend_service_descriptor_t {
  1263. crypto_pk_env_t *pk;
  1264. time_t timestamp;
  1265. int n_intro_points;
  1266. char **intro_points;
  1267. } rend_service_descriptor_t;
  1268. int rend_cmp_service_ids(const char *one, const char *two);
  1269. void rend_process_relay_cell(circuit_t *circ, int command, size_t length,
  1270. const char *payload);
  1271. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  1272. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  1273. crypto_pk_env_t *key,
  1274. char **str_out,
  1275. size_t *len_out);
  1276. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, size_t len);
  1277. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  1278. typedef struct rend_cache_entry_t {
  1279. size_t len; /* Length of desc */
  1280. time_t received; /* When did we get the descriptor? */
  1281. char *desc; /* Service descriptor */
  1282. rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
  1283. } rend_cache_entry_t;
  1284. void rend_cache_init(void);
  1285. void rend_cache_clean(void);
  1286. int rend_valid_service_id(const char *query);
  1287. int rend_cache_lookup_desc(const char *query, const char **desc, size_t *desc_len);
  1288. int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
  1289. int rend_cache_store(const char *desc, size_t desc_len);
  1290. /********************************* rendservice.c ***************************/
  1291. int rend_config_services(or_options_t *options, int validate_only);
  1292. int rend_service_load_keys(void);
  1293. void rend_services_init(void);
  1294. void rend_services_introduce(void);
  1295. void rend_consider_services_upload(time_t now);
  1296. void rend_service_intro_has_opened(circuit_t *circuit);
  1297. int rend_service_intro_established(circuit_t *circuit, const char *request, size_t request_len);
  1298. void rend_service_rendezvous_has_opened(circuit_t *circuit);
  1299. int rend_service_introduce(circuit_t *circuit, const char *request, size_t request_len);
  1300. void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
  1301. int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
  1302. void rend_service_dump_stats(int severity);
  1303. /********************************* rendmid.c *******************************/
  1304. int rend_mid_establish_intro(circuit_t *circ, const char *request, size_t request_len);
  1305. int rend_mid_introduce(circuit_t *circ, const char *request, size_t request_len);
  1306. int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1307. int rend_mid_rendezvous(circuit_t *circ, const char *request, size_t request_len);
  1308. /********************************* router.c ***************************/
  1309. void set_onion_key(crypto_pk_env_t *k);
  1310. crypto_pk_env_t *get_onion_key(void);
  1311. crypto_pk_env_t *get_previous_onion_key(void);
  1312. time_t get_onion_key_set_at(void);
  1313. void set_identity_key(crypto_pk_env_t *k);
  1314. crypto_pk_env_t *get_identity_key(void);
  1315. int identity_key_is_set(void);
  1316. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  1317. int init_keys(void);
  1318. crypto_pk_env_t *init_key_from_file(const char *fname);
  1319. void rotate_onion_key(void);
  1320. void router_set_bandwidth_capacity(int bw);
  1321. int router_get_bandwidth_capacity(void);
  1322. void router_retry_connections(void);
  1323. int router_is_clique_mode(routerinfo_t *router);
  1324. void router_upload_dir_desc_to_dirservers(int force);
  1325. void mark_my_descriptor_dirty(void);
  1326. int router_compare_to_my_exit_policy(connection_t *conn);
  1327. routerinfo_t *router_get_my_routerinfo(void);
  1328. const char *router_get_my_descriptor(void);
  1329. int router_is_me(routerinfo_t *router);
  1330. int router_rebuild_descriptor(int force);
  1331. int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  1332. crypto_pk_env_t *ident_key);
  1333. int is_legal_nickname(const char *s);
  1334. int is_legal_nickname_or_hexdigest(const char *s);
  1335. /********************************* routerlist.c ***************************/
  1336. typedef struct trusted_dir_server_t {
  1337. char *address;
  1338. uint32_t addr;
  1339. uint16_t dir_port;
  1340. char digest[DIGEST_LEN];
  1341. int is_running;
  1342. } trusted_dir_server_t;
  1343. int router_reload_router_list(void);
  1344. void router_get_trusted_dir_servers(smartlist_t **outp);
  1345. routerinfo_t *router_pick_directory_server(int requireothers,
  1346. int fascistfirewall,
  1347. int for_running_routers,
  1348. int retry_if_no_servers);
  1349. trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
  1350. int fascistfirewall,
  1351. int retry_if_no_servers);
  1352. int all_trusted_directory_servers_down(void);
  1353. struct smartlist_t;
  1354. void routerlist_add_family(struct smartlist_t *sl, routerinfo_t *router);
  1355. void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list, int warn_if_down);
  1356. int router_nickname_is_in_list(routerinfo_t *router, const char *list);
  1357. routerinfo_t *routerlist_find_my_routerinfo(void);
  1358. int router_nickname_matches(routerinfo_t *router, const char *nickname);
  1359. /** How many seconds a router must be up before we'll use it for
  1360. * reliability-critical node positions.
  1361. */
  1362. #define ROUTER_REQUIRED_MIN_UPTIME 3600 /* an hour */
  1363. #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
  1364. int router_is_unreliable_router(routerinfo_t *router, int need_uptime, int need_bw);
  1365. routerinfo_t *routerlist_sl_choose_by_bandwidth(smartlist_t *sl);
  1366. routerinfo_t *router_choose_random_node(const char *preferred,
  1367. const char *excluded,
  1368. struct smartlist_t *excludedsmartlist,
  1369. int preferuptime, int preferbandwidth,
  1370. int allow_unverified, int strict);
  1371. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  1372. routerinfo_t *router_get_by_nickname(const char *nickname);
  1373. routerinfo_t *router_get_by_hexdigest(const char *hexdigest);
  1374. routerinfo_t *router_get_by_digest(const char *digest);
  1375. int router_digest_is_trusted_dir(const char *digest);
  1376. void router_get_routerlist(routerlist_t **prouterlist);
  1377. time_t routerlist_get_published_time(void);
  1378. void routerlist_free(routerlist_t *routerlist);
  1379. void routerinfo_free(routerinfo_t *router);
  1380. routerinfo_t *routerinfo_copy(const routerinfo_t *router);
  1381. void router_mark_as_down(const char *digest);
  1382. void routerlist_remove_old_routers(int age);
  1383. int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey,
  1384. int check_version);
  1385. int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  1386. addr_policy_t *policy);
  1387. #define ADDR_POLICY_ACCEPTED 0
  1388. #define ADDR_POLICY_REJECTED -1
  1389. #define ADDR_POLICY_UNKNOWN 1
  1390. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  1391. int router_exit_policy_rejects_all(routerinfo_t *router);
  1392. void running_routers_free(running_routers_t *rr);
  1393. void routerlist_update_from_runningrouters(routerlist_t *list,
  1394. running_routers_t *rr);
  1395. int routers_update_status_from_entry(smartlist_t *routers,
  1396. time_t list_time,
  1397. const char *s,
  1398. int rr_format);
  1399. int router_update_status_from_smartlist(routerinfo_t *r,
  1400. time_t list_time,
  1401. smartlist_t *running_list,
  1402. int rr_format);
  1403. void add_trusted_dir_server(const char *addr, uint16_t port,const char *digest);
  1404. void clear_trusted_dir_servers(void);
  1405. /********************************* routerparse.c ************************/
  1406. #define MAX_STATUS_TAG_LEN 32
  1407. /** Structure to hold parsed Tor versions. This is a little messier
  1408. * than we would like it to be, because we changed version schemes with 0.1.0.
  1409. *
  1410. * Before 0.1.0, versions were of the format:
  1411. * MAJOR.MINOR.MICRO(status(PATCHLEVEL))?(-cvs)?
  1412. * where MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, status is one of
  1413. * "pre" (for an alpha release), "rc" (for a release candidate), or "." for a
  1414. * release. As a special case, "a.b.c" was equivalent to "a.b.c.0". We
  1415. * compare the elements in order (major, minor, micro, status, patchlevel,
  1416. * cvs), with "cvs" preceding non-cvs.
  1417. *
  1418. * We would start each development branch with a final version in mind: say,
  1419. * "0.0.8". Our first pre-release would be "0.0.8pre1", followed by (for
  1420. * example) "0.0.8pre2-cvs", "0.0.8pre2", "0.0.8pre3-cvs", "0.0.8rc1",
  1421. * "0.0.8rc2-cvs", and "0.0.8rc2". Finally, we'd release 0.0.8. The stable
  1422. * CVS branch would then be versioned "0.0.8.1-cvs", and any eventual bugfix
  1423. * release would be "0.0.8.1".
  1424. *
  1425. * After 0.1.0, versions are of the format:
  1426. * MAJOR.MINOR.MICRO(.PATCHLEVEL([-.]status_tag)?)?
  1427. * As before, MAJOR, MINOR, MICRO, and PATCHLEVEL are numbers, with an absent
  1428. * number equivalent to 0. All versions _should_ be distinguishable purely by
  1429. * those four numbers; the status tag is purely informational. If we *do*
  1430. * encounter two versions that differ only by status tag, we compare them
  1431. * lexically.
  1432. *
  1433. * Now, we start each development branch with (say) 0.1.1.1-alpha.
  1434. * The patchlevel increments consistently as the status tag changes,
  1435. * for example, as in: 0.1.1.2-alpha, 0.1.1.3-alpha, 0.1.1.4-rc
  1436. * 0.1.1.5-rc, Eventually, we release 0.1.1.6. The next patch release
  1437. * is 0.1.1.7.
  1438. *
  1439. * Between these releases, CVS is versioned with a -cvs tag: after
  1440. * 0.1.1.1-alpha comes 0.1.1.1-alpha-cvs, and so on.
  1441. */
  1442. typedef struct tor_version_t {
  1443. int major;
  1444. int minor;
  1445. int micro;
  1446. /** Release status. For version in the post-0.1 format, this is always
  1447. * VER_RELEASE. */
  1448. enum { VER_PRE=0, VER_RC=1, VER_RELEASE=2, } status;
  1449. int patchlevel;
  1450. /** CVS status. For version in the post-0.1 format, this is always
  1451. * IS_NOT_CVS */
  1452. enum { IS_CVS=0, IS_NOT_CVS=1} cvs;
  1453. char status_tag[MAX_STATUS_TAG_LEN];
  1454. } tor_version_t;
  1455. int router_get_router_hash(const char *s, char *digest);
  1456. int router_get_dir_hash(const char *s, char *digest);
  1457. int router_get_runningrouters_hash(const char *s, char *digest);
  1458. int router_parse_list_from_string(const char **s,
  1459. routerlist_t **dest,
  1460. smartlist_t *good_nickname_list,
  1461. int rr_format,
  1462. time_t published);
  1463. int router_parse_routerlist_from_directory(const char *s,
  1464. routerlist_t **dest,
  1465. crypto_pk_env_t *pkey,
  1466. int check_version);
  1467. running_routers_t *router_parse_runningrouters(const char *str);
  1468. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
  1469. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  1470. addr_policy_t *router_parse_addr_policy_from_string(const char *s);
  1471. int check_software_version_against_directory(const char *directory,
  1472. int ignoreversion);
  1473. int tor_version_parse(const char *s, tor_version_t *out);
  1474. int tor_version_as_new_as(const char *platform, const char *cutoff);
  1475. int tor_version_compare(tor_version_t *a, tor_version_t *b);
  1476. void assert_addr_policy_ok(addr_policy_t *t);
  1477. #endif