DnsProxyListener.cpp revision ff2c0d8c13457e43f0d4bf06d3177271aac104c1
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#include <string.h>
27
28#define LOG_TAG "DnsProxyListener"
29#define DBG 0
30
31#include <cutils/log.h>
32#include <sysutils/SocketClient.h>
33
34#include "DnsProxyListener.h"
35
36DnsProxyListener::DnsProxyListener() :
37                 FrameworkListener("dnsproxyd") {
38    registerCmd(new GetAddrInfoCmd());
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