def route(self, dest, verbose=None):
        if type(dest) is list and dest:
            dest = dest[0]
        if dest in self.cache:
            return self.cache[dest]
        if verbose is None:
            verbose = conf.verb
        # Transform "192.168.*.1-5" to one IP of the set
        dst = dest.split("/")[0]
        dst = dst.replace("*", "0")
        while 1:
            l = dst.find("-")
            if l < 0:
                break
            m = (dst[l:] + ".").find(".")
            dst = dst[:l] + dst[l + m:]

        dst = atol(dst)
        pathes = []
        for d, m, gw, i, a in self.routes:
            aa = atol(a)
            if aa == dst:
                pathes.append((0xffffffffL, (LOOPBACK_NAME, a, "0.0.0.0")))
            if (dst & m) == (d & m):
                pathes.append((m, (i, a, gw)))
        if not pathes:
            if verbose:
                warning("No route found (no default route?)")
            return LOOPBACK_NAME, "0.0.0.0", "0.0.0.0"  #XXX linux specific!
        # Choose the more specific route (greatest netmask).
        # XXX: we don't care about metrics
        pathes.sort()
        ret = pathes[-1][1]
        self.cache[dest] = ret
        return ret
Ejemplo n.º 2
0
    def route(self, dest, verbose=None):
        if dest in self.cache:
            return self.cache[dest]
        if verbose is None:
            verbose = conf.verb
        # Transform "192.168.*.1-5" to one IP of the set
        dst = dest.split("/")[0]
        dst = dst.replace("*", "0")
        while 1:
            l = dst.find("-")
            if l < 0:
                break
            m = (dst[l:] + ".").find(".")
            dst = dst[:l] + dst[l + m:]

        dst = atol(dst)
        pathes = []
        for d, m, gw, i, a in self.routes:
            aa = atol(a)
            if aa == dst:
                pathes.append((0xffffffffL, (LOOPBACK_NAME, a, "0.0.0.0")))
            if (dst & m) == (d & m):
                pathes.append((m, (i, a, gw)))
        if not pathes:
            if verbose:
                warning("No route found (no default route?)")
            return LOOPBACK_NAME, "0.0.0.0", "0.0.0.0"  #XXX linux specific!
        # Choose the more specific route (greatest netmask).
        # XXX: we don't care about metrics
        pathes.sort()
        ret = pathes[-1][1]
        self.cache[dest] = ret
        return ret
 def get_if_bcast(self, iff):
     for net, msk, gw, iface, addr in self.routes:
         if (iff == iface and net != 0L):
             bcast = atol(addr) | (~msk & 0xffffffffL)
             # FIXME: check error in atol()
             return ltoa(bcast)
     warning("No broadcast address found for iface %s\n" % iff)
 def ifadd(self, iff, addr):
     self.invalidate_cache()
     the_addr, the_msk = (addr.split("/") + ["32"])[:2]
     the_msk = itom(int(the_msk))
     the_rawaddr = atol(the_addr)
     the_net = the_rawaddr & the_msk
     self.routes.append((the_net, the_msk, '0.0.0.0', iff, the_addr))
Ejemplo n.º 5
0
 def ifadd(self, iff, addr):
     self.invalidate_cache()
     the_addr,the_msk = (addr.split("/")+["32"])[:2]
     the_msk = itom(int(the_msk))
     the_rawaddr = atol(the_addr)
     the_net = the_rawaddr & the_msk
     self.routes.append((the_net,the_msk,'0.0.0.0',iff,the_addr))
Ejemplo n.º 6
0
 def get_if_bcast(self, iff):
     for net, msk, gw, iface, addr in self.routes:
         if (iff == iface and net != 0L):
             bcast = atol(addr) | (~msk & 0xffffffffL)
             # FIXME: check error in atol()
             return ltoa(bcast)
     warning("No broadcast address found for iface %s\n" % iff)
    def ifchange(self, iff, addr):
        self.invalidate_cache()
        the_addr, the_msk = (addr.split("/") + ["32"])[:2]
        the_msk = itom(int(the_msk))
        the_rawaddr = atol(the_addr)
        the_net = the_rawaddr & the_msk

        for i in range(len(self.routes)):
            net, msk, gw, iface, addr = self.routes[i]
            if iface != iff:
                continue
            if gw == '0.0.0.0':
                self.routes[i] = (the_net, the_msk, gw, iface, the_addr)
            else:
                self.routes[i] = (net, msk, gw, iface, the_addr)
        conf.netcache.flush()
Ejemplo n.º 8
0
    def ifchange(self, iff, addr):
        self.invalidate_cache()
        the_addr, the_msk = (addr.split("/") + ["32"])[:2]
        the_msk = itom(int(the_msk))
        the_rawaddr = atol(the_addr)
        the_net = the_rawaddr & the_msk

        for i in range(len(self.routes)):
            net, msk, gw, iface, addr = self.routes[i]
            if iface != iff:
                continue
            if gw == '0.0.0.0':
                self.routes[i] = (the_net, the_msk, gw, iface, the_addr)
            else:
                self.routes[i] = (net, msk, gw, iface, the_addr)
        conf.netcache.flush()
Ejemplo n.º 9
0
 def make_route(self, host=None, net=None, gw=None, dev=None):
     if host is not None:
         thenet,msk = host,32
     elif net is not None:
         thenet,msk = net.split("/")
         msk = int(msk)
     else:
         raise Scapy_Exception("make_route: Incorrect parameters. You should specify a host or a net")
     if gw is None:
         gw="0.0.0.0"
     if dev is None:
         if gw:
             nhop = gw
         else:
             nhop = thenet
         dev,ifaddr,x = self.route(nhop)
     else:
         ifaddr = get_if_addr(dev)
     return (atol(thenet), itom(msk), gw, dev, ifaddr)
Ejemplo n.º 10
0
    def route(self,dest,verbose=None, iface_hint=None):
        # iface_hint: if has the same dest and mask( mulitcast specific), get iface_hint route
        #  by chenzongze 2013.10.17
        if type(dest) is list and dest:
            dest = dest[0]
        if iface_hint :
            cache_hash = dest + iface_hint
        else:
            cache_hash = dest
            
        if cache_hash in self.cache:
            return self.cache[cache_hash]
        if verbose is None:
            verbose=conf.verb
        # Transform "192.168.*.1-5" to one IP of the set
        dst = dest.split("/")[0]
        dst = dst.replace("*","0") 
        while 1:
            l = dst.find("-")
            if l < 0:
                break
            m = (dst[l:]+".").find(".")
            dst = dst[:l]+dst[l+m:]

            
        dst = atol(dst)
        pathes=[]
        MAX_METRIC = 0xffffffffL
        # take metric into considaration by chenzongze 2012.07.24
        for d,m,gw,i,a,metric in self.routes:
            metric = MAX_METRIC - int(metric)
            aa = atol(a)
            if aa == dst:
                pathes.append((0xffffffffL,(MAX_METRIC,LOOPBACK_NAME,a,"0.0.0.0")))
            if (dst & m) == (d & m):
                pathes.append((m,(metric,i,a,gw)))
        if not pathes:
            if verbose:
                warning("No route found (no default route?)")
            return LOOPBACK_NAME,"0.0.0.0","0.0.0.0" #XXX linux specific!
        # Choose the more specific route (greatest netmask).
        # XXX: we don't care about metrics
        pathes.sort()
        
        ret = pathes[-1][1][1:]  # omit the metric again
        
        if iface_hint: # indicate that 
            last_metric = pathes[-1][0]

            
            for i in range(-1,-len(pathes)-1,-1):
                if last_metric == pathes[i][0]:
                    if pathes[i][1][1] == iface_hint:
                        # we know we get it
                        ret = pathes[i][1][1:]
                else:
                    break
                

            
        
       
           
        
        self.cache[cache_hash] = ret
        return ret