Browse Source

wip; there is an error creating circuits after a number have already been created

Chelsea H. Komlo 4 years ago
parent
commit
70890f8cd3
2 changed files with 43 additions and 9 deletions
  1. 11 2
      client.py
  2. 32 7
      relay.py

+ 11 - 2
client.py

@@ -81,6 +81,8 @@ class TelescopingCreatedHandler:
         deckey = nacl.hash.sha256(secret + b'downstream')
         circhandler.add_crypt_layer(enckey, deckey)
 
+        circhandler.replace_celltype_handler(relay.TelescopingCreatedCircuitCell, None)
+
         nexthopidx = None
         while nexthopidx is None:
             nexthopidx = self.channelmgr.relaypicker.pick_weighted_relay_index()
@@ -111,8 +113,8 @@ class TelescopingExtendedHandler:
     def received_cell(self, circhandler, cell):
         print("LOG: Received cell in TelescopingExtendedHandler")
 
-        #TODO verify the SNIP that was received
-        # Bail if it is invalid
+        #TODO validate the SNIP
+        print("WARNING: Unimplemented! Need to validate the SNIP before proceeding.")
 
         onionkey = cell.snip.snipdict['onionkey']
         idkey = cell.snip.snipdict['idkey']
@@ -122,10 +124,15 @@ class TelescopingExtendedHandler:
         deckey = nacl.hash.sha256(secret + b'downstream')
         circhandler.add_crypt_layer(enckey, deckey)
 
+        circhandler.replace_celltype_handler(
+                    relay.TelescopingExtendedCircuitCell, None)
+        circhandler.circuit_descs.append(cell.snip)
+
 
         # Are we done building the circuit?
         print("WARNING: we may need another circhandler structure for snips")
         if len(circhandler.circuit_descs) == 3:
+            print("Log: Circuit is long enough; exiting.")
             # Yes!
             return
 
@@ -273,6 +280,8 @@ class ClientChannelManager(relay.ChannelManager):
         # Send the message
         guardchannel.send_msg(circcreatemsg)
 
+        print("Log: Successfully creating a telescoping circuit")
+
         return circhandler
 
     def new_circuit(self):

+ 32 - 7
relay.py

@@ -275,6 +275,7 @@ class VanillaExtendCircuitHandler:
 
         # Set up a handler for when the VanillaCreatedCircuitCell comes
         # back
+        #TODO shouldn't these be extended?
         newcirchandler.replace_celltype_handler(
                 VanillaCreatedCircuitCell,
                 VanillaCreatedRelayHandler())
@@ -295,6 +296,7 @@ class TelescopingExtendCircuitHandler:
     def received_cell(self, circhandler, cell):
         # Remove ourselves from handling a second
         # TelescopingExtendCircuitCell on this circuit
+        print("Log: Received TelescopingExtendCircuitHandler cell")
         circhandler.replace_celltype_handler(TelescopingExtendCircuitCell, None)
 
         # Find the SNIP corresponding to the  index sent by the client
@@ -303,15 +305,22 @@ class TelescopingExtendCircuitHandler:
         # Allocate a new circuit id to the requested next hop
         channelmgr = circhandler.channel.channelmgr
         nexthopchannel = channelmgr.get_channel_to(next_snip.snipdict["addr"])
+        newcircid, newcirchandler = nexthopchannel.new_circuit()
 
-        #newcircid, newcirchandler = nexthopchannel.new_circuit()
-
-        sys.exit("WARNING: Unimplemented! TelescopingExtendCircuitHandler")
-
-
+        # Connect the existing and new circuits together
+        circhandler.adjacent_circuit_handler = newcirchandler
+        newcirchandler.adjacent_circuit_handler = circhandler
 
 
+        # Set up a handler for when the TelescopingCreatedCircuitCell comes
+        # back
+        newcirchandler.replace_celltype_handler(
+                TelescopingCreatedCircuitCell,
+                TelescopingCreatedRelayHandler(next_snip))
 
+        # Forward a TelescopingCreateCircuitMsg to the next hop
+        nexthopchannel.send_msg(
+                TelescopingCreateCircuitMsg(newcircid, cell.ntor_request))
 
 class VanillaCreatedRelayHandler:
     """Handle a VanillaCreatedCircuitCell received by a _relay_ that
@@ -328,6 +337,24 @@ class VanillaCreatedRelayHandler:
         circhandler.adjacent_circuit_handler.send_cell(
             VanillaExtendedCircuitCell(cell.ntor_reply))
 
+class TelescopingCreatedRelayHandler:
+    """Handle a TelescopingCreatedCircuitCell received by a _relay_ that
+    recently received a TelescopingExtendCircuitCell from a client, and so
+    forwarded a TelescopingCreateCircuitCell to the next hop."""
+
+    def __init__(self, next_snip):
+        self.next_snip = next_snip
+
+    def received_cell(self, circhandler, cell):
+        print("LOG: Handle a TelescopingCreatedCircui received by a relay")
+        # Remove ourselves from handling a second
+        # VanillaCreatedCircuitCell on this circuit
+        circhandler.replace_celltype_handler(TelescopingCreatedCircuitCell, None)
+
+        # Just forward a TelescopingExtendedCircuitCell back towards the
+        # client
+        circhandler.adjacent_circuit_handler.send_cell(
+            TelescopingExtendedCircuitCell(cell.ntor_reply, self.next_snip))
 
 class CircuitHandler:
     """A class for managing sending and receiving encrypted cells on a
@@ -692,8 +719,6 @@ class RelayChannelManager(ChannelManager):
             # Send the ntor reply
             self.send_msg(CircuitCellMsg(msg.circid,
                     TelescopingCreatedCircuitCell(reply)), peeraddr)
-        elif isinstance(msg, TelescopingExtendCircuitMsg):
-            sys.exit("ERR: TelescopingExtendCircuitMsg is not yet implemented ")
         else:
             return super().received_msg(msg, peeraddr, channel)