blob: 5054abfc4f2cfd4062d33bf5c9b49101857ab2dc [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) {
64 break;
65 }
66 ALOGW("dlopen(%s) failed: %s", path.c_str(), strerror(errno));
67 }
68
69 if (netdResolvHandle == nullptr) {
70 ALOGE("Fatal: couldn't open libnetd_resolv");
71 abort();
72 }
73
74#define STR(x) #x
75#define RESOLV_STUB_LOAD_SYMBOL(x) resolvStubInitFunction(netdResolvHandle, STR(x), &RESOLV_STUB.x)
76 RESOLV_STUB_LOAD_SYMBOL(android_getaddrinfofornetcontext);
77 RESOLV_STUB_LOAD_SYMBOL(android_gethostbyaddrfornetcontext);
78 RESOLV_STUB_LOAD_SYMBOL(android_gethostbynamefornetcontext);
79 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_aggregate);
80 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_get_info_for_net);
81 RESOLV_STUB_LOAD_SYMBOL(android_net_res_stats_get_usable_servers);
82 RESOLV_STUB_LOAD_SYMBOL(resolv_delete_cache_for_net);
83 RESOLV_STUB_LOAD_SYMBOL(resolv_delete_private_dns_for_net);
84 RESOLV_STUB_LOAD_SYMBOL(resolv_get_private_dns_status_for_net);
85 RESOLV_STUB_LOAD_SYMBOL(resolv_has_nameservers);
86 RESOLV_STUB_LOAD_SYMBOL(resolv_register_private_dns_callback);
87 RESOLV_STUB_LOAD_SYMBOL(resolv_res_nsend);
88 RESOLV_STUB_LOAD_SYMBOL(resolv_set_nameservers_for_net);
89 RESOLV_STUB_LOAD_SYMBOL(resolv_set_private_dns_for_net);
90#undef RESOLV_STUB_LOAD_SYMBOL
91#undef STR
92
93 return 0;
94}