aboutsummaryrefslogtreecommitdiff
path: root/PrintServiceStubs/src/com/android/printservicestubs/servicediscovery/mdns/DnsSdParser.java
blob: 631931566361088c636fa26e1b1201ba041d780a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * (c) Copyright 2016 Mopria Alliance, Inc.
 * (c) Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.printservicestubs.servicediscovery.mdns;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

class DnsSdParser {
    private static final String TAG = DnsSdParser.class.getSimpleName();

    private static final char SEPARATOR = '=';

    private DnsPacket packet;

    public DnsService[] parse(DnsPacket aPacket) throws DnsException {
        this.packet = aPacket;
        return this.parseServices();
    }

    private DnsService[] parseServices() throws DnsException {
        ArrayList<DnsService> serviceList = new ArrayList<>();

        for (DnsPacket.Entry answerEntry : this.packet.getAnswers()) {
            if (DnsPacket.ResourceType.PTR == answerEntry.getType()) {
                DnsPacket.Ptr ptr = (DnsPacket.Ptr) answerEntry;

                try {
                    serviceList.add(this.buildService(ptr));
                } catch (DnsSdException ignore) {
                }
            }
        }
        return serviceList.toArray(new DnsService[serviceList.size()]);
    }

    private DnsService buildService(DnsPacket.Ptr ptr) throws DnsSdException {
        DnsPacket.Name serviceNameSuffix = ptr.getName();
        DnsPacket.Name serviceName = ptr.getPointedName();
        DnsPacket.Srv srvEntry = this.findSrv(serviceName);
        DnsPacket.Txt txtEntry = this.findTxt(serviceName);
        DnsPacket.Name hostname = srvEntry.getTarget();
        DnsPacket.Address[] addressEntries = this.findAddresses(hostname);
        int port = srvEntry.getPort();
        Map<String, byte[]> attributes = parseAttributes(txtEntry.getText());
        byte[][] addresses = new byte[addressEntries.length][];

        for (int i = 0; i < addressEntries.length; i++) {
            addresses[i] = addressEntries[i].getAddress();
        }
        return new DnsService(serviceName, serviceNameSuffix, hostname, addresses, port, attributes);
    }

    private DnsPacket.Srv findSrv(DnsPacket.Name serviceName) throws DnsSdException {
        for (DnsPacket.Entry additionalEntry : this.packet.getAdditionals()) {
            if (DnsPacket.ResourceType.SRV == additionalEntry.getType()) {
                DnsPacket.Srv srv = (DnsPacket.Srv) additionalEntry;

                if (srv.getName().equals(serviceName)) {
                    return srv;
                }
            }
        }
        throw new DnsSdException("Service does not contain correspondent srv entry.");
    }

    private DnsPacket.Txt findTxt(DnsPacket.Name serviceName) throws DnsSdException {
        for (DnsPacket.Entry additionalEntry : this.packet.getAdditionals()) {
            if (DnsPacket.ResourceType.TXT == additionalEntry.getType()) {
                DnsPacket.Txt txt = (DnsPacket.Txt) additionalEntry;

                if (txt.getName().equals(serviceName)) {
                    return txt;
                }
            }
        }
        throw new DnsSdException("Service does not contain correspondent txt entry.");
    }

    private DnsPacket.Address[] findAddresses(DnsPacket.Name hostname) throws DnsSdException {
        ArrayList<DnsPacket.Address> addressEntries = new ArrayList<>();

        for (DnsPacket.Entry additionalEntry : this.packet.getAdditionals()) {
            if ((DnsPacket.ResourceType.A == additionalEntry.getType())
                    || (DnsPacket.ResourceType.AAAA == additionalEntry.getType())) {
                DnsPacket.Address address = (DnsPacket.Address) additionalEntry;

                if (address.getName().equals(hostname)) {
                    addressEntries.add(address);
                }
            }
        }
        if (addressEntries.isEmpty()) {
            throw new DnsSdException("Service does not contain correspondent address entry.");
        }
        return addressEntries.toArray(new DnsPacket.Address[addressEntries.size()]);
    }

    private static Map<String, byte[]> parseAttributes(byte[] txtData) {
        Map<String, byte[]> attributes = new HashMap<>();
        int offset = 0;

        while (offset < txtData.length) {
            int attrLength = txtData[offset++];
            if (attrLength < 0) {
                attrLength += 256;
            }
            int sepIndex;
            int keyLength;
            String key;
            byte[] value = null;

            if ((attrLength < 0) || ((offset + attrLength) > txtData.length)) {
                return attributes;
            }
            sepIndex = findSeparator(txtData, offset, attrLength);
            keyLength = (sepIndex > 0) ? (sepIndex - offset) : attrLength;
            if (keyLength == 0) {
                return attributes;
            }
            try {
                key = new String(txtData, offset, keyLength, "US-ASCII");
            } catch (Exception e) {
                Log.e(TAG, "Exception: parsing attribute: " + e);
                e.printStackTrace();
                continue;
            }

            if (sepIndex > 0) {
                int valueLength = (attrLength - keyLength - 1);

                value = new byte[valueLength];
                System.arraycopy(txtData, sepIndex + 1, value, 0, valueLength);
            }
            attributes.put(key, value);
            offset += attrLength;
        }
        return attributes;
    }

    private static int findSeparator(byte[] txtData, int offset, int length) {
        for (int i = offset; i < (offset + length); i++) {
            if (txtData[i] == (byte) SEPARATOR) {
                return i;
            }
        }
        return -1;
    }
}