or.h 73 KB

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