Exemplo n.º 1
0
 def __init__(self, arg, value, fetcher=None, compatibility=None, transform_label=True, parent=None):
     ArgDataBase.__init__(self, arg, value, fetcher, compatibility, transform_label, parent)
     self.info_area = InfoArea(parent)
Exemplo n.º 2
0
class ArgDataIP(ArgDataBase):

    DNS_CACHE = {}

    def __init__(self, arg, value, fetcher=None, compatibility=None, transform_label=True, parent=None):
        ArgDataBase.__init__(self, arg, value, fetcher, compatibility, transform_label, parent)
        self.info_area = InfoArea(parent)

    def resolve(self):
        if self.label in ArgDataIP.DNS_CACHE:
            return

        self.fetcher.client.async().call('ufwi_log', 'resolveReverseDNS', self.label, callback=lambda x: self.resolved(self.label, x),
                                                                                    errback=lambda x: self.resolveFailed(self.label))

    def resolved(self, ip, dns):
        ArgDataIP.DNS_CACHE[self.label] = dns
        self.label = dns
        self.emit(SIGNAL('label_changed'))

    def resolveFailed(self, ip):
        try:
            message = unicode(self.tr("Unable to resolve %s")) % self.label
            self.info_area.setText(message)
        except RuntimeError:
            return # probably closing

    def printip(self):
        if self.label in ArgDataIP.DNS_CACHE.values():
            for key in ArgDataIP.DNS_CACHE:
                if ArgDataIP.DNS_CACHE[key] == self.label:
                    del ArgDataIP.DNS_CACHE[key]
                    break
            self.emit(SIGNAL('label_changed'))

    def actions(self):
        # Is label an already computed reverse DNS name ?
        if self.label in ArgDataIP.DNS_CACHE.values() and self.label not in ArgDataIP.DNS_CACHE:
                resolveAction = QAction(tr('Switch to IP address mode'), None)
                resolveAction.connect(resolveAction, SIGNAL('triggered()'), self.printip)
        else:
                resolveAction = QAction(tr('Resolve reverse DNS'), None)
                resolveAction.connect(resolveAction, SIGNAL('triggered()'), self.resolve)
        return [resolveAction]

    def ip2str(self, ip): return ip

    def _get_label(self):
        ipaddr = self.ip2str(self.label)
        try:
            dns = ArgDataIP.DNS_CACHE[ipaddr]
            if dns:
                return dns
            else:
                return ipaddr
        except KeyError:
            try:
                ip = IP(ipaddr)
            except ValueError:
                pass
            else:
                # FIXME this timer usage is responsible for the interface to get stuck
                # on packet list page during 10 sec
                #   Regit: I desactivate RDNS by default
                # Do not try to resolve *.0 and *.255 IP addresses.
                if False and ip.version() == 4 and not (ip.int() & 0xff) in (0, 255):
                #if ip.version() == 4 and not (ip.int() & 0xff) in (0,255):
                    # TODO add an option to activate the auto resolution
                    QTimer.singleShot(random.randint(0, 4000), self.resolve)
            return ipaddr