Selaa lähdekoodia

Be more consistent in terminology between "Msg" and "Cell"

Messages are the things send on Channels among relays and clients, and
are subclasses of RelayNetMsg.  One such message is the CircuitCellMsg.
A CircuitCellMsg contains a circuit id and a cell.  Cells are the things
sent within circuits, and are subclasses of RelayCell.
Ian Goldberg 4 vuotta sitten
vanhempi
commit
a8361be7e3
2 muutettua tiedostoa jossa 39 lisäystä ja 33 poistoa
  1. 10 10
      client.py
  2. 29 23
      relay.py

+ 10 - 10
client.py

@@ -10,8 +10,8 @@ import relay
 import nacl.hash
 import nacl.hash
 
 
 class VanillaCreatedExtendedHandler:
 class VanillaCreatedExtendedHandler:
-    """A handler for VanillaCreatedCircuitMsg and
-    VanillaExtendedCircuitMsg cells."""
+    """A handler for VanillaCreatedCircuitCell and
+    VanillaExtendedCircuitCell cells."""
 
 
     def __init__(self, cellhandler, ntor, expecteddesc):
     def __init__(self, cellhandler, ntor, expecteddesc):
         self.cellhandler = cellhandler
         self.cellhandler = cellhandler
@@ -26,11 +26,11 @@ class VanillaCreatedExtendedHandler:
         deckey = nacl.hash.sha256(secret + b'downstream')
         deckey = nacl.hash.sha256(secret + b'downstream')
         circhandler.add_crypt_layer(enckey, deckey)
         circhandler.add_crypt_layer(enckey, deckey)
         if len(circhandler.circuit_descs) == 0:
         if len(circhandler.circuit_descs) == 0:
-            # This was a VanillaCreatedCircuitMsg
-            del circhandler.cell_dispatch_table[relay.VanillaCreatedCircuitMsg]
+            # This was a VanillaCreatedCircuitCell
+            del circhandler.cell_dispatch_table[relay.VanillaCreatedCircuitCell]
         else:
         else:
-            # This was a VanillaExtendedCircuitMsg
-            del circhandler.cell_dispatch_table[relay.VanillaExtendedCircuitMsg]
+            # This was a VanillaExtendedCircuitCell
+            del circhandler.cell_dispatch_table[relay.VanillaExtendedCircuitCell]
         circhandler.circuit_descs.append(self.expecteddesc)
         circhandler.circuit_descs.append(self.expecteddesc)
 
 
         # Are we done building the circuit?
         # Are we done building the circuit?
@@ -45,14 +45,14 @@ class VanillaCreatedExtendedHandler:
             if nexthop.descdict['addr'] in circhandler.circuit_descs:
             if nexthop.descdict['addr'] in circhandler.circuit_descs:
                 nexthop = None
                 nexthop = None
 
 
-        # Construct the VanillaExtendCircuitMsg cell
+        # Construct the VanillaExtendCircuitCell
         ntor = relay.NTor(self.cellhandler.perfstats)
         ntor = relay.NTor(self.cellhandler.perfstats)
         ntor_request = ntor.request()
         ntor_request = ntor.request()
-        circextendmsg = relay.VanillaExtendCircuitMsg( \
+        circextendmsg = relay.VanillaExtendCircuitCell( \
                 nexthop.descdict['addr'], ntor_request)
                 nexthop.descdict['addr'], ntor_request)
 
 
         # Set up the reply handler
         # Set up the reply handler
-        circhandler.cell_dispatch_table[relay.VanillaExtendedCircuitMsg] = \
+        circhandler.cell_dispatch_table[relay.VanillaExtendedCircuitCell] = \
                 VanillaCreatedExtendedHandler(self, ntor, nexthop)
                 VanillaCreatedExtendedHandler(self, ntor, nexthop)
 
 
         # Send the cell
         # Send the cell
@@ -117,7 +117,7 @@ class CellClient(relay.CellHandler):
         circcreatemsg = relay.VanillaCreateCircuitMsg(circid, ntor_request)
         circcreatemsg = relay.VanillaCreateCircuitMsg(circid, ntor_request)
 
 
         # Set up the reply handler
         # Set up the reply handler
-        circhandler.cell_dispatch_table[relay.VanillaCreatedCircuitMsg] = \
+        circhandler.cell_dispatch_table[relay.VanillaCreatedCircuitCell] = \
                 VanillaCreatedExtendedHandler(self, ntor, self.guard)
                 VanillaCreatedExtendedHandler(self, ntor, self.guard)
 
 
         # Send the message
         # Send the message

+ 29 - 23
relay.py

@@ -38,6 +38,26 @@ class RelayRandomHopMsg(RelayNetMsg):
         return "RandomHop TTL=%d" % self.ttl
         return "RandomHop TTL=%d" % self.ttl
 
 
 
 
+class CircuitCellMsg(RelayNetMsg):
+    """Send a message tagged with a circuit id."""
+
+    def __init__(self, circuitid, cell):
+        self.circid = circuitid
+        self.cell = cell
+
+    def __str__(self):
+        return "C%d:%s" % (self.circid, self.cell)
+
+    def size(self):
+        # circuitids are 4 bytes
+        return 4 + self.cell.size()
+
+
+class RelayCell(RelayNetMsg):
+    """All cells (which are sent inside a CircuitCellMsg, and so do not
+    need their own circuitid) should be a subclass of this class."""
+
+
 class VanillaCreateCircuitMsg(RelayNetMsg):
 class VanillaCreateCircuitMsg(RelayNetMsg):
     """The message for requesting circuit creation in Vanilla Onion
     """The message for requesting circuit creation in Vanilla Onion
     Routing."""
     Routing."""
@@ -47,7 +67,7 @@ class VanillaCreateCircuitMsg(RelayNetMsg):
         self.ntor_request = ntor_request
         self.ntor_request = ntor_request
 
 
 
 
-class VanillaCreatedCircuitMsg(RelayNetMsg):
+class VanillaCreatedCircuitCell(RelayCell):
     """The message for responding to circuit creation in Vanilla Onion
     """The message for responding to circuit creation in Vanilla Onion
     Routing."""
     Routing."""
 
 
@@ -55,7 +75,7 @@ class VanillaCreatedCircuitMsg(RelayNetMsg):
         self.ntor_reply = ntor_reply
         self.ntor_reply = ntor_reply
 
 
 
 
-class VanillaExtendCircuitMsg(RelayNetMsg):
+class VanillaExtendCircuitCell(RelayCell):
     """The message for requesting circuit creation in Vanilla Onion
     """The message for requesting circuit creation in Vanilla Onion
     Routing."""
     Routing."""
 
 
@@ -64,7 +84,7 @@ class VanillaExtendCircuitMsg(RelayNetMsg):
         self.ntor_request = ntor_request
         self.ntor_request = ntor_request
 
 
 
 
-class VanillaExtendedCircuitMsg(RelayNetMsg):
+class VanillaExtendedCircuitCell(RelayCell):
     """The message for responding to circuit creation in Vanilla Onion
     """The message for responding to circuit creation in Vanilla Onion
     Routing."""
     Routing."""
 
 
@@ -72,22 +92,7 @@ class VanillaExtendedCircuitMsg(RelayNetMsg):
         self.ntor_reply = ntor_reply
         self.ntor_reply = ntor_reply
 
 
 
 
-class CircuitCellMsg(RelayNetMsg):
-    """Send a message tagged with a circuit id."""
-
-    def __init__(self, circuitid, cell):
-        self.circid = circuitid
-        self.cell = cell
-
-    def __str__(self):
-        return "C%d:%s" % (self.circid, self.cell)
-
-    def size(self):
-        # circuitids are 4 bytes
-        return 4 + self.cell.size()
-
-
-class EncryptedMsg(RelayNetMsg):
+class EncryptedCell(RelayCell):
     """Send a message encrypted with a symmetric key.  In this
     """Send a message encrypted with a symmetric key.  In this
     implementation, the encryption is not really done.  A hash of the
     implementation, the encryption is not really done.  A hash of the
     key is stored with the message so that it can be checked at
     key is stored with the message so that it can be checked at
@@ -100,7 +105,7 @@ class EncryptedMsg(RelayNetMsg):
     def decrypt(self, key):
     def decrypt(self, key):
         keyhash = nacl.hash.sha256(key)
         keyhash = nacl.hash.sha256(key)
         if keyhash != self.keyhash:
         if keyhash != self.keyhash:
-            raise ValueError("EncryptedMsg key mismatch")
+            raise ValueError("EncryptedCell key mismatch")
         return self.plaintext
         return self.plaintext
 
 
 
 
@@ -178,7 +183,7 @@ class CircuitHandler:
             self.deckey = deckey
             self.deckey = deckey
             self.next_layer = next_layer
             self.next_layer = next_layer
         def encrypt_msg(self, msg):
         def encrypt_msg(self, msg):
-            return self.next_layer.encrypt_msg(EncryptedMsg(self.enckey, msg))
+            return self.next_layer.encrypt_msg(EncryptedCell(self.enckey, msg))
         def decrypt_msg(self, msg):
         def decrypt_msg(self, msg):
             return self.next_layer.decrypt_msg(msg).decrypt(self.deckey)
             return self.next_layer.decrypt_msg(msg).decrypt(self.deckey)
 
 
@@ -211,7 +216,7 @@ class CircuitHandler:
     def received_cell(self, cell):
     def received_cell(self, cell):
         """A cell has been received on this circuit.  Dispatch it
         """A cell has been received on this circuit.  Dispatch it
         according to its type."""
         according to its type."""
-        if isinstance(cell, EncryptedMsg):
+        if isinstance(cell, EncryptedCell):
             cell = self.crypt_layer.decrypt_msg(cell)
             cell = self.crypt_layer.decrypt_msg(cell)
 
 
         print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.cellhandler.myaddr, cell, self.circid, self.channel.peer.cellhandler.myaddr))
         print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.cellhandler.myaddr, cell, self.circid, self.channel.peer.cellhandler.myaddr))
@@ -407,7 +412,8 @@ class CellRelay(CellHandler):
             deckey = nacl.hash.sha256(secret + b'upstream')
             deckey = nacl.hash.sha256(secret + b'upstream')
             circhandler.add_crypt_layer(enckey, deckey)
             circhandler.add_crypt_layer(enckey, deckey)
             # Send the ntor reply
             # Send the ntor reply
-            self.send_msg(CircuitCellMsg(msg.circid, VanillaCreatedCircuitMsg(reply)), peeraddr)
+            self.send_msg(CircuitCellMsg(msg.circid, \
+                    VanillaCreatedCircuitCell(reply)), peeraddr)
         else:
         else:
             return super().received_msg(msg, peeraddr, channel)
             return super().received_msg(msg, peeraddr, channel)