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 #include <resolv_netid.h>
31 #define LOG_TAG "DnsProxyListener"
35 #include <cutils/log.h>
36 #include <sysutils/SocketClient.h>
39 #include "DnsProxyListener.h"
40 #include "NetdConstants.h"
41 #include "NetworkController.h"
42 #include "ResponseCode.h"
44 DnsProxyListener::DnsProxyListener(const NetworkController* netCtrl) :
45 FrameworkListener("dnsproxyd"), mNetCtrl(netCtrl) {
46 registerCmd(new GetAddrInfoCmd(this));
47 registerCmd(new GetHostByAddrCmd(this));
48 registerCmd(new GetHostByNameCmd(this));
51 DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(
52 SocketClient *c, char* host, char* service, struct addrinfo* hints,
53 const struct android_net_context& netcontext)
58 mNetContext(netcontext) {
61 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
67 void DnsProxyListener::GetAddrInfoHandler::start() {
69 pthread_create(&thread, NULL,
70 DnsProxyListener::GetAddrInfoHandler::threadStart, this);
71 pthread_detach(thread);
74 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
75 GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
82 static bool sendBE32(SocketClient* c, uint32_t data) {
83 uint32_t be_data = htonl(data);
84 return c->sendData(&be_data, sizeof(be_data)) == 0;
87 // Sends 4 bytes of big-endian length, followed by the data.
88 // Returns true on success.
89 static bool sendLenAndData(SocketClient* c, const int len, const void* data) {
90 return sendBE32(c, len) && (len == 0 || c->sendData(data, len) == 0);
93 // Returns true on success
94 static bool sendhostent(SocketClient *c, struct hostent *hp) {
97 if (hp->h_name != NULL) {
98 success &= sendLenAndData(c, strlen(hp->h_name)+1, hp->h_name);
100 success &= sendLenAndData(c, 0, "") == 0;
103 for (i=0; hp->h_aliases[i] != NULL; i++) {
104 success &= sendLenAndData(c, strlen(hp->h_aliases[i])+1, hp->h_aliases[i]);
106 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
108 uint32_t buf = htonl(hp->h_addrtype);
109 success &= c->sendData(&buf, sizeof(buf)) == 0;
111 buf = htonl(hp->h_length);
112 success &= c->sendData(&buf, sizeof(buf)) == 0;
114 for (i=0; hp->h_addr_list[i] != NULL; i++) {
115 success &= sendLenAndData(c, 16, hp->h_addr_list[i]);
117 success &= sendLenAndData(c, 0, ""); // null to indicate we're done
121 static bool sendaddrinfo(SocketClient* c, struct addrinfo* ai) {
123 // int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
124 // int ai_family; /* PF_xxx */
125 // int ai_socktype; /* SOCK_xxx */
126 // int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
127 // socklen_t ai_addrlen; /* length of ai_addr */
128 // char *ai_canonname; /* canonical name for hostname */
129 // struct sockaddr *ai_addr; /* binary address */
130 // struct addrinfo *ai_next; /* next structure in linked list */
133 // Write the struct piece by piece because we might be a 64-bit netd
134 // talking to a 32-bit process.
136 sendBE32(c, ai->ai_flags) &&
137 sendBE32(c, ai->ai_family) &&
138 sendBE32(c, ai->ai_socktype) &&
139 sendBE32(c, ai->ai_protocol);
144 // ai_addrlen and ai_addr.
145 if (!sendLenAndData(c, ai->ai_addrlen, ai->ai_addr)) {
149 // strlen(ai_canonname) and ai_canonname.
150 if (!sendLenAndData(c, ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0, ai->ai_canonname)) {
157 void DnsProxyListener::GetAddrInfoHandler::run() {
159 ALOGD("GetAddrInfoHandler, now for %s / %s / {%u,%u,%u,%u,%u}", mHost, mService,
160 mNetContext.app_netid, mNetContext.app_mark,
161 mNetContext.dns_netid, mNetContext.dns_mark,
165 struct addrinfo* result = NULL;
166 uint32_t rv = android_getaddrinfofornetcontext(mHost, mService, mHints, &mNetContext, &result);
168 // getaddrinfo failed
169 mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
171 bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
172 struct addrinfo* ai = result;
173 while (ai && success) {
174 success = sendBE32(mClient, 1) && sendaddrinfo(mClient, ai);
177 success = success && sendBE32(mClient, 0);
179 ALOGW("Error writing DNS result to client");
183 freeaddrinfo(result);
188 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd(const DnsProxyListener* dnsProxyListener) :
189 NetdCommand("getaddrinfo"),
190 mDnsProxyListener(dnsProxyListener) {
193 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
194 int argc, char **argv) {
196 for (int i = 0; i < argc; i++) {
197 ALOGD("argv[%i]=%s", i, argv[i]);
202 asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
204 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
209 char* name = argv[1];
210 if (strcmp("^", name) == 0) {
216 char* service = argv[2];
217 if (strcmp("^", service) == 0) {
220 service = strdup(service);
223 struct addrinfo* hints = NULL;
224 int ai_flags = atoi(argv[3]);
225 int ai_family = atoi(argv[4]);
226 int ai_socktype = atoi(argv[5]);
227 int ai_protocol = atoi(argv[6]);
228 unsigned netId = strtoul(argv[7], NULL, 10);
229 uid_t uid = cli->getUid();
231 struct android_net_context netcontext;
232 mDnsProxyListener->mNetCtrl->getNetworkContext(netId, uid, &netcontext);
234 if (ai_flags != -1 || ai_family != -1 ||
235 ai_socktype != -1 || ai_protocol != -1) {
236 hints = (struct addrinfo*) calloc(1, sizeof(struct addrinfo));
237 hints->ai_flags = ai_flags;
238 hints->ai_family = ai_family;
239 hints->ai_socktype = ai_socktype;
240 hints->ai_protocol = ai_protocol;
244 ALOGD("GetAddrInfoHandler for %s / %s / {%u,%u,%u,%u,%u}",
245 name ? name : "[nullhost]",
246 service ? service : "[nullservice]",
247 netcontext.app_netid, netcontext.app_mark,
248 netcontext.dns_netid, netcontext.dns_mark,
253 DnsProxyListener::GetAddrInfoHandler* handler =
254 new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netcontext);
260 /*******************************************************
262 *******************************************************/
263 DnsProxyListener::GetHostByNameCmd::GetHostByNameCmd(const DnsProxyListener* dnsProxyListener) :
264 NetdCommand("gethostbyname"),
265 mDnsProxyListener(dnsProxyListener) {
268 int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
269 int argc, char **argv) {
271 for (int i = 0; i < argc; i++) {
272 ALOGD("argv[%i]=%s", i, argv[i]);
277 asprintf(&msg, "Invalid number of arguments to gethostbyname: %i", argc);
279 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
284 uid_t uid = cli->getUid();
285 unsigned netId = strtoul(argv[1], NULL, 10);
286 char* name = argv[2];
287 int af = atoi(argv[3]);
289 if (strcmp(name, "^") == 0) {
295 uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
298 DnsProxyListener::GetHostByNameHandler* handler =
299 new DnsProxyListener::GetHostByNameHandler(cli, name, af, netId, mark);
305 DnsProxyListener::GetHostByNameHandler::GetHostByNameHandler(SocketClient* c,
317 DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
321 void DnsProxyListener::GetHostByNameHandler::start() {
323 pthread_create(&thread, NULL,
324 DnsProxyListener::GetHostByNameHandler::threadStart, this);
325 pthread_detach(thread);
328 void* DnsProxyListener::GetHostByNameHandler::threadStart(void* obj) {
329 GetHostByNameHandler* handler = reinterpret_cast<GetHostByNameHandler*>(obj);
336 void DnsProxyListener::GetHostByNameHandler::run() {
338 ALOGD("DnsProxyListener::GetHostByNameHandler::run\n");
343 hp = android_gethostbynamefornet(mName, mAf, mNetId, mMark);
346 ALOGD("GetHostByNameHandler::run gethostbyname errno: %s hp->h_name = %s, name_len = %zu\n",
347 hp ? "success" : strerror(errno),
348 (hp && hp->h_name) ? hp->h_name : "null",
349 (hp && hp->h_name) ? strlen(hp->h_name) + 1 : 0);
354 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
355 success &= sendhostent(mClient, hp);
357 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
361 ALOGW("GetHostByNameHandler: Error writing DNS result to client\n");
367 /*******************************************************
369 *******************************************************/
370 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener) :
371 NetdCommand("gethostbyaddr"),
372 mDnsProxyListener(dnsProxyListener) {
375 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
376 int argc, char **argv) {
378 for (int i = 0; i < argc; i++) {
379 ALOGD("argv[%i]=%s", i, argv[i]);
384 asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
386 cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
391 char* addrStr = argv[1];
392 int addrLen = atoi(argv[2]);
393 int addrFamily = atoi(argv[3]);
394 uid_t uid = cli->getUid();
395 unsigned netId = strtoul(argv[4], NULL, 10);
397 void* addr = malloc(sizeof(struct in6_addr));
399 int result = inet_pton(addrFamily, addrStr, addr);
402 asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
404 cli->sendMsg(ResponseCode::OperationFailed, msg, false);
410 uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
413 DnsProxyListener::GetHostByAddrHandler* handler =
414 new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily, netId, mark);
420 DnsProxyListener::GetHostByAddrHandler::GetHostByAddrHandler(SocketClient* c,
428 mAddressLen(addressLen),
429 mAddressFamily(addressFamily),
434 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
438 void DnsProxyListener::GetHostByAddrHandler::start() {
440 pthread_create(&thread, NULL,
441 DnsProxyListener::GetHostByAddrHandler::threadStart, this);
442 pthread_detach(thread);
445 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
446 GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
453 void DnsProxyListener::GetHostByAddrHandler::run() {
455 ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
459 // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
460 hp = android_gethostbyaddrfornet((char*)mAddress, mAddressLen, mAddressFamily, mNetId, mMark);
463 ALOGD("GetHostByAddrHandler::run gethostbyaddr errno: %s hp->h_name = %s, name_len = %zu\n",
464 hp ? "success" : strerror(errno),
465 (hp && hp->h_name) ? hp->h_name : "null",
466 (hp && hp->h_name) ? strlen(hp->h_name) + 1 : 0);
471 success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
472 success &= sendhostent(mClient, hp);
474 success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
478 ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");