blob: 7a69ed19ba252efa791e679a4599af51def570d9 [file] [log] [blame]
Ben Schwartzded1b702017-10-25 14:41:02 -04001/*
2 * Copyright (C) 2018 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 _DNS_DNSTLSSOCKET_H
18#define _DNS_DNSTLSSOCKET_H
19
20#include <future>
21#include <mutex>
22#include <openssl/ssl.h>
23
24#include <android-base/thread_annotations.h>
25#include <android-base/unique_fd.h>
26#include <netdutils/Slice.h>
27#include <netdutils/Status.h>
28
Mike Yu5ae61542018-10-19 22:11:43 +080029#include "DnsTlsServer.h"
30#include "IDnsTlsSocket.h"
Ben Schwartzbfc8d992019-01-10 14:30:46 -050031#include "LockedQueue.h"
Mike Yu5ae61542018-10-19 22:11:43 +080032#include "params.h"
Ben Schwartzded1b702017-10-25 14:41:02 -040033
34namespace android {
35namespace net {
36
Ben Schwartz33860762017-10-25 14:41:02 -040037class IDnsTlsSocketObserver;
Ben Schwartzded1b702017-10-25 14:41:02 -040038class DnsTlsSessionCache;
39
40using netdutils::Slice;
41
42// A class for managing a TLS socket that sends and receives messages in
43// [length][value] format, with a 2-byte length (i.e. DNS-over-TCP format).
Ben Schwartz33860762017-10-25 14:41:02 -040044// This class is not aware of query-response pairing or anything else about DNS.
45// For the observer:
46// This class is not re-entrant: the observer is not permitted to wait for a call to query()
47// or the destructor in a callback. Doing so will result in deadlocks.
48// This class may call the observer at any time after initialize(), until the destructor
49// returns (but not after).
Mike Yua46fae72018-11-01 20:07:00 +080050class DnsTlsSocket : public IDnsTlsSocket {
51 public:
Ben Schwartzded1b702017-10-25 14:41:02 -040052 DnsTlsSocket(const DnsTlsServer& server, unsigned mark,
Ben Schwartz33860762017-10-25 14:41:02 -040053 IDnsTlsSocketObserver* _Nonnull observer,
Ben Schwartzded1b702017-10-25 14:41:02 -040054 DnsTlsSessionCache* _Nonnull cache) :
Ben Schwartz33860762017-10-25 14:41:02 -040055 mMark(mark), mServer(server), mObserver(observer), mCache(cache) {}
Ben Schwartzded1b702017-10-25 14:41:02 -040056 ~DnsTlsSocket();
57
58 // Creates the SSL context for this session and connect. Returns false on failure.
59 // This method should be called after construction and before use of a DnsTlsSocket.
60 // Only call this method once per DnsTlsSocket.
61 bool initialize() EXCLUDES(mLock);
62
63 // Send a query on the provided SSL socket. |query| contains
Ben Schwartz33860762017-10-25 14:41:02 -040064 // the body of a query, not including the ID header. This function will typically return before
65 // the query is actually sent. If this function fails, DnsTlsSocketObserver will be
66 // notified that the socket is closed.
67 // Note that success here indicates successful sending, not receipt of a response.
68 // Thread-safe.
69 bool query(uint16_t id, const Slice query) override;
Ben Schwartzded1b702017-10-25 14:41:02 -040070
71private:
Ben Schwartz33860762017-10-25 14:41:02 -040072 // Lock to be held by the SSL event loop thread. This is not normally in contention.
Ben Schwartzded1b702017-10-25 14:41:02 -040073 std::mutex mLock;
74
Ben Schwartz33860762017-10-25 14:41:02 -040075 // Forwards queries and receives responses. Blocks until the idle timeout.
76 void loop() EXCLUDES(mLock);
77 std::unique_ptr<std::thread> mLoopThread GUARDED_BY(mLock);
78
Ben Schwartzded1b702017-10-25 14:41:02 -040079 // On success, sets mSslFd to a socket connected to mAddr (the
80 // connection will likely be in progress if mProtocol is IPPROTO_TCP).
81 // On error, returns the errno.
82 netdutils::Status tcpConnect() REQUIRES(mLock);
83
84 // Connect an SSL session on the provided socket. If connection fails, closing the
85 // socket remains the caller's responsibility.
86 bssl::UniquePtr<SSL> sslConnect(int fd) REQUIRES(mLock);
87
88 // Disconnect the SSL session and close the socket.
89 void sslDisconnect() REQUIRES(mLock);
90
91 // Writes a buffer to the socket.
92 bool sslWrite(const Slice buffer) REQUIRES(mLock);
93
Ben Schwartz33860762017-10-25 14:41:02 -040094 // Reads exactly the specified number of bytes from the socket, or fails.
95 // Returns SSL_ERROR_NONE on success.
96 // If |wait| is true, then this function always blocks. Otherwise, it
97 // will return SSL_ERROR_WANT_READ if there is no data from the server to read.
98 int sslRead(const Slice buffer, bool wait) REQUIRES(mLock);
Ben Schwartzded1b702017-10-25 14:41:02 -040099
Ben Schwartzbfc8d992019-01-10 14:30:46 -0500100 bool sendQuery(const std::vector<uint8_t>& buf) REQUIRES(mLock);
Ben Schwartz33860762017-10-25 14:41:02 -0400101 bool readResponse() REQUIRES(mLock);
102
Ben Schwartzbfc8d992019-01-10 14:30:46 -0500103 // Queue of pending queries. query() pushes items onto the queue and notifies
104 // the loop thread by incrementing mEventFd. loop() reads items off the queue.
105 LockedQueue<std::vector<uint8_t>> mQueue;
106
107 // eventfd socket used for notifying the SSL thread when queries are ready to send.
108 // This socket acts similarly to an atomic counter, incremented by query() and cleared
109 // by loop(). We have to use a socket because the SSL thread needs to wait in poll()
110 // for input from either a remote server or a query thread.
111 // EOF indicates a close request.
112 base::unique_fd mEventFd;
Ben Schwartzded1b702017-10-25 14:41:02 -0400113
114 // SSL Socket fields.
115 bssl::UniquePtr<SSL_CTX> mSslCtx GUARDED_BY(mLock);
116 base::unique_fd mSslFd GUARDED_BY(mLock);
117 bssl::UniquePtr<SSL> mSsl GUARDED_BY(mLock);
Ben Schwartz33860762017-10-25 14:41:02 -0400118 static constexpr std::chrono::seconds kIdleTimeout = std::chrono::seconds(20);
Ben Schwartzded1b702017-10-25 14:41:02 -0400119
120 const unsigned mMark; // Socket mark
121 const DnsTlsServer mServer;
Ben Schwartz33860762017-10-25 14:41:02 -0400122 IDnsTlsSocketObserver* _Nonnull const mObserver;
Ben Schwartzded1b702017-10-25 14:41:02 -0400123 DnsTlsSessionCache* _Nonnull const mCache;
124};
125
126} // end of namespace net
127} // end of namespace android
128
129#endif // _DNS_DNSTLSSOCKET_H