2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <arpa/inet.h>
22 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
30 #define LOG_TAG "MDnsDS"
34 #include <cutils/log.h>
35 #include <cutils/properties.h>
36 #include <sysutils/SocketClient.h>
38 #include "MDnsSdListener.h"
39 #include "ResponseCode.h"
41 #define MDNS_SERVICE_NAME "mdnsd"
42 #define MDNS_SERVICE_STATUS "init.svc.mdnsd"
44 MDnsSdListener::MDnsSdListener() :
45 FrameworkListener("mdns", true) {
46 Monitor *m = new Monitor();
47 registerCmd(new Handler(m, this));
50 MDnsSdListener::Handler::Handler(Monitor *m, MDnsSdListener *listener) :
51 NetdCommand("mdnssd") {
52 if (DBG) ALOGD("MDnsSdListener::Hander starting up");
57 MDnsSdListener::Handler::~Handler() {}
59 void MDnsSdListener::Handler::discover(SocketClient *cli,
64 const int requestFlags) {
66 ALOGD("discover(%s, %s, %s, %d, %d)", iface, regType, domain, requestId,
69 Context *context = new Context(requestId, mListener);
70 DNSServiceRef *ref = mMonitor->allocateServiceRef(requestId, context);
72 ALOGE("requestId %d already in use during discover call", requestId);
73 cli->sendMsg(ResponseCode::CommandParameterError,
74 "RequestId already in use during discover call", false);
77 if (VDBG) ALOGD("using ref %p", ref);
78 DNSServiceFlags nativeFlags = iToFlags(requestFlags);
79 int interfaceInt = ifaceNameToI(iface);
81 DNSServiceErrorType result = DNSServiceBrowse(ref, nativeFlags, interfaceInt, regType,
82 domain, &MDnsSdListenerDiscoverCallback, context);
83 if (result != kDNSServiceErr_NoError) {
84 ALOGE("Discover request %d got an error from DNSServiceBrowse %d", requestId, result);
85 mMonitor->freeServiceRef(requestId);
86 cli->sendMsg(ResponseCode::CommandParameterError,
87 "Discover request got an error from DNSServiceBrowse", false);
90 mMonitor->startMonitoring(requestId);
91 if (VDBG) ALOGD("discover successful");
92 cli->sendMsg(ResponseCode::CommandOkay, "Discover operation started", false);
96 void MDnsSdListenerDiscoverCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
97 uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *serviceName,
98 const char *regType, const char *replyDomain, void *inContext) {
99 MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
101 int refNumber = context->mRefNumber;
103 if (errorCode != kDNSServiceErr_NoError) {
104 asprintf(&msg, "%d %d", refNumber, errorCode);
105 context->mListener->sendBroadcast(ResponseCode::ServiceDiscoveryFailed, msg, false);
106 if (DBG) ALOGE("discover failure for %d, error= %d", refNumber, errorCode);
109 char *quotedServiceName = SocketClient::quoteArg(serviceName);
110 if (flags & kDNSServiceFlagsAdd) {
112 ALOGD("Discover found new serviceName %s, regType %s and domain %s for %d",
113 serviceName, regType, replyDomain, refNumber);
115 respCode = ResponseCode::ServiceDiscoveryServiceAdded;
118 ALOGD("Discover lost serviceName %s, regType %s and domain %s for %d",
119 serviceName, regType, replyDomain, refNumber);
121 respCode = ResponseCode::ServiceDiscoveryServiceRemoved;
123 asprintf(&msg, "%d %s %s %s", refNumber, quotedServiceName, regType, replyDomain);
124 free(quotedServiceName);
125 context->mListener->sendBroadcast(respCode, msg, false);
130 void MDnsSdListener::Handler::stop(SocketClient *cli, int argc, char **argv, const char *str) {
133 asprintf(&msg, "Invalid number of arguments to %s", str);
134 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
138 int requestId = atoi(argv[2]);
139 DNSServiceRef *ref = mMonitor->lookupServiceRef(requestId);
141 if (DBG) ALOGE("%s stop used unknown requestId %d", str, requestId);
142 cli->sendMsg(ResponseCode::CommandParameterError, "Unknown requestId", false);
145 if (VDBG) ALOGD("Stopping %s with ref %p", str, ref);
146 DNSServiceRefDeallocate(*ref);
147 mMonitor->freeServiceRef(requestId);
149 asprintf(&msg, "%s stopped", str);
150 cli->sendMsg(ResponseCode::CommandOkay, msg, false);
154 void MDnsSdListener::Handler::serviceRegister(SocketClient *cli, int requestId,
155 const char *interfaceName, const char *serviceName, const char *serviceType,
156 const char *domain, const char *host, int port, int txtLen, void *txtRecord) {
158 ALOGD("serviceRegister(%d, %s, %s, %s, %s, %s, %d, %d, <binary>)", requestId,
159 interfaceName, serviceName, serviceType, domain, host, port, txtLen);
161 Context *context = new Context(requestId, mListener);
162 DNSServiceRef *ref = mMonitor->allocateServiceRef(requestId, context);
165 ALOGE("requestId %d already in use during register call", requestId);
166 cli->sendMsg(ResponseCode::CommandParameterError,
167 "RequestId already in use during register call", false);
170 DNSServiceFlags nativeFlags = 0;
171 int interfaceInt = ifaceNameToI(interfaceName);
172 DNSServiceErrorType result = DNSServiceRegister(ref, interfaceInt, nativeFlags, serviceName,
173 serviceType, domain, host, port, txtLen, txtRecord, &MDnsSdListenerRegisterCallback,
175 if (result != kDNSServiceErr_NoError) {
176 ALOGE("service register request %d got an error from DNSServiceRegister %d", requestId,
178 mMonitor->freeServiceRef(requestId);
179 cli->sendMsg(ResponseCode::CommandParameterError,
180 "serviceRegister request got an error from DNSServiceRegister", false);
183 mMonitor->startMonitoring(requestId);
184 if (VDBG) ALOGD("serviceRegister successful");
185 cli->sendMsg(ResponseCode::CommandOkay, "serviceRegister started", false);
189 void MDnsSdListenerRegisterCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
190 DNSServiceErrorType errorCode, const char *serviceName, const char *regType,
191 const char *domain, void *inContext) {
192 MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
194 int refNumber = context->mRefNumber;
195 if (errorCode != kDNSServiceErr_NoError) {
196 asprintf(&msg, "%d %d", refNumber, errorCode);
197 context->mListener->sendBroadcast(ResponseCode::ServiceRegistrationFailed, msg, false);
198 if (DBG) ALOGE("register failure for %d, error= %d", refNumber, errorCode);
200 char *quotedServiceName = SocketClient::quoteArg(serviceName);
201 asprintf(&msg, "%d %s", refNumber, quotedServiceName);
202 free(quotedServiceName);
203 context->mListener->sendBroadcast(ResponseCode::ServiceRegistrationSucceeded, msg, false);
204 if (VDBG) ALOGD("register succeeded for %d as %s", refNumber, serviceName);
210 void MDnsSdListener::Handler::resolveService(SocketClient *cli, int requestId,
211 const char *interfaceName, const char *serviceName, const char *regType,
212 const char *domain) {
214 ALOGD("resolveService(%d, %s, %s, %s, %s)", requestId, interfaceName,
215 serviceName, regType, domain);
217 Context *context = new Context(requestId, mListener);
218 DNSServiceRef *ref = mMonitor->allocateServiceRef(requestId, context);
220 ALOGE("request Id %d already in use during resolve call", requestId);
221 cli->sendMsg(ResponseCode::CommandParameterError,
222 "RequestId already in use during resolve call", false);
225 DNSServiceFlags nativeFlags = 0;
226 int interfaceInt = ifaceNameToI(interfaceName);
227 DNSServiceErrorType result = DNSServiceResolve(ref, nativeFlags, interfaceInt, serviceName,
228 regType, domain, &MDnsSdListenerResolveCallback, context);
229 if (result != kDNSServiceErr_NoError) {
230 ALOGE("service resolve request %d got an error from DNSServiceResolve %d", requestId,
232 mMonitor->freeServiceRef(requestId);
233 cli->sendMsg(ResponseCode::CommandParameterError,
234 "resolveService got an error from DNSServiceResolve", false);
237 mMonitor->startMonitoring(requestId);
238 if (VDBG) ALOGD("resolveService successful");
239 cli->sendMsg(ResponseCode::CommandOkay, "resolveService started", false);
243 void MDnsSdListenerResolveCallback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interface,
244 DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t port,
245 uint16_t txtLen, const unsigned char *txtRecord, void *inContext) {
246 MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
248 int refNumber = context->mRefNumber;
250 if (errorCode != kDNSServiceErr_NoError) {
251 asprintf(&msg, "%d %d", refNumber, errorCode);
252 context->mListener->sendBroadcast(ResponseCode::ServiceResolveFailed, msg, false);
253 if (DBG) ALOGE("resolve failure for %d, error= %d", refNumber, errorCode);
255 char *quotedFullName = SocketClient::quoteArg(fullname);
256 char *quotedHostTarget = SocketClient::quoteArg(hosttarget);
257 asprintf(&msg, "%d %s %s %d %d", refNumber, quotedFullName, quotedHostTarget, port, txtLen);
258 free(quotedFullName);
259 free(quotedHostTarget);
260 context->mListener->sendBroadcast(ResponseCode::ServiceResolveSuccess, msg, false);
262 ALOGD("resolve succeeded for %d finding %s at %s:%d with txtLen %d",
263 refNumber, fullname, hosttarget, port, txtLen);
269 void MDnsSdListener::Handler::getAddrInfo(SocketClient *cli, int requestId,
270 const char *interfaceName, uint32_t protocol, const char *hostname) {
271 if (VDBG) ALOGD("getAddrInfo(%d, %s %d, %s)", requestId, interfaceName, protocol, hostname);
272 Context *context = new Context(requestId, mListener);
273 DNSServiceRef *ref = mMonitor->allocateServiceRef(requestId, context);
275 ALOGE("request ID %d already in use during getAddrInfo call", requestId);
276 cli->sendMsg(ResponseCode::CommandParameterError,
277 "RequestId already in use during getAddrInfo call", false);
280 DNSServiceFlags nativeFlags = 0;
281 int interfaceInt = ifaceNameToI(interfaceName);
282 DNSServiceErrorType result = DNSServiceGetAddrInfo(ref, nativeFlags, interfaceInt, protocol,
283 hostname, &MDnsSdListenerGetAddrInfoCallback, context);
284 if (result != kDNSServiceErr_NoError) {
285 ALOGE("getAddrInfo request %d got an error from DNSServiceGetAddrInfo %d", requestId,
287 mMonitor->freeServiceRef(requestId);
288 cli->sendMsg(ResponseCode::CommandParameterError,
289 "getAddrInfo request got an error from DNSServiceGetAddrInfo", false);
292 mMonitor->startMonitoring(requestId);
293 if (VDBG) ALOGD("getAddrInfo successful");
294 cli->sendMsg(ResponseCode::CommandOkay, "getAddrInfo started", false);
298 void MDnsSdListenerGetAddrInfoCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
299 uint32_t interface, DNSServiceErrorType errorCode, const char *hostname,
300 const struct sockaddr *const sa, uint32_t ttl, void *inContext) {
301 MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
302 int refNumber = context->mRefNumber;
304 if (errorCode != kDNSServiceErr_NoError) {
306 asprintf(&msg, "%d %d", refNumber, errorCode);
307 context->mListener->sendBroadcast(ResponseCode::ServiceGetAddrInfoFailed, msg, false);
308 if (DBG) ALOGE("getAddrInfo failure for %d, error= %d", refNumber, errorCode);
311 char addr[INET6_ADDRSTRLEN];
313 char *quotedHostname = SocketClient::quoteArg(hostname);
314 if (sa->sa_family == AF_INET) {
315 inet_ntop(sa->sa_family, &(((struct sockaddr_in *)sa)->sin_addr), addr, sizeof(addr));
317 inet_ntop(sa->sa_family, &(((struct sockaddr_in6 *)sa)->sin6_addr), addr, sizeof(addr));
319 asprintf(&msg, "%d %s %d %s", refNumber, quotedHostname, ttl, addr);
320 free(quotedHostname);
321 context->mListener->sendBroadcast(ResponseCode::ServiceGetAddrInfoSuccess, msg, false);
323 ALOGD("getAddrInfo succeeded for %d: %s", refNumber, msg);
329 void MDnsSdListener::Handler::setHostname(SocketClient *cli, int requestId,
330 const char *hostname) {
331 if (VDBG) ALOGD("setHostname(%d, %s)", requestId, hostname);
332 Context *context = new Context(requestId, mListener);
333 DNSServiceRef *ref = mMonitor->allocateServiceRef(requestId, context);
335 ALOGE("request Id %d already in use during setHostname call", requestId);
336 cli->sendMsg(ResponseCode::CommandParameterError,
337 "RequestId already in use during setHostname call", false);
340 DNSServiceFlags nativeFlags = 0;
341 DNSServiceErrorType result = DNSSetHostname(ref, nativeFlags, hostname,
342 &MDnsSdListenerSetHostnameCallback, context);
343 if (result != kDNSServiceErr_NoError) {
344 ALOGE("setHostname request %d got an error from DNSSetHostname %d", requestId, result);
345 mMonitor->freeServiceRef(requestId);
346 cli->sendMsg(ResponseCode::CommandParameterError,
347 "setHostname got an error from DNSSetHostname", false);
350 mMonitor->startMonitoring(requestId);
351 if (VDBG) ALOGD("setHostname successful");
352 cli->sendMsg(ResponseCode::CommandOkay, "setHostname started", false);
356 void MDnsSdListenerSetHostnameCallback(DNSServiceRef sdRef, DNSServiceFlags flags,
357 DNSServiceErrorType errorCode, const char *hostname, void *inContext) {
358 MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
360 int refNumber = context->mRefNumber;
361 if (errorCode != kDNSServiceErr_NoError) {
362 asprintf(&msg, "%d %d", refNumber, errorCode);
363 context->mListener->sendBroadcast(ResponseCode::ServiceSetHostnameFailed, msg, false);
364 if (DBG) ALOGE("setHostname failure for %d, error= %d", refNumber, errorCode);
366 char *quotedHostname = SocketClient::quoteArg(hostname);
367 asprintf(&msg, "%d %s", refNumber, quotedHostname);
368 free(quotedHostname);
369 context->mListener->sendBroadcast(ResponseCode::ServiceSetHostnameSuccess, msg, false);
370 if (VDBG) ALOGD("setHostname succeeded for %d. Set to %s", refNumber, hostname);
376 int MDnsSdListener::Handler::ifaceNameToI(const char *iface) {
380 const char *MDnsSdListener::Handler::iToIfaceName(int i) {
384 DNSServiceFlags MDnsSdListener::Handler::iToFlags(int i) {
388 int MDnsSdListener::Handler::flagsToI(DNSServiceFlags flags) {
392 int MDnsSdListener::Handler::runCommand(SocketClient *cli,
393 int argc, char **argv) {
396 asprintf( &msg, "Invalid number of arguments to mdnssd: %i", argc);
398 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
405 if (strcmp(cmd, "discover") == 0) {
407 cli->sendMsg(ResponseCode::CommandParameterError,
408 "Invalid number of arguments to mdnssd discover", false);
411 int requestId = atoi(argv[2]);
412 char *serviceType = argv[3];
414 discover(cli, NULL, serviceType, NULL, requestId, 0);
415 } else if (strcmp(cmd, "stop-discover") == 0) {
416 stop(cli, argc, argv, "discover");
417 } else if (strcmp(cmd, "register") == 0) {
419 cli->sendMsg(ResponseCode::CommandParameterError,
420 "Invalid number of arguments to mdnssd register", false);
423 int requestId = atoi(argv[2]);
424 char *serviceName = argv[3];
425 char *serviceType = argv[4];
426 int port = atoi(argv[5]);
427 char *interfaceName = NULL; // will use all
428 char *domain = NULL; // will use default
429 char *host = NULL; // will use default hostname
431 void *textRecord = NULL;
433 serviceRegister(cli, requestId, interfaceName, serviceName,
434 serviceType, domain, host, port, textLen, textRecord);
435 } else if (strcmp(cmd, "stop-register") == 0) {
436 stop(cli, argc, argv, "register");
437 } else if (strcmp(cmd, "resolve") == 0) {
439 cli->sendMsg(ResponseCode::CommandParameterError,
440 "Invalid number of arguments to mdnssd resolve", false);
443 int requestId = atoi(argv[2]);
444 char *interfaceName = NULL; // will use all
445 char *serviceName = argv[3];
446 char *regType = argv[4];
447 char *domain = argv[5];
448 resolveService(cli, requestId, interfaceName, serviceName, regType, domain);
449 } else if (strcmp(cmd, "stop-resolve") == 0) {
450 stop(cli, argc, argv, "resolve");
451 } else if (strcmp(cmd, "start-service") == 0) {
452 if (mMonitor->startService()) {
453 cli->sendMsg(ResponseCode::CommandOkay, "Service Started", false);
455 cli->sendMsg(ResponseCode::ServiceStartFailed, "Service already running", false);
457 } else if (strcmp(cmd, "stop-service") == 0) {
458 if (mMonitor->stopService()) {
459 cli->sendMsg(ResponseCode::CommandOkay, "Service Stopped", false);
461 cli->sendMsg(ResponseCode::ServiceStopFailed, "Service still in use", false);
463 } else if (strcmp(cmd, "sethostname") == 0) {
465 cli->sendMsg(ResponseCode::CommandParameterError,
466 "Invalid number of arguments to mdnssd sethostname", false);
469 int requestId = atoi(argv[2]);
470 char *hostname = argv[3];
471 setHostname(cli, requestId, hostname);
472 } else if (strcmp(cmd, "stop-sethostname") == 0) {
473 stop(cli, argc, argv, "sethostname");
474 } else if (strcmp(cmd, "getaddrinfo") == 0) {
476 cli->sendMsg(ResponseCode::CommandParameterError,
477 "Invalid number of arguments to mdnssd getaddrinfo", false);
480 int requestId = atoi(argv[2]);
481 char *hostname = argv[3];
482 char *interfaceName = NULL; // default
483 int protocol = 0; // intelligient heuristic (both v4 + v6)
484 getAddrInfo(cli, requestId, interfaceName, protocol, hostname);
485 } else if (strcmp(cmd, "stop-getaddrinfo") == 0) {
486 stop(cli, argc, argv, "getaddrinfo");
488 if (VDBG) ALOGE("Unknown cmd %s", cmd);
489 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown mdnssd cmd", false);
495 MDnsSdListener::Monitor::Monitor() {
497 pthread_mutex_init(&mHeadMutex, NULL);
498 socketpair(AF_LOCAL, SOCK_STREAM, 0, mCtrlSocketPair);
499 pthread_create(&mThread, NULL, MDnsSdListener::Monitor::threadStart, this);
500 pthread_detach(mThread);
503 void *MDnsSdListener::Monitor::threadStart(void *obj) {
504 Monitor *monitor = reinterpret_cast<Monitor *>(obj);
512 int MDnsSdListener::Monitor::startService() {
514 char property_value[PROPERTY_VALUE_MAX];
515 pthread_mutex_lock(&mHeadMutex);
516 property_get(MDNS_SERVICE_STATUS, property_value, "");
517 if (strcmp("running", property_value) != 0) {
518 ALOGD("Starting MDNSD");
519 property_set("ctl.start", MDNS_SERVICE_NAME);
520 wait_for_property(MDNS_SERVICE_STATUS, "running", 5);
525 pthread_mutex_unlock(&mHeadMutex);
529 int MDnsSdListener::Monitor::stopService() {
531 pthread_mutex_lock(&mHeadMutex);
533 ALOGD("Stopping MDNSD");
534 property_set("ctl.stop", MDNS_SERVICE_NAME);
535 wait_for_property(MDNS_SERVICE_STATUS, "stopped", 5);
540 pthread_mutex_unlock(&mHeadMutex);
544 void MDnsSdListener::Monitor::run() {
548 mPollFds = (struct pollfd *)calloc(sizeof(struct pollfd), mPollSize);
549 mPollRefs = (DNSServiceRef **)calloc(sizeof(DNSServiceRef *), mPollSize);
551 mPollFds[0].fd = mCtrlSocketPair[0];
552 mPollFds[0].events = POLLIN;
554 if (VDBG) ALOGD("MDnsSdListener starting to monitor");
556 if (VDBG) ALOGD("Going to poll with pollCount %d", pollCount);
557 int pollResults = poll(mPollFds, pollCount, 10000000);
558 if (pollResults < 0) {
559 ALOGE("Error in poll - got %d", errno);
560 } else if (pollResults > 0) {
561 if (VDBG) ALOGD("Monitor poll got data pollCount = %d, %d", pollCount, pollResults);
562 for(int i = 1; i < pollCount; i++) {
563 if (mPollFds[i].revents != 0) {
565 ALOGD("Monitor found [%d].revents = %d - calling ProcessResults",
566 i, mPollFds[i].revents);
568 DNSServiceProcessResult(*(mPollRefs[i]));
569 mPollFds[i].revents = 0;
572 if (VDBG) ALOGD("controlSocket shows revent= %d", mPollFds[0].revents);
573 switch (mPollFds[0].revents) {
576 read(mCtrlSocketPair[0], &readBuf, 1);
577 if (DBG) ALOGD("MDnsSdListener::Monitor got %c", readBuf[0]);
578 if (memcmp(RESCAN, readBuf, 1) == 0) {
579 pollCount = rescan();
583 mPollFds[0].revents = 0;
585 if (VDBG) ALOGD("MDnsSdListener::Monitor poll timed out");
594 int MDnsSdListener::Monitor::rescan() {
595 // rescan the list from mHead and make new pollfds and serviceRefs
597 ALOGD("MDnsSdListener::Monitor poll rescanning - size=%d, live=%d", mPollSize, mLiveCount);
600 pthread_mutex_lock(&mHeadMutex);
601 Element **prevPtr = &mHead;
603 if (mPollSize <= mLiveCount) {
604 mPollSize = mLiveCount + 5;
607 mPollFds = (struct pollfd *)calloc(sizeof(struct pollfd), mPollSize);
608 mPollRefs = (DNSServiceRef **)calloc(sizeof(DNSServiceRef *), mPollSize);
610 memset(mPollFds, 0, sizeof(struct pollfd) * mPollSize);
611 memset(mPollRefs, 0, sizeof(DNSServiceRef *) * mPollSize);
613 mPollFds[0].fd = mCtrlSocketPair[0];
614 mPollFds[0].events = POLLIN;
615 if (DBG_RESCAN) ALOGD("mHead = %p", mHead);
616 while (*prevPtr != NULL) {
617 if (DBG_RESCAN) ALOGD("checking %p, mReady = %d", *prevPtr, (*prevPtr)->mReady);
618 if ((*prevPtr)->mReady == 1) {
619 int fd = DNSServiceRefSockFD((*prevPtr)->mRef);
621 if (DBG_RESCAN) ALOGD(" adding FD %d", fd);
623 mPollFds[i].events = POLLIN;
624 mPollRefs[i] = &((*prevPtr)->mRef);
627 ALOGE("Error retreving socket FD for live ServiceRef");
629 prevPtr = &((*prevPtr)->mNext); // advance to the next element
630 } else if ((*prevPtr)->mReady == -1) {
631 if (DBG_RESCAN) ALOGD(" removing %p from play", *prevPtr);
632 Element *cur = *prevPtr;
633 *prevPtr = (cur)->mNext; // change our notion of this element and don't advance
637 pthread_mutex_unlock(&mHeadMutex);
641 DNSServiceRef *MDnsSdListener::Monitor::allocateServiceRef(int id, Context *context) {
642 if (lookupServiceRef(id) != NULL) {
646 Element *e = new Element(id, context);
647 pthread_mutex_lock(&mHeadMutex);
650 pthread_mutex_unlock(&mHeadMutex);
654 DNSServiceRef *MDnsSdListener::Monitor::lookupServiceRef(int id) {
655 pthread_mutex_lock(&mHeadMutex);
656 Element *cur = mHead;
657 while (cur != NULL) {
658 if (cur->mId == id) {
659 DNSServiceRef *result = &(cur->mRef);
660 pthread_mutex_unlock(&mHeadMutex);
665 pthread_mutex_unlock(&mHeadMutex);
669 void MDnsSdListener::Monitor::startMonitoring(int id) {
670 if (VDBG) ALOGD("startMonitoring %d", id);
671 pthread_mutex_lock(&mHeadMutex);
672 Element *cur = mHead;
673 while (cur != NULL) {
674 if (cur->mId == id) {
675 if (DBG_RESCAN) ALOGD("marking %p as ready to be added", cur);
678 pthread_mutex_unlock(&mHeadMutex);
679 write(mCtrlSocketPair[1], RESCAN, 1); // trigger a rescan for a fresh poll
680 if (VDBG) ALOGD("triggering rescan");
685 pthread_mutex_unlock(&mHeadMutex);
688 #define NAP_TIME 200 // 200 ms between polls
689 static int wait_for_property(const char *name, const char *desired_value, int maxwait)
691 char value[PROPERTY_VALUE_MAX] = {'\0'};
692 int maxnaps = (maxwait * 1000) / NAP_TIME;
698 while (maxnaps-- > 0) {
699 usleep(NAP_TIME * 1000);
700 if (property_get(name, value, NULL)) {
701 if (desired_value == NULL || strcmp(value, desired_value) == 0) {
706 return -1; /* failure */
709 void MDnsSdListener::Monitor::freeServiceRef(int id) {
710 if (VDBG) ALOGD("freeServiceRef %d", id);
711 pthread_mutex_lock(&mHeadMutex);
712 Element **prevPtr = &mHead;
714 while (*prevPtr != NULL) {
716 if (cur->mId == id) {
717 if (DBG_RESCAN) ALOGD("marking %p as ready to be removed", cur);
719 if (cur->mReady == 1) {
720 cur->mReady = -1; // tell poll thread to delete
721 write(mCtrlSocketPair[1], RESCAN, 1); // trigger a rescan for a fresh poll
722 if (VDBG) ALOGD("triggering rescan");
724 *prevPtr = cur->mNext;
727 pthread_mutex_unlock(&mHeadMutex);
730 prevPtr = &(cur->mNext);
732 pthread_mutex_unlock(&mHeadMutex);