• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIBWEAVE_EXAMPLES_PROVIDER_SSL_STREAM_H_
6 #define LIBWEAVE_EXAMPLES_PROVIDER_SSL_STREAM_H_
7 
8 #include <openssl/ssl.h>
9 
10 #include <base/memory/weak_ptr.h>
11 #include <weave/provider/network.h>
12 #include <weave/stream.h>
13 
14 namespace weave {
15 
16 namespace provider {
17 class TaskRunner;
18 }
19 
20 namespace examples {
21 
22 class SSLStream : public Stream {
23  public:
24   ~SSLStream() override;
25 
26   void Read(void* buffer,
27             size_t size_to_read,
28             const ReadCallback& callback) override;
29 
30   void Write(const void* buffer,
31              size_t size_to_write,
32              const WriteCallback& callback) override;
33 
34   void CancelPendingOperations() override;
35 
36   static void Connect(provider::TaskRunner* task_runner,
37                       const std::string& host,
38                       uint16_t port,
39                       const provider::Network::OpenSslSocketCallback& callback);
40 
41  private:
42   struct SslDeleter {
43     void operator()(BIO* bio) const;
44     void operator()(SSL* ssl) const;
45     void operator()(SSL_CTX* ctx) const;
46   };
47 
48   SSLStream(provider::TaskRunner* task_runner,
49             std::unique_ptr<BIO, SslDeleter> stream_bio);
50 
51   static void ConnectBio(
52       std::unique_ptr<SSLStream> stream,
53       const provider::Network::OpenSslSocketCallback& callback);
54   static void DoHandshake(
55       std::unique_ptr<SSLStream> stream,
56       const provider::Network::OpenSslSocketCallback& callback);
57 
58   // Send task to this method with WeakPtr if callback should not be executed
59   // after SSLStream is destroyed.
60   void RunTask(const base::Closure& task);
61 
62   provider::TaskRunner* task_runner_{nullptr};
63   std::unique_ptr<SSL_CTX, SslDeleter> ctx_;
64   std::unique_ptr<SSL, SslDeleter> ssl_;
65 
66   base::WeakPtrFactory<SSLStream> weak_ptr_factory_{this};
67 };
68 
69 }  // namespace examples
70 }  // namespace weave
71 
72 #endif  // LIBWEAVE_EXAMPLES_PROVIDER_SSL_STREAM_H_
73