or.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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_SYS_PARAM_H
  38. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  39. #endif
  40. #ifdef HAVE_SYS_LIMITS_H
  41. #include <sys/limits.h>
  42. #endif
  43. #ifdef HAVE_MACHINE_LIMITS_H
  44. #ifndef __FreeBSD__
  45. /* FreeBSD has a bug where it complains that this file is obsolete,
  46. and I should migrate to using sys/limits. It complains even when
  47. I include both. */
  48. #include <machine/limits.h>
  49. #endif
  50. #endif
  51. #ifdef HAVE_SYS_TYPES_H
  52. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  53. #endif
  54. #ifdef HAVE_SYS_WAIT_H
  55. #include <sys/wait.h>
  56. #endif
  57. #ifdef HAVE_SYS_FCNTL_H
  58. #include <sys/fcntl.h>
  59. #endif
  60. #ifdef HAVE_FCNTL_H
  61. #include <fcntl.h>
  62. #endif
  63. #ifdef HAVE_SYS_IOCTL_H
  64. #include <sys/ioctl.h>
  65. #endif
  66. #ifdef HAVE_SYS_SOCKET_H
  67. #include <sys/socket.h>
  68. #endif
  69. #ifdef HAVE_SYS_TIME_H
  70. #include <sys/time.h>
  71. #endif
  72. #ifdef HAVE_SYS_STAT_H
  73. #include <sys/stat.h>
  74. #endif
  75. #ifdef HAVE_NETINET_IN_H
  76. #include <netinet/in.h>
  77. #endif
  78. #ifdef HAVE_ARPA_INET_H
  79. #include <arpa/inet.h>
  80. #endif
  81. #ifdef HAVE_ERRNO_H
  82. #include <errno.h>
  83. #endif
  84. #ifdef HAVE_ASSERT_H
  85. #include <assert.h>
  86. #endif
  87. #ifdef HAVE_TIME_H
  88. #include <time.h>
  89. #endif
  90. #ifdef HAVE_WINSOCK_H
  91. #include <winsock.h>
  92. #endif
  93. #if _MSC_VER > 1300
  94. #include <winsock2.h>
  95. #include <ws2tcpip.h>
  96. #elif defined(_MSC_VER)
  97. #include <winsock.h>
  98. #endif
  99. #ifdef MS_WINDOWS
  100. #include <io.h>
  101. #include <process.h>
  102. #include <direct.h>
  103. #include <windows.h>
  104. #define snprintf _snprintf
  105. #endif
  106. #include "../common/crypto.h"
  107. #include "../common/tortls.h"
  108. #include "../common/log.h"
  109. #include "../common/util.h"
  110. /** Upper bound on maximum simultaneous connections; can be lowered by
  111. * config file. */
  112. #define MAXCONNECTIONS 10000
  113. #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
  114. #define MAX_NICKNAME_LEN 19
  115. #define MAX_DIR_SIZE 500000
  116. #ifdef TOR_PERF
  117. /** How long do we keep DNS cache entries before purging them? */
  118. #define MAX_DNS_ENTRY_AGE (150*60)
  119. #else
  120. #define MAX_DNS_ENTRY_AGE (15*60)
  121. #endif
  122. /** How often do we rotate onion keys? */
  123. #define MIN_ONION_KEY_LIFETIME (120*60)
  124. /** How often do we rotate TLS contexts? */
  125. #define MAX_SSL_KEY_LIFETIME (120*60)
  126. /** How old do we allow a router to get before removing it, either
  127. * from the descriptor list (for dirservers) or the router list (for others)?
  128. * In seconds. */
  129. #define ROUTER_MAX_AGE (60*60*24)
  130. #define CIRC_ID_TYPE_LOWER 0
  131. #define CIRC_ID_TYPE_HIGHER 1
  132. #define _CONN_TYPE_MIN 3
  133. /** Type for sockets listening for OR connections. */
  134. #define CONN_TYPE_OR_LISTENER 3
  135. /** Type for OR-to-OR or OP-to-OR connections. */
  136. #define CONN_TYPE_OR 4
  137. /** Type for connections from final OR to chosen destination. */
  138. #define CONN_TYPE_EXIT 5
  139. /** Type for sockets listening for SOCKS connections. */
  140. #define CONN_TYPE_AP_LISTENER 6
  141. /** Type for SOCKS connections to OP. */
  142. #define CONN_TYPE_AP 7
  143. /** Type for sockets listening for HTTP connections to the directory server. */
  144. #define CONN_TYPE_DIR_LISTENER 8
  145. /** Type for HTTP connections to the directory server. */
  146. #define CONN_TYPE_DIR 9
  147. /** Type for connections to local dnsworker processes. */
  148. #define CONN_TYPE_DNSWORKER 10
  149. /** Type for connections to local cpuworker processes. */
  150. #define CONN_TYPE_CPUWORKER 11
  151. #define _CONN_TYPE_MAX 11
  152. /** State for any listener connection. */
  153. #define LISTENER_STATE_READY 0
  154. #define _DNSWORKER_STATE_MIN 1
  155. /** State for a connection to a dnsworker process that's idle. */
  156. #define DNSWORKER_STATE_IDLE 1
  157. /** State for a connection to a dnsworker process that's resolving a hostname. */
  158. #define DNSWORKER_STATE_BUSY 2
  159. #define _DNSWORKER_STATE_MAX 2
  160. #define _CPUWORKER_STATE_MIN 1
  161. /** State for a connection to a cpuworker process that's idle. */
  162. #define CPUWORKER_STATE_IDLE 1
  163. /** State for a connection to a cpuworker process that's processing a
  164. * handshake. */
  165. #define CPUWORKER_STATE_BUSY_ONION 2
  166. #define _CPUWORKER_STATE_MAX 2
  167. #define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION
  168. #define _OR_CONN_STATE_MIN 1
  169. /** State for a connection to an OR: waiting for connect() to finish. */
  170. #define OR_CONN_STATE_CONNECTING 1
  171. /** State for a connection to an OR: SSL is handshaking, not done yet. */
  172. #define OR_CONN_STATE_HANDSHAKING 2
  173. /** State for a connection to an OR: Ready to send/receive cells. */
  174. #define OR_CONN_STATE_OPEN 3
  175. #define _OR_CONN_STATE_MAX 3
  176. #define _EXIT_CONN_STATE_MIN 1
  177. /** State for an exit connection: waiting for response from dns farm. */
  178. #define EXIT_CONN_STATE_RESOLVING 1
  179. /** State for an exit connection: waiting for connect() to finish. */
  180. #define EXIT_CONN_STATE_CONNECTING 2
  181. /** State for an exit connection: open and ready to transmit data. */
  182. #define EXIT_CONN_STATE_OPEN 3
  183. /** State for an exit connection: waiting to be removed. */
  184. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  185. #define _EXIT_CONN_STATE_MAX 4
  186. #if 0
  187. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  188. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  189. #endif
  190. /* the AP state values must be disjoint from the EXIT state values */
  191. #define _AP_CONN_STATE_MIN 5
  192. /** State for a SOCKS connection: waiting for SOCKS request. */
  193. #define AP_CONN_STATE_SOCKS_WAIT 5
  194. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  195. * rendezvous rescriptor. */
  196. #define AP_CONN_STATE_RENDDESC_WAIT 6
  197. /** State for a SOCKS connection: waiting for a completed circuit. */
  198. #define AP_CONN_STATE_CIRCUIT_WAIT 7
  199. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  200. #define AP_CONN_STATE_CONNECT_WAIT 8
  201. /** State for a SOCKS connection: ready to send and receive. */
  202. #define AP_CONN_STATE_OPEN 9
  203. #define _AP_CONN_STATE_MAX 9
  204. #define _DIR_CONN_STATE_MIN 1
  205. /** State for connection to directory server: waiting for connect(). */
  206. #define DIR_CONN_STATE_CONNECTING 1
  207. /** State for connection to directory server: sending HTTP request. */
  208. #define DIR_CONN_STATE_CLIENT_SENDING 2
  209. /** State for connection to directory server: reading HTTP response. */
  210. #define DIR_CONN_STATE_CLIENT_READING 3
  211. /** State for connection at directory server: waiting for HTTP request. */
  212. #define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
  213. /** State for connection at directory server: sending HTTP response. */
  214. #define DIR_CONN_STATE_SERVER_WRITING 5
  215. #define _DIR_CONN_STATE_MAX 5
  216. #define _DIR_PURPOSE_MIN 1
  217. /** Purpose for connection to directory server: download a directory. */
  218. #define DIR_PURPOSE_FETCH_DIR 1
  219. /** Purpose for connection to directory server: download a rendezvous
  220. * descriptor. */
  221. #define DIR_PURPOSE_FETCH_RENDDESC 2
  222. /** Purpose for connection to directory server: set after a rendezvous
  223. * descriptor is downloaded. */
  224. #define DIR_PURPOSE_HAS_FETCHED_RENDDESC 3
  225. /** Purpose for connection to directory server: upload a server descriptor. */
  226. #define DIR_PURPOSE_UPLOAD_DIR 4
  227. /** Purpose for connection to directory server: upload a rendezvous
  228. * descriptor. */
  229. #define DIR_PURPOSE_UPLOAD_RENDDESC 5
  230. /** Purpose for connection at a directory server. */
  231. #define DIR_PURPOSE_SERVER 6
  232. #define _DIR_PURPOSE_MAX 6
  233. /** Circuit state: I'm the OP, still haven't done all my handshakes. */
  234. #define CIRCUIT_STATE_BUILDING 0
  235. /** Circuit state: Waiting to process the onionskin. */
  236. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  237. /** Circuit state: I'm the OP, my firsthop is still connecting. */
  238. #define CIRCUIT_STATE_OR_WAIT 2
  239. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  240. #define CIRCUIT_STATE_OPEN 3
  241. #define _CIRCUIT_PURPOSE_MIN 1
  242. /* these circuits were initiated elsewhere */
  243. #define _CIRCUIT_PURPOSE_OR_MIN 1
  244. /** OR-side circuit purpose: normal circuit, at OR. */
  245. #define CIRCUIT_PURPOSE_OR 1
  246. /** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
  247. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  248. /** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
  249. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  250. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  251. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  252. #define _CIRCUIT_PURPOSE_OR_MAX 4
  253. /* these circuits originate at this node */
  254. /* here's how circ client-side purposes work:
  255. * normal circuits are C_GENERAL.
  256. * circuits that are c_introducing are either on their way to
  257. * becoming open, or they are open and waiting for a
  258. * suitable rendcirc before they send the intro.
  259. * circuits that are c_introduce_ack_wait have sent the intro,
  260. * but haven't gotten a response yet.
  261. * circuits that are c_establish_rend are either on their way
  262. * to becoming open, or they are open and have sent the
  263. * establish_rendezvous cell but haven't received an ack.
  264. * circuits that are c_rend_ready are open and have received a
  265. * rend ack, but haven't heard from bob yet. if they have a
  266. * buildstate->pending_final_cpath then they're expecting a
  267. * cell from bob, else they're not.
  268. * circuits that are c_rend_ready_intro_acked are open, and
  269. * some intro circ has sent its intro and received an ack.
  270. * circuits that are c_rend_joined are open, have heard from
  271. * bob, and are talking to him.
  272. */
  273. /** Client-side circuit purpose: Normal circuit, with cpath. */
  274. #define CIRCUIT_PURPOSE_C_GENERAL 5
  275. /** Client-side circuit purpose: at Alice, connecting to intro point. */
  276. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  277. /** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
  278. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  279. /** Client-side circuit purpose: at Alice, introduced and acked, closing. */
  280. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  281. /** Client-side circuit purpose: at Alice, waiting for ack. */
  282. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  283. /** Client-side circuit purpose: at Alice, waiting for Bob. */
  284. #define CIRCUIT_PURPOSE_C_REND_READY 10
  285. /** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
  286. * has been acknowledged. */
  287. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  288. /** Client-side circuit purpose: at Alice, rendezvous established. */
  289. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  290. /** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
  291. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
  292. /** Hidden-service-side circuit purpose: at Bob, successfully established
  293. * intro. */
  294. #define CIRCUIT_PURPOSE_S_INTRO 14
  295. /** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
  296. #define CIRCUIT_PURPOSE_S_CONNECT_REND 15
  297. /** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
  298. #define CIRCUIT_PURPOSE_S_REND_JOINED 16
  299. #define _CIRCUIT_PURPOSE_MAX 16
  300. /** True iff the circuit purpose <b>p</b> is for a circuit at the OP
  301. * that this OP has originated. */
  302. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
  303. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  304. #define RELAY_COMMAND_BEGIN 1
  305. #define RELAY_COMMAND_DATA 2
  306. #define RELAY_COMMAND_END 3
  307. #define RELAY_COMMAND_CONNECTED 4
  308. #define RELAY_COMMAND_SENDME 5
  309. #define RELAY_COMMAND_EXTEND 6
  310. #define RELAY_COMMAND_EXTENDED 7
  311. #define RELAY_COMMAND_TRUNCATE 8
  312. #define RELAY_COMMAND_TRUNCATED 9
  313. #define RELAY_COMMAND_DROP 10
  314. #define RELAY_COMMAND_RESOLVE 11
  315. #define RELAY_COMMAND_RESOLVED 12
  316. #define RELAY_COMMAND_ESTABLISH_INTRO 32
  317. #define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
  318. #define RELAY_COMMAND_INTRODUCE1 34
  319. #define RELAY_COMMAND_INTRODUCE2 35
  320. #define RELAY_COMMAND_RENDEZVOUS1 36
  321. #define RELAY_COMMAND_RENDEZVOUS2 37
  322. /* DOCDOC Spec these next two. */
  323. #define RELAY_COMMAND_INTRO_ESTABLISHED 38
  324. #define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
  325. #define RELAY_COMMAND_INTRODUCE_ACK 40
  326. #define _MIN_END_STREAM_REASON 1
  327. #define END_STREAM_REASON_MISC 1
  328. #define END_STREAM_REASON_RESOLVEFAILED 2
  329. #define END_STREAM_REASON_CONNECTFAILED 3
  330. #define END_STREAM_REASON_EXITPOLICY 4
  331. #define END_STREAM_REASON_DESTROY 5
  332. #define END_STREAM_REASON_DONE 6
  333. #define END_STREAM_REASON_TIMEOUT 7
  334. #define _MAX_END_STREAM_REASON 7
  335. /** Length of 'y' portion of 'y.onion' URL. */
  336. #define REND_SERVICE_ID_LEN 16
  337. #define CELL_DIRECTION_IN 1
  338. #define CELL_DIRECTION_OUT 2
  339. #ifdef TOR_PERF
  340. #define CIRCWINDOW_START 10000
  341. #define CIRCWINDOW_INCREMENT 1000
  342. #define STREAMWINDOW_START 5000
  343. #define STREAMWINDOW_INCREMENT 500
  344. #else
  345. #define CIRCWINDOW_START 1000
  346. #define CIRCWINDOW_INCREMENT 100
  347. #define STREAMWINDOW_START 500
  348. #define STREAMWINDOW_INCREMENT 50
  349. #endif
  350. /* cell commands */
  351. #define CELL_PADDING 0
  352. #define CELL_CREATE 1
  353. #define CELL_CREATED 2
  354. #define CELL_RELAY 3
  355. #define CELL_DESTROY 4
  356. /* legal characters in a nickname */
  357. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  358. #define SOCKS4_NETWORK_LEN 8
  359. /*
  360. * Relay payload:
  361. * Relay command [1 byte]
  362. * Recognized [2 bytes]
  363. * Stream ID [2 bytes]
  364. * Partial SHA-1 [4 bytes]
  365. * Length [2 bytes]
  366. * Relay payload [498 bytes]
  367. */
  368. #define CELL_PAYLOAD_SIZE 509
  369. #define CELL_NETWORK_SIZE 512
  370. #define RELAY_HEADER_SIZE (1+2+2+4+2)
  371. #define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)
  372. /** Parsed onion routing cell. All communication from OP-to-OR, or from
  373. * OR-to-OR, is via cells. */
  374. typedef struct {
  375. uint16_t circ_id; /**< Circuit which received the cell. */
  376. unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
  377. * or DESTROY. */
  378. unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
  379. } cell_t;
  380. /** Beginning of a RELAY cell payload. */
  381. typedef struct {
  382. uint8_t command; /**< The end-to-end relay command. */
  383. uint16_t recognized; /**< Used to tell whether cell is for us. */
  384. uint16_t stream_id; /**< Which stream is this cell associated with? */
  385. char integrity[4]; /**< Used to tell whether cell is corrupted. */
  386. uint16_t length; /**< How long is the payload body? */
  387. } relay_header_t;
  388. typedef struct buf_t buf_t;
  389. typedef struct socks_request_t socks_request_t;
  390. #define CONNECTION_MAGIC 0x7C3C304Eu
  391. /** Description of a connection to another host or process, and associated
  392. * data. */
  393. struct connection_t {
  394. uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
  395. uint8_t type; /**< What kind of connection is this? */
  396. uint8_t state; /**< Current state of this connection. */
  397. uint8_t purpose; /**< Only used for DIR types currently. */
  398. uint8_t wants_to_read; /**< Boolean: should we start reading again once
  399. * the bandwidth throttler allows it?
  400. */
  401. uint8_t wants_to_write; /**< Boolean: should we start writing again once
  402. * the bandwidth throttler allows reads?
  403. */
  404. int s; /**< Our socket; -1 if this connection is closed. */
  405. int poll_index; /**< Index of this conn into the poll_array. */
  406. int marked_for_close; /**< Boolean: should we close this conn on the next
  407. * iteration of the main loop?
  408. */
  409. char *marked_for_close_file; /**< For debugging: in which file were we marked
  410. * for close? */
  411. int hold_open_until_flushed; /**< Despite this connection's being marked
  412. * for close, do we flush it before closing it?
  413. */
  414. buf_t *inbuf; /**< Buffer holding data read over this connection. */
  415. int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
  416. time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
  417. buf_t *outbuf; /**< Buffer holding data to write over this connection. */
  418. int outbuf_flushlen; /**< How much data should we try to flush from the
  419. * outbuf? */
  420. time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
  421. time_t timestamp_created; /**< When was this connection_t created? */
  422. uint32_t addr; /**< IP of the other side of the connection; used to identify
  423. * routers, along with port. */
  424. uint16_t port; /**< If non-zero, port on the other end
  425. * of the connection. */
  426. char *address; /**< FQDN (or IP) of the guy on the other end.
  427. * strdup into this, because free_connection frees it.
  428. */
  429. crypto_pk_env_t *identity_pkey; /**> Public RSA key for the other side's
  430. * signing key. */
  431. char *nickname; /**< Nickname of OR on other side (if any). */
  432. /* Used only by OR connections: */
  433. tor_tls *tls; /**< TLS connection state (OR only.) */
  434. uint16_t next_circ_id; /**< Which circ_id do we try to use next on
  435. * this connection? This is always in the
  436. * range 0..1<<15-1. (OR only.)*/
  437. /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  438. int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
  439. int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
  440. * add 'bandwidth' to this, capping it at 10*bandwidth.
  441. * (OPEN ORs only)
  442. */
  443. /* Used only by DIR and AP connections: */
  444. char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
  445. * querying for? (DIR/AP only) */
  446. /* Used only by edge connections: */
  447. uint16_t stream_id;
  448. struct connection_t *next_stream; /**< Points to the next stream at this
  449. * edge, if any (Edge only). */
  450. struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
  451. * this conn exits at. (Edge only.) */
  452. int package_window; /**< How many more relay cells can i send into the
  453. * circuit? (Edge only.) */
  454. int deliver_window; /**< How many more relay cells can end at me? (Edge
  455. * only.) */
  456. int done_sending; /**< For half-open connections; not used currently. */
  457. int done_receiving; /**< For half-open connections; not used currently. */
  458. char has_sent_end; /**< For debugging: set once we've set the stream end,
  459. and check in circuit_about_to_close_connection(). */
  460. char num_retries; /**< How many times have we re-tried beginning this stream? (Edge only) */
  461. /* Used only by AP connections */
  462. socks_request_t *socks_request; /**< SOCKS structure describing request (AP
  463. * only.) */
  464. };
  465. typedef struct connection_t connection_t;
  466. #define EXIT_POLICY_ACCEPT 1
  467. #define EXIT_POLICY_REJECT 2
  468. /** A linked list of exit policy rules */
  469. struct exit_policy_t {
  470. char policy_type; /**< One of EXIT_POLICY_ACCEPT or EXIT_POLICY_REJECT. */
  471. char *string; /**< String representation of this rule. */
  472. uint32_t addr; /**< Base address to accept or reject. */
  473. uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that a & msk ==
  474. * <b>addr</b> & msk . */
  475. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  476. uint16_t prt_max; /**< Highest port number to accept/reject. */
  477. struct exit_policy_t *next; /**< Next rule in list. */
  478. };
  479. /** Information about another onion router in the network. */
  480. typedef struct {
  481. char *address; /**< Location of OR: either a hostname or an IP address. */
  482. char *nickname; /**< Human-readable OR name. */
  483. uint32_t addr; /**< IPv4 address of OR, in host order. */
  484. uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
  485. uint16_t socks_port; /**< Port for SOCKS connections. */
  486. uint16_t dir_port; /**< Port for HTTP directory connections. */
  487. time_t published_on; /**< When was the information in this routerinfo_t
  488. * published? */
  489. crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  490. crypto_pk_env_t *identity_pkey; /**< Public RSA key for signing. */
  491. char *platform; /**< What software/operating system is this OR using? */
  492. /* link info */
  493. uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
  494. * bucket per second? */
  495. uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
  496. struct exit_policy_t *exit_policy; /**< What streams will this OR permit
  497. * to exit? */
  498. /* local info */
  499. int is_running; /**< As far as we know, is this OR currently running? */
  500. int is_trusted_dir; /**< Do we trust this OR as a directory server? */
  501. } routerinfo_t;
  502. /** Contents of a directory of onion routers. */
  503. typedef struct {
  504. /** List of routerinfo_t */
  505. smartlist_t *routers;
  506. /** Which versions of tor are recommended by this directory? */
  507. char *software_versions;
  508. /** When was the most recent directory that contributed to this list
  509. * published?
  510. */
  511. time_t published_on;
  512. /** Which router is claimed to have signed it? */
  513. char *signing_router;
  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. int connection_in_array(connection_t *conn);
  963. void get_connection_array(connection_t ***array, int *n);
  964. void connection_watch_events(connection_t *conn, short events);
  965. int connection_is_reading(connection_t *conn);
  966. void connection_stop_reading(connection_t *conn);
  967. void connection_start_reading(connection_t *conn);
  968. int connection_is_writing(connection_t *conn);
  969. void connection_stop_writing(connection_t *conn);
  970. void connection_start_writing(connection_t *conn);
  971. void directory_has_arrived(void);
  972. int main(int argc, char *argv[]);
  973. /********************************* onion.c ***************************/
  974. int onion_pending_add(circuit_t *circ);
  975. circuit_t *onion_next_task(void);
  976. void onion_pending_remove(circuit_t *circ);
  977. int onion_skin_create(crypto_pk_env_t *router_key,
  978. crypto_dh_env_t **handshake_state_out,
  979. char *onion_skin_out);
  980. int onion_skin_server_handshake(char *onion_skin,
  981. crypto_pk_env_t *private_key,
  982. crypto_pk_env_t *prev_private_key,
  983. char *handshake_reply_out,
  984. char *key_out,
  985. int key_out_len);
  986. int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  987. char *handshake_reply,
  988. char *key_out,
  989. int key_out_len);
  990. /********************************* relay.c ***************************/
  991. extern unsigned long stats_n_relay_cells_relayed;
  992. extern unsigned long stats_n_relay_cells_delivered;
  993. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  994. int cell_direction);
  995. void relay_header_pack(char *dest, const relay_header_t *src);
  996. void relay_header_unpack(relay_header_t *dest, const char *src);
  997. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  998. int relay_command, const char *payload,
  999. int payload_len, crypt_path_t *cpath_layer);
  1000. int connection_edge_package_raw_inbuf(connection_t *conn);
  1001. void connection_edge_consider_sending_sendme(connection_t *conn);
  1002. extern uint64_t stats_n_data_cells_packaged;
  1003. extern uint64_t stats_n_data_bytes_packaged;
  1004. extern uint64_t stats_n_data_cells_received;
  1005. extern uint64_t stats_n_data_bytes_received;
  1006. /********************************* rephist.c ***************************/
  1007. void rep_hist_init(void);
  1008. void rep_hist_note_connect_failed(const char* nickname, time_t when);
  1009. void rep_hist_note_connect_succeeded(const char* nickname, time_t when);
  1010. void rep_hist_note_disconnect(const char* nickname, time_t when);
  1011. void rep_hist_note_connection_died(const char* nickname, time_t when);
  1012. void rep_hist_note_extend_succeeded(const char *from_name,
  1013. const char *to_name);
  1014. void rep_hist_note_extend_failed(const char *from_name, const char *to_name);
  1015. void rep_hist_dump_stats(time_t now, int severity);
  1016. /********************************* rendclient.c ***************************/
  1017. void rend_client_introcirc_has_opened(circuit_t *circ);
  1018. void rend_client_rendcirc_has_opened(circuit_t *circ);
  1019. int rend_client_introduction_acked(circuit_t *circ, const char *request, int request_len);
  1020. void rend_client_refetch_renddesc(const char *query);
  1021. int rend_client_remove_intro_point(char *failed_intro, const char *query);
  1022. int rend_client_rendezvous_acked(circuit_t *circ, const char *request, int request_len);
  1023. int rend_client_receive_rendezvous(circuit_t *circ, const char *request, int request_len);
  1024. void rend_client_desc_fetched(char *query, int success);
  1025. char *rend_client_get_random_intro(char *query);
  1026. int rend_parse_rendezvous_address(char *address);
  1027. int rend_client_send_introduction(circuit_t *introcirc, circuit_t *rendcirc);
  1028. /********************************* rendcommon.c ***************************/
  1029. typedef struct rend_service_descriptor_t {
  1030. crypto_pk_env_t *pk;
  1031. time_t timestamp;
  1032. int n_intro_points;
  1033. char **intro_points;
  1034. } rend_service_descriptor_t;
  1035. int rend_cmp_service_ids(const char *one, const char *two);
  1036. void rend_process_relay_cell(circuit_t *circ, int command, int length,
  1037. const char *payload);
  1038. void rend_service_descriptor_free(rend_service_descriptor_t *desc);
  1039. int rend_encode_service_descriptor(rend_service_descriptor_t *desc,
  1040. crypto_pk_env_t *key,
  1041. char **str_out,
  1042. int *len_out);
  1043. rend_service_descriptor_t *rend_parse_service_descriptor(const char *str, int len);
  1044. int rend_get_service_id(crypto_pk_env_t *pk, char *out);
  1045. typedef struct rend_cache_entry_t {
  1046. int len; /* Length of desc */
  1047. time_t received; /* When did we get the descriptor? */
  1048. char *desc; /* Service descriptor */
  1049. rend_service_descriptor_t *parsed; /* Parsed value of 'desc' */
  1050. } rend_cache_entry_t;
  1051. void rend_cache_init(void);
  1052. void rend_cache_clean(void);
  1053. int rend_valid_service_id(const char *query);
  1054. int rend_cache_lookup_desc(const char *query, const char **desc, int *desc_len);
  1055. int rend_cache_lookup_entry(const char *query, rend_cache_entry_t **entry_out);
  1056. int rend_cache_store(const char *desc, int desc_len);
  1057. /********************************* rendservice.c ***************************/
  1058. int rend_config_services(or_options_t *options);
  1059. int rend_service_load_keys(void);
  1060. void rend_services_init(void);
  1061. void rend_services_introduce(void);
  1062. void rend_services_upload(int force);
  1063. void rend_service_intro_has_opened(circuit_t *circuit);
  1064. int rend_service_intro_established(circuit_t *circuit, const char *request, int request_len);
  1065. void rend_service_rendezvous_has_opened(circuit_t *circuit);
  1066. int rend_service_introduce(circuit_t *circuit, const char *request, int request_len);
  1067. void rend_service_relaunch_rendezvous(circuit_t *oldcirc);
  1068. int rend_service_set_connection_addr_port(connection_t *conn, circuit_t *circ);
  1069. void rend_service_dump_stats(int severity);
  1070. /********************************* rendmid.c *******************************/
  1071. int rend_mid_establish_intro(circuit_t *circ, const char *request, int request_len);
  1072. int rend_mid_introduce(circuit_t *circ, const char *request, int request_len);
  1073. int rend_mid_establish_rendezvous(circuit_t *circ, const char *request, int request_len);
  1074. int rend_mid_rendezvous(circuit_t *circ, const char *request, int request_len);
  1075. /********************************* router.c ***************************/
  1076. void set_onion_key(crypto_pk_env_t *k);
  1077. crypto_pk_env_t *get_onion_key(void);
  1078. crypto_pk_env_t *get_previous_onion_key(void);
  1079. time_t get_onion_key_set_at(void);
  1080. void set_identity_key(crypto_pk_env_t *k);
  1081. crypto_pk_env_t *get_identity_key(void);
  1082. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last);
  1083. int init_keys(void);
  1084. crypto_pk_env_t *init_key_from_file(const char *fname);
  1085. void rotate_onion_key(void);
  1086. void router_retry_connections(void);
  1087. void router_upload_dir_desc_to_dirservers(void);
  1088. int router_compare_to_my_exit_policy(connection_t *conn);
  1089. routerinfo_t *router_get_my_routerinfo(void);
  1090. const char *router_get_my_descriptor(void);
  1091. int router_is_me(routerinfo_t *router);
  1092. int router_rebuild_descriptor(void);
  1093. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  1094. crypto_pk_env_t *ident_key);
  1095. /********************************* routerlist.c ***************************/
  1096. routerinfo_t *router_pick_directory_server(void);
  1097. struct smartlist_t;
  1098. void add_nickname_list_to_smartlist(struct smartlist_t *sl, const char *list);
  1099. void router_add_running_routers_to_smartlist(struct smartlist_t *sl);
  1100. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  1101. struct smartlist_t *excludedsmartlist);
  1102. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  1103. routerinfo_t *router_get_by_nickname(char *nickname);
  1104. void router_get_routerlist(routerlist_t **prouterlist);
  1105. void routerlist_free(routerlist_t *routerlist);
  1106. void routerlist_clear_trusted_directories(void);
  1107. void routerinfo_free(routerinfo_t *router);
  1108. routerinfo_t *routerinfo_copy(const routerinfo_t *router);
  1109. void router_mark_as_down(char *nickname);
  1110. void routerlist_remove_old_routers(void);
  1111. int router_load_routerlist_from_file(char *routerfile, int trusted);
  1112. int router_load_routerlist_from_string(const char *s, int trusted);
  1113. int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey);
  1114. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  1115. struct exit_policy_t *policy);
  1116. #define ADDR_POLICY_ACCEPTED 0
  1117. #define ADDR_POLICY_REJECTED -1
  1118. #define ADDR_POLICY_UNKNOWN 1
  1119. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
  1120. int router_exit_policy_rejects_all(routerinfo_t *router);
  1121. /********************************* routerparse.c ************************/
  1122. int router_get_router_hash(const char *s, char *digest);
  1123. int router_get_dir_hash(const char *s, char *digest);
  1124. int router_parse_list_from_string(const char **s,
  1125. routerlist_t **dest,
  1126. int n_good_nicknames,
  1127. const char **good_nickname_lst);
  1128. int router_parse_routerlist_from_directory(const char *s,
  1129. routerlist_t **dest,
  1130. crypto_pk_env_t *pkey);
  1131. routerinfo_t *router_parse_entry_from_string(const char *s, const char *end);
  1132. int router_add_exit_policy_from_string(routerinfo_t *router, const char *s);
  1133. struct exit_policy_t *router_parse_exit_policy_from_string(const char *s);
  1134. int check_software_version_against_directory(const char *directory,
  1135. int ignoreversion);
  1136. #endif
  1137. /*
  1138. Local Variables:
  1139. mode:c
  1140. indent-tabs-mode:nil
  1141. c-basic-offset:2
  1142. End:
  1143. */