[20] | 1 | |
---|
| 2 | import sys |
---|
| 3 | import socket |
---|
| 4 | import queue |
---|
| 5 | import statistics |
---|
| 6 | import threading |
---|
| 7 | import json |
---|
| 8 | from .stats import OLSRegression |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | def trickleHTTPRequest(ip,port,hostname): |
---|
| 12 | my_port = None |
---|
| 13 | try: |
---|
| 14 | sock = socket.create_connection((ip, port)) |
---|
| 15 | my_port = sock.getsockname()[1] |
---|
| 16 | |
---|
| 17 | #print('.') |
---|
| 18 | sock.sendall(b'GET / HTTP/1.1\r\n') |
---|
| 19 | time.sleep(0.5) |
---|
| 20 | rest = b'''Host: '''+hostname.encode('utf-8')+b'''\r\nUser-Agent: Secret Agent Man\r\nX-Extra: extra read all about it!\r\nConnection: close\r\n''' |
---|
| 21 | for r in rest: |
---|
| 22 | sock.sendall(bytearray([r])) |
---|
| 23 | time.sleep(0.05) |
---|
| 24 | |
---|
| 25 | time.sleep(0.5) |
---|
| 26 | sock.sendall('\r\n') |
---|
| 27 | |
---|
| 28 | r = None |
---|
| 29 | while r != b'': |
---|
| 30 | r = sock.recv(16) |
---|
| 31 | |
---|
| 32 | sock.close() |
---|
| 33 | except Exception as e: |
---|
| 34 | pass |
---|
| 35 | |
---|
| 36 | return my_port |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | def runTimestampProbes(host_ip, port, hostname, num_trials, concurrency=4): |
---|
| 40 | #XXX: can we use WorkerThreads for this parallel stuff? |
---|
| 41 | myq = queue.Queue() |
---|
| 42 | def threadWrapper(*args): |
---|
| 43 | try: |
---|
| 44 | myq.put(trickleHTTPRequest(*args)) |
---|
| 45 | except Exception as e: |
---|
| 46 | sys.stderr.write("ERROR from trickleHTTPRequest: %s\n" % repr(e)) |
---|
| 47 | myq.put(None) |
---|
| 48 | |
---|
| 49 | threads = [] |
---|
| 50 | ports = [] |
---|
| 51 | for i in range(num_trials): |
---|
| 52 | if len(threads) >= concurrency: |
---|
| 53 | ports.append(myq.get()) |
---|
| 54 | t = threading.Thread(target=threadWrapper, args=(host_ip, port, hostname)) |
---|
| 55 | t.start() |
---|
| 56 | threads.append(t) |
---|
| 57 | |
---|
| 58 | for t in threads: |
---|
| 59 | t.join() |
---|
| 60 | |
---|
| 61 | while myq.qsize() > 0: |
---|
| 62 | ports.append(myq.get()) |
---|
| 63 | |
---|
| 64 | return ports |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | def computeTimestampPrecision(sniffer_fp, ports): |
---|
| 68 | rcvd = [] |
---|
| 69 | for line in sniffer_fp: |
---|
| 70 | p = json.loads(line) |
---|
| 71 | if p['sent']==0: |
---|
| 72 | rcvd.append((p['observed'],p['tsval'],int(p['local_port']))) |
---|
| 73 | |
---|
| 74 | slopes = [] |
---|
| 75 | for port in ports: |
---|
| 76 | trcvd = [tr for tr in rcvd if tr[2]==port and tr[1]!=0] |
---|
| 77 | |
---|
| 78 | if len(trcvd) < 2: |
---|
| 79 | sys.stderr.write("WARN: Inadequate data points.\n") |
---|
| 80 | continue |
---|
| 81 | |
---|
| 82 | if trcvd[0][1] > trcvd[-1][1]: |
---|
| 83 | sys.stderr.write("WARN: TSval wrap.\n") |
---|
| 84 | continue |
---|
| 85 | |
---|
| 86 | x = [tr[1] for tr in trcvd] |
---|
| 87 | y = [tr[0] for tr in trcvd] |
---|
| 88 | |
---|
| 89 | slope,intercept = OLSRegression(x, y) |
---|
| 90 | slopes.append(slope) |
---|
| 91 | |
---|
| 92 | if len(slopes) == 0: |
---|
| 93 | return None,None,None |
---|
| 94 | |
---|
| 95 | m = statistics.mean(slopes) |
---|
| 96 | if len(slopes) == 1: |
---|
| 97 | return (m, None, slopes) |
---|
| 98 | else: |
---|
| 99 | return (m, statistics.stdev(slopes), slopes) |
---|