blob: c41b4a8655413b506c9b777b7fd05663064c16e1 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_SOCKET_ADAPTERS_H_
12#define RTC_BASE_SOCKET_ADAPTERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Ali Tofigh7fa90572022-03-17 14:47:4916#include "absl/strings/string_view.h"
Niels Möller44153152018-12-17 13:04:0517#include "api/array_view.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/async_socket.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5019
20namespace rtc {
21
22struct HttpAuthContext;
23class ByteBufferReader;
24class ByteBufferWriter;
25
26///////////////////////////////////////////////////////////////////////////////
27
28// Implements a socket adapter that can buffer and process data internally,
29// as in the case of connecting to a proxy, where you must speak the proxy
30// protocol before commencing normal socket behavior.
31class BufferedReadAdapter : public AsyncSocketAdapter {
32 public:
Niels Möllerd0b88792021-08-12 08:32:3033 BufferedReadAdapter(Socket* socket, size_t buffer_size);
Henrik Kjellanderec78f1c2017-06-29 05:52:5034 ~BufferedReadAdapter() override;
35
Byoungchan Lee14af7622022-01-11 20:24:5836 BufferedReadAdapter(const BufferedReadAdapter&) = delete;
37 BufferedReadAdapter& operator=(const BufferedReadAdapter&) = delete;
38
Henrik Kjellanderec78f1c2017-06-29 05:52:5039 int Send(const void* pv, size_t cb) override;
40 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
41
42 protected:
43 int DirectSend(const void* pv, size_t cb) {
44 return AsyncSocketAdapter::Send(pv, cb);
45 }
46
47 void BufferInput(bool on = true);
48 virtual void ProcessInput(char* data, size_t* len) = 0;
49
Niels Möllerd0b88792021-08-12 08:32:3050 void OnReadEvent(Socket* socket) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5051
52 private:
Yves Gerey665174f2018-06-19 13:03:0553 char* buffer_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5054 size_t buffer_size_, data_len_;
55 bool buffering_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5056};
57
58///////////////////////////////////////////////////////////////////////////////
59
Henrik Kjellanderec78f1c2017-06-29 05:52:5060// Implements a socket adapter that performs the client side of a
61// fake SSL handshake. Used for "ssltcp" P2P functionality.
62class AsyncSSLSocket : public BufferedReadAdapter {
63 public:
Niels Möller44153152018-12-17 13:04:0564 static ArrayView<const uint8_t> SslClientHello();
65 static ArrayView<const uint8_t> SslServerHello();
66
Niels Möllerd0b88792021-08-12 08:32:3067 explicit AsyncSSLSocket(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5068
Byoungchan Lee14af7622022-01-11 20:24:5869 AsyncSSLSocket(const AsyncSSLSocket&) = delete;
70 AsyncSSLSocket& operator=(const AsyncSSLSocket&) = delete;
71
Henrik Kjellanderec78f1c2017-06-29 05:52:5072 int Connect(const SocketAddress& addr) override;
73
74 protected:
Niels Möllerd0b88792021-08-12 08:32:3075 void OnConnectEvent(Socket* socket) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5076 void ProcessInput(char* data, size_t* len) override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5077};
78
Henrik Kjellanderec78f1c2017-06-29 05:52:5079} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2680
Steve Anton10542f22019-01-11 17:11:0081#endif // RTC_BASE_SOCKET_ADAPTERS_H_