Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36796f35 authored by Luke Huang's avatar Luke Huang
Browse files

Move resolv_init out and make callbacks of libnetd_resolv global

1. Revise init flow of libnetd_resolv
   - A better and generic design for it
2. Rename and make callbacks from netd global
   - libnetd_resolv has multiple components need to use it
   - res* component could use it easily

Bug: 126141549
Test: built, flashed, booted
      system/netd/tests/runtests.sh pass

Change-Id: Iacb4fd708a26fd9c9139a606f0320fc12b4fc7e1
parent 067e9847
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ cc_library {
        "res_state.cpp",
        "res_stats.cpp",
        "DnsProxyListener.cpp",
        "DnsResolver.cpp",
        "DnsResolverService.cpp",
        "DnsTlsDispatcher.cpp",
        "DnsTlsQueryMap.cpp",
+9 −42
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@
 * limitations under the License.
 */

#include "DnsProxyListener.h"

#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
@@ -33,8 +35,6 @@
#include <list>
#include <vector>

#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/multinetwork.h>  // ResNsendFlags
#include <cutils/misc.h>           // FIRST_APPLICATION_UID
@@ -47,8 +47,7 @@
#include <sysutils/SocketClient.h>

// TODO: Considering moving ResponseCode.h Stopwatch.h thread_util.h to libnetdutils.
#include "DnsProxyListener.h"
#include "DnsResolverService.h"
#include "DnsResolver.h"
#include "NetdClient.h"  // NETID_USE_LOCAL_NAMESERVERS
#include "NetdPermissions.h"
#include "ResolverEventReporter.h"
@@ -61,32 +60,6 @@

using aidl::android::net::metrics::INetdEventListener;

static android::net::DnsProxyListener gDnsProxyListener;

bool resolv_init(const dnsproxylistener_callbacks& callbacks) {
    android::base::InitLogging(/*argv=*/nullptr);
    android::base::SetDefaultTag("libnetd_resolv");
    ALOGI("Initializing resolver");
    const std::string logSeverityStr =
            android::base::GetProperty("persist.sys.nw_dns_resolver_log", "WARNING");
    android::base::SetMinimumLogSeverity(logSeverityStrToEnum(logSeverityStr));

    if (!gDnsProxyListener.setCallbacks(callbacks)) {
        ALOGE("Unable to set callbacks to DnsProxyListener");
        return false;
    }
    if (gDnsProxyListener.startListener()) {
        ALOGE("Unable to start DnsProxyListener (%s)", strerror(errno));
        return false;
    }
    binder_status_t ret;
    if ((ret = android::net::DnsResolverService::start(callbacks)) != STATUS_OK) {
        ALOGE("Unable to start DnsResolverService: %d", ret);
        return false;
    }
    return true;
}

namespace android {
namespace net {

@@ -166,7 +139,7 @@ bool hasPermissionToBypassPrivateDns(uid_t uid) {
    for (const char* const permission :
         {PERM_CONNECTIVITY_USE_RESTRICTED_NETWORKS, PERM_NETWORK_BYPASS_PRIVATE_DNS,
          PERM_MAINLINE_NETWORK_STACK}) {
        if (gDnsProxyListener.mCallbacks.check_calling_permission(permission)) {
        if (gResNetdCallbacks.check_calling_permission(permission)) {
            return true;
        }
    }
@@ -506,7 +479,7 @@ bool synthesizeNat64PrefixWithARecord(const netdutils::IPPrefix& prefix, addrinf
bool getDns64Prefix(unsigned netId, netdutils::IPPrefix* prefix) {
    in6_addr v6addr{};
    uint8_t prefixLen = 0;
    if (!gDnsProxyListener.mCallbacks.get_dns64_prefix(netId, &v6addr, &prefixLen)) {
    if (!gResNetdCallbacks.get_dns64_prefix(netId, &v6addr, &prefixLen)) {
        return false;
    }
    const netdutils::IPAddress ipv6(v6addr);
@@ -516,12 +489,6 @@ bool getDns64Prefix(unsigned netId, netdutils::IPPrefix* prefix) {

}  // namespace

bool DnsProxyListener::setCallbacks(const dnsproxylistener_callbacks& callbacks) {
    mCallbacks = callbacks;
    return mCallbacks.check_calling_permission && mCallbacks.get_network_context &&
           mCallbacks.get_dns64_prefix;
}

DnsProxyListener::DnsProxyListener() : FrameworkListener(SOCKET_NAME) {
    registerCmd(new GetAddrInfoCmd());
    registerCmd(new GetHostByAddrCmd());
@@ -765,7 +732,7 @@ int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
    const uid_t uid = cli->getUid();

    android_net_context netcontext;
    gDnsProxyListener.mCallbacks.get_network_context(netId, uid, &netcontext);
    gResNetdCallbacks.get_network_context(netId, uid, &netcontext);

    if (useLocalNameservers) {
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
@@ -820,7 +787,7 @@ int DnsProxyListener::ResNSendCommand::runCommand(SocketClient* cli, int argc, c
    }

    android_net_context netcontext;
    gDnsProxyListener.mCallbacks.get_network_context(netId, uid, &netcontext);
    gResNetdCallbacks.get_network_context(netId, uid, &netcontext);
    if (checkAndClearUseLocalNameserversFlag(&netId)) {
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
    }
@@ -949,7 +916,7 @@ int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
    }

    android_net_context netcontext;
    gDnsProxyListener.mCallbacks.get_network_context(netId, uid, &netcontext);
    gResNetdCallbacks.get_network_context(netId, uid, &netcontext);

    if (useLocalNameservers) {
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
@@ -1087,7 +1054,7 @@ int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
    }

    android_net_context netcontext;
    gDnsProxyListener.mCallbacks.get_network_context(netId, uid, &netcontext);
    gResNetdCallbacks.get_network_context(netId, uid, &netcontext);

    if (useLocalNameservers) {
        netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
+2 −5
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#ifndef _DNSPROXYLISTENER_H__
#define _DNSPROXYLISTENER_H__

#include <string>

#include <netd_resolv/resolv.h>  // android_net_context
#include <sysutils/FrameworkCommand.h>
#include <sysutils/FrameworkListener.h>
@@ -29,13 +31,8 @@ class DnsProxyListener : public FrameworkListener {
    DnsProxyListener();
    virtual ~DnsProxyListener() {}

    bool setCallbacks(const dnsproxylistener_callbacks& callbacks);

    static constexpr const char* SOCKET_NAME = "dnsproxyd";

    // TODO: Considering putting this callbacks structure in its own file.
    dnsproxylistener_callbacks mCallbacks{};

  private:
    class GetAddrInfoCmd : public FrameworkCommand {
      public:

DnsResolver.cpp

0 → 100644
+79 −0
Original line number Diff line number Diff line
/**
 * Copyright (c) 2019, 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 "DnsResolver.h"

#include <android-base/logging.h>
#include <android-base/properties.h>

#include <resolv.h>
#include "DnsProxyListener.h"
#include "DnsResolverService.h"
#include "resolv_private.h"

bool resolv_init(const ResolverNetdCallbacks& callbacks) {
    android::base::InitLogging(/*argv=*/nullptr);
    android::base::SetDefaultTag("libnetd_resolv");
    LOG(INFO) << "Initializing resolver";
    const std::string logSeverityStr =
            android::base::GetProperty("persist.sys.nw_dns_resolver_log", "WARNING");
    android::base::SetMinimumLogSeverity(logSeverityStrToEnum(logSeverityStr));

    android::net::gResNetdCallbacks = callbacks;
    android::net::gDnsResolv = android::net::DnsResolver::getInstance();
    return android::net::gDnsResolv->start();
}

namespace android {
namespace net {

namespace {

bool verifyCallbacks() {
    return gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
           gResNetdCallbacks.get_dns64_prefix;
}

}  // namespace

DnsResolver* gDnsResolv = nullptr;
ResolverNetdCallbacks gResNetdCallbacks;

DnsResolver* DnsResolver::getInstance() {
    // Instantiated on first use.
    static DnsResolver instance;
    return &instance;
}

bool DnsResolver::start() {
    if (!verifyCallbacks()) {
        LOG(ERROR) << "Callback verification failed";
        return false;
    }
    if (mDnsProxyListener.startListener()) {
        PLOG(ERROR) << "Unable to start DnsProxyListener";
        return false;
    }
    binder_status_t ret;
    if ((ret = DnsResolverService::start()) != STATUS_OK) {
        LOG(ERROR) << "Unable to start DnsResolverService: " << ret;
        return false;
    }
    return true;
}

}  // namespace net
}  // namespace android

DnsResolver.h

0 → 100644
+45 −0
Original line number Diff line number Diff line
/**
 * Copyright (c) 2019, 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.
 */

#ifndef _DNS_RESOLVER_H_
#define _DNS_RESOLVER_H_

#include <resolv.h>
#include "DnsProxyListener.h"

namespace android {
namespace net {

class DnsResolver {
  public:
    static DnsResolver* getInstance();
    bool start();

    DnsResolver(DnsResolver const&) = delete;
    void operator=(DnsResolver const&) = delete;

  private:
    DnsResolver() {}
    DnsProxyListener mDnsProxyListener;
};

extern DnsResolver* gDnsResolv;
extern ResolverNetdCallbacks gResNetdCallbacks;

}  // namespace net
}  // namespace android

#endif  // _DNS_RESOLVER_H_
Loading