1 // Copyright 2019 The Chromium 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 CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_ 6 #define CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_ 7 8 #include <openssl/x509.h> 9 10 #include <set> 11 #include <utility> 12 #include <vector> 13 14 #include "cast/common/public/cast_socket.h" 15 #include "platform/api/serial_delete_ptr.h" 16 #include "platform/api/task_runner.h" 17 #include "platform/api/tls_connection_factory.h" 18 #include "platform/base/ip_address.h" 19 20 namespace openscreen { 21 namespace cast { 22 23 class AuthContext; 24 25 class SenderSocketFactory final : public TlsConnectionFactory::Client, 26 public CastSocket::Client { 27 public: 28 class Client { 29 public: 30 virtual void OnConnected(SenderSocketFactory* factory, 31 const IPEndpoint& endpoint, 32 std::unique_ptr<CastSocket> socket) = 0; 33 virtual void OnError(SenderSocketFactory* factory, 34 const IPEndpoint& endpoint, 35 Error error) = 0; 36 }; 37 38 enum class DeviceMediaPolicy { 39 kNone = 0, 40 kAudioOnly, 41 kIncludesVideo, 42 }; 43 44 // |client| and |task_runner| must outlive |this|. 45 SenderSocketFactory(Client* client, TaskRunner* task_runner); 46 ~SenderSocketFactory(); 47 48 // |factory| cannot be nullptr and must outlive |this|. 49 void set_factory(TlsConnectionFactory* factory); 50 51 // Begins connecting to a Cast device at |endpoint|. If a successful 52 // connection is made, including device authentication, the new CastSocket 53 // will be passed to |client_|'s OnConnected method. The new CastSocket will 54 // have its client set to |client|. If any part of the connection process 55 // fails, |client_|'s OnError method is called instead. This includes if the 56 // device's media policy, as determined by authentication, is audio-only and 57 // |media_policy| is kIncludesVideo. 58 void Connect(const IPEndpoint& endpoint, 59 DeviceMediaPolicy media_policy, 60 CastSocket::Client* client); 61 62 // TlsConnectionFactory::Client overrides. 63 void OnAccepted(TlsConnectionFactory* factory, 64 std::vector<uint8_t> der_x509_peer_cert, 65 std::unique_ptr<TlsConnection> connection) override; 66 void OnConnected(TlsConnectionFactory* factory, 67 std::vector<uint8_t> der_x509_peer_cert, 68 std::unique_ptr<TlsConnection> connection) override; 69 void OnConnectionFailed(TlsConnectionFactory* factory, 70 const IPEndpoint& remote_address) override; 71 void OnError(TlsConnectionFactory* factory, Error error) override; 72 73 private: 74 struct PendingConnection { 75 IPEndpoint endpoint; 76 DeviceMediaPolicy media_policy; 77 CastSocket::Client* client; 78 }; 79 80 struct PendingAuth { 81 IPEndpoint endpoint; 82 DeviceMediaPolicy media_policy; 83 SerialDeletePtr<CastSocket> socket; 84 CastSocket::Client* client; 85 std::unique_ptr<AuthContext> auth_context; 86 bssl::UniquePtr<X509> peer_cert; 87 }; 88 89 friend bool operator<(const std::unique_ptr<PendingAuth>& a, int b); 90 friend bool operator<(int a, const std::unique_ptr<PendingAuth>& b); 91 92 std::vector<PendingConnection>::iterator FindPendingConnection( 93 const IPEndpoint& endpoint); 94 95 // CastSocket::Client overrides. 96 void OnError(CastSocket* socket, Error error) override; 97 void OnMessage(CastSocket* socket, 98 ::cast::channel::CastMessage message) override; 99 100 Client* const client_; 101 TaskRunner* const task_runner_; 102 TlsConnectionFactory* factory_ = nullptr; 103 std::vector<PendingConnection> pending_connections_; 104 std::vector<std::unique_ptr<PendingAuth>> pending_auth_; 105 }; 106 107 } // namespace cast 108 } // namespace openscreen 109 110 #endif // CAST_SENDER_PUBLIC_SENDER_SOCKET_FACTORY_H_ 111