network.py 11 KB

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