blob: 774b65181c5ce6a4cc2710f6cb4352276ea44be4 [file] [log] [blame]
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +09001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <string>
30
31#include <dlfcn.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <string.h>
35
36#define LOG_TAG "Netd"
37#include <log/log.h>
38
39#include <netd_resolv/resolv_stub.h>
40
41struct ResolvStub RESOLV_STUB;
42
43inline constexpr char APEX_LIB64_DIR[] = "/apex/com.android.resolv/lib64";
44inline constexpr char APEX_LIB_DIR[] = "/apex/com.android.resolv/lib";
45inline constexpr char LIBNAME[] = "libnetd_resolv.so";
46
47template <typename FunctionType>
48static void resolvStubInitFunction(void* handle, const char* symbol, FunctionType** stubPtr) {
49 void* f = dlsym(handle, symbol);
50 if (f == nullptr) {
51 ALOGE("Can't find symbol %s in %s", symbol, LIBNAME);
52 abort();
53 }
54 *stubPtr = reinterpret_cast<FunctionType*>(f);
55}
56
57int resolv_stub_init() {
58 void* netdResolvHandle;
59
60 for (const auto& dir : {APEX_LIB64_DIR, APEX_LIB_DIR}) {
61 std::string path = std::string(dir) + "/" + LIBNAME;
62 netdResolvHandle = dlopen(path.c_str(), RTLD_NOW);
63 if (netdResolvHandle != nullptr) {
Lorenzo Colitti80332d42018-12-21 08:48:39 +090064 ALOGI("Loaded resolver library from %s", path.c_str());
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090065 break;
66 }
67 ALOGW("dlopen(%s) failed: %s", path.c_str(), strerror(errno));
68 }
69
70 if (netdResolvHandle == nullptr) {
71 ALOGE("Fatal: couldn't open libnetd_resolv");
72 abort();
73 }
74
75#define STR(x) #x
76#define RESOLV_STUB_LOAD_SYMBOL(x) resolvStubInitFunction(netdResolvHandle, STR(x), &RESOLV_STUB.x)
77 RESOLV_STUB_LOAD_SYMBOL(android_getaddrinfofornetcontext);
78 RESOLV_STUB_LOAD_SYMBOL(android_gethostbyaddrfornetcontext);
79 RESOLV_STUB_LOAD_SYMBOL(android_gethostbynamefornetcontext);
80 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_aggregate);
81 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_get_info_for_net);
82 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_get_usable_servers);
83 RESOLV_STUB_LOAD_SYMBOL(resolv_delete_cache_for_net);
84 RESOLV_STUB_LOAD_SYMBOL(resolv_delete_private_dns_for_net);
85 RESOLV_STUB_LOAD_SYMBOL(resolv_get_private_dns_status_for_net);
86 RESOLV_STUB_LOAD_SYMBOL(resolv_has_nameservers);
Mike Yu0ae31af2018-11-15 21:58:19 +080087 RESOLV_STUB_LOAD_SYMBOL(resolv_init);
Lorenzo Colittiafaaa8e2018-12-18 19:16:12 +090088 RESOLV_STUB_LOAD_SYMBOL(resolv_register_private_dns_callback);
89 RESOLV_STUB_LOAD_SYMBOL(resolv_res_nsend);
90 RESOLV_STUB_LOAD_SYMBOL(resolv_set_nameservers_for_net);
91 RESOLV_STUB_LOAD_SYMBOL(resolv_set_private_dns_for_net);
92#undef RESOLV_STUB_LOAD_SYMBOL
93#undef STR
94
95 return 0;
96}