1 /* 2 * Copyright (C) 2024 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 com.android.server.connectivity.mdns; 18 19 import static com.android.net.module.util.HexDump.toHexString; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 24 import androidx.annotation.VisibleForTesting; 25 26 import java.io.IOException; 27 import java.util.Arrays; 28 29 /** An mDNS "KEY" record, which contains a public key for a name. See RFC 2535. */ 30 @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) 31 public class MdnsKeyRecord extends MdnsRecord { 32 @Nullable private byte[] rData; 33 MdnsKeyRecord(@onNull String[] name, @NonNull MdnsPacketReader reader)34 public MdnsKeyRecord(@NonNull String[] name, @NonNull MdnsPacketReader reader) 35 throws IOException { 36 this(name, reader, false); 37 } 38 MdnsKeyRecord(@onNull String[] name, @NonNull MdnsPacketReader reader, boolean isQuestion)39 public MdnsKeyRecord(@NonNull String[] name, @NonNull MdnsPacketReader reader, 40 boolean isQuestion) throws IOException { 41 super(name, TYPE_KEY, reader, isQuestion); 42 } 43 MdnsKeyRecord(@onNull String[] name, boolean isUnicast)44 public MdnsKeyRecord(@NonNull String[] name, boolean isUnicast) { 45 super(name, TYPE_KEY, 46 MdnsConstants.QCLASS_INTERNET | (isUnicast ? MdnsConstants.QCLASS_UNICAST : 0), 47 0L /* receiptTimeMillis */, false /* cacheFlush */, 0L /* ttlMillis */); 48 } 49 MdnsKeyRecord(@onNull String[] name, long receiptTimeMillis, boolean cacheFlush, long ttlMillis, @Nullable byte[] rData)50 public MdnsKeyRecord(@NonNull String[] name, long receiptTimeMillis, boolean cacheFlush, 51 long ttlMillis, @Nullable byte[] rData) { 52 super(name, TYPE_KEY, MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush, 53 ttlMillis); 54 if (rData != null) { 55 this.rData = Arrays.copyOf(rData, rData.length); 56 } 57 } 58 /** Returns the KEY RDATA in bytes **/ getRData()59 public byte[] getRData() { 60 if (rData == null) { 61 return null; 62 } 63 return Arrays.copyOf(rData, rData.length); 64 } 65 66 @Override readData(MdnsPacketReader reader)67 protected void readData(MdnsPacketReader reader) throws IOException { 68 rData = new byte[reader.getRemaining()]; 69 reader.readBytes(rData); 70 } 71 72 @Override writeData(MdnsPacketWriter writer)73 protected void writeData(MdnsPacketWriter writer) throws IOException { 74 if (rData != null) { 75 writer.writeBytes(rData); 76 } 77 } 78 79 @Override toString()80 public String toString() { 81 return "KEY: " + toHexString(rData); 82 } 83 84 @Override hashCode()85 public int hashCode() { 86 return (super.hashCode() * 31) + Arrays.hashCode(rData); 87 } 88 89 @Override equals(@ullable Object other)90 public boolean equals(@Nullable Object other) { 91 if (this == other) { 92 return true; 93 } 94 if (!(other instanceof MdnsKeyRecord)) { 95 return false; 96 } 97 98 return super.equals(other) && Arrays.equals(rData, ((MdnsKeyRecord) other).rData); 99 } 100 }