• 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.os.Parcelable;
21 import android.os.Parcel;
22 import android.text.TextUtils;
23 import android.util.Base64;
24 import android.util.Log;
25 import android.util.ArrayMap;
26 
27 import java.io.UnsupportedEncodingException;
28 import java.net.InetAddress;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Collections;
31 import java.util.Map;
32 
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<String, byte[]>();
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 */
setAttribute(String key, byte[] value)189     public void setAttribute(String key, byte[] value) {
190         if (TextUtils.isEmpty(key)) {
191             throw new IllegalArgumentException("Key cannot be empty");
192         }
193 
194         // Key must be printable US-ASCII, excluding =.
195         for (int i = 0; i < key.length(); ++i) {
196             char character = key.charAt(i);
197             if (character < 0x20 || character > 0x7E) {
198                 throw new IllegalArgumentException("Key strings must be printable US-ASCII");
199             } else if (character == 0x3D) {
200                 throw new IllegalArgumentException("Key strings must not include '='");
201             }
202         }
203 
204         // Key length + value length must be < 255.
205         if (key.length() + (value == null ? 0 : value.length) >= 255) {
206             throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
207         }
208 
209         // Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
210         if (key.length() > 9) {
211             Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
212         }
213 
214         // Check against total TXT record size limits.
215         // Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
216         int txtRecordSize = getTxtRecordSize();
217         int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
218         if (futureSize > 1300) {
219             throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
220         } else if (futureSize > 400) {
221             Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
222         }
223 
224         mTxtRecord.put(key, value);
225     }
226 
227     /**
228      * Add a service attribute as a key/value pair.
229      *
230      * <p> Service attributes are included as DNS-SD TXT record pairs.
231      *
232      * <p> The key must be US-ASCII printable characters, excluding the '=' character.  Values may
233      * be UTF-8 strings or null.  The total length of key + value must be less than 255 bytes.
234      *
235      * <p> Keys should be short, ideally no more than 9 characters, and unique per instance of
236      * {@link NsdServiceInfo}.  Calling {@link #setAttribute} twice with the same key will overwrite
237      * first value.
238      */
setAttribute(String key, String value)239     public void setAttribute(String key, String value) {
240         try {
241             setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
242         } catch (UnsupportedEncodingException e) {
243             throw new IllegalArgumentException("Value must be UTF-8");
244         }
245     }
246 
247     /** Remove an attribute by key */
removeAttribute(String key)248     public void removeAttribute(String key) {
249         mTxtRecord.remove(key);
250     }
251 
252     /**
253      * Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
254      * valid for a resolved service.
255      *
256      * <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
257      * {@link #removeAttribute}.
258      */
getAttributes()259     public Map<String, byte[]> getAttributes() {
260         return Collections.unmodifiableMap(mTxtRecord);
261     }
262 
getTxtRecordSize()263     private int getTxtRecordSize() {
264         int txtRecordSize = 0;
265         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
266             txtRecordSize += 2;  // One for the length byte, one for the = between key and value.
267             txtRecordSize += entry.getKey().length();
268             byte[] value = entry.getValue();
269             txtRecordSize += value == null ? 0 : value.length;
270         }
271         return txtRecordSize;
272     }
273 
274     /** @hide */
getTxtRecord()275     public @NonNull byte[] getTxtRecord() {
276         int txtRecordSize = getTxtRecordSize();
277         if (txtRecordSize == 0) {
278             return new byte[]{};
279         }
280 
281         byte[] txtRecord = new byte[txtRecordSize];
282         int ptr = 0;
283         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
284             String key = entry.getKey();
285             byte[] value = entry.getValue();
286 
287             // One byte to record the length of this key/value pair.
288             txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
289 
290             // The key, in US-ASCII.
291             // Note: use the StandardCharsets const here because it doesn't raise exceptions and we
292             // already know the key is ASCII at this point.
293             System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
294                     key.length());
295             ptr += key.length();
296 
297             // US-ASCII '=' character.
298             txtRecord[ptr++] = (byte)'=';
299 
300             // The value, as any raw bytes.
301             if (value != null) {
302                 System.arraycopy(value, 0, txtRecord, ptr, value.length);
303                 ptr += value.length;
304             }
305         }
306         return txtRecord;
307     }
308 
toString()309     public String toString() {
310         StringBuffer sb = new StringBuffer();
311 
312         sb.append("name: ").append(mServiceName)
313                 .append(", type: ").append(mServiceType)
314                 .append(", host: ").append(mHost)
315                 .append(", port: ").append(mPort);
316 
317         byte[] txtRecord = getTxtRecord();
318         if (txtRecord != null) {
319             sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
320         }
321         return sb.toString();
322     }
323 
324     /** Implement the Parcelable interface */
describeContents()325     public int describeContents() {
326         return 0;
327     }
328 
329     /** Implement the Parcelable interface */
writeToParcel(Parcel dest, int flags)330     public void writeToParcel(Parcel dest, int flags) {
331         dest.writeString(mServiceName);
332         dest.writeString(mServiceType);
333         if (mHost != null) {
334             dest.writeInt(1);
335             dest.writeByteArray(mHost.getAddress());
336         } else {
337             dest.writeInt(0);
338         }
339         dest.writeInt(mPort);
340 
341         // TXT record key/value pairs.
342         dest.writeInt(mTxtRecord.size());
343         for (String key : mTxtRecord.keySet()) {
344             byte[] value = mTxtRecord.get(key);
345             if (value != null) {
346                 dest.writeInt(1);
347                 dest.writeInt(value.length);
348                 dest.writeByteArray(value);
349             } else {
350                 dest.writeInt(0);
351             }
352             dest.writeString(key);
353         }
354     }
355 
356     /** Implement the Parcelable interface */
357     public static final Creator<NsdServiceInfo> CREATOR =
358         new Creator<NsdServiceInfo>() {
359             public NsdServiceInfo createFromParcel(Parcel in) {
360                 NsdServiceInfo info = new NsdServiceInfo();
361                 info.mServiceName = in.readString();
362                 info.mServiceType = in.readString();
363 
364                 if (in.readInt() == 1) {
365                     try {
366                         info.mHost = InetAddress.getByAddress(in.createByteArray());
367                     } catch (java.net.UnknownHostException e) {}
368                 }
369 
370                 info.mPort = in.readInt();
371 
372                 // TXT record key/value pairs.
373                 int recordCount = in.readInt();
374                 for (int i = 0; i < recordCount; ++i) {
375                     byte[] valueArray = null;
376                     if (in.readInt() == 1) {
377                         int valueLength = in.readInt();
378                         valueArray = new byte[valueLength];
379                         in.readByteArray(valueArray);
380                     }
381                     info.mTxtRecord.put(in.readString(), valueArray);
382                 }
383                 return info;
384             }
385 
386             public NsdServiceInfo[] newArray(int size) {
387                 return new NsdServiceInfo[size];
388             }
389         };
390 }
391