]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
added a simple tcp over airhook proxy, not fully tested
authorburris <burris>
Sun, 11 Jul 2004 08:57:54 +0000 (08:57 +0000)
committerburris <burris>
Sun, 11 Jul 2004 08:57:54 +0000 (08:57 +0000)
cleaned up airhook a bit

airhook.py
airproxy.py [new file with mode: 0644]
test_airhook.py

index 17b046e7e1d3d710ec0113a3ce153c7f79419c45..397cf53e41c4eb1a8c2b4d04b14ae5e7dd5de133 100644 (file)
@@ -30,7 +30,7 @@ confirmed = 2
 
 
 
-class Airhook(protocol.DatagramProtocol):       
+class Airhook:       
     def __init__(self):
         self.noisy = None
         # this should be changed to storage that drops old entries
@@ -46,19 +46,24 @@ class Airhook(protocol.DatagramProtocol):
             raise Exception
         if not self.connections.has_key(addr):
             conn = self.connection()
+            conn.addr = addr
+            conn.makeConnection(self.transport)
             conn.protocol = self.factory.buildProtocol(addr)
             conn.protocol.makeConnection(conn)
-            conn.makeConnection(self.transport)
-            conn.addr = addr
             self.connections[addr] = conn
         else:
             conn = self.connections[addr]
         return conn
     def makeConnection(self, transport):
-        protocol.DatagramProtocol.makeConnection(self, transport)
-        tup = transport.getHost()
-        self.addr = (tup[1], tup[2])
-        
+        self.transport = transport
+        addr = transport.getHost()
+        self.addr = (addr.host, addr.port)
+
+    def doStart(self):
+        pass
+    def doStop(self):
+        pass
+    
 class AirhookPacket:
     def __init__(self, msg):
         self.datagram = msg
@@ -93,7 +98,7 @@ class AirhookPacket:
                 self.msgs.append( msg[skip:skip+n])
                 skip += n
 
-class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConnectedTransport):
+class AirhookConnection:
     def __init__(self):        
         self.outSeq = 0  # highest sequence we have sent, can't be 255 more than obSeq
         self.observed = None  # their session id
@@ -108,7 +113,10 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne
         self.response = 0 # if we know we have a response now (like resending missed packets)
         self.noisy = 0
         self.resetConnection()
-    
+
+    def makeConnection(self, transport):
+        self.transport = transport
+        
     def resetConnection(self):
         self.weMissed = []
         self.outMsgs = [None] * 256  # outgoing messages  (seq sent, message), index = message number
@@ -298,16 +306,20 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne
     def timeToSend(self):
         if self.state == pending:
             return (1, 0)
+
+        outstanding = (256 + (((self.next - 1) % 256) - self.outMsgNums[self.obSeq % 256])) % 256
         # any outstanding messages and are we not too far ahead of our counterparty?
-        elif len(self.omsgq) > 0 and self.state != sent and (self.next + 1) % 256 != self.outMsgNums[self.obSeq % 256] and (self.outSeq - self.obSeq) % 2**16 < 256:
+        if len(self.omsgq) > 0 and self.state != sent and (self.next + 1) % 256 != self.outMsgNums[self.obSeq % 256] and (self.outSeq - self.obSeq) % 2**16 < 256:
+        #if len(self.omsgq) > 0 and self.state != sent and (self.next + 1) % 256 != self.outMsgNums[self.obSeq % 256] and not outstanding:
             return (1, 0)
+
         # do we explicitly need to send a response?
-        elif self.response:
+        if self.response:
             self.response = 0
             return (1, 0)
         # have we not sent anything in a while?
-        elif time() - self.lastTransmit > 1.0:
-            return (1, 1)
+        #elif time() - self.lastTransmit > 1.0:
+        #return (1, 1)
             
         # nothing to send
         return (0, 0)
@@ -398,7 +410,6 @@ class StreamConnection(AirhookConnection):
             self.omsgq.insert(0, p)
             data = data[253:]
             self.oseq = (self.oseq + 1) % 2**16
-
         self.schedule()
         
     def writeSequence(self, sequence):
diff --git a/airproxy.py b/airproxy.py
new file mode 100644 (file)
index 0000000..209d510
--- /dev/null
@@ -0,0 +1,92 @@
+from airhook import listenAirhookStream, StreamConnection
+
+from twisted.internet import protocol, reactor
+import sys
+from random import randrange
+
+#### airhook -> tcp
+class UDPListener(protocol.Protocol):
+    started = 0
+    def makeConnection(self, connection):
+        self.conn = connection
+    def dataReceived(self, data):
+        if not self.started:
+            if data == '\03BAP':
+                self.started = 1
+        else:
+            self.out.transport.write(data)
+        
+class TCPReceiver(protocol.Protocol):
+    def dataReceived(self, data):
+        self.out.conn.write(data)
+
+class TCPOutFactory(protocol.ClientFactory):
+    protocol = TCPReceiver
+    def __init__(self, out):
+        self.out = out
+    def buildProtocol(self, addr):
+        p = protocol.ClientFactory.buildProtocol(self, addr)
+        p.out = self.out
+        p.out.out = p
+        return p
+
+class AirUDPProxyFactory(protocol.ServerFactory):
+    oport = 0
+    protocol = UDPListener
+    def __init__(self, out):
+        self.out = out
+    def buildProtocol(self, addr):
+        p = protocol.ServerFactory.buildProtocol(self, addr)
+        reactor.connectTCP('localhost', self.out, TCPOutFactory(p))
+        return p
+
+def remote(udp, tcp):
+    f = AirUDPProxyFactory(tcp)
+    ah = listenAirhookStream(udp, f)
+
+
+######  tcp -> airhook
+class UDPReceiver(protocol.Protocol):
+    def __init__(self, tcp):
+        self.tcp = tcp
+    def dataReceived(self, data):
+        self.tcp.transport.write(data)
+    def makeConnection(self, conn):
+        self.tcp.out = conn
+        conn.write("\03BAP")
+
+class TCPListener(protocol.Protocol):
+    def dataReceived(self, data):
+        self.out.write(data)
+
+class UDPOutFactory(protocol.ClientFactory):
+    protocol = UDPReceiver
+    def __init__(self, out):
+        self.out = out
+    def buildProtocol(self, addr):
+        p = UDPReceiver(self.out)
+        return p
+        
+class AirTCPProxyFactory(protocol.ServerFactory):
+    oaddr = ('',0)
+    protocol = TCPListener
+    def __init__(self, oaddr):
+        self.oaddr = oaddr
+    def buildProtocol(self, addr):
+        p = TCPListener()
+        ah = listenAirhookStream(randrange(10000,12000), UDPOutFactory(p))
+        reactor.iterate()
+        c = ah.connectionForAddr(self.oaddr)
+        #c.noisy= 1 
+        return p
+        
+def local(tcp, udp):
+    f = AirTCPProxyFactory(('64.81.64.214', udp))
+    reactor.listenTCP(tcp, f)
+    
+if __name__ == '__main__':
+    if sys.argv[1] == '-l':
+        local(int(sys.argv[2]), int(sys.argv[3]))
+    else:
+        remote(int(sys.argv[2]), int(sys.argv[3]))
+    reactor.run()
index 0812f3775282267a210216e6564777d6860a1802..8ad6382ceaac76c1701e666879c444ed4c4a2096 100644 (file)
@@ -595,7 +595,7 @@ class BasicTests(unittest.TestCase):
     def testRecipientReset(self):
         self.testTwoWayBlast()
         self.b.protocol.q = []
-        self.noisy = 1
+        self.noisy = 0
         a = self.a
         b = self.b
         msg = swap(a, noisy=self.noisy)