or.h 64 KB

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