2 * Copyright (C) 2016 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 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.
18 #ifndef DNS_RESPONDER_H
19 #define DNS_RESPONDER_H
21 #include <arpa/nameser.h>
27 #include <unordered_map>
30 #include <android-base/thread_annotations.h>
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.
45 DNSResponder(const char* listen_address, const char* listen_service,
46 int poll_timeout_ms, uint16_t error_rcode,
47 double response_probability);
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);
55 std::vector<std::pair<std::string, ns_type>> queries() const;
59 // Key used for accessing mappings.
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;
67 bool operator < (const QueryKey& o) const {
68 if (name < o.name) return true;
69 if (name > o.name) return false;
75 size_t operator() (const QueryKey& key) const {
76 return std::hash<std::string>()(key.name) +
77 static_cast<size_t>(key.type);
81 // DNS request handler.
82 void requestHandler();
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;
89 bool addAnswerRecords(const DNSQuestion& question,
90 std::vector<DNSRecord>* answers) const;
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;
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_;
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.
121 // File descriptor for epoll.
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_;
132 #endif // DNS_RESPONDER_H