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  * A class representing service information for network service discovery
     35  * {@see NsdManager}
     36  */
     37 public final class NsdServiceInfo implements Parcelable {
     38 
     39     private static final String TAG = "NsdServiceInfo";
     40 
     41     private String mServiceName;
     42 
     43     private String mServiceType;
     44 
     45     private final ArrayMap<String, byte[]> mTxtRecord = new ArrayMap<>();
     46 
     47     private InetAddress mHost;
     48 
     49     private int mPort;
     50 
     51     public NsdServiceInfo() {
     52     }
     53 
     54     /** @hide */
     55     public NsdServiceInfo(String sn, String rt) {
     56         mServiceName = sn;
     57         mServiceType = rt;
     58     }
     59 
     60     /** Get the service name */
     61     public String getServiceName() {
     62         return mServiceName;
     63     }
     64 
     65     /** Set the service name */
     66     public void setServiceName(String s) {
     67         mServiceName = s;
     68     }
     69 
     70     /** Get the service type */
     71     public String getServiceType() {
     72         return mServiceType;
     73     }
     74 
     75     /** Set the service type */
     76     public void setServiceType(String s) {
     77         mServiceType = s;
     78     }
     79 
     80     /** Get the host address. The host address is valid for a resolved service. */
     81     public InetAddress getHost() {
     82         return mHost;
     83     }
     84 
     85     /** Set the host address */
     86     public void setHost(InetAddress s) {
     87         mHost = s;
     88     }
     89 
     90     /** Get port number. The port number is valid for a resolved service. */
     91     public int getPort() {
     92         return mPort;
     93     }
     94 
     95     /** Set port number */
     96     public void setPort(int p) {
     97         mPort = p;
     98     }
     99 
    100     /**
    101      * Unpack txt information from a base-64 encoded byte array.
    102      *
    103      * @param rawRecords The raw base64 encoded records string read from netd.
    104      *
    105      * @hide
    106      */
    107     public void setTxtRecords(@NonNull String rawRecords) {
    108         byte[] txtRecordsRawBytes = Base64.decode(rawRecords, Base64.DEFAULT);
    109 
    110         // There can be multiple TXT records after each other. Each record has to following format:
    111         //
    112         // byte                  type                  required   meaning
    113         // -------------------   -------------------   --------   ----------------------------------
    114         // 0                     unsigned 8 bit        yes        size of record excluding this byte
    115         // 1 - n                 ASCII but not '='     yes        key
    116         // n + 1                 '='                   optional   separator of key and value
    117         // n + 2 - record size   uninterpreted bytes   optional   value
    118         //
    119         // Example legal records:
    120         // [11, 'm', 'y', 'k', 'e', 'y', '=', 0x0, 0x4, 0x65, 0x7, 0xff]
    121         // [17, 'm', 'y', 'K', 'e', 'y', 'W', 'i', 't', 'h', 'N', 'o', 'V', 'a', 'l', 'u', 'e', '=']
    122         // [12, 'm', 'y', 'B', 'o', 'o', 'l', 'e', 'a', 'n', 'K', 'e', 'y']
    123         //
    124         // Example corrupted records
    125         // [3, =, 1, 2]    <- key is empty
    126         // [3, 0, =, 2]    <- key contains non-ASCII character. We handle this by replacing the
    127         //                    invalid characters instead of skipping the record.
    128         // [30, 'a', =, 2] <- length exceeds total left over bytes in the TXT records array, we
    129         //                    handle this by reducing the length of the record as needed.
    130         int pos = 0;
    131         while (pos < txtRecordsRawBytes.length) {
    132             // recordLen is an unsigned 8 bit value
    133             int recordLen = txtRecordsRawBytes[pos] & 0xff;
    134             pos += 1;
    135 
    136             try {
    137                 if (recordLen == 0) {
    138                     throw new IllegalArgumentException("Zero sized txt record");
    139                 } else if (pos + recordLen > txtRecordsRawBytes.length) {
    140                     Log.w(TAG, "Corrupt record length (pos = " + pos + "): " + recordLen);
    141                     recordLen = txtRecordsRawBytes.length - pos;
    142                 }
    143 
    144                 // Decode key-value records
    145                 String key = null;
    146                 byte[] value = null;
    147                 int valueLen = 0;
    148                 for (int i = pos; i < pos + recordLen; i++) {
    149                     if (key == null) {
    150                         if (txtRecordsRawBytes[i] == '=') {
    151                             key = new String(txtRecordsRawBytes, pos, i - pos,
    152                                     StandardCharsets.US_ASCII);
    153                         }
    154                     } else {
    155                         if (value == null) {
    156                             value = new byte[recordLen - key.length() - 1];
    157                         }
    158                         value[valueLen] = txtRecordsRawBytes[i];
    159                         valueLen++;
    160                     }
    161                 }
    162 
    163                 // If '=' was not found we have a boolean record
    164                 if (key == null) {
    165                     key = new String(txtRecordsRawBytes, pos, recordLen, StandardCharsets.US_ASCII);
    166                 }
    167 
    168                 if (TextUtils.isEmpty(key)) {
    169                     // Empty keys are not allowed (RFC6763 6.4)
    170                     throw new IllegalArgumentException("Invalid txt record (key is empty)");
    171                 }
    172 
    173                 if (getAttributes().containsKey(key)) {
    174                     // When we have a duplicate record, the later ones are ignored (RFC6763 6.4)
    175                     throw new IllegalArgumentException("Invalid txt record (duplicate key \"" + key + "\")");
    176                 }
    177 
    178                 setAttribute(key, value);
    179             } catch (IllegalArgumentException e) {
    180                 Log.e(TAG, "While parsing txt records (pos = " + pos + "): " + e.getMessage());
    181             }
    182 
    183             pos += recordLen;
    184         }
    185     }
    186 
    187     /** @hide */
    188     public void setAttribute(String key, byte[] value) {
    189         if (TextUtils.isEmpty(key)) {
    190             throw new IllegalArgumentException("Key cannot be empty");
    191         }
    192 
    193         // Key must be printable US-ASCII, excluding =.
    194         for (int i = 0; i < key.length(); ++i) {
    195             char character = key.charAt(i);
    196             if (character < 0x20 || character > 0x7E) {
    197                 throw new IllegalArgumentException("Key strings must be printable US-ASCII");
    198             } else if (character == 0x3D) {
    199                 throw new IllegalArgumentException("Key strings must not include '='");
    200             }
    201         }
    202 
    203         // Key length + value length must be < 255.
    204         if (key.length() + (value == null ? 0 : value.length) >= 255) {
    205             throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
    206         }
    207 
    208         // Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
    209         if (key.length() > 9) {
    210             Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
    211         }
    212 
    213         // Check against total TXT record size limits.
    214         // Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
    215         int txtRecordSize = getTxtRecordSize();
    216         int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
    217         if (futureSize > 1300) {
    218             throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
    219         } else if (futureSize > 400) {
    220             Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
    221         }
    222 
    223         mTxtRecord.put(key, value);
    224     }
    225 
    226     /**
    227      * Add a service attribute as a key/value pair.
    228      *
    229      * <p> Service attributes are included as DNS-SD TXT record pairs.
    230      *
    231      * <p> The key must be US-ASCII printable characters, excluding the '=' character.  Values may
    232      * be UTF-8 strings or null.  The total length of key + value must be less than 255 bytes.
    233      *
    234      * <p> Keys should be short, ideally no more than 9 characters, and unique per instance of
    235      * {@link NsdServiceInfo}.  Calling {@link #setAttribute} twice with the same key will overwrite
    236      * first value.
    237      */
    238     public void setAttribute(String key, String value) {
    239         try {
    240             setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
    241         } catch (UnsupportedEncodingException e) {
    242             throw new IllegalArgumentException("Value must be UTF-8");
    243         }
    244     }
    245 
    246     /** Remove an attribute by key */
    247     public void removeAttribute(String key) {
    248         mTxtRecord.remove(key);
    249     }
    250 
    251     /**
    252      * Retrieve attributes as a map of String keys to byte[] values. The attributes map is only
    253      * valid for a resolved service.
    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