untested python adapter for gast program
authorcharlie <charlie.gerhardus@student.ru.nl>
Sun, 20 Dec 2015 15:40:36 +0000 (16:40 +0100)
committercharlie <charlie.gerhardus@student.ru.nl>
Sun, 20 Dec 2015 15:40:36 +0000 (16:40 +0100)
a3/code/adapter/listener.py [new file with mode: 0644]
a3/code/adapter/sender.py [new file with mode: 0644]

diff --git a/a3/code/adapter/listener.py b/a3/code/adapter/listener.py
new file mode 100644 (file)
index 0000000..1cb7981
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/python
+from sender import Sender
+import sys
+import socket
+
+GASTPORT = 1203
+
+if __name__ == "__main__":
+    serverPort = 10000
+    if len(sys.argv) > 1:
+        serverPort = int(sys.argv[1])
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    s.bind(('', GASTPORT))
+    s.listen(1)
+    print 'Listening on port {} for Gast'.format(GASTPORT)
+    conn, addr = s.accept()
+    print 'Connected by Gast at {}'.format(addr)
+    sender = None
+    seqnr = None
+    response = ''
+    data = ''
+    while True:
+        ch = conn.recv(1)
+        if not ch:
+            break
+        
+        if ch != ' ':
+            data = data+ch
+            continue
+        
+        data = conn.recv(int(data))
+        print "INPUT: "+data
+        
+        data = ''
+        continue
+         
+        if not data:
+            break
+        data = data.strip()
+        if data == 'Listen?':
+            sender = Sender(serverIP="127.0.0.1", networkInterface="lo", isLocal=True, serverPort=serverPort, waitTime=1, isVerbose=0)
+            response = ''
+        if data == 'SYN?':
+            seqnr = 100
+            print 'S: {}'.format(seqnr)
+            response = sender.sendInput('S', seqnr, seqnr) 
+        if data == 'ACK?':
+            seqnr += 1
+            print 'A: {}'.format(seqnr)
+            response = sender.sendInput('A', seqnr, sender.lastSeqReceived + 1) 
+        if data == 'FINACK?':
+            seqnr += 1
+            print 'FA: {}'.format(seqnr)
+            response = sender.sendInput("FA", seqnr, sender.lastSeqReceived + 1)
+        if data == 'FIN?':
+            seqnr += 1
+            print 'F: {}'.format(seqnr)
+            response = sender.sendInput("F", seqnr, sender.lastSeqReceived + 1)
+        if data == 'RST?':
+            seqnr += 1
+            print 'RP: {}'.format(seqnr)
+            response = sender.sendInput("RP", seqnr, 0)
+            sender.sendReset()
+        if response:
+            if response == 'Timeout':
+                print 'data: {} - TIMEOUT!'.format(data)
+                conn.send('TIMEOUT!\n')
+            elif response[0] == 'A':
+                print 'data: {} - ACK!'.format(data)
+                conn.send('ACK!\n')
+            elif response[0] == 'R':
+                print 'data: {} - RST!'.format(data)
+                conn.send('RST!\n')
+            elif response[0] == 'SA':
+                print 'data: {} - SYNACK!'.format(data)
+                conn.send('SYNACK!\n')
+        else:
+            print 'data: {}'.format(data)
+    print 'closed'
+    conn.close()
+    exit()
diff --git a/a3/code/adapter/sender.py b/a3/code/adapter/sender.py
new file mode 100644 (file)
index 0000000..f80bd4b
--- /dev/null
@@ -0,0 +1,150 @@
+from scapy.all import *\r
+\r
+verbose = 0\r
+\r
+def vb_print(msg):\r
+    global verbose\r
+    if verbose == 1:\r
+        print msg\r
+\r
+# the sender sends packets with configurable parameters to a server and retrieves responses\r
+class Sender:\r
+    # information on the SUT\r
+    def __init__(self, serverIP, serverPort=8000,\r
+                 networkInterface="lo", isLocal=True, senderPortMinimum=20000,\r
+                 senderPortMaximum=40000, portNumberFile="sn.txt",\r
+                 isVerbose=0, waitTime=1):\r
+        \r
+        \r
+        # file where the last sender port used is stored\r
+        self.portNumberFile = portNumberFile;\r
+        \r
+        # when choosing a fresh port, a new port is chosen\r
+        # within boundaries defined by the parameters below\r
+        self.senderPortMinimum = senderPortMinimum\r
+        self.senderPortMaximum = senderPortMaximum\r
+        \r
+        # data on sender and server needed to send packets \r
+        self.serverIP = serverIP\r
+        self.serverPort = serverPort\r
+        self.networkInterface = networkInterface\r
+        self.senderPort = self.getNextPort()\r
+        self.isLocal = isLocal\r
+        \r
+        # time to wait for a response from the server before concluding a timeout\r
+        self.waitTime = waitTime\r
+        \r
+        # verbose or not\r
+        self.isVerbose = isVerbose\r
+        \r
+        # variables added so you can easily test the last system response\r
+        self.lastSeqReceived = None\r
+        self.lastAckReceived = None\r
+        self.isTimeout = None\r
+        self.lastFlagsReceived = None\r
+        self.lastDataReceived = None\r
+        \r
+\r
+    # chooses a new port to send packets from\r
+    def refreshNetworkPort(self):\r
+        vb_print("previous local port: " + str(self.senderPort))\r
+        self.senderPort = self.getNextPort()\r
+        vb_print("next local port: " + str(self.senderPort) + "\n")\r
+        return self.senderPort\r
+\r
+    # gets a new port number, an increment of the old within the specified limits. Uses a file.\r
+    def getNextPort(self):\r
+        f = open(self.portNumberFile, "a+")\r
+        f.seek(0)\r
+        line = f.readline()\r
+        if line == '' or int(line) < self.senderPortMinimum:\r
+            networkPort = self.senderPortMinimum\r
+        else:\r
+            networkPort = (int(line) + 1) % self.senderPortMaximum\r
+        f.closed\r
+        f = open(self.portNumberFile, "w")\r
+        f.write(str(networkPort))\r
+        f.closed\r
+        return networkPort\r
+\r
+    # send a packet onto the network with the given parameters, and return the response packet\r
+    # in case of a timeout, returns None, otherwise, returns the tuple (flags, seqNo, ackNo)\r
+    def sendPacket(self, flagsSet, seqNr, ackNr, data = None):\r
+        packet = self.createPacket(flagsSet, seqNr, ackNr, data)\r
+        # consider adding the parameter: iface="ethx" if you don't receive a response. Also consider increasing the wait time\r
+        scapyResponse = sr1(packet, timeout=self.waitTime, verbose=self.isVerbose)\r
+        if scapyResponse is not None:\r
+   #         scapyResponse.show() \r
+            # ^^ in case you want to show the packet content \r
+            # here is what you store from every packet response\r
+            if Raw not in scapyResponse: \r
+                response = (self.intToFlags(scapyResponse[TCP].flags), scapyResponse[TCP].seq, scapyResponse[TCP].ack, None)\r
+            else: \r
+                response = (self.intToFlags(scapyResponse[TCP].flags), scapyResponse[TCP].seq, scapyResponse[TCP].ack, scapyResponse[Raw].load)\r
+        else:\r
+            response = "Timeout"\r
+        return response\r
+    \r
+    # function that creates packet from data strings/integers \r
+    # data is used for attaching data to the packet\r
+    def createPacket(self, tcpFlagsSet, seqNr, ackNr, data=None):\r
+        vb_print("" + tcpFlagsSet + " " + str(seqNr) + " " + str(ackNr))\r
+        pIP = IP(dst=self.serverIP, flags="DF")\r
+        pTCP = TCP(sport=self.senderPort,\r
+        dport=self.serverPort,\r
+        seq=seqNr,\r
+        ack=ackNr,\r
+        flags=tcpFlagsSet)\r
+        if data is None:\r
+            p = pIP / pTCP\r
+        else:\r
+            p = pIP / pTCP / Raw(load=data)\r
+        return p\r
+    \r
+    # check whether there is a 1 at the given bit-position of the integer\r
+    def checkForFlag(self, x, flagPosition):\r
+        if x & 2 ** flagPosition == 0:\r
+            return False\r
+        else:\r
+            return True\r
+\r
+    # the flags-parameter of a network packets is returned as an int, this function converts\r
+    # it to a string (such as "FA" if the Fin-flag and Ack-flag have been set)\r
+    def intToFlags(self, x):\r
+        result = ""\r
+        if self.checkForFlag(x, 0):\r
+            result = result + "F"\r
+        if self.checkForFlag(x, 1):\r
+            result = result + "S"\r
+        if self.checkForFlag(x, 2):\r
+            result = result + "R"\r
+        if self.checkForFlag(x, 3):\r
+            result = result + "P"\r
+        if self.checkForFlag(x, 4):\r
+            result = result + "A"\r
+        return result\r
+    \r
+    # sends input over the network to the server\r
+    def sendInput(self, input1, seqNr, ackNr, data = None):\r
+        conf.sniff_promisc = False\r
+        conf.iface = self.networkInterface\r
+        if self.isLocal == True:\r
+            conf.L3socket = L3RawSocket  # if the connection is local/localhost, use l3 raw sockets\r
+        vb_print("sending: "+ str((input1, seqNr, ackNr, data)))\r
+        response = self.sendPacket(input1, seqNr, ackNr, data)\r
+            \r
+        # updating sender state variables\r
+        if response != "Timeout":\r
+            (self.lastFlagsReceived, self.lastSeqReceived, self.lastAckReceived, self.lastDataReceived) = response\r
+            self.isTimeout = False\r
+        else: \r
+            self.isTimeout = True\r
+        \r
+        # printing response\r
+        vb_print("received: "+ str(response))\r
+        return response\r
+\r
+    # resets the connection by changing the port number. On some OSes (Win 8) upon hitting a certain number of\r
+    # connections opened on a port, packets are sent to close down connections. \r
+    def sendReset(self):\r
+        self.refreshNetworkPort()\r