summaryrefslogtreecommitdiff
path: root/server/MDnsService.cpp
blob: 1c1dfca79395177d67bbb3e7dcc9ba7f4fca42c9 (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
/**
 * Copyright (c) 2022, 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.
 */

#define LOG_TAG "MDnsService"

#include "MDnsService.h"

#include "binder_utils/BinderUtil.h"
#include "binder_utils/NetdPermissions.h"

#include <android-base/strings.h>
#include <binder/Status.h>

#include <string>
#include <vector>

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;

namespace android::net {

// TODO: DnsResolver has same macro definition but returns ScopedAStatus. Move these macros to
// BinderUtil.h to do the same permission check.
#define ENFORCE_ANY_PERMISSION(...)                                \
    do {                                                           \
        binder::Status status = checkAnyPermission({__VA_ARGS__}); \
        if (!status.isOk()) {                                      \
            return status;                                         \
        }                                                          \
    } while (0)

#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
    ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)

status_t MDnsService::start() {
    IPCThreadState::self()->disableBackgroundScheduling(true);
    const status_t ret = BinderService<MDnsService>::publish();
    if (ret != android::OK) {
        return ret;
    }
    return android::OK;
}

binder::Status MDnsService::startDaemon() {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.startDaemon();
    return statusFromErrcode(res);
}

binder::Status MDnsService::stopDaemon() {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.stopDaemon();
    return statusFromErrcode(res);
}

binder::Status MDnsService::registerService(const RegistrationInfo& info) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.serviceRegister(
            info.id, info.serviceName.c_str(), info.registrationType.c_str(), nullptr /* domain */,
            nullptr /* host */, info.port, info.txtRecord, info.interfaceIdx);
    return statusFromErrcode(res);
}

binder::Status MDnsService::discover(const DiscoveryInfo& info) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.discover(info.interfaceIdx, info.registrationType.c_str(),
                                 nullptr /* domain */, info.id, 0 /* requestFlags */);
    return statusFromErrcode(res);
}

binder::Status MDnsService::resolve(const ResolutionInfo& info) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.resolveService(info.id, info.interfaceIdx, info.serviceName.c_str(),
                                       info.registrationType.c_str(), info.domain.c_str());
    return statusFromErrcode(res);
}

binder::Status MDnsService::getServiceAddress(const GetAddressInfo& info) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.getAddrInfo(info.id, info.interfaceIdx, 0 /* protocol */,
                                    info.hostname.c_str());
    return statusFromErrcode(res);
}

binder::Status MDnsService::stopOperation(int32_t id) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = mListener.stop(id);
    return statusFromErrcode(res);
}

binder::Status MDnsService::registerEventListener(const android::sp<IMDnsEventListener>& listener) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = MDnsEventReporter::getInstance().addEventListener(listener);
    return statusFromErrcode(res);
}

binder::Status MDnsService::unregisterEventListener(
        const android::sp<IMDnsEventListener>& listener) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    int res = MDnsEventReporter::getInstance().removeEventListener(listener);
    return statusFromErrcode(res);
}

}  // namespace android::net