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 <android-base/properties.h>
22 #include <binder/Status.h>
23 #include <binder_utils/BinderUtil.h>
24 
25 using android::net::mdns::aidl::DiscoveryInfo;
26 using android::net::mdns::aidl::GetAddressInfo;
27 using android::net::mdns::aidl::IMDnsEventListener;
28 using android::net::mdns::aidl::RegistrationInfo;
29 using android::net::mdns::aidl::ResolutionInfo;
30 
31 using std::literals::chrono_literals::operator""s;
32 
33 namespace android::net {
34 
35 #define MDNS_SERVICE_NAME "mdnsd"
36 #define MDNS_SERVICE_STATUS "init.svc.mdnsd"
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     if (android::base::GetProperty(MDNS_SERVICE_STATUS, "") == "running") {
63         return android::binder::Status::fromServiceSpecificError(EBUSY, strerror(EBUSY));
64     }
65 
66     ALOGD("Starting MDNSD");
67     android::base::SetProperty("ctl.start", MDNS_SERVICE_NAME);
68     // To maintain the same behavior as before, the returned value is not checked.
69     android::base::WaitForProperty(MDNS_SERVICE_STATUS, "running", 5s);
70     return binder::Status::ok();
71 }
72 
stopDaemon()73 binder::Status MDnsService::stopDaemon() {
74     ENFORCE_NETWORK_STACK_PERMISSIONS();
75     ALOGD("Stopping MDNSD");
76     android::base::SetProperty("ctl.stop", MDNS_SERVICE_NAME);
77     android::base::WaitForProperty(MDNS_SERVICE_STATUS, "stopped", 5s);
78     return binder::Status::ok();
79 }
80 
registerService(const RegistrationInfo &)81 binder::Status MDnsService::registerService(const RegistrationInfo&) {
82     // TODO(b/298594687): switch from EX_SERVICE_SPECIFIC to DEPRECATED when tethering module
83     // for 2024-02 release is fully rolled out and prebuilt updated in AP1A.xxxxxx.yy build.
84     // Return EX_SERVICE_SPECIFIC for short-term only because callers in tethering module do not
85     // catch the EX_UNSUPPORTED_OPERATION. It will throw an exception and cause a fatal exception.
86     // The EX_UNSUPPORTED_OPERATION has been catched in tethering module since 2024-02 release.
87     // TODO(b/298594687): switch to DEPRECATED.
88     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
89     // DEPRECATED;
90 }
91 
discover(const DiscoveryInfo &)92 binder::Status MDnsService::discover(const DiscoveryInfo&) {
93     // TODO(b/298594687): switch to DEPRECATED.
94     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
95     // DEPRECATED;
96 }
97 
resolve(const ResolutionInfo &)98 binder::Status MDnsService::resolve(const ResolutionInfo&) {
99     // TODO(b/298594687): switch to DEPRECATED.
100     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
101     // DEPRECATED;
102 }
103 
getServiceAddress(const GetAddressInfo &)104 binder::Status MDnsService::getServiceAddress(const GetAddressInfo&) {
105     // TODO(b/298594687): switch to DEPRECATED.
106     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
107     // DEPRECATED;
108 }
109 
stopOperation(int32_t)110 binder::Status MDnsService::stopOperation(int32_t) {
111     // TODO(b/298594687): switch to DEPRECATED.
112     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
113     // DEPRECATED;
114 }
115 
registerEventListener(const android::sp<IMDnsEventListener> &)116 binder::Status MDnsService::registerEventListener(const android::sp<IMDnsEventListener>&) {
117     // TODO(b/298594687): switch to DEPRECATED.
118     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
119     // DEPRECATED;
120 }
121 
unregisterEventListener(const android::sp<IMDnsEventListener> &)122 binder::Status MDnsService::unregisterEventListener(const android::sp<IMDnsEventListener>&) {
123     // TODO(b/298594687): switch to DEPRECATED.
124     return binder::Status::fromExceptionCode(binder::Status::EX_SERVICE_SPECIFIC);
125     // DEPRECATED;
126 }
127 
128 }  // namespace android::net
129