OSDN Git Service

Use proper socket mark for DNS resolution.
[android-x86/system-netd.git] / DnsProxyListener.h
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 #ifndef _DNSPROXYLISTENER_H__
18 #define _DNSPROXYLISTENER_H__
19
20 #include <sysutils/FrameworkListener.h>
21
22 #include "NetdCommand.h"
23
24 class NetworkController;
25 class PermissionsController;
26
27 class DnsProxyListener : public FrameworkListener {
28 public:
29     DnsProxyListener(const NetworkController* netCtrl, const PermissionsController* permCtrl);
30     virtual ~DnsProxyListener() {}
31
32 private:
33     const NetworkController *mNetCtrl;
34     const PermissionsController *mPermCtrl;
35     class GetAddrInfoCmd : public NetdCommand {
36     public:
37         GetAddrInfoCmd(const DnsProxyListener* dnsProxyListener);
38         virtual ~GetAddrInfoCmd() {}
39         int runCommand(SocketClient *c, int argc, char** argv);
40     private:
41         const DnsProxyListener* mDnsProxyListener;
42     };
43
44     class GetAddrInfoHandler {
45     public:
46         // Note: All of host, service, and hints may be NULL
47         GetAddrInfoHandler(SocketClient *c,
48                            char* host,
49                            char* service,
50                            struct addrinfo* hints,
51                            unsigned netId,
52                            uint32_t mark);
53         ~GetAddrInfoHandler();
54
55         static void* threadStart(void* handler);
56         void start();
57
58     private:
59         void run();
60         SocketClient* mClient;  // ref counted
61         char* mHost;    // owned
62         char* mService; // owned
63         struct addrinfo* mHints;  // owned
64         unsigned mNetId;
65         uint32_t mMark;
66     };
67
68     /* ------ gethostbyname ------*/
69     class GetHostByNameCmd : public NetdCommand {
70     public:
71         GetHostByNameCmd(const DnsProxyListener* dnsProxyListener);
72         virtual ~GetHostByNameCmd() {}
73         int runCommand(SocketClient *c, int argc, char** argv);
74     private:
75         const DnsProxyListener* mDnsProxyListener;
76     };
77
78     class GetHostByNameHandler {
79     public:
80         GetHostByNameHandler(SocketClient *c,
81                             char *name,
82                             int af,
83                             unsigned netId,
84                             uint32_t mark);
85         ~GetHostByNameHandler();
86         static void* threadStart(void* handler);
87         void start();
88     private:
89         void run();
90         SocketClient* mClient; //ref counted
91         char* mName; // owned
92         int mAf;
93         unsigned mNetId;
94         uint32_t mMark;
95     };
96
97     /* ------ gethostbyaddr ------*/
98     class GetHostByAddrCmd : public NetdCommand {
99     public:
100         GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener);
101         virtual ~GetHostByAddrCmd() {}
102         int runCommand(SocketClient *c, int argc, char** argv);
103     private:
104         const DnsProxyListener* mDnsProxyListener;
105     };
106
107     class GetHostByAddrHandler {
108     public:
109         GetHostByAddrHandler(SocketClient *c,
110                             void* address,
111                             int addressLen,
112                             int addressFamily,
113                             unsigned netId,
114                             uint32_t mark);
115         ~GetHostByAddrHandler();
116
117         static void* threadStart(void* handler);
118         void start();
119
120     private:
121         void run();
122         SocketClient* mClient;  // ref counted
123         void* mAddress;    // address to lookup; owned
124         int mAddressLen; // length of address to look up
125         int mAddressFamily;  // address family
126         unsigned mNetId;
127         uint32_t mMark;
128     };
129
130     // Calculate the socket mark to use for a DNS resolution.
131     uint32_t calcMark(SocketClient *c, unsigned netId) const;
132 };
133
134 #endif