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