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"
36 DnsProxyListener::DnsProxyListener() :
37 FrameworkListener("dnsproxyd") {
38 registerCmd(new GetAddrInfoCmd());
39 registerCmd(new GetHostByAddrCmd());
42 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
48 void DnsProxyListener::GetAddrInfoHandler::start() {
49 pthread_create(&mThread, NULL,
50 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
53 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
54 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
61 // Sends 4 bytes of big-endian length, followed by the data.
62 // Returns true on success.
63 static bool sendLenAndData(SocketClient *c, const int len, const void* data) {
64 uint32_t len_be = htonl(len);
65 return c->sendData(&len_be, 4) == 0 &&
66 (len == 0 || c->sendData(data, len) == 0);
69 void DnsProxyListener::GetAddrInfoHandler::run() {
71 LOGD("GetAddrInfoHandler, now for %s / %s", mHost, mService);
74 struct addrinfo* result = NULL;
75 int rv = getaddrinfo(mHost, mService, mHints, &result);
76 bool success = (mClient->sendData(&rv, sizeof(rv)) == 0);
78 struct addrinfo* ai = result;
79 while (ai && success) {
80 success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
81 && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
82 && sendLenAndData(mClient,
83 ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
87 success = success && sendLenAndData(mClient, 0, "");
93 LOGW("Error writing DNS result to client");
98 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
99 NetdCommand("getaddrinfo") {
102 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
103 int argc, char **argv) {
105 for (int i = 0; i < argc; i++) {
106 LOGD("argv[%i]=%s", i, argv[i]);
110 LOGW("Invalid number of arguments to getaddrinfo: %i", argc);
111 sendLenAndData(cli, 0, NULL);
115 char* name = argv[1];
116 if (strcmp("^", name) == 0) {
122 char* service = argv[2];
123 if (strcmp("^", service) == 0) {
126 service = strdup(service);
129 struct addrinfo* hints = NULL;
130 int ai_flags = atoi(argv[3]);
131 int ai_family = atoi(argv[4]);
132 int ai_socktype = atoi(argv[5]);
133 int ai_protocol = atoi(argv[6]);
134 if (ai_flags != -1 || ai_family != -1 ||
135 ai_socktype != -1 || ai_protocol != -1) {
136 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
137 hints->ai_flags = ai_flags;
138 hints->ai_family = ai_family;
139 hints->ai_socktype = ai_socktype;
140 hints->ai_protocol = ai_protocol;
144 LOGD("GetAddrInfoHandler for %s / %s",
145 name ? name : "[nullhost]",
146 service ? service : "[nullservice]");
150 DnsProxyListener::GetAddrInfoHandler* handler =
151 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints);
157 /*******************************************************
159 *******************************************************/
160 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
161 NetdCommand("gethostbyaddr") {
164 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
165 int argc, char **argv) {
167 for (int i = 0; i < argc; i++) {
168 LOGD("argv[%i]=%s", i, argv[i]);
172 LOGW("Invalid number of arguments to gethostbyaddr: %i", argc);
173 sendLenAndData(cli, 0, NULL);
177 char* addrStr = argv[1];
178 int addrLen = atoi(argv[2]);
179 int addrFamily = atoi(argv[3]);
181 void* addr = malloc(sizeof(struct in6_addr));
183 int result = inet_pton(addrFamily, addrStr, addr);
185 LOGW("inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
187 sendLenAndData(cli, 0, NULL);
192 DnsProxyListener::GetHostByAddrHandler* handler =
193 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily);
199 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
203 void DnsProxyListener::GetHostByAddrHandler::start() {
204 pthread_create(&mThread, NULL,
205 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
208 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
209 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
216 void DnsProxyListener::GetHostByAddrHandler::run() {
218 LOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
223 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
224 hp = gethostbyaddr((char*)mAddress, mAddressLen, mAddressFamily);
227 LOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
228 hp ? "success" : strerror(errno),
229 (hp && hp->h_name) ? hp->h_name: "null",
230 (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
233 bool success = sendLenAndData(mClient, (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0,
234 (hp && hp->h_name) ? hp->h_name : "");
237 LOGW("GetHostByAddrHandler: Error writing DNS result to client\n");