Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 2 | |
| 3 | import socket |
| 4 | import sys |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 5 | import time |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 6 | import argparse |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 7 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 8 | # action can be reflect or drop |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 9 | action = "drop" |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 10 | test = 0 |
| 11 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 12 | |
| 13 | def test_data(data, n_rcvd): |
| 14 | n_read = len(data) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 15 | for i in range(n_read): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 16 | 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 Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 24 | return n_read |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 25 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 26 | |
| 27 | def handle_connection(connection, client_address): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 28 | print("Received connection from {}".format(repr(client_address))) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 29 | n_rcvd = 0 |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 30 | try: |
| 31 | while True: |
| 32 | data = connection.recv(4096) |
| 33 | if not data: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 34 | break |
| 35 | if test == 1: |
| 36 | n_rcvd += test_data(data, n_rcvd) |
| 37 | if action != "drop": |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 38 | connection.sendall(data) |
| 39 | finally: |
| 40 | connection.close() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 41 | |
| 42 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 43 | def run_tcp_server(ip, port): |
| 44 | print("Starting TCP server {}:{}".format(repr(ip), repr(port))) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 45 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 46 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 47 | server_address = (ip, int(port)) |
| 48 | sock.bind(server_address) |
| 49 | sock.listen(1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 50 | while True: |
| 51 | connection, client_address = sock.accept() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 52 | handle_connection(connection, client_address) |
| 53 | |
| 54 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 55 | def 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 63 | if action != "drop": |
| 64 | # snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 65 | sock.sendto(data, addr) |
| 66 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 67 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 68 | def run_server(ip, port, proto): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 69 | if proto == "tcp": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 70 | run_tcp_server(ip, port) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | elif proto == "udp": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 72 | run_udp_server(ip, port) |
| 73 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 74 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 75 | def prepare_data(power): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 76 | buf = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 77 | for i in range(0, pow(2, power)): |
| 78 | buf.append(i & 0xFF) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 79 | return bytearray(buf) |
| 80 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 81 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 82 | def run_tcp_client(ip, port): |
| 83 | print("Starting TCP client {}:{}".format(repr(ip), repr(port))) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 84 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 85 | server_address = (ip, int(port)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 86 | sock.connect(server_address) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 87 | |
| 88 | data = prepare_data(16) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 89 | n_rcvd = 0 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 90 | n_sent = len(data) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 91 | try: |
| 92 | sock.sendall(data) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 93 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 94 | timeout = time.time() + 2 |
| 95 | while n_rcvd < n_sent and time.time() < timeout: |
| 96 | tmp = sock.recv(1500) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 97 | tmp = bytearray(tmp) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 98 | n_read = len(tmp) |
| 99 | for i in range(n_read): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 100 | 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 Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 106 | n_rcvd += n_read |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 107 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 108 | if n_rcvd < n_sent or n_rcvd > n_sent: |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 109 | print("Sent {} and got back {}".format(n_sent, n_rcvd)) |
| 110 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | print("Got back what we've sent!!") |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 112 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 113 | finally: |
| 114 | sock.close() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 115 | |
| 116 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 117 | def 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 124 | for i in range(0, n_packets): |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 125 | sock.sendto(data, server_address) |
| 126 | finally: |
| 127 | sock.close() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 128 | |
| 129 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 130 | def run_client(ip, port, proto): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 131 | if proto == "tcp": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 132 | run_tcp_client(ip, port) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 133 | elif proto == "udp": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 134 | run_udp_client(ip, port) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 135 | |
| 136 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 137 | def run(mode, ip, port, proto): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 138 | if mode == "server": |
| 139 | run_server(ip, port, proto) |
| 140 | elif mode == "client": |
| 141 | run_client(ip, port, proto) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 142 | else: |
| 143 | raise Exception("Unknown mode. Only client and server supported") |
| 144 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 145 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 146 | if __name__ == "__main__": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 147 | parser = argparse.ArgumentParser() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 148 | 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 Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 154 | results = parser.parse_args() |
| 155 | action = results.action |
| 156 | test = results.test |
| 157 | run(results.mode, results.ip, results.port, results.proto) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 158 | # if (len(sys.argv)) < 4: |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 159 | # raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]") |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 160 | # if (len(sys.argv) == 6): |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 161 | # action = sys.argv[4] |
| 162 | # test = int(sys.argv[5]) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 163 | # run (sys.argv[1], sys.argv[2], int(sys.argv[3])) |