Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 8 | # action can be reflect or drop |
| 9 | action = "drop" |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 10 | test = 0 |
| 11 | |
| 12 | def 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 Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 21 | |
| 22 | def handle_connection (connection, client_address): |
| 23 | print("Received connection from {}".format(repr(client_address))) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 24 | n_rcvd = 0 |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 25 | try: |
| 26 | while True: |
| 27 | data = connection.recv(4096) |
| 28 | if not data: |
| 29 | break; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 30 | if (test == 1): |
| 31 | n_rcvd += test_data (data, n_rcvd) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 32 | if (action != "drop"): |
| 33 | connection.sendall(data) |
| 34 | finally: |
| 35 | connection.close() |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 36 | def run_tcp_server(ip, port): |
| 37 | print("Starting TCP server {}:{}".format(repr(ip), repr(port))) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 38 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 39 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 40 | server_address = (ip, int(port)) |
| 41 | sock.bind(server_address) |
| 42 | sock.listen(1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 43 | while True: |
| 44 | connection, client_address = sock.accept() |
| 45 | handle_connection (connection, client_address) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 46 | def 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 Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 57 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 58 | def 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 | |
| 64 | def prepare_data(power): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 65 | buf = [] |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 66 | for i in range (0, pow(2, power)): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 67 | buf.append(i & 0xff) |
| 68 | return bytearray(buf) |
| 69 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 70 | def run_tcp_client(ip, port): |
| 71 | print("Starting TCP client {}:{}".format(repr(ip), repr(port))) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 72 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 73 | server_address = (ip, int(port)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 74 | sock.connect(server_address) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 75 | |
| 76 | data = prepare_data(16) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 77 | n_rcvd = 0 |
| 78 | n_sent = len (data) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 79 | try: |
| 80 | sock.sendall(data) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 81 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 82 | 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 Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 92 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 93 | 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 Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 97 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 98 | finally: |
| 99 | sock.close() |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 100 | def 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() |
| 111 | def 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) |
| 116 | def run(mode, ip, port, proto): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 117 | if (mode == "server"): |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 118 | run_server (ip, port, proto) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 119 | elif (mode == "client"): |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 120 | run_client (ip, port, proto) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 121 | else: |
| 122 | raise Exception("Unknown mode. Only client and server supported") |
| 123 | |
| 124 | if __name__ == "__main__": |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 125 | 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])) |