Commit 5451af7a authored by thomas alfroy's avatar thomas alfroy
Browse files

modif header

parent 82d5db12
# tagger juste le premier
# Fenetre source -> dest
# 2 modes drop / ecn / mode hybride
import socket
import select
import sys
......@@ -13,19 +18,34 @@ TYPE_FIN = 2
TYPE_ACK = 16
TYPE_RST = 4
MAX_PACKETS = 20
MAX_PACKETS = 100
def help():
print("Usage : python3 medium.py [option]\n\n" \
"This program is used to simulate loss of congestion (with the ECN mode). In normal mode, generates 30 percents loss when receiving more than 100 packets within a second. In ECN mode, set the ECN bit for the first packet of a time interval of 1 second when receiving more than 100 packets within a second.\n\n" \
"\t-v,--verbose\t\tUsed for debug, display the pseudo TCP header for each received packet\n" \
"\t-s,--second\t\tDisplay the number of received message each second\n" \
"\t-e,--ecn\t\tActivate the ECN mode\n" \
"\t-l,--limit [val]\tSet the packet limit rate before loss to the choosen value\n")
def parse_type(t):
if t == 2:
print("Type : FIN")
elif t == 16:
print("Type : ACK")
elif t == 1:
print("Type : SYN")
elif t == 4:
print("Type : RST")
else:
print("Unrecognized Type : Please check your packet format")
ok_ = False
print("Type :", end="")
if t & TYPE_FIN:
print(" FIN ", end="")
ok_ = True
if t & TYPE_ACK:
print(" ACK ", end="")
ok_ = True
if t & TYPE_SYN:
print(" SYN ", end="")
ok_ = True
if t & TYPE_RST:
print(" RST ", end="")
ok_ = True
if not ok_:
print("Unrecognized Type : Please check your packet format", end="")
print("")
......@@ -45,48 +65,54 @@ def parse_ecn(e):
def parse_new_messages(msg):
print("\nNew source message received")
t = msg[0]
flux = msg[0]
print("Flux ID : {}".format(flux))
t = msg[1]
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))
seq = sum((msg[byte + 2])<<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))
ack_seq = sum((msg[byte + 4])<<8*(nchars-byte-1) for byte in range(nchars))
print("Ack seq Num : {}".format(ack_seq))
#seq = ord(msg[1:4])
ecn = msg[5]
ecn = msg[6]
parse_ecn(ecn)
pay = msg[6:]
#print("Payload : {}".format(pay))
win_size = msg[7]
print("Size of congestion Window : {}".format(win_size))
def parse_new_messages_server(msg):
print("\nNew server message received")
t = msg[0]
flux = msg[0]
print("Flux ID : {}".format(flux))
t = msg[1]
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))
seq = sum((msg[byte + 2])<<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))
ack_seq = sum((msg[byte + 4])<<8*(nchars-byte-1) for byte in range(nchars))
print("Ack seq Num : {}".format(ack_seq))
#seq = ord(msg[1:4])
ecn = msg[5]
ecn = msg[6]
parse_ecn(ecn)
win_size = msg[6]
win_size = msg[7]
print("Size of new Window : {}".format(win_size))
pay = msg[7:]
#print("Payload : {}".format(pay))
debug = False
verb = False
second = False
ecn = False
hybride = False
options, remainder = getopt.getopt(sys.argv[1:], 'dvs', ['debug', 'verbose', 'second',])
options, remainder = getopt.getopt(sys.argv[1:], 'dvsehl:', ['debug', 'verbose', 'second', 'ecn', 'help', 'limit=',])
for opt, arg in options:
if opt in ('-d', '--debug'):
......@@ -95,6 +121,16 @@ for opt, arg in options:
verb = True
if opt in ('-s', '--second'):
second = True
if opt in ('-e','--ecn'):
ecn = True
if opt in ('-l','--limit'):
MAX_PACKETS = arg
if opt in ('-h', '--help'):
help()
exit(0)
random.seed(time.time())
sock_sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
......@@ -113,13 +149,15 @@ input = [ sock_sender, sock_recv , sys.stdin ]
con = True
act_time = time.time()
nb_packets = 0
tagged = False
while con:
if second :
if act_time + 1 <= time.time():
if act_time + 1 <= time.time():
if second :
print("{} packets received last second".format(nb_packets))
nb_packets = 0
act_time = time.time()
tagged = False
i_ready, o_ready, e_ready = select.select(input, [], [], 1.0)
......@@ -129,11 +167,19 @@ while con:
nb_packets += 1
if nb_packets > MAX_PACKETS:
data_tmp = bytearray(data)
data_tmp[5] = ECN_ACTIVE
data = bytes(data_tmp)
if ecn :
if not tagged:
data_tmp = bytearray(data)
data_tmp[5] = ECN_ACTIVE
data = bytes(data_tmp)
tagged = True
sock_sender.sendto(data, (dest_recv, port_recv))
else:
if random.random() <= 0.7:
sock_sender.sendto(data, (dest_recv, port_recv))
sock_sender.sendto(data, (dest_recv, port_recv))
if debug :
print("message {} received from sender".format(data))
......
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