1007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick/*
2007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project
3007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick *
4007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License");
5007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * you may not use this file except in compliance with the License.
6007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * You may obtain a copy of the License at
7007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick *
8007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick *      http://www.apache.org/licenses/LICENSE-2.0
9007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick *
10007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software
11007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS,
12007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * See the License for the specific language governing permissions and
14007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick * limitations under the License.
15007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick */
16007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
17007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <arpa/inet.h>
18007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <dirent.h>
19007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <errno.h>
20007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <linux/if.h>
21007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <netdb.h>
22007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <netinet/in.h>
23007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <stdlib.h>
24007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <sys/socket.h>
25007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <sys/types.h>
26ff2c0d8c13457e43f0d4bf06d3177271aac104c1Olivier Bailly#include <string.h>
270cdb680c7648b0f48e6513926cb0a06d290a5cbeJurijs Oniscuks#include <pthread.h>
28007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
29007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#define LOG_TAG "DnsProxyListener"
30007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#define DBG 0
31007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
32007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <cutils/log.h>
33007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include <sysutils/SocketClient.h>
34007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
35007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick#include "DnsProxyListener.h"
36ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun#include "ResponseCode.h"
37007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
38007e987fee7e815e0c4bc820f434a632b7a69a9dBrad FitzpatrickDnsProxyListener::DnsProxyListener() :
39007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                 FrameworkListener("dnsproxyd") {
40007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    registerCmd(new GetAddrInfoCmd());
411dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    registerCmd(new GetHostByAddrCmd());
42007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
43007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
44007e987fee7e815e0c4bc820f434a632b7a69a9dBrad FitzpatrickDnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
45007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    free(mHost);
46007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    free(mService);
47007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    free(mHints);
48007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
49007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
50007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrickvoid DnsProxyListener::GetAddrInfoHandler::start() {
510cdb680c7648b0f48e6513926cb0a06d290a5cbeJurijs Oniscuks    pthread_t thread;
520cdb680c7648b0f48e6513926cb0a06d290a5cbeJurijs Oniscuks    pthread_create(&thread, NULL,
53007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                   DnsProxyListener::GetAddrInfoHandler::threadStart, this);
54007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
55007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
56007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrickvoid* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
57007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
58007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    handler->run();
59007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    delete handler;
60007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    pthread_exit(NULL);
61007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    return NULL;
62007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
63007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
64007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick// Sends 4 bytes of big-endian length, followed by the data.
65007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick// Returns true on success.
66007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrickstatic bool sendLenAndData(SocketClient *c, const int len, const void* data) {
67007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    uint32_t len_be = htonl(len);
68e6a3a58d6aa4b551e38e1e19fdfea2c0d2841b85Brad Fitzpatrick    return c->sendData(&len_be, 4) == 0 &&
69e6a3a58d6aa4b551e38e1e19fdfea2c0d2841b85Brad Fitzpatrick        (len == 0 || c->sendData(data, len) == 0);
70007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
71007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
72007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrickvoid DnsProxyListener::GetAddrInfoHandler::run() {
73007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (DBG) {
747b984e3f7e724f8a3547a707210319f3d479f261Steve Block        ALOGD("GetAddrInfoHandler, now for %s / %s", mHost, mService);
75007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
76007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
77007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    struct addrinfo* result = NULL;
78ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    uint32_t rv = getaddrinfo(mHost, mService, mHints, &result);
79ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    if (rv) {
80ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        // getaddrinfo failed
8140e0c13d6b77a9260b6da4c9e11cca8a5f994ea2Selim Gurun        mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
82ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    } else {
83ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
84007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        struct addrinfo* ai = result;
85007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        while (ai && success) {
86007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick            success = sendLenAndData(mClient, sizeof(struct addrinfo), ai)
87007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                && sendLenAndData(mClient, ai->ai_addrlen, ai->ai_addr)
88007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                && sendLenAndData(mClient,
89007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                                  ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0,
90007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                                  ai->ai_canonname);
91007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick            ai = ai->ai_next;
92007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        }
93007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        success = success && sendLenAndData(mClient, 0, "");
94ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        if (!success) {
95ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun            ALOGW("Error writing DNS result to client");
96ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        }
97007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
98007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (result) {
99007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        freeaddrinfo(result);
100007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
10134ffd85aed3838d53bd1136c751a7825de1940e0Brad Fitzpatrick    mClient->decRef();
102007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
103007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
104007e987fee7e815e0c4bc820f434a632b7a69a9dBrad FitzpatrickDnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd() :
105007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    NetdCommand("getaddrinfo") {
106007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
107007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
108007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrickint DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
109007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick                                            int argc, char **argv) {
1100475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    if (DBG) {
1110475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        for (int i = 0; i < argc; i++) {
1127b984e3f7e724f8a3547a707210319f3d479f261Steve Block            ALOGD("argv[%i]=%s", i, argv[i]);
1130475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        }
1140475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    }
115007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (argc != 7) {
116ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        char* msg = NULL;
117ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
118ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        ALOGW("%s", msg);
119ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
120ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        free(msg);
1210475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        return -1;
122007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
123007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
124007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    char* name = argv[1];
125007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (strcmp("^", name) == 0) {
126007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        name = NULL;
127007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    } else {
128007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        name = strdup(name);
129007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
130007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
131007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    char* service = argv[2];
132007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (strcmp("^", service) == 0) {
133007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        service = NULL;
134007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    } else {
135007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        service = strdup(service);
136007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
137007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
138007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    struct addrinfo* hints = NULL;
139007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    int ai_flags = atoi(argv[3]);
140007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    int ai_family = atoi(argv[4]);
141007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    int ai_socktype = atoi(argv[5]);
142007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    int ai_protocol = atoi(argv[6]);
143007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (ai_flags != -1 || ai_family != -1 ||
144007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        ai_socktype != -1 || ai_protocol != -1) {
145007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
146007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        hints->ai_flags = ai_flags;
147007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        hints->ai_family = ai_family;
148007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        hints->ai_socktype = ai_socktype;
149007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        hints->ai_protocol = ai_protocol;
150007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
151007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
152007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    if (DBG) {
1537b984e3f7e724f8a3547a707210319f3d479f261Steve Block        ALOGD("GetAddrInfoHandler for %s / %s",
154007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick             name ? name : "[nullhost]",
155007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick             service ? service : "[nullservice]");
156007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    }
157007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
15834ffd85aed3838d53bd1136c751a7825de1940e0Brad Fitzpatrick    cli->incRef();
159007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    DnsProxyListener::GetAddrInfoHandler* handler =
160007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick        new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints);
161007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    handler->start();
162007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick
163007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick    return 0;
164007e987fee7e815e0c4bc820f434a632b7a69a9dBrad Fitzpatrick}
1651dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
1661dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk/*******************************************************
1671dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk *                  GetHostByAddr                       *
1681dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk *******************************************************/
1691dbd6cf148ea3fab57ec0644c336e94c78a488beMattias FalkDnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd() :
1701dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk        NetdCommand("gethostbyaddr") {
1711dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
1721dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
1731dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falkint DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
1741dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk                                            int argc, char **argv) {
1750475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    if (DBG) {
1760475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        for (int i = 0; i < argc; i++) {
1777b984e3f7e724f8a3547a707210319f3d479f261Steve Block            ALOGD("argv[%i]=%s", i, argv[i]);
1780475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        }
1790475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    }
180ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun
1811dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    if (argc != 4) {
182ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        char* msg = NULL;
183ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
184ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        ALOGW("%s", msg);
185ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
186ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        free(msg);
1870475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        return -1;
1881dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    }
1891dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
1900475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    char* addrStr = argv[1];
1911dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    int addrLen = atoi(argv[2]);
1921dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    int addrFamily = atoi(argv[3]);
1931dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
1940475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    void* addr = malloc(sizeof(struct in6_addr));
1950475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    errno = 0;
1960475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    int result = inet_pton(addrFamily, addrStr, addr);
1970475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    if (result <= 0) {
198ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        char* msg = NULL;
199ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
200ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        ALOGW("%s", msg);
201ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        cli->sendMsg(ResponseCode::OperationFailed, msg, false);
2020475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        free(addr);
203ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        free(msg);
2040475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom        return -1;
2050475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    }
2060475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom
20734ffd85aed3838d53bd1136c751a7825de1940e0Brad Fitzpatrick    cli->incRef();
2081dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    DnsProxyListener::GetHostByAddrHandler* handler =
2091dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk            new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily);
2101dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    handler->start();
2111dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2121dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    return 0;
2131dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
2141dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2151dbd6cf148ea3fab57ec0644c336e94c78a488beMattias FalkDnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
2161dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    free(mAddress);
2171dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
2181dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2191dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falkvoid DnsProxyListener::GetHostByAddrHandler::start() {
2200cdb680c7648b0f48e6513926cb0a06d290a5cbeJurijs Oniscuks    pthread_t thread;
2210cdb680c7648b0f48e6513926cb0a06d290a5cbeJurijs Oniscuks    pthread_create(&thread, NULL,
2221dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk                   DnsProxyListener::GetHostByAddrHandler::threadStart, this);
2231dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
2241dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2251dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falkvoid* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
2261dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
2271dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    handler->run();
2281dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    delete handler;
2291dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    pthread_exit(NULL);
2301dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    return NULL;
2311dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
2321dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2331dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falkvoid DnsProxyListener::GetHostByAddrHandler::run() {
2341dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    if (DBG) {
2357b984e3f7e724f8a3547a707210319f3d479f261Steve Block        ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
2361dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    }
2371dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2381dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    struct hostent* hp;
2391dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2400475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
2410475ae98cd8f45de815d67d1966edaf5997be9a9Brian Carlstrom    hp = gethostbyaddr((char*)mAddress, mAddressLen, mAddressFamily);
2421dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
2431dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    if (DBG) {
2447b984e3f7e724f8a3547a707210319f3d479f261Steve Block        ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %d\n",
2451dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk                hp ? "success" : strerror(errno),
2461dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk                (hp && hp->h_name) ? hp->h_name: "null",
2471dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk                (hp && hp->h_name) ? strlen(hp->h_name)+ 1 : 0);
2481dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    }
2491dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
250ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    bool failed = true;
251ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    if (hp) {
252ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyQueryResult,
253ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun                                        hp->h_name ? hp->h_name : "",
254ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun                                        hp->h_name ? strlen(hp->h_name)+ 1 : 0);
255ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    } else {
256ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        uint32_t error = h_errno;
257ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun        failed = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed,
25840e0c13d6b77a9260b6da4c9e11cca8a5f994ea2Selim Gurun                                        &error, sizeof(error));
259ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    }
2601dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk
261ddb34755fb54882b3ece8d4919593e26a2c1cfcbSelim Gurun    if (failed) {
2620e76b761a1514d5182675dd7b7d33725f62d6bc5Steve Block        ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");
2631dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk    }
26434ffd85aed3838d53bd1136c751a7825de1940e0Brad Fitzpatrick    mClient->decRef();
2651dbd6cf148ea3fab57ec0644c336e94c78a488beMattias Falk}
266