Skip to content

Commit

Permalink
Always sent a txt, it's required
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed May 20, 2024
1 parent b37dc02 commit df12177
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/main/com/bfo/zeroconf/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public class Packet {
List<Record> additionals = new ArrayList<Record>();
answers.add(Record.newPtr(domain, fqdn));
answers.add(Record.newSrv(fqdn, service.getHost(), service.getPort(), 0, 0));
if (!service.getText().isEmpty()) {
additionals.add(Record.newTxt(fqdn, service.getText()));
}
answers.add(Record.newTxt(fqdn, service.getText())); // Seems "txt" is always required
for (InetAddress address : service.getAddresses()) {
additionals.add(Record.newAddress(service.getHost(), address));
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/com/bfo/zeroconf/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,18 @@ static Record newPtr(String name, String value) {
if (name == null || value == null) {
throw new IllegalArgumentException("name or value is null");
}
return new Record(TYPE_PTR, CLAZZ, 28800, name, value);
return new Record(TYPE_PTR, 1, 4500, name, value);
}

static Record newSrv(String name, String host, int port, int weight, int priority) {
if (name == null || host == null || port < 1 || port > 65535) {
if (name == null || host == null || port < 0 || port > 65535) {
throw new IllegalArgumentException("name, host or port is invalid");
}
return new Record(TYPE_SRV, CLAZZ, 120, name, new SrvData(priority, weight, port, host));
}

static Record newTxt(String name, Map<String,String> map) {
if (name == null || map == null || map.isEmpty()) {
if (name == null || map == null) {
throw new IllegalArgumentException("name or map is invalid");
}
return new Record(TYPE_TXT, CLAZZ, 4500, name, map);
Expand Down Expand Up @@ -239,11 +239,15 @@ void write(ByteBuffer out) {
} else if (type == TYPE_AAAA) {
out.put(((Inet6Address)getAddress()).getAddress());
} else if (type == TYPE_TXT) {
for (Map.Entry<String,String> e : getText().entrySet()) {
String value = e.getKey()+"="+e.getValue();
byte[] b = value.getBytes(StandardCharsets.UTF_8);
out.put((byte)b.length);
out.put(b);
if (getText().isEmpty()) {
out.put((byte)0);
} else {
for (Map.Entry<String,String> e : getText().entrySet()) {
String value = e.getKey()+"="+e.getValue();
byte[] b = value.getBytes(StandardCharsets.UTF_8);
out.put((byte)b.length);
out.put(b);
}
}
} else if (data instanceof byte[]) {
out.put((byte[])data);
Expand Down
9 changes: 7 additions & 2 deletions src/main/com/bfo/zeroconf/Zeroconf.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public void query(String type, String name) {
//--------------------------------------------------------------------

void send(Packet packet) {
// System.out.println("# TX " + packet);
thread.push(packet);
}

Expand Down Expand Up @@ -669,6 +670,7 @@ public void run() {
if (address != null && buf.position() != 0) {
buf.flip();
packet = new Packet(buf, address);
// System.out.println("# RX: " + packet);
processPacket(packet);
}
}
Expand Down Expand Up @@ -803,14 +805,17 @@ private void processQuestions(Packet packet) {
additionals = new ArrayList<Record>();
}
answers.add(answer);
List<Record> l = new ArrayList<Record>();
l.addAll(p.getAnswers());
l.addAll(p.getAdditionals());
if (answer.getType() == Record.TYPE_PTR && question.getType() != Record.TYPE_ANY) {
// When including a DNS-SD Service Instance Enumeration or Selective
// Instance Enumeration (subtype) PTR record in a response packet, the
// server/responder SHOULD include the following additional records:
// * The SRV record(s) named in the PTR rdata.
// * The TXT record(s) named in the PTR rdata.
// * All address records (type "A" and "AAAA") named in the SRV rdata.
for (Record a : p.getAnswers()) {
for (Record a : l) {
if (a.getType() == Record.TYPE_SRV || a.getType() == Record.TYPE_A || a.getType() == Record.TYPE_AAAA || a.getType() == Record.TYPE_TXT) {
additionals.add(a);
}
Expand All @@ -819,7 +824,7 @@ private void processQuestions(Packet packet) {
// When including an SRV record in a response packet, the
// server/responder SHOULD include the following additional records:
// * All address records (type "A" and "AAAA") named in the SRV rdata.
for (Record a : p.getAnswers()) {
for (Record a : l) {
if (a.getType() == Record.TYPE_A || a.getType() == Record.TYPE_AAAA || a.getType() == Record.TYPE_TXT) {
additionals.add(a);
}
Expand Down

0 comments on commit df12177

Please sign in to comment.