Add constructors to MDNS records
Add constructors to allow creating the records locally, which is useful
for advertising as opposed to only receiving such records in discovery.
Bug: 241738458
Test: atest
Change-Id: I61a1bb91cdc2d06e2e2e57e3951c63e2820203eb
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java b/service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java
index 47ac20e..dd8a526 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java
+++ b/service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java
@@ -43,7 +43,32 @@
*/
public MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader)
throws IOException {
- super(name, type, reader);
+ this(name, type, reader, false);
+ }
+
+ /**
+ * Constructs the {@link MdnsRecord}
+ *
+ * @param name the service host name
+ * @param type the type of record (either Type 'AAAA' or Type 'A')
+ * @param reader the reader to read the record from.
+ * @param isQuestion whether the record is in the question section
+ */
+ public MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader,
+ boolean isQuestion)
+ throws IOException {
+ super(name, type, reader, isQuestion);
+ }
+
+ public MdnsInetAddressRecord(String[] name, long receiptTimeMillis, boolean cacheFlush,
+ long ttlMillis, InetAddress address) {
+ super(name, address instanceof Inet4Address ? TYPE_A : TYPE_AAAA,
+ MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush, ttlMillis);
+ if (address instanceof Inet4Address) {
+ inet4Address = (Inet4Address) address;
+ } else {
+ inet6Address = (Inet6Address) address;
+ }
}
/** Returns the IPv6 address. */
@@ -127,4 +152,4 @@
&& Objects.equals(inet4Address, ((MdnsInetAddressRecord) other).inet4Address)
&& Objects.equals(inet6Address, ((MdnsInetAddressRecord) other).inet6Address);
}
-}
\ No newline at end of file
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java b/service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java
index 9641a40..2c7b26b 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java
+++ b/service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java
@@ -29,7 +29,19 @@
private String[] pointer;
public MdnsPointerRecord(String[] name, MdnsPacketReader reader) throws IOException {
- super(name, TYPE_PTR, reader);
+ this(name, reader, false);
+ }
+
+ public MdnsPointerRecord(String[] name, MdnsPacketReader reader, boolean isQuestion)
+ throws IOException {
+ super(name, TYPE_PTR, reader, isQuestion);
+ }
+
+ public MdnsPointerRecord(String[] name, long receiptTimeMillis, boolean cacheFlush,
+ long ttlMillis, String[] pointer) {
+ super(name, TYPE_PTR, MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush,
+ ttlMillis);
+ this.pointer = pointer;
}
/** Returns the pointer as an array of labels. */
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java b/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java
index 35f6da1..7c802e6 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java
+++ b/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java
@@ -40,6 +40,10 @@
public static final int TYPE_SRV = 0x0021;
public static final int TYPE_TXT = 0x0010;
+ private static final int FLAG_CACHE_FLUSH = 0x8000;
+
+ public static final long RECEIPT_TIME_NOT_SENT = 0L;
+
/** Status indicating that the record is current. */
public static final int STATUS_OK = 0;
/** Status indicating that the record has expired (TTL reached 0). */
@@ -58,23 +62,52 @@
* Constructs a new record with the given name and type.
*
* @param reader The reader to read the record from.
+ * @param isQuestion Whether the record was included in the questions part of the message.
+ * @throws IOException If an error occurs while reading the packet.
+ */
+ protected MdnsRecord(String[] name, int type, MdnsPacketReader reader, boolean isQuestion)
+ throws IOException {
+ this.name = name;
+ this.type = type;
+ cls = reader.readUInt16();
+ receiptTimeMillis = SystemClock.elapsedRealtime();
+
+ if (isQuestion) {
+ // Questions do not have TTL or data
+ ttlMillis = 0L;
+ } else {
+ ttlMillis = SECONDS.toMillis(reader.readUInt32());
+ int dataLength = reader.readUInt16();
+
+ reader.setLimit(dataLength);
+ readData(reader);
+ reader.clearLimit();
+ }
+ }
+
+ /**
+ * Constructs a new record with the given name and type.
+ *
+ * @param reader The reader to read the record from.
* @throws IOException If an error occurs while reading the packet.
*/
// call to readData(com.android.server.connectivity.mdns.MdnsPacketReader) not allowed on given
// receiver.
@SuppressWarnings("nullness:method.invocation.invalid")
protected MdnsRecord(String[] name, int type, MdnsPacketReader reader) throws IOException {
+ this(name, type, reader, false);
+ }
+
+ /**
+ * Constructs a new record with the given properties.
+ */
+ protected MdnsRecord(String[] name, int type, int cls, long receiptTimeMillis,
+ boolean cacheFlush, long ttlMillis) {
this.name = name;
this.type = type;
- cls = reader.readUInt16();
- ttlMillis = SECONDS.toMillis(reader.readUInt32());
- int dataLength = reader.readUInt16();
-
- receiptTimeMillis = SystemClock.elapsedRealtime();
-
- reader.setLimit(dataLength);
- readData(reader);
- reader.clearLimit();
+ this.cls = cls | (cacheFlush ? FLAG_CACHE_FLUSH : 0);
+ this.receiptTimeMillis = receiptTimeMillis;
+ this.ttlMillis = ttlMillis;
}
/**
@@ -126,13 +159,29 @@
return type;
}
+ /** Return the record's class. */
+ public final int getRecordClass() {
+ return cls & ~FLAG_CACHE_FLUSH;
+ }
+
+ /** Return whether the cache flush flag is set. */
+ public final boolean getCacheFlush() {
+ return (cls & FLAG_CACHE_FLUSH) != 0;
+ }
+
/**
* Returns the record's remaining TTL.
*
+ * If the record was not sent yet (receipt time {@link #RECEIPT_TIME_NOT_SENT}), this is the
+ * original TTL of the record.
* @param now The current system time.
* @return The remaning TTL, in milliseconds.
*/
public long getRemainingTTL(final long now) {
+ if (receiptTimeMillis == RECEIPT_TIME_NOT_SENT) {
+ return ttlMillis;
+ }
+
long age = now - receiptTimeMillis;
if (age > ttlMillis) {
return 0;
@@ -187,6 +236,9 @@
/** Gets the status of the record. */
public int getStatus(final long now) {
+ if (receiptTimeMillis == RECEIPT_TIME_NOT_SENT) {
+ return STATUS_OK;
+ }
final long age = now - receiptTimeMillis;
if (age > ttlMillis) {
return STATUS_EXPIRED;
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java b/service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java
index c52d25e..ebd8b77 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java
+++ b/service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java
@@ -39,7 +39,23 @@
private String[] serviceHost;
public MdnsServiceRecord(String[] name, MdnsPacketReader reader) throws IOException {
- super(name, TYPE_SRV, reader);
+ this(name, reader, false);
+ }
+
+ public MdnsServiceRecord(String[] name, MdnsPacketReader reader, boolean isQuestion)
+ throws IOException {
+ super(name, TYPE_SRV, reader, isQuestion);
+ }
+
+ public MdnsServiceRecord(String[] name, long receiptTimeMillis, boolean cacheFlush,
+ long ttlMillis, int servicePriority, int serviceWeight, int servicePort,
+ String[] serviceHost) {
+ super(name, TYPE_SRV, MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush,
+ ttlMillis);
+ this.servicePriority = servicePriority;
+ this.serviceWeight = serviceWeight;
+ this.servicePort = servicePort;
+ this.serviceHost = serviceHost;
}
/** Returns the service's port number. */
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java b/service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java
index 1e66589..4149dbe 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java
+++ b/service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java
@@ -33,7 +33,19 @@
private List<TextEntry> entries;
public MdnsTextRecord(String[] name, MdnsPacketReader reader) throws IOException {
- super(name, TYPE_TXT, reader);
+ this(name, reader, false);
+ }
+
+ public MdnsTextRecord(String[] name, MdnsPacketReader reader, boolean isQuestion)
+ throws IOException {
+ super(name, TYPE_TXT, reader, isQuestion);
+ }
+
+ public MdnsTextRecord(String[] name, long receiptTimeMillis, boolean cacheFlush, long ttlMillis,
+ List<TextEntry> entries) {
+ super(name, TYPE_TXT, MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush,
+ ttlMillis);
+ this.entries = entries;
}
/** Returns the list of strings. */