network.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/usr/bin/env python3
  2. import random
  3. import pickle
  4. from enum import Enum
  5. # Set this to True if you want the bytes sent and received to be added
  6. # symbolically, in terms of the numbers of each type of network message.
  7. # You will need sympy installed for this to work.
  8. symbolic_byte_counters = False
  9. if symbolic_byte_counters:
  10. import sympy
  11. # Network parameters
  12. # On average, how large is a consensus diff as compared to a full
  13. # consensus?
  14. P_Delta = 0.019
  15. class WOMode(Enum):
  16. """The different Walking Onion modes"""
  17. VANILLA = 0 # No Walking Onions
  18. TELESCOPING = 1 # Telescoping Walking Onions
  19. SINGLEPASS = 2 # Single-Pass Walking Onions
  20. def string_to_type(type_input):
  21. reprs = {'vanilla': WOMode.VANILLA, 'telescoping': WOMode.TELESCOPING,
  22. 'single-pass': WOMode.SINGLEPASS }
  23. if type_input in reprs.keys():
  24. return reprs[type_input]
  25. return -1
  26. class SNIPAuthMode(Enum):
  27. """The different styles of SNIP authentication"""
  28. NONE = 0 # No SNIPs; only used for WOMode = VANILLA
  29. MERKLE = 1 # Merkle trees
  30. THRESHSIG = 2 # Threshold signatures
  31. class EntType(Enum):
  32. """The different types of entities in the system."""
  33. NONE = 0
  34. DIRAUTH = 1
  35. RELAY = 2
  36. CLIENT = 3
  37. class PerfStats:
  38. """A class to store performance statistics for a relay or client.
  39. We keep track of bytes sent, bytes received, and counts of
  40. public-key operations of various types. We will reset these every
  41. epoch."""
  42. def __init__(self, ent_type):
  43. # Which type of entity is this for (DIRAUTH, RELAY, CLIENT)
  44. self.ent_type = ent_type
  45. # A printable name for the entity
  46. self.name = None
  47. self.reset()
  48. def __str__(self):
  49. return "%s: type=%s boot=%s sent=%s recv=%s keygen=%d sig=%d verif=%d dh=%d" % \
  50. (self.name, self.ent_type.name, self.is_bootstrapping, \
  51. self.bytes_sent, self.bytes_received, self.keygens, \
  52. self.sigs, self.verifs, self.dhs)
  53. def reset(self):
  54. """Reset the counters, typically at the beginning of each
  55. epoch."""
  56. # True if bootstrapping this epoch
  57. self.is_bootstrapping = False
  58. # Bytes sent and received
  59. self.bytes_sent = 0
  60. self.bytes_received = 0
  61. # Public-key operations: key generation, signing, verification,
  62. # Diffie-Hellman
  63. self.keygens = 0
  64. self.sigs = 0
  65. self.verifs = 0
  66. self.dhs = 0
  67. class NetAddr:
  68. """A class representing a network address"""
  69. nextaddr = 1
  70. def __init__(self):
  71. """Generate a fresh network address"""
  72. self.addr = NetAddr.nextaddr
  73. NetAddr.nextaddr += 1
  74. def __eq__(self, other):
  75. return (isinstance(other, self.__class__)
  76. and self.__dict__ == other.__dict__)
  77. def __hash__(self):
  78. return hash(self.addr)
  79. def __str__(self):
  80. return self.addr.__str__()
  81. class NetNoServer(Exception):
  82. """No server is listening on the address someone tried to connect
  83. to."""
  84. class Network:
  85. """A class representing a simulated network. Servers can bind()
  86. to the network, yielding a NetAddr (network address), and clients
  87. can connect() to a NetAddr yielding a Connection."""
  88. def __init__(self):
  89. self.servers = dict()
  90. self.epoch = 1
  91. self.epochcallbacks = []
  92. self.epochendingcallbacks = []
  93. self.dirauthkeylist = []
  94. self.fallbackrelays = []
  95. self.womode = WOMode.VANILLA
  96. self.snipauthmode = SNIPAuthMode.NONE
  97. def printservers(self):
  98. """Print the list of NetAddrs bound to something."""
  99. print("Servers:")
  100. for a in self.servers.keys():
  101. print(a)
  102. def setdirauthkey(self, index, vk):
  103. """Set the public verification key for dirauth number index to
  104. vk."""
  105. if index >= len(self.dirauthkeylist):
  106. self.dirauthkeylist.extend([None] * (index+1-len(self.dirauthkeylist)))
  107. self.dirauthkeylist[index] = vk
  108. def dirauthkeys(self):
  109. """Return the list of dirauth public verification keys."""
  110. return self.dirauthkeylist
  111. def getepoch(self):
  112. """Return the current epoch."""
  113. return self.epoch
  114. def nextepoch(self):
  115. """Increment the current epoch, and return it."""
  116. for c in self.epochendingcallbacks:
  117. c.epoch_ending(self.epoch)
  118. self.epoch += 1
  119. for c in self.epochcallbacks:
  120. c.newepoch(self.epoch)
  121. return self.epoch
  122. def wantepochticks(self, callback, want, end=False):
  123. """Register or deregister an object from receiving epoch change
  124. callbacks. If want is True, the callback object's newepoch()
  125. method will be called at each epoch change, with an argument of
  126. the new epoch. If want if False, the callback object will be
  127. deregistered. If end is True, the callback object's
  128. epoch_ending() method will be called instead at the end of the
  129. epoch, just _before_ the epoch number change."""
  130. if end:
  131. if want:
  132. self.epochendingcallbacks.append(callback)
  133. else:
  134. self.epochendingcallbacks.remove(callback)
  135. else:
  136. if want:
  137. self.epochcallbacks.append(callback)
  138. else:
  139. self.epochcallbacks.remove(callback)
  140. def bind(self, server):
  141. """Bind a server to a newly generated NetAddr, returning the
  142. NetAddr. The server's bound() callback will also be invoked."""
  143. addr = NetAddr()
  144. self.servers[addr] = server
  145. server.bound(addr, lambda: self.servers.pop(addr))
  146. return addr
  147. def connect(self, client, srvaddr, perfstats):
  148. """Connect the given client to the server bound to addr. Throw
  149. an exception if there is no server bound to that address."""
  150. try:
  151. server = self.servers[srvaddr]
  152. except KeyError:
  153. raise NetNoServer()
  154. conn = server.connected(client)
  155. conn.perfstats = perfstats
  156. return conn
  157. def setfallbackrelays(self, fallbackrelays):
  158. """Set the list of globally known fallback relays. Clients use
  159. these to bootstrap when they know no other relays."""
  160. self.fallbackrelays = fallbackrelays
  161. def getfallbackrelays(self):
  162. """Get the list of globally known fallback relays. Clients use
  163. these to bootstrap when they know no other relays."""
  164. return self.fallbackrelays
  165. def set_wo_style(self, womode, snipauthmode):
  166. """Set the Walking Onions mode and the SNIP authenticate mode
  167. for the network."""
  168. if ((womode == WOMode.VANILLA) \
  169. and (snipauthmode != SNIPAuthMode.NONE)) or \
  170. ((womode != WOMode.VANILLA) and \
  171. (snipauthmode == SNIPAuthMode.NONE)):
  172. # Incompatible settings
  173. raise ValueError("Bad argument combination")
  174. self.womode = womode
  175. self.snipauthmode = snipauthmode
  176. # The singleton instance of Network
  177. thenetwork = Network()
  178. # Initialize the (non-cryptographic) random seed
  179. random.seed(1)
  180. class NetMsg:
  181. """The parent class of network messages. Subclass this class to
  182. implement specific kinds of network messages."""
  183. def size(self):
  184. """Return the size of this network message. For now, just
  185. pickle it and return the length of that. There's some
  186. unnecessary overhead in this method; if you want specific
  187. messages to have more accurate sizes, override this method in
  188. the subclass. Alternately, if symbolic_byte_counters is set,
  189. return a symbolic representation of the message size instead, so
  190. that the total byte counts will clearly show how many of each
  191. message type were sent and received."""
  192. if symbolic_byte_counters:
  193. sz = sympy.symbols(type(self).__name__)
  194. else:
  195. sz = len(pickle.dumps(self))
  196. return sz
  197. class StringNetMsg(NetMsg):
  198. """Send an arbitratry string as a NetMsg."""
  199. def __init__(self, data):
  200. self.data = data
  201. def __str__(self):
  202. return self.data.__str__()
  203. class Connection:
  204. def __init__(self, peer = None):
  205. """Create a Connection object with the given peer."""
  206. self.peer = peer
  207. def closed(self):
  208. print("connection closed with", self.peer)
  209. self.peer = None
  210. def close(self):
  211. print("closing connection with", self.peer)
  212. self.peer.closed()
  213. self.peer = None
  214. class ClientConnection(Connection):
  215. """The parent class of client-side network connections. Subclass
  216. this class to do anything more elaborate than just passing arbitrary
  217. NetMsgs, which then get ignored. Use subclasses of this class when
  218. the server required no per-connection state, such as just fetching
  219. consensus documents."""
  220. def __init__(self, peer):
  221. """Create a ClientConnection object with the given peer. The
  222. peer must have a received(client, msg) method."""
  223. self.peer = peer
  224. self.perfstats = None
  225. def sendmsg(self, netmsg):
  226. assert(isinstance(netmsg, NetMsg))
  227. msgsize = netmsg.size()
  228. self.perfstats.bytes_sent += msgsize
  229. self.peer.received(self, netmsg)
  230. def reply(self, netmsg):
  231. assert(isinstance(netmsg, NetMsg))
  232. msgsize = netmsg.size()
  233. self.perfstats.bytes_received += msgsize
  234. self.receivedfromserver(netmsg)
  235. class ServerConnection(Connection):
  236. """The parent class of server-side network connections."""
  237. def __init__(self):
  238. self.peer = None
  239. def sendmsg(self, netmsg):
  240. assert(isinstance(netmsg, NetMsg))
  241. self.peer.received(netmsg)
  242. def received(self, client, netmsg):
  243. print("received", netmsg, "from client", client)
  244. class Server:
  245. """The parent class of network servers. Subclass this class to
  246. implement servers of different kinds. You will probably only need
  247. to override the implementation of connected()."""
  248. def __init__(self, name):
  249. self.name = name
  250. def __str__(self):
  251. return self.name.__str__()
  252. def bound(self, netaddr, closer):
  253. """Callback invoked when the server is successfully bound to a
  254. NetAddr. The parameters are the netaddr to which the server is
  255. bound and closer (as in a thing that causes something to close)
  256. is a callback function that should be used when the server
  257. wishes to stop listening for new connections."""
  258. self.closer = closer
  259. def close(self):
  260. """Stop listening for new connections."""
  261. self.closer()
  262. def connected(self, client):
  263. """Callback invoked when a client connects to this server.
  264. This callback must create the Connection object that will be
  265. returned to the client."""
  266. print("server", self, "conected to client", client)
  267. serverconnection = ServerConnection()
  268. clientconnection = ClientConnection(serverconnection)
  269. serverconnection.peer = clientconnection
  270. return clientconnection
  271. if __name__ == '__main__':
  272. n1 = NetAddr()
  273. n2 = NetAddr()
  274. assert(n1 == n1)
  275. assert(not (n1 == n2))
  276. assert(n1 != n2)
  277. print(n1, n2)
  278. srv = Server("hello world server")
  279. thenetwork.printservers()
  280. a = thenetwork.bind(srv)
  281. thenetwork.printservers()
  282. print("in main", a)
  283. perfstats = PerfStats(EntType.NONE)
  284. conn = thenetwork.connect("hello world client", a, perfstats)
  285. conn.sendmsg(StringNetMsg("hi"))
  286. conn.close()
  287. srv.close()
  288. thenetwork.printservers()