2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <arpa/inet.h>
23 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
29 #include <resolv_netid.h>
32 #define LOG_TAG "DnsProxyListener"
38 #include <cutils/log.h>
39 #include <binder/IServiceManager.h>
40 #include <utils/String16.h>
41 #include <sysutils/SocketClient.h>
44 #include "DnsProxyListener.h"
45 #include "NetdConstants.h"
46 #include "NetworkController.h"
47 #include "ResponseCode.h"
48 #include "android/net/metrics/IDnsEventListener.h"
50 using android::String16;
51 using android::interface_cast;
52 using android::net::metrics::IDnsEventListener;
54 DnsProxyListener::DnsProxyListener(const NetworkController* netCtrl) :
55 FrameworkListener("dnsproxyd"), mNetCtrl(netCtrl) {
56 registerCmd(new GetAddrInfoCmd(this));
57 registerCmd(new GetHostByAddrCmd(this));
58 registerCmd(new GetHostByNameCmd(this));
61 DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(
62 SocketClient *c, char* host, char* service, struct addrinfo* hints,
63 const struct android_net_context& netcontext,
64 const android::sp<android::net::metrics::IDnsEventListener>& dnsEventListener)
69 mNetContext(netcontext),
70 mDnsEventListener(dnsEventListener) {
73 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
79 void DnsProxyListener::GetAddrInfoHandler::start() {
81 pthread_create(&thread, NULL,
82 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
83 pthread_detach(thread);
86 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
87 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
94 android::sp<IDnsEventListener> DnsProxyListener::getDnsEventListener() {
95 if (mDnsEventListener == nullptr) {
96 // Use checkService instead of getService because getService waits for 5 seconds for the
97 // service to become available. The DNS resolver inside netd is started much earlier in the
98 // boot sequence than the framework DNS listener, and we don't want to delay all DNS lookups
99 // for 5 seconds until the DNS listener starts up.
100 android::sp<android::IBinder> b = android::defaultServiceManager()->checkService(
101 android::String16("dns_listener"));
103 mDnsEventListener = interface_cast<IDnsEventListener>(b);
106 // If the DNS listener service is dead, the binder call will just return an error, which should
107 // be fine because the only impact is that we can't log DNS events. In any case, this should
108 // only happen if the system server is going down, which means it will shortly be taking us down
110 return mDnsEventListener;
113 static bool sendBE32(SocketClient* c, uint32_t data) {
114 uint32_t be_data = htonl(data);
115 return c->sendData(&be_data, sizeof(be_data)) == 0;
118 // Sends 4 bytes of big-endian length, followed by the data.
119 // Returns true on success.
120 static bool sendLenAndData(SocketClient* c, const int len, const void* data) {
121 return sendBE32(c, len) && (len == 0 || c->sendData(data, len) == 0);
124 // Returns true on success
125 static bool sendhostent(SocketClient *c, struct hostent *hp) {
128 if (hp->h_name != NULL) {
129 success &= sendLenAndData(c, strlen(hp->h_name)+1, hp->h_name);
131 success &= sendLenAndData(c, 0, "") == 0;
134 for (i=0; hp->h_aliases[i] != NULL; i++) {
135 success &= sendLenAndData(c, strlen(hp->h_aliases[i])+1, hp->h_aliases[i]);
137 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
139 uint32_t buf = htonl(hp->h_addrtype);
140 success &= c->sendData(&buf, sizeof(buf)) == 0;
142 buf = htonl(hp->h_length);
143 success &= c->sendData(&buf, sizeof(buf)) == 0;
145 for (i=0; hp->h_addr_list[i] != NULL; i++) {
146 success &= sendLenAndData(c, 16, hp->h_addr_list[i]);
148 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
152 static bool sendaddrinfo(SocketClient* c, struct addrinfo* ai) {
154 // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
155 // int ai_family; /* PF_xxx */
156 // int ai_socktype; /* SOCK_xxx */
157 // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
158 // socklen_t ai_addrlen; /* length of ai_addr */
159 // char *ai_canonname; /* canonical name for hostname */
160 // struct sockaddr *ai_addr; /* binary address */
161 // struct addrinfo *ai_next; /* next structure in linked list */
164 // Write the struct piece by piece because we might be a 64-bit netd
165 // talking to a 32-bit process.
167 sendBE32(c, ai->ai_flags) &&
168 sendBE32(c, ai->ai_family) &&
169 sendBE32(c, ai->ai_socktype) &&
170 sendBE32(c, ai->ai_protocol);
175 // ai_addrlen and ai_addr.
176 if (!sendLenAndData(c, ai->ai_addrlen, ai->ai_addr)) {
180 // strlen(ai_canonname) and ai_canonname.
181 if (!sendLenAndData(c, ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0, ai->ai_canonname)) {
188 void DnsProxyListener::GetAddrInfoHandler::run() {
190 ALOGD("GetAddrInfoHandler, now for %s / %s / {%u,%u,%u,%u,%u}", mHost, mService,
191 mNetContext.app_netid, mNetContext.app_mark,
192 mNetContext.dns_netid, mNetContext.dns_mark,
196 struct addrinfo* result = NULL;
198 uint32_t rv = android_getaddrinfofornetcontext(mHost, mService, mHints, &mNetContext, &result);
199 const int latencyMs = lround(s.timeTaken());
202 // getaddrinfo failed
203 mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
205 bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
206 struct addrinfo* ai = result;
207 while (ai && success) {
208 success = sendBE32(mClient, 1) && sendaddrinfo(mClient, ai);
211 success = success && sendBE32(mClient, 0);
213 ALOGW("Error writing DNS result to client");
217 freeaddrinfo(result);
220 if (mDnsEventListener != nullptr) {
221 mDnsEventListener->onDnsEvent(mNetContext.dns_netid, IDnsEventListener::EVENT_GETADDRINFO,
222 (int32_t) rv, latencyMs);
226 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd(DnsProxyListener* dnsProxyListener) :
227 NetdCommand("getaddrinfo"),
228 mDnsProxyListener(dnsProxyListener) {
231 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
232 int argc, char **argv) {
234 for (int i = 0; i < argc; i++) {
235 ALOGD("argv[%i]=%s", i, argv[i]);
240 asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
242 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
247 char* name = argv[1];
248 if (strcmp("^", name) == 0) {
254 char* service = argv[2];
255 if (strcmp("^", service) == 0) {
258 service = strdup(service);
261 struct addrinfo* hints = NULL;
262 int ai_flags = atoi(argv[3]);
263 int ai_family = atoi(argv[4]);
264 int ai_socktype = atoi(argv[5]);
265 int ai_protocol = atoi(argv[6]);
266 unsigned netId = strtoul(argv[7], NULL, 10);
267 uid_t uid = cli->getUid();
269 struct android_net_context netcontext;
270 mDnsProxyListener->mNetCtrl->getNetworkContext(netId, uid, &netcontext);
272 if (ai_flags != -1 || ai_family != -1 ||
273 ai_socktype != -1 || ai_protocol != -1) {
274 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
275 hints->ai_flags = ai_flags;
276 hints->ai_family = ai_family;
277 hints->ai_socktype = ai_socktype;
278 hints->ai_protocol = ai_protocol;
282 ALOGD("GetAddrInfoHandler for %s / %s / {%u,%u,%u,%u,%u}",
283 name ? name : "[nullhost]",
284 service ? service : "[nullservice]",
285 netcontext.app_netid, netcontext.app_mark,
286 netcontext.dns_netid, netcontext.dns_mark,
291 DnsProxyListener::GetAddrInfoHandler* handler =
292 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netcontext,
293 mDnsProxyListener->getDnsEventListener());
299 /*******************************************************
301 *******************************************************/
302 DnsProxyListener::GetHostByNameCmd::GetHostByNameCmd(DnsProxyListener* dnsProxyListener) :
303 NetdCommand("gethostbyname"),
304 mDnsProxyListener(dnsProxyListener) {
307 int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
308 int argc, char **argv) {
310 for (int i = 0; i < argc; i++) {
311 ALOGD("argv[%i]=%s", i, argv[i]);
316 asprintf(&msg, "Invalid number of arguments to gethostbyname: %i", argc);
318 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
323 uid_t uid = cli->getUid();
324 unsigned netId = strtoul(argv[1], NULL, 10);
325 char* name = argv[2];
326 int af = atoi(argv[3]);
328 if (strcmp(name, "^") == 0) {
334 uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
337 DnsProxyListener::GetHostByNameHandler* handler =
338 new DnsProxyListener::GetHostByNameHandler(cli, name, af, netId, mark,
339 mDnsProxyListener->getDnsEventListener());
345 DnsProxyListener::GetHostByNameHandler::GetHostByNameHandler(
346 SocketClient* c, char* name, int af, unsigned netId, uint32_t mark,
347 const android::sp<android::net::metrics::IDnsEventListener>& dnsEventListener)
353 mDnsEventListener(dnsEventListener) {
356 DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
360 void DnsProxyListener::GetHostByNameHandler::start() {
362 pthread_create(&thread, NULL,
363 DnsProxyListener::GetHostByNameHandler::threadStart, this);
364 pthread_detach(thread);
367 void* DnsProxyListener::GetHostByNameHandler::threadStart(void* obj) {
368 GetHostByNameHandler* handler = reinterpret_cast<GetHostByNameHandler*>(obj);
375 void DnsProxyListener::GetHostByNameHandler::run() {
377 ALOGD("DnsProxyListener::GetHostByNameHandler::run\n");
381 struct hostent* hp = android_gethostbynamefornet(mName, mAf, mNetId, mMark);
382 const int latencyMs = lround(s.timeTaken());
385 ALOGD("GetHostByNameHandler::run gethostbyname errno: %s hp->h_name = %s, name_len = %zu\n",
386 hp ? "success" : strerror(errno),
387 (hp && hp->h_name) ? hp->h_name : "null",
388 (hp && hp->h_name) ? strlen(hp->h_name) + 1 : 0);
393 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
394 success &= sendhostent(mClient, hp);
396 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
400 ALOGW("GetHostByNameHandler: Error writing DNS result to client\n");
404 if (mDnsEventListener != nullptr) {
405 mDnsEventListener->onDnsEvent(mNetId, IDnsEventListener::EVENT_GETHOSTBYNAME,
411 /*******************************************************
413 *******************************************************/
414 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener) :
415 NetdCommand("gethostbyaddr"),
416 mDnsProxyListener(dnsProxyListener) {
419 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
420 int argc, char **argv) {
422 for (int i = 0; i < argc; i++) {
423 ALOGD("argv[%i]=%s", i, argv[i]);
428 asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
430 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
435 char* addrStr = argv[1];
436 int addrLen = atoi(argv[2]);
437 int addrFamily = atoi(argv[3]);
438 uid_t uid = cli->getUid();
439 unsigned netId = strtoul(argv[4], NULL, 10);
441 void* addr = malloc(sizeof(struct in6_addr));
443 int result = inet_pton(addrFamily, addrStr, addr);
446 asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
448 cli->sendMsg(ResponseCode::OperationFailed, msg, false);
454 uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
457 DnsProxyListener::GetHostByAddrHandler* handler =
458 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily, netId, mark);
464 DnsProxyListener::GetHostByAddrHandler::GetHostByAddrHandler(SocketClient* c,
472 mAddressLen(addressLen),
473 mAddressFamily(addressFamily),
478 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
482 void DnsProxyListener::GetHostByAddrHandler::start() {
484 pthread_create(&thread, NULL,
485 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
486 pthread_detach(thread);
489 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
490 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
497 void DnsProxyListener::GetHostByAddrHandler::run() {
499 ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
503 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
504 hp = android_gethostbyaddrfornet((char*)mAddress, mAddressLen, mAddressFamily, mNetId, mMark);
507 ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %zu\n",
508 hp ? "success" : strerror(errno),
509 (hp && hp->h_name) ? hp->h_name : "null",
510 (hp && hp->h_name) ? strlen(hp->h_name) + 1 : 0);
515 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
516 success &= sendhostent(mClient, hp);
518 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
522 ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");