blob: 7b845be749216ef0ef7091faac0c156d1389ba14 [file] [log] [blame]
Irfan Sherifffa291e62012-04-04 13:18:17 -07001/*
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
17package android.net.nsd;
18
Philip P. Moltmann312c61e2016-03-16 10:15:39 -070019import android.annotation.NonNull;
Irfan Sherifffa291e62012-04-04 13:18:17 -070020import android.os.Parcelable;
21import android.os.Parcel;
Philip P. Moltmann312c61e2016-03-16 10:15:39 -070022import android.text.TextUtils;
23import android.util.Base64;
Christopher Laneb72d8b42014-03-17 16:35:45 -070024import android.util.Log;
25import android.util.ArrayMap;
Irfan Sherifffa291e62012-04-04 13:18:17 -070026
Christopher Laneb72d8b42014-03-17 16:35:45 -070027import java.io.UnsupportedEncodingException;
Irfan Sheriff817388e2012-04-11 14:52:19 -070028import java.net.InetAddress;
Christopher Laneb72d8b42014-03-17 16:35:45 -070029import java.nio.charset.StandardCharsets;
30import java.util.Collections;
31import java.util.Map;
32
Irfan Sheriff817388e2012-04-11 14:52:19 -070033
Irfan Sherifffa291e62012-04-04 13:18:17 -070034/**
Irfan Sheriff92784672012-04-13 12:15:41 -070035 * A class representing service information for network service discovery
36 * {@see NsdManager}
Irfan Sherifffa291e62012-04-04 13:18:17 -070037 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -070038public final class NsdServiceInfo implements Parcelable {
Irfan Sherifffa291e62012-04-04 13:18:17 -070039
Christopher Laneb72d8b42014-03-17 16:35:45 -070040 private static final String TAG = "NsdServiceInfo";
41
Irfan Sherifffa291e62012-04-04 13:18:17 -070042 private String mServiceName;
43
Irfan Sheriff817388e2012-04-11 14:52:19 -070044 private String mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070045
Christopher Laneb72d8b42014-03-17 16:35:45 -070046 private final ArrayMap<String, byte[]> mTxtRecord = new ArrayMap<String, byte[]>();
Irfan Sherifffa291e62012-04-04 13:18:17 -070047
Irfan Sheriff817388e2012-04-11 14:52:19 -070048 private InetAddress mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070049
50 private int mPort;
51
Irfan Sheriff22af38c2012-05-03 16:44:27 -070052 public NsdServiceInfo() {
Irfan Sherifffa291e62012-04-04 13:18:17 -070053 }
54
Irfan Sheriff92784672012-04-13 12:15:41 -070055 /** @hide */
Christopher Laneb72d8b42014-03-17 16:35:45 -070056 public NsdServiceInfo(String sn, String rt) {
Irfan Sherifffa291e62012-04-04 13:18:17 -070057 mServiceName = sn;
Irfan Sheriff817388e2012-04-11 14:52:19 -070058 mServiceType = rt;
Irfan Sherifffa291e62012-04-04 13:18:17 -070059 }
60
Irfan Sheriff92784672012-04-13 12:15:41 -070061 /** Get the service name */
Irfan Sherifffa291e62012-04-04 13:18:17 -070062 public String getServiceName() {
63 return mServiceName;
64 }
65
Irfan Sheriff92784672012-04-13 12:15:41 -070066 /** Set the service name */
Irfan Sherifffa291e62012-04-04 13:18:17 -070067 public void setServiceName(String s) {
68 mServiceName = s;
69 }
70
Irfan Sheriff92784672012-04-13 12:15:41 -070071 /** Get the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070072 public String getServiceType() {
Irfan Sheriff817388e2012-04-11 14:52:19 -070073 return mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070074 }
75
Irfan Sheriff92784672012-04-13 12:15:41 -070076 /** Set the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070077 public void setServiceType(String s) {
Irfan Sheriff817388e2012-04-11 14:52:19 -070078 mServiceType = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070079 }
80
Irfan Sheriff92784672012-04-13 12:15:41 -070081 /** Get the host address. The host address is valid for a resolved service. */
Irfan Sheriff817388e2012-04-11 14:52:19 -070082 public InetAddress getHost() {
83 return mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070084 }
85
Irfan Sheriff92784672012-04-13 12:15:41 -070086 /** Set the host address */
Irfan Sheriff817388e2012-04-11 14:52:19 -070087 public void setHost(InetAddress s) {
88 mHost = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070089 }
90
Irfan Sheriff92784672012-04-13 12:15:41 -070091 /** Get port number. The port number is valid for a resolved service. */
Irfan Sherifffa291e62012-04-04 13:18:17 -070092 public int getPort() {
93 return mPort;
94 }
95
Irfan Sheriff92784672012-04-13 12:15:41 -070096 /** Set port number */
Irfan Sherifffa291e62012-04-04 13:18:17 -070097 public void setPort(int p) {
98 mPort = p;
99 }
100
Philip P. Moltmann312c61e2016-03-16 10:15:39 -0700101 /**
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 */
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
Christopher Laneb72d8b42014-03-17 16:35:45 -0700188 /** @hide */
189 public void setAttribute(String key, byte[] value) {
Philip P. Moltmann312c61e2016-03-16 10:15:39 -0700190 if (TextUtils.isEmpty(key)) {
191 throw new IllegalArgumentException("Key cannot be empty");
192 }
193
Christopher Laneb72d8b42014-03-17 16:35:45 -0700194 // 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 */
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 */
248 public void removeAttribute(String key) {
249 mTxtRecord.remove(key);
250 }
251
252 /**
Philip P. Moltmann092feb52016-04-18 16:23:06 -0700253 * Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
254 * valid for a resolved service.
Christopher Laneb72d8b42014-03-17 16:35:45 -0700255 *
256 * <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
257 * {@link #removeAttribute}.
258 */
259 public Map<String, byte[]> getAttributes() {
260 return Collections.unmodifiableMap(mTxtRecord);
261 }
262
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 */
Philip P. Moltmann312c61e2016-03-16 10:15:39 -0700275 public @NonNull byte[] getTxtRecord() {
Christopher Laneb72d8b42014-03-17 16:35:45 -0700276 int txtRecordSize = getTxtRecordSize();
277 if (txtRecordSize == 0) {
Philip P. Moltmann312c61e2016-03-16 10:15:39 -0700278 return new byte[]{};
Christopher Laneb72d8b42014-03-17 16:35:45 -0700279 }
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
Irfan Sherifffa291e62012-04-04 13:18:17 -0700309 public String toString() {
310 StringBuffer sb = new StringBuffer();
311
Christopher Laneb72d8b42014-03-17 16:35:45 -0700312 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 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700321 return sb.toString();
322 }
323
324 /** Implement the Parcelable interface */
325 public int describeContents() {
326 return 0;
327 }
328
329 /** Implement the Parcelable interface */
330 public void writeToParcel(Parcel dest, int flags) {
331 dest.writeString(mServiceName);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700332 dest.writeString(mServiceType);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700333 if (mHost != null) {
Christopher Laneb72d8b42014-03-17 16:35:45 -0700334 dest.writeInt(1);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700335 dest.writeByteArray(mHost.getAddress());
336 } else {
Christopher Laneb72d8b42014-03-17 16:35:45 -0700337 dest.writeInt(0);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700338 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700339 dest.writeInt(mPort);
Christopher Laneb72d8b42014-03-17 16:35:45 -0700340
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 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700354 }
355
356 /** Implement the Parcelable interface */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700357 public static final Creator<NsdServiceInfo> CREATOR =
358 new Creator<NsdServiceInfo>() {
359 public NsdServiceInfo createFromParcel(Parcel in) {
360 NsdServiceInfo info = new NsdServiceInfo();
Irfan Sherifffa291e62012-04-04 13:18:17 -0700361 info.mServiceName = in.readString();
Irfan Sheriff817388e2012-04-11 14:52:19 -0700362 info.mServiceType = in.readString();
Irfan Sheriff817388e2012-04-11 14:52:19 -0700363
Christopher Laneb72d8b42014-03-17 16:35:45 -0700364 if (in.readInt() == 1) {
Irfan Sheriff817388e2012-04-11 14:52:19 -0700365 try {
366 info.mHost = InetAddress.getByAddress(in.createByteArray());
367 } catch (java.net.UnknownHostException e) {}
368 }
369
Irfan Sherifffa291e62012-04-04 13:18:17 -0700370 info.mPort = in.readInt();
Christopher Laneb72d8b42014-03-17 16:35:45 -0700371
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 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700383 return info;
384 }
385
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700386 public NsdServiceInfo[] newArray(int size) {
387 return new NsdServiceInfo[size];
Irfan Sherifffa291e62012-04-04 13:18:17 -0700388 }
389 };
Irfan Sherifffa291e62012-04-04 13:18:17 -0700390}