blob: 0c37a520fbf25cd94ced806642dcb0cff6f3d744 [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
29#include "dns/DnsTlsServer.h"
30#include "dns/IDnsTlsSocket.h"
31
32namespace android {
33namespace net {
34
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040035class IDnsTlsSocketObserver;
Ben Schwartzded1b702017-10-25 14:41:02 -040036class DnsTlsSessionCache;
37
38using netdutils::Slice;
39
40// A class for managing a TLS socket that sends and receives messages in
41// [length][value] format, with a 2-byte length (i.e. DNS-over-TCP format).
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040042// This class is not aware of query-response pairing or anything else about DNS.
43// For the observer:
44// This class is not re-entrant: the observer is not permitted to wait for a call to query()
45// or the destructor in a callback. Doing so will result in deadlocks.
46// This class may call the observer at any time after initialize(), until the destructor
47// returns (but not after).
Ben Schwartzded1b702017-10-25 14:41:02 -040048class DnsTlsSocket : public IDnsTlsSocket {
49public:
50 DnsTlsSocket(const DnsTlsServer& server, unsigned mark,
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040051 IDnsTlsSocketObserver* _Nonnull observer,
Ben Schwartzded1b702017-10-25 14:41:02 -040052 DnsTlsSessionCache* _Nonnull cache) :
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040053 mMark(mark), mServer(server), mObserver(observer), mCache(cache) {}
Ben Schwartzded1b702017-10-25 14:41:02 -040054 ~DnsTlsSocket();
55
56 // Creates the SSL context for this session and connect. Returns false on failure.
57 // This method should be called after construction and before use of a DnsTlsSocket.
58 // Only call this method once per DnsTlsSocket.
59 bool initialize() EXCLUDES(mLock);
60
61 // Send a query on the provided SSL socket. |query| contains
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040062 // the body of a query, not including the ID header. This function will typically return before
63 // the query is actually sent. If this function fails, DnsTlsSocketObserver will be
64 // notified that the socket is closed.
65 // Note that success here indicates successful sending, not receipt of a response.
66 // Thread-safe.
67 bool query(uint16_t id, const Slice query) override;
Ben Schwartzded1b702017-10-25 14:41:02 -040068
69private:
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040070 // Lock to be held by the SSL event loop thread. This is not normally in contention.
Ben Schwartzded1b702017-10-25 14:41:02 -040071 std::mutex mLock;
72
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040073 // Forwards queries and receives responses. Blocks until the idle timeout.
74 void loop() EXCLUDES(mLock);
75 std::unique_ptr<std::thread> mLoopThread GUARDED_BY(mLock);
76
Ben Schwartzded1b702017-10-25 14:41:02 -040077 // On success, sets mSslFd to a socket connected to mAddr (the
78 // connection will likely be in progress if mProtocol is IPPROTO_TCP).
79 // On error, returns the errno.
80 netdutils::Status tcpConnect() REQUIRES(mLock);
81
82 // Connect an SSL session on the provided socket. If connection fails, closing the
83 // socket remains the caller's responsibility.
84 bssl::UniquePtr<SSL> sslConnect(int fd) REQUIRES(mLock);
85
86 // Disconnect the SSL session and close the socket.
87 void sslDisconnect() REQUIRES(mLock);
88
89 // Writes a buffer to the socket.
90 bool sslWrite(const Slice buffer) REQUIRES(mLock);
91
Ben Schwartz8d1e7a62017-10-25 14:41:02 -040092 // Reads exactly the specified number of bytes from the socket, or fails.
93 // Returns SSL_ERROR_NONE on success.
94 // If |wait| is true, then this function always blocks. Otherwise, it
95 // will return SSL_ERROR_WANT_READ if there is no data from the server to read.
96 int sslRead(const Slice buffer, bool wait) REQUIRES(mLock);
Ben Schwartzded1b702017-10-25 14:41:02 -040097
98 struct Query {
99 uint16_t id;
Ben Schwartz8d1e7a62017-10-25 14:41:02 -0400100 Slice query;
Ben Schwartzded1b702017-10-25 14:41:02 -0400101 };
102
103 bool sendQuery(const Query& q) REQUIRES(mLock);
Ben Schwartz8d1e7a62017-10-25 14:41:02 -0400104 bool readResponse() REQUIRES(mLock);
105
106 // SOCK_SEQPACKET socket pair used for sending queries from myriad query
107 // threads to the SSL thread. EOF indicates a close request.
108 // We have to use a socket pair (i.e. a pipe) because the SSL thread needs to wait in
109 // select() for input from either a remote server or a query thread.
110 base::unique_fd mIpcInFd;
111 base::unique_fd mIpcOutFd GUARDED_BY(mLock);
Ben Schwartzded1b702017-10-25 14:41:02 -0400112
113 // SSL Socket fields.
114 bssl::UniquePtr<SSL_CTX> mSslCtx GUARDED_BY(mLock);
115 base::unique_fd mSslFd GUARDED_BY(mLock);
116 bssl::UniquePtr<SSL> mSsl GUARDED_BY(mLock);
Ben Schwartz8d1e7a62017-10-25 14:41:02 -0400117 static constexpr std::chrono::seconds kIdleTimeout = std::chrono::seconds(20);
Ben Schwartzded1b702017-10-25 14:41:02 -0400118
119 const unsigned mMark; // Socket mark
120 const DnsTlsServer mServer;
Ben Schwartz8d1e7a62017-10-25 14:41:02 -0400121 IDnsTlsSocketObserver* _Nonnull const mObserver;
Ben Schwartzded1b702017-10-25 14:41:02 -0400122 DnsTlsSessionCache* _Nonnull const mCache;
123};
124
125} // end of namespace net
126} // end of namespace android
127
128#endif // _DNS_DNSTLSSOCKET_H