From: charlie Date: Sun, 20 Dec 2015 15:40:36 +0000 (+0100) Subject: untested python adapter for gast program X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=9c42119ca93659f5f3cba903861b41a75cfed23b;p=tt2015.git untested python adapter for gast program --- diff --git a/a3/code/adapter/listener.py b/a3/code/adapter/listener.py new file mode 100644 index 0000000..1cb7981 --- /dev/null +++ b/a3/code/adapter/listener.py @@ -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 index 0000000..f80bd4b --- /dev/null +++ b/a3/code/adapter/sender.py @@ -0,0 +1,150 @@ +from scapy.all import * + +verbose = 0 + +def vb_print(msg): + global verbose + if verbose == 1: + print msg + +# the sender sends packets with configurable parameters to a server and retrieves responses +class Sender: + # information on the SUT + def __init__(self, serverIP, serverPort=8000, + networkInterface="lo", isLocal=True, senderPortMinimum=20000, + senderPortMaximum=40000, portNumberFile="sn.txt", + isVerbose=0, waitTime=1): + + + # file where the last sender port used is stored + self.portNumberFile = portNumberFile; + + # when choosing a fresh port, a new port is chosen + # within boundaries defined by the parameters below + self.senderPortMinimum = senderPortMinimum + self.senderPortMaximum = senderPortMaximum + + # data on sender and server needed to send packets + self.serverIP = serverIP + self.serverPort = serverPort + self.networkInterface = networkInterface + self.senderPort = self.getNextPort() + self.isLocal = isLocal + + # time to wait for a response from the server before concluding a timeout + self.waitTime = waitTime + + # verbose or not + self.isVerbose = isVerbose + + # variables added so you can easily test the last system response + self.lastSeqReceived = None + self.lastAckReceived = None + self.isTimeout = None + self.lastFlagsReceived = None + self.lastDataReceived = None + + + # chooses a new port to send packets from + def refreshNetworkPort(self): + vb_print("previous local port: " + str(self.senderPort)) + self.senderPort = self.getNextPort() + vb_print("next local port: " + str(self.senderPort) + "\n") + return self.senderPort + + # gets a new port number, an increment of the old within the specified limits. Uses a file. + def getNextPort(self): + f = open(self.portNumberFile, "a+") + f.seek(0) + line = f.readline() + if line == '' or int(line) < self.senderPortMinimum: + networkPort = self.senderPortMinimum + else: + networkPort = (int(line) + 1) % self.senderPortMaximum + f.closed + f = open(self.portNumberFile, "w") + f.write(str(networkPort)) + f.closed + return networkPort + + # send a packet onto the network with the given parameters, and return the response packet + # in case of a timeout, returns None, otherwise, returns the tuple (flags, seqNo, ackNo) + def sendPacket(self, flagsSet, seqNr, ackNr, data = None): + packet = self.createPacket(flagsSet, seqNr, ackNr, data) + # consider adding the parameter: iface="ethx" if you don't receive a response. Also consider increasing the wait time + scapyResponse = sr1(packet, timeout=self.waitTime, verbose=self.isVerbose) + if scapyResponse is not None: + # scapyResponse.show() + # ^^ in case you want to show the packet content + # here is what you store from every packet response + if Raw not in scapyResponse: + response = (self.intToFlags(scapyResponse[TCP].flags), scapyResponse[TCP].seq, scapyResponse[TCP].ack, None) + else: + response = (self.intToFlags(scapyResponse[TCP].flags), scapyResponse[TCP].seq, scapyResponse[TCP].ack, scapyResponse[Raw].load) + else: + response = "Timeout" + return response + + # function that creates packet from data strings/integers + # data is used for attaching data to the packet + def createPacket(self, tcpFlagsSet, seqNr, ackNr, data=None): + vb_print("" + tcpFlagsSet + " " + str(seqNr) + " " + str(ackNr)) + pIP = IP(dst=self.serverIP, flags="DF") + pTCP = TCP(sport=self.senderPort, + dport=self.serverPort, + seq=seqNr, + ack=ackNr, + flags=tcpFlagsSet) + if data is None: + p = pIP / pTCP + else: + p = pIP / pTCP / Raw(load=data) + return p + + # check whether there is a 1 at the given bit-position of the integer + def checkForFlag(self, x, flagPosition): + if x & 2 ** flagPosition == 0: + return False + else: + return True + + # the flags-parameter of a network packets is returned as an int, this function converts + # it to a string (such as "FA" if the Fin-flag and Ack-flag have been set) + def intToFlags(self, x): + result = "" + if self.checkForFlag(x, 0): + result = result + "F" + if self.checkForFlag(x, 1): + result = result + "S" + if self.checkForFlag(x, 2): + result = result + "R" + if self.checkForFlag(x, 3): + result = result + "P" + if self.checkForFlag(x, 4): + result = result + "A" + return result + + # sends input over the network to the server + def sendInput(self, input1, seqNr, ackNr, data = None): + conf.sniff_promisc = False + conf.iface = self.networkInterface + if self.isLocal == True: + conf.L3socket = L3RawSocket # if the connection is local/localhost, use l3 raw sockets + vb_print("sending: "+ str((input1, seqNr, ackNr, data))) + response = self.sendPacket(input1, seqNr, ackNr, data) + + # updating sender state variables + if response != "Timeout": + (self.lastFlagsReceived, self.lastSeqReceived, self.lastAckReceived, self.lastDataReceived) = response + self.isTimeout = False + else: + self.isTimeout = True + + # printing response + vb_print("received: "+ str(response)) + return response + + # resets the connection by changing the port number. On some OSes (Win 8) upon hitting a certain number of + # connections opened on a port, packets are sent to close down connections. + def sendReset(self): + self.refreshNetworkPort()