'Deal with incoming query packets.  Provides a response if
possible.'

def handleQuery(self, msg, addr, port):

    out = None
    if (port != _MDNS_PORT):
        out = DNSOutgoing((_FLAGS_QR_RESPONSE | _FLAGS_AA), 0)
        for question in msg.questions:
            out.addQuestion(question)
    for question in msg.questions:
        if (question.type == _TYPE_PTR):
            if (question.name == '_services._dns-sd._udp.local.'):
                for stype in self.servicetypes.keys():
                    if (out is None):
                        out = DNSOutgoing((_FLAGS_QR_RESPONSE | _FLAGS_AA))
                    out.addAnswer(msg, DNSPointer('_services._dns-sd._udp.local.', _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype))
            for service in self.services.values():
                if (question.name == service.type):
                    if (out is None):
                        out = DNSOutgoing((_FLAGS_QR_RESPONSE | _FLAGS_AA))
                    out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name))
        else:
            try:
                if (out is None):
                    out = DNSOutgoing((_FLAGS_QR_RESPONSE | _FLAGS_AA))
                if (question.type in (_TYPE_A, _TYPE_AAAA, _TYPE_ANY)):
                    for service in self.services.values():
                        if (service.server == question.name.lower()):
                            out.addAnswer(msg, DNSAddress(question.name, address_type(service.address), (_CLASS_IN | _CLASS_UNIQUE), _DNS_TTL, service.address))
                service = self.services.get(question.name.lower(), None)
                if (not service):
                    continue
                if ((question.type == _TYPE_SRV) or (question.type == _TYPE_ANY)):
                    out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, (_CLASS_IN | _CLASS_UNIQUE), _DNS_TTL, service.priority, service.weight, service.port, service.server))
                if ((question.type == _TYPE_TXT) or (question.type == _TYPE_ANY)):
                    out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, (_CLASS_IN | _CLASS_UNIQUE), _DNS_TTL, service.text))
                if (question.type == _TYPE_SRV):
                    out.addAdditionalAnswer(DNSAddress(service.server, address_type(service.address), (_CLASS_IN | _CLASS_UNIQUE), _DNS_TTL, service.address))
            except:
                traceback.print_exc()
    if ((out is not None) and out.answers):
        out.id = msg.id
        self.send(out, addr, port)
