or.h 63 KB

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