Home | History | Annotate | Download | only in nsd
      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 
     52     public NsdServiceInfo() {
     53     }
     54 
     55     /** @hide */
     56     public NsdServiceInfo(String sn, String rt) {
     57         mServiceName = sn;
     58         mServiceType = rt;
     59     }
     60 
     61     /** Get the service name */
     62     public String getServiceName() {
     63         return mServiceName;
     64     }
     65 
     66     /** Set the service name */
     67     public void setServiceName(String s) {
     68         mServiceName = s;
     69     }
     70 
     71     /** Get the service type */
     72     public String getServiceType() {
     73         return mServiceType;
     74     }
     75 
     76     /** Set the service type */
     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. */
     82     public InetAddress getHost() {
     83         return mHost;
     84     }
     85 
     86     /** Set the host address */
     87     public void setHost(InetAddress s) {
     88         mHost = s;
     89     }
     90 
     91     /** Get port number. The port number is valid for a resolved service. */
     92     public int getPort() {
     93         return mPort;
     94     }
     95 
     96     /** Set port number */
     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      */
    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     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      */
    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     /**
    253      * Retrive attributes as a map of String keys to byte[] values.
    254      *
    255      * <p> The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
    256      * {@link #removeAttribute}.
    257      */
    258     public Map<String, byte[]> getAttributes() {
    259         return Collections.unmodifiableMap(mTxtRecord);
    260     }
    261 
    262     private int getTxtRecordSize() {
    263         int txtRecordSize = 0;
    264         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
    265             txtRecordSize += 2;  // One for the length byte, one for the = between key and value.
    266             txtRecordSize += entry.getKey().length();
    267             byte[] value = entry.getValue();
    268             txtRecordSize += value == null ? 0 : value.length;
    269         }
    270         return txtRecordSize;
    271     }
    272 
    273     /** @hide */
    274     public @NonNull byte[] getTxtRecord() {
    275         int txtRecordSize = getTxtRecordSize();
    276         if (txtRecordSize == 0) {
    277             return new byte[]{};
    278         }
    279 
    280         byte[] txtRecord = new byte[txtRecordSize];
    281         int ptr = 0;
    282         for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
    283             String key = entry.getKey();
    284             byte[] value = entry.getValue();
    285 
    286             // One byte to record the length of this key/value pair.
    287             txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
    288 
    289             // The key, in US-ASCII.
    290             // Note: use the StandardCharsets const here because it doesn't raise exceptions and we
    291             // already know the key is ASCII at this point.
    292             System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
    293                     key.length());
    294             ptr += key.length();
    295 
    296             // US-ASCII '=' character.
    297             txtRecord[ptr++] = (byte)'=';
    298 
    299             // The value, as any raw bytes.
    300             if (value != null) {
    301                 System.arraycopy(value, 0, txtRecord, ptr, value.length);
    302                 ptr += value.length;
    303             }
    304         }
    305         return txtRecord;
    306     }
    307 
    308     public String toString() {
    309         StringBuffer sb = new StringBuffer();
    310 
    311         sb.append("name: ").append(mServiceName)
    312                 .append(", type: ").append(mServiceType)
    313                 .append(", host: ").append(mHost)
    314                 .append(", port: ").append(mPort);
    315 
    316         byte[] txtRecord = getTxtRecord();
    317         if (txtRecord != null) {
    318             sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
    319         }
    320         return sb.toString();
    321     }
    322 
    323     /** Implement the Parcelable interface */
    324     public int describeContents() {
    325         return 0;
    326     }
    327 
    328     /** Implement the Parcelable interface */
    329     public void writeToParcel(Parcel dest, int flags) {
    330         dest.writeString(mServiceName);
    331         dest.writeString(mServiceType);
    332         if (mHost != null) {
    333             dest.writeInt(1);
    334             dest.writeByteArray(mHost.getAddress());
    335         } else {
    336             dest.writeInt(0);
    337         }
    338         dest.writeInt(mPort);
    339 
    340         // TXT record key/value pairs.
    341         dest.writeInt(mTxtRecord.size());
    342         for (String key : mTxtRecord.keySet()) {
    343             byte[] value = mTxtRecord.get(key);
    344             if (value != null) {
    345                 dest.writeInt(1);
    346                 dest.writeInt(value.length);
    347                 dest.writeByteArray(value);
    348             } else {
    349                 dest.writeInt(0);
    350             }
    351             dest.writeString(key);
    352         }
    353     }
    354 
    355     /** Implement the Parcelable interface */
    356     public static final Creator<NsdServiceInfo> CREATOR =
    357         new Creator<NsdServiceInfo>() {
    358             public NsdServiceInfo createFromParcel(Parcel in) {
    359                 NsdServiceInfo info = new NsdServiceInfo();
    360                 info.mServiceName = in.readString();
    361                 info.mServiceType = in.readString();
    362 
    363                 if (in.readInt() == 1) {
    364                     try {
    365                         info.mHost = InetAddress.getByAddress(in.createByteArray());
    366                     } catch (java.net.UnknownHostException e) {}
    367                 }
    368 
    369                 info.mPort = in.readInt();
    370 
    371                 // TXT record key/value pairs.
    372                 int recordCount = in.readInt();
    373                 for (int i = 0; i < recordCount; ++i) {
    374                     byte[] valueArray = null;
    375                     if (in.readInt() == 1) {
    376                         int valueLength = in.readInt();
    377                         valueArray = new byte[valueLength];
    378                         in.readByteArray(valueArray);
    379                     }
    380                     info.mTxtRecord.put(in.readString(), valueArray);
    381                 }
    382                 return info;
    383             }
    384 
    385             public NsdServiceInfo[] newArray(int size) {
    386                 return new NsdServiceInfo[size];
    387             }
    388         };
    389 }
    390