Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 com.android.server; |
| 18 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 19 | import android.content.ContentResolver; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 20 | import android.content.Context; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 22 | import android.database.ContentObserver; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 23 | import android.net.NetworkStack; |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 24 | import android.net.Uri; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 25 | import android.net.nsd.DnsSdTxtRecord; |
| 26 | import android.net.nsd.INsdManager; |
| 27 | import android.net.nsd.NsdManager; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 28 | import android.net.nsd.NsdServiceInfo; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 29 | import android.os.Handler; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 30 | import android.os.HandlerThread; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 31 | import android.os.Message; |
| 32 | import android.os.Messenger; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 34 | import android.provider.Settings; |
Philip P. Moltmann | 312c61e | 2016-03-16 10:15:39 -0700 | [diff] [blame] | 35 | import android.util.Base64; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 37 | import android.util.SparseArray; |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 38 | import android.util.SparseIntArray; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 39 | |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 40 | import com.android.internal.annotations.VisibleForTesting; |
| 41 | import com.android.internal.util.AsyncChannel; |
| 42 | import com.android.internal.util.DumpUtils; |
| 43 | import com.android.internal.util.State; |
| 44 | import com.android.internal.util.StateMachine; |
| 45 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 46 | import java.io.FileDescriptor; |
| 47 | import java.io.PrintWriter; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 48 | import java.net.InetAddress; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 49 | import java.util.Arrays; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 50 | import java.util.HashMap; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 51 | import java.util.concurrent.CountDownLatch; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 52 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Network Service Discovery Service handles remote service discovery operation requests by |
| 55 | * implementing the INsdManager interface. |
| 56 | * |
| 57 | * @hide |
| 58 | */ |
| 59 | public class NsdService extends INsdManager.Stub { |
| 60 | private static final String TAG = "NsdService"; |
| 61 | private static final String MDNS_TAG = "mDnsConnector"; |
| 62 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 63 | private static final boolean DBG = true; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 64 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 65 | private final Context mContext; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 66 | private final NsdSettings mNsdSettings; |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 67 | private final NsdStateMachine mNsdStateMachine; |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 68 | private final DaemonConnection mDaemon; |
| 69 | private final NativeCallbackReceiver mDaemonCallback; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Clients receiving asynchronous messages |
| 73 | */ |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 74 | private final HashMap<Messenger, ClientInfo> mClients = new HashMap<>(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 75 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 76 | /* A map from unique id to client info */ |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 77 | private final SparseArray<ClientInfo> mIdToClientInfoMap= new SparseArray<>(); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 78 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 79 | private final AsyncChannel mReplyChannel = new AsyncChannel(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 80 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 81 | private static final int INVALID_ID = 0; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 82 | private int mUniqueId = 1; |
| 83 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 84 | private class NsdStateMachine extends StateMachine { |
| 85 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 86 | private final DefaultState mDefaultState = new DefaultState(); |
| 87 | private final DisabledState mDisabledState = new DisabledState(); |
| 88 | private final EnabledState mEnabledState = new EnabledState(); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 89 | |
| 90 | @Override |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 91 | protected String getWhatToString(int what) { |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 92 | return NsdManager.nameOf(what); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Observes the NSD on/off setting, and takes action when changed. |
| 97 | */ |
| 98 | private void registerForNsdSetting() { |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 99 | final ContentObserver contentObserver = new ContentObserver(this.getHandler()) { |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 100 | @Override |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 101 | public void onChange(boolean selfChange) { |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 102 | notifyEnabled(isNsdEnabled()); |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 103 | } |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 106 | final Uri uri = Settings.Global.getUriFor(Settings.Global.NSD_ON); |
| 107 | mNsdSettings.registerContentObserver(uri, contentObserver); |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 110 | NsdStateMachine(String name, Handler handler) { |
| 111 | super(name, handler); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 112 | addState(mDefaultState); |
| 113 | addState(mDisabledState, mDefaultState); |
| 114 | addState(mEnabledState, mDefaultState); |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 115 | State initialState = isNsdEnabled() ? mEnabledState : mDisabledState; |
| 116 | setInitialState(initialState); |
Wink Saville | bbf30dfd | 2012-05-29 12:40:46 -0700 | [diff] [blame] | 117 | setLogRecSize(25); |
Irfan Sheriff | 919aca5 | 2012-06-01 16:44:13 -0700 | [diff] [blame] | 118 | registerForNsdSetting(); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | class DefaultState extends State { |
| 122 | @Override |
| 123 | public boolean processMessage(Message msg) { |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 124 | ClientInfo cInfo = null; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 125 | switch (msg.what) { |
| 126 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 127 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 128 | AsyncChannel c = (AsyncChannel) msg.obj; |
| 129 | if (DBG) Slog.d(TAG, "New client listening to asynchronous messages"); |
| 130 | c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 131 | cInfo = new ClientInfo(c, msg.replyTo); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 132 | mClients.put(msg.replyTo, cInfo); |
| 133 | } else { |
| 134 | Slog.e(TAG, "Client connection failure, error=" + msg.arg1); |
| 135 | } |
| 136 | break; |
| 137 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 138 | switch (msg.arg1) { |
| 139 | case AsyncChannel.STATUS_SEND_UNSUCCESSFUL: |
| 140 | Slog.e(TAG, "Send failed, client connection lost"); |
| 141 | break; |
| 142 | case AsyncChannel.STATUS_REMOTE_DISCONNECTION: |
| 143 | if (DBG) Slog.d(TAG, "Client disconnected"); |
| 144 | break; |
| 145 | default: |
| 146 | if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1); |
| 147 | break; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 148 | } |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 149 | cInfo = mClients.get(msg.replyTo); |
| 150 | if (cInfo != null) { |
| 151 | cInfo.expungeAllRequests(); |
| 152 | mClients.remove(msg.replyTo); |
| 153 | } |
| 154 | //Last client |
| 155 | if (mClients.size() == 0) { |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 156 | mDaemon.stop(); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 157 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 158 | break; |
| 159 | case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: |
| 160 | AsyncChannel ac = new AsyncChannel(); |
| 161 | ac.connect(mContext, getHandler(), msg.replyTo); |
| 162 | break; |
| 163 | case NsdManager.DISCOVER_SERVICES: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 164 | replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED, |
| 165 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 166 | break; |
| 167 | case NsdManager.STOP_DISCOVERY: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 168 | replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED, |
| 169 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 170 | break; |
| 171 | case NsdManager.REGISTER_SERVICE: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 172 | replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED, |
| 173 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 174 | break; |
| 175 | case NsdManager.UNREGISTER_SERVICE: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 176 | replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED, |
| 177 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 178 | break; |
| 179 | case NsdManager.RESOLVE_SERVICE: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 180 | replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED, |
| 181 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 182 | break; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 183 | case NsdManager.NATIVE_DAEMON_EVENT: |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 184 | default: |
| 185 | Slog.e(TAG, "Unhandled " + msg); |
| 186 | return NOT_HANDLED; |
| 187 | } |
| 188 | return HANDLED; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | class DisabledState extends State { |
| 193 | @Override |
| 194 | public void enter() { |
| 195 | sendNsdStateChangeBroadcast(false); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean processMessage(Message msg) { |
| 200 | switch (msg.what) { |
| 201 | case NsdManager.ENABLE: |
| 202 | transitionTo(mEnabledState); |
| 203 | break; |
| 204 | default: |
| 205 | return NOT_HANDLED; |
| 206 | } |
| 207 | return HANDLED; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | class EnabledState extends State { |
| 212 | @Override |
| 213 | public void enter() { |
| 214 | sendNsdStateChangeBroadcast(true); |
| 215 | if (mClients.size() > 0) { |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 216 | mDaemon.start(); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void exit() { |
| 222 | if (mClients.size() > 0) { |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 223 | mDaemon.stop(); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 227 | private boolean requestLimitReached(ClientInfo clientInfo) { |
| 228 | if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) { |
| 229 | if (DBG) Slog.d(TAG, "Exceeded max outstanding requests " + clientInfo); |
| 230 | return true; |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 235 | private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo, int what) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 236 | clientInfo.mClientIds.put(clientId, globalId); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 237 | clientInfo.mClientRequests.put(clientId, what); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 238 | mIdToClientInfoMap.put(globalId, clientInfo); |
| 239 | } |
| 240 | |
| 241 | private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) { |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 242 | clientInfo.mClientIds.delete(clientId); |
| 243 | clientInfo.mClientRequests.delete(clientId); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 244 | mIdToClientInfoMap.remove(globalId); |
| 245 | } |
| 246 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public boolean processMessage(Message msg) { |
| 249 | ClientInfo clientInfo; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 250 | NsdServiceInfo servInfo; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 251 | int id; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 252 | switch (msg.what) { |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 253 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 254 | //First client |
| 255 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL && |
| 256 | mClients.size() == 0) { |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 257 | mDaemon.start(); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 258 | } |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 259 | return NOT_HANDLED; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 260 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 261 | return NOT_HANDLED; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 262 | case NsdManager.DISABLE: |
| 263 | //TODO: cleanup clients |
| 264 | transitionTo(mDisabledState); |
| 265 | break; |
| 266 | case NsdManager.DISCOVER_SERVICES: |
| 267 | if (DBG) Slog.d(TAG, "Discover services"); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 268 | servInfo = (NsdServiceInfo) msg.obj; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 269 | clientInfo = mClients.get(msg.replyTo); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 270 | |
| 271 | if (requestLimitReached(clientInfo)) { |
| 272 | replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED, |
| 273 | NsdManager.FAILURE_MAX_LIMIT); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 274 | break; |
| 275 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 276 | |
| 277 | id = getUniqueId(); |
| 278 | if (discoverServices(id, servInfo.getServiceType())) { |
| 279 | if (DBG) { |
| 280 | Slog.d(TAG, "Discover " + msg.arg2 + " " + id + |
| 281 | servInfo.getServiceType()); |
| 282 | } |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 283 | storeRequestMap(msg.arg2, id, clientInfo, msg.what); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 284 | replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED, servInfo); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 285 | } else { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 286 | stopServiceDiscovery(id); |
| 287 | replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED, |
| 288 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 289 | } |
| 290 | break; |
| 291 | case NsdManager.STOP_DISCOVERY: |
| 292 | if (DBG) Slog.d(TAG, "Stop service discovery"); |
| 293 | clientInfo = mClients.get(msg.replyTo); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 294 | |
| 295 | try { |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 296 | id = clientInfo.mClientIds.get(msg.arg2); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 297 | } catch (NullPointerException e) { |
| 298 | replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED, |
| 299 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 300 | break; |
| 301 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 302 | removeRequestMap(msg.arg2, id, clientInfo); |
| 303 | if (stopServiceDiscovery(id)) { |
| 304 | replyToMessage(msg, NsdManager.STOP_DISCOVERY_SUCCEEDED); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 305 | } else { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 306 | replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED, |
| 307 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 308 | } |
| 309 | break; |
| 310 | case NsdManager.REGISTER_SERVICE: |
| 311 | if (DBG) Slog.d(TAG, "Register service"); |
| 312 | clientInfo = mClients.get(msg.replyTo); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 313 | if (requestLimitReached(clientInfo)) { |
| 314 | replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED, |
| 315 | NsdManager.FAILURE_MAX_LIMIT); |
| 316 | break; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 319 | id = getUniqueId(); |
| 320 | if (registerService(id, (NsdServiceInfo) msg.obj)) { |
| 321 | if (DBG) Slog.d(TAG, "Register " + msg.arg2 + " " + id); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 322 | storeRequestMap(msg.arg2, id, clientInfo, msg.what); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 323 | // Return success after mDns reports success |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 324 | } else { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 325 | unregisterService(id); |
| 326 | replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED, |
| 327 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 328 | } |
| 329 | break; |
| 330 | case NsdManager.UNREGISTER_SERVICE: |
| 331 | if (DBG) Slog.d(TAG, "unregister service"); |
| 332 | clientInfo = mClients.get(msg.replyTo); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 333 | try { |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 334 | id = clientInfo.mClientIds.get(msg.arg2); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 335 | } catch (NullPointerException e) { |
| 336 | replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED, |
| 337 | NsdManager.FAILURE_INTERNAL_ERROR); |
| 338 | break; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 339 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 340 | removeRequestMap(msg.arg2, id, clientInfo); |
| 341 | if (unregisterService(id)) { |
| 342 | replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_SUCCEEDED); |
| 343 | } else { |
| 344 | replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED, |
| 345 | NsdManager.FAILURE_INTERNAL_ERROR); |
| 346 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 347 | break; |
| 348 | case NsdManager.RESOLVE_SERVICE: |
| 349 | if (DBG) Slog.d(TAG, "Resolve service"); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 350 | servInfo = (NsdServiceInfo) msg.obj; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 351 | clientInfo = mClients.get(msg.replyTo); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 352 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 353 | |
| 354 | if (clientInfo.mResolvedService != null) { |
| 355 | replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED, |
| 356 | NsdManager.FAILURE_ALREADY_ACTIVE); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 357 | break; |
| 358 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 359 | |
| 360 | id = getUniqueId(); |
| 361 | if (resolveService(id, servInfo)) { |
| 362 | clientInfo.mResolvedService = new NsdServiceInfo(); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 363 | storeRequestMap(msg.arg2, id, clientInfo, msg.what); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 364 | } else { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 365 | replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED, |
| 366 | NsdManager.FAILURE_INTERNAL_ERROR); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 367 | } |
| 368 | break; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 369 | case NsdManager.NATIVE_DAEMON_EVENT: |
| 370 | NativeEvent event = (NativeEvent) msg.obj; |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 371 | if (!handleNativeEvent(event.code, event.raw, event.cooked)) { |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 372 | return NOT_HANDLED; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 373 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 374 | break; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 375 | default: |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 376 | return NOT_HANDLED; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 377 | } |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 378 | return HANDLED; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 379 | } |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 380 | |
| 381 | private boolean handleNativeEvent(int code, String raw, String[] cooked) { |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 382 | NsdServiceInfo servInfo; |
| 383 | int id = Integer.parseInt(cooked[1]); |
| 384 | ClientInfo clientInfo = mIdToClientInfoMap.get(id); |
| 385 | if (clientInfo == null) { |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 386 | String name = NativeResponseCode.nameOf(code); |
| 387 | Slog.e(TAG, String.format("id %d for %s has no client mapping", id, name)); |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 388 | return false; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* This goes in response as msg.arg2 */ |
Christopher Lane | 5a57790 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 392 | int clientId = clientInfo.getClientId(id); |
| 393 | if (clientId < 0) { |
Vinit Deshapnde | 8ed09e8 | 2013-06-25 19:45:03 -0700 | [diff] [blame] | 394 | // This can happen because of race conditions. For example, |
| 395 | // SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY, |
| 396 | // and we may get in this situation. |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 397 | String name = NativeResponseCode.nameOf(code); |
| 398 | Slog.d(TAG, String.format( |
| 399 | "Notification %s for listener id %d that is no longer active", |
| 400 | name, id)); |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 401 | return false; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 402 | } |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 403 | if (DBG) { |
| 404 | String name = NativeResponseCode.nameOf(code); |
| 405 | Slog.d(TAG, String.format("Native daemon message %s: %s", name, raw)); |
| 406 | } |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 407 | switch (code) { |
| 408 | case NativeResponseCode.SERVICE_FOUND: |
| 409 | /* NNN uniqueId serviceName regType domain */ |
Christopher Lane | b72d8b4 | 2014-03-17 16:35:45 -0700 | [diff] [blame] | 410 | servInfo = new NsdServiceInfo(cooked[2], cooked[3]); |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 411 | clientInfo.mChannel.sendMessage(NsdManager.SERVICE_FOUND, 0, |
| 412 | clientId, servInfo); |
| 413 | break; |
| 414 | case NativeResponseCode.SERVICE_LOST: |
| 415 | /* NNN uniqueId serviceName regType domain */ |
Christopher Lane | b72d8b4 | 2014-03-17 16:35:45 -0700 | [diff] [blame] | 416 | servInfo = new NsdServiceInfo(cooked[2], cooked[3]); |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 417 | clientInfo.mChannel.sendMessage(NsdManager.SERVICE_LOST, 0, |
| 418 | clientId, servInfo); |
| 419 | break; |
| 420 | case NativeResponseCode.SERVICE_DISCOVERY_FAILED: |
| 421 | /* NNN uniqueId errorCode */ |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 422 | clientInfo.mChannel.sendMessage(NsdManager.DISCOVER_SERVICES_FAILED, |
| 423 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
| 424 | break; |
| 425 | case NativeResponseCode.SERVICE_REGISTERED: |
| 426 | /* NNN regId serviceName regType */ |
Christopher Lane | b72d8b4 | 2014-03-17 16:35:45 -0700 | [diff] [blame] | 427 | servInfo = new NsdServiceInfo(cooked[2], null); |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 428 | clientInfo.mChannel.sendMessage(NsdManager.REGISTER_SERVICE_SUCCEEDED, |
| 429 | id, clientId, servInfo); |
| 430 | break; |
| 431 | case NativeResponseCode.SERVICE_REGISTRATION_FAILED: |
| 432 | /* NNN regId errorCode */ |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 433 | clientInfo.mChannel.sendMessage(NsdManager.REGISTER_SERVICE_FAILED, |
| 434 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
| 435 | break; |
| 436 | case NativeResponseCode.SERVICE_UPDATED: |
| 437 | /* NNN regId */ |
| 438 | break; |
| 439 | case NativeResponseCode.SERVICE_UPDATE_FAILED: |
| 440 | /* NNN regId errorCode */ |
| 441 | break; |
| 442 | case NativeResponseCode.SERVICE_RESOLVED: |
| 443 | /* NNN resolveId fullName hostName port txtlen txtdata */ |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 444 | int index = 0; |
| 445 | while (index < cooked[2].length() && cooked[2].charAt(index) != '.') { |
| 446 | if (cooked[2].charAt(index) == '\\') { |
| 447 | ++index; |
| 448 | } |
| 449 | ++index; |
| 450 | } |
| 451 | if (index >= cooked[2].length()) { |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 452 | Slog.e(TAG, "Invalid service found " + raw); |
| 453 | break; |
| 454 | } |
| 455 | String name = cooked[2].substring(0, index); |
| 456 | String rest = cooked[2].substring(index); |
| 457 | String type = rest.replace(".local.", ""); |
| 458 | |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 459 | name = unescape(name); |
| 460 | |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 461 | clientInfo.mResolvedService.setServiceName(name); |
| 462 | clientInfo.mResolvedService.setServiceType(type); |
| 463 | clientInfo.mResolvedService.setPort(Integer.parseInt(cooked[4])); |
Philip P. Moltmann | 312c61e | 2016-03-16 10:15:39 -0700 | [diff] [blame] | 464 | clientInfo.mResolvedService.setTxtRecords(cooked[6]); |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 465 | |
| 466 | stopResolveService(id); |
Vinit Deshapnde | 4038724 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 467 | removeRequestMap(clientId, id, clientInfo); |
| 468 | |
| 469 | int id2 = getUniqueId(); |
| 470 | if (getAddrInfo(id2, cooked[3])) { |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 471 | storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE); |
Vinit Deshapnde | 4038724 | 2013-11-12 15:36:37 -0800 | [diff] [blame] | 472 | } else { |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 473 | clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED, |
| 474 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 475 | clientInfo.mResolvedService = null; |
| 476 | } |
| 477 | break; |
| 478 | case NativeResponseCode.SERVICE_RESOLUTION_FAILED: |
| 479 | /* NNN resolveId errorCode */ |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 480 | stopResolveService(id); |
| 481 | removeRequestMap(clientId, id, clientInfo); |
| 482 | clientInfo.mResolvedService = null; |
| 483 | clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED, |
| 484 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
| 485 | break; |
| 486 | case NativeResponseCode.SERVICE_GET_ADDR_FAILED: |
| 487 | /* NNN resolveId errorCode */ |
| 488 | stopGetAddrInfo(id); |
| 489 | removeRequestMap(clientId, id, clientInfo); |
| 490 | clientInfo.mResolvedService = null; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 491 | clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED, |
| 492 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
| 493 | break; |
| 494 | case NativeResponseCode.SERVICE_GET_ADDR_SUCCESS: |
| 495 | /* NNN resolveId hostname ttl addr */ |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 496 | try { |
| 497 | clientInfo.mResolvedService.setHost(InetAddress.getByName(cooked[4])); |
| 498 | clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_SUCCEEDED, |
| 499 | 0, clientId, clientInfo.mResolvedService); |
| 500 | } catch (java.net.UnknownHostException e) { |
| 501 | clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED, |
| 502 | NsdManager.FAILURE_INTERNAL_ERROR, clientId); |
| 503 | } |
| 504 | stopGetAddrInfo(id); |
| 505 | removeRequestMap(clientId, id, clientInfo); |
| 506 | clientInfo.mResolvedService = null; |
| 507 | break; |
| 508 | default: |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 509 | return false; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 510 | } |
Hugo Benichi | 23dba85 | 2017-04-05 14:43:29 +0900 | [diff] [blame] | 511 | return true; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 512 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 515 | |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 516 | private String unescape(String s) { |
| 517 | StringBuilder sb = new StringBuilder(s.length()); |
| 518 | for (int i = 0; i < s.length(); ++i) { |
| 519 | char c = s.charAt(i); |
| 520 | if (c == '\\') { |
| 521 | if (++i >= s.length()) { |
| 522 | Slog.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 523 | break; |
| 524 | } |
| 525 | c = s.charAt(i); |
| 526 | if (c != '.' && c != '\\') { |
| 527 | if (i + 2 >= s.length()) { |
| 528 | Slog.e(TAG, "Unexpected end of escape sequence in: " + s); |
| 529 | break; |
| 530 | } |
| 531 | c = (char) ((c-'0') * 100 + (s.charAt(i+1)-'0') * 10 + (s.charAt(i+2)-'0')); |
| 532 | i += 2; |
| 533 | } |
| 534 | } |
| 535 | sb.append(c); |
| 536 | } |
| 537 | return sb.toString(); |
| 538 | } |
| 539 | |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 540 | @VisibleForTesting |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 541 | NsdService(Context ctx, NsdSettings settings, Handler handler, DaemonConnectionSupplier fn) { |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 542 | mContext = ctx; |
| 543 | mNsdSettings = settings; |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 544 | mNsdStateMachine = new NsdStateMachine(TAG, handler); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 545 | mNsdStateMachine.start(); |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 546 | mDaemonCallback = new NativeCallbackReceiver(); |
| 547 | mDaemon = fn.get(mDaemonCallback); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | public static NsdService create(Context context) throws InterruptedException { |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 551 | NsdSettings settings = NsdSettings.makeDefault(context); |
| 552 | HandlerThread thread = new HandlerThread(TAG); |
| 553 | thread.start(); |
| 554 | Handler handler = new Handler(thread.getLooper()); |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 555 | NsdService service = new NsdService(context, settings, handler, DaemonConnection::new); |
| 556 | service.mDaemonCallback.awaitConnection(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 557 | return service; |
| 558 | } |
| 559 | |
| 560 | public Messenger getMessenger() { |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 561 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService"); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 562 | return new Messenger(mNsdStateMachine.getHandler()); |
| 563 | } |
| 564 | |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 565 | public void setEnabled(boolean isEnabled) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 566 | NetworkStack.checkNetworkStackPermission(mContext); |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 567 | mNsdSettings.putEnabledStatus(isEnabled); |
| 568 | notifyEnabled(isEnabled); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 571 | private void notifyEnabled(boolean isEnabled) { |
| 572 | mNsdStateMachine.sendMessage(isEnabled ? NsdManager.ENABLE : NsdManager.DISABLE); |
| 573 | } |
| 574 | |
| 575 | private void sendNsdStateChangeBroadcast(boolean isEnabled) { |
Irfan Sheriff | 54ac7a5 | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 576 | final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 577 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Hugo Benichi | 2d968b4 | 2017-04-24 16:41:03 +0900 | [diff] [blame] | 578 | int nsdState = isEnabled ? NsdManager.NSD_STATE_ENABLED : NsdManager.NSD_STATE_DISABLED; |
| 579 | intent.putExtra(NsdManager.EXTRA_NSD_STATE, nsdState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 580 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | private boolean isNsdEnabled() { |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 584 | boolean ret = mNsdSettings.isEnabled(); |
| 585 | if (DBG) { |
| 586 | Slog.d(TAG, "Network service discovery is " + (ret ? "enabled" : "disabled")); |
| 587 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 588 | return ret; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 591 | private int getUniqueId() { |
| 592 | if (++mUniqueId == INVALID_ID) return ++mUniqueId; |
| 593 | return mUniqueId; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 596 | /* These should be in sync with system/netd/server/ResponseCode.h */ |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 597 | static final class NativeResponseCode { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 598 | public static final int SERVICE_DISCOVERY_FAILED = 602; |
| 599 | public static final int SERVICE_FOUND = 603; |
| 600 | public static final int SERVICE_LOST = 604; |
| 601 | |
| 602 | public static final int SERVICE_REGISTRATION_FAILED = 605; |
| 603 | public static final int SERVICE_REGISTERED = 606; |
| 604 | |
| 605 | public static final int SERVICE_RESOLUTION_FAILED = 607; |
| 606 | public static final int SERVICE_RESOLVED = 608; |
| 607 | |
| 608 | public static final int SERVICE_UPDATED = 609; |
| 609 | public static final int SERVICE_UPDATE_FAILED = 610; |
| 610 | |
| 611 | public static final int SERVICE_GET_ADDR_FAILED = 611; |
| 612 | public static final int SERVICE_GET_ADDR_SUCCESS = 612; |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 613 | |
| 614 | private static final SparseArray<String> CODE_NAMES = new SparseArray<>(); |
| 615 | static { |
| 616 | CODE_NAMES.put(SERVICE_DISCOVERY_FAILED, "SERVICE_DISCOVERY_FAILED"); |
| 617 | CODE_NAMES.put(SERVICE_FOUND, "SERVICE_FOUND"); |
| 618 | CODE_NAMES.put(SERVICE_LOST, "SERVICE_LOST"); |
| 619 | CODE_NAMES.put(SERVICE_REGISTRATION_FAILED, "SERVICE_REGISTRATION_FAILED"); |
| 620 | CODE_NAMES.put(SERVICE_REGISTERED, "SERVICE_REGISTERED"); |
| 621 | CODE_NAMES.put(SERVICE_RESOLUTION_FAILED, "SERVICE_RESOLUTION_FAILED"); |
| 622 | CODE_NAMES.put(SERVICE_RESOLVED, "SERVICE_RESOLVED"); |
| 623 | CODE_NAMES.put(SERVICE_UPDATED, "SERVICE_UPDATED"); |
| 624 | CODE_NAMES.put(SERVICE_UPDATE_FAILED, "SERVICE_UPDATE_FAILED"); |
| 625 | CODE_NAMES.put(SERVICE_GET_ADDR_FAILED, "SERVICE_GET_ADDR_FAILED"); |
| 626 | CODE_NAMES.put(SERVICE_GET_ADDR_SUCCESS, "SERVICE_GET_ADDR_SUCCESS"); |
| 627 | } |
| 628 | |
| 629 | static String nameOf(int code) { |
| 630 | String name = CODE_NAMES.get(code); |
| 631 | if (name == null) { |
| 632 | return Integer.toString(code); |
| 633 | } |
| 634 | return name; |
| 635 | } |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 636 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 637 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 638 | private class NativeEvent { |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 639 | final int code; |
| 640 | final String raw; |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 641 | final String[] cooked; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 642 | |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 643 | NativeEvent(int code, String raw, String[] cooked) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 644 | this.code = code; |
| 645 | this.raw = raw; |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 646 | this.cooked = cooked; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 650 | class NativeCallbackReceiver implements INativeDaemonConnectorCallbacks { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 651 | private final CountDownLatch connected = new CountDownLatch(1); |
| 652 | |
| 653 | public void awaitConnection() throws InterruptedException { |
| 654 | connected.await(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 657 | @Override |
| 658 | public void onDaemonConnected() { |
| 659 | connected.countDown(); |
| 660 | } |
| 661 | |
| 662 | @Override |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 663 | public boolean onCheckHoldWakeLock(int code) { |
| 664 | return false; |
| 665 | } |
| 666 | |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 667 | @Override |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 668 | public boolean onEvent(int code, String raw, String[] cooked) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 669 | // TODO: NDC translates a message to a callback, we could enhance NDC to |
| 670 | // directly interact with a state machine through messages |
Sreeram Ramachandran | ef12884 | 2014-09-03 15:45:59 -0700 | [diff] [blame] | 671 | NativeEvent event = new NativeEvent(code, raw, cooked); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 672 | mNsdStateMachine.sendMessage(NsdManager.NATIVE_DAEMON_EVENT, event); |
| 673 | return true; |
| 674 | } |
| 675 | } |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 676 | |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 677 | interface DaemonConnectionSupplier { |
| 678 | DaemonConnection get(NativeCallbackReceiver callback); |
| 679 | } |
| 680 | |
| 681 | @VisibleForTesting |
| 682 | public static class DaemonConnection { |
| 683 | final NativeDaemonConnector mNativeConnector; |
| 684 | |
| 685 | DaemonConnection(NativeCallbackReceiver callback) { |
| 686 | mNativeConnector = new NativeDaemonConnector(callback, "mdns", 10, MDNS_TAG, 25, null); |
| 687 | new Thread(mNativeConnector, MDNS_TAG).start(); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 688 | } |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 689 | |
| 690 | public boolean execute(Object... args) { |
| 691 | if (DBG) { |
| 692 | Slog.d(TAG, "mdnssd " + Arrays.toString(args)); |
| 693 | } |
| 694 | try { |
| 695 | mNativeConnector.execute("mdnssd", args); |
| 696 | } catch (NativeDaemonConnectorException e) { |
| 697 | Slog.e(TAG, "Failed to execute mdnssd " + Arrays.toString(args), e); |
| 698 | return false; |
| 699 | } |
| 700 | return true; |
| 701 | } |
| 702 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 703 | public void start() { |
| 704 | execute("start-service"); |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 705 | } |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 706 | |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 707 | public void stop() { |
| 708 | execute("stop-service"); |
| 709 | } |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 712 | private boolean registerService(int regId, NsdServiceInfo service) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 713 | if (DBG) { |
| 714 | Slog.d(TAG, "registerService: " + regId + " " + service); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 715 | } |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 716 | String name = service.getServiceName(); |
| 717 | String type = service.getServiceType(); |
| 718 | int port = service.getPort(); |
| 719 | byte[] textRecord = service.getTxtRecord(); |
| 720 | String record = Base64.encodeToString(textRecord, Base64.DEFAULT).replace("\n", ""); |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 721 | return mDaemon.execute("register", regId, name, type, port, record); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 724 | private boolean unregisterService(int regId) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 725 | return mDaemon.execute("stop-register", regId); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | private boolean updateService(int regId, DnsSdTxtRecord t) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 729 | if (t == null) { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 730 | return false; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 731 | } |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 732 | return mDaemon.execute("update", regId, t.size(), t.getRawData()); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 735 | private boolean discoverServices(int discoveryId, String serviceType) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 736 | return mDaemon.execute("discover", discoveryId, serviceType); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 739 | private boolean stopServiceDiscovery(int discoveryId) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 740 | return mDaemon.execute("stop-discover", discoveryId); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 743 | private boolean resolveService(int resolveId, NsdServiceInfo service) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 744 | String name = service.getServiceName(); |
| 745 | String type = service.getServiceType(); |
| 746 | return mDaemon.execute("resolve", resolveId, name, type, "local."); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | private boolean stopResolveService(int resolveId) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 750 | return mDaemon.execute("stop-resolve", resolveId); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | private boolean getAddrInfo(int resolveId, String hostname) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 754 | return mDaemon.execute("getaddrinfo", resolveId, hostname); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | private boolean stopGetAddrInfo(int resolveId) { |
Hugo Benichi | 1fac319 | 2017-04-24 16:19:58 +0900 | [diff] [blame] | 758 | return mDaemon.execute("stop-getaddrinfo", resolveId); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | @Override |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 762 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 763 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 764 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 765 | for (ClientInfo client : mClients.values()) { |
| 766 | pw.println("Client Info"); |
| 767 | pw.println(client); |
| 768 | } |
| 769 | |
| 770 | mNsdStateMachine.dump(fd, pw, args); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 771 | } |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 772 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 773 | /* arg2 on the source message has an id that needs to be retained in replies |
| 774 | * see NsdManager for details */ |
| 775 | private Message obtainMessage(Message srcMsg) { |
| 776 | Message msg = Message.obtain(); |
| 777 | msg.arg2 = srcMsg.arg2; |
| 778 | return msg; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 781 | private void replyToMessage(Message msg, int what) { |
| 782 | if (msg.replyTo == null) return; |
| 783 | Message dstMsg = obtainMessage(msg); |
| 784 | dstMsg.what = what; |
| 785 | mReplyChannel.replyToMessage(msg, dstMsg); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 788 | private void replyToMessage(Message msg, int what, int arg1) { |
| 789 | if (msg.replyTo == null) return; |
| 790 | Message dstMsg = obtainMessage(msg); |
| 791 | dstMsg.what = what; |
| 792 | dstMsg.arg1 = arg1; |
| 793 | mReplyChannel.replyToMessage(msg, dstMsg); |
| 794 | } |
| 795 | |
| 796 | private void replyToMessage(Message msg, int what, Object obj) { |
| 797 | if (msg.replyTo == null) return; |
| 798 | Message dstMsg = obtainMessage(msg); |
| 799 | dstMsg.what = what; |
| 800 | dstMsg.obj = obj; |
| 801 | mReplyChannel.replyToMessage(msg, dstMsg); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* Information tracked per client */ |
| 805 | private class ClientInfo { |
| 806 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 807 | private static final int MAX_LIMIT = 10; |
Vairavan Srinivasan | 6727e73 | 2012-08-05 13:14:12 -0700 | [diff] [blame] | 808 | private final AsyncChannel mChannel; |
| 809 | private final Messenger mMessenger; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 810 | /* Remembers a resolved service until getaddrinfo completes */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 811 | private NsdServiceInfo mResolvedService; |
| 812 | |
| 813 | /* A map from client id to unique id sent to mDns */ |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 814 | private final SparseIntArray mClientIds = new SparseIntArray(); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 815 | |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 816 | /* A map from client id to the type of the request we had received */ |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 817 | private final SparseIntArray mClientRequests = new SparseIntArray(); |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 818 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 819 | private ClientInfo(AsyncChannel c, Messenger m) { |
| 820 | mChannel = c; |
| 821 | mMessenger = m; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 822 | if (DBG) Slog.d(TAG, "New client, channel: " + c + " messenger: " + m); |
| 823 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 824 | |
| 825 | @Override |
| 826 | public String toString() { |
| 827 | StringBuffer sb = new StringBuffer(); |
| 828 | sb.append("mChannel ").append(mChannel).append("\n"); |
| 829 | sb.append("mMessenger ").append(mMessenger).append("\n"); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 830 | sb.append("mResolvedService ").append(mResolvedService).append("\n"); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 831 | for(int i = 0; i< mClientIds.size(); i++) { |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 832 | int clientID = mClientIds.keyAt(i); |
| 833 | sb.append("clientId ").append(clientID). |
| 834 | append(" mDnsId ").append(mClientIds.valueAt(i)). |
| 835 | append(" type ").append(mClientRequests.get(clientID)).append("\n"); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 836 | } |
| 837 | return sb.toString(); |
| 838 | } |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 839 | |
| 840 | // Remove any pending requests from the global map when we get rid of a client, |
| 841 | // and send cancellations to the daemon. |
| 842 | private void expungeAllRequests() { |
| 843 | int globalId, clientId, i; |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 844 | // TODO: to keep handler responsive, do not clean all requests for that client at once. |
Dave Platt | f31760c | 2014-03-07 14:48:22 -0800 | [diff] [blame] | 845 | for (i = 0; i < mClientIds.size(); i++) { |
| 846 | clientId = mClientIds.keyAt(i); |
| 847 | globalId = mClientIds.valueAt(i); |
| 848 | mIdToClientInfoMap.remove(globalId); |
| 849 | if (DBG) Slog.d(TAG, "Terminating client-ID " + clientId + |
| 850 | " global-ID " + globalId + " type " + mClientRequests.get(clientId)); |
| 851 | switch (mClientRequests.get(clientId)) { |
| 852 | case NsdManager.DISCOVER_SERVICES: |
| 853 | stopServiceDiscovery(globalId); |
| 854 | break; |
| 855 | case NsdManager.RESOLVE_SERVICE: |
| 856 | stopResolveService(globalId); |
| 857 | break; |
| 858 | case NsdManager.REGISTER_SERVICE: |
| 859 | unregisterService(globalId); |
| 860 | break; |
| 861 | default: |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | mClientIds.clear(); |
| 866 | mClientRequests.clear(); |
| 867 | } |
| 868 | |
Christopher Lane | 5a57790 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 869 | // mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id, |
| 870 | // return the corresponding listener id. mDnsClient id is also called a global id. |
| 871 | private int getClientId(final int globalId) { |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 872 | int idx = mClientIds.indexOfValue(globalId); |
| 873 | if (idx < 0) { |
| 874 | return idx; |
Christopher Lane | 5a57790 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 875 | } |
Hugo Benichi | 0f86b44 | 2017-04-11 14:42:47 +0900 | [diff] [blame] | 876 | return mClientIds.keyAt(idx); |
Christopher Lane | 5a57790 | 2014-04-25 18:39:07 -0700 | [diff] [blame] | 877 | } |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 878 | } |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 879 | |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 880 | /** |
| 881 | * Interface which encapsulates dependencies of NsdService that are hard to mock, hard to |
| 882 | * override, or have side effects on global state in unit tests. |
| 883 | */ |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 884 | @VisibleForTesting |
| 885 | public interface NsdSettings { |
| 886 | boolean isEnabled(); |
| 887 | void putEnabledStatus(boolean isEnabled); |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 888 | void registerContentObserver(Uri uri, ContentObserver observer); |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 889 | |
| 890 | static NsdSettings makeDefault(Context context) { |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 891 | final ContentResolver resolver = context.getContentResolver(); |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 892 | return new NsdSettings() { |
| 893 | @Override |
| 894 | public boolean isEnabled() { |
| 895 | return Settings.Global.getInt(resolver, Settings.Global.NSD_ON, 1) == 1; |
| 896 | } |
| 897 | |
| 898 | @Override |
| 899 | public void putEnabledStatus(boolean isEnabled) { |
| 900 | Settings.Global.putInt(resolver, Settings.Global.NSD_ON, isEnabled ? 1 : 0); |
| 901 | } |
Hugo Benichi | 03f6e1d | 2017-05-25 13:49:32 +0900 | [diff] [blame] | 902 | |
| 903 | @Override |
| 904 | public void registerContentObserver(Uri uri, ContentObserver observer) { |
| 905 | resolver.registerContentObserver(uri, false, observer); |
| 906 | } |
Hugo Benichi | cbb1367 | 2017-04-24 11:35:06 +0900 | [diff] [blame] | 907 | }; |
| 908 | } |
| 909 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 910 | } |