or.h 68 KB

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