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>
29 #define LOG_TAG "DnsProxyListener"
32 #include <cutils/log.h>
33 #include <sysutils/SocketClient.h>
35 #include "DnsProxyListener.h"
36 #include "ResponseCode.h"
38 DnsProxyListener::DnsProxyListener() :
39 FrameworkListener("dnsproxyd") {
40 registerCmd(new GetAddrInfoCmd());
41 registerCmd(new GetHostByAddrCmd());
44 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
50 void DnsProxyListener::GetAddrInfoHandler::start() {
52 pthread_create(&thread, NULL,
53 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
56 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
57 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
64 // Sends 4 bytes of big-endian length, followed by the data.
65 // Returns true on success.
66 static bool sendLenAndData(SocketClient *c, const int len, const void* data) {
67 uint32_t len_be = htonl(len);
68 return c->sendData(&len_be, 4) == 0 &&
69 (len == 0 || c->sendData(data, len) == 0);
72 void DnsProxyListener::GetAddrInfoHandler::run() {
74 ALOGD("GetAddrInfoHandler, now for %s / %s", mHost, mService);
77 struct addrinfo* result = NULL;
78 uint32_t rv = getaddrinfo(mHost, mService, mHints, &result);
81 mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
83 bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
84 struct addrinfo* ai = result;
85 while (ai && success) {
86 success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
87 && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
88 && sendLenAndData(mClient,
89 ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
93 success = success && sendLenAndData(mClient, 0, "");
95 ALOGW("Error writing DNS result to client");
104 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
105 NetdCommand("getaddrinfo") {
108 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
109 int argc, char **argv) {
111 for (int i = 0; i < argc; i++) {
112 ALOGD("argv[%i]=%s", i, argv[i]);
117 asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
119 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
124 char* name = argv[1];
125 if (strcmp("^", name) == 0) {
131 char* service = argv[2];
132 if (strcmp("^", service) == 0) {
135 service = strdup(service);
138 struct addrinfo* hints = NULL;
139 int ai_flags = atoi(argv[3]);
140 int ai_family = atoi(argv[4]);
141 int ai_socktype = atoi(argv[5]);
142 int ai_protocol = atoi(argv[6]);
143 if (ai_flags != -1 || ai_family != -1 ||
144 ai_socktype != -1 || ai_protocol != -1) {
145 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
146 hints->ai_flags = ai_flags;
147 hints->ai_family = ai_family;
148 hints->ai_socktype = ai_socktype;
149 hints->ai_protocol = ai_protocol;
153 ALOGD("GetAddrInfoHandler for %s / %s",
154 name ? name : "[nullhost]",
155 service ? service : "[nullservice]");
159 DnsProxyListener::GetAddrInfoHandler* handler =
160 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints);
166 /*******************************************************
168 *******************************************************/
169 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
170 NetdCommand("gethostbyaddr") {
173 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
174 int argc, char **argv) {
176 for (int i = 0; i < argc; i++) {
177 ALOGD("argv[%i]=%s", i, argv[i]);
183 asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
185 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
190 char* addrStr = argv[1];
191 int addrLen = atoi(argv[2]);
192 int addrFamily = atoi(argv[3]);
194 void* addr = malloc(sizeof(struct in6_addr));
196 int result = inet_pton(addrFamily, addrStr, addr);
199 asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
201 cli->sendMsg(ResponseCode::OperationFailed, msg, false);
208 DnsProxyListener::GetHostByAddrHandler* handler =
209 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily);
215 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
219 void DnsProxyListener::GetHostByAddrHandler::start() {
221 pthread_create(&thread, NULL,
222 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
225 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
226 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
233 void DnsProxyListener::GetHostByAddrHandler::run() {
235 ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
240 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
241 hp = gethostbyaddr((char*)mAddress, mAddressLen, mAddressFamily);
244 ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
245 hp ? "success" : strerror(errno),
246 (hp && hp->h_name) ? hp->h_name: "null",
247 (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
252 failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyQueryResult,
253 hp->h_name ? hp->h_name : "",
254 hp->h_name ? strlen(hp->h_name)+ 1 : 0);
256 uint32_t error = h_errno;
257 failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed,
258 &error, sizeof(error));
262 ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");