network.py 11 KB

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