File: tls_connection_factory_posix.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (95 lines) | stat: -rw-r--r-- 3,556 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PLATFORM_IMPL_TLS_CONNECTION_FACTORY_POSIX_H_
#define PLATFORM_IMPL_TLS_CONNECTION_FACTORY_POSIX_H_

#include <openssl/ssl.h>

#include <memory>

#include "platform/api/tls_connection.h"
#include "platform/api/tls_connection_factory.h"
#include "platform/base/error.h"
#include "platform/impl/platform_client_posix.h"
#include "platform/impl/tls_data_router_posix.h"
#include "util/weak_ptr.h"

namespace openscreen {

class StreamSocket;

class TlsConnectionFactoryPosix : public TlsConnectionFactory,
                                  public TlsDataRouterPosix::SocketObserver {
 public:
  TlsConnectionFactoryPosix(Client* client,
                            TaskRunner* task_runner,
                            PlatformClientPosix* platform_client =
                                PlatformClientPosix::GetInstance());
  ~TlsConnectionFactoryPosix() override;

  // TlsConnectionFactory overrides.
  //
  // TODO(jophba, rwkeane): Determine how to handle multiple connection attempts
  // to the same remote_address, and how to distinguish errors.
  void Connect(const IPEndpoint& remote_address,
               const TlsConnectOptions& options) override;
  void SetListenCredentials(const TlsCredentials& credentials) override;
  void Listen(const IPEndpoint& local_address,
              const TlsListenOptions& options) override;

 private:
  // TlsDataRouterPosix::SocketObserver overrides.
  void OnConnectionPending(StreamSocketPosix* socket) override;

  // Configures a new SSL connection when a StreamSocket connection is accepted.
  void OnSocketAccepted(std::unique_ptr<StreamSocket> socket);

  // Configures the SSL connection for the provided TlsConnectionPosix,
  // returning true if the process is successful, false otherwise.
  bool ConfigureSsl(TlsConnectionPosix* connection);

  // Ensures that SSL is initialized, then gets a new SSL connection.
  ErrorOr<bssl::UniquePtr<SSL>> GetSslConnection();

  // Method wrapping the Initialize() private method, that can be safely called
  // multiple times.
  void EnsureInitialized();

  // Create the shared context used for all SSL connections created by this
  // factory.
  void Initialize();

  // Handles their respective SSL handshake calls.  These will continue to be
  // scheduled on |task_runner_| until the handshake completes.
  void Connect(std::unique_ptr<TlsConnectionPosix> connection);
  void Accept(std::unique_ptr<TlsConnectionPosix> connection);

  // Called on any thread, to post a task to notify the Client that a connection
  // failure or other error has occurred.
  void DispatchConnectionFailed(const IPEndpoint& remote_endpoint);
  void DispatchError(Error error);

  // Thread-safe mechanism to ensure Initialize() is only called once.
  std::once_flag init_instance_flag_;

  // Are the Listen() credentials set? Getting the certificate directly
  // from the SSL_CTX is non-trivial, so we store a property instead.
  bool listen_credentials_set_ = false;

  Client* const client_;
  TaskRunner* const task_runner_;
  PlatformClientPosix* const platform_client_;

  // SSL context, for creating SSL Connections via BoringSSL.
  bssl::UniquePtr<SSL_CTX> ssl_context_;

  WeakPtrFactory<TlsConnectionFactoryPosix> weak_factory_{this};

  OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnectionFactoryPosix);
};

}  // namespace openscreen

#endif  // PLATFORM_IMPL_TLS_CONNECTION_FACTORY_POSIX_H_