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 android.annotation.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 import android.util.ArrayMap;
25 import android.util.Base64;
26 import android.util.Log;
27 
28 import java.io.UnsupportedEncodingException;
29 import java.net.InetAddress;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Collections;
32 import java.util.Map;
33 
34 /**
35  * A class representing service information for network service discovery
36  * {@see NsdManager}
37  */
38 public final class NsdServiceInfo implements Parcelable {
39 
40     private static final String TAG = "NsdServiceInfo";
41 
42     private String mServiceName;
43 
44     private String mServiceType;
45 
46     private final ArrayMap<String, byte[]> mTxtRecord = new ArrayMap<>();
47 
48     private InetAddress mHost;
49 
50     private int mPort;
51 
NsdServiceInfo()52     public NsdServiceInfo() {
53     }
54 
55     /** @hide */
NsdServiceInfo(String sn, String rt)56     public NsdServiceInfo(String sn, String rt) {
57         mServiceName = sn;
58         mServiceType = rt;
59     }
60 
61     /** Get the service name */
getServiceName()62     public String getServiceName() {
63         return mServiceName;
64     }
65 
66     /** Set the service name */
setServiceName(String s)67     public void setServiceName(String s) {
68         mServiceName = s;
69     }
70 
71     /** Get the service type */
getServiceType()72     public String getServiceType() {
73         return mServiceType;
74     }
75 
76     /** Set the service type */
setServiceType(String s)77     public void setServiceType(String s) {
78         mServiceType = s;
79     }
80 
81     /** Get the host address. The host address is valid for a resolved service. */
getHost()82     public InetAddress getHost() {
83         return mHost;
84     }
85 
86     /** Set the host address */
setHost(InetAddress s)87     public void setHost(InetAddress s) {
88         mHost = s;
89     }
90 
91     /** Get port number. The port number is valid for a resolved service. */
getPort()92     public int getPort() {
93         return mPort;
94     }
95 
96     /** Set port number */
setPort(int p)97     public void setPort(int p) {
98         mPort = p;
99     }
100 
101     /**
102      * Unpack txt information from a base-64 encoded byte array.
103      *
104      * @param rawRecords The raw base64 encoded records string read from netd.
105      *
106      * @hide
107      */
setTxtRecords(@onNull String rawRecords)108     public void setTxtRecords(@NonNull String rawRecords) {
109         byte[] txtRecordsRawBytes = Base64.decode(rawRecords, Base64.DEFAULT);
110 
111         // There can be multiple TXT records after each other. Each record has to following format:
112         //
113         // byte                  type                  required   meaning
114         // -------------------   -------------------   --------   ----------------------------------
115         // 0                     unsigned 8 bit        yes        size of record excluding this byte
116         // 1 - n                 ASCII but not '='     yes        key
117         // n + 1                 '='                   optional   separator of key and value
118         // n + 2 - record size   uninterpreted bytes   optional   value
119         //
120         // Example legal records:
121         // [11, 'm', 'y', 'k', 'e', 'y', '=', 0x0, 0x4, 0x65, 0x7, 0xff]
122         // [17, 'm', 'y', 'K', 'e', 'y', 'W', 'i', 't', 'h', 'N', 'o', 'V', 'a', 'l', 'u', 'e', '=']
123         // [12, 'm', 'y', 'B', 'o', 'o', 'l', 'e', 'a', 'n', 'K', 'e', 'y']
124         //
125         // Example corrupted records
126         // [3, =, 1, 2]    <- key is empty
127         // [3, 0, =, 2]    <- key contains non-ASCII character. We handle this by replacing the
128         //                    invalid characters instead of skipping the record.
129         // [30, 'a', =, 2] <- length exceeds total left over bytes in the TXT records array, we
130         //                    handle this by reducing the length of the record as needed.
131         int pos = 0;
132         while (pos < txtRecordsRawBytes.length) {
133             // recordLen is an unsigned 8 bit value
134             int recordLen = txtRecordsRawBytes[pos] & 0xff;
135             pos += 1;
136 
137             try {
138                 if (recordLen == 0) {
139                     throw new IllegalArgumentException("Zero sized txt record");
140                 } else if (pos + recordLen > txtRecordsRawBytes.length) {
141                     Log.w(TAG, "Corrupt record length (pos = " + pos + "): " + recordLen);
142                     recordLen = txtRecordsRawBytes.length - pos;
143                 }
144 
145                 // Decode key-value records
146                 String key = null;
147                 byte[] value = null;
148                 int valueLen = 0;
149                 for (int i = pos; i < pos + recordLen; i++) {
150                     if (key == null) {
151                         if (txtRecordsRawBytes[i] == '=') {
152                             key = new String(txtRecordsRawBytes, pos, i - pos,
153                                     StandardCharsets.US_ASCII);
154                         }
155                     } else {
156                         if (value == null) {
157                             value = new byte[recordLen - key.length() - 1];
158                         }
159                         value[valueLen] = txtRecordsRawBytes[i];
160                         valueLen++;
161                     }
162                 }
163 
164                 // If '=' was not found we have a boolean record
165                 if (key == null) {
166                     key = new String(txtRecordsRawBytes, pos, recordLen, StandardCharsets.US_ASCII);
167                 }
168 
169                 if (TextUtils.isEmpty(key)) {
170                     // Empty keys are not allowed (RFC6763 6.4)
171                     throw new IllegalArgumentException("Invalid txt record (key is empty)");
172                 }
173 
174                 if (getAttributes().containsKey(key)) {
175                     // When we have a duplicate record, the later ones are ignored (RFC6763 6.4)
176                     throw new IllegalArgumentException("Invalid txt record (duplicate key \"" + key + "\")");
177                 }
178 
179                 setAttribute(key, value);
180             } catch (IllegalArgumentException e) {
181                 Log.e(TAG, "While parsing txt records (pos = " + pos + "): " + e.getMessage());
182             }
183 
184             pos += recordLen;
185         }
186     }
187 
188     /** @hide */
189     @UnsupportedAppUsage
setAttribute(String key, byte[] value)190     public void setAttribute(String key, byte[] value) {
191         if (TextUtils.isEmpty(key)) {
192             throw new IllegalArgumentException("Key cannot be empty");
193         }
194 
195         // Key must be printable US-ASCII, excluding =.
196         for (int i = 0; i < key.length(); ++i) {
197             char character = key.charAt(i);
198             if (character < 0x20 || character > 0x7E) {
199                 throw new IllegalArgumentException("Key strings must be printable US-ASCII");
200             } else if (character == 0x3D) {
201                 throw new IllegalArgumentException("Key strings must not include '='");
202             }
203         }
204 
205         // Key length + value length must be < 255.
206         if (key.length() + (value == null ? 0 : value.length) >= 255) {
207             throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
208         }
209 
210         // Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
211         if (key.length() > 9) {
212             Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
213         }
214 
215         // Check against total TXT record size limits.
216         // Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
217         int txtRecordSize = getTxtRecordSize();
218         int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
219         if (futureSize > 1300) {
220             throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
221         } else if (futureSize > 400) {
222             Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
223         }
224 
225         mTxtRecord.put(key, value);
226     }
227 
228     /**
229      * Add a service attribute as a key/value pair.
230      *
231      * <p> Service attributes are included as DNS-SD TXT record pairs.
232      *
233      * <p> The key must be US-ASCII printable characters, excluding the '=' character.  Values may
234      * be UTF-8 strings or null.  The total length of key + value must be less than 255 bytes.
235      *
236      * <p> Keys should be short, ideally no more than 9 characters, and unique per instance of
237      * {@link NsdServiceInfo}.  Calling {@link #setAttribute} twice with the same key will overwrite
238      * first value.
239      */
setAttribute(String key, String value)240     public void setAttribute(String key, String value) {
241         try {
242             setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
243         } catch (UnsupportedEncodingException e) {
244             throw new IllegalArgumentException("Value must be UTF-8");
245         }
246     }
247 
248     /** Remove an attribute by key */
removeAttribute(String key)249     public void removeAttribute(String key) {
250         mTxtRecord.remove(key);
251     }
252 
253     /**
254      * Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
255      * valid for a resolved service.
256      *
257      * <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
258      * {@link #removeAttribute}.
259      */
getAttributes()260     public Map<String, byte[]> getAttributes() {
261         return Collections.unmodifiableMap(mTxtRecord);
262     }
263 
getTxtRecordSize()264     private int getTxtRecordSize() {
265         int txtRecordSize = 0;
266         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
267             txtRecordSize += 2;  // One for the length byte, one for the = between key and value.
268             txtRecordSize += entry.getKey().length();
269             byte[] value = entry.getValue();
270             txtRecordSize += value == null ? 0 : value.length;
271         }
272         return txtRecordSize;
273     }
274 
275     /** @hide */
getTxtRecord()276     public @NonNull byte[] getTxtRecord() {
277         int txtRecordSize = getTxtRecordSize();
278         if (txtRecordSize == 0) {
279             return new byte[]{};
280         }
281 
282         byte[] txtRecord = new byte[txtRecordSize];
283         int ptr = 0;
284         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
285             String key = entry.getKey();
286             byte[] value = entry.getValue();
287 
288             // One byte to record the length of this key/value pair.
289             txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
290 
291             // The key, in US-ASCII.
292             // Note: use the StandardCharsets const here because it doesn't raise exceptions and we
293             // already know the key is ASCII at this point.
294             System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
295                     key.length());
296             ptr += key.length();
297 
298             // US-ASCII '=' character.
299             txtRecord[ptr++] = (byte)'=';
300 
301             // The value, as any raw bytes.
302             if (value != null) {
303                 System.arraycopy(value, 0, txtRecord, ptr, value.length);
304                 ptr += value.length;
305             }
306         }
307         return txtRecord;
308     }
309 
toString()310     public String toString() {
311         StringBuffer sb = new StringBuffer();
312 
313         sb.append("name: ").append(mServiceName)
314                 .append(", type: ").append(mServiceType)
315                 .append(", host: ").append(mHost)
316                 .append(", port: ").append(mPort);
317 
318         byte[] txtRecord = getTxtRecord();
319         if (txtRecord != null) {
320             sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
321         }
322         return sb.toString();
323     }
324 
325     /** Implement the Parcelable interface */
describeContents()326     public int describeContents() {
327         return 0;
328     }
329 
330     /** Implement the Parcelable interface */
writeToParcel(Parcel dest, int flags)331     public void writeToParcel(Parcel dest, int flags) {
332         dest.writeString(mServiceName);
333         dest.writeString(mServiceType);
334         if (mHost != null) {
335             dest.writeInt(1);
336             dest.writeByteArray(mHost.getAddress());
337         } else {
338             dest.writeInt(0);
339         }
340         dest.writeInt(mPort);
341 
342         // TXT record key/value pairs.
343         dest.writeInt(mTxtRecord.size());
344         for (String key : mTxtRecord.keySet()) {
345             byte[] value = mTxtRecord.get(key);
346             if (value != null) {
347                 dest.writeInt(1);
348                 dest.writeInt(value.length);
349                 dest.writeByteArray(value);
350             } else {
351                 dest.writeInt(0);
352             }
353             dest.writeString(key);
354         }
355     }
356 
357     /** Implement the Parcelable interface */
358     public static final @android.annotation.NonNull Creator<NsdServiceInfo> CREATOR =
359         new Creator<NsdServiceInfo>() {
360             public NsdServiceInfo createFromParcel(Parcel in) {
361                 NsdServiceInfo info = new NsdServiceInfo();
362                 info.mServiceName = in.readString();
363                 info.mServiceType = in.readString();
364 
365                 if (in.readInt() == 1) {
366                     try {
367                         info.mHost = InetAddress.getByAddress(in.createByteArray());
368                     } catch (java.net.UnknownHostException e) {}
369                 }
370 
371                 info.mPort = in.readInt();
372 
373                 // TXT record key/value pairs.
374                 int recordCount = in.readInt();
375                 for (int i = 0; i < recordCount; ++i) {
376                     byte[] valueArray = null;
377                     if (in.readInt() == 1) {
378                         int valueLength = in.readInt();
379                         valueArray = new byte[valueLength];
380                         in.readByteArray(valueArray);
381                     }
382                     info.mTxtRecord.put(in.readString(), valueArray);
383                 }
384                 return info;
385             }
386 
387             public NsdServiceInfo[] newArray(int size) {
388                 return new NsdServiceInfo[size];
389             }
390         };
391 }
392