or.h 53 KB

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