소스 검색

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 년 전
부모
커밋
a8361be7e3
2개의 변경된 파일39개의 추가작업 그리고 33개의 파일을 삭제
  1. 10 10
      client.py
  2. 29 23
      relay.py

+ 10 - 10
client.py

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

+ 29 - 23
relay.py

@@ -38,6 +38,26 @@ class RelayRandomHopMsg(RelayNetMsg):
         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):
     """The message for requesting circuit creation in Vanilla Onion
     Routing."""
@@ -47,7 +67,7 @@ class VanillaCreateCircuitMsg(RelayNetMsg):
         self.ntor_request = ntor_request
 
 
-class VanillaCreatedCircuitMsg(RelayNetMsg):
+class VanillaCreatedCircuitCell(RelayCell):
     """The message for responding to circuit creation in Vanilla Onion
     Routing."""
 
@@ -55,7 +75,7 @@ class VanillaCreatedCircuitMsg(RelayNetMsg):
         self.ntor_reply = ntor_reply
 
 
-class VanillaExtendCircuitMsg(RelayNetMsg):
+class VanillaExtendCircuitCell(RelayCell):
     """The message for requesting circuit creation in Vanilla Onion
     Routing."""
 
@@ -64,7 +84,7 @@ class VanillaExtendCircuitMsg(RelayNetMsg):
         self.ntor_request = ntor_request
 
 
-class VanillaExtendedCircuitMsg(RelayNetMsg):
+class VanillaExtendedCircuitCell(RelayCell):
     """The message for responding to circuit creation in Vanilla Onion
     Routing."""
 
@@ -72,22 +92,7 @@ class VanillaExtendedCircuitMsg(RelayNetMsg):
         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
     implementation, the encryption is not really done.  A hash of the
     key is stored with the message so that it can be checked at
@@ -100,7 +105,7 @@ class EncryptedMsg(RelayNetMsg):
     def decrypt(self, key):
         keyhash = nacl.hash.sha256(key)
         if keyhash != self.keyhash:
-            raise ValueError("EncryptedMsg key mismatch")
+            raise ValueError("EncryptedCell key mismatch")
         return self.plaintext
 
 
@@ -178,7 +183,7 @@ class CircuitHandler:
             self.deckey = deckey
             self.next_layer = next_layer
         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):
             return self.next_layer.decrypt_msg(msg).decrypt(self.deckey)
 
@@ -211,7 +216,7 @@ class CircuitHandler:
     def received_cell(self, cell):
         """A cell has been received on this circuit.  Dispatch it
         according to its type."""
-        if isinstance(cell, EncryptedMsg):
+        if isinstance(cell, EncryptedCell):
             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))
@@ -407,7 +412,8 @@ class CellRelay(CellHandler):
             deckey = nacl.hash.sha256(secret + b'upstream')
             circhandler.add_crypt_layer(enckey, deckey)
             # Send the ntor reply
-            self.send_msg(CircuitCellMsg(msg.circid, VanillaCreatedCircuitMsg(reply)), peeraddr)
+            self.send_msg(CircuitCellMsg(msg.circid, \
+                    VanillaCreatedCircuitCell(reply)), peeraddr)
         else:
             return super().received_msg(msg, peeraddr, channel)