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