diff --git a/example.py b/example.py index ab99369..1ae6192 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,7 @@ import masstraceroute +import logging + +logging.basicConfig(level=logging.DEBUG) targets = ["8.8.8.8", "9.9.9.9", "31.13.64.35"] t = masstraceroute.MassTraceroute(10, 60, 2, targets, 30) diff --git a/masstraceroute/__init__.py b/masstraceroute/__init__.py index f1bcdcf..e62e3ed 100644 --- a/masstraceroute/__init__.py +++ b/masstraceroute/__init__.py @@ -11,23 +11,17 @@ class MassTraceroute: def __init__(self, socket_timeout, listener_timeout, threads, targets, maxhops): logging.info("Setting up class") self.wait = 0.2 - self.maxhops = maxhops + self.maxhops = maxhops + 1 self.listener_timeout = listener_timeout self.socket_timeout = socket_timeout self.targets = targets self.threads = threads self.pool = ThreadPool(processes=self.threads) self.routes = dict() - logging.info("creating listener socket") + logging.info("Creating listener socket") self.listener = self.init_listener() self.sources = dict() self.last_send = 0 - self.sender = dict() - for thread in range(threads): - logging.info("Creating #{} sender socket".format(thread)) - self.sender[thread] = dict() - self.sender[thread]['socket'] = self.init_sender() - self.sender[thread]['lock'] = Lock() def main(self): logging.info("Starting listener thread") @@ -46,11 +40,15 @@ class MassTraceroute: return listener def process_listener(self): - while((self.last_send + self.listener_timeout) > time()): + while((self.last_send + self.listener_timeout) < time()): + logging.debug("Listening...") try: data, addr = self.listener.recvfrom(1024) except socket.error as e: pass + logging.debug("Got data from ip = {}".format(addr)) + logging.debug(data) + logging.debug(data[48:49]) srcport = int.from_bytes(data[48:49], byteorder="big") dstport = int.from_bytes(data[50:51], byteorder="big") if srcport in self.sources and dstport in self.sources[srcport]: @@ -61,6 +59,10 @@ class MassTraceroute: logging.error("Received package with srcport or dstport non existsant?") def process_sender(self): + logging.info("Starting process_sender") + #for target in self.targets: + # self.send(target) + self.pool.map(self.send, self.targets) @@ -73,32 +75,29 @@ class MassTraceroute: return sender def send(self, dstip): - lock = False - while(not lock): - for thread in range(self.threads): - if self.sender[thread]["lock"].acquire(): - break - sleep(self.wait) - + logging.info("Entering send()") + sender = self.init_sender() srcport = getrandbits(16) while(srcport not in self.sources): + self.sources[srcport] = dict() + self.sources[srcport]["ip"] = dstip srcport = getrandbits(16) - self.sources[srcport]["ip"] = dstip + logging.info("Binding sending socket") + sender.bind(('', srcport)) - self.sender[thread]["socket"].bind(('', srcport)) - - for ttl in range(self.maxhops): - self.sender[thread]["socket"].setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) + for ttl in range(1, self.maxhops): + + sender.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) dstport = getrandbits(16) - while(dstport not in self.sources["srcport"]): + while(dstport not in self.sources[srcport]): + self.sources[srcport][dstport] = ttl dstport = getrandbits(16) - self.sources["srcport"][dstport] = ttl logging.debug("Sending packet srcport = {}, dstport = {}, ttl = {}, dstip = {}".format(srcport, dstport, ttl, dstip)) sender.sendto(b'', (dstip, dstport)) self.last_send = time() - self.thread[sender]["lock"].release() + sender.close()