Example #1
0
 def connection_made(self, transport):
     self._transport = transport
     
     callbackAddr, callbackPort = self._callbackService.location()
     openSocketPacket = VNICSocketOpenPacket(callbackAddress=callbackAddr, callbackPort=callbackPort)
     openSocketPacket.connectData = openSocketPacket.SocketConnectData(destination=self._destination, destinationPort=self._destinationPort)
     self._transport.write(openSocketPacket.__serialize__())
Example #2
0
 def connection_made(self, transport):
     self.transport = transport
     
     callbackAddr, callbackPort = self._callbackService.location()
     openSocketPacket = VNICSocketOpenPacket(callbackAddress=callbackAddr, callbackPort=callbackPort)
     openSocketPacket.listenData = openSocketPacket.SocketListenData(sourcePort = self._listenPort)
     
     self.transport.write(openSocketPacket.__serialize__())
Example #3
0
    def listen(self, listenPort, applicationProtocolFactory):
        self._connectionId += 1
        logger.debug(
            "Requesting listenting socket on port {} from vnic (connection ID {})"
            .format(listenPort, self._connectionId))
        logger.info("Listen in {}. Has transport {}. For port {}".format(
            self, self.transport, listenPort))
        callbackAddr, callbackPort = self._callbackService.location()
        openSocketPacket = VNICSocketOpenPacket(
            ConnectionId=self._connectionId,
            callbackAddress=callbackAddr,
            callbackPort=callbackPort)
        openSocketPacket.listenData = openSocketPacket.SocketListenData(
            sourcePort=listenPort)

        self.transport.write(openSocketPacket.__serialize__())
        future = Future()
        self._connections[self._connectionId] = applicationProtocolFactory
        self._futures[self._connectionId] = ("listen", future)
        return future
Example #4
0
    def connect(self, destination, destinationPort,
                applicationProtocolFactory):
        self._connectionId += 1
        logger.debug(
            "Requesting connect to {}:{} from vnic (connection ID {})".format(
                destination, destinationPort, self._connectionId))
        callbackAddr, callbackPort = self._callbackService.location()
        openSocketPacket = VNICSocketOpenPacket(
            ConnectionId=self._connectionId,
            callbackAddress=callbackAddr,
            callbackPort=callbackPort)
        openSocketPacket.connectData = openSocketPacket.SocketConnectData(
            destination=destination, destinationPort=destinationPort)
        packetBytes = openSocketPacket.__serialize__()
        self.transport.write(packetBytes)

        future = Future()
        self._connections[self._connectionId] = applicationProtocolFactory
        self._futures[self._connectionId] = ("connect", future)
        return future
Example #5
0
def basicUnitTest():
    from playground.network.testing import MockTransportToStorageStream as MockTransport
    from playground.asyncio_lib.testing import TestLoopEx
    from playground.network.protocols.packets.vsocket_packets import    VNICSocketOpenPacket,           \
                                                                        VNICSocketOpenResponsePacket,   \
                                                                        VNICStartDumpPacket,            \
                                                                        PacketType
    from playground.network.protocols.packets.switching_packets import WirePacket
    import io, asyncio

    vnic1 = VNIC("1.1.1.1")
    assert vnic1.address() == "1.1.1.1"

    linkTx = vnic1.switchConnectionFactory()
    linkTransport = MockTransport(io.BytesIO())
    linkTx.connection_made(linkTransport)

    control = vnic1.controlConnectionFactory()
    controlTransport = MockTransport(io.BytesIO())
    control.connection_made(controlTransport)

    class TransportFactory:
        def __init__(self):
            self.transports = {}
            self.txPort = 5000

        def __call__(self, protocol, addr, port):
            t = MockTransport(io.BytesIO(),
                              extra={
                                  'sockname': ('192.168.0.1', self.txPort),
                                  'peername': (addr, port)
                              })
            t.protocol = protocol
            self.transports[(addr, port)] = t
            self.txPort += 1
            return t

    transportFactory = TransportFactory()

    loop = TestLoopEx()
    loop.setTransportFactory(transportFactory)
    asyncio.set_event_loop(loop)

    openPacket = VNICSocketOpenPacket(callbackAddress="192.168.0.2",
                                      callbackPort=9091)
    openPacket.connectData = openPacket.SocketConnectData(
        destination="2.2.2.2", destinationPort=100)
    control.data_received(openPacket.__serialize__())

    deserializer = PacketType.Deserializer()
    deserializer.update(controlTransport.sink.getvalue())
    responsePackets = list(deserializer.nextPackets())
    assert len(responsePackets) == 1
    assert isinstance(responsePackets[0], VNICSocketOpenResponsePacket)
    assert not responsePackets[0].isFailure()

    # Two advances might be necessary to create the reverse connection AND complete it.
    loop.advanceClock(1)
    loop.advanceClock(1)

    assert ("192.168.0.2", 9091) in transportFactory.transports
    socketTransport = transportFactory.transports[("192.168.0.2", 9091)]

    txPacket1 = WirePacket(source="2.2.2.2",
                           sourcePort=100,
                           destination="1.1.1.1",
                           destinationPort=responsePackets[0].port,
                           data=b"This is a test message")
    linkTx.data_received(txPacket1.__serialize__())

    assert socketTransport.sink.getvalue() == txPacket1.data

    listenPacket = VNICSocketOpenPacket(callbackAddress="192.168.0.2",
                                        callbackPort=9092)
    listenPacket.listenData = listenPacket.SocketListenData(sourcePort=666)

    control2 = vnic1.controlConnectionFactory()
    control2Transport = MockTransport(io.BytesIO())
    control2.connection_made(control2Transport)
    control2.data_received(listenPacket.__serialize__())

    deserializer = PacketType.Deserializer()
    deserializer.update(control2Transport.sink.getvalue())
    responsePackets = list(deserializer.nextPackets())
    assert len(responsePackets) == 1
    assert isinstance(responsePackets[0], VNICSocketOpenResponsePacket)
    assert not responsePackets[0].isFailure()

    txPacket2 = WirePacket(source="2.2.2.2",
                           sourcePort=100,
                           destination="1.1.1.1",
                           destinationPort=666,
                           data=b"This is a test message x2")
    linkTx.data_received(txPacket2.__serialize__())

    loop.advanceClock(1)
    loop.advanceClock(1)

    assert ("192.168.0.2", 9092) in transportFactory.transports

    socket2Transport = transportFactory.transports[("192.168.0.2", 9092)]
    assert socket2Transport.sink.getvalue() == txPacket2.data

    txPacket3 = WirePacket(source="1.1.1.1",
                           sourcePort=666,
                           destination="2.2.2.2",
                           destinationPort=100,
                           data=b"response1")
    socket2Transport.protocol.data_received(txPacket3.__serialize__())

    vnic1.setPromiscuousLevel(2)
    # TODO: Asert new announce packet set
    dumper = vnic1.controlConnectionFactory()
    dumperTransport = MockTransport(io.BytesIO())
    dumper.connection_made(dumperTransport)
    dumper.data_received(VNICStartDumpPacket().__serialize__())
    txPacket4 = WirePacket(source="2.2.2.2",
                           sourcePort=666,
                           destination="2.2.1.5",
                           destinationPort=300,
                           data=b"No Address")
    linkTx.data_received(txPacket4.__serialize__())

    deserializer = FramedPacketType.Deserializer()
    deserializer.update(dumperTransport.sink.getvalue())

    dumpPackets = list(deserializer.nextPackets())
    assert len(dumpPackets) == 1
    assert dumpPackets[0].data == txPacket4.data

    # set "myProtocol" so that closing the transport closes
    # the protocol (connection_lost). Check that no further
    # messages are sent to the protocol
    dumperTransport.setMyProtocol(dumper)
    dumperTransport.close()
    dumperTransport.sink.truncate(0)

    txPacket5 = WirePacket(source="2.2.2.2",
                           sourcePort=666,
                           destination="2.2.1.5",
                           destinationPort=300,
                           data=b"No Address")
    linkTx.data_received(txPacket5.__serialize__())

    deserializer.update(dumperTransport.sink.getvalue())

    dumpPackets = list(deserializer.nextPackets())
    assert len(dumpPackets) == 0
Example #6
0
 def __init__(self, vnic):
     self._vnic = vnic
     self._state = self.MODE_OPENING 
     self._deserializer = VNICSocketOpenPacket.Deserializer()
     self._control = None
     self.transport = None