network.py 15 KB

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