OSDN Git Service

Add a binder IPC to close socket connections.
[android-x86/system-netd.git] / tests / dns_responder.h
1 /*
2  * Copyright (C) 2016 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 requied 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
18 #ifndef DNS_RESPONDER_H
19 #define DNS_RESPONDER_H
20
21 #include <arpa/nameser.h>
22
23 #include <atomic>
24 #include <mutex>
25 #include <string>
26 #include <thread>
27 #include <unordered_map>
28 #include <vector>
29
30 #include <android-base/thread_annotations.h>
31
32 namespace test {
33
34 struct DNSHeader;
35 struct DNSQuestion;
36 struct DNSRecord;
37
38 /*
39  * Simple DNS responder, which replies to queries with the registered response
40  * for that type. Class is assumed to be IN. If no response is registered, the
41  * default error response code is returned.
42  */
43 class DNSResponder {
44 public:
45     DNSResponder(const char* listen_address, const char* listen_service,
46                  int poll_timeout_ms, uint16_t error_rcode,
47                  double response_probability);
48     ~DNSResponder();
49     void addMapping(const char* name, ns_type type, const char* addr);
50     void removeMapping(const char* name, ns_type type);
51     void setResponseProbability(double response_probability);
52     bool running() const;
53     bool startServer();
54     bool stopServer();
55     std::vector<std::pair<std::string, ns_type>> queries() const;
56     void clearQueries();
57
58 private:
59     // Key used for accessing mappings.
60     struct QueryKey {
61         std::string name;
62         unsigned type;
63         QueryKey(std::string n, unsigned t) : name(n), type(t) {}
64         bool operator == (const QueryKey& o) const {
65             return name == o.name && type == o.type;
66         }
67         bool operator < (const QueryKey& o) const {
68             if (name < o.name) return true;
69             if (name > o.name) return false;
70             return type < o.type;
71         }
72     };
73
74     struct QueryKeyHash {
75         size_t operator() (const QueryKey& key) const {
76             return std::hash<std::string>()(key.name) +
77                    static_cast<size_t>(key.type);
78         }
79     };
80
81     // DNS request handler.
82     void requestHandler();
83
84     // Parses and generates a response message for incoming DNS requests.
85     // Returns false on parsing errors.
86     bool handleDNSRequest(const char* buffer, ssize_t buffer_len,
87                           char* response, size_t* response_len) const;
88
89     bool addAnswerRecords(const DNSQuestion& question,
90                           std::vector<DNSRecord>* answers) const;
91
92     bool generateErrorResponse(DNSHeader* header, ns_rcode rcode,
93                                char* response, size_t* response_len) const;
94     bool makeErrorResponse(DNSHeader* header, ns_rcode rcode, char* response,
95                            size_t* response_len) const;
96
97
98     // Address and service to listen on, currently limited to UDP.
99     const std::string listen_address_;
100     const std::string listen_service_;
101     // epoll_wait() timeout in ms.
102     const int poll_timeout_ms_;
103     // Error code to return for requests for an unknown name.
104     const uint16_t error_rcode_;
105     // Probability that a valid response is being sent instead of being sent
106     // instead of returning error_rcode_.
107     std::atomic<double> response_probability_;
108
109     // Mappings from (name, type) to registered response and the
110     // mutex protecting them.
111     std::unordered_map<QueryKey, std::string, QueryKeyHash> mappings_
112         GUARDED_BY(mappings_mutex_);
113     // TODO(imaipi): enable GUARDED_BY(mappings_mutex_);
114     std::mutex mappings_mutex_;
115     // Query names received so far and the corresponding mutex.
116     mutable std::vector<std::pair<std::string, ns_type>> queries_
117         GUARDED_BY(queries_mutex_);
118     mutable std::mutex queries_mutex_;
119     // Socket on which the server is listening.
120     int socket_;
121     // File descriptor for epoll.
122     int epoll_fd_;
123     // Signal for request handler termination.
124     std::atomic<bool> terminate_ GUARDED_BY(update_mutex_);
125     // Thread for handling incoming threads.
126     std::thread handler_thread_ GUARDED_BY(update_mutex_);
127     std::mutex update_mutex_;
128 };
129
130 }  // namespace test
131
132 #endif  // DNS_RESPONDER_H