• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
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 
30 using android::net::mdns::aidl::DiscoveryInfo;
31 using android::net::mdns::aidl::GetAddressInfo;
32 using android::net::mdns::aidl::IMDnsEventListener;
33 using android::net::mdns::aidl::RegistrationInfo;
34 using android::net::mdns::aidl::ResolutionInfo;
35 
36 namespace 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 
start()51 status_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 
startDaemon()60 binder::Status MDnsService::startDaemon() {
61     ENFORCE_NETWORK_STACK_PERMISSIONS();
62     int res = mListener.startDaemon();
63     return statusFromErrcode(res);
64 }
65 
stopDaemon()66 binder::Status MDnsService::stopDaemon() {
67     ENFORCE_NETWORK_STACK_PERMISSIONS();
68     int res = mListener.stopDaemon();
69     return statusFromErrcode(res);
70 }
71 
registerService(const RegistrationInfo & info)72 binder::Status MDnsService::registerService(const RegistrationInfo& info) {
73     ENFORCE_NETWORK_STACK_PERMISSIONS();
74     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);
78 }
79 
discover(const DiscoveryInfo & info)80 binder::Status MDnsService::discover(const DiscoveryInfo& info) {
81     ENFORCE_NETWORK_STACK_PERMISSIONS();
82     int res = mListener.discover(info.interfaceIdx, info.registrationType.c_str(),
83                                  nullptr /* domain */, info.id, 0 /* requestFlags */);
84     return statusFromErrcode(res);
85 }
86 
resolve(const ResolutionInfo & info)87 binder::Status MDnsService::resolve(const ResolutionInfo& info) {
88     ENFORCE_NETWORK_STACK_PERMISSIONS();
89     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);
92 }
93 
getServiceAddress(const GetAddressInfo & info)94 binder::Status MDnsService::getServiceAddress(const GetAddressInfo& info) {
95     ENFORCE_NETWORK_STACK_PERMISSIONS();
96     int res = mListener.getAddrInfo(info.id, info.interfaceIdx, 0 /* protocol */,
97                                     info.hostname.c_str());
98     return statusFromErrcode(res);
99 }
100 
stopOperation(int32_t id)101 binder::Status MDnsService::stopOperation(int32_t id) {
102     ENFORCE_NETWORK_STACK_PERMISSIONS();
103     int res = mListener.stop(id);
104     return statusFromErrcode(res);
105 }
106 
registerEventListener(const android::sp<IMDnsEventListener> & listener)107 binder::Status MDnsService::registerEventListener(const android::sp<IMDnsEventListener>& listener) {
108     ENFORCE_NETWORK_STACK_PERMISSIONS();
109     int res = MDnsEventReporter::getInstance().addEventListener(listener);
110     return statusFromErrcode(res);
111 }
112 
unregisterEventListener(const android::sp<IMDnsEventListener> & listener)113 binder::Status MDnsService::unregisterEventListener(
114         const android::sp<IMDnsEventListener>& listener) {
115     ENFORCE_NETWORK_STACK_PERMISSIONS();
116     int res = MDnsEventReporter::getInstance().removeEventListener(listener);
117     return statusFromErrcode(res);
118 }
119 
120 }  // namespace android::net
121