or.h 56 KB

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