DnsProxyListener.cpp revision 1dbd6cf148ea3fab57ec0644c336e94c78a488be
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <arpa/inet.h>
18#include <dirent.h>
19#include <errno.h>
20#include <linux/if.h>
21#include <netdb.h>
22#include <netinet/in.h>
23#include <stdlib.h>
24#include <sys/socket.h>
25#include <sys/types.h>
26
27#define LOG_TAG "DnsProxyListener"
28#define DBG 0
29
30#include <cutils/log.h>
31#include <sysutils/SocketClient.h>
32
33#include "DnsProxyListener.h"
34
35DnsProxyListener::DnsProxyListener() :
36                 FrameworkListener("dnsproxyd") {
37    registerCmd(new GetAddrInfoCmd());
38    registerCmd(new GetHostByAddrCmd());
39}
40
41DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
42    free(mHost);
43    free(mService);
44    free(mHints);
45}
46
47void DnsProxyListener::GetAddrInfoHandler::start() {
48    pthread_create(&mThread, NULL,
49                   DnsProxyListener::GetAddrInfoHandler::threadStart, this);
50}
51
52void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
53    GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
54    handler->run();
55    delete handler;
56    pthread_exit(NULL);
57    return NULL;
58}
59
60// Sends 4 bytes of big-endian length, followed by the data.
61// Returns true on success.
62static bool sendLenAndData(SocketClient *c, const int len, const void* data) {
63    uint32_t len_be = htonl(len);
64    return c->sendData(&len_be, 4) == 0 &&
65        (len == 0 || c->sendData(data, len) == 0);
66}
67
68void DnsProxyListener::GetAddrInfoHandler::run() {
69    if (DBG) {
70        LOGD("GetAddrInfoHandler, now for %s / %s", mHost, mService);
71    }
72
73    struct addrinfo* result = NULL;
74    int rv = getaddrinfo(mHost, mService, mHints, &result);
75    bool success = (mClient->sendData(&rv, sizeof(rv)) == 0);
76    if (rv == 0) {
77        struct addrinfo* ai = result;
78        while (ai && success) {
79            success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
80                && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
81                && sendLenAndData(mClient,
82                                  ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
83                                  ai->ai_canonname);
84            ai = ai->ai_next;
85        }
86        success = success && sendLenAndData(mClient, 0, "");
87    }
88    if (result) {
89        freeaddrinfo(result);
90    }
91    if (!success) {
92        LOGW("Error writing DNS result to client");
93    }
94}
95
96DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
97    NetdCommand("getaddrinfo") {
98}
99
100int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
101                                            int argc, char **argv) {
102    if (argc != 7) {
103        LOGW("Invalid number of arguments to getaddrinfo");
104        return 0;
105    }
106
107    char* name = argv[1];
108    if (strcmp("^", name) == 0) {
109        name = NULL;
110    } else {
111        name = strdup(name);
112    }
113
114    char* service = argv[2];
115    if (strcmp("^", service) == 0) {
116        service = NULL;
117    } else {
118        service = strdup(service);
119    }
120
121    struct addrinfo* hints = NULL;
122    int ai_flags = atoi(argv[3]);
123    int ai_family = atoi(argv[4]);
124    int ai_socktype = atoi(argv[5]);
125    int ai_protocol = atoi(argv[6]);
126    if (ai_flags != -1 || ai_family != -1 ||
127        ai_socktype != -1 || ai_protocol != -1) {
128        hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
129        hints->ai_flags = ai_flags;
130        hints->ai_family = ai_family;
131        hints->ai_socktype = ai_socktype;
132        hints->ai_protocol = ai_protocol;
133    }
134
135    if (DBG) {
136        LOGD("GetAddrInfoHandler for %s / %s",
137             name ? name : "[nullhost]",
138             service ? service : "[nullservice]");
139    }
140
141    DnsProxyListener::GetAddrInfoHandler* handler =
142        new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints);
143    handler->start();
144
145
146    return 0;
147}
148
149/*******************************************************
150 *                  GetHostByAddr                       *
151 *******************************************************/
152DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
153        NetdCommand("gethostbyaddr") {
154}
155
156int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
157                                            int argc, char **argv) {
158    if (argc != 4) {
159        LOGW("Invalid number of arguments to gethostbyaddr");
160        return 0;
161    }
162
163    char* addr = argv[1];
164    addr = strdup(addr);
165
166    int addrLen = atoi(argv[2]);
167    int addrFamily = atoi(argv[3]);
168
169    DnsProxyListener::GetHostByAddrHandler* handler =
170            new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily);
171    handler->start();
172
173    return 0;
174}
175
176DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
177    free(mAddress);
178}
179
180void DnsProxyListener::GetHostByAddrHandler::start() {
181    pthread_create(&mThread, NULL,
182                   DnsProxyListener::GetHostByAddrHandler::threadStart, this);
183}
184
185void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
186    GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
187    handler->run();
188    delete handler;
189    pthread_exit(NULL);
190    return NULL;
191}
192
193void DnsProxyListener::GetHostByAddrHandler::run() {
194    if (DBG) {
195        LOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
196        if (mAddress) {
197            LOGD("mAdress %u.%u.%u.%u mAdressLen %d, mAddressFamily %d",
198                    mAddress[0], mAddress[1], mAddress[2], mAddress[3],
199                    mAddressLen, mAddressFamily);
200        }
201        else {
202            LOGD("mAddress = NULL");
203        }
204    }
205
206    struct hostent* hp;
207
208    hp = gethostbyaddr(mAddress, mAddressLen, mAddressFamily);
209
210    if (DBG) {
211        LOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
212                hp ? "success" : strerror(errno),
213                (hp && hp->h_name) ? hp->h_name: "null",
214                (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
215    }
216
217    bool success = sendLenAndData(mClient, (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0,
218            (hp && hp->h_name) ? hp->h_name : "");
219
220    if (!success) {
221        LOGW("GetHostByAddrHandler: Error writing DNS result to client\n");
222    }
223}
224