summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/NsdService.java
blob: 768be7df410d644ad19e211a6d88bf90376b1e64 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2010 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.server;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.nsd.DnsSdServiceInfo;
import android.net.nsd.DnsSdTxtRecord;
import android.net.nsd.INsdManager;
import android.net.nsd.NsdManager;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Messenger;
import android.os.IBinder;
import android.util.Slog;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.List;

import com.android.internal.app.IBatteryStats;
import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.util.AsyncChannel;
import com.android.server.am.BatteryStatsService;
import com.android.server.NativeDaemonConnector.Command;
import com.android.internal.R;

/**
 * Network Service Discovery Service handles remote service discovery operation requests by
 * implementing the INsdManager interface.
 *
 * @hide
 */
public class NsdService extends INsdManager.Stub {
    private static final String TAG = "NsdService";
    private static final String MDNS_TAG = "mDnsConnector";

    private static final boolean DBG = true;

    private Context mContext;

    /**
     * Clients receiving asynchronous messages
     */
    private List<AsyncChannel> mClients = new ArrayList<AsyncChannel>();

    private AsyncChannel mReplyChannel = new AsyncChannel();

    /**
     * Handles client(app) connections
     */
    private class AsyncServiceHandler extends Handler {

        AsyncServiceHandler(android.os.Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
                        AsyncChannel c = (AsyncChannel) msg.obj;
                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
                        c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
                        mClients.add(c);
                    } else {
                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
                    }
                    break;
                case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
                        Slog.e(TAG, "Send failed, client connection lost");
                    } else {
                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
                    }
                    mClients.remove((AsyncChannel) msg.obj);
                    break;
                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
                    AsyncChannel ac = new AsyncChannel();
                    ac.connect(mContext, this, msg.replyTo);
                    break;
                case NsdManager.DISCOVER_SERVICES:
                    if (DBG) Slog.d(TAG, "Discover services");
                    DnsSdServiceInfo s = (DnsSdServiceInfo) msg.obj;
                    discoverServices(1, s.getServiceType());
                    mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED);
                    break;
                case NsdManager.STOP_DISCOVERY:
                    if (DBG) Slog.d(TAG, "Stop service discovery");
                    mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED);
                    break;
                case NsdManager.REGISTER_SERVICE:
                    if (DBG) Slog.d(TAG, "Register service");
                    mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED);
                    break;
                case NsdManager.UPDATE_SERVICE:
                    if (DBG) Slog.d(TAG, "Update service");
                    mReplyChannel.replyToMessage(msg, NsdManager.UPDATE_SERVICE_FAILED);
                    break;
                default:
                    Slog.d(TAG, "NsdServicehandler.handleMessage ignoring msg=" + msg);
                    break;
            }
        }
    }
    private AsyncServiceHandler mAsyncServiceHandler;

    private NativeDaemonConnector mNativeConnector;
    private final CountDownLatch mNativeDaemonConnected = new CountDownLatch(1);

    private NsdService(Context context) {
        mContext = context;

        HandlerThread nsdThread = new HandlerThread("NsdService");
        nsdThread.start();
        mAsyncServiceHandler = new AsyncServiceHandler(nsdThread.getLooper());

        /*
        mNativeConnector = new NativeDaemonConnector(new NativeCallbackReceiver(), "mdns", 10,
                MDNS_TAG, 25);
        Thread th = new Thread(mNativeConnector, MDNS_TAG);
        th.start();
        */
    }

    public static NsdService create(Context context) throws InterruptedException {
        NsdService service = new NsdService(context);
        /* service.mNativeDaemonConnected.await(); */
        return service;
    }

    public Messenger getMessenger() {
        return new Messenger(mAsyncServiceHandler);
    }

    /* These should be in sync with system/netd/mDnsResponseCode.h */
    class NativeResponseCode {
        public static final int SERVICE_FOUND               =   101;
        public static final int SERVICE_LOST                =   102;
        public static final int SERVICE_DISCOVERY_FAILED    =   103;

        public static final int SERVICE_REGISTERED          =   104;
        public static final int SERVICE_REGISTRATION_FAILED =   105;

        public static final int SERVICE_UPDATED             =   106;
        public static final int SERVICE_UPDATE_FAILED       =   107;

        public static final int SERVICE_RESOLVED            =   108;
        public static final int SERVICE_RESOLUTION_FAILED   =   109;
    }


    class NativeCallbackReceiver implements INativeDaemonConnectorCallbacks {
        public void onDaemonConnected() {
            mNativeDaemonConnected.countDown();
        }

        public boolean onEvent(int code, String raw, String[] cooked) {
            switch (code) {
                case NativeResponseCode.SERVICE_FOUND:
                    /* NNN uniqueId serviceName regType */
                    break;
                case NativeResponseCode.SERVICE_LOST:
                    /* NNN uniqueId serviceName regType */
                    break;
                case NativeResponseCode.SERVICE_DISCOVERY_FAILED:
                    /* NNN uniqueId errorCode */
                    break;
                case NativeResponseCode.SERVICE_REGISTERED:
                    /* NNN regId serviceName regType */
                    break;
                case NativeResponseCode.SERVICE_REGISTRATION_FAILED:
                    /* NNN regId errorCode */
                    break;
                case NativeResponseCode.SERVICE_UPDATED:
                    /* NNN regId */
                    break;
                case NativeResponseCode.SERVICE_UPDATE_FAILED:
                    /* NNN regId errorCode */
                    break;
                case NativeResponseCode.SERVICE_RESOLVED:
                    /* NNN resolveId fullName hostName port txtlen txtdata */
                    break;
                case NativeResponseCode.SERVICE_RESOLUTION_FAILED:
                    /* NNN resovleId errorCode */
                    break;
                default:
                    break;
            }
            return false;
        }
    }

    private void registerService(int regId, DnsSdServiceInfo service) {
        try {
            //Add txtlen and txtdata
            mNativeConnector.execute("mdnssd", "register", regId, service.getServiceName(),
                    service.getServiceType(), service.getPort());
        } catch(NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to execute registerService");
        }
    }

    private void updateService(int regId, DnsSdTxtRecord t) {
        try {
            if (t == null) return;
            mNativeConnector.execute("mdnssd", "update", regId, t.size(), t.getRawData());
        } catch(NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to updateServices");
        }
    }

    private void discoverServices(int discoveryId, String serviceType) {
        try {
            mNativeConnector.execute("mdnssd", "discover", discoveryId, serviceType);
        } catch(NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to discoverServices");
        }
    }

    private void stopServiceDiscovery(int discoveryId) {
        try {
            mNativeConnector.execute("mdnssd", "stopdiscover", discoveryId);
        } catch(NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to stopServiceDiscovery");
        }
    }

    private void resolveService(DnsSdServiceInfo service) {
        try {
        mNativeConnector.execute("mdnssd", "resolve", service.getServiceName(),
                service.getServiceType());
        } catch(NativeDaemonConnectorException e) {
            Slog.e(TAG, "Failed to resolveService");
        }
    }

    @Override
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                != PackageManager.PERMISSION_GRANTED) {
            pw.println("Permission Denial: can't dump ServiceDiscoverService from from pid="
                    + Binder.getCallingPid()
                    + ", uid=" + Binder.getCallingUid());
            return;
        }

        pw.println("Internal state:");
    }
}