Esempio n. 1
0
def getmacbyip(ip, chainCC=0):
    """Return MAC address corresponding to a given IP address"""
    if isinstance(ip,Net):
        ip = iter(ip).next()
    ip = inet_ntoa(inet_aton(ip))
    tmp = map(ord, inet_aton(ip))
    if (tmp[0] & 0xf0) == 0xe0: # mcast @
        return "01:00:5e:%.2x:%.2x:%.2x" % (tmp[1]&0x7f,tmp[2],tmp[3])
    iff,a,gw = conf.route.route(ip)
    if ( (iff == LOOPBACK_NAME) or (ip == conf.route.get_if_bcast(iff)) ):
        return "ff:ff:ff:ff:ff:ff"
    if gw != "0.0.0.0":
        ip = gw

    mac = conf.netcache.arp_cache.get(ip)
    if mac:
        return mac

    res = srp1(Ether(dst=ETHER_BROADCAST)/ARP(op="who-has", pdst=ip),
               type=ETH_P_ARP,
               iface = iff,
               timeout=2,
               verbose=0,
               chainCC=chainCC,
               nofilter=1)
    if res is not None:
        mac = res.payload.hwsrc
        conf.netcache.arp_cache[ip] = mac
        return mac
    return None
Esempio n. 2
0
File: ber.py Progetto: 0x0mar/zarp
 def do_dec(cls, s, context=None, safe=False):
     l,s,t = cls.check_type_check_len(s)
     try:
         ipaddr_ascii = inet_ntoa(s)
     except Exception:
         raise BER_Decoding_Error("IP address could not be decoded", decoded=obj)
     return cls.asn1_object(ipaddr_ascii), t
Esempio n. 3
0
File: ldp.py Progetto: commial/scapy
 def m2i(self, pkt, x):
     nbr = struct.unpack("!H", x[2:4])[0] - 2
     nbr //= 4
     x = x[6:]
     list = []
     for i in range(0, nbr):
         add = x[4 * i:4 * i + 4]
         list.append(inet_ntoa(add))
     return list
Esempio n. 4
0
File: ldp.py Progetto: commial/scapy
 def m2i(self, pkt, x):
     lst = [struct.unpack("!H", x[6:8])[0]]
     octet = struct.unpack("B", x[8:9])[0]
     lst.append((octet & 2**7) >> 7)
     lst.append((octet & 2**6) >> 6)
     lst.append(struct.unpack("B", x[9:10])[0])
     lst.append(struct.unpack("!H", x[10:12])[0])
     lst.append(inet_ntoa(x[12:16]))
     lst.append(struct.unpack("!H", x[16:18])[0])
     return lst
Esempio n. 5
0
    def m2i(self, pkt, x):
        tmp_len = self.length_from(pkt)

        if tmp_len <= 8:
            x += b"\x00\x00\x00"
        elif tmp_len <= 16:
            x += b"\x00\x00"
        elif tmp_len <= 24:
            x += b"\x00"

        return inet_ntoa(x)
Esempio n. 6
0
File: ldp.py Progetto: commial/scapy
 def m2i(self, pkt, x):
     used = 0
     x = x[4:]
     list = []
     while x:
         # if x[0] == 1:
         #   list.append('Wildcard')
         # else:
         # mask=orb(x[8*i+3])
         # add=inet_ntoa(x[8*i+4:8*i+8])
         mask = orb(x[3])
         nbroctets = mask // 8
         if mask % 8:
             nbroctets += 1
         add = inet_ntoa(x[4:4 + nbroctets] + b"\x00" * (4 - nbroctets))
         list.append((add, mask))
         used += 4 + nbroctets
         x = x[4 + nbroctets:]
     return list