1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server;
import static android.net.ConnectivityManager.NETID_UNSET;
import static android.net.nsd.NsdManager.MDNS_SERVICE_EVENT;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.INetd;
import android.net.LinkProperties;
import android.net.Network;
import android.net.mdns.aidl.DiscoveryInfo;
import android.net.mdns.aidl.GetAddressInfo;
import android.net.mdns.aidl.IMDnsEventListener;
import android.net.mdns.aidl.RegistrationInfo;
import android.net.mdns.aidl.ResolutionInfo;
import android.net.nsd.INsdManager;
import android.net.nsd.INsdManagerCallback;
import android.net.nsd.INsdServiceConnector;
import android.net.nsd.MDnsManager;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.util.SparseIntArray;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
/**
* Network Service Discovery Service handles remote service discovery operation requests by
* implementing the INsdManager interface.
*
* @hide
*/
public class NsdService extends INsdManager.Stub {
private static final String TAG = "NsdService";
private static final String MDNS_TAG = "mDnsConnector";
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final long CLEANUP_DELAY_MS = 10000;
private static final int IFACE_IDX_ANY = 0;
private final Context mContext;
private final NsdStateMachine mNsdStateMachine;
private final MDnsManager mMDnsManager;
private final MDnsEventCallback mMDnsEventCallback;
// WARNING : Accessing this value in any thread is not safe, it must only be changed in the
// state machine thread. If change this outside state machine, it will need to introduce
// synchronization.
private boolean mIsDaemonStarted = false;
/**
* Clients receiving asynchronous messages
*/
private final HashMap<NsdServiceConnector, ClientInfo> mClients = new HashMap<>();
/* A map from unique id to client info */
private final SparseArray<ClientInfo> mIdToClientInfoMap= new SparseArray<>();
private final long mCleanupDelayMs;
private static final int INVALID_ID = 0;
private int mUniqueId = 1;
// The count of the connected legacy clients.
private int mLegacyClientCount = 0;
private class NsdStateMachine extends StateMachine {
private final DefaultState mDefaultState = new DefaultState();
private final DisabledState mDisabledState = new DisabledState();
private final EnabledState mEnabledState = new EnabledState();
@Override
protected String getWhatToString(int what) {
return NsdManager.nameOf(what);
}
private void maybeStartDaemon() {
if (mIsDaemonStarted) {
if (DBG) Log.d(TAG, "Daemon is already started.");
return;
}
mMDnsManager.registerEventListener(mMDnsEventCallback);
mMDnsManager.startDaemon();
mIsDaemonStarted = true;
maybeScheduleStop();
}
private void maybeStopDaemon() {
if (!mIsDaemonStarted) {
if (DBG) Log.d(TAG, "Daemon has not been started.");
return;
}
mMDnsManager.unregisterEventListener(mMDnsEventCallback);
mMDnsManager.stopDaemon();
mIsDaemonStarted = false;
}
private boolean isAnyRequestActive() {
return mIdToClientInfoMap.size() != 0;
}
private void scheduleStop() {
sendMessageDelayed(NsdManager.DAEMON_CLEANUP, mCleanupDelayMs);
}
private void maybeScheduleStop() {
// The native daemon should stay alive and can't be cleanup
// if any legacy client connected.
if (!isAnyRequestActive() && mLegacyClientCount == 0) {
scheduleStop();
}
}
private void cancelStop() {
this.removeMessages(NsdManager.DAEMON_CLEANUP);
}
NsdStateMachine(String name, Handler handler) {
super(name, handler);
addState(mDefaultState);
addState(mDisabledState, mDefaultState);
addState(mEnabledState, mDefaultState);
State initialState = mEnabledState;
setInitialState(initialState);
setLogRecSize(25);
}
class DefaultState extends State {
@Override
public boolean processMessage(Message msg) {
final ClientInfo cInfo;
final int clientId = msg.arg2;
switch (msg.what) {
case NsdManager.REGISTER_CLIENT:
final Pair<NsdServiceConnector, INsdManagerCallback> arg =
(Pair<NsdServiceConnector, INsdManagerCallback>) msg.obj;
final INsdManagerCallback cb = arg.second;
try {
cb.asBinder().linkToDeath(arg.first, 0);
cInfo = new ClientInfo(cb);
mClients.put(arg.first, cInfo);
} catch (RemoteException e) {
Log.w(TAG, "Client " + clientId + " has already died");
}
break;
case NsdManager.UNREGISTER_CLIENT:
final NsdServiceConnector connector = (NsdServiceConnector) msg.obj;
cInfo = mClients.remove(connector);
if (cInfo != null) {
cInfo.expungeAllRequests();
if (cInfo.isLegacy()) {
mLegacyClientCount -= 1;
}
}
maybeScheduleStop();
break;
case NsdManager.DISCOVER_SERVICES:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cInfo.onDiscoverServicesFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.STOP_DISCOVERY:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cInfo.onStopDiscoveryFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.REGISTER_SERVICE:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cInfo.onRegisterServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.UNREGISTER_SERVICE:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cInfo.onUnregisterServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.RESOLVE_SERVICE:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.DAEMON_CLEANUP:
maybeStopDaemon();
break;
// This event should be only sent by the legacy (target SDK < S) clients.
// Mark the sending client as legacy.
case NsdManager.DAEMON_STARTUP:
cInfo = getClientInfoForReply(msg);
if (cInfo != null) {
cancelStop();
cInfo.setLegacy();
mLegacyClientCount += 1;
maybeStartDaemon();
}
break;
default:
Log.e(TAG, "Unhandled " + msg);
return NOT_HANDLED;
}
return HANDLED;
}
private ClientInfo getClientInfoForReply(Message msg) {
final ListenerArgs args = (ListenerArgs) msg.obj;
return mClients.get(args.connector);
}
}
class DisabledState extends State {
@Override
public void enter() {
sendNsdStateChangeBroadcast(false);
}
@Override
public boolean processMessage(Message msg) {
switch (msg.what) {
case NsdManager.ENABLE:
transitionTo(mEnabledState);
break;
default:
return NOT_HANDLED;
}
return HANDLED;
}
}
class EnabledState extends State {
@Override
public void enter() {
sendNsdStateChangeBroadcast(true);
}
@Override
public void exit() {
// TODO: it is incorrect to stop the daemon without expunging all requests
// and sending error callbacks to clients.
scheduleStop();
}
private boolean requestLimitReached(ClientInfo clientInfo) {
if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) {
if (DBG) Log.d(TAG, "Exceeded max outstanding requests " + clientInfo);
return true;
}
return false;
}
private void storeRequestMap(int clientId, int globalId, ClientInfo clientInfo, int what) {
clientInfo.mClientIds.put(clientId, globalId);
clientInfo.mClientRequests.put(clientId, what);
mIdToClientInfoMap.put(globalId, clientInfo);
// Remove the cleanup event because here comes a new request.
cancelStop();
}
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
clientInfo.mClientIds.delete(clientId);
clientInfo.mClientRequests.delete(clientId);
mIdToClientInfoMap.remove(globalId);
maybeScheduleStop();
}
@Override
public boolean processMessage(Message msg) {
final ClientInfo clientInfo;
final int id;
final int clientId = msg.arg2;
final ListenerArgs args;
switch (msg.what) {
case NsdManager.DISABLE:
//TODO: cleanup clients
transitionTo(mDisabledState);
break;
case NsdManager.DISCOVER_SERVICES:
if (DBG) Log.d(TAG, "Discover services");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
if (requestLimitReached(clientInfo)) {
clientInfo.onDiscoverServicesFailed(
clientId, NsdManager.FAILURE_MAX_LIMIT);
break;
}
maybeStartDaemon();
id = getUniqueId();
if (discoverServices(id, args.serviceInfo)) {
if (DBG) {
Log.d(TAG, "Discover " + msg.arg2 + " " + id
+ args.serviceInfo.getServiceType());
}
storeRequestMap(clientId, id, clientInfo, msg.what);
clientInfo.onDiscoverServicesStarted(clientId, args.serviceInfo);
} else {
stopServiceDiscovery(id);
clientInfo.onDiscoverServicesFailed(clientId,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.STOP_DISCOVERY:
if (DBG) Log.d(TAG, "Stop service discovery");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
try {
id = clientInfo.mClientIds.get(clientId);
} catch (NullPointerException e) {
clientInfo.onStopDiscoveryFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
}
removeRequestMap(clientId, id, clientInfo);
if (stopServiceDiscovery(id)) {
clientInfo.onStopDiscoverySucceeded(clientId);
} else {
clientInfo.onStopDiscoveryFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.REGISTER_SERVICE:
if (DBG) Log.d(TAG, "Register service");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
if (requestLimitReached(clientInfo)) {
clientInfo.onRegisterServiceFailed(
clientId, NsdManager.FAILURE_MAX_LIMIT);
break;
}
maybeStartDaemon();
id = getUniqueId();
if (registerService(id, args.serviceInfo)) {
if (DBG) Log.d(TAG, "Register " + clientId + " " + id);
storeRequestMap(clientId, id, clientInfo, msg.what);
// Return success after mDns reports success
} else {
unregisterService(id);
clientInfo.onRegisterServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.UNREGISTER_SERVICE:
if (DBG) Log.d(TAG, "unregister service");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
if (clientInfo == null) {
Log.e(TAG, "Unknown connector in unregistration");
break;
}
id = clientInfo.mClientIds.get(clientId);
removeRequestMap(clientId, id, clientInfo);
if (unregisterService(id)) {
clientInfo.onUnregisterServiceSucceeded(clientId);
} else {
clientInfo.onUnregisterServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.RESOLVE_SERVICE:
if (DBG) Log.d(TAG, "Resolve service");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
if (clientInfo.mResolvedService != null) {
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_ALREADY_ACTIVE);
break;
}
maybeStartDaemon();
id = getUniqueId();
if (resolveService(id, args.serviceInfo)) {
clientInfo.mResolvedService = new NsdServiceInfo();
storeRequestMap(clientId, id, clientInfo, msg.what);
} else {
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case MDNS_SERVICE_EVENT:
if (!handleMDnsServiceEvent(msg.arg1, msg.arg2, msg.obj)) {
return NOT_HANDLED;
}
break;
default:
return NOT_HANDLED;
}
return HANDLED;
}
private boolean handleMDnsServiceEvent(int code, int id, Object obj) {
NsdServiceInfo servInfo;
ClientInfo clientInfo = mIdToClientInfoMap.get(id);
if (clientInfo == null) {
Log.e(TAG, String.format("id %d for %d has no client mapping", id, code));
return false;
}
/* This goes in response as msg.arg2 */
int clientId = clientInfo.getClientId(id);
if (clientId < 0) {
// This can happen because of race conditions. For example,
// SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY,
// and we may get in this situation.
Log.d(TAG, String.format("%d for listener id %d that is no longer active",
code, id));
return false;
}
if (DBG) {
Log.d(TAG, String.format("MDns service event code:%d id=%d", code, id));
}
switch (code) {
case IMDnsEventListener.SERVICE_FOUND: {
final DiscoveryInfo info = (DiscoveryInfo) obj;
final String name = info.serviceName;
final String type = info.registrationType;
servInfo = new NsdServiceInfo(name, type);
final int foundNetId = info.netId;
if (foundNetId == 0L) {
// Ignore services that do not have a Network: they are not usable
// by apps, as they would need privileged permissions to use
// interfaces that do not have an associated Network.
break;
}
setServiceNetworkForCallback(servInfo, info.netId, info.interfaceIdx);
clientInfo.onServiceFound(clientId, servInfo);
break;
}
case IMDnsEventListener.SERVICE_LOST: {
final DiscoveryInfo info = (DiscoveryInfo) obj;
final String name = info.serviceName;
final String type = info.registrationType;
final int lostNetId = info.netId;
servInfo = new NsdServiceInfo(name, type);
// The network could be set to null (netId 0) if it was torn down when the
// service is lost
// TODO: avoid returning null in that case, possibly by remembering
// found services on the same interface index and their network at the time
setServiceNetworkForCallback(servInfo, lostNetId, info.interfaceIdx);
clientInfo.onServiceLost(clientId, servInfo);
break;
}
case IMDnsEventListener.SERVICE_DISCOVERY_FAILED:
clientInfo.onDiscoverServicesFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
case IMDnsEventListener.SERVICE_REGISTERED: {
final RegistrationInfo info = (RegistrationInfo) obj;
final String name = info.serviceName;
servInfo = new NsdServiceInfo(name, null /* serviceType */);
clientInfo.onRegisterServiceSucceeded(clientId, servInfo);
break;
}
case IMDnsEventListener.SERVICE_REGISTRATION_FAILED:
clientInfo.onRegisterServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
case IMDnsEventListener.SERVICE_RESOLVED: {
final ResolutionInfo info = (ResolutionInfo) obj;
int index = 0;
final String fullName = info.serviceFullName;
while (index < fullName.length() && fullName.charAt(index) != '.') {
if (fullName.charAt(index) == '\\') {
++index;
}
++index;
}
if (index >= fullName.length()) {
Log.e(TAG, "Invalid service found " + fullName);
break;
}
String name = unescape(fullName.substring(0, index));
String rest = fullName.substring(index);
String type = rest.replace(".local.", "");
clientInfo.mResolvedService.setServiceName(name);
clientInfo.mResolvedService.setServiceType(type);
clientInfo.mResolvedService.setPort(info.port);
clientInfo.mResolvedService.setTxtRecords(info.txtRecord);
// Network will be added after SERVICE_GET_ADDR_SUCCESS
stopResolveService(id);
removeRequestMap(clientId, id, clientInfo);
final int id2 = getUniqueId();
if (getAddrInfo(id2, info.hostname, info.interfaceIdx)) {
storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE);
} else {
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
clientInfo.mResolvedService = null;
}
break;
}
case IMDnsEventListener.SERVICE_RESOLUTION_FAILED:
/* NNN resolveId errorCode */
stopResolveService(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
case IMDnsEventListener.SERVICE_GET_ADDR_FAILED:
/* NNN resolveId errorCode */
stopGetAddrInfo(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
case IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS: {
/* NNN resolveId hostname ttl addr interfaceIdx netId */
final GetAddressInfo info = (GetAddressInfo) obj;
final String address = info.address;
final int netId = info.netId;
InetAddress serviceHost = null;
try {
serviceHost = InetAddress.getByName(address);
} catch (UnknownHostException e) {
Log.wtf(TAG, "Invalid host in GET_ADDR_SUCCESS", e);
}
// If the resolved service is on an interface without a network, consider it
// as a failure: it would not be usable by apps as they would need
// privileged permissions.
if (netId != NETID_UNSET && serviceHost != null) {
clientInfo.mResolvedService.setHost(serviceHost);
setServiceNetworkForCallback(clientInfo.mResolvedService,
netId, info.interfaceIdx);
clientInfo.onResolveServiceSucceeded(
clientId, clientInfo.mResolvedService);
} else {
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
stopGetAddrInfo(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
break;
}
default:
return false;
}
return true;
}
}
}
private static void setServiceNetworkForCallback(NsdServiceInfo info, int netId, int ifaceIdx) {
switch (netId) {
case NETID_UNSET:
info.setNetwork(null);
break;
case INetd.LOCAL_NET_ID:
// Special case for LOCAL_NET_ID: Networks on netId 99 are not generally
// visible / usable for apps, so do not return it. Store the interface
// index instead, so at least if the client tries to resolve the service
// with that NsdServiceInfo, it will be done on the same interface.
// If they recreate the NsdServiceInfo themselves, resolution would be
// done on all interfaces as before T, which should also work.
info.setNetwork(null);
info.setInterfaceIndex(ifaceIdx);
break;
default:
info.setNetwork(new Network(netId));
}
}
// The full service name is escaped from standard DNS rules on mdnsresponder, making it suitable
// for passing to standard system DNS APIs such as res_query() . Thus, make the service name
// unescape for getting right service address. See "Notes on DNS Name Escaping" on
// external/mdnsresponder/mDNSShared/dns_sd.h for more details.
private String unescape(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (c == '\\') {
if (++i >= s.length()) {
Log.e(TAG, "Unexpected end of escape sequence in: " + s);
break;
}
c = s.charAt(i);
if (c != '.' && c != '\\') {
if (i + 2 >= s.length()) {
Log.e(TAG, "Unexpected end of escape sequence in: " + s);
break;
}
c = (char) ((c - '0') * 100 + (s.charAt(i + 1) - '0') * 10
+ (s.charAt(i + 2) - '0'));
i += 2;
}
}
sb.append(c);
}
return sb.toString();
}
@VisibleForTesting
NsdService(Context ctx, Handler handler, long cleanupDelayMs) {
mCleanupDelayMs = cleanupDelayMs;
mContext = ctx;
mNsdStateMachine = new NsdStateMachine(TAG, handler);
mNsdStateMachine.start();
mMDnsManager = ctx.getSystemService(MDnsManager.class);
mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine);
}
public static NsdService create(Context context) {
HandlerThread thread = new HandlerThread(TAG);
thread.start();
Handler handler = new Handler(thread.getLooper());
NsdService service = new NsdService(context, handler, CLEANUP_DELAY_MS);
return service;
}
private static class MDnsEventCallback extends IMDnsEventListener.Stub {
private final StateMachine mStateMachine;
MDnsEventCallback(StateMachine sm) {
mStateMachine = sm;
}
@Override
public void onServiceRegistrationStatus(final RegistrationInfo status) {
mStateMachine.sendMessage(
MDNS_SERVICE_EVENT, status.result, status.id, status);
}
@Override
public void onServiceDiscoveryStatus(final DiscoveryInfo status) {
mStateMachine.sendMessage(
MDNS_SERVICE_EVENT, status.result, status.id, status);
}
@Override
public void onServiceResolutionStatus(final ResolutionInfo status) {
mStateMachine.sendMessage(
MDNS_SERVICE_EVENT, status.result, status.id, status);
}
@Override
public void onGettingServiceAddressStatus(final GetAddressInfo status) {
mStateMachine.sendMessage(
MDNS_SERVICE_EVENT, status.result, status.id, status);
}
@Override
public int getInterfaceVersion() throws RemoteException {
return this.VERSION;
}
@Override
public String getInterfaceHash() throws RemoteException {
return this.HASH;
}
}
@Override
public INsdServiceConnector connect(INsdManagerCallback cb) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService");
final INsdServiceConnector connector = new NsdServiceConnector();
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.REGISTER_CLIENT, new Pair<>(connector, cb)));
return connector;
}
private static class ListenerArgs {
public final NsdServiceConnector connector;
public final NsdServiceInfo serviceInfo;
ListenerArgs(NsdServiceConnector connector, NsdServiceInfo serviceInfo) {
this.connector = connector;
this.serviceInfo = serviceInfo;
}
}
private class NsdServiceConnector extends INsdServiceConnector.Stub
implements IBinder.DeathRecipient {
@Override
public void registerService(int listenerKey, NsdServiceInfo serviceInfo) {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.REGISTER_SERVICE, 0, listenerKey,
new ListenerArgs(this, serviceInfo)));
}
@Override
public void unregisterService(int listenerKey) {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.UNREGISTER_SERVICE, 0, listenerKey,
new ListenerArgs(this, null)));
}
@Override
public void discoverServices(int listenerKey, NsdServiceInfo serviceInfo) {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.DISCOVER_SERVICES, 0, listenerKey,
new ListenerArgs(this, serviceInfo)));
}
@Override
public void stopDiscovery(int listenerKey) {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.STOP_DISCOVERY, 0, listenerKey, new ListenerArgs(this, null)));
}
@Override
public void resolveService(int listenerKey, NsdServiceInfo serviceInfo) {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.RESOLVE_SERVICE, 0, listenerKey,
new ListenerArgs(this, serviceInfo)));
}
@Override
public void startDaemon() {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.DAEMON_STARTUP, new ListenerArgs(this, null)));
}
@Override
public void binderDied() {
mNsdStateMachine.sendMessage(
mNsdStateMachine.obtainMessage(NsdManager.UNREGISTER_CLIENT, this));
}
}
private void sendNsdStateChangeBroadcast(boolean isEnabled) {
final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
int nsdState = isEnabled ? NsdManager.NSD_STATE_ENABLED : NsdManager.NSD_STATE_DISABLED;
intent.putExtra(NsdManager.EXTRA_NSD_STATE, nsdState);
mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
}
private int getUniqueId() {
if (++mUniqueId == INVALID_ID) return ++mUniqueId;
return mUniqueId;
}
private boolean registerService(int regId, NsdServiceInfo service) {
if (DBG) {
Log.d(TAG, "registerService: " + regId + " " + service);
}
String name = service.getServiceName();
String type = service.getServiceType();
int port = service.getPort();
byte[] textRecord = service.getTxtRecord();
final int registerInterface = getNetworkInterfaceIndex(service);
if (service.getNetwork() != null && registerInterface == IFACE_IDX_ANY) {
Log.e(TAG, "Interface to register service on not found");
return false;
}
return mMDnsManager.registerService(regId, name, type, port, textRecord, registerInterface);
}
private boolean unregisterService(int regId) {
return mMDnsManager.stopOperation(regId);
}
private boolean discoverServices(int discoveryId, NsdServiceInfo serviceInfo) {
final String type = serviceInfo.getServiceType();
final int discoverInterface = getNetworkInterfaceIndex(serviceInfo);
if (serviceInfo.getNetwork() != null && discoverInterface == IFACE_IDX_ANY) {
Log.e(TAG, "Interface to discover service on not found");
return false;
}
return mMDnsManager.discover(discoveryId, type, discoverInterface);
}
private boolean stopServiceDiscovery(int discoveryId) {
return mMDnsManager.stopOperation(discoveryId);
}
private boolean resolveService(int resolveId, NsdServiceInfo service) {
final String name = service.getServiceName();
final String type = service.getServiceType();
final int resolveInterface = getNetworkInterfaceIndex(service);
if (service.getNetwork() != null && resolveInterface == IFACE_IDX_ANY) {
Log.e(TAG, "Interface to resolve service on not found");
return false;
}
return mMDnsManager.resolve(resolveId, name, type, "local.", resolveInterface);
}
/**
* Guess the interface to use to resolve or discover a service on a specific network.
*
* This is an imperfect guess, as for example the network may be gone or not yet fully
* registered. This is fine as failing is correct if the network is gone, and a client
* attempting to resolve/discover on a network not yet setup would have a bad time anyway; also
* this is to support the legacy mdnsresponder implementation, which historically resolved
* services on an unspecified network.
*/
private int getNetworkInterfaceIndex(NsdServiceInfo serviceInfo) {
final Network network = serviceInfo.getNetwork();
if (network == null) {
// Fallback to getInterfaceIndex if present (typically if the NsdServiceInfo was
// provided by NsdService from discovery results, and the service was found on an
// interface that has no app-usable Network).
if (serviceInfo.getInterfaceIndex() != 0) {
return serviceInfo.getInterfaceIndex();
}
return IFACE_IDX_ANY;
}
final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class);
if (cm == null) {
Log.wtf(TAG, "No ConnectivityManager for resolveService");
return IFACE_IDX_ANY;
}
final LinkProperties lp = cm.getLinkProperties(network);
if (lp == null) return IFACE_IDX_ANY;
// Only resolve on non-stacked interfaces
final NetworkInterface iface;
try {
iface = NetworkInterface.getByName(lp.getInterfaceName());
} catch (SocketException e) {
Log.e(TAG, "Error querying interface", e);
return IFACE_IDX_ANY;
}
if (iface == null) {
Log.e(TAG, "Interface not found: " + lp.getInterfaceName());
return IFACE_IDX_ANY;
}
return iface.getIndex();
}
private boolean stopResolveService(int resolveId) {
return mMDnsManager.stopOperation(resolveId);
}
private boolean getAddrInfo(int resolveId, String hostname, int interfaceIdx) {
return mMDnsManager.getServiceAddress(resolveId, hostname, interfaceIdx);
}
private boolean stopGetAddrInfo(int resolveId) {
return mMDnsManager.stopOperation(resolveId);
}
@Override
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
!= PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump " + TAG
+ " due to missing android.permission.DUMP permission");
return;
}
for (ClientInfo client : mClients.values()) {
pw.println("Client Info");
pw.println(client);
}
mNsdStateMachine.dump(fd, pw, args);
}
/* Information tracked per client */
private class ClientInfo {
private static final int MAX_LIMIT = 10;
private final INsdManagerCallback mCb;
/* Remembers a resolved service until getaddrinfo completes */
private NsdServiceInfo mResolvedService;
/* A map from client id to unique id sent to mDns */
private final SparseIntArray mClientIds = new SparseIntArray();
/* A map from client id to the type of the request we had received */
private final SparseIntArray mClientRequests = new SparseIntArray();
// The target SDK of this client < Build.VERSION_CODES.S
private boolean mIsLegacy = false;
private ClientInfo(INsdManagerCallback cb) {
mCb = cb;
if (DBG) Log.d(TAG, "New client");
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("mResolvedService ").append(mResolvedService).append("\n");
sb.append("mIsLegacy ").append(mIsLegacy).append("\n");
for(int i = 0; i< mClientIds.size(); i++) {
int clientID = mClientIds.keyAt(i);
sb.append("clientId ").append(clientID).
append(" mDnsId ").append(mClientIds.valueAt(i)).
append(" type ").append(mClientRequests.get(clientID)).append("\n");
}
return sb.toString();
}
private boolean isLegacy() {
return mIsLegacy;
}
private void setLegacy() {
mIsLegacy = true;
}
// Remove any pending requests from the global map when we get rid of a client,
// and send cancellations to the daemon.
private void expungeAllRequests() {
int globalId, clientId, i;
// TODO: to keep handler responsive, do not clean all requests for that client at once.
for (i = 0; i < mClientIds.size(); i++) {
clientId = mClientIds.keyAt(i);
globalId = mClientIds.valueAt(i);
mIdToClientInfoMap.remove(globalId);
if (DBG) {
Log.d(TAG, "Terminating client-ID " + clientId
+ " global-ID " + globalId + " type " + mClientRequests.get(clientId));
}
switch (mClientRequests.get(clientId)) {
case NsdManager.DISCOVER_SERVICES:
stopServiceDiscovery(globalId);
break;
case NsdManager.RESOLVE_SERVICE:
stopResolveService(globalId);
break;
case NsdManager.REGISTER_SERVICE:
unregisterService(globalId);
break;
default:
break;
}
}
mClientIds.clear();
mClientRequests.clear();
}
// mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id,
// return the corresponding listener id. mDnsClient id is also called a global id.
private int getClientId(final int globalId) {
int idx = mClientIds.indexOfValue(globalId);
if (idx < 0) {
return idx;
}
return mClientIds.keyAt(idx);
}
void onDiscoverServicesStarted(int listenerKey, NsdServiceInfo info) {
try {
mCb.onDiscoverServicesStarted(listenerKey, info);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onDiscoverServicesStarted", e);
}
}
void onDiscoverServicesFailed(int listenerKey, int error) {
try {
mCb.onDiscoverServicesFailed(listenerKey, error);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onDiscoverServicesFailed", e);
}
}
void onServiceFound(int listenerKey, NsdServiceInfo info) {
try {
mCb.onServiceFound(listenerKey, info);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onServiceFound(", e);
}
}
void onServiceLost(int listenerKey, NsdServiceInfo info) {
try {
mCb.onServiceLost(listenerKey, info);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onServiceLost(", e);
}
}
void onStopDiscoveryFailed(int listenerKey, int error) {
try {
mCb.onStopDiscoveryFailed(listenerKey, error);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onStopDiscoveryFailed", e);
}
}
void onStopDiscoverySucceeded(int listenerKey) {
try {
mCb.onStopDiscoverySucceeded(listenerKey);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onStopDiscoverySucceeded", e);
}
}
void onRegisterServiceFailed(int listenerKey, int error) {
try {
mCb.onRegisterServiceFailed(listenerKey, error);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onRegisterServiceFailed", e);
}
}
void onRegisterServiceSucceeded(int listenerKey, NsdServiceInfo info) {
try {
mCb.onRegisterServiceSucceeded(listenerKey, info);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onRegisterServiceSucceeded", e);
}
}
void onUnregisterServiceFailed(int listenerKey, int error) {
try {
mCb.onUnregisterServiceFailed(listenerKey, error);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onUnregisterServiceFailed", e);
}
}
void onUnregisterServiceSucceeded(int listenerKey) {
try {
mCb.onUnregisterServiceSucceeded(listenerKey);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onUnregisterServiceSucceeded", e);
}
}
void onResolveServiceFailed(int listenerKey, int error) {
try {
mCb.onResolveServiceFailed(listenerKey, error);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onResolveServiceFailed", e);
}
}
void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) {
try {
mCb.onResolveServiceSucceeded(listenerKey, info);
} catch (RemoteException e) {
Log.e(TAG, "Error calling onResolveServiceSucceeded", e);
}
}
}
}
|