OSDN Git Service

Switch to ifc_add_address() for setcfg
[android-x86/system-netd.git] / server / DnsProxyListener.cpp
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 #include <pthread.h>
28 #include <resolv_netid.h>
29 #include <net/if.h>
30
31 #define LOG_TAG "DnsProxyListener"
32 #define DBG 0
33 #define VDBG 0
34
35 #include <cutils/log.h>
36 #include <sysutils/SocketClient.h>
37
38 #include "Fwmark.h"
39 #include "DnsProxyListener.h"
40 #include "NetdConstants.h"
41 #include "NetworkController.h"
42 #include "ResponseCode.h"
43
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));
49 }
50
51 DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(
52         SocketClient *c, char* host, char* service, struct addrinfo* hints,
53         const struct android_net_context& netcontext)
54         : mClient(c),
55           mHost(host),
56           mService(service),
57           mHints(hints),
58           mNetContext(netcontext) {
59 }
60
61 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
62     free(mHost);
63     free(mService);
64     free(mHints);
65 }
66
67 void DnsProxyListener::GetAddrInfoHandler::start() {
68     pthread_t thread;
69     pthread_create(&thread, NULL,
70                    DnsProxyListener::GetAddrInfoHandler::threadStart, this);
71     pthread_detach(thread);
72 }
73
74 void* DnsProxyListener::GetAddrInfoHandler::threadStart(void* obj) {
75     GetAddrInfoHandler* handler = reinterpret_cast<GetAddrInfoHandler*>(obj);
76     handler->run();
77     delete handler;
78     pthread_exit(NULL);
79     return NULL;
80 }
81
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;
85 }
86
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);
91 }
92
93 // Returns true on success
94 static bool sendhostent(SocketClient *c, struct hostent *hp) {
95     bool success = true;
96     int i;
97     if (hp->h_name != NULL) {
98         success &= sendLenAndData(c, strlen(hp->h_name)+1, hp->h_name);
99     } else {
100         success &= sendLenAndData(c, 0, "") == 0;
101     }
102
103     for (i=0; hp->h_aliases[i] != NULL; i++) {
104         success &= sendLenAndData(c, strlen(hp->h_aliases[i])+1, hp->h_aliases[i]);
105     }
106     success &= sendLenAndData(c, 0, ""); // null to indicate we're done
107
108     uint32_t buf = htonl(hp->h_addrtype);
109     success &= c->sendData(&buf, sizeof(buf)) == 0;
110
111     buf = htonl(hp->h_length);
112     success &= c->sendData(&buf, sizeof(buf)) == 0;
113
114     for (i=0; hp->h_addr_list[i] != NULL; i++) {
115         success &= sendLenAndData(c, 16, hp->h_addr_list[i]);
116     }
117     success &= sendLenAndData(c, 0, ""); // null to indicate we're done
118     return success;
119 }
120
121 static bool sendaddrinfo(SocketClient* c, struct addrinfo* ai) {
122     // struct addrinfo {
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 */
131     // };
132
133     // Write the struct piece by piece because we might be a 64-bit netd
134     // talking to a 32-bit process.
135     bool success =
136             sendBE32(c, ai->ai_flags) &&
137             sendBE32(c, ai->ai_family) &&
138             sendBE32(c, ai->ai_socktype) &&
139             sendBE32(c, ai->ai_protocol);
140     if (!success) {
141         return false;
142     }
143
144     // ai_addrlen and ai_addr.
145     if (!sendLenAndData(c, ai->ai_addrlen, ai->ai_addr)) {
146         return false;
147     }
148
149     // strlen(ai_canonname) and ai_canonname.
150     if (!sendLenAndData(c, ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0, ai->ai_canonname)) {
151         return false;
152     }
153
154     return true;
155 }
156
157 void DnsProxyListener::GetAddrInfoHandler::run() {
158     if (DBG) {
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,
162                 mNetContext.uid);
163     }
164
165     struct addrinfo* result = NULL;
166     uint32_t rv = android_getaddrinfofornetcontext(mHost, mService, mHints, &mNetContext, &result);
167     if (rv) {
168         // getaddrinfo failed
169         mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
170     } else {
171         bool success = !mClient->sendCode(ResponseCode::DnsProxyQueryResult);
172         struct addrinfo* ai = result;
173         while (ai && success) {
174             success = sendBE32(mClient, 1) && sendaddrinfo(mClient, ai);
175             ai = ai->ai_next;
176         }
177         success = success && sendBE32(mClient, 0);
178         if (!success) {
179             ALOGW("Error writing DNS result to client");
180         }
181     }
182     if (result) {
183         freeaddrinfo(result);
184     }
185     mClient->decRef();
186 }
187
188 DnsProxyListener::GetAddrInfoCmd::GetAddrInfoCmd(const DnsProxyListener* dnsProxyListener) :
189     NetdCommand("getaddrinfo"),
190     mDnsProxyListener(dnsProxyListener) {
191 }
192
193 int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
194                                             int argc, char **argv) {
195     if (DBG) {
196         for (int i = 0; i < argc; i++) {
197             ALOGD("argv[%i]=%s", i, argv[i]);
198         }
199     }
200     if (argc != 8) {
201         char* msg = NULL;
202         asprintf( &msg, "Invalid number of arguments to getaddrinfo: %i", argc);
203         ALOGW("%s", msg);
204         cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
205         free(msg);
206         return -1;
207     }
208
209     char* name = argv[1];
210     if (strcmp("^", name) == 0) {
211         name = NULL;
212     } else {
213         name = strdup(name);
214     }
215
216     char* service = argv[2];
217     if (strcmp("^", service) == 0) {
218         service = NULL;
219     } else {
220         service = strdup(service);
221     }
222
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();
230
231     struct android_net_context netcontext;
232     mDnsProxyListener->mNetCtrl->getNetworkContext(netId, uid, &netcontext);
233
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;
241     }
242
243     if (DBG) {
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,
249              netcontext.uid);
250     }
251
252     cli->incRef();
253     DnsProxyListener::GetAddrInfoHandler* handler =
254             new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netcontext);
255     handler->start();
256
257     return 0;
258 }
259
260 /*******************************************************
261  *                  GetHostByName                      *
262  *******************************************************/
263 DnsProxyListener::GetHostByNameCmd::GetHostByNameCmd(const DnsProxyListener* dnsProxyListener) :
264       NetdCommand("gethostbyname"),
265       mDnsProxyListener(dnsProxyListener) {
266 }
267
268 int DnsProxyListener::GetHostByNameCmd::runCommand(SocketClient *cli,
269                                             int argc, char **argv) {
270     if (DBG) {
271         for (int i = 0; i < argc; i++) {
272             ALOGD("argv[%i]=%s", i, argv[i]);
273         }
274     }
275     if (argc != 4) {
276         char* msg = NULL;
277         asprintf(&msg, "Invalid number of arguments to gethostbyname: %i", argc);
278         ALOGW("%s", msg);
279         cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
280         free(msg);
281         return -1;
282     }
283
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]);
288
289     if (strcmp(name, "^") == 0) {
290         name = NULL;
291     } else {
292         name = strdup(name);
293     }
294
295     uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
296
297     cli->incRef();
298     DnsProxyListener::GetHostByNameHandler* handler =
299             new DnsProxyListener::GetHostByNameHandler(cli, name, af, netId, mark);
300     handler->start();
301
302     return 0;
303 }
304
305 DnsProxyListener::GetHostByNameHandler::GetHostByNameHandler(SocketClient* c,
306                                                              char* name,
307                                                              int af,
308                                                              unsigned netId,
309                                                              uint32_t mark)
310         : mClient(c),
311           mName(name),
312           mAf(af),
313           mNetId(netId),
314           mMark(mark) {
315 }
316
317 DnsProxyListener::GetHostByNameHandler::~GetHostByNameHandler() {
318     free(mName);
319 }
320
321 void DnsProxyListener::GetHostByNameHandler::start() {
322     pthread_t thread;
323     pthread_create(&thread, NULL,
324             DnsProxyListener::GetHostByNameHandler::threadStart, this);
325     pthread_detach(thread);
326 }
327
328 void* DnsProxyListener::GetHostByNameHandler::threadStart(void* obj) {
329     GetHostByNameHandler* handler = reinterpret_cast<GetHostByNameHandler*>(obj);
330     handler->run();
331     delete handler;
332     pthread_exit(NULL);
333     return NULL;
334 }
335
336 void DnsProxyListener::GetHostByNameHandler::run() {
337     if (DBG) {
338         ALOGD("DnsProxyListener::GetHostByNameHandler::run\n");
339     }
340
341     struct hostent* hp;
342
343     hp = android_gethostbynamefornet(mName, mAf, mNetId, mMark);
344
345     if (DBG) {
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);
350     }
351
352     bool success = true;
353     if (hp) {
354         success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
355         success &= sendhostent(mClient, hp);
356     } else {
357         success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
358     }
359
360     if (!success) {
361         ALOGW("GetHostByNameHandler: Error writing DNS result to client\n");
362     }
363     mClient->decRef();
364 }
365
366
367 /*******************************************************
368  *                  GetHostByAddr                      *
369  *******************************************************/
370 DnsProxyListener::GetHostByAddrCmd::GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener) :
371         NetdCommand("gethostbyaddr"),
372         mDnsProxyListener(dnsProxyListener) {
373 }
374
375 int DnsProxyListener::GetHostByAddrCmd::runCommand(SocketClient *cli,
376                                             int argc, char **argv) {
377     if (DBG) {
378         for (int i = 0; i < argc; i++) {
379             ALOGD("argv[%i]=%s", i, argv[i]);
380         }
381     }
382     if (argc != 5) {
383         char* msg = NULL;
384         asprintf(&msg, "Invalid number of arguments to gethostbyaddr: %i", argc);
385         ALOGW("%s", msg);
386         cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
387         free(msg);
388         return -1;
389     }
390
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);
396
397     void* addr = malloc(sizeof(struct in6_addr));
398     errno = 0;
399     int result = inet_pton(addrFamily, addrStr, addr);
400     if (result <= 0) {
401         char* msg = NULL;
402         asprintf(&msg, "inet_pton(\"%s\") failed %s", addrStr, strerror(errno));
403         ALOGW("%s", msg);
404         cli->sendMsg(ResponseCode::OperationFailed, msg, false);
405         free(addr);
406         free(msg);
407         return -1;
408     }
409
410     uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
411
412     cli->incRef();
413     DnsProxyListener::GetHostByAddrHandler* handler =
414             new DnsProxyListener::GetHostByAddrHandler(cli, addr, addrLen, addrFamily, netId, mark);
415     handler->start();
416
417     return 0;
418 }
419
420 DnsProxyListener::GetHostByAddrHandler::GetHostByAddrHandler(SocketClient* c,
421                                                              void* address,
422                                                              int   addressLen,
423                                                              int   addressFamily,
424                                                              unsigned netId,
425                                                              uint32_t mark)
426         : mClient(c),
427           mAddress(address),
428           mAddressLen(addressLen),
429           mAddressFamily(addressFamily),
430           mNetId(netId),
431           mMark(mark) {
432 }
433
434 DnsProxyListener::GetHostByAddrHandler::~GetHostByAddrHandler() {
435     free(mAddress);
436 }
437
438 void DnsProxyListener::GetHostByAddrHandler::start() {
439     pthread_t thread;
440     pthread_create(&thread, NULL,
441                    DnsProxyListener::GetHostByAddrHandler::threadStart, this);
442     pthread_detach(thread);
443 }
444
445 void* DnsProxyListener::GetHostByAddrHandler::threadStart(void* obj) {
446     GetHostByAddrHandler* handler = reinterpret_cast<GetHostByAddrHandler*>(obj);
447     handler->run();
448     delete handler;
449     pthread_exit(NULL);
450     return NULL;
451 }
452
453 void DnsProxyListener::GetHostByAddrHandler::run() {
454     if (DBG) {
455         ALOGD("DnsProxyListener::GetHostByAddrHandler::run\n");
456     }
457     struct hostent* hp;
458
459     // NOTE gethostbyaddr should take a void* but bionic thinks it should be char*
460     hp = android_gethostbyaddrfornet((char*)mAddress, mAddressLen, mAddressFamily, mNetId, mMark);
461
462     if (DBG) {
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);
467     }
468
469     bool success = true;
470     if (hp) {
471         success = mClient->sendCode(ResponseCode::DnsProxyQueryResult) == 0;
472         success &= sendhostent(mClient, hp);
473     } else {
474         success = mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, NULL, 0) == 0;
475     }
476
477     if (!success) {
478         ALOGW("GetHostByAddrHandler: Error writing DNS result to client\n");
479     }
480     mClient->decRef();
481 }