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 | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 6 | |
| 7 | # action can be reflect or drop |
| 8 | action = "drop" |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 9 | test = 0 |
| 10 | |
| 11 | def test_data (data, n_rcvd): |
| 12 | n_read = len (data); |
| 13 | for i in range(n_read): |
| 14 | expected = (n_rcvd + i) & 0xff |
| 15 | byte_got = ord (data[i]) |
| 16 | if (byte_got != expected): |
| 17 | print("Difference at byte {}. Expected {} got {}" |
| 18 | .format(n_rcvd + i, expected, byte_got)) |
| 19 | return n_read |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 20 | |
| 21 | def handle_connection (connection, client_address): |
| 22 | print("Received connection from {}".format(repr(client_address))) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 23 | n_rcvd = 0 |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 24 | try: |
| 25 | while True: |
| 26 | data = connection.recv(4096) |
| 27 | if not data: |
| 28 | break; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 29 | if (test == 1): |
| 30 | n_rcvd += test_data (data, n_rcvd) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 31 | if (action != "drop"): |
| 32 | connection.sendall(data) |
| 33 | finally: |
| 34 | connection.close() |
| 35 | |
| 36 | def run_server(ip, port): |
| 37 | print("Starting server {}:{}".format(repr(ip), repr(port))) |
| 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) |
| 43 | |
| 44 | while True: |
| 45 | connection, client_address = sock.accept() |
| 46 | handle_connection (connection, client_address) |
| 47 | |
| 48 | def prepare_data(): |
| 49 | buf = [] |
| 50 | for i in range (0, pow(2, 16)): |
| 51 | buf.append(i & 0xff) |
| 52 | return bytearray(buf) |
| 53 | |
| 54 | def run_client(ip, port): |
| 55 | print("Starting client {}:{}".format(repr(ip), repr(port))) |
| 56 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 57 | server_address = (ip, port) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 58 | sock.connect(server_address) |
| 59 | |
| 60 | data = prepare_data() |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 61 | n_rcvd = 0 |
| 62 | n_sent = len (data) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 63 | try: |
| 64 | sock.sendall(data) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 65 | |
| 66 | timeout = time.time() + 2 |
| 67 | while n_rcvd < n_sent and time.time() < timeout: |
| 68 | tmp = sock.recv(1500) |
| 69 | tmp = bytearray (tmp) |
| 70 | n_read = len(tmp) |
| 71 | for i in range(n_read): |
| 72 | if (data[n_rcvd + i] != tmp[i]): |
| 73 | print("Difference at byte {}. Sent {} got {}" |
| 74 | .format(n_rcvd + i, data[n_rcvd + i], tmp[i])) |
| 75 | n_rcvd += n_read |
| 76 | |
| 77 | if (n_rcvd < n_sent or n_rcvd > n_sent): |
| 78 | print("Sent {} and got back {}".format(n_sent, n_rcvd)) |
| 79 | else: |
| 80 | print("Got back what we've sent!!"); |
| 81 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 82 | finally: |
| 83 | sock.close() |
| 84 | |
| 85 | def run(mode, ip, port): |
| 86 | if (mode == "server"): |
| 87 | run_server (ip, port) |
| 88 | elif (mode == "client"): |
| 89 | run_client (ip, port) |
| 90 | else: |
| 91 | raise Exception("Unknown mode. Only client and server supported") |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | if (len(sys.argv)) < 4: |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 95 | raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]") |
| 96 | if (len(sys.argv) == 6): |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 97 | action = sys.argv[4] |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 98 | test = int(sys.argv[5]) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 99 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 100 | run (sys.argv[1], sys.argv[2], int(sys.argv[3])) |