diff options
author | btolsch <btolsch@chromium.org> | 2019-10-15 12:54:10 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-10-15 20:03:29 +0000 |
commit | a04b13eaa77d8ca31c0f06e32231bef84f96e32d (patch) | |
tree | 5158547e199df455b63cbe7f93c6c99748054181 /cast/sender/channel/sender_socket_factory.h | |
parent | c56f0388085f0e0d4df657498381f35317fbf041 (diff) | |
download | openscreen-a04b13eaa77d8ca31c0f06e32231bef84f96e32d.tar.gz |
Add cast sender socket factory
This change adds a CastSocket factory for the sender-side which performs
the sender auth challenge and verification before passing a CastSocket
back to the caller.
Bug: openscreen:59
Change-Id: Ibbbdb2b8881e385cc0a8defbe309c7f10a2af323
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1834457
Commit-Queue: Brandon Tolsch <btolsch@chromium.org>
Reviewed-by: Ryan Keane <rwkeane@google.com>
Diffstat (limited to 'cast/sender/channel/sender_socket_factory.h')
-rw-r--r-- | cast/sender/channel/sender_socket_factory.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/cast/sender/channel/sender_socket_factory.h b/cast/sender/channel/sender_socket_factory.h new file mode 100644 index 00000000..d5b6622d --- /dev/null +++ b/cast/sender/channel/sender_socket_factory.h @@ -0,0 +1,104 @@ +// Copyright 2019 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 CAST_SENDER_CHANNEL_SENDER_SOCKET_FACTORY_H_ +#define CAST_SENDER_CHANNEL_SENDER_SOCKET_FACTORY_H_ + +#include <set> +#include <utility> +#include <vector> + +#include "cast/common/channel/cast_socket.h" +#include "cast/sender/channel/cast_auth_util.h" +#include "platform/api/logging.h" +#include "platform/api/tls_connection_factory.h" +#include "platform/base/ip_address.h" + +namespace cast { +namespace channel { + +using openscreen::Error; +using openscreen::IPEndpoint; +using openscreen::IPEndpointComparator; +using openscreen::platform::TlsConnection; +using openscreen::platform::TlsConnectionFactory; + +class SenderSocketFactory final : public TlsConnectionFactory::Client, + public CastSocket::Client { + public: + class Client { + public: + virtual void OnConnected(SenderSocketFactory* factory, + const IPEndpoint& endpoint, + std::unique_ptr<CastSocket> socket) = 0; + virtual void OnError(SenderSocketFactory* factory, + const IPEndpoint& endpoint, + Error error) = 0; + }; + + enum class DeviceMediaPolicy { + kAudioOnly, + kIncludesVideo, + }; + + // |client| must outlive |this|. + explicit SenderSocketFactory(Client* client); + ~SenderSocketFactory(); + + void set_factory(TlsConnectionFactory* factory) { + OSP_DCHECK(factory); + factory_ = factory; + } + + void Connect(const IPEndpoint& endpoint, + DeviceMediaPolicy media_policy, + CastSocket::Client* client); + + // TlsConnectionFactory::Client overrides. + void OnAccepted(TlsConnectionFactory* factory, + X509* peer_cert, + std::unique_ptr<TlsConnection> connection) override; + void OnConnected(TlsConnectionFactory* factory, + X509* peer_cert, + std::unique_ptr<TlsConnection> connection) override; + void OnConnectionFailed(TlsConnectionFactory* factory, + const IPEndpoint& remote_address) override; + void OnError(TlsConnectionFactory* factory, Error error) override; + + private: + struct PendingConnection { + IPEndpoint endpoint; + DeviceMediaPolicy media_policy; + CastSocket::Client* client; + }; + + struct PendingAuth { + IPEndpoint endpoint; + DeviceMediaPolicy media_policy; + std::unique_ptr<CastSocket> socket; + CastSocket::Client* client; + AuthContext auth_context; + X509* peer_cert; + }; + + friend bool operator<(const std::unique_ptr<PendingAuth>& a, uint32_t b); + friend bool operator<(uint32_t a, const std::unique_ptr<PendingAuth>& b); + + std::vector<PendingConnection>::iterator FindPendingConnection( + const IPEndpoint& endpoint); + + // CastSocket::Client overrides. + void OnError(CastSocket* socket, Error error) override; + void OnMessage(CastSocket* socket, CastMessage message) override; + + Client* const client_; + TlsConnectionFactory* factory_ = nullptr; + std::vector<PendingConnection> pending_connections_; + std::vector<std::unique_ptr<PendingAuth>> pending_auth_; +}; + +} // namespace channel +} // namespace cast + +#endif // CAST_SENDER_CHANNEL_SENDER_SOCKET_FACTORY_H_ |