answers.add(host); answers.add(new DNSRecord.Pointer( info.type, DNSConstants.TYPE_PTR, DNSConstants.CLASS_IN, DNSConstants.DNS_TTL, info.getQualifiedName()) ); answers.add( new DNSRecord.Service( info.getQualifiedName(), DNSConstants.TYPE_SRV, DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE, DNSConstants.DNS_TTL, info.priority, info.weight, info.port, host.name ) ); answers.add( new DNSRecord.Text( info.getQualifiedName(), DNSConstants.TYPE_TXT, DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE, DNSConstants.DNS_TTL, info.text ) );
DNSRecord answer = localHost.getDNS4AddressRecord(); if (answer != null) answers.add(answer); answer = localHost.getDNS6AddressRecord(); if (answer != null) answers.add(answer); answers.add(new DNSRecord.Pointer( info.type, DNSConstants.TYPE_PTR, DNSConstants.CLASS_IN, DNSConstants.DNS_TTL, info.getQualifiedName() ) ); answers.add(new DNSRecord.Service( info.getQualifiedName(), DNSConstants.TYPE_SRV, DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE, DNSConstants.DNS_TTL, info.priority, info.weight, info.port, localHost.getName() ) ); answers.add(new DNSRecord.Text( info.getQualifiedName(), DNSConstants.TYPE_TXT, DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE, DNSConstants.DNS_TTL, info.text ) );
        public void run() {            synchronized (ioLock) {                if (plannedAnswer == in) {                    plannedAnswer = null;                }                                // We use these sets to prevent duplicate records                // FIXME - This should be moved into DNSOutgoing                HashSet questions = new HashSet();                HashSet answers = new HashSet();                                                if (state == DNSState.ANNOUNCED) {                    try {                        long now = System.currentTimeMillis();                        long expirationTime = now + 1; //=now+DNSConstants.KNOWN_ANSWER_TTL;                        boolean isUnicast = (port != DNSConstants.MDNS_PORT);                                                                        // Answer questions                        for (Enumeration e = in.questions.elements(); e.hasMoreElements(); ) {                            DNSEntry entry = (DNSEntry) e.nextElement();                            if (entry instanceof DNSQuestion) {                                DNSQuestion q = (DNSQuestion) entry;                                                                // for unicast responses the question must be included                                if (isUnicast) {                                    //out.addQuestion(q);                                    questions.add(q);                                }                                                                int type = q.type;                                if (type == DNSConstants.TYPE_ANY || type == DNSConstants.TYPE_SRV) {                                    if (host.name.equalsIgnoreCase(q.name)) {                                        type = DNSConstants.TYPE_A;                                    } else if (serviceTypes.containsKey(q.name.toLowerCase())) {                                        type = DNSConstants.TYPE_PTR;                                    }                                }                                                                switch( type ) {                                    case DNSConstants.TYPE_A: {                                        // Answer a query for a domain name                                        //out = addAnswer( in, addr, port, out, host );                                        answers.add(host);                                        break;                                    }                                    case DNSConstants.TYPE_PTR: {                                        // Answer a query for services of a given type                                                                                // find matching services                                        for( Enumeration s = services.elements(); s.hasMoreElements(); ) {                                            ServiceInfo info = (ServiceInfo)s.nextElement();                                            if (info.getState() == DNSState.ANNOUNCED) {                                                if( q.name.equalsIgnoreCase(info.type) ) {                                                    answers.add(host);                                                    answers.add(new DNSRecord.Pointer( info.type, DNSConstants.TYPE_PTR,                                                    DNSConstants.CLASS_IN, DNSConstants.DNS_TTL,                                                    info.getQualifiedName())                                                    );                                                    answers.add(                                                    new DNSRecord.Service( info.getQualifiedName(), DNSConstants.TYPE_SRV,                                                    DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE,                                                    DNSConstants.DNS_TTL, info.priority,                                                    info.weight, info.port, host.name )                                                    );                                                    answers.add(                                                    new DNSRecord.Text( info.getQualifiedName(), DNSConstants.TYPE_TXT,                                                    DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE,                                                    DNSConstants.DNS_TTL, info.text )                                                    );                                                }                                            }                                        }                                        if( q.name.equalsIgnoreCase( "_services._mdns._udp.local." ) ) {                                            for( Enumeration t = serviceTypes.elements(); t.hasMoreElements(); ) {                                                answers.add(                                                new DNSRecord.Pointer( "_services._mdns._udp.local.",                                                DNSConstants.TYPE_PTR, DNSConstants.CLASS_IN,                                                DNSConstants.DNS_TTL, (String)t.nextElement() )                                                );                                            }                                        }                                        break;                                    }                                    case DNSConstants.TYPE_SRV :                                    case DNSConstants.TYPE_ANY :                                    case DNSConstants.TYPE_TXT : {                                        ServiceInfo info = (ServiceInfo) services.get(q.name.toLowerCase());                                        if (info != null && info.getState() == DNSState.ANNOUNCED) {                                            answers.add(host);                                            answers.add(                                            new DNSRecord.Pointer( info.type, DNSConstants.TYPE_PTR,                                            DNSConstants.CLASS_IN, DNSConstants.DNS_TTL,                                            info.getQualifiedName())                                            );                                                                                        answers.add(                                            new DNSRecord.Service( info.getQualifiedName(), DNSConstants.TYPE_SRV,                                            DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE,                                            DNSConstants.DNS_TTL, info.priority,                                            info.weight, info.port, host.name )                                            );                                            answers.add(                                            new DNSRecord.Text( info.getQualifiedName(), DNSConstants.TYPE_TXT,                                            DNSConstants.CLASS_IN | DNSConstants.CLASS_UNIQUE,                                            DNSConstants.DNS_TTL, info.text )                                            );                                        }                                        break;                                    }                                    default : {                                        //System.out.println("JmDNSResponder.unhandled query:"+q);                                        break;                                    }                                }                            }                        }                                                                        if (answers.size() !=0) {                            if (debug > 1) System.out.println("JmDNS responding");                            DNSOutgoing out = null;                            if (isUnicast) {                                out = new DNSOutgoing( DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, false );                            }                                                        for (Iterator i=questions.iterator(); i.hasNext(); ) {                                out.addQuestion((DNSQuestion) i.next());                            }                            for (Iterator i=answers.iterator(); i.hasNext(); ) {                                out = addAnswer(in, addr, port, out, (DNSRecord) i.next());                            }                            send(out);                        }                        cancel();                    } catch (Throwable e) {                        e.printStackTrace();                        close();                    }                }            }        }