blob: 1c1dfca79395177d67bbb3e7dcc9ba7f4fca42c9 [file] [log] [blame]
paulhuc8a58ff2022-02-09 18:37:27 +08001/**
2 * Copyright (c) 2022, The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#define LOG_TAG "MDnsService"
18
19#include "MDnsService.h"
20
21#include "binder_utils/BinderUtil.h"
22#include "binder_utils/NetdPermissions.h"
23
24#include <android-base/strings.h>
25#include <binder/Status.h>
26
27#include <string>
28#include <vector>
29
30using android::net::mdns::aidl::DiscoveryInfo;
31using android::net::mdns::aidl::GetAddressInfo;
32using android::net::mdns::aidl::IMDnsEventListener;
33using android::net::mdns::aidl::RegistrationInfo;
34using android::net::mdns::aidl::ResolutionInfo;
35
36namespace android::net {
37
38// TODO: DnsResolver has same macro definition but returns ScopedAStatus. Move these macros to
39// BinderUtil.h to do the same permission check.
40#define ENFORCE_ANY_PERMISSION(...) \
41 do { \
42 binder::Status status = checkAnyPermission({__VA_ARGS__}); \
43 if (!status.isOk()) { \
44 return status; \
45 } \
46 } while (0)
47
48#define ENFORCE_NETWORK_STACK_PERMISSIONS() \
49 ENFORCE_ANY_PERMISSION(PERM_NETWORK_STACK, PERM_MAINLINE_NETWORK_STACK)
50
51status_t MDnsService::start() {
52 IPCThreadState::self()->disableBackgroundScheduling(true);
53 const status_t ret = BinderService<MDnsService>::publish();
54 if (ret != android::OK) {
55 return ret;
56 }
57 return android::OK;
58}
59
60binder::Status MDnsService::startDaemon() {
61 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080062 int res = mListener.startDaemon();
63 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080064}
65
66binder::Status MDnsService::stopDaemon() {
67 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080068 int res = mListener.stopDaemon();
69 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080070}
71
paulhu1ad658f2022-02-10 21:54:34 +080072binder::Status MDnsService::registerService(const RegistrationInfo& info) {
paulhuc8a58ff2022-02-09 18:37:27 +080073 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080074 int res = mListener.serviceRegister(
75 info.id, info.serviceName.c_str(), info.registrationType.c_str(), nullptr /* domain */,
76 nullptr /* host */, info.port, info.txtRecord, info.interfaceIdx);
77 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080078}
79
paulhu1ad658f2022-02-10 21:54:34 +080080binder::Status MDnsService::discover(const DiscoveryInfo& info) {
paulhuc8a58ff2022-02-09 18:37:27 +080081 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080082 int res = mListener.discover(info.interfaceIdx, info.registrationType.c_str(),
83 nullptr /* domain */, info.id, 0 /* requestFlags */);
84 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080085}
86
paulhu1ad658f2022-02-10 21:54:34 +080087binder::Status MDnsService::resolve(const ResolutionInfo& info) {
paulhuc8a58ff2022-02-09 18:37:27 +080088 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080089 int res = mListener.resolveService(info.id, info.interfaceIdx, info.serviceName.c_str(),
90 info.registrationType.c_str(), info.domain.c_str());
91 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080092}
93
paulhu1ad658f2022-02-10 21:54:34 +080094binder::Status MDnsService::getServiceAddress(const GetAddressInfo& info) {
paulhuc8a58ff2022-02-09 18:37:27 +080095 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +080096 int res = mListener.getAddrInfo(info.id, info.interfaceIdx, 0 /* protocol */,
97 info.hostname.c_str());
98 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +080099}
100
paulhu1ad658f2022-02-10 21:54:34 +0800101binder::Status MDnsService::stopOperation(int32_t id) {
paulhuc8a58ff2022-02-09 18:37:27 +0800102 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu1ad658f2022-02-10 21:54:34 +0800103 int res = mListener.stop(id);
104 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +0800105}
106
paulhu72742952022-02-09 21:24:09 +0800107binder::Status MDnsService::registerEventListener(const android::sp<IMDnsEventListener>& listener) {
paulhuc8a58ff2022-02-09 18:37:27 +0800108 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu72742952022-02-09 21:24:09 +0800109 int res = MDnsEventReporter::getInstance().addEventListener(listener);
110 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +0800111}
112
113binder::Status MDnsService::unregisterEventListener(
paulhu72742952022-02-09 21:24:09 +0800114 const android::sp<IMDnsEventListener>& listener) {
paulhuc8a58ff2022-02-09 18:37:27 +0800115 ENFORCE_NETWORK_STACK_PERMISSIONS();
paulhu72742952022-02-09 21:24:09 +0800116 int res = MDnsEventReporter::getInstance().removeEventListener(listener);
117 return statusFromErrcode(res);
paulhuc8a58ff2022-02-09 18:37:27 +0800118}
119
120} // namespace android::net