HACKING 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 0. Intro.
  2. Onion Routing is still very much in development stages. This document
  3. aims to get you started in the right direction if you want to understand
  4. the code, add features, fix bugs, etc.
  5. Read the README file first, so you can get familiar with the basics.
  6. The pieces.
  7. Routers. Onion routers, as far as the 'tor' program is concerned,
  8. are a bunch of data items that are loaded into the router_array when
  9. the program starts. Periodically it downloads a new set of routers
  10. from a directory server, and updates the router_array. When a new OR
  11. connection is started (see below), the relevant information is copied
  12. from the router struct to the connection struct.
  13. Connections. A connection is a long-standing tcp socket between
  14. nodes. A connection is named based on what it's connected to -- an "OR
  15. connection" has an onion router on the other end, an "OP connection" has
  16. an onion proxy on the other end, an "exit connection" has a website or
  17. other server on the other end, and an "AP connection" has an application
  18. proxy (and thus a user) on the other end.
  19. Circuits. A circuit is a path over the onion routing
  20. network. Applications can connect to one end of the circuit, and can
  21. create exit connections at the other end of the circuit. AP and exit
  22. connections have only one circuit associated with them (and thus these
  23. connection types are closed when the circuit is closed), whereas OP and
  24. OR connections multiplex many circuits at once, and stay standing even
  25. when there are no circuits running over them.
  26. Streams. Streams are specific conversations between an AP and an exit.
  27. Streams are multiplexed over circuits.
  28. Cells. Some connections, specifically OR and OP connections, speak
  29. "cells". This means that data over that connection is bundled into 256
  30. byte packets (8 bytes of header and 248 bytes of payload). Each cell has
  31. a type, or "command", which indicates what it's for.
  32. Robustness features.
  33. [XXX no longer up to date]
  34. Bandwidth throttling. Each cell-speaking connection has a maximum
  35. bandwidth it can use, as specified in the routers.or file. Bandwidth
  36. throttling can occur on both the sender side and the receiving side. If
  37. the LinkPadding option is on, the sending side sends cells at regularly
  38. spaced intervals (e.g., a connection with a bandwidth of 25600B/s would
  39. queue a cell every 10ms). The receiving side protects against misbehaving
  40. servers that send cells more frequently, by using a simple token bucket:
  41. Each connection has a token bucket with a specified capacity. Tokens are
  42. added to the bucket each second (when the bucket is full, new tokens
  43. are discarded.) Each token represents permission to receive one byte
  44. from the network --- to receive a byte, the connection must remove a
  45. token from the bucket. Thus if the bucket is empty, that connection must
  46. wait until more tokens arrive. The number of tokens we add enforces a
  47. longterm average rate of incoming bytes, yet we still permit short-term
  48. bursts above the allowed bandwidth. Currently bucket sizes are set to
  49. ten seconds worth of traffic.
  50. The bandwidth throttling uses TCP to push back when we stop reading.
  51. We extend it with token buckets to allow more flexibility for traffic
  52. bursts.
  53. Data congestion control. Even with the above bandwidth throttling,
  54. we still need to worry about congestion, either accidental or intentional.
  55. If a lot of people make circuits into same node, and they all come out
  56. through the same connection, then that connection may become saturated
  57. (be unable to send out data cells as quickly as it wants to). An adversary
  58. can make a 'put' request through the onion routing network to a webserver
  59. he owns, and then refuse to read any of the bytes at the webserver end
  60. of the circuit. These bottlenecks can propagate back through the entire
  61. network, mucking up everything.
  62. (See the tor-spec.txt document for details of how congestion control
  63. works.)
  64. In practice, all the nodes in the circuit maintain a receive window
  65. close to maximum except the exit node, which stays around 0, periodically
  66. receiving a sendme and reading more data cells from the webserver.
  67. In this way we can use pretty much all of the available bandwidth for
  68. data, but gracefully back off when faced with multiple circuits (a new
  69. sendme arrives only after some cells have traversed the entire network),
  70. stalled network connections, or attacks.
  71. We don't need to reimplement full tcp windows, with sequence numbers,
  72. the ability to drop cells when we're full etc, because the tcp streams
  73. already guarantee in-order delivery of each cell. Rather than trying
  74. to build some sort of tcp-on-tcp scheme, we implement this minimal data
  75. congestion control; so far it's enough.
  76. Router twins. In many cases when we ask for a router with a given
  77. address and port, we really mean a router who knows a given key. Router
  78. twins are two or more routers that share the same private key. We thus
  79. give routers extra flexibility in choosing the next hop in the circuit: if
  80. some of the twins are down or slow, it can choose the more available ones.
  81. Currently the code tries for the primary router first, and if it's down,
  82. chooses the first available twin.
  83. Coding conventions:
  84. Log convention: use only these four log severities.
  85. ERR is if something fatal just happened.
  86. WARNING is something bad happened, but we're still running. The
  87. bad thing is either a bug in the code, an attack or buggy
  88. protocol/implementation of the remote peer, etc. The operator should
  89. examine the bad thing and try to correct it.
  90. (No error or warning messages should be expected. I expect most people
  91. to run on -l warning eventually. If a library function is currently
  92. called such that failure always means ERR, then the library function
  93. should log WARNING and let the caller log ERR.)
  94. INFO means something happened (maybe bad, maybe ok), but there's nothing
  95. you need to (or can) do about it.
  96. DEBUG is for everything louder than INFO.