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>
22 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
28 #include <resolv_iface.h>
31 #define LOG_TAG "DnsProxyListener"
35 #include <cutils/log.h>
36 #include <sysutils/SocketClient.h>
38 #include "DnsProxyListener.h"
39 #include "ResponseCode.h"
41 DnsProxyListener::DnsProxyListener() :
42 FrameworkListener("dnsproxyd") {
43 registerCmd(new GetAddrInfoCmd());
44 registerCmd(new GetHostByAddrCmd());
45 registerCmd(new GetHostByNameCmd());
48 DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(SocketClient *c,
51 struct addrinfo* hints,
62 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
69 void DnsProxyListener::GetAddrInfoHandler::start() {
71 pthread_create(&thread, NULL,
72 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
73 pthread_detach(thread);
76 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
77 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
84 // Sends 4 bytes of big-endian length, followed by the data.
85 // Returns true on success.
86 static bool sendLenAndData(SocketClient *c, const int len, const void* data) {
87 uint32_t len_be = htonl(len);
88 return c->sendData(&len_be, 4) == 0 &&
89 (len == 0 || c->sendData(data, len) == 0);
92 // Returns true on success
93 static bool sendhostent(SocketClient *c, struct hostent *hp) {
96 if (hp->h_name != NULL) {
97 success &= sendLenAndData(c, strlen(hp->h_name)+1, hp->h_name);
99 success &= sendLenAndData(c, 0, "") == 0;
102 for (i=0; hp->h_aliases[i] != NULL; i++) {
103 success &= sendLenAndData(c, strlen(hp->h_aliases[i])+1, hp->h_aliases[i]);
105 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
107 uint32_t buf = htonl(hp->h_addrtype);
108 success &= c->sendData(&buf, sizeof(buf)) == 0;
110 buf = htonl(hp->h_length);
111 success &= c->sendData(&buf, sizeof(buf)) == 0;
113 for (i=0; hp->h_addr_list[i] != NULL; i++) {
114 success &= sendLenAndData(c, 16, hp->h_addr_list[i]);
116 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
120 void DnsProxyListener::GetAddrInfoHandler::run() {
122 ALOGD("GetAddrInfoHandler, now for %s / %s / %s", mHost, mService, mIface);
125 char tmp[IF_NAMESIZE + 1];
126 if (mIface == NULL) {
127 _resolv_get_pids_associated_interface(mPid, tmp, sizeof(tmp));
130 struct addrinfo* result = NULL;
131 uint32_t rv = android_getaddrinfoforiface(mHost, mService, mHints, mIface ? mIface : tmp,
134 // getaddrinfo failed
135 mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
137 bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
138 struct addrinfo* ai = result;
139 while (ai && success) {
140 success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
141 && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
142 && sendLenAndData(mClient,
143 ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
147 success = success && sendLenAndData(mClient, 0, "");
149 ALOGW("Error writing DNS result to client");
153 freeaddrinfo(result);
158 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
159 NetdCommand("getaddrinfo") {
162 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
163 int argc, char **argv) {
165 for (int i = 0; i < argc; i++) {
166 ALOGD("argv[%i]=%s", i, argv[i]);
171 asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
173 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
178 char* name = argv[1];
179 if (strcmp("^", name) == 0) {
185 char* service = argv[2];
186 if (strcmp("^", service) == 0) {
189 service = strdup(service);
192 char* iface = argv[7];
193 if (strcmp(iface, "^") == 0) {
196 iface = strdup(iface);
199 struct addrinfo* hints = NULL;
200 int ai_flags = atoi(argv[3]);
201 int ai_family = atoi(argv[4]);
202 int ai_socktype = atoi(argv[5]);
203 int ai_protocol = atoi(argv[6]);
204 int pid = atoi(argv[8]);
206 if (ai_flags != -1 || ai_family != -1 ||
207 ai_socktype != -1 || ai_protocol != -1) {
208 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
209 hints->ai_flags = ai_flags;
210 hints->ai_family = ai_family;
211 hints->ai_socktype = ai_socktype;
212 hints->ai_protocol = ai_protocol;
216 ALOGD("GetAddrInfoHandler for %s / %s / %s / %d",
217 name ? name : "[nullhost]",
218 service ? service : "[nullservice]",
219 iface ? iface : "[nulliface]",
224 DnsProxyListener::GetAddrInfoHandler* handler =
225 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, iface, pid);
231 /*******************************************************
233 *******************************************************/
234 DnsProxyListener::GetHostByNameCmd::GetHostByNameCmd() :
235 NetdCommand("gethostbyname") {
238 int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
239 int argc, char **argv) {
241 for (int i = 0; i < argc; i++) {
242 ALOGD("argv[%i]=%s", i, argv[i]);
247 asprintf(&msg, "Invalid number of arguments to gethostbyname: %i", argc);
249 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
254 int pid = atoi(argv[1]);
255 char* iface = argv[2];
256 char* name = argv[3];
257 int af = atoi(argv[4]);
259 if (strcmp(iface, "^") == 0) {
262 iface = strdup(iface);
265 if (strcmp(name, "^") == 0) {
272 DnsProxyListener::GetHostByNameHandler* handler =
273 new DnsProxyListener::GetHostByNameHandler(cli, pid, iface, name, af);
279 DnsProxyListener::GetHostByNameHandler::GetHostByNameHandler(SocketClient* c,
291 DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
296 void DnsProxyListener::GetHostByNameHandler::start() {
298 pthread_create(&thread, NULL,
299 DnsProxyListener::GetHostByNameHandler::threadStart, this);
300 pthread_detach(thread);
303 void* DnsProxyListener::GetHostByNameHandler::threadStart(void* obj) {
304 GetHostByNameHandler* handler = reinterpret_cast<GetHostByNameHandler*>(obj);
311 void DnsProxyListener::GetHostByNameHandler::run() {
313 ALOGD("DnsProxyListener::GetHostByNameHandler::run\n");
316 char iface[IF_NAMESIZE + 1];
317 if (mIface == NULL) {
318 _resolv_get_pids_associated_interface(mPid, iface, sizeof(iface));
323 hp = android_gethostbynameforiface(mName, mAf, mIface ? mIface : iface);
326 ALOGD("GetHostByNameHandler::run gethostbyname errno: %s hp->h_name = %s, name_len = %d\n",
327 hp ? "success" : strerror(errno),
328 (hp && hp->h_name) ? hp->h_name: "null",
329 (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
334 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
335 success &= sendhostent(mClient, hp);
337 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
341 ALOGW("GetHostByNameHandler: Error writing DNS result to client\n");
347 /*******************************************************
349 *******************************************************/
350 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
351 NetdCommand("gethostbyaddr") {
354 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
355 int argc, char **argv) {
357 for (int i = 0; i < argc; i++) {
358 ALOGD("argv[%i]=%s", i, argv[i]);
363 asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
365 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
370 char* addrStr = argv[1];
371 int addrLen = atoi(argv[2]);
372 int addrFamily = atoi(argv[3]);
373 int pid = atoi(argv[4]);
374 char* iface = argv[5];
376 if (strcmp(iface, "^") == 0) {
379 iface = strdup(iface);
382 void* addr = malloc(sizeof(struct in6_addr));
384 int result = inet_pton(addrFamily, addrStr, addr);
387 asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
389 cli->sendMsg(ResponseCode::OperationFailed, msg, false);
396 DnsProxyListener::GetHostByAddrHandler* handler =
397 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily, iface ,pid);
403 DnsProxyListener::GetHostByAddrHandler::GetHostByAddrHandler(SocketClient* c,
411 mAddressLen(addressLen),
412 mAddressFamily(addressFamily),
417 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
422 void DnsProxyListener::GetHostByAddrHandler::start() {
424 pthread_create(&thread, NULL,
425 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
426 pthread_detach(thread);
429 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
430 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
437 void DnsProxyListener::GetHostByAddrHandler::run() {
439 ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
442 char tmp[IF_NAMESIZE + 1];
443 if (mIface == NULL) {
444 _resolv_get_pids_associated_interface(mPid, tmp, sizeof(tmp));
449 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
450 hp = android_gethostbyaddrforiface((char*)mAddress, mAddressLen, mAddressFamily,
451 mIface ? mIface : tmp);
454 ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
455 hp ? "success" : strerror(errno),
456 (hp && hp->h_name) ? hp->h_name: "null",
457 (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
462 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
463 success &= sendhostent(mClient, hp);
465 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
469 ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");