blob: 7fab2d766ad9ab7f63bc03faa13bd91132046f55 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Florin Corase04c2992017-03-01 08:17:34 -08002
3import socket
4import sys
Florin Corasd79b41e2017-03-04 05:37:52 -08005import time
Florin Coras3cbc04b2017-10-02 00:18:51 -07006import argparse
Florin Corase04c2992017-03-01 08:17:34 -08007
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02008# action can be reflect or drop
Florin Corase04c2992017-03-01 08:17:34 -08009action = "drop"
Florin Corasf6d68ed2017-05-07 19:12:02 -070010test = 0
11
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012
13def test_data(data, n_rcvd):
14 n_read = len(data)
Florin Corasf6d68ed2017-05-07 19:12:02 -070015 for i in range(n_read):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020016 expected = (n_rcvd + i) & 0xFF
17 byte_got = ord(data[i])
18 if byte_got != expected:
19 print(
20 "Difference at byte {}. Expected {} got {}".format(
21 n_rcvd + i, expected, byte_got
22 )
23 )
Florin Corasf6d68ed2017-05-07 19:12:02 -070024 return n_read
Florin Corase04c2992017-03-01 08:17:34 -080025
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020026
27def handle_connection(connection, client_address):
Florin Corase04c2992017-03-01 08:17:34 -080028 print("Received connection from {}".format(repr(client_address)))
Florin Corasf6d68ed2017-05-07 19:12:02 -070029 n_rcvd = 0
Florin Corase04c2992017-03-01 08:17:34 -080030 try:
31 while True:
32 data = connection.recv(4096)
33 if not data:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 break
35 if test == 1:
36 n_rcvd += test_data(data, n_rcvd)
37 if action != "drop":
Florin Corase04c2992017-03-01 08:17:34 -080038 connection.sendall(data)
39 finally:
40 connection.close()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041
42
Florin Coras3cbc04b2017-10-02 00:18:51 -070043def run_tcp_server(ip, port):
44 print("Starting TCP server {}:{}".format(repr(ip), repr(port)))
Florin Corase04c2992017-03-01 08:17:34 -080045 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Corasd79b41e2017-03-04 05:37:52 -080046 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Florin Corase04c2992017-03-01 08:17:34 -080047 server_address = (ip, int(port))
48 sock.bind(server_address)
49 sock.listen(1)
Florin Corase04c2992017-03-01 08:17:34 -080050 while True:
51 connection, client_address = sock.accept()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 handle_connection(connection, client_address)
53
54
Florin Coras3cbc04b2017-10-02 00:18:51 -070055def run_udp_server(ip, port):
56 print("Starting UDP server {}:{}".format(repr(ip), repr(port)))
57 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
58 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
59 server_address = (ip, int(port))
60 sock.bind(server_address)
61 while True:
62 data, addr = sock.recvfrom(4096)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020063 if action != "drop":
64 # snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
65 sock.sendto(data, addr)
66
Florin Corase04c2992017-03-01 08:17:34 -080067
Florin Coras3cbc04b2017-10-02 00:18:51 -070068def run_server(ip, port, proto):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020069 if proto == "tcp":
Florin Coras3cbc04b2017-10-02 00:18:51 -070070 run_tcp_server(ip, port)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 elif proto == "udp":
Florin Coras3cbc04b2017-10-02 00:18:51 -070072 run_udp_server(ip, port)
73
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020074
Florin Coras3cbc04b2017-10-02 00:18:51 -070075def prepare_data(power):
Florin Corase04c2992017-03-01 08:17:34 -080076 buf = []
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 for i in range(0, pow(2, power)):
78 buf.append(i & 0xFF)
Florin Corase04c2992017-03-01 08:17:34 -080079 return bytearray(buf)
80
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081
Florin Coras3cbc04b2017-10-02 00:18:51 -070082def run_tcp_client(ip, port):
83 print("Starting TCP client {}:{}".format(repr(ip), repr(port)))
Florin Corase04c2992017-03-01 08:17:34 -080084 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Coras3cbc04b2017-10-02 00:18:51 -070085 server_address = (ip, int(port))
Florin Corase04c2992017-03-01 08:17:34 -080086 sock.connect(server_address)
Florin Coras3cbc04b2017-10-02 00:18:51 -070087
88 data = prepare_data(16)
Florin Corasd79b41e2017-03-04 05:37:52 -080089 n_rcvd = 0
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020090 n_sent = len(data)
Florin Corase04c2992017-03-01 08:17:34 -080091 try:
92 sock.sendall(data)
Florin Coras3cbc04b2017-10-02 00:18:51 -070093
Florin Corasd79b41e2017-03-04 05:37:52 -080094 timeout = time.time() + 2
95 while n_rcvd < n_sent and time.time() < timeout:
96 tmp = sock.recv(1500)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 tmp = bytearray(tmp)
Florin Corasd79b41e2017-03-04 05:37:52 -080098 n_read = len(tmp)
99 for i in range(n_read):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200100 if data[n_rcvd + i] != tmp[i]:
101 print(
102 "Difference at byte {}. Sent {} got {}".format(
103 n_rcvd + i, data[n_rcvd + i], tmp[i]
104 )
105 )
Florin Corasd79b41e2017-03-04 05:37:52 -0800106 n_rcvd += n_read
Florin Coras3cbc04b2017-10-02 00:18:51 -0700107
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200108 if n_rcvd < n_sent or n_rcvd > n_sent:
Florin Corasd79b41e2017-03-04 05:37:52 -0800109 print("Sent {} and got back {}".format(n_sent, n_rcvd))
110 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 print("Got back what we've sent!!")
Florin Coras3cbc04b2017-10-02 00:18:51 -0700112
Florin Corase04c2992017-03-01 08:17:34 -0800113 finally:
114 sock.close()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115
116
Florin Coras3cbc04b2017-10-02 00:18:51 -0700117def run_udp_client(ip, port):
118 print("Starting UDP client {}:{}".format(repr(ip), repr(port)))
119 n_packets = 100
120 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
121 server_address = (ip, int(port))
122 data = prepare_data(10)
123 try:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 for i in range(0, n_packets):
Florin Coras3cbc04b2017-10-02 00:18:51 -0700125 sock.sendto(data, server_address)
126 finally:
127 sock.close()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128
129
Florin Coras3cbc04b2017-10-02 00:18:51 -0700130def run_client(ip, port, proto):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200131 if proto == "tcp":
Florin Coras3cbc04b2017-10-02 00:18:51 -0700132 run_tcp_client(ip, port)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 elif proto == "udp":
Florin Coras3cbc04b2017-10-02 00:18:51 -0700134 run_udp_client(ip, port)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200135
136
Florin Coras3cbc04b2017-10-02 00:18:51 -0700137def run(mode, ip, port, proto):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200138 if mode == "server":
139 run_server(ip, port, proto)
140 elif mode == "client":
141 run_client(ip, port, proto)
Florin Corase04c2992017-03-01 08:17:34 -0800142 else:
143 raise Exception("Unknown mode. Only client and server supported")
144
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145
Florin Corase04c2992017-03-01 08:17:34 -0800146if __name__ == "__main__":
Florin Coras3cbc04b2017-10-02 00:18:51 -0700147 parser = argparse.ArgumentParser()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 parser.add_argument("-m", action="store", dest="mode")
149 parser.add_argument("-i", action="store", dest="ip")
150 parser.add_argument("-p", action="store", dest="port")
151 parser.add_argument("-proto", action="store", dest="proto")
152 parser.add_argument("-a", action="store", dest="action")
153 parser.add_argument("-t", action="store", dest="test")
Florin Coras3cbc04b2017-10-02 00:18:51 -0700154 results = parser.parse_args()
155 action = results.action
156 test = results.test
157 run(results.mode, results.ip, results.port, results.proto)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200158 # if (len(sys.argv)) < 4:
Florin Coras3cbc04b2017-10-02 00:18:51 -0700159 # raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200160 # if (len(sys.argv) == 6):
Florin Coras3cbc04b2017-10-02 00:18:51 -0700161 # action = sys.argv[4]
162 # test = int(sys.argv[5])
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 # run (sys.argv[1], sys.argv[2], int(sys.argv[3]))