/** * Add an answer to the message. * * @param out * outgoing message * @param rec * DNS record answer * @param now * @return outgoing message for the next answer * @exception IOException */ public DNSOutgoing addAnswer(DNSOutgoing out, DNSRecord rec, long now) throws IOException { DNSOutgoing newOut = out; try { newOut.addAnswer(rec, now); } catch (final IOException e) { int flags = newOut.getFlags(); boolean multicast = newOut.isMulticast(); int maxUDPPayload = newOut.getMaxUDPPayload(); int id = newOut.getId(); newOut.setFlags(flags | DNSConstants.FLAGS_TC); newOut.setId(id); this._jmDNSImpl.send(newOut); newOut = new DNSOutgoing(flags, multicast, maxUDPPayload); newOut.addAnswer(rec, now); } return newOut; }
/** * Add an additional answer to the record. Omit if there is no room. * * @param out * outgoing message * @param in * incoming request * @param rec * DNS record answer * @return outgoing message for the next answer * @exception IOException */ public DNSOutgoing addAdditionalAnswer(DNSOutgoing out, DNSIncoming in, DNSRecord rec) throws IOException { DNSOutgoing newOut = out; try { newOut.addAdditionalAnswer(in, rec); } catch (final IOException e) { int flags = newOut.getFlags(); boolean multicast = newOut.isMulticast(); int maxUDPPayload = newOut.getMaxUDPPayload(); int id = newOut.getId(); newOut.setFlags(flags | DNSConstants.FLAGS_TC); newOut.setId(id); this._jmDNSImpl.send(newOut); newOut = new DNSOutgoing(flags, multicast, maxUDPPayload); newOut.addAdditionalAnswer(in, rec); } return newOut; }
/** * Add an authoritative answer to the message. * * @param out * outgoing message * @param rec * DNS record answer * @return outgoing message for the next answer * @exception IOException */ public DNSOutgoing addAuthoritativeAnswer(DNSOutgoing out, DNSRecord rec) throws IOException { DNSOutgoing newOut = out; try { newOut.addAuthorativeAnswer(rec); } catch (final IOException e) { int flags = newOut.getFlags(); boolean multicast = newOut.isMulticast(); int maxUDPPayload = newOut.getMaxUDPPayload(); int id = newOut.getId(); newOut.setFlags(flags | DNSConstants.FLAGS_TC); newOut.setId(id); this._jmDNSImpl.send(newOut); newOut = new DNSOutgoing(flags, multicast, maxUDPPayload); newOut.addAuthorativeAnswer(rec); } return newOut; }
/** * Add a question to the message. * * @param out * outgoing message * @param rec * DNS question * @return outgoing message for the next question * @exception IOException */ public DNSOutgoing addQuestion(DNSOutgoing out, DNSQuestion rec) throws IOException { DNSOutgoing newOut = out; try { newOut.addQuestion(rec); } catch (final IOException e) { int flags = newOut.getFlags(); boolean multicast = newOut.isMulticast(); int maxUDPPayload = newOut.getMaxUDPPayload(); int id = newOut.getId(); newOut.setFlags(flags | DNSConstants.FLAGS_TC); newOut.setId(id); this._jmDNSImpl.send(newOut); newOut = new DNSOutgoing(flags, multicast, maxUDPPayload); newOut.addQuestion(rec); } return newOut; }
@Override public String toString() { StringBuffer buf = new StringBuffer(); buf.append(isQuery() ? "dns[query:" : "dns[response:"); buf.append(" id=0x"); buf.append(Integer.toHexString(this.getId())); if (this.getFlags() != 0) { buf.append(", flags=0x"); buf.append(Integer.toHexString(this.getFlags())); if ((this.getFlags() & DNSConstants.FLAGS_QR_RESPONSE) != 0) { buf.append(":r"); if ((this.getFlags() & DNSConstants.FLAGS_AA) != 0) { buf.append(":aa"); if ((this.getFlags() & DNSConstants.FLAGS_TC) != 0) { buf.append(":tc"); if (this.getNumberOfQuestions() > 0) { buf.append(", questions="); buf.append(this.getNumberOfQuestions()); if (this.getNumberOfAnswers() > 0) { buf.append(", answers="); buf.append(this.getNumberOfAnswers()); if (this.getNumberOfAuthorities() > 0) { buf.append(", authorities="); buf.append(this.getNumberOfAuthorities());
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append(isQuery() ? "dns[query:" : "dns[response:"); sb.append(" id=0x"); sb.append(Integer.toHexString(this.getId())); if (this.getFlags() != 0) { sb.append(", flags=0x"); sb.append(Integer.toHexString(this.getFlags())); if (this.isResponse()) { sb.append(":r"); if (this.isAuthoritativeAnswer()) { sb.append(":aa"); if (this.isTruncated()) { sb.append(":tc"); if (this.getNumberOfQuestions() > 0) { sb.append(", questions="); sb.append(this.getNumberOfQuestions()); if (this.getNumberOfAnswers() > 0) { sb.append(", answers="); sb.append(this.getNumberOfAnswers()); if (this.getNumberOfAuthorities() > 0) { sb.append(", authorities="); sb.append(this.getNumberOfAuthorities());
/** * Add an answer to a question. Deal with the case when the outgoing packet overflows * * @param in * @param addr * @param port * @param out * @param rec * @return outgoing answer * @exception IOException */ public DNSOutgoing addAnswer(DNSIncoming in, InetAddress addr, int port, DNSOutgoing out, DNSRecord rec) throws IOException { DNSOutgoing newOut = out; if (newOut == null) { newOut = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, false, in.getSenderUDPPayload()); } try { newOut.addAnswer(in, rec); } catch (final IOException e) { newOut.setFlags(newOut.getFlags() | DNSConstants.FLAGS_TC); newOut.setId(in.getId()); send(newOut); newOut = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, false, in.getSenderUDPPayload()); newOut.addAnswer(in, rec); } return newOut; }
logger.finer(this.getName() + "run() JmDNS responding"); DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, !_unicast, _in.getSenderUDPPayload()); out.setId(_in.getId()); for (DNSQuestion question : questions) { if (question != null) { if (!out.isEmpty()) this.getDns().send(out);
@Override protected DNSOutgoing createOugoing() { return new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA); }
/** * Add a question to the message. * * @param rec * @exception IOException */ public void addQuestion(DNSQuestion rec) throws IOException { MessageOutputStream record = new MessageOutputStream(512, this); record.writeQuestion(rec); byte[] byteArray = record.toByteArray(); if (byteArray.length < this.availableSpace()) { _questions.add(rec); _questionsBytes.write(byteArray, 0, byteArray.length); } else { throw new IOException("message full"); } }
@Override public void run() { try { if (this.getDns().isCanceling() || this.getDns().isCanceled()) { this.cancel(); } else { if (_count++ < 3) { if (logger.isLoggable(Level.FINER)) { logger.finer(this.getName() + ".run() JmDNS " + this.description()); } DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY); out = this.addQuestions(out); if (this.getDns().isAnnounced()) { out = this.addAnswers(out); } if (!out.isEmpty()) { this.getDns().send(out); } } else { // After three queries, we can quit. this.cancel(); } } } catch (Throwable e) { logger.log(Level.WARNING, this.getName() + ".run() exception ", e); this.getDns().recover(); } }
logger.debug("{}.run() JmDNS responding", this.getName()); DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, !_unicast, _in.getSenderUDPPayload()); if (_unicast) { out.setDestination(new InetSocketAddress(_addr, _port)); out.setId(_in.getId()); for (DNSQuestion question : questions) { if (question != null) { if (!out.isEmpty()) this.getDns().send(out);
/** * Send an outgoing multicast DNS message. * * @param out * @exception IOException */ public void send(DNSOutgoing out) throws IOException { if (!out.isEmpty()) { byte[] message = out.data(); final DatagramPacket packet = new DatagramPacket(message, message.length, _group, DNSConstants.MDNS_PORT); if (logger.isLoggable(Level.FINEST)) { try { final DNSIncoming msg = new DNSIncoming(packet); if (logger.isLoggable(Level.FINEST)) { logger.finest("send(" + this.getName() + ") JmDNS out:" + msg.print(true)); } } catch (final IOException e) { logger.throwing(getClass().toString(), "send(" + this.getName() + ") - JmDNS can not parse what it sends!!!", e); } } final MulticastSocket ms = _socket; if (ms != null && !ms.isClosed()) { ms.send(packet); } } }
if (!out.isEmpty()) { final InetAddress addr; final int port; if (out.getDestination() != null) { addr = out.getDestination().getAddress(); port = out.getDestination().getPort(); } else { addr = _group; byte[] message = out.data(); final DatagramPacket packet = new DatagramPacket(message, message.length, addr, port);
@Override protected DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException { DNSOutgoing newOut = out; newOut.addQuestion(DNSQuestion.newQuestion(this.getDns().getLocalHost().getName(), DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE)); for (DNSRecord answer : this.getDns().getLocalHost().answers(DNSRecordClass.CLASS_ANY, DNSRecordClass.NOT_UNIQUE, this.getTTL())) { newOut = this.addAuthoritativeAnswer(newOut, answer); } return newOut; }
/** * Add an answer if it is not suppressed. * * @param in * @param rec * @exception IOException */ public void addAnswer(DNSIncoming in, DNSRecord rec) throws IOException { if ((in == null) || !rec.suppressedBy(in)) { this.addAnswer(rec, 0); } }
@Override public boolean isQuery() { return (this.getFlags() & DNSConstants.FLAGS_QR_MASK) == DNSConstants.FLAGS_QR_QUERY; }
@Override public String toString() { StringBuffer buf = new StringBuffer(); buf.append(isQuery() ? "dns[query:" : "dns[response:"); buf.append(" id=0x"); buf.append(Integer.toHexString(this.getId())); if (this.getFlags() != 0) { buf.append(", flags=0x"); buf.append(Integer.toHexString(this.getFlags())); if ((this.getFlags() & DNSConstants.FLAGS_QR_RESPONSE) != 0) { buf.append(":r"); if ((this.getFlags() & DNSConstants.FLAGS_AA) != 0) { buf.append(":aa"); if ((this.getFlags() & DNSConstants.FLAGS_TC) != 0) { buf.append(":tc"); if (this.getNumberOfQuestions() > 0) { buf.append(", questions="); buf.append(this.getNumberOfQuestions()); if (this.getNumberOfAnswers() > 0) { buf.append(", answers="); buf.append(this.getNumberOfAnswers()); if (this.getNumberOfAuthorities() > 0) { buf.append(", authorities="); buf.append(this.getNumberOfAuthorities());
/** * Add a question to the message. * * @param out * outgoing message * @param rec * DNS question * @return outgoing message for the next question * @exception IOException */ public DNSOutgoing addQuestion(DNSOutgoing out, DNSQuestion rec) throws IOException { DNSOutgoing newOut = out; try { newOut.addQuestion(rec); } catch (final IOException e) { int flags = newOut.getFlags(); boolean multicast = newOut.isMulticast(); int maxUDPPayload = newOut.getMaxUDPPayload(); int id = newOut.getId(); newOut.setFlags(flags | DNSConstants.FLAGS_TC); newOut.setId(id); this._jmDNSImpl.send(newOut); newOut = new DNSOutgoing(flags, multicast, maxUDPPayload); newOut.addQuestion(rec); } return newOut; }