1 /* 2 * Copyright (C) 2021 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 android.annotation.Nullable; 20 21 import java.io.IOException; 22 import java.net.Inet4Address; 23 import java.net.Inet6Address; 24 import java.net.InetAddress; 25 import java.net.UnknownHostException; 26 import java.util.Locale; 27 import java.util.Objects; 28 29 /** An mDNS "AAAA" or "A" record, which holds an IPv6 or IPv4 address. */ 30 public class MdnsInetAddressRecord extends MdnsRecord { 31 @Nullable private Inet6Address inet6Address; 32 @Nullable private Inet4Address inet4Address; 33 34 /** 35 * Constructs the {@link MdnsRecord} 36 * 37 * @param name the service host name 38 * @param type the type of record (either Type 'AAAA' or Type 'A') 39 * @param reader the reader to read the record from. 40 */ MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader)41 public MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader) 42 throws IOException { 43 this(name, type, reader, false); 44 } 45 46 /** 47 * Constructs the {@link MdnsRecord} 48 * 49 * @param name the service host name 50 * @param type the type of record (either Type 'AAAA' or Type 'A') 51 * @param reader the reader to read the record from. 52 * @param isQuestion whether the record is in the question section 53 */ MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader, boolean isQuestion)54 public MdnsInetAddressRecord(String[] name, int type, MdnsPacketReader reader, 55 boolean isQuestion) 56 throws IOException { 57 super(name, type, reader, isQuestion); 58 } 59 MdnsInetAddressRecord(String[] name, int type, boolean isUnicast)60 public MdnsInetAddressRecord(String[] name, int type, boolean isUnicast) { 61 super(name, type, 62 MdnsConstants.QCLASS_INTERNET | (isUnicast ? MdnsConstants.QCLASS_UNICAST : 0), 63 0L /* receiptTimeMillis */, false /* cacheFlush */, 0L /* ttlMillis */); 64 } 65 MdnsInetAddressRecord(String[] name, long receiptTimeMillis, boolean cacheFlush, long ttlMillis, InetAddress address)66 public MdnsInetAddressRecord(String[] name, long receiptTimeMillis, boolean cacheFlush, 67 long ttlMillis, InetAddress address) { 68 super(name, address instanceof Inet4Address ? TYPE_A : TYPE_AAAA, 69 MdnsConstants.QCLASS_INTERNET, receiptTimeMillis, cacheFlush, ttlMillis); 70 if (address instanceof Inet4Address) { 71 inet4Address = (Inet4Address) address; 72 } else { 73 inet6Address = (Inet6Address) address; 74 } 75 } 76 77 /** Returns the IPv6 address. */ 78 @Nullable getInet6Address()79 public Inet6Address getInet6Address() { 80 return inet6Address; 81 } 82 83 /** Returns the IPv4 address. */ 84 @Nullable getInet4Address()85 public Inet4Address getInet4Address() { 86 return inet4Address; 87 } 88 89 @Override readData(MdnsPacketReader reader)90 protected void readData(MdnsPacketReader reader) throws IOException { 91 int size = 4; 92 if (super.getType() == MdnsRecord.TYPE_AAAA) { 93 size = 16; 94 } 95 byte[] buf = new byte[size]; 96 reader.readBytes(buf); 97 try { 98 InetAddress address = InetAddress.getByAddress(buf); 99 if (address instanceof Inet4Address) { 100 inet4Address = (Inet4Address) address; 101 inet6Address = null; 102 } else if (address instanceof Inet6Address) { 103 inet4Address = null; 104 inet6Address = (Inet6Address) address; 105 } else { 106 inet4Address = null; 107 inet6Address = null; 108 } 109 } catch (UnknownHostException e) { 110 // Ignore exception 111 } 112 } 113 114 @Override writeData(MdnsPacketWriter writer)115 protected void writeData(MdnsPacketWriter writer) throws IOException { 116 byte[] buf = null; 117 if (inet4Address != null) { 118 buf = inet4Address.getAddress(); 119 } else if (inet6Address != null) { 120 buf = inet6Address.getAddress(); 121 } 122 if (buf != null) { 123 writer.writeBytes(buf); 124 } 125 } 126 127 @Override toString()128 public String toString() { 129 String type = "AAAA"; 130 if (super.getType() == MdnsRecord.TYPE_A) { 131 type = "A"; 132 } 133 return String.format( 134 Locale.ROOT, "%s: Inet4Address: %s Inet6Address: %s", type, inet4Address, 135 inet6Address); 136 } 137 138 @Override hashCode()139 public int hashCode() { 140 return (super.hashCode() * 31) 141 + Objects.hashCode(inet4Address) 142 + Objects.hashCode(inet6Address); 143 } 144 145 @Override equals(@ullable Object other)146 public boolean equals(@Nullable Object other) { 147 if (this == other) { 148 return true; 149 } 150 if (!(other instanceof MdnsInetAddressRecord)) { 151 return false; 152 } 153 154 return super.equals(other) 155 && Objects.equals(inet4Address, ((MdnsInetAddressRecord) other).inet4Address) 156 && Objects.equals(inet6Address, ((MdnsInetAddressRecord) other).inet6Address); 157 } 158 } 159