Commit 064b28f8 authored by thomas alfroy's avatar thomas alfroy
Browse files

add server answer and congestion simulation

parent 3d3f1d7f
......@@ -2,27 +2,34 @@ import socket
import select
import sys
import random
import getopt
import time
ECN_ACTIVE = 1
ECN_DISABLED = 2
TYPE_SYN = 0X01
TYPE_ACK = 0X02
TYPE_DATA = 0X04
TYPE_SYN = 1
TYPE_FIN = 2
TYPE_ACK = 16
TYPE_RST = 4
MAX_PACKETS = 20
def parse_type(t):
if t == 3:
print("Type : Syn/Ack")
elif t == 2:
print("Type : Ack")
if t == 2:
print("Type : FIN")
elif t == 16:
print("Type : ACK")
elif t == 1:
print("Type : Syn")
print("Type : SYN")
elif t == 4:
print("Type : Data")
print("Type : RST")
else:
print("Unrecognized Type : Please check your packet format")
def parse_ecn(e):
if e == ECN_ACTIVE:
print("ECN bit is enabled")
......@@ -37,17 +44,57 @@ def parse_ecn(e):
##############################################################
def parse_new_messages(msg):
t = ord(msg[0])
print("\nNew source message received")
t = msg[0]
parse_type(t)
nchars = 3
nchars = 1
# string to int or long. Type depends on nchars
seq = sum(ord(msg[byte + 1])<<8*(nchars-byte-1) for byte in range(nchars))
seq = sum((msg[byte + 1])<<8*(nchars-byte-1) for byte in range(nchars))
print("SeqNum : {}".format(seq))
nchars = 1
# string to int or long. Type depends on nchars
ack_seq = sum((msg[byte + 3])<<8*(nchars-byte-1) for byte in range(nchars))
print("Ack seq Num : {}".format(ack_seq))
#seq = ord(msg[1:4])
ecn = ord(msg[5])
ecn = msg[5]
parse_ecn(ecn)
pay = msg[6:]
print("Payload : {}".format(pay))
#print("Payload : {}".format(pay))
def parse_new_messages_server(msg):
print("\nNew server message received")
t = msg[0]
parse_type(t)
nchars = 1
# string to int or long. Type depends on nchars
seq = sum((msg[byte + 1])<<8*(nchars-byte-1) for byte in range(nchars))
print("SeqNum : {}".format(seq))
nchars = 1
# string to int or long. Type depends on nchars
ack_seq = sum((msg[byte + 3])<<8*(nchars-byte-1) for byte in range(nchars))
print("Ack seq Num : {}".format(ack_seq))
#seq = ord(msg[1:4])
ecn = msg[5]
parse_ecn(ecn)
win_size = msg[6]
print("Size of new Window : {}".format(win_size))
pay = msg[7:]
#print("Payload : {}".format(pay))
debug = False
verb = False
second = False
options, remainder = getopt.getopt(sys.argv[1:], 'dvs', ['debug', 'verbose', 'second',])
for opt, arg in options:
if opt in ('-d', '--debug'):
debug = True
if opt in ('-v', '--verbose'):
verb = True
if opt in ('-s', '--second'):
second = True
sock_sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
......@@ -55,30 +102,52 @@ sock_recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dest_sender = "127.0.0.1"
dest_recv = "127.0.0.1"
port_sender = 5000
port_recv = 5003
port_sender = 3333
port_recv = 6666
sock_sender.bind(("127.0.0.1", 5001))
sock_recv.bind(("127.0.0.1", 5002))
sock_sender.bind(("127.0.0.1", 4444))
sock_recv.bind(("127.0.0.1", 5555))
input = [ sock_sender, sock_recv , sys.stdin ]
con = True
act_time = time.time()
nb_packets = 0
while con:
i_ready, o_ready, e_ready = select.select(input, [], [])
if second :
if act_time + 1 <= time.time():
print("{} packets received last second".format(nb_packets))
nb_packets = 0
act_time = time.time()
i_ready, o_ready, e_ready = select.select(input, [], [], 1.0)
for s in i_ready:
if s == sock_sender:
data, addr = sock_sender.recvfrom(1024)
if random.randint(0, 100) > 25 :
sock_sender.sendto(data, (dest_recv, port_recv))
print("message {} received from sender".format(data))
parse_new_messages(data)
data, addr = sock_sender.recvfrom(64)
nb_packets += 1
if nb_packets > MAX_PACKETS:
data_tmp = bytearray(data)
data_tmp[5] = ECN_ACTIVE
data = bytes(data_tmp)
sock_sender.sendto(data, (dest_recv, port_recv))
if debug :
print("message {} received from sender".format(data))
if verb :
parse_new_messages(data)
elif s == sock_recv:
data, addr = sock_recv.recvfrom(1024)
data, addr = sock_recv.recvfrom(64)
sock_recv.sendto(data, (dest_sender, port_sender))
print("message {} received from receiver".format(data))
if debug :
print("message {} received from receiver".format(data))
#parse_new_messages(data)
if verb :
parse_new_messages_server(data)
elif s == sys.stdin:
data = sys.stdin.readline()
data = data.strip()
......
File added
File added
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment