Commit 3d3f1d7f authored by thomas alfroy's avatar thomas alfroy
Browse files

continue project

parent 8d3a50dc
{
"files.associations": {
"errno.h": "c"
}
}
\ No newline at end of file
import socket
import select
import sys
import random
ECN_ACTIVE = 1
ECN_DISABLED = 2
TYPE_SYN = 0X01
TYPE_ACK = 0X02
TYPE_DATA = 0X04
def parse_type(t):
if t == 3:
print("Type : Syn/Ack")
elif t == 2:
print("Type : Ack")
elif t == 1:
print("Type : Syn")
elif t == 4:
print("Type : Data")
else:
print("Unrecognized Type : Please check your packet format")
def parse_ecn(e):
if e == ECN_ACTIVE:
print("ECN bit is enabled")
elif e == ECN_DISABLED:
print("ECN bit is disable")
else:
print("There is an issue with your ECN bit : please check your packet format")
##############################################################
## Packet format : Type, Seq, Ecn, Payload ###################
##############################################################
def parse_new_messages(msg):
t = ord(msg[0])
parse_type(t)
nchars = 3
# string to int or long. Type depends on nchars
seq = sum(ord(msg[byte + 1])<<8*(nchars-byte-1) for byte in range(nchars))
print("SeqNum : {}".format(seq))
#seq = ord(msg[1:4])
ecn = ord(msg[5])
parse_ecn(ecn)
pay = msg[6:]
print("Payload : {}".format(pay))
# udp_dest = "127.0.0.1"
# udp_port = 5001
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock.bind(("127.0.0.1", 5000))
# data, addr = sock.recvfrom(1024)
# sock.sendto(data, (udp_dest, udp_port))
# print("Data succesfully transfered")
# sock_rcv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock_rcv.bind(("127.0.0.1", 5002))
# data, addr = sock_rcv.recvfrom(1024)
# sock_rcv.sendto(data, (udp_dest, 5003))
# print("Ack successfully tranfered")
sock_sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
......@@ -47,14 +71,17 @@ while con:
for s in i_ready:
if s == sock_sender:
data, addr = sock_sender.recvfrom(1024)
sock_sender.sendto(data, (dest_recv, port_recv))
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)
elif s == sock_recv:
data, addr = sock_recv.recvfrom(1024)
sock_recv.sendto(data, (dest_sender, port_sender))
print("message {} received from receiver".format(data))
elif s == sys.stdin:
data = sys.stdin.readline()
data = data.strip()
#print("received data")
if data == "quit":
con = False
else:
......
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