blob: 431de060ce8f4c1ca2ce792bad83287bf37c53f4 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2012 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 09:11:00 -080011#ifndef P2P_BASE_PORT_INTERFACE_H_
12#define P2P_BASE_PORT_INTERFACE_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <string>
Lahiru Ginnaliya Gamathige3ba7beb2021-02-01 02:06:11 -080015#include <utility>
Steve Anton6c38cc72017-11-29 10:25:58 -080016#include <vector>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017
Ali Tofighea5a9442022-06-14 15:20:15 +020018#include "absl/strings/string_view.h"
Danil Chapovalov00c71832018-06-15 15:58:38 +020019#include "absl/types/optional.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080020#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "p2p/base/transport_description.h"
22#include "rtc_base/async_packet_socket.h"
Lahiru Ginnaliya Gamathige3ba7beb2021-02-01 02:06:11 -080023#include "rtc_base/callback_list.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/socket_address.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26namespace rtc {
27class Network;
28struct PacketOptions;
Yves Gerey665174f2018-06-19 15:03:05 +020029} // namespace rtc
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030
31namespace cricket {
32class Connection;
33class IceMessage;
34class StunMessage;
Qingsi Wang72a43a12018-02-20 16:03:18 -080035class StunStats;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036
37enum ProtocolType {
38 PROTO_UDP,
39 PROTO_TCP,
hnsl277b2502016-12-13 05:17:23 -080040 PROTO_SSLTCP, // Pseudo-TLS.
41 PROTO_TLS,
42 PROTO_LAST = PROTO_TLS
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043};
44
45// Defines the interface for a port, which represents a local communication
46// mechanism that can be used to create connections to similar mechanisms of
47// the other client. Various types of ports will implement this interface.
48class PortInterface {
49 public:
Steve Anton1cf1b7d2017-10-30 10:00:15 -070050 virtual ~PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051
52 virtual const std::string& Type() const = 0;
Niels Möllere0c6bdf2022-03-24 15:18:02 +010053 virtual const rtc::Network* Network() const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 // Methods to set/get ICE role and tiebreaker values.
56 virtual void SetIceRole(IceRole role) = 0;
57 virtual IceRole GetIceRole() const = 0;
58
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
60 virtual uint64_t IceTiebreaker() const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061
62 virtual bool SharedSocket() const = 0;
63
Ali Tofighea5a9442022-06-14 15:20:15 +020064 // TODO(webrtc:13579): Remove std::string version once downstream users have
65 // migrated to the absl::string_view version.
Honghai Zhangf9945b22015-12-15 12:20:13 -080066 virtual bool SupportsProtocol(const std::string& protocol) const = 0;
Ali Tofighea5a9442022-06-14 15:20:15 +020067 virtual bool SupportsProtocol(absl::string_view protocol) const;
Honghai Zhangf9945b22015-12-15 12:20:13 -080068
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 // PrepareAddress will attempt to get an address for this port that other
70 // clients can send to. It may take some time before the address is ready.
71 // Once it is ready, we will send SignalAddressReady. If errors are
72 // preventing the port from getting an address, it may send
73 // SignalAddressError.
74 virtual void PrepareAddress() = 0;
75
76 // Returns the connection to the given address or NULL if none exists.
Yves Gerey665174f2018-06-19 15:03:05 +020077 virtual Connection* GetConnection(const rtc::SocketAddress& remote_addr) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078
79 // Creates a new connection to the given address.
80 enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE };
Yves Gerey665174f2018-06-19 15:03:05 +020081 virtual Connection* CreateConnection(const Candidate& remote_candidate,
82 CandidateOrigin origin) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083
84 // Functions on the underlying socket(s).
85 virtual int SetOption(rtc::Socket::Option opt, int value) = 0;
86 virtual int GetOption(rtc::Socket::Option opt, int* value) = 0;
87 virtual int GetError() = 0;
88
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070089 virtual ProtocolType GetProtocol() const = 0;
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 virtual const std::vector<Candidate>& Candidates() const = 0;
92
93 // Sends the given packet to the given address, provided that the address is
94 // that of a connection or an address that has sent to us already.
Yves Gerey665174f2018-06-19 15:03:05 +020095 virtual int SendTo(const void* data,
96 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 const rtc::SocketAddress& addr,
Yves Gerey665174f2018-06-19 15:03:05 +020098 const rtc::PacketOptions& options,
99 bool payload) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
101 // Indicates that we received a successful STUN binding request from an
102 // address that doesn't correspond to any current connection. To turn this
103 // into a real connection, call CreateConnection.
Yves Gerey665174f2018-06-19 15:03:05 +0200104 sigslot::signal6<PortInterface*,
105 const rtc::SocketAddress&,
106 ProtocolType,
107 IceMessage*,
108 const std::string&,
109 bool>
110 SignalUnknownAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111
112 // Sends a response message (normal or error) to the given request. One of
113 // these methods should be called as a response to SignalUnknownAddress.
Tommi278b19d2022-04-12 14:03:40 +0200114 virtual void SendBindingErrorResponse(StunMessage* message,
Yves Gerey665174f2018-06-19 15:03:05 +0200115 const rtc::SocketAddress& addr,
116 int error_code,
117 const std::string& reason) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118
119 // Signaled when this port decides to delete itself because it no longer has
120 // any usefulness.
Lahiru Ginnaliya Gamathige3ba7beb2021-02-01 02:06:11 -0800121 virtual void SubscribePortDestroyed(
122 std::function<void(PortInterface*)> callback) = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
124 // Signaled when Port discovers ice role conflict with the peer.
125 sigslot::signal1<PortInterface*> SignalRoleConflict;
126
127 // Normally, packets arrive through a connection (or they result signaling of
128 // unknown address). Calling this method turns off delivery of packets
129 // through their respective connection and instead delivers every packet
130 // through this port.
131 virtual void EnablePortPackets() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200132 sigslot::
133 signal4<PortInterface*, const char*, size_t, const rtc::SocketAddress&>
134 SignalReadPacket;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135
stefanc1aeaf02015-10-15 07:26:07 -0700136 // Emitted each time a packet is sent on this port.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100137 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
stefanc1aeaf02015-10-15 07:26:07 -0700138
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 virtual std::string ToString() const = 0;
140
Danil Chapovalov00c71832018-06-15 15:58:38 +0200141 virtual void GetStunStats(absl::optional<StunStats>* stats) = 0;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800142
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 protected:
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700144 PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145};
146
147} // namespace cricket
148
Steve Anton10542f22019-01-11 09:11:00 -0800149#endif // P2P_BASE_PORT_INTERFACE_H_