or.h 66 KB

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