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
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server;
import android.content.Context;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.DnsSdTxtRecord;
import android.net.nsd.INsdManager;
import android.net.nsd.NsdManager;
import android.os.Binder;
import android.os.Message;
import android.os.Messenger;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Slog;
import android.util.SparseArray;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import com.android.internal.util.AsyncChannel;
import com.android.internal.util.Protocol;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.server.NativeDaemonConnector.Command;
/**
* Network Service Discovery Service handles remote service discovery operation requests by
* implementing the INsdManager interface.
*
* @hide
*/
public class NsdService extends INsdManager.Stub {
private static final String TAG = "NsdService";
private static final String MDNS_TAG = "mDnsConnector";
private static final boolean DBG = true;
private Context mContext;
private ContentResolver mContentResolver;
private NsdStateMachine mNsdStateMachine;
/**
* Clients receiving asynchronous messages
*/
private HashMap<Messenger, ClientInfo> mClients = new HashMap<Messenger, ClientInfo>();
/* A map from unique id to client info */
private SparseArray<ClientInfo> mIdToClientInfoMap= new SparseArray<ClientInfo>();
private AsyncChannel mReplyChannel = new AsyncChannel();
private int INVALID_ID = 0;
private int mUniqueId = 1;
private static final int BASE = Protocol.BASE_NSD_MANAGER;
private static final int CMD_TO_STRING_COUNT = NsdManager.RESOLVE_SERVICE - BASE + 1;
private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
static {
sCmdToString[NsdManager.DISCOVER_SERVICES - BASE] = "DISCOVER";
sCmdToString[NsdManager.STOP_DISCOVERY - BASE] = "STOP-DISCOVER";
sCmdToString[NsdManager.REGISTER_SERVICE - BASE] = "REGISTER";
sCmdToString[NsdManager.UNREGISTER_SERVICE - BASE] = "UNREGISTER";
sCmdToString[NsdManager.RESOLVE_SERVICE - BASE] = "RESOLVE";
}
private static String cmdToString(int cmd) {
cmd -= BASE;
if ((cmd >= 0) && (cmd < sCmdToString.length)) {
return sCmdToString[cmd];
} else {
return null;
}
}
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 cmdToString(what);
}
/**
* Observes the NSD on/off setting, and takes action when changed.
*/
private void registerForNsdSetting() {
ContentObserver contentObserver = new ContentObserver(this.getHandler()) {
@Override
public void onChange(boolean selfChange) {
if (isNsdEnabled()) {
mNsdStateMachine.sendMessage(NsdManager.ENABLE);
} else {
mNsdStateMachine.sendMessage(NsdManager.DISABLE);
}
}
};
mContext.getContentResolver().registerContentObserver(
Settings.Global.getUriFor(Settings.Global.NSD_ON),
false, contentObserver);
}
NsdStateMachine(String name) {
super(name);
addState(mDefaultState);
addState(mDisabledState, mDefaultState);
addState(mEnabledState, mDefaultState);
if (isNsdEnabled()) {
setInitialState(mEnabledState);
} else {
setInitialState(mDisabledState);
}
setLogRecSize(25);
registerForNsdSetting();
}
class DefaultState extends State {
@Override
public boolean processMessage(Message msg) {
ClientInfo cInfo = null;
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
AsyncChannel c = (AsyncChannel) msg.obj;
if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
cInfo = new ClientInfo(c, msg.replyTo);
mClients.put(msg.replyTo, cInfo);
} else {
Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
}
break;
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
switch (msg.arg1) {
case AsyncChannel.STATUS_SEND_UNSUCCESSFUL:
Slog.e(TAG, "Send failed, client connection lost");
break;
case AsyncChannel.STATUS_REMOTE_DISCONNECTION:
if (DBG) Slog.d(TAG, "Client disconnected");
break;
default:
if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
break;
}
cInfo = mClients.get(msg.replyTo);
if (cInfo != null) {
cInfo.expungeAllRequests();
mClients.remove(msg.replyTo);
}
//Last client
if (mClients.size() == 0) {
stopMDnsDaemon();
}
break;
case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
AsyncChannel ac = new AsyncChannel();
ac.connect(mContext, getHandler(), msg.replyTo);
break;
case NsdManager.DISCOVER_SERVICES:
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
case NsdManager.STOP_DISCOVERY:
replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
case NsdManager.REGISTER_SERVICE:
replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
case NsdManager.UNREGISTER_SERVICE:
replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
case NsdManager.RESOLVE_SERVICE:
replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
case NsdManager.NATIVE_DAEMON_EVENT:
default:
Slog.e(TAG, "Unhandled " + msg);
return NOT_HANDLED;
}
return HANDLED;
}
}
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);
if (mClients.size() > 0) {
startMDnsDaemon();
}
}
@Override
public void exit() {
if (mClients.size() > 0) {
stopMDnsDaemon();
}
}
private boolean requestLimitReached(ClientInfo clientInfo) {
if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) {
if (DBG) Slog.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);
}
private void removeRequestMap(int clientId, int globalId, ClientInfo clientInfo) {
clientInfo.mClientIds.remove(clientId);
clientInfo.mClientRequests.remove(clientId);
mIdToClientInfoMap.remove(globalId);
}
@Override
public boolean processMessage(Message msg) {
ClientInfo clientInfo;
NsdServiceInfo servInfo;
boolean result = HANDLED;
int id;
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
//First client
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL &&
mClients.size() == 0) {
startMDnsDaemon();
}
result = NOT_HANDLED;
break;
case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
result = NOT_HANDLED;
break;
case NsdManager.DISABLE:
//TODO: cleanup clients
transitionTo(mDisabledState);
break;
case NsdManager.DISCOVER_SERVICES:
if (DBG) Slog.d(TAG, "Discover services");
servInfo = (NsdServiceInfo) msg.obj;
clientInfo = mClients.get(msg.replyTo);
if (requestLimitReached(clientInfo)) {
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
NsdManager.FAILURE_MAX_LIMIT);
break;
}
id = getUniqueId();
if (discoverServices(id, servInfo.getServiceType())) {
if (DBG) {
Slog.d(TAG, "Discover " + msg.arg2 + " " + id +
servInfo.getServiceType());
}
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED, servInfo);
} else {
stopServiceDiscovery(id);
replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.STOP_DISCOVERY:
if (DBG) Slog.d(TAG, "Stop service discovery");
clientInfo = mClients.get(msg.replyTo);
try {
id = clientInfo.mClientIds.get(msg.arg2).intValue();
} catch (NullPointerException e) {
replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
}
removeRequestMap(msg.arg2, id, clientInfo);
if (stopServiceDiscovery(id)) {
replyToMessage(msg, NsdManager.STOP_DISCOVERY_SUCCEEDED);
} else {
replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.REGISTER_SERVICE:
if (DBG) Slog.d(TAG, "Register service");
clientInfo = mClients.get(msg.replyTo);
if (requestLimitReached(clientInfo)) {
replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
NsdManager.FAILURE_MAX_LIMIT);
break;
}
id = getUniqueId();
if (registerService(id, (NsdServiceInfo) msg.obj)) {
if (DBG) Slog.d(TAG, "Register " + msg.arg2 + " " + id);
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
// Return success after mDns reports success
} else {
unregisterService(id);
replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.UNREGISTER_SERVICE:
if (DBG) Slog.d(TAG, "unregister service");
clientInfo = mClients.get(msg.replyTo);
try {
id = clientInfo.mClientIds.get(msg.arg2).intValue();
} catch (NullPointerException e) {
replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
break;
}
removeRequestMap(msg.arg2, id, clientInfo);
if (unregisterService(id)) {
replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_SUCCEEDED);
} else {
replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.RESOLVE_SERVICE:
if (DBG) Slog.d(TAG, "Resolve service");
servInfo = (NsdServiceInfo) msg.obj;
clientInfo = mClients.get(msg.replyTo);
if (clientInfo.mResolvedService != null) {
replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_ALREADY_ACTIVE);
break;
}
id = getUniqueId();
if (resolveService(id, servInfo)) {
clientInfo.mResolvedService = new NsdServiceInfo();
storeRequestMap(msg.arg2, id, clientInfo, msg.what);
} else {
replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
case NsdManager.NATIVE_DAEMON_EVENT:
NativeEvent event = (NativeEvent) msg.obj;
if (!handleNativeEvent(event.code, event.raw, event.cooked)) {
result = NOT_HANDLED;
}
break;
default:
result = NOT_HANDLED;
break;
}
return result;
}
private boolean handleNativeEvent(int code, String raw, String[] cooked) {
boolean handled = true;
NsdServiceInfo servInfo;
int id = Integer.parseInt(cooked[1]);
ClientInfo clientInfo = mIdToClientInfoMap.get(id);
if (clientInfo == null) {
Slog.e(TAG, "Unique id with no client mapping: " + id);
handled = false;
return handled;
}
/* 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.
Slog.d(TAG, "Notification for a listener that is no longer active: " + id);
handled = false;
return handled;
}
switch (code) {
case NativeResponseCode.SERVICE_FOUND:
/* NNN uniqueId serviceName regType domain */
if (DBG) Slog.d(TAG, "SERVICE_FOUND Raw: " + raw);
servInfo = new NsdServiceInfo(cooked[2], cooked[3]);
clientInfo.mChannel.sendMessage(NsdManager.SERVICE_FOUND, 0,
clientId, servInfo);
break;
case NativeResponseCode.SERVICE_LOST:
/* NNN uniqueId serviceName regType domain */
if (DBG) Slog.d(TAG, "SERVICE_LOST Raw: " + raw);
servInfo = new NsdServiceInfo(cooked[2], cooked[3]);
clientInfo.mChannel.sendMessage(NsdManager.SERVICE_LOST, 0,
clientId, servInfo);
break;
case NativeResponseCode.SERVICE_DISCOVERY_FAILED:
/* NNN uniqueId errorCode */
if (DBG) Slog.d(TAG, "SERVICE_DISC_FAILED Raw: " + raw);
clientInfo.mChannel.sendMessage(NsdManager.DISCOVER_SERVICES_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
break;
case NativeResponseCode.SERVICE_REGISTERED:
/* NNN regId serviceName regType */
if (DBG) Slog.d(TAG, "SERVICE_REGISTERED Raw: " + raw);
servInfo = new NsdServiceInfo(cooked[2], null);
clientInfo.mChannel.sendMessage(NsdManager.REGISTER_SERVICE_SUCCEEDED,
id, clientId, servInfo);
break;
case NativeResponseCode.SERVICE_REGISTRATION_FAILED:
/* NNN regId errorCode */
if (DBG) Slog.d(TAG, "SERVICE_REGISTER_FAILED Raw: " + raw);
clientInfo.mChannel.sendMessage(NsdManager.REGISTER_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
break;
case NativeResponseCode.SERVICE_UPDATED:
/* NNN regId */
break;
case NativeResponseCode.SERVICE_UPDATE_FAILED:
/* NNN regId errorCode */
break;
case NativeResponseCode.SERVICE_RESOLVED:
/* NNN resolveId fullName hostName port txtlen txtdata */
if (DBG) Slog.d(TAG, "SERVICE_RESOLVED Raw: " + raw);
int index = 0;
while (index < cooked[2].length() && cooked[2].charAt(index) != '.') {
if (cooked[2].charAt(index) == '\\') {
++index;
}
++index;
}
if (index >= cooked[2].length()) {
Slog.e(TAG, "Invalid service found " + raw);
break;
}
String name = cooked[2].substring(0, index);
String rest = cooked[2].substring(index);
String type = rest.replace(".local.", "");
name = unescape(name);
clientInfo.mResolvedService.setServiceName(name);
clientInfo.mResolvedService.setServiceType(type);
clientInfo.mResolvedService.setPort(Integer.parseInt(cooked[4]));
stopResolveService(id);
removeRequestMap(clientId, id, clientInfo);
int id2 = getUniqueId();
if (getAddrInfo(id2, cooked[3])) {
storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE);
} else {
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
clientInfo.mResolvedService = null;
}
break;
case NativeResponseCode.SERVICE_RESOLUTION_FAILED:
/* NNN resolveId errorCode */
if (DBG) Slog.d(TAG, "SERVICE_RESOLVE_FAILED Raw: " + raw);
stopResolveService(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
break;
case NativeResponseCode.SERVICE_GET_ADDR_FAILED:
/* NNN resolveId errorCode */
stopGetAddrInfo(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
if (DBG) Slog.d(TAG, "SERVICE_RESOLVE_FAILED Raw: " + raw);
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
break;
case NativeResponseCode.SERVICE_GET_ADDR_SUCCESS:
/* NNN resolveId hostname ttl addr */
if (DBG) Slog.d(TAG, "SERVICE_GET_ADDR_SUCCESS Raw: " + raw);
try {
clientInfo.mResolvedService.setHost(InetAddress.getByName(cooked[4]));
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_SUCCEEDED,
0, clientId, clientInfo.mResolvedService);
} catch (java.net.UnknownHostException e) {
clientInfo.mChannel.sendMessage(NsdManager.RESOLVE_SERVICE_FAILED,
NsdManager.FAILURE_INTERNAL_ERROR, clientId);
}
stopGetAddrInfo(id);
removeRequestMap(clientId, id, clientInfo);
clientInfo.mResolvedService = null;
break;
default:
handled = false;
break;
}
return handled;
}
}
}
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()) {
Slog.e(TAG, "Unexpected end of escape sequence in: " + s);
break;
}
c = s.charAt(i);
if (c != '.' && c != '\\') {
if (i + 2 >= s.length()) {
Slog.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();
}
private NativeDaemonConnector mNativeConnector;
private final CountDownLatch mNativeDaemonConnected = new CountDownLatch(1);
private NsdService(Context context) {
mContext = context;
mContentResolver = context.getContentResolver();
mNativeConnector = new NativeDaemonConnector(new NativeCallbackReceiver(), "mdns", 10,
MDNS_TAG, 25, null);
mNsdStateMachine = new NsdStateMachine(TAG);
mNsdStateMachine.start();
Thread th = new Thread(mNativeConnector, MDNS_TAG);
th.start();
}
public static NsdService create(Context context) throws InterruptedException {
NsdService service = new NsdService(context);
service.mNativeDaemonConnected.await();
return service;
}
public Messenger getMessenger() {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET,
"NsdService");
return new Messenger(mNsdStateMachine.getHandler());
}
public void setEnabled(boolean enable) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
"NsdService");
Settings.Global.putInt(mContentResolver, Settings.Global.NSD_ON, enable ? 1 : 0);
if (enable) {
mNsdStateMachine.sendMessage(NsdManager.ENABLE);
} else {
mNsdStateMachine.sendMessage(NsdManager.DISABLE);
}
}
private void sendNsdStateChangeBroadcast(boolean enabled) {
final Intent intent = new Intent(NsdManager.ACTION_NSD_STATE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
if (enabled) {
intent.putExtra(NsdManager.EXTRA_NSD_STATE, NsdManager.NSD_STATE_ENABLED);
} else {
intent.putExtra(NsdManager.EXTRA_NSD_STATE, NsdManager.NSD_STATE_DISABLED);
}
mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
}
private boolean isNsdEnabled() {
boolean ret = Settings.Global.getInt(mContentResolver, Settings.Global.NSD_ON, 1) == 1;
if (DBG) Slog.d(TAG, "Network service discovery enabled " + ret);
return ret;
}
private int getUniqueId() {
if (++mUniqueId == INVALID_ID) return ++mUniqueId;
return mUniqueId;
}
/* These should be in sync with system/netd/server/ResponseCode.h */
class NativeResponseCode {
public static final int SERVICE_DISCOVERY_FAILED = 602;
public static final int SERVICE_FOUND = 603;
public static final int SERVICE_LOST = 604;
public static final int SERVICE_REGISTRATION_FAILED = 605;
public static final int SERVICE_REGISTERED = 606;
public static final int SERVICE_RESOLUTION_FAILED = 607;
public static final int SERVICE_RESOLVED = 608;
public static final int SERVICE_UPDATED = 609;
public static final int SERVICE_UPDATE_FAILED = 610;
public static final int SERVICE_GET_ADDR_FAILED = 611;
public static final int SERVICE_GET_ADDR_SUCCESS = 612;
}
private class NativeEvent {
final int code;
final String raw;
final String[] cooked;
NativeEvent(int code, String raw, String[] cooked) {
this.code = code;
this.raw = raw;
this.cooked = cooked;
}
}
class NativeCallbackReceiver implements INativeDaemonConnectorCallbacks {
public void onDaemonConnected() {
mNativeDaemonConnected.countDown();
}
public boolean onCheckHoldWakeLock(int code) {
return false;
}
public boolean onEvent(int code, String raw, String[] cooked) {
// TODO: NDC translates a message to a callback, we could enhance NDC to
// directly interact with a state machine through messages
NativeEvent event = new NativeEvent(code, raw, cooked);
mNsdStateMachine.sendMessage(NsdManager.NATIVE_DAEMON_EVENT, event);
return true;
}
}
private boolean startMDnsDaemon() {
if (DBG) Slog.d(TAG, "startMDnsDaemon");
try {
mNativeConnector.execute("mdnssd", "start-service");
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to start daemon" + e);
return false;
}
return true;
}
private boolean stopMDnsDaemon() {
if (DBG) Slog.d(TAG, "stopMDnsDaemon");
try {
mNativeConnector.execute("mdnssd", "stop-service");
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to start daemon" + e);
return false;
}
return true;
}
private boolean registerService(int regId, NsdServiceInfo service) {
if (DBG) Slog.d(TAG, "registerService: " + regId + " " + service);
try {
Command cmd = new Command("mdnssd", "register", regId, service.getServiceName(),
service.getServiceType(), service.getPort());
// Add TXT records as additional arguments.
Map<String, byte[]> txtRecords = service.getAttributes();
for (String key : txtRecords.keySet()) {
try {
// TODO: Send encoded TXT record as bytes once NDC/netd supports binary data.
byte[] recordValue = txtRecords.get(key);
cmd.appendArg(String.format(Locale.US, "%s=%s", key,
recordValue != null ? new String(recordValue, "UTF_8") : ""));
} catch (UnsupportedEncodingException e) {
Slog.e(TAG, "Failed to encode txtRecord " + e);
}
}
mNativeConnector.execute(cmd);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to execute registerService " + e);
return false;
}
return true;
}
private boolean unregisterService(int regId) {
if (DBG) Slog.d(TAG, "unregisterService: " + regId);
try {
mNativeConnector.execute("mdnssd", "stop-register", regId);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to execute unregisterService " + e);
return false;
}
return true;
}
private boolean updateService(int regId, DnsSdTxtRecord t) {
if (DBG) Slog.d(TAG, "updateService: " + regId + " " + t);
try {
if (t == null) return false;
mNativeConnector.execute("mdnssd", "update", regId, t.size(), t.getRawData());
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to updateServices " + e);
return false;
}
return true;
}
private boolean discoverServices(int discoveryId, String serviceType) {
if (DBG) Slog.d(TAG, "discoverServices: " + discoveryId + " " + serviceType);
try {
mNativeConnector.execute("mdnssd", "discover", discoveryId, serviceType);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to discoverServices " + e);
return false;
}
return true;
}
private boolean stopServiceDiscovery(int discoveryId) {
if (DBG) Slog.d(TAG, "stopServiceDiscovery: " + discoveryId);
try {
mNativeConnector.execute("mdnssd", "stop-discover", discoveryId);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to stopServiceDiscovery " + e);
return false;
}
return true;
}
private boolean resolveService(int resolveId, NsdServiceInfo service) {
if (DBG) Slog.d(TAG, "resolveService: " + resolveId + " " + service);
try {
mNativeConnector.execute("mdnssd", "resolve", resolveId, service.getServiceName(),
service.getServiceType(), "local.");
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to resolveService " + e);
return false;
}
return true;
}
private boolean stopResolveService(int resolveId) {
if (DBG) Slog.d(TAG, "stopResolveService: " + resolveId);
try {
mNativeConnector.execute("mdnssd", "stop-resolve", resolveId);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to stop resolve " + e);
return false;
}
return true;
}
private boolean getAddrInfo(int resolveId, String hostname) {
if (DBG) Slog.d(TAG, "getAdddrInfo: " + resolveId);
try {
mNativeConnector.execute("mdnssd", "getaddrinfo", resolveId, hostname);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to getAddrInfo " + e);
return false;
}
return true;
}
private boolean stopGetAddrInfo(int resolveId) {
if (DBG) Slog.d(TAG, "stopGetAdddrInfo: " + resolveId);
try {
mNativeConnector.execute("mdnssd", "stop-getaddrinfo", resolveId);
} catch(NativeDaemonConnectorException e) {
Slog.e(TAG, "Failed to stopGetAddrInfo " + e);
return false;
}
return true;
}
@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 ServiceDiscoverService from from pid="
+ Binder.getCallingPid()
+ ", uid=" + Binder.getCallingUid());
return;
}
for (ClientInfo client : mClients.values()) {
pw.println("Client Info");
pw.println(client);
}
mNsdStateMachine.dump(fd, pw, args);
}
/* arg2 on the source message has an id that needs to be retained in replies
* see NsdManager for details */
private Message obtainMessage(Message srcMsg) {
Message msg = Message.obtain();
msg.arg2 = srcMsg.arg2;
return msg;
}
private void replyToMessage(Message msg, int what) {
if (msg.replyTo == null) return;
Message dstMsg = obtainMessage(msg);
dstMsg.what = what;
mReplyChannel.replyToMessage(msg, dstMsg);
}
private void replyToMessage(Message msg, int what, int arg1) {
if (msg.replyTo == null) return;
Message dstMsg = obtainMessage(msg);
dstMsg.what = what;
dstMsg.arg1 = arg1;
mReplyChannel.replyToMessage(msg, dstMsg);
}
private void replyToMessage(Message msg, int what, Object obj) {
if (msg.replyTo == null) return;
Message dstMsg = obtainMessage(msg);
dstMsg.what = what;
dstMsg.obj = obj;
mReplyChannel.replyToMessage(msg, dstMsg);
}
/* Information tracked per client */
private class ClientInfo {
private static final int MAX_LIMIT = 10;
private final AsyncChannel mChannel;
private final Messenger mMessenger;
/* Remembers a resolved service until getaddrinfo completes */
private NsdServiceInfo mResolvedService;
/* A map from client id to unique id sent to mDns */
private SparseArray<Integer> mClientIds = new SparseArray<Integer>();
/* A map from client id to the type of the request we had received */
private SparseArray<Integer> mClientRequests = new SparseArray<Integer>();
private ClientInfo(AsyncChannel c, Messenger m) {
mChannel = c;
mMessenger = m;
if (DBG) Slog.d(TAG, "New client, channel: " + c + " messenger: " + m);
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("mChannel ").append(mChannel).append("\n");
sb.append("mMessenger ").append(mMessenger).append("\n");
sb.append("mResolvedService ").append(mResolvedService).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();
}
// 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;
for (i = 0; i < mClientIds.size(); i++) {
clientId = mClientIds.keyAt(i);
globalId = mClientIds.valueAt(i);
mIdToClientInfoMap.remove(globalId);
if (DBG) Slog.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) {
// This doesn't use mClientIds.indexOfValue because indexOfValue uses == (not .equals)
// while also coercing the int primitives to Integer objects.
for (int i = 0, nSize = mClientIds.size(); i < nSize; i++) {
int mDnsId = mClientIds.valueAt(i);
if (globalId == mDnsId) {
return mClientIds.keyAt(i);
}
}
return -1;
}
}
}
|