def setUp(self): self.echoServerFactory = EchoServer() self.serverAddress = PlaygroundAddress(1,2,3,4) self.clientAddress = PlaygroundAddress(4,3,2,1) self.serverProtocol = EchoServerProtocol(self.echoServerFactory, self.serverAddress) self.serverTransport = DummyTransportToStorage(self.serverAddress, self.clientAddress) self.clientTransport = DummyTransportToProtocol(self.clientAddress, self.serverAddress, self.serverProtocol) self.serverProtocol.makeConnection(self.serverTransport)
def getVnicPlaygroundAddress(self, vnicName): if not vnicName: return None device = self.deviceManager.getDevice(vnicName) if device: address = device.address() return PlaygroundAddress.FromString(address) return None
async def create_playground_connection(self, protocolFactory, destination, destinationPort, vnicName="default", cbPort=0, timeout=60): if not self._ready: await self.create_callback_service() if not isinstance(destination, PlaygroundAddress): destination = PlaygroundAddress.FromString(destination) if vnicName == "default": vnicName = self._vnicService.getVnicByDestination(destination, destinationPort) if not vnicName: raise Exception("Could not find a valid vnic four outbound connection {}:{}".format(destination, destinationPort)) location = self._vnicService.getVnicTcpLocation(vnicName) if not location: raise Exception("Playground network not ready. Could not find interface to connect to {}:{}".format(destination, destinationPort)) vnicAddr, vnicPort = location connectProtocol = VNICConnectProtocol(destination, destinationPort, self._callbackService, protocolFactory) coro = asyncio.get_event_loop().create_connection(lambda: connectProtocol, vnicAddr, vnicPort) transport, protocol = await coro # now we have to wait for protocol to make it's callback. coro = self._callbackService.waitForConnections(protocol) connections = await asyncio.wait_for(coro, timeout) if len(connections) != 1: raise Exception("VNIC Open Failed (Unexpected Error, connections={})!".format(len(connections))) playgroundProtocol = connections[0] while isinstance(playgroundProtocol, StackingProtocol) and playgroundProtocol.higherProtocol(): playgroundProtocol = playgroundProtocol.higherProtocol() connectionMadeCoro = connectionMadeObserver.awaitConnection(playgroundProtocol) await asyncio.wait_for(connectionMadeCoro, timeout) return playgroundProtocol.transport, playgroundProtocol
def startTest(self, testControl, serverAddr, serverPort, stack=None): #logctx = playgroundlog.LoggingContext() #logctx.nodeId = myAddress.toString() #logctx.doPacketTracing = True #playgroundlog.startLogging(logctx) throughputFactory = TestThroughputFactory(testControl) if serverAddr == None: print "Starting server on port", serverPort serverEP = GateServerEndpoint(reactor, serverPort, self.gateAddr, self.gatePort, networkStack=stack) serverEP.listen(throughputFactory) #self.client.listen(throughputFactory, serverPort, connectionType) else: serverAddr = PlaygroundAddress.FromString(serverAddr) print "connecting client to", serverAddr, serverPort clientEP = GateClientEndpoint(reactor, serverAddr, serverPort, self.gateAddr, self.gatePort, networkStack=stack) testProtocol = throughputFactory.buildProtocol(None) connectProtocol(clientEP, testProtocol) reactor.run()
def simpleMain(args): myAddress = PlaygroundAddress.FromString(args[3]) client = ClientBase(myAddress) serverAddress, serverPortString = args[1:3] serverPort = int(serverPortString) hijacker = Hijacker(client) client.listen(hijacker, 100) client.connectToChaperone(serverAddress, serverPort)
def __call__(self, protocol, msg): playgroundAddressString = msg.address success = True try: """ Make sure the address is valid (can be converted back to an PlaygroundAddress) """ PlaygroundAddress.FromString(playgroundAddressString) except PlaygroundNetworkError, e: success = False
async def create_playground_connection(self, protocolFactory, destination, destinationPort, vnicName="default", cbPort=0, timeout=60): if not self._ready: await self.create_callback_service() if not isinstance(destination, PlaygroundAddress): destination = PlaygroundAddress.FromString(destination) if vnicName == "default": vnicName = self._vnicService.getVnicByDestination( destination, destinationPort) if not vnicName: raise Exception( "Could not find a valid vnic four outbound connection {}:{}". format(destination, destinationPort)) location = self._vnicService.getVnicTcpLocation(vnicName) if not location: raise Exception( "Playground network not ready. Could not find interface to connect to {}:{}" .format(destination, destinationPort)) vnicAddr, vnicPort = location connectProtocol = VNICConnectProtocol(destination, destinationPort, self._callbackService, protocolFactory) coro = asyncio.get_event_loop().create_connection( lambda: connectProtocol, vnicAddr, vnicPort) transport, protocol = await coro # now we have to wait for protocol to make it's callback. coro = self._callbackService.waitForConnections(protocol, timeout=timeout) connections = await coro if len(connections) != 1: raise Exception( "VNIC Open Failed (Unexpected Error, connections={})!".format( len(connections))) playgroundProtocol = connections[0] while isinstance( playgroundProtocol, StackingProtocol) and playgroundProtocol.higherProtocol(): playgroundProtocol = playgroundProtocol.higherProtocol() # this is the application protocol! # unlike twisted, asyncio does not require transport to be set at all. # consequently, there's no easy way to know if its connected while not playgroundProtocol.transport: await asyncio.sleep(.1) timeout = timeout - .1 if not playgroundProtocol.transport: raise Exception("Timeout before application connection made") return playgroundProtocol.transport, playgroundProtocol
def __init__(self, playgroundAddress): self._address = PlaygroundAddress.FromString(playgroundAddress) logger.info("{} just started up".format(self)) # ports and connections are interrelated but slightly different # a port is just an integer key mapped to a control object. # a connection is tied to the port. self._ports = {} self._connections = {} self._dumps = set([]) self._freePorts = self._freePortsGenerator() self._linkTx = None #PlaygroundSwitchTxProtocol(self, self.address()) self._connectedToNetwork = False self._promiscuousMode = None
async def create_playground_connection(self, protocolFactory, destination, destinationPort, vnicName="default", cbPort=0, timeout=60): startTime = time.time() logger.info("Create playground connection to {}:{}".format( destination, destinationPort)) if not self._ready: await self.create_callback_service( lambda: VNICCallbackProtocol(self._callbackService) ) #.buildConnectDataProtocol) if destination == "localhost": destination = self._vnicService.getVnicPlaygroundAddress( self._vnicService.getDefaultVnic()) if not isinstance(destination, PlaygroundAddress): destination = PlaygroundAddress.FromString(destination) if vnicName == "default": vnicName = self._vnicService.getVnicByDestination( destination, destinationPort) if not vnicName: raise Exception( "Could not find a valid vnic four outbound connection {}:{}". format(destination, destinationPort)) location = self._vnicService.getVnicTcpLocation(vnicName) if not location: raise Exception( "Playground network not ready. Could not find interface to connect to {}:{}" .format(destination, destinationPort)) vnicAddr, vnicPort = location if not location in self._vnicConnections: self._vnicConnections[location] = "CONNECTING" logger.info( "No control conenction to VNIC {} yet. Connecting".format( location)) controlProtocol = VNICSocketControlClientProtocol( self._callbackService) coro = asyncio.get_event_loop().create_connection( lambda: controlProtocol, vnicAddr, vnicPort) res = await coro logger.info("Control protocol connected. {}".format(res)) self._vnicConnections[location] = controlProtocol while self._vnicConnections[location] == "CONNECTING": # another co-routing is rying to make the connection logger.debug("Waiting to connect for {}".format(location)) await asyncio.sleep(0.5) logger.debug("Ready to proceed for {}".format(location)) controlTime = time.time() controlProtocol = self._vnicConnections[location] future = controlProtocol.connect(destination, destinationPort, protocolFactory) logger.debug("Awaiting outbound connection to complete") try: connectionId, port = await asyncio.wait_for(future, timeout) logger.debug( "Connection complete. Outbound port is {} for connection {}". format(port, connectionId)) except TimeoutError: raise Exception("Could not connect to {}:{} in {} seconds.".format( destination, destinationPort, timeout)) callbackTime = time.time() logger.debug( "Complete playground connection. Total Time: {} (Control {}, callback {})" .format(callbackTime - startTime, controlTime - startTime, callbackTime - controlTime)) connections = await self._callbackService.waitForConnections( connectionId, n=1) if len(connections) != 1: raise Exception( "VNIC Unexpected Error connecting to {}:{} (ID {}). Should be one connection, but got {})!" .format(destination, destinationPort, connectionId, len(connections))) dataProtocol = connections[0] playgroundProtocol = dataProtocol while isinstance( playgroundProtocol, StackingProtocol) and playgroundProtocol.higherProtocol(): playgroundProtocol = playgroundProtocol.higherProtocol() connectionMadeCoro = connectionMadeObserver.awaitConnection( playgroundProtocol) try: await asyncio.wait_for(connectionMadeCoro, timeout) except TimeoutError as te: logger.debug( "Playground protocol could not connect in {} seconds.".format( timeout)) try: dataProtocol.transport.close() except: pass raise te return playgroundProtocol.transport, playgroundProtocol
def __init__(self, chaperoneAddr, chaperonePort, gatePort, gateAddr): self.chaperoneAddr = chaperoneAddr self.chaperonePort = int(chaperonePort) self.playgroundAddr = PlaygroundAddress.FromString(gateAddr) self.gatePort = int(gatePort)