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 #define LOG_TAG "DnsProxyListener"
31 #include <cutils/log.h>
32 #include <sysutils/SocketClient.h>
34 #include "DnsProxyListener.h"
35 #include "ResponseCode.h"
37 DnsProxyListener::DnsProxyListener() :
38 FrameworkListener("dnsproxyd") {
39 registerCmd(new GetAddrInfoCmd());
40 registerCmd(new GetHostByAddrCmd());
43 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
49 void DnsProxyListener::GetAddrInfoHandler::start() {
50 pthread_create(&mThread, NULL,
51 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
54 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
55 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
62 // Sends 4 bytes of big-endian length, followed by the data.
63 // Returns true on success.
64 static bool sendLenAndData(SocketClient *c, const int len, const void* data) {
65 uint32_t len_be = htonl(len);
66 return c->sendData(&len_be, 4) == 0 &&
67 (len == 0 || c->sendData(data, len) == 0);
70 void DnsProxyListener::GetAddrInfoHandler::run() {
72 ALOGD("GetAddrInfoHandler, now for %s / %s", mHost, mService);
75 struct addrinfo* result = NULL;
76 uint32_t rv = getaddrinfo(mHost, mService, mHints, &result);
79 mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
81 bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
82 struct addrinfo* ai = result;
83 while (ai && success) {
84 success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
85 && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
86 && sendLenAndData(mClient,
87 ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
91 success = success && sendLenAndData(mClient, 0, "");
93 ALOGW("Error writing DNS result to client");
102 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
103 NetdCommand("getaddrinfo") {
106 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
107 int argc, char **argv) {
109 for (int i = 0; i < argc; i++) {
110 ALOGD("argv[%i]=%s", i, argv[i]);
115 asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
117 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
122 char* name = argv[1];
123 if (strcmp("^", name) == 0) {
129 char* service = argv[2];
130 if (strcmp("^", service) == 0) {
133 service = strdup(service);
136 struct addrinfo* hints = NULL;
137 int ai_flags = atoi(argv[3]);
138 int ai_family = atoi(argv[4]);
139 int ai_socktype = atoi(argv[5]);
140 int ai_protocol = atoi(argv[6]);
141 if (ai_flags != -1 || ai_family != -1 ||
142 ai_socktype != -1 || ai_protocol != -1) {
143 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
144 hints->ai_flags = ai_flags;
145 hints->ai_family = ai_family;
146 hints->ai_socktype = ai_socktype;
147 hints->ai_protocol = ai_protocol;
151 ALOGD("GetAddrInfoHandler for %s / %s",
152 name ? name : "[nullhost]",
153 service ? service : "[nullservice]");
157 DnsProxyListener::GetAddrInfoHandler* handler =
158 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints);
164 /*******************************************************
166 *******************************************************/
167 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
168 NetdCommand("gethostbyaddr") {
171 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
172 int argc, char **argv) {
174 for (int i = 0; i < argc; i++) {
175 ALOGD("argv[%i]=%s", i, argv[i]);
181 asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
183 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
188 char* addrStr = argv[1];
189 int addrLen = atoi(argv[2]);
190 int addrFamily = atoi(argv[3]);
192 void* addr = malloc(sizeof(struct in6_addr));
194 int result = inet_pton(addrFamily, addrStr, addr);
197 asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
199 cli->sendMsg(ResponseCode::OperationFailed, msg, false);
206 DnsProxyListener::GetHostByAddrHandler* handler =
207 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily);
213 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
217 void DnsProxyListener::GetHostByAddrHandler::start() {
218 pthread_create(&mThread, NULL,
219 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
222 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
223 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
230 void DnsProxyListener::GetHostByAddrHandler::run() {
232 ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
237 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
238 hp = gethostbyaddr((char*)mAddress, mAddressLen, mAddressFamily);
241 ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
242 hp ? "success" : strerror(errno),
243 (hp && hp->h_name) ? hp->h_name: "null",
244 (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
249 failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyQueryResult,
250 hp->h_name ? hp->h_name : "",
251 hp->h_name ? strlen(hp->h_name)+ 1 : 0);
253 uint32_t error = h_errno;
254 failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed,
255 &error, sizeof(error));
259 ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");