or.h 54 KB

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