-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDP_Client.py
115 lines (81 loc) · 2.49 KB
/
RDP_Client.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
from socket import *
import time
import sys
import random
import datetime
import hashlib
import struct
IP_ADDR = sys.argv[1]
PORT = sys.argv[2]
FILENAME = sys.argv[3]
PORT = int(PORT)
s = socket(AF_INET,SOCK_DGRAM)
server_addr = (IP_ADDR, PORT)
# (name, seq. no., ack. no., payload)
#send SYN packet.
def packet_string(name, seq_no, ack_no, payload):
pack_str = name + "," + str(seq_no) + "," + str(ack_no) + "," + str(payload)
return pack_str
seq_no = 0
ack_no = 0
syn = packet_string("SYN", seq_no, ack_no, "1")
s.settimeout(1)
while(True):
try:
print("sending syn")
s.sendto(syn, server_addr)
data, address = s.recvfrom(1024)
l = data.split(",")
name = l[0]
if(l[0]=="ACK"):
print("recieved ACK")
seq_no = int(data.split(",")[2])
ack_no = int(data.split(",")[1]) + len(data.split(",")[3])
ack = packet_string("ACK", seq_no, ack_no, "1")
s.sendto(ack, server_addr)
break
except timeout as e:
continue
# GET: (GET, seq_no, ack_no, payload length)
seq_no = 1
ack_no = 1
y = ""
send_flag = False
recieved = ""
md5_requested_file = ""
md5_recieved_file = ""
get_req = packet_string("GET", seq_no, ack_no, FILENAME)
s.sendto(get_req, server_addr)
while True:
try:
data, address = s.recvfrom(1024)
seq_no = int(data.split(",")[2])
ack_no = int(data.split(",")[1]) + len(data.split(",")[3])
pkt = packet_string("ACK", seq_no, ack_no,"1")
if(str(data.split(",")[0])=="DATA"):
x = data.split(",")
x = x[3:]
z = ",".join(x)
y += z
s.sendto(pkt,server_addr)
print("packet recieved")
if(str(data.split(",")[0])=="FIN"):
pkt = packet_string("ACK", seq_no, ack_no,"1")
s.sendto(pkt, server_addr)
break
if(str(data.split(",")[0]) == "MD5"):
pkt = packet_string("ACK", seq_no, ack_no,"1")
s.sendto(pkt, server_addr)
md5_requested_file = (data.split(",")[3])
continue
except timeout as e :
print("timeout, didnt receive packet.")
continue
print(y)
print("MD5 of requested file:", md5_requested_file)
h = hashlib.md5()
h.update(y)
print("MD5 of reecieved file:", h.hexdigest())
new_file = open("recieved_file.txt", "w+")
new_file.write(y)
s.close()