1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.nsd;
18 
19 import static com.android.net.module.util.HexDump.toHexString;
20 
21 import android.annotation.FlaggedApi;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.compat.annotation.UnsupportedAppUsage;
25 import android.net.Network;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.text.TextUtils;
29 import android.util.ArrayMap;
30 import android.util.ArraySet;
31 import android.util.Log;
32 
33 import com.android.net.module.util.InetAddressUtils;
34 
35 import java.io.UnsupportedEncodingException;
36 import java.net.InetAddress;
37 import java.nio.charset.StandardCharsets;
38 import java.time.Instant;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.StringJoiner;
46 
47 /**
48  * A class representing service information for network service discovery
49  * @see NsdManager
50  */
51 public final class NsdServiceInfo implements Parcelable {
52 
53     private static final String TAG = "NsdServiceInfo";
54 
55     @Nullable
56     private String mServiceName;
57 
58     @Nullable
59     private String mServiceType;
60 
61     private final Set<String> mSubtypes;
62 
63     private final ArrayMap<String, byte[]> mTxtRecord;
64 
65     private final List<InetAddress> mHostAddresses;
66 
67     @Nullable
68     private String mHostname;
69 
70     private int mPort;
71 
72     @Nullable
73     private byte[] mPublicKey;
74 
75     @Nullable
76     private Network mNetwork;
77 
78     private int mInterfaceIndex;
79 
80     // The timestamp that one or more resource records associated with this service are considered
81     // invalid.
82     @Nullable
83     private Instant mExpirationTime;
84 
NsdServiceInfo()85     public NsdServiceInfo() {
86         mSubtypes = new ArraySet<>();
87         mTxtRecord = new ArrayMap<>();
88         mHostAddresses = new ArrayList<>();
89     }
90 
91     /** @hide */
NsdServiceInfo(String sn, String rt)92     public NsdServiceInfo(String sn, String rt) {
93         this();
94         mServiceName = sn;
95         mServiceType = rt;
96     }
97 
98     /**
99      * Creates a copy of {@code other}.
100      *
101      * @hide
102      */
NsdServiceInfo(@onNull NsdServiceInfo other)103     public NsdServiceInfo(@NonNull NsdServiceInfo other) {
104         mServiceName = other.getServiceName();
105         mServiceType = other.getServiceType();
106         mSubtypes = new ArraySet<>(other.getSubtypes());
107         mTxtRecord = new ArrayMap<>(other.mTxtRecord);
108         mHostAddresses = new ArrayList<>(other.getHostAddresses());
109         mHostname = other.getHostname();
110         mPort = other.getPort();
111         mNetwork = other.getNetwork();
112         mInterfaceIndex = other.getInterfaceIndex();
113         mExpirationTime = other.getExpirationTime();
114     }
115 
116     /** Get the service name */
getServiceName()117     public String getServiceName() {
118         return mServiceName;
119     }
120 
121     /** Set the service name */
setServiceName(String s)122     public void setServiceName(String s) {
123         mServiceName = s;
124     }
125 
126     /** Get the service type */
getServiceType()127     public String getServiceType() {
128         return mServiceType;
129     }
130 
131     /** Set the service type */
setServiceType(String s)132     public void setServiceType(String s) {
133         mServiceType = s;
134     }
135 
136     /**
137      * Get the host address. The host address is valid for a resolved service.
138      *
139      * @deprecated Use {@link #getHostAddresses()} to get the entire list of addresses for the host.
140      */
141     @Deprecated
getHost()142     public InetAddress getHost() {
143         return mHostAddresses.size() == 0 ? null : mHostAddresses.get(0);
144     }
145 
146     /**
147      * Set the host address
148      *
149      * @deprecated Use {@link #setHostAddresses(List)} to set multiple addresses for the host.
150      */
151     @Deprecated
setHost(InetAddress s)152     public void setHost(InetAddress s) {
153         setHostAddresses(Collections.singletonList(s));
154     }
155 
156     /**
157      * Get port number. The port number is valid for a resolved service.
158      *
159      * The port is valid for all addresses.
160      * @see #getHostAddresses()
161      */
getPort()162     public int getPort() {
163         return mPort;
164     }
165 
166     /** Set port number */
setPort(int p)167     public void setPort(int p) {
168         mPort = p;
169     }
170 
171     /**
172      * Get the host addresses.
173      *
174      * All host addresses are valid for the resolved service.
175      * All addresses share the same port
176      * @see #getPort()
177      */
178     @NonNull
getHostAddresses()179     public List<InetAddress> getHostAddresses() {
180         return new ArrayList<>(mHostAddresses);
181     }
182 
183     /** Set the host addresses */
setHostAddresses(@onNull List<InetAddress> addresses)184     public void setHostAddresses(@NonNull List<InetAddress> addresses) {
185         mHostAddresses.clear();
186         mHostAddresses.addAll(addresses);
187     }
188 
189     /**
190      * Get the hostname.
191      *
192      * <p>When a service is resolved, it returns the hostname of the resolved service . The top
193      * level domain ".local." is omitted.
194      *
195      * <p>For example, it returns "MyHost" when the service's hostname is "MyHost.local.".
196      *
197      * @hide
198      */
199 //    @FlaggedApi(NsdManager.Flags.NSD_CUSTOM_HOSTNAME_ENABLED)
200     @Nullable
getHostname()201     public String getHostname() {
202         return mHostname;
203     }
204 
205     /**
206      * Set a custom hostname for this service instance for registration.
207      *
208      * <p>A hostname must be in ".local." domain. The ".local." must be omitted when calling this
209      * method.
210      *
211      * <p>For example, you should call setHostname("MyHost") to use the hostname "MyHost.local.".
212      *
213      * <p>If a hostname is set with this method, the addresses set with {@link #setHostAddresses}
214      * will be registered with the hostname.
215      *
216      * <p>If the hostname is null (which is the default for a new {@link NsdServiceInfo}), a random
217      * hostname is used and the addresses of this device will be registered.
218      *
219      * @hide
220      */
221 //    @FlaggedApi(NsdManager.Flags.NSD_CUSTOM_HOSTNAME_ENABLED)
setHostname(@ullable String hostname)222     public void setHostname(@Nullable String hostname) {
223         mHostname = hostname;
224     }
225 
226     /**
227      * Set the public key RDATA to be advertised in a KEY RR (RFC 2535).
228      *
229      * <p>This is the public key of the key pair used for signing a DNS message (e.g. SRP). Clients
230      * typically don't need this information, but the KEY RR is usually published to claim the use
231      * of the DNS name so that another mDNS advertiser can't take over the ownership during a
232      * temporary power down of the original host device.
233      *
234      * <p>When the public key is set to non-null, exactly one KEY RR will be advertised for each of
235      * the service and host name if they are not null.
236      *
237      * @hide // For Thread only
238      */
setPublicKey(@ullable byte[] publicKey)239     public void setPublicKey(@Nullable byte[] publicKey) {
240         if (publicKey == null) {
241             mPublicKey = null;
242             return;
243         }
244         mPublicKey = Arrays.copyOf(publicKey, publicKey.length);
245     }
246 
247     /**
248      * Get the public key RDATA in the KEY RR (RFC 2535) or {@code null} if no KEY RR exists.
249      *
250      * @hide // For Thread only
251      */
252     @Nullable
getPublicKey()253     public byte[] getPublicKey() {
254         if (mPublicKey == null) {
255             return null;
256         }
257         return Arrays.copyOf(mPublicKey, mPublicKey.length);
258     }
259 
260     /**
261      * Unpack txt information from a base-64 encoded byte array.
262      *
263      * @param txtRecordsRawBytes The raw base64 encoded byte array.
264      *
265      * @hide
266      */
setTxtRecords(@onNull byte[] txtRecordsRawBytes)267     public void setTxtRecords(@NonNull byte[] txtRecordsRawBytes) {
268         // There can be multiple TXT records after each other. Each record has to following format:
269         //
270         // byte                  type                  required   meaning
271         // -------------------   -------------------   --------   ----------------------------------
272         // 0                     unsigned 8 bit        yes        size of record excluding this byte
273         // 1 - n                 ASCII but not '='     yes        key
274         // n + 1                 '='                   optional   separator of key and value
275         // n + 2 - record size   uninterpreted bytes   optional   value
276         //
277         // Example legal records:
278         // [11, 'm', 'y', 'k', 'e', 'y', '=', 0x0, 0x4, 0x65, 0x7, 0xff]
279         // [17, 'm', 'y', 'K', 'e', 'y', 'W', 'i', 't', 'h', 'N', 'o', 'V', 'a', 'l', 'u', 'e', '=']
280         // [12, 'm', 'y', 'B', 'o', 'o', 'l', 'e', 'a', 'n', 'K', 'e', 'y']
281         //
282         // Example corrupted records
283         // [3, =, 1, 2]    <- key is empty
284         // [3, 0, =, 2]    <- key contains non-ASCII character. We handle this by replacing the
285         //                    invalid characters instead of skipping the record.
286         // [30, 'a', =, 2] <- length exceeds total left over bytes in the TXT records array, we
287         //                    handle this by reducing the length of the record as needed.
288         int pos = 0;
289         while (pos < txtRecordsRawBytes.length) {
290             // recordLen is an unsigned 8 bit value
291             int recordLen = txtRecordsRawBytes[pos] & 0xff;
292             pos += 1;
293 
294             try {
295                 if (recordLen == 0) {
296                     throw new IllegalArgumentException("Zero sized txt record");
297                 } else if (pos + recordLen > txtRecordsRawBytes.length) {
298                     Log.w(TAG, "Corrupt record length (pos = " + pos + "): " + recordLen);
299                     recordLen = txtRecordsRawBytes.length - pos;
300                 }
301 
302                 // Decode key-value records
303                 String key = null;
304                 byte[] value = null;
305                 int valueLen = 0;
306                 for (int i = pos; i < pos + recordLen; i++) {
307                     if (key == null) {
308                         if (txtRecordsRawBytes[i] == '=') {
309                             key = new String(txtRecordsRawBytes, pos, i - pos,
310                                     StandardCharsets.US_ASCII);
311                         }
312                     } else {
313                         if (value == null) {
314                             value = new byte[recordLen - key.length() - 1];
315                         }
316                         value[valueLen] = txtRecordsRawBytes[i];
317                         valueLen++;
318                     }
319                 }
320 
321                 // If '=' was not found we have a boolean record
322                 if (key == null) {
323                     key = new String(txtRecordsRawBytes, pos, recordLen, StandardCharsets.US_ASCII);
324                 }
325 
326                 if (TextUtils.isEmpty(key)) {
327                     // Empty keys are not allowed (RFC6763 6.4)
328                     throw new IllegalArgumentException("Invalid txt record (key is empty)");
329                 }
330 
331                 if (getAttributes().containsKey(key)) {
332                     // When we have a duplicate record, the later ones are ignored (RFC6763 6.4)
333                     throw new IllegalArgumentException("Invalid txt record (duplicate key \"" + key + "\")");
334                 }
335 
336                 setAttribute(key, value);
337             } catch (IllegalArgumentException e) {
338                 Log.e(TAG, "While parsing txt records (pos = " + pos + "): " + e.getMessage());
339             }
340 
341             pos += recordLen;
342         }
343     }
344 
345     /** @hide */
346     @UnsupportedAppUsage
setAttribute(String key, byte[] value)347     public void setAttribute(String key, byte[] value) {
348         if (TextUtils.isEmpty(key)) {
349             throw new IllegalArgumentException("Key cannot be empty");
350         }
351 
352         // Key must be printable US-ASCII, excluding =.
353         for (int i = 0; i < key.length(); ++i) {
354             char character = key.charAt(i);
355             if (character < 0x20 || character > 0x7E) {
356                 throw new IllegalArgumentException("Key strings must be printable US-ASCII");
357             } else if (character == 0x3D) {
358                 throw new IllegalArgumentException("Key strings must not include '='");
359             }
360         }
361 
362         // Key length + value length must be < 255.
363         if (key.length() + (value == null ? 0 : value.length) >= 255) {
364             throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
365         }
366 
367         // Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
368         if (key.length() > 9) {
369             Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
370         }
371 
372         // Check against total TXT record size limits.
373         // Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
374         int txtRecordSize = getTxtRecordSize();
375         int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
376         if (futureSize > 1300) {
377             throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
378         } else if (futureSize > 400) {
379             Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
380         }
381 
382         mTxtRecord.put(key, value);
383     }
384 
385     /**
386      * Add a service attribute as a key/value pair.
387      *
388      * <p> Service attributes are included as DNS-SD TXT record pairs.
389      *
390      * <p> The key must be US-ASCII printable characters, excluding the '=' character.  Values may
391      * be UTF-8 strings or null.  The total length of key + value must be less than 255 bytes.
392      *
393      * <p> Keys should be short, ideally no more than 9 characters, and unique per instance of
394      * {@link NsdServiceInfo}.  Calling {@link #setAttribute} twice with the same key will overwrite
395      * first value.
396      */
setAttribute(String key, String value)397     public void setAttribute(String key, String value) {
398         try {
399             setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
400         } catch (UnsupportedEncodingException e) {
401             throw new IllegalArgumentException("Value must be UTF-8");
402         }
403     }
404 
405     /** Remove an attribute by key */
removeAttribute(String key)406     public void removeAttribute(String key) {
407         mTxtRecord.remove(key);
408     }
409 
410     /**
411      * Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
412      * valid for a resolved service.
413      *
414      * <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
415      * {@link #removeAttribute}.
416      */
getAttributes()417     public Map<String, byte[]> getAttributes() {
418         return Collections.unmodifiableMap(mTxtRecord);
419     }
420 
getTxtRecordSize()421     private int getTxtRecordSize() {
422         int txtRecordSize = 0;
423         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
424             txtRecordSize += 2;  // One for the length byte, one for the = between key and value.
425             txtRecordSize += entry.getKey().length();
426             byte[] value = entry.getValue();
427             txtRecordSize += value == null ? 0 : value.length;
428         }
429         return txtRecordSize;
430     }
431 
432     /** @hide */
getTxtRecord()433     public @NonNull byte[] getTxtRecord() {
434         int txtRecordSize = getTxtRecordSize();
435         if (txtRecordSize == 0) {
436             return new byte[]{};
437         }
438 
439         byte[] txtRecord = new byte[txtRecordSize];
440         int ptr = 0;
441         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
442             String key = entry.getKey();
443             byte[] value = entry.getValue();
444 
445             // One byte to record the length of this key/value pair.
446             txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
447 
448             // The key, in US-ASCII.
449             // Note: use the StandardCharsets const here because it doesn't raise exceptions and we
450             // already know the key is ASCII at this point.
451             System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
452                     key.length());
453             ptr += key.length();
454 
455             // US-ASCII '=' character.
456             txtRecord[ptr++] = (byte)'=';
457 
458             // The value, as any raw bytes.
459             if (value != null) {
460                 System.arraycopy(value, 0, txtRecord, ptr, value.length);
461                 ptr += value.length;
462             }
463         }
464         return txtRecord;
465     }
466 
467     /**
468      * Get the network where the service can be found.
469      *
470      * This is set if this {@link NsdServiceInfo} was obtained from
471      * {@link NsdManager#discoverServices} or {@link NsdManager#resolveService}, unless the service
472      * was found on a network interface that does not have a {@link Network} (such as a tethering
473      * downstream, where services are advertised from devices connected to this device via
474      * tethering).
475      */
476     @Nullable
getNetwork()477     public Network getNetwork() {
478         return mNetwork;
479     }
480 
481     /**
482      * Set the network where the service can be found.
483      * @param network The network, or null to search for, or to announce, the service on all
484      *                connected networks.
485      */
setNetwork(@ullable Network network)486     public void setNetwork(@Nullable Network network) {
487         mNetwork = network;
488     }
489 
490     /**
491      * Get the index of the network interface where the service was found.
492      *
493      * This is only set when the service was found on an interface that does not have a usable
494      * Network, in which case {@link #getNetwork()} returns null.
495      * @return The interface index as per {@link java.net.NetworkInterface#getIndex}, or 0 if unset.
496      * @hide
497      */
getInterfaceIndex()498     public int getInterfaceIndex() {
499         return mInterfaceIndex;
500     }
501 
502     /**
503      * Set the index of the network interface where the service was found.
504      * @hide
505      */
setInterfaceIndex(int interfaceIndex)506     public void setInterfaceIndex(int interfaceIndex) {
507         mInterfaceIndex = interfaceIndex;
508     }
509 
510     /**
511      * Sets the subtypes to be advertised for this service instance.
512      *
513      * The elements in {@code subtypes} should be the subtype identifiers which have the trailing
514      * "._sub" removed. For example, the subtype should be "_printer" for
515      * "_printer._sub._http._tcp.local".
516      *
517      * Only one subtype will be registered if multiple elements of {@code subtypes} have the same
518      * case-insensitive value.
519      */
520     @FlaggedApi(NsdManager.Flags.NSD_SUBTYPES_SUPPORT_ENABLED)
setSubtypes(@onNull Set<String> subtypes)521     public void setSubtypes(@NonNull Set<String> subtypes) {
522         mSubtypes.clear();
523         mSubtypes.addAll(subtypes);
524     }
525 
526     /**
527      * Returns subtypes of this service instance.
528      *
529      * When this object is returned by the service discovery/browse APIs (etc. {@link
530      * NsdManager.DiscoveryListener}), the return value may or may not include the subtypes of this
531      * service.
532      */
533     @FlaggedApi(NsdManager.Flags.NSD_SUBTYPES_SUPPORT_ENABLED)
534     @NonNull
getSubtypes()535     public Set<String> getSubtypes() {
536         return Collections.unmodifiableSet(mSubtypes);
537     }
538 
539     /**
540      * Sets the timestamp after when this service is expired.
541      *
542      * Note: the value after the decimal point (in unit of seconds) will be discarded. For
543      * example, {@code 30} seconds will be used when {@code Duration.ofSeconds(30L, 50_000L)}
544      * is provided.
545      *
546      * @hide
547      */
setExpirationTime(@ullable Instant expirationTime)548     public void setExpirationTime(@Nullable Instant expirationTime) {
549         if (expirationTime == null) {
550             mExpirationTime = null;
551         } else {
552             mExpirationTime = Instant.ofEpochSecond(expirationTime.getEpochSecond());
553         }
554     }
555 
556     /**
557      * Returns the timestamp after when this service is expired or {@code null} if it's unknown.
558      *
559      * A service is considered expired if any of its DNS record is expired.
560      *
561      * Clients that are depending on the refreshness of the service information should not continue
562      * use this service after the returned timestamp. Instead, clients may re-send queries for the
563      * service to get updated the service information.
564      *
565      * @hide
566      */
567     // @FlaggedApi(NsdManager.Flags.NSD_CUSTOM_TTL_ENABLED)
568     @Nullable
getExpirationTime()569     public Instant getExpirationTime() {
570         return mExpirationTime;
571     }
572 
573     @Override
toString()574     public String toString() {
575         StringBuilder sb = new StringBuilder();
576         sb.append("name: ").append(mServiceName)
577                 .append(", type: ").append(mServiceType)
578                 .append(", subtypes: ").append(TextUtils.join(", ", mSubtypes))
579                 .append(", hostAddresses: ").append(TextUtils.join(", ", mHostAddresses))
580                 .append(", hostname: ").append(mHostname)
581                 .append(", port: ").append(mPort)
582                 .append(", network: ").append(mNetwork)
583                 .append(", expirationTime: ").append(mExpirationTime);
584 
585         final StringJoiner txtJoiner =
586                 new StringJoiner(", " /* delimiter */, "{" /* prefix */, "}" /* suffix */);
587 
588         sb.append(", txtRecord: ");
589         for (int i = 0; i < mTxtRecord.size(); i++) {
590             txtJoiner.add(mTxtRecord.keyAt(i) + "=" + getPrintableTxtValue(mTxtRecord.valueAt(i)));
591         }
592         sb.append(txtJoiner.toString());
593         return sb.toString();
594     }
595 
596     /**
597      * Returns printable string for {@code txtValue}.
598      *
599      * If {@code txtValue} contains non-printable ASCII characters, a HEX string with prefix "0x"
600      * will be returned. Otherwise, the ASCII string of {@code txtValue} is returned.
601      *
602      */
getPrintableTxtValue(@ullable byte[] txtValue)603     private static String getPrintableTxtValue(@Nullable byte[] txtValue) {
604         if (txtValue == null) {
605             return "(null)";
606         }
607 
608         if (containsNonPrintableChars(txtValue)) {
609             return "0x" + toHexString(txtValue);
610         }
611 
612         return new String(txtValue, StandardCharsets.US_ASCII);
613     }
614 
615     /**
616      * Returns {@code true} if {@code txtValue} contains non-printable ASCII characters.
617      *
618      * The printable characters are in range of [32, 126].
619      */
containsNonPrintableChars(byte[] txtValue)620     private static boolean containsNonPrintableChars(byte[] txtValue) {
621         for (int i = 0; i < txtValue.length; i++) {
622             if (txtValue[i] < 32 || txtValue[i] > 126) {
623                 return true;
624             }
625         }
626         return false;
627     }
628 
629     /** Implement the Parcelable interface */
describeContents()630     public int describeContents() {
631         return 0;
632     }
633 
634     /** Implement the Parcelable interface */
writeToParcel(Parcel dest, int flags)635     public void writeToParcel(Parcel dest, int flags) {
636         dest.writeString(mServiceName);
637         dest.writeString(mServiceType);
638         dest.writeStringList(new ArrayList<>(mSubtypes));
639         dest.writeInt(mPort);
640 
641         // TXT record key/value pairs.
642         dest.writeInt(mTxtRecord.size());
643         for (String key : mTxtRecord.keySet()) {
644             byte[] value = mTxtRecord.get(key);
645             if (value != null) {
646                 dest.writeInt(1);
647                 dest.writeInt(value.length);
648                 dest.writeByteArray(value);
649             } else {
650                 dest.writeInt(0);
651             }
652             dest.writeString(key);
653         }
654 
655         dest.writeParcelable(mNetwork, 0);
656         dest.writeInt(mInterfaceIndex);
657         dest.writeInt(mHostAddresses.size());
658         for (InetAddress address : mHostAddresses) {
659             InetAddressUtils.parcelInetAddress(dest, address, flags);
660         }
661         dest.writeString(mHostname);
662         dest.writeLong(mExpirationTime != null ? mExpirationTime.getEpochSecond() : -1);
663         dest.writeByteArray(mPublicKey);
664     }
665 
666     /** Implement the Parcelable interface */
667     public static final @android.annotation.NonNull Creator<NsdServiceInfo> CREATOR =
668         new Creator<NsdServiceInfo>() {
669             public NsdServiceInfo createFromParcel(Parcel in) {
670                 NsdServiceInfo info = new NsdServiceInfo();
671                 info.mServiceName = in.readString();
672                 info.mServiceType = in.readString();
673                 info.setSubtypes(new ArraySet<>(in.createStringArrayList()));
674                 info.mPort = in.readInt();
675 
676                 // TXT record key/value pairs.
677                 int recordCount = in.readInt();
678                 for (int i = 0; i < recordCount; ++i) {
679                     byte[] valueArray = null;
680                     if (in.readInt() == 1) {
681                         int valueLength = in.readInt();
682                         valueArray = new byte[valueLength];
683                         in.readByteArray(valueArray);
684                     }
685                     info.mTxtRecord.put(in.readString(), valueArray);
686                 }
687                 info.mNetwork = in.readParcelable(null, Network.class);
688                 info.mInterfaceIndex = in.readInt();
689                 int size = in.readInt();
690                 for (int i = 0; i < size; i++) {
691                     info.mHostAddresses.add(InetAddressUtils.unparcelInetAddress(in));
692                 }
693                 info.mHostname = in.readString();
694                 final long seconds = in.readLong();
695                 info.setExpirationTime(seconds < 0 ? null : Instant.ofEpochSecond(seconds));
696                 info.mPublicKey = in.createByteArray();
697                 return info;
698             }
699 
700             public NsdServiceInfo[] newArray(int size) {
701                 return new NsdServiceInfo[size];
702             }
703         };
704 }
705