summaryrefslogtreecommitdiff
path: root/server/MDnsSdListener.cpp
blob: d2422f6be2b43382e8c2438a43610f346096697b (plain)
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
/*
 * 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.
 */

#include "MDnsSdListener.h"

#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <linux/if.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <resolv.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>

#define LOG_TAG "MDnsDS"
#define DBG 1
#define VDBG 1

#include <cutils/properties.h>
#include <log/log.h>
#include <netdutils/ThreadUtil.h>
#include <sysutils/SocketClient.h>

#include "Controllers.h"
#include "MDnsEventReporter.h"
#include "netid_client.h"

using android::net::gCtls;

#define MDNS_SERVICE_NAME "mdnsd"
#define MDNS_SERVICE_STATUS "init.svc.mdnsd"

#define CEIL(x, y) (((x) + (y) - 1) / (y))

constexpr char RESCAN[] = "1";

using android::net::mdns::aidl::DiscoveryInfo;
using android::net::mdns::aidl::GetAddressInfo;
using android::net::mdns::aidl::IMDnsEventListener;
using android::net::mdns::aidl::RegistrationInfo;
using android::net::mdns::aidl::ResolutionInfo;

static unsigned ifaceIndexToNetId(uint32_t interfaceIndex) {
    char interfaceName[IFNAMSIZ] = {};
    unsigned netId;
    if (if_indextoname(interfaceIndex, interfaceName) == nullptr) {
        ALOGE("Interface %d was not found", interfaceIndex);
        return NETID_UNSET;
    } else if ((netId = gCtls->netCtrl.getNetworkForInterface(interfaceName)) == NETID_UNSET) {
        ALOGE("Network was not found for interface %s", interfaceName);
        return NETID_UNSET;
    }
    return netId;
}

int MDnsSdListener::discover(uint32_t ifIndex, const char* regType, const char* domain,
                             const int requestId, const int requestFlags) {
    if (VDBG) {
        ALOGD("discover(%d, %s, %s, %d, %d)", ifIndex, regType, domain ? domain : "null", requestId,
              requestFlags);
    }
    Context* context = new Context(requestId);
    DNSServiceRef* ref = mMonitor.allocateServiceRef(requestId, context);
    if (ref == nullptr) {
        ALOGE("requestId %d already in use during discover call", requestId);
        return -EBUSY;
    }
    if (VDBG) ALOGD("using ref %p", ref);

    DNSServiceErrorType result = DNSServiceBrowse(ref, requestFlags, ifIndex, regType, domain,
                                                  &MDnsSdListenerDiscoverCallback, context);
    if (result != kDNSServiceErr_NoError) {
        ALOGE("Discover request %d got an error from DNSServiceBrowse %d", requestId, result);
        mMonitor.freeServiceRef(requestId);
        // Return kDNSServiceErr_* directly instead of transferring to an UNIX error.
        // This can help caller to know what going wrong from mdnsresponder side.
        return -result;
    }
    mMonitor.startMonitoring(requestId);
    if (VDBG) ALOGD("discover successful");
    return 0;
}

void MDnsSdListenerDiscoverCallback(DNSServiceRef /* sdRef */, DNSServiceFlags flags,
                                    uint32_t ifIndex, DNSServiceErrorType errorCode,
                                    const char* serviceName, const char* regType,
                                    const char* replyDomain, void* inContext) {
    MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
    int refNumber = context->mRefNumber;
    const std::lock_guard lock(MDnsEventReporter::getInstance().mMutex);
    const auto& listeners = MDnsEventReporter::getInstance().getEventListeners();
    if (listeners.empty()) {
        ALOGI("Discover callback not sent since no IMDnsEventListener receiver is available.");
        return;
    }

    DiscoveryInfo info;
    info.id = refNumber;
    info.serviceName = serviceName;
    info.registrationType = regType;
    info.interfaceIdx = ifIndex;
    // If the network is not found, still send the event and let
    // the service decide what to do with a callback with an empty network
    info.netId = ifaceIndexToNetId(ifIndex);
    if (errorCode == kDNSServiceErr_NoError) {
        if (flags & kDNSServiceFlagsAdd) {
            if (VDBG) {
                ALOGD("Discover found new serviceName %s, regType %s and domain %s for %d",
                      serviceName, regType, replyDomain, refNumber);
            }
            info.result = IMDnsEventListener::SERVICE_FOUND;
        } else {
            if (VDBG) {
                ALOGD("Discover lost serviceName %s, regType %s and domain %s for %d", serviceName,
                      regType, replyDomain, refNumber);
            }
            info.result = IMDnsEventListener::SERVICE_LOST;
        }
    } else {
        if (DBG) ALOGE("discover failure for %d, error= %d", refNumber, errorCode);
        info.result = IMDnsEventListener::SERVICE_DISCOVERY_FAILED;
    }

    for (const auto& it : listeners) {
        it->getListener()->onServiceDiscoveryStatus(info);
    }
}

int MDnsSdListener::stop(int requestId) {
    DNSServiceRef* ref = mMonitor.lookupServiceRef(requestId);
    if (ref == nullptr) {
        if (DBG) ALOGE("Stop used unknown requestId %d", requestId);
        return -ESRCH;
    }
    if (VDBG) ALOGD("Stopping operation with ref %p", ref);
    mMonitor.deallocateServiceRef(ref);
    mMonitor.freeServiceRef(requestId);
    return 0;
}

int MDnsSdListener::serviceRegister(int requestId, const char* serviceName, const char* serviceType,
                                    const char* domain, const char* host, int port,
                                    const std::vector<unsigned char>& txtRecord, uint32_t ifIndex) {
    if (VDBG) {
        ALOGD("serviceRegister(%d, %d, %s, %s, %s, %s, %d, <binary>)", requestId, ifIndex,
              serviceName, serviceType, domain ? domain : "null", host ? host : "null", port);
    }
    Context* context = new Context(requestId);
    DNSServiceRef* ref = mMonitor.allocateServiceRef(requestId, context);
    if (ref == nullptr) {
        ALOGE("requestId %d already in use during register call", requestId);
        return -EBUSY;
    }
    port = htons(port);
    DNSServiceFlags nativeFlags = 0;
    DNSServiceErrorType result = DNSServiceRegister(
            ref, nativeFlags, ifIndex, serviceName, serviceType, domain, host, port,
            txtRecord.size(), &txtRecord.front(), &MDnsSdListenerRegisterCallback, context);
    if (result != kDNSServiceErr_NoError) {
        ALOGE("service register request %d got an error from DNSServiceRegister %d", requestId,
                result);
        mMonitor.freeServiceRef(requestId);
        // Return kDNSServiceErr_* directly instead of transferring to an UNIX error.
        // This can help caller to know what going wrong from mdnsresponder side.
        return -result;
    }
    mMonitor.startMonitoring(requestId);
    if (VDBG) ALOGD("serviceRegister successful");
    return 0;
}

void MDnsSdListenerRegisterCallback(DNSServiceRef /* sdRef */, DNSServiceFlags /* flags */,
                                    DNSServiceErrorType errorCode, const char* serviceName,
                                    const char* regType, const char* /* domain */,
                                    void* inContext) {
    MDnsSdListener::Context* context = reinterpret_cast<MDnsSdListener::Context*>(inContext);
    int refNumber = context->mRefNumber;
    const std::lock_guard lock(MDnsEventReporter::getInstance().mMutex);
    const auto& listeners = MDnsEventReporter::getInstance().getEventListeners();
    if (listeners.empty()) {
        ALOGI("Register callback not sent since no IMDnsEventListener receiver is available.");
        return;
    }

    RegistrationInfo info;
    info.id = refNumber;
    info.serviceName = serviceName;
    info.registrationType = regType;
    if (errorCode == kDNSServiceErr_NoError) {
        if (VDBG) ALOGD("register succeeded for %d as %s", refNumber, serviceName);
        info.result = IMDnsEventListener::SERVICE_REGISTERED;
    } else {
        if (DBG) ALOGE("register failure for %d, error= %d", refNumber, errorCode);
        info.result = IMDnsEventListener::SERVICE_REGISTRATION_FAILED;
    }

    for (const auto& it : listeners) {
        it->getListener()->onServiceRegistrationStatus(info);
    }
}

int MDnsSdListener::resolveService(int requestId, uint32_t ifIndex, const char* serviceName,
                                   const char* regType, const char* domain) {
    if (VDBG) {
        ALOGD("resolveService(%d, %d, %s, %s, %s)", requestId, ifIndex, serviceName, regType,
              domain);
    }
    Context* context = new Context(requestId);
    DNSServiceRef* ref = mMonitor.allocateServiceRef(requestId, context);
    if (ref == nullptr) {
        ALOGE("request Id %d already in use during resolve call", requestId);
        return -EBUSY;
    }
    DNSServiceFlags nativeFlags = 0;
    DNSServiceErrorType result = DNSServiceResolve(ref, nativeFlags, ifIndex, serviceName, regType,
                                                   domain, &MDnsSdListenerResolveCallback, context);
    if (result != kDNSServiceErr_NoError) {
        ALOGE("service resolve request %d on iface %d: got an error from DNSServiceResolve %d",
              requestId, ifIndex, result);
        mMonitor.freeServiceRef(requestId);
        // Return kDNSServiceErr_* directly instead of transferring to an UNIX error.
        // This can help caller to know what going wrong from mdnsresponder side.
        return -result;
    }
    mMonitor.startMonitoring(requestId);
    if (VDBG) ALOGD("resolveService successful");
    return 0;
}

void MDnsSdListenerResolveCallback(DNSServiceRef /* sdRef */, DNSServiceFlags /* flags */,
                                   uint32_t ifIndex, DNSServiceErrorType errorCode,
                                   const char* fullname, const char* hosttarget, uint16_t port,
                                   uint16_t txtLen, const unsigned char* txtRecord,
                                   void* inContext) {
    MDnsSdListener::Context* context = reinterpret_cast<MDnsSdListener::Context*>(inContext);
    int refNumber = context->mRefNumber;
    const std::lock_guard lock(MDnsEventReporter::getInstance().mMutex);
    const auto& listeners = MDnsEventReporter::getInstance().getEventListeners();
    if (listeners.empty()) {
        ALOGI("Resolve callback not sent since no IMDnsEventListener receiver is available.");
        return;
    }
    port = ntohs(port);

    ResolutionInfo info;
    info.id = refNumber;
    info.port = port;
    info.serviceFullName = fullname;
    info.hostname = hosttarget;
    info.txtRecord = std::vector<unsigned char>(txtRecord, txtRecord + txtLen);
    info.interfaceIdx = ifIndex;
    if (errorCode == kDNSServiceErr_NoError) {
        if (VDBG) {
            ALOGD("resolve succeeded for %d finding %s at %s:%d with txtLen %d", refNumber,
                  fullname, hosttarget, port, txtLen);
        }
        info.result = IMDnsEventListener::SERVICE_RESOLVED;
    } else {
        if (DBG) ALOGE("resolve failure for %d, error= %d", refNumber, errorCode);
        info.result = IMDnsEventListener::SERVICE_RESOLUTION_FAILED;
    }

    for (const auto& it : listeners) {
        it->getListener()->onServiceResolutionStatus(info);
    }
}

int MDnsSdListener::getAddrInfo(int requestId, uint32_t ifIndex, uint32_t protocol,
                                const char* hostname) {
    if (VDBG) ALOGD("getAddrInfo(%d, %u %d, %s)", requestId, ifIndex, protocol, hostname);
    Context* context = new Context(requestId);
    DNSServiceRef* ref = mMonitor.allocateServiceRef(requestId, context);
    if (ref == nullptr) {
        ALOGE("request ID %d already in use during getAddrInfo call", requestId);
        return -EBUSY;
    }
    DNSServiceFlags nativeFlags = 0;
    DNSServiceErrorType result =
            DNSServiceGetAddrInfo(ref, nativeFlags, ifIndex, protocol, hostname,
                                  &MDnsSdListenerGetAddrInfoCallback, context);
    if (result != kDNSServiceErr_NoError) {
        ALOGE("getAddrInfo request %d got an error from DNSServiceGetAddrInfo %d", requestId,
                result);
        mMonitor.freeServiceRef(requestId);
        // Return kDNSServiceErr_* directly instead of transferring to an UNIX error.
        // This can help caller to know what going wrong from mdnsresponder side.
        return -result;
    }
    mMonitor.startMonitoring(requestId);
    if (VDBG) ALOGD("getAddrInfo successful");
    return 0;
}

void MDnsSdListenerGetAddrInfoCallback(DNSServiceRef /* sdRef */, DNSServiceFlags /* flags */,
                                       uint32_t ifIndex, DNSServiceErrorType errorCode,
                                       const char* hostname, const struct sockaddr* const sa,
                                       uint32_t /* ttl */, void* inContext) {
    MDnsSdListener::Context *context = reinterpret_cast<MDnsSdListener::Context *>(inContext);
    int refNumber = context->mRefNumber;
    const std::lock_guard lock(MDnsEventReporter::getInstance().mMutex);
    const auto& listeners = MDnsEventReporter::getInstance().getEventListeners();
    if (listeners.empty()) {
        ALOGI("Get address callback not sent since no IMDnsEventListener receiver is available.");
        return;
    }

    GetAddressInfo info;
    info.id = refNumber;
    info.hostname = hostname;
    info.interfaceIdx = ifIndex;
    // If the network is not found, still send the event with an empty network
    // and let the service decide what to do with it
    info.netId = ifaceIndexToNetId(ifIndex);
    if (errorCode == kDNSServiceErr_NoError) {
        char addr[INET6_ADDRSTRLEN];
        if (sa->sa_family == AF_INET) {
            inet_ntop(sa->sa_family, &(((struct sockaddr_in *)sa)->sin_addr), addr, sizeof(addr));
        } else {
            inet_ntop(sa->sa_family, &(((struct sockaddr_in6 *)sa)->sin6_addr), addr, sizeof(addr));
        }
        info.address = addr;
        if (VDBG) {
            ALOGD("getAddrInfo succeeded for %d:", refNumber);
        }
        info.result = IMDnsEventListener::SERVICE_GET_ADDR_SUCCESS;
    } else {
        if (DBG) ALOGE("getAddrInfo failure for %d, error= %d", refNumber, errorCode);
        info.result = IMDnsEventListener::SERVICE_GET_ADDR_FAILED;
    }
    for (const auto& it : listeners) {
        it->getListener()->onGettingServiceAddressStatus(info);
    }
}

int MDnsSdListener::startDaemon() {
    if (!mMonitor.startService()) {
        ALOGE("Failed to start: Service already running");
        return -EBUSY;
    }
    return 0;
}

int MDnsSdListener::stopDaemon() {
    if (!mMonitor.stopService()) {
        ALOGE("Failed to stop: Service still in use");
        return -EBUSY;
    }
    return 0;
}

MDnsSdListener::Monitor::Monitor() {
    mHead = nullptr;
    mLiveCount = 0;
    mPollFds = nullptr;
    mPollRefs = nullptr;
    mPollSize = 10;
    socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, mCtrlSocketPair);

    const int rval = ::android::netdutils::threadLaunch(this);
    if (rval != 0) {
        ALOGW("Error spawning monitor thread: %s (%d)", strerror(-rval), -rval);
    }
}

#define NAP_TIME 200  // 200 ms between polls
static int wait_for_property(const char *name, const char *desired_value, int maxwait)
{
    char value[PROPERTY_VALUE_MAX] = {'\0'};
    int maxnaps = (maxwait * 1000) / NAP_TIME;

    if (maxnaps < 1) {
        maxnaps = 1;
    }

    while (maxnaps-- > 0) {
        usleep(NAP_TIME * 1000);
        if (property_get(name, value, nullptr)) {
            if (desired_value == nullptr || strcmp(value, desired_value) == 0) {
                return 0;
            }
        }
    }
    return -1; /* failure */
}

int MDnsSdListener::Monitor::startService() {
    char property_value[PROPERTY_VALUE_MAX];
    std::lock_guard guard(mMutex);
    property_get(MDNS_SERVICE_STATUS, property_value, "");
    if (strcmp("running", property_value) != 0) {
        ALOGD("Starting MDNSD");
        property_set("ctl.start", MDNS_SERVICE_NAME);
        wait_for_property(MDNS_SERVICE_STATUS, "running", 5);
        return -1;
    }
    return 0;
}

int MDnsSdListener::Monitor::stopService() {
    std::lock_guard guard(mMutex);
    if (mHead == nullptr) {
        ALOGD("Stopping MDNSD");
        property_set("ctl.stop", MDNS_SERVICE_NAME);
        wait_for_property(MDNS_SERVICE_STATUS, "stopped", 5);
        return -1;
    }
    return 0;
}

void MDnsSdListener::Monitor::run() {
    int pollCount = 1;

    mPollFds = (struct pollfd *)calloc(sizeof(struct pollfd), mPollSize);
    mPollRefs = (DNSServiceRef **)calloc(sizeof(DNSServiceRef *), mPollSize);
    LOG_ALWAYS_FATAL_IF((mPollFds == nullptr), "initial calloc failed on mPollFds with a size of %d",
            ((int)sizeof(struct pollfd)) * mPollSize);
    LOG_ALWAYS_FATAL_IF((mPollRefs == nullptr), "initial calloc failed on mPollRefs with a size of %d",
            ((int)sizeof(DNSServiceRef *)) * mPollSize);

    mPollFds[0].fd = mCtrlSocketPair[0];
    mPollFds[0].events = POLLIN;

    if (VDBG) ALOGD("MDnsSdListener starting to monitor");
    while (1) {
        if (VDBG) ALOGD("Going to poll with pollCount %d", pollCount);
        int pollResults = poll(mPollFds, pollCount, 10000000);
        if (VDBG) ALOGD("pollResults=%d", pollResults);
        if (pollResults < 0) {
            ALOGE("Error in poll - got %d", errno);
        } else if (pollResults > 0) {
            if (VDBG) ALOGD("Monitor poll got data pollCount = %d, %d", pollCount, pollResults);
            for(int i = 1; i < pollCount; i++) {
                if (mPollFds[i].revents != 0) {
                    if (VDBG) {
                        ALOGD("Monitor found [%d].revents = %d - calling ProcessResults",
                                i, mPollFds[i].revents);
                    }
                    std::lock_guard guard(mMutex);
                    DNSServiceProcessResult(*(mPollRefs[i]));
                    mPollFds[i].revents = 0;
                }
            }
            if (VDBG) ALOGD("controlSocket shows revent= %d", mPollFds[0].revents);
            switch (mPollFds[0].revents) {
                case POLLIN: {
                    char readBuf[2];
                    read(mCtrlSocketPair[0], &readBuf, 1);
                    if (DBG) ALOGD("MDnsSdListener::Monitor got %c", readBuf[0]);
                    if (memcmp(RESCAN, readBuf, 1) == 0) {
                        pollCount = rescan();
                    }
                }
            }
            mPollFds[0].revents = 0;
        } else {
            if (VDBG) ALOGD("MDnsSdListener::Monitor poll timed out");
        }
    }
    free(mPollFds);
    free(mPollRefs);
}

#define DBG_RESCAN 0

int MDnsSdListener::Monitor::rescan() {
// rescan the list from mHead and make new pollfds and serviceRefs
    if (VDBG) {
        ALOGD("MDnsSdListener::Monitor poll rescanning - size=%d, live=%d", mPollSize, mLiveCount);
    }
    std::lock_guard guard(mMutex);
    Element **prevPtr = &mHead;
    int i = 1;
    if (mPollSize <= mLiveCount) {
        mPollSize = mLiveCount + 5;
        free(mPollFds);
        free(mPollRefs);
        mPollFds = (struct pollfd *)calloc(sizeof(struct pollfd), mPollSize);
        mPollRefs = (DNSServiceRef **)calloc(sizeof(DNSServiceRef *), mPollSize);
        LOG_ALWAYS_FATAL_IF((mPollFds == nullptr), "calloc failed on mPollFds with a size of %d",
                ((int)sizeof(struct pollfd)) * mPollSize);
        LOG_ALWAYS_FATAL_IF((mPollRefs == nullptr), "calloc failed on mPollRefs with a size of %d",
                ((int)sizeof(DNSServiceRef *)) * mPollSize);
    } else {
        memset(mPollFds, 0, sizeof(struct pollfd) * mPollSize);
        memset(mPollRefs, 0, sizeof(DNSServiceRef *) * mPollSize);
    }
    mPollFds[0].fd = mCtrlSocketPair[0];
    mPollFds[0].events = POLLIN;
    if (DBG_RESCAN) ALOGD("mHead = %p", mHead);
    while (*prevPtr != nullptr) {
        if (DBG_RESCAN) ALOGD("checking %p, mReady = %d", *prevPtr, (*prevPtr)->mReady);
        if ((*prevPtr)->mReady == 1) {
            int fd = DNSServiceRefSockFD((*prevPtr)->mRef);
            if (fd != -1) {
                if (DBG_RESCAN) ALOGD("  adding FD %d", fd);
                mPollFds[i].fd = fd;
                mPollFds[i].events = POLLIN;
                mPollRefs[i] = &((*prevPtr)->mRef);
                i++;
            } else {
                ALOGE("Error retreving socket FD for live ServiceRef");
            }
            prevPtr = &((*prevPtr)->mNext); // advance to the next element
        } else if ((*prevPtr)->mReady == -1) {
            if (DBG_RESCAN) ALOGD("  removing %p from  play", *prevPtr);
            Element *cur = *prevPtr;
            *prevPtr = (cur)->mNext; // change our notion of this element and don't advance
            delete cur;
        } else if ((*prevPtr)->mReady == 0) {
            // Not ready so just skip this node and continue on
            if (DBG_RESCAN) ALOGD("%p not ready.  Continuing.", *prevPtr);
            prevPtr = &((*prevPtr)->mNext);
        }
    }

    return i;
}

DNSServiceRef *MDnsSdListener::Monitor::allocateServiceRef(int id, Context *context) {
    if (lookupServiceRef(id) != nullptr) {
        delete(context);
        return nullptr;
    }
    Element *e = new Element(id, context);
    std::lock_guard guard(mMutex);
    e->mNext = mHead;
    mHead = e;
    return &(e->mRef);
}

DNSServiceRef *MDnsSdListener::Monitor::lookupServiceRef(int id) {
    std::lock_guard guard(mMutex);
    Element *cur = mHead;
    while (cur != nullptr) {
        if (cur->mId == id) {
            DNSServiceRef *result = &(cur->mRef);
            return result;
        }
        cur = cur->mNext;
    }
    return nullptr;
}

void MDnsSdListener::Monitor::startMonitoring(int id) {
    if (VDBG) ALOGD("startMonitoring %d", id);
    std::lock_guard guard(mMutex);
    for (Element* cur = mHead; cur != nullptr; cur = cur->mNext) {
        if (cur->mId == id) {
            if (DBG_RESCAN) ALOGD("marking %p as ready to be added", cur);
            mLiveCount++;
            cur->mReady = 1;
            write(mCtrlSocketPair[1], RESCAN, 1);  // trigger a rescan for a fresh poll
            if (VDBG) ALOGD("triggering rescan");
            return;
        }
    }
}

void MDnsSdListener::Monitor::freeServiceRef(int id) {
    if (VDBG) ALOGD("freeServiceRef %d", id);
    std::lock_guard guard(mMutex);
    Element* cur;
    for (Element** prevPtr = &mHead; *prevPtr != nullptr; prevPtr = &(cur->mNext)) {
        cur = *prevPtr;
        if (cur->mId == id) {
            if (DBG_RESCAN) ALOGD("marking %p as ready to be removed", cur);
            mLiveCount--;
            if (cur->mReady == 1) {
                cur->mReady = -1; // tell poll thread to delete
                cur->mRef = nullptr; // do not process further results
                write(mCtrlSocketPair[1], RESCAN, 1); // trigger a rescan for a fresh poll
                if (VDBG) ALOGD("triggering rescan");
            } else {
                *prevPtr = cur->mNext;
                delete cur;
            }
            return;
        }
    }
}

void MDnsSdListener::Monitor::deallocateServiceRef(DNSServiceRef* ref) {
    std::lock_guard guard(mMutex);
    DNSServiceRefDeallocate(*ref);
    *ref = nullptr;
}