or.h 78 KB

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