blob: 4202fccc32b91e832e3ce17a7f7c174033769243 [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_ASYNC_PACKET_SOCKET_H_
12#define RTC_BASE_ASYNC_PACKET_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Per Kjellander906deaf2024-10-31 14:10:1914#include <cstddef>
Per K4ac37182023-11-15 09:43:3215#include <cstdint>
Per Kjellander906deaf2024-10-31 14:10:1916#include <functional>
Steve Antonf4172382020-01-27 23:45:0217#include <vector>
18
Per Kjellander906deaf2024-10-31 14:10:1919#include "absl/functional/any_invocable.h"
Tomas Gunnarssonf15189d2022-04-13 09:03:5220#include "api/sequence_checker.h"
21#include "rtc_base/callback_list.h"
Per Kjellander906deaf2024-10-31 14:10:1922#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/dscp.h"
Per K4ac37182023-11-15 09:43:3224#include "rtc_base/network/received_packet.h"
Yves Gerey3e707812018-11-28 15:47:4925#include "rtc_base/network/sent_packet.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "rtc_base/socket.h"
Per Kjellander906deaf2024-10-31 14:10:1927#include "rtc_base/socket_address.h"
Tomas Gunnarssonf15189d2022-04-13 09:03:5228#include "rtc_base/system/no_unique_address.h"
Mirko Bonadei35214fc2019-09-23 12:54:2829#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 13:04:2830#include "rtc_base/third_party/sigslot/sigslot.h"
Per Kjellander906deaf2024-10-31 14:10:1931#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 17:11:0032#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2633
Henrik Kjellanderec78f1c2017-06-29 05:52:5034namespace rtc {
35
36// This structure holds the info needed to update the packet send time header
37// extension, including the information needed to update the authentication tag
38// after changing the value.
39struct PacketTimeUpdateParams {
40 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-12 03:14:1741 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 05:52:5042 ~PacketTimeUpdateParams();
43
Qingsi Wang6e641e62018-04-12 03:14:1744 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Yves Gerey665174f2018-06-19 13:03:0545 std::vector<char> srtp_auth_key; // Authentication key.
46 int srtp_auth_tag_len = -1; // Authentication tag length.
47 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 05:52:5048};
49
50// This structure holds meta information for the packet which is about to send
51// over network.
Mirko Bonadei35214fc2019-09-23 12:54:2852struct RTC_EXPORT PacketOptions {
Qingsi Wang6e641e62018-04-12 03:14:1753 PacketOptions();
54 explicit PacketOptions(DiffServCodePoint dscp);
55 PacketOptions(const PacketOptions& other);
56 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 05:52:5057
Qingsi Wang6e641e62018-04-12 03:14:1758 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Per K776c1a12024-03-12 12:00:5059
60 // Packet will be sent with ECN(1), RFC-3168, Section 5.
61 // Intended to be used with L4S
62 // https://www.rfc-editor.org/rfc/rfc9331.html
Per K776c1a12024-03-12 12:00:5063 bool ecn_1 = false;
64
Bjorn Mellem3a9c46d2018-04-25 20:24:4865 // When used with RTP packets (for example, webrtc::PacketOptions), the value
66 // should be 16 bits. A value of -1 represents "not set".
67 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 05:52:5068 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-12 03:14:1769 // PacketInfo is passed to SentPacket when signaling this packet is sent.
70 PacketInfo info_signaled_after_sent;
Markus Handellc8c4a282023-05-08 14:46:2171 // True if this is a batchable packet. Batchable packets are collected at low
72 // levels and sent first when their AsyncPacketSocket receives a
73 // OnSendBatchComplete call.
74 bool batchable = false;
75 // True if this is the last packet of a batch.
76 bool last_packet_in_batch = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:5077};
78
Henrik Kjellanderec78f1c2017-06-29 05:52:5079// Provides the ability to receive packets asynchronously. Sends are not
80// buffered since it is acceptable to drop packets under high load.
Mirko Bonadei35214fc2019-09-23 12:54:2881class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 05:52:5082 public:
83 enum State {
84 STATE_CLOSED,
85 STATE_BINDING,
86 STATE_BOUND,
87 STATE_CONNECTING,
88 STATE_CONNECTED
89 };
90
Tommic8482682023-03-23 21:20:3991 AsyncPacketSocket() = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:5092 ~AsyncPacketSocket() override;
93
Byoungchan Lee14af7622022-01-11 20:24:5894 AsyncPacketSocket(const AsyncPacketSocket&) = delete;
95 AsyncPacketSocket& operator=(const AsyncPacketSocket&) = delete;
96
Henrik Kjellanderec78f1c2017-06-29 05:52:5097 // Returns current local address. Address may be set to null if the
98 // socket is not bound yet (GetState() returns STATE_BINDING).
99 virtual SocketAddress GetLocalAddress() const = 0;
100
101 // Returns remote address. Returns zeroes if this is not a client TCP socket.
102 virtual SocketAddress GetRemoteAddress() const = 0;
103
104 // Send a packet.
Yves Gerey665174f2018-06-19 13:03:05105 virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
106 virtual int SendTo(const void* pv,
107 size_t cb,
108 const SocketAddress& addr,
Henrik Kjellanderec78f1c2017-06-29 05:52:50109 const PacketOptions& options) = 0;
110
111 // Close the socket.
112 virtual int Close() = 0;
113
114 // Returns current state of the socket.
115 virtual State GetState() const = 0;
116
117 // Get/set options.
118 virtual int GetOption(Socket::Option opt, int* value) = 0;
119 virtual int SetOption(Socket::Option opt, int value) = 0;
120
121 // Get/Set current error.
122 // TODO: Remove SetError().
123 virtual int GetError() const = 0;
124 virtual void SetError(int error) = 0;
125
Tomas Gunnarssonf15189d2022-04-13 09:03:52126 // Register a callback to be called when the socket is closed.
Tommi2afd2842023-09-05 07:36:24127 void SubscribeCloseEvent(
128 const void* removal_tag,
129 std::function<void(AsyncPacketSocket*, int)> callback);
130 void UnsubscribeCloseEvent(const void* removal_tag);
Tomas Gunnarssonf15189d2022-04-13 09:03:52131
Per K4ac37182023-11-15 09:43:32132 void RegisterReceivedPacketCallback(
133 absl::AnyInvocable<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)>
134 received_packet_callback);
135 void DeregisterReceivedPacketCallback();
136
Henrik Kjellanderec78f1c2017-06-29 05:52:50137 // Emitted each time a packet is sent.
138 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
139
140 // Emitted when the socket is currently able to send.
141 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
142
143 // Emitted after address for the socket is allocated, i.e. binding
144 // is finished. State of the socket is changed from BINDING to BOUND
Niels Möller4a1c2c42021-09-28 08:17:07145 // (for UDP sockets).
Henrik Kjellanderec78f1c2017-06-29 05:52:50146 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
147
148 // Emitted for client TCP sockets when state is changed from
149 // CONNECTING to CONNECTED.
150 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
151
Tomas Gunnarssonf15189d2022-04-13 09:03:52152 void NotifyClosedForTest(int err) { NotifyClosed(err); }
153
154 protected:
155 // TODO(bugs.webrtc.org/11943): Remove after updating downstream code.
156 void SignalClose(AsyncPacketSocket* s, int err) {
157 RTC_DCHECK_EQ(s, this);
158 NotifyClosed(err);
159 }
160
161 void NotifyClosed(int err) {
162 RTC_DCHECK_RUN_ON(&network_checker_);
163 on_close_.Send(this, err);
164 }
165
Per K4ac37182023-11-15 09:43:32166 void NotifyPacketReceived(const rtc::ReceivedPacket& packet);
167
Tommic8482682023-03-23 21:20:39168 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker network_checker_{
169 webrtc::SequenceChecker::kDetached};
Tomas Gunnarssonf15189d2022-04-13 09:03:52170
171 private:
172 webrtc::CallbackList<AsyncPacketSocket*, int> on_close_
173 RTC_GUARDED_BY(&network_checker_);
Per K4ac37182023-11-15 09:43:32174 absl::AnyInvocable<void(AsyncPacketSocket*, const rtc::ReceivedPacket&)>
175 received_packet_callback_ RTC_GUARDED_BY(&network_checker_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50176};
177
Niels Möllerd30ece12021-10-19 08:11:02178// Listen socket, producing an AsyncPacketSocket when a peer connects.
179class RTC_EXPORT AsyncListenSocket : public sigslot::has_slots<> {
180 public:
181 enum class State {
182 kClosed,
183 kBound,
184 };
185
186 // Returns current state of the socket.
187 virtual State GetState() const = 0;
188
189 // Returns current local address. Address may be set to null if the
190 // socket is not bound yet (GetState() returns kBinding).
191 virtual SocketAddress GetLocalAddress() const = 0;
192
Niels Möllerd30ece12021-10-19 08:11:02193 sigslot::signal2<AsyncListenSocket*, AsyncPacketSocket*> SignalNewConnection;
194};
Niels Möller6d19d142021-10-06 09:19:03195
Qingsi Wang6e641e62018-04-12 03:14:17196void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
197 const AsyncPacketSocket& socket_from,
198 rtc::PacketInfo* info);
199
Henrik Kjellanderec78f1c2017-06-29 05:52:50200} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26201
Steve Anton10542f22019-01-11 17:11:00202#endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_