fixed buffer management in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation
Fork of NSAPITests by
EchoServer.py@13:950327f445a3, 2016-10-26 (annotated)
- Committer:
- mapellil
- Date:
- Wed Oct 26 09:52:29 2016 +0000
- Revision:
- 13:950327f445a3
- Parent:
- 12:152ae238ddc1
Fixed buffer wraparound in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
4:b52f17273177 | 1 | #!/usr/bin/env python |
Christopher Haster |
4:b52f17273177 | 2 | import socket |
Christopher Haster |
4:b52f17273177 | 3 | import signal |
Christopher Haster |
4:b52f17273177 | 4 | import select |
Christopher Haster |
4:b52f17273177 | 5 | |
Christopher Haster |
4:b52f17273177 | 6 | |
Christopher Haster |
4:b52f17273177 | 7 | def main(port): |
Christopher Haster |
4:b52f17273177 | 8 | port = int(port) |
Christopher Haster |
4:b52f17273177 | 9 | |
Christopher Haster |
4:b52f17273177 | 10 | udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
mridup | 12:152ae238ddc1 | 11 | udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Christopher Haster |
4:b52f17273177 | 12 | udp.bind(('', port)) |
mridup | 12:152ae238ddc1 | 13 | udp.setblocking(0) |
Christopher Haster |
4:b52f17273177 | 14 | |
Christopher Haster |
4:b52f17273177 | 15 | tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
mridup | 12:152ae238ddc1 | 16 | tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Christopher Haster |
4:b52f17273177 | 17 | tcp.bind(('', port)) |
Christopher Haster |
4:b52f17273177 | 18 | tcp.setblocking(0) |
Christopher Haster |
4:b52f17273177 | 19 | |
c1728p9 | 11:fbfe3498404a | 20 | udp6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) |
mridup | 12:152ae238ddc1 | 21 | udp6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
c1728p9 | 11:fbfe3498404a | 22 | udp6.bind(('', port)) |
mridup | 12:152ae238ddc1 | 23 | udp6.setblocking(0) |
c1728p9 | 11:fbfe3498404a | 24 | |
c1728p9 | 11:fbfe3498404a | 25 | tcp6 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
mridup | 12:152ae238ddc1 | 26 | tcp6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
c1728p9 | 11:fbfe3498404a | 27 | tcp6.bind(('', port)) |
c1728p9 | 11:fbfe3498404a | 28 | tcp6.setblocking(0) |
c1728p9 | 11:fbfe3498404a | 29 | |
Christopher Haster |
4:b52f17273177 | 30 | print "running on port %d" % port |
c1728p9 | 11:fbfe3498404a | 31 | sockets = [tcp, udp, tcp6, udp6] |
Christopher Haster |
4:b52f17273177 | 32 | clients = [] |
Christopher Haster |
4:b52f17273177 | 33 | |
Christopher Haster |
4:b52f17273177 | 34 | while True: |
Christopher Haster |
4:b52f17273177 | 35 | select.select(sockets, [], []) |
Christopher Haster |
4:b52f17273177 | 36 | |
Christopher Haster |
4:b52f17273177 | 37 | try: |
Christopher Haster |
4:b52f17273177 | 38 | data, addr = udp.recvfrom(1 << 12) |
Christopher Haster |
4:b52f17273177 | 39 | print 'udp %s:%d %d' % (addr[0], addr[1], len(data)) |
Christopher Haster |
4:b52f17273177 | 40 | udp.sendto(data, addr) |
Christopher Haster |
4:b52f17273177 | 41 | except socket.error: |
Christopher Haster |
4:b52f17273177 | 42 | pass |
Christopher Haster |
4:b52f17273177 | 43 | |
Christopher Haster |
4:b52f17273177 | 44 | try: |
mridup | 12:152ae238ddc1 | 45 | tcp.listen(5) |
Christopher Haster |
4:b52f17273177 | 46 | client, addr = tcp.accept() |
Christopher Haster |
4:b52f17273177 | 47 | print 'tcp %s:%d connect' % (addr[0], addr[1]) |
Christopher Haster |
4:b52f17273177 | 48 | client.setblocking(0) |
Christopher Haster |
4:b52f17273177 | 49 | client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Christopher Haster |
4:b52f17273177 | 50 | sockets.append(client) |
Christopher Haster |
4:b52f17273177 | 51 | clients.append((client, addr)) |
Christopher Haster |
4:b52f17273177 | 52 | except socket.error: |
Christopher Haster |
4:b52f17273177 | 53 | pass |
Christopher Haster |
4:b52f17273177 | 54 | |
c1728p9 | 11:fbfe3498404a | 55 | try: |
c1728p9 | 11:fbfe3498404a | 56 | data, addr = udp6.recvfrom(1 << 12) |
c1728p9 | 11:fbfe3498404a | 57 | print 'udp6 %s:%d %d' % (addr[0], addr[1], len(data)) |
c1728p9 | 11:fbfe3498404a | 58 | udp6.sendto(data, addr) |
c1728p9 | 11:fbfe3498404a | 59 | except socket.error: |
c1728p9 | 11:fbfe3498404a | 60 | pass |
c1728p9 | 11:fbfe3498404a | 61 | |
c1728p9 | 11:fbfe3498404a | 62 | try: |
mridup | 12:152ae238ddc1 | 63 | tcp6.listen(5) |
c1728p9 | 11:fbfe3498404a | 64 | client, addr = tcp6.accept() |
c1728p9 | 11:fbfe3498404a | 65 | print 'tcp6 %s:%d connect' % (addr[0], addr[1]) |
c1728p9 | 11:fbfe3498404a | 66 | client.setblocking(0) |
c1728p9 | 11:fbfe3498404a | 67 | client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
c1728p9 | 11:fbfe3498404a | 68 | sockets.append(client) |
c1728p9 | 11:fbfe3498404a | 69 | clients.append((client, addr)) |
c1728p9 | 11:fbfe3498404a | 70 | except socket.error: |
c1728p9 | 11:fbfe3498404a | 71 | pass |
c1728p9 | 11:fbfe3498404a | 72 | |
Christopher Haster |
4:b52f17273177 | 73 | for client, addr in clients: |
Christopher Haster |
4:b52f17273177 | 74 | try: |
Christopher Haster |
4:b52f17273177 | 75 | data = client.recv(1 << 12) |
Christopher Haster |
4:b52f17273177 | 76 | if data: |
Christopher Haster |
4:b52f17273177 | 77 | print 'tcp %s:%d %d' % (addr[0], addr[1], len(data)) |
Christopher Haster |
4:b52f17273177 | 78 | client.send(data) |
Christopher Haster |
4:b52f17273177 | 79 | else: |
Christopher Haster |
4:b52f17273177 | 80 | print 'tcp %s:%d disconnect' % (addr[0], addr[1]) |
Christopher Haster |
4:b52f17273177 | 81 | sockets.remove(client) |
Christopher Haster |
4:b52f17273177 | 82 | clients.remove((client, addr)) |
Christopher Haster |
4:b52f17273177 | 83 | client.close() |
Christopher Haster |
4:b52f17273177 | 84 | except socket.error: |
Christopher Haster |
4:b52f17273177 | 85 | pass |
Christopher Haster |
4:b52f17273177 | 86 | |
Christopher Haster |
4:b52f17273177 | 87 | |
Christopher Haster |
4:b52f17273177 | 88 | if __name__ == "__main__": |
Christopher Haster |
4:b52f17273177 | 89 | import sys |
Christopher Haster |
4:b52f17273177 | 90 | main(*sys.argv[1:]) |