Skip to content

Commit

Permalink
Pep8 reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeb16157 committed Feb 17, 2021
1 parent 092d3dc commit ac6483d
Showing 1 changed file with 90 additions and 63 deletions.
153 changes: 90 additions & 63 deletions Python/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,141 +4,170 @@


class client:
def __init__(self, router_connection, client_IP, normal_Server_IP, reflector_ip,attacker_ip, normal_traffic_rate, fileGen, packetSize, max_port_numb,queueLimit, bandwidthToRouter, packet_ID_prefix = "C-"):
self.clientIP = client_IP #client's ip
self.normalServerIP = normal_Server_IP #normal traffic's
self.routerConnection = router_connection #connection used to connect with the router
def __init__(self, router_connection, client_IP,
normal_Server_IP, reflector_ip, attacker_ip,
normal_traffic_rate, fileGen, packetSize,
max_port_numb, queueLimit, bandwidthToRouter,
packet_ID_prefix="C-"):
self.clientIP = client_IP # client's ip
self.normalServerIP = normal_Server_IP # normal traffic's
self.routerConnection = router_connection
self.reflectorIP = reflector_ip
self.internetHalfOpenConnections = [] #list of half open connections from normal traffic
self.serverHalfOpenConnections = [] #list of half open connections with the server that's under attack
self.receiveBuffer = [] #Receive buffer for client
self.internetHalfOpenConnections = [] # half open connections (normal traffic)
self.serverHalfOpenConnections = [] # list of half open connections (Server)
self.receiveBuffer = [] # Receive buffer for client
self.packetsInReceiveBuffer = 0
self.queue = [] #queue of the client, for the router connection
self.curTime = 0 #current time of the simulation
self.expectedSYNACKs = dict() #Stores the list of expected SYN-ACKs as a dictionary. I assume that the keys are (source IP, expected ACK Number), and the value is the number of times a response has been sent
self.sendingPacket = None #Current packet that is being sent out
self.normalTrafficRate = normal_traffic_rate #rate at which the client sends normal traffic, packets per sec
self.trafficInterval = 1000 / self.normalTrafficRate
self.sendNextSyn = 0 #Time until next syn is sent (random traffic)
self.queue = [] # queue of the router conneciton
self.curTime = 0 # current time of the simulation
self.expectedSYNACKs = dict() # Stores the list of expected SYN-ACKs as a dictionary.
self.sendingPacket = None # Current packet being sent
self.normalTrafficRate = normal_traffic_rate # packets per sec
self.trafficInterval = 1000 / self.normalTrafficRate
self.sendNextSyn = 0 # Time until next syn is sent

self.queueLimit = queueLimit
self.currentQueueLoad = 0

self.packetSize = packetSize #Size of packets in MB
self.packetSize = packetSize # Size of packets in MB
self.maxNumbPorts = max_port_numb
self.packetsSent = 0 #counter for logging purposes
self.packetsReceived = 0 #counter for logging purposes
self.packetsDropped = 0 #counter for packets dropped (currently unused but it makes it easier for the log file to operate)
self.packetsInQueue = 0 #counter for packets in the queue
self.packetsGenerated = 0 #number of unique packets generated
self.packetsInSendBuffer = 0 #counter for packets in the sendBuffer
self.packetsDroppedOnConnection = 0 #specifies the number of packets that dropped on the connectionObject due to bandwidth after being sent by the device
#elf.sendRate = send_rate #MB per Sec
self.packetsSent = 0 # counter for logging purposes
self.packetsReceived = 0 # counter for logging purposes
self.packetsDropped = 0 # counter for packets dropped
self.packetsInQueue = 0 # counter for packets in the queue
self.packetsGenerated = 0 # number of unique packets generated
self.packetsInSendBuffer = 0 # counter for packets in the sendBuffer
self.packetsDroppedOnConnection = 0 # Numb packets dropped

self.bandwidthToRouter = bandwidthToRouter
self.fileGen = fileGen #File management object
self.packetIDPrefix = packet_ID_prefix #Used for packet IDs as a prefix so that we're not using the IPs
self.fileGen = fileGen # File management object
self.packetIDPrefix = packet_ID_prefix # Used for packet IDs
self.timeToNextIteration = 1

self.sendToNormalServer = True
#If no packet is being sent, it will pop a packet from the queue and start sending it

# If no packet is being sent, it will send another one
def processPackets(self):
if(len(self.queue) > 0 and self.sendingPacket == None):
if(len(self.queue) > 0 and self.sendingPacket is None):
curPacket = self.queue.pop(0)
### DW AUDIT 31JAN2021 transmissionDelay = (curPacket.packetSize / self.sendRate) * 1000
transmissionDelay = (self.packetSize / self.bandwidthToRouter) * 1000
self.timeToNextIteration -= transmissionDelay
if(self.timeToNextIteration >= 0):
#self.packetsGenerated += 1
self.sendingPacket = [curPacket, self.curTime + transmissionDelay] #Packet will be put onto connection after transmission delay has occured
self.sendingPacket = [curPacket, self.curTime + transmissionDelay]
self.currentQueueLoad -= curPacket.packetSize
else:
self.timeToNextIteration += transmissionDelay
#self.packetsGenerated += 1
self.sendingPacket = [curPacket, (self.curTime + transmissionDelay) - (self.timeToNextIteration)]
self.currentQueueLoad -= curPacket.packetSize
self.timeToNextIteration -= transmissionDelay
elif(self.sendingPacket != None):
elif(self.sendingPacket is not None):
leftoverTime = self.sendingPacket[1] - self.curTime
self.timeToNextIteration -= leftoverTime
#self.sendPackets()

#If packet is being sent, the packet is placed onto the connection after the packet's transmission delay is satifisfied
# Place packet on connection after trans delay
def sendPackets(self):
if((self.sendingPacket[1] - self.curTime) < 1):
self.packetsSent += 1 #increment counter
self.routerConnection.transferPacketOut(self.sendingPacket[0]) #place packet on connection
self.packetsSent += 1 # increment counter
self.routerConnection.transferPacketOut(self.sendingPacket[0]) # place packet on connection
self.sendingPacket = None

#Places new packets into the receive buffer
# Places new packets into the receive buffer
def receivePacket(self, packet):
self.receiveBuffer.append(packet)
self.fileGen.addToLog(packet.packetID, "Received at Client", packet.sequenceNumber, packet.ackNumber, packet.packetType, self.curTime)
self.fileGen.addToLog(packet.packetID, "Received at Client",
packet.sequenceNumber, packet.ackNumber,
packet.packetType, self.curTime)
self.packetsInReceiveBuffer += 1
self.packetsReceived += 1

#Takes packets in the receive buffer and creates reponses based on the packet type and data.
# Creates reponses based on the packet type and data.
def createResponses(self):
i = 0
while (i < len(self.receiveBuffer)): #iterate over the receive buffer
while (i < len(self.receiveBuffer)): # iterate over the receive buffer
curPacket = self.receiveBuffer[i]
if ( (curPacket.srcIP, curPacket.ackNumber) in self.expectedSYNACKs):
if((curPacket.srcIP, curPacket.ackNumber) in self.expectedSYNACKs):
self.packetsGenerated += 1
responsePacket = packet.packet(self.clientIP, curPacket.dstPortNumber, curPacket.srcIP, curPacket.srcPortNumber, "ACK", 0, curPacket.sequenceNumber + 1, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.clientIP)
self.fileGen.addToLog(responsePacket.packetID, "Generated at Client", responsePacket.sequenceNumber, responsePacket.ackNumber, responsePacket.packetType, self.curTime)
responsePacket = packet.packet(self.clientIP, curPacket.dstPortNumber,
curPacket.srcIP, curPacket.srcPortNumber,
"ACK", 0, curPacket.sequenceNumber + 1,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.clientIP)
self.fileGen.addToLog(responsePacket.packetID, "Generated at Client",
responsePacket.sequenceNumber, responsePacket.ackNumber,
responsePacket.packetType, self.curTime)
self.currentQueueLoad += responsePacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.queue.append(responsePacket)
else:
self.currentQueueLoad -= responsePacket.packetSize
#Drop Packet
else: #if the packet is not expected, send an RST ###dw - check this with Jesse since I'm not sure how the RSTs should be formatted
# Drop Packet
else: # if the packet is not expected, send an RST
self.packetsGenerated += 1
responsePacket = packet.packet(self.clientIP, curPacket.dstPortNumber, curPacket.srcIP, curPacket.srcPortNumber, "RST", 0, curPacket.sequenceNumber + 1, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.clientIP)
self.fileGen.addToLog(responsePacket.packetID, "Generated at Client", responsePacket.sequenceNumber, responsePacket.ackNumber, responsePacket.packetType, self.curTime)
responsePacket = packet.packet(self.clientIP, curPacket.dstPortNumber,
curPacket.srcIP, curPacket.srcPortNumber,
"RST", 0, curPacket.sequenceNumber + 1,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.clientIP)
self.fileGen.addToLog(responsePacket.packetID, "Generated at Client",
responsePacket.sequenceNumber, responsePacket.ackNumber,
responsePacket.packetType, self.curTime)
self.currentQueueLoad += responsePacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.queue.append(responsePacket)
else:
self.currentQueueLoad -= responsePacket.packetSize
#Drop Packet
self.receiveBuffer.pop(i) #remove the packet from the queue
# Drop Packet
self.receiveBuffer.pop(i) # remove the packet from the queue
self.packetsInReceiveBuffer -= 1
self.fileGen.addToLog(curPacket.packetID, "Responded to by Client", curPacket.sequenceNumber, curPacket.ackNumber, curPacket.packetType, self.curTime)
self.fileGen.addToLog(curPacket.packetID, "Responded to by Client",
curPacket.sequenceNumber, curPacket.ackNumber,
curPacket.packetType, self.curTime)

#Simulates normal traffic by sending periodic syn requests to a normal server
# Simulates normal traffic by sending periodic syn requests to a normal server
def createNormalTraffic(self):
if(self.sendNextSyn <= self.curTime):
#Send a syn packet to the normal server
# Send a syn packet to the normal server
if(self.sendToNormalServer):
self.packetsGenerated += 1
normalPacket = packet.packet(self.clientIP, random.randint(0, self.maxNumbPorts), self.normalServerIP, random.randint(0,1000), "SYN", random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.clientIP)
self.fileGen.addToLog(normalPacket.packetID, "Generated at Client", normalPacket.sequenceNumber, normalPacket.ackNumber, normalPacket.packetType, self.curTime)
normalPacket = packet.packet(self.clientIP, random.randint(0, self.maxNumbPorts),
self.normalServerIP, random.randint(0, 1000), "SYN",
random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.clientIP)
self.fileGen.addToLog(normalPacket.packetID, "Generated at Client",
normalPacket.sequenceNumber, normalPacket.ackNumber,
normalPacket.packetType, self.curTime)
self.currentQueueLoad += normalPacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.queue.append(normalPacket)
self.packetsInQueue += 1
self.sendNextSyn = self.curTime + self.normalTrafficRate
#Code for adding the expected response to the dictionary
# Code for adding the expected response to the dictionary
if ((normalPacket.dstIP, normalPacket.sequenceNumber + 1) not in self.expectedSYNACKs):
self.expectedSYNACKs[(normalPacket.dstIP, normalPacket.sequenceNumber + 1)] = 0
else:
self.expectedSYNACKs[(normalPacket.dstIP, normalPacket.sequenceNumber + 1)] += 1
else:
self.currentQueueLoad -= normalPacket.packetSize
self.sendNextSyn = self.curTime + self.normalTrafficRate
#Drop Packet
# Drop Packet
self.sendToNormalServer = False
#send a syn packet to the reflector server
# send a syn packet to the reflector server
else:
self.packetsGenerated += 1
normalPacket = packet.packet(self.clientIP, random.randint(0, self.maxNumbPorts), self.reflectorIP, random.randint(0,1000), "SYN", random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.clientIP)
self.fileGen.addToLog(normalPacket.packetID, "Generated at Client", normalPacket.sequenceNumber, normalPacket.ackNumber, normalPacket.packetType, self.curTime)
normalPacket = packet.packet(self.clientIP, random.randint(0, self.maxNumbPorts),
self.reflectorIP, random.randint(0, 1000), "SYN",
random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.clientIP)
self.fileGen.addToLog(normalPacket.packetID, "Generated at Client",
normalPacket.sequenceNumber, normalPacket.ackNumber,
normalPacket.packetType, self.curTime)
self.currentQueueLoad += normalPacket.packetSize
if(self.currentQueueLoad <= self.queueLimit):
self.queue.append(normalPacket)
self.packetsInQueue += 1
self.sendNextSyn = self.curTime + self.normalTrafficRate
#Code for adding the expected response to the dictionary
# Code for adding the expected response to the dictionary
if ((normalPacket.dstIP, normalPacket.sequenceNumber + 1) not in self.expectedSYNACKs):
self.expectedSYNACKs[(normalPacket.dstIP, normalPacket.sequenceNumber + 1)] = 0
else:
Expand All @@ -148,7 +177,7 @@ def createNormalTraffic(self):
self.sendNextSyn = self.curTime + self.normalTrafficRate
self.sendToNormalServer = True

#Updates time and calls necessary functions for each iteration
# Updates time and calls necessary functions for each iteration
def updateTime(self):
self.curTime += 1
self.timeToNextIteration = 1
Expand All @@ -157,5 +186,3 @@ def updateTime(self):
while((len(self.queue) > 0 or self.sendingPacket is not None) and self.timeToNextIteration > 0):
self.processPackets()
self.sendPackets()
# while(self.timeToNextIteration > 0):
# self.sendPackets()

0 comments on commit ac6483d

Please sign in to comment.