-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdt.py
executable file
·268 lines (244 loc) · 9.97 KB
/
rdt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
Author Jiahong Xiang
SID 11812613
"""
import math
import random
from socket import timeout
from USocket import UnreliableSocket
from ast import literal_eval
import threading
import time
TIME_OUT = 1.6
PKT_SIZE = 512
WND_SIZE = 3
client_isn = 3
server_isn = 0
CONGESTION = 0.001
def CONGESTION_CONTROL(ACK_NUM, SEND_WND):
if ACK_NUM < SEND_WND * 0.1:
return 1.8
elif ACK_NUM < SEND_WND * 0.3:
return 1.6
elif ACK_NUM < SEND_WND * 0.6:
return 1.4
elif ACK_NUM < SEND_WND * 0.8:
return 1.2
elif ACK_NUM < SEND_WND * 0.9:
return 0.9
else:
return 0.8
pass
class RDTSocket(UnreliableSocket):
def __init__(self, rate=None, debug=True):
super().__init__(rate=rate)
self._rate = rate
self.debug = debug
self.address = None
self.WND_SIZE = 128
def accept(self): # -> (RDTSocket, (str, int)):
while True:
# receive syn
data, addr = super().recvfrom(2048)
header = getHeader(data)
if self.debug:
print("first handshaking: {}".format(header))
if header['syn'] == 1:
conn = RDTSocket(self._rate)
conn.address = addr
server_addr = self.getsockname()
conn.bind((server_addr[0], server_addr[1] + random.randint(1, 50000)))
while True:
# send syn, ack
conn.sendto(genPKT(1, 0, 0, server_isn, header['seq'] + 1, 0), addr)
# receive ack
conn_header = getHeader(conn.recvfrom(2048)[0])
if (conn_header['syn'] == 0 and conn_header['seq_ack'] == server_isn + 1) or \
(conn_header['syn'] == 1 and conn_header['fin'] == 1 and conn_header['ack'] == 1):
if self.debug:
print('third handshaking: {}\n<Connection setup>\n'.format(conn_header))
return conn, conn.address
def connect(self, addr: (str, int)):
while True:
# send syn
self.sendto(genPKT(1, 0, 0, client_isn, 0, 0), addr)
# receive syn, ack
data, address = super().recvfrom(2048)
header = getHeader(data)
if self.debug:
print("second handshaking: {}".format(header))
if header['syn'] == 1:
for i in range(5):
self.sendto(genPKT(0, 0, 0, header['seq_ack'], header['seq'] + 1, 0), address) # send ack
time.sleep(0.1)
self.address = address
if self.debug:
print('<Connection setup>\n')
break
# (syn, fin, ack, seq, seq_ack, length):
def send(self, msg: bytes):
global CONGESTION
SEND_WND = 128
# the number of slices for sending
pktBufferSize = math.ceil(len(msg) / PKT_SIZE)
super().settimeout(TIME_OUT)
while True:
try:
# tell the recv how many slice need to recv
self.sendto(genPKT(1, 1, 1, pktBufferSize, 0, 0), self.address)
while True:
data, addr = super().recvfrom(2048)
if addr != self.address:
continue
header = getHeader(data)
# if recv other status: like recv request -> the ack pkt missed so just break to next status
if not check(data) or header['syn'] != 1 or header['fin'] != 0 or header['ack'] != 1:
break
if not check(data) or header['syn'] != 1 or header['fin'] != 1 or header['ack'] != 0:
continue
# print('<send 1> header :{0}'.format(header))
break
break
except timeout:
continue
while True:
try:
# get the query from recv
data, addr = super().recvfrom(2048)
if addr != self.address:
continue
header = getHeader(data)
# if recv other status: like recv request -> the ack pkt missed so just break to next status
if check(data) and header['syn'] == 1 and header['fin'] == 1 and header['ack'] == 1:
break
if check(data) and header['syn'] == 0 and header['fin'] == 1 and header['ack'] == 0:
return b''
if not check(data) or header['syn'] != 1 or header['fin'] != 0 or header['ack'] != 1:
continue
ACK_NUM = header['seq_ack']
NAKList = (data[17:17 + header['len']]).decode()
# congestion control
if len(NAKList) > 64: CONGESTION = CONGESTION * CONGESTION_CONTROL(ACK_NUM, SEND_WND)
if CONGESTION > 0.15: CONGESTION = 0.15
if self.debug:
print("=======ACK_NUM=======SEND_WND==========CONGESTION========", 'TIME_OUT')
print("========", ACK_NUM, "========", SEND_WND, "========", CONGESTION, "========",TIME_OUT)
print('<send 2>', (data[17:17 + header['len']]))
NAKList = literal_eval(NAKList)
SEND_WND = len(NAKList)
# send the query list
for i in NAKList:
payload = msg[i * PKT_SIZE:(i + 1) * PKT_SIZE if (i + 1) * PKT_SIZE < len(msg) else len(msg)]
self.sendto(genPKT(1, 0, 0, i, 0, len(payload), payload), self.address)
time.sleep(CONGESTION)
if not NAKList:
break
except timeout:
continue
def recv(self, bufsize: int):
global TIME_OUT
ACK_NUM = 0
super().settimeout(TIME_OUT)
# pktbuffer to recv data
# NAK-list to record the item which is NAK
pktBuffer, NAKList = [], []
while True:
try:
data, addr = super().recvfrom(bufsize)
if addr != self.address:
continue
header = getHeader(data)
if check(data) and header['syn'] == 0 and header['fin'] == 1 and header['ack'] == 0:
return b''
if not check(data) or header['syn'] != 1 or header['fin'] != 1 or header['ack'] != 1:
continue
# new pkt buffer and nak-list
for i in range(header['seq']):
pktBuffer.append([])
NAKList = [i for i in range(header['seq'])]
for i in range(5):
self.sendto(genPKT(1, 1, 0, 0, 0, 0), addr)
time.sleep(0.01)
break
except timeout:
continue
# initial TIMEOUT by the len of NAK-LIST
TIME_OUT = 0.02 * len(NAKList)
TIME_OUT = 0.15 if TIME_OUT < 0.15 else TIME_OUT
TIME_OUT = 1.6 if TIME_OUT > 1.6 else TIME_OUT
super().settimeout(TIME_OUT)
while True:
subNAKList = NAKList[:self.WND_SIZE] if len(NAKList) > self.WND_SIZE else NAKList
bytesNAKList = str(subNAKList).encode('utf-8')
self.sendto(genPKT(1, 0, 1, 0, ACK_NUM, len(bytesNAKList), bytesNAKList), addr) # send ack
ACK_NUM = 0
if self.debug:
print('<recv 2>', NAKList)
if not NAKList: break
while True:
try:
data, addr = super().recvfrom(bufsize)
if addr != self.address:
continue
header = getHeader(data)
seq = header['seq']
if check(data) and header['syn'] == 0 and header['fin'] == 1 and header['ack'] == 0:
return b''
if not check(data) or seq not in NAKList or header['syn'] != 1 or header['fin'] != 0 or header[
'ack'] != 0:
continue
ACK_NUM += 1
pktBuffer[seq] = data[17:17 + header['len']]
NAKList.remove(seq)
except timeout:
break
MSG = b''
for i in pktBuffer:
MSG += i
return MSG
def close(self):
if self.debug:
print('start to close')
super().settimeout(30)
while True:
self.sendto(genPKT(0, 1, 0, 0, 0, 0), self.address)
data, address = super().recvfrom(2048)
header = getHeader(data)
if self.debug:
print('wave header:{}'.format(header))
if header['fin'] == 1:
self.sendto(genPKT(0, 0, 1, 0, 0, 0), self.address) # send ack
self.sendto(genPKT(0, 1, 0, 0, 0, 0), self.address) # send fin
super().close()
if self.debug:
print('closed')
break
super().close()
def genPKT(syn, fin, ack, seq, seq_ack, length, payload=b''):
header = syn.to_bytes(1, 'big') + fin.to_bytes(1, 'big') + \
ack.to_bytes(1, 'big') + seq.to_bytes(4, 'big') + \
seq_ack.to_bytes(4, 'big') + length.to_bytes(4, 'big')
checksum = int.to_bytes(calc_checksum(header + payload), 2, 'big')
return header + checksum + payload
def getHeader(data):
keys = {'syn': bytes_int(data[0:1]), 'fin': bytes_int(data[1:2]),
'ack': bytes_int(data[2:3]), 'seq': bytes_int(data[3:7]),
'seq_ack': bytes_int(data[7:11]), 'len': bytes_int(data[11:15])}
return keys
def bytes_int(bytes):
return int.from_bytes(bytes, 'big')
def check(payload):
sum = 0
for byte in payload:
sum += byte
return sum % 256 == 0
def calc_checksum(payload):
sum = 0
for byte in payload:
sum += byte
sum = -(sum % 256)
return sum & 0xFF
def updateNAK(ACKList, NAKList):
for i in ACKList:
NAKList.remove(i)
return NAKList