blob: 85ea4dcd4749296f9223aa404483f9aae0ec32c1 [file] [log] [blame]
Florin Corase04c2992017-03-01 08:17:34 -08001#!/usr/bin/env python
2
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
8# action can be reflect or drop
9action = "drop"
Florin Corasf6d68ed2017-05-07 19:12:02 -070010test = 0
11
12def test_data (data, n_rcvd):
13 n_read = len (data);
14 for i in range(n_read):
15 expected = (n_rcvd + i) & 0xff
16 byte_got = ord (data[i])
17 if (byte_got != expected):
18 print("Difference at byte {}. Expected {} got {}"
19 .format(n_rcvd + i, expected, byte_got))
20 return n_read
Florin Corase04c2992017-03-01 08:17:34 -080021
22def handle_connection (connection, client_address):
23 print("Received connection from {}".format(repr(client_address)))
Florin Corasf6d68ed2017-05-07 19:12:02 -070024 n_rcvd = 0
Florin Corase04c2992017-03-01 08:17:34 -080025 try:
26 while True:
27 data = connection.recv(4096)
28 if not data:
29 break;
Florin Corasf6d68ed2017-05-07 19:12:02 -070030 if (test == 1):
31 n_rcvd += test_data (data, n_rcvd)
Florin Corase04c2992017-03-01 08:17:34 -080032 if (action != "drop"):
33 connection.sendall(data)
34 finally:
35 connection.close()
Florin Coras3cbc04b2017-10-02 00:18:51 -070036def run_tcp_server(ip, port):
37 print("Starting TCP server {}:{}".format(repr(ip), repr(port)))
Florin Corase04c2992017-03-01 08:17:34 -080038 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Corasd79b41e2017-03-04 05:37:52 -080039 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Florin Corase04c2992017-03-01 08:17:34 -080040 server_address = (ip, int(port))
41 sock.bind(server_address)
42 sock.listen(1)
Florin Corase04c2992017-03-01 08:17:34 -080043 while True:
44 connection, client_address = sock.accept()
45 handle_connection (connection, client_address)
Florin Coras3cbc04b2017-10-02 00:18:51 -070046def run_udp_server(ip, port):
47 print("Starting UDP server {}:{}".format(repr(ip), repr(port)))
48 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
49 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
50 server_address = (ip, int(port))
51 sock.bind(server_address)
52 while True:
53 data, addr = sock.recvfrom(4096)
54 if (action != "drop"):
55 #snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
56 sock.sendto (data, addr)
Florin Corase04c2992017-03-01 08:17:34 -080057
Florin Coras3cbc04b2017-10-02 00:18:51 -070058def run_server(ip, port, proto):
59 if (proto == "tcp"):
60 run_tcp_server(ip, port)
61 elif (proto == "udp"):
62 run_udp_server(ip, port)
63
64def prepare_data(power):
Florin Corase04c2992017-03-01 08:17:34 -080065 buf = []
Florin Coras3cbc04b2017-10-02 00:18:51 -070066 for i in range (0, pow(2, power)):
Florin Corase04c2992017-03-01 08:17:34 -080067 buf.append(i & 0xff)
68 return bytearray(buf)
69
Florin Coras3cbc04b2017-10-02 00:18:51 -070070def run_tcp_client(ip, port):
71 print("Starting TCP client {}:{}".format(repr(ip), repr(port)))
Florin Corase04c2992017-03-01 08:17:34 -080072 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Florin Coras3cbc04b2017-10-02 00:18:51 -070073 server_address = (ip, int(port))
Florin Corase04c2992017-03-01 08:17:34 -080074 sock.connect(server_address)
Florin Coras3cbc04b2017-10-02 00:18:51 -070075
76 data = prepare_data(16)
Florin Corasd79b41e2017-03-04 05:37:52 -080077 n_rcvd = 0
78 n_sent = len (data)
Florin Corase04c2992017-03-01 08:17:34 -080079 try:
80 sock.sendall(data)
Florin Coras3cbc04b2017-10-02 00:18:51 -070081
Florin Corasd79b41e2017-03-04 05:37:52 -080082 timeout = time.time() + 2
83 while n_rcvd < n_sent and time.time() < timeout:
84 tmp = sock.recv(1500)
85 tmp = bytearray (tmp)
86 n_read = len(tmp)
87 for i in range(n_read):
88 if (data[n_rcvd + i] != tmp[i]):
89 print("Difference at byte {}. Sent {} got {}"
90 .format(n_rcvd + i, data[n_rcvd + i], tmp[i]))
91 n_rcvd += n_read
Florin Coras3cbc04b2017-10-02 00:18:51 -070092
Florin Corasd79b41e2017-03-04 05:37:52 -080093 if (n_rcvd < n_sent or n_rcvd > n_sent):
94 print("Sent {} and got back {}".format(n_sent, n_rcvd))
95 else:
96 print("Got back what we've sent!!");
Florin Coras3cbc04b2017-10-02 00:18:51 -070097
Florin Corase04c2992017-03-01 08:17:34 -080098 finally:
99 sock.close()
Florin Coras3cbc04b2017-10-02 00:18:51 -0700100def run_udp_client(ip, port):
101 print("Starting UDP client {}:{}".format(repr(ip), repr(port)))
102 n_packets = 100
103 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
104 server_address = (ip, int(port))
105 data = prepare_data(10)
106 try:
107 for i in range (0, n_packets):
108 sock.sendto(data, server_address)
109 finally:
110 sock.close()
111def run_client(ip, port, proto):
112 if (proto == "tcp"):
113 run_tcp_client(ip, port)
114 elif (proto == "udp"):
115 run_udp_client(ip, port)
116def run(mode, ip, port, proto):
Florin Corase04c2992017-03-01 08:17:34 -0800117 if (mode == "server"):
Florin Coras3cbc04b2017-10-02 00:18:51 -0700118 run_server (ip, port, proto)
Florin Corase04c2992017-03-01 08:17:34 -0800119 elif (mode == "client"):
Florin Coras3cbc04b2017-10-02 00:18:51 -0700120 run_client (ip, port, proto)
Florin Corase04c2992017-03-01 08:17:34 -0800121 else:
122 raise Exception("Unknown mode. Only client and server supported")
123
124if __name__ == "__main__":
Florin Coras3cbc04b2017-10-02 00:18:51 -0700125 parser = argparse.ArgumentParser()
126 parser.add_argument('-m', action='store', dest='mode')
127 parser.add_argument('-i', action='store', dest='ip')
128 parser.add_argument('-p', action='store', dest='port')
129 parser.add_argument('-proto', action='store', dest='proto')
130 parser.add_argument('-a', action='store', dest='action')
131 parser.add_argument('-t', action='store', dest='test')
132 results = parser.parse_args()
133 action = results.action
134 test = results.test
135 run(results.mode, results.ip, results.port, results.proto)
136 #if (len(sys.argv)) < 4:
137 # raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]")
138 #if (len(sys.argv) == 6):
139 # action = sys.argv[4]
140 # test = int(sys.argv[5])
141 #run (sys.argv[1], sys.argv[2], int(sys.argv[3]))