-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import random | ||
|
||
|
||
class nat: | ||
def __init__(self, connectiontoRouter, connectiontoClient, | ||
queueSize, fileGen, queueLimit, | ||
bandwidthtoRouter, bandwidthtoClient, | ||
packet_ID_prefix="T-"): | ||
self.connectiontoRouter = connectiontoRouter | ||
self.connectiontoClient = connectiontoClient | ||
self.queueSize = queueSize | ||
self.fileGen = fileGen | ||
self.ipAddress = "10.0.0.0" | ||
|
||
self.queueRouter = [] | ||
self.queueClient = [] | ||
self.sendRouter = None | ||
self.sendClient = None | ||
|
||
self.receiveBuffer = [] | ||
self.curTime = 0 | ||
self.packetsReceived = 0 | ||
|
||
self.queueLimit = queueLimit | ||
self.currentQueueLoadClient = 0 | ||
self.currentQueueLoadRouter = 0 | ||
|
||
self.bandwidthtoRouter = bandwidthtoRouter | ||
self.bandwidthtoClient = bandwidthtoClient | ||
|
||
self.timeToNextIterationClient = 1 | ||
self.timeToNextIterationRouter = 1 | ||
|
||
self.packetsSent = 0 | ||
self.packetsDropped = 0 | ||
self.packetIDPrefix = packet_ID_prefix | ||
|
||
self.openConnections = {} # Key = (IP, Port number), Value = (Inside IP, Inside Port number) | ||
|
||
def receivePacket(self, packet): | ||
if(len(self.receiveBuffer) < self.queueSize): | ||
self.receiveBuffer.append(packet) | ||
self.packetsReceived += 1 | ||
self.fileGen.addToLog(packet.packetID, "Received at NAT Device", | ||
packet.sequenceNumber, packet.ackNumber, | ||
packet.packetType, self.curTime) | ||
else: | ||
# print("Packet is getting Dropped") | ||
# self.fileGen.animateDrop(self.packetIDPrefix[0], | ||
# curPacket.packetType, | ||
# curPacket.sequenceNumber, | ||
# curPacket.ackNumber, | ||
# curPacket.dstPortNumber) | ||
self.packetsDropped += 1 | ||
self.fileGen.addToLog(packet.packetID, | ||
"Dropped at Router: No space in receiveBuffer", | ||
packet.sequenceNumber, packet.ackNumber, | ||
packet.packetType, self.curTime) | ||
|
||
def addtoQueues(self): | ||
for i in self.receiveBuffer: | ||
if(i.dstIP == self.ipAddress): | ||
self.currentQueueLoadClient += i.packetSize | ||
if(self.currentQueueLoadClient <= self.queueLimit): | ||
self.queueClient.append(i) | ||
else: | ||
self.currentQueueLoadClient -= i.packetSize | ||
# Drop Packet | ||
self.receiveBuffer.remove(i) | ||
else: | ||
self.currentQueueLoadRouter += i.packetSize | ||
if(self.currentQueueLoadRouter <= self.queueLimit): | ||
self.queueRouter.append(i) | ||
else: | ||
self.currentQueueLoadRouter -= i.packetSize | ||
# Drop Packet | ||
self.receiveBuffer.remove(i) | ||
|
||
def mapPacket(self, packet, dst): | ||
if(dst == "CLIENT"): | ||
print("Client packet getting mapped") | ||
print(packet) | ||
internalInfo = self.openConnections.get((packet.dstIP, packet.dstPortNumber)) | ||
if(internalInfo is not None): | ||
self.openConnections.pop((packet.dstIP, packet.dstPortNumber)) | ||
packet.dstIP = internalInfo[0] | ||
packet.dstPortNumber = internalInfo[1] | ||
print(packet) | ||
return True | ||
else: | ||
# Drop Packet | ||
print("Mapping not found") | ||
return False | ||
else: | ||
print("Router packet getting mapped") | ||
portNumber = random.randint(0, 64000) | ||
print(packet) | ||
while(self.openConnections.get((self.ipAddress, portNumber)) is not None): | ||
portNumber = random.randint(0, 64000) | ||
self.openConnections[(self.ipAddress, portNumber)] = (packet.srcIP, packet.srcPortNumber) | ||
packet.srcIP = self.ipAddress | ||
packet.srcPortNumber = portNumber | ||
print(packet) | ||
return True | ||
|
||
# Add send packets function and mapping function | ||
def processPackets(self): | ||
if(len(self.queueClient) > 0 and self.sendClient is None and | ||
self.timeToNextIterationClient >= 0): # send packet to client | ||
curPacket = self.queueClient.pop(0) | ||
if(self.mapPacket(curPacket, "CLIENT")): | ||
transmissionDelay = (curPacket.packetSize / self.bandwidthtoRouter) * 1000 | ||
self.timeToNextIterationClient -= transmissionDelay | ||
if(self.timeToNextIterationClient >= 0): | ||
self.packetsSent += 1 | ||
self.sendClient = [curPacket, self.curTime | ||
+ transmissionDelay] | ||
self.currentQueueLoadClient -= curPacket.packetSize | ||
else: | ||
self.timeToNextIterationClient += transmissionDelay | ||
self.packetsSent += 1 | ||
self.sendClient = [curPacket, self.curTime + transmissionDelay | ||
- (self.timeToNextIterationClient)] | ||
self.currentQueueLoadClient -= curPacket.packetSize | ||
self.timeToNextIterationClient -= transmissionDelay | ||
elif(self.sendClient is not None): | ||
leftoverTime = self.sendClient[1] - self.curTime | ||
self.timeToNextIterationClient -= leftoverTime | ||
if(len(self.queueRouter) > 0 and self.sendRouter is None and | ||
self.timeToNextIterationRouter >= 0): # send packet to client | ||
curPacket = self.queueRouter.pop(0) | ||
if(self.mapPacket(curPacket, "ROUTER")): | ||
transmissionDelay = (curPacket.packetSize / self.bandwidthtoRouter) * 1000 | ||
self.timeToNextIterationRouter -= transmissionDelay | ||
if(self.timeToNextIterationRouter >= 0): | ||
self.packetsSent += 1 | ||
self.sendRouter = [curPacket, self.curTime | ||
+ transmissionDelay] | ||
self.currentQueueLoadRouter -= curPacket.packetSize | ||
else: | ||
self.timeToNextIterationRouter += transmissionDelay | ||
self.packetsSent += 1 | ||
self.sendRouter = [curPacket, self.curTime + transmissionDelay | ||
- (self.timeToNextIterationRouter)] | ||
self.currentQueueLoadRouter -= curPacket.packetSize | ||
self.timeToNextIterationRouter -= transmissionDelay | ||
elif(self.sendRouter is not None): | ||
leftoverTime = self.sendRouter[1] - self.curTime | ||
self.timeToNextIterationRouter -= leftoverTime | ||
|
||
def sendPackets(self): | ||
if(self.sendClient is not None): | ||
if((self.sendClient[1] - self.curTime) < 1): | ||
destinationConnection = self.connectiontoClient | ||
destinationConnection.transferPacketOut(self.sendClient[0]) | ||
self.packetsSent += 1 | ||
self.sendClient = None | ||
if(self.sendRouter is not None): | ||
if((self.sendRouter[1] - self.curTime) < 1): | ||
destinationConnection = self.connectiontoRouter | ||
destinationConnection.transferPacketOut(self.sendRouter[0]) | ||
self.packetsSent += 1 | ||
self.sendRouter = None | ||
|
||
def updateTime(self): | ||
self.curTime = self.curTime + 1 | ||
self.timeToNextIterationClient = 1 | ||
self.timeToNextIterationRouter = 1 | ||
# self.numberOfPacketsReceived = 0 | ||
self.addtoQueues() | ||
while((self.timeToNextIterationRouter > 0 and (self.sendRouter is not None or len(self.queueRouter) > 0)) | ||
or (self.timeToNextIterationClient > 0 and (self.sendClient is not None or len(self.queueClient) > 0))): | ||
self.processPackets() | ||
self.sendPackets() |