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