henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef P2P_BASE_PORT_INTERFACE_H_ |
| 12 | #define P2P_BASE_PORT_INTERFACE_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
Lahiru Ginnaliya Gamathige | 3ba7beb | 2021-02-01 02:06:11 -0800 | [diff] [blame] | 15 | #include <utility> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 16 | #include <vector> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 19 | #include "api/candidate.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "p2p/base/transport_description.h" |
| 21 | #include "rtc_base/async_packet_socket.h" |
Lahiru Ginnaliya Gamathige | 3ba7beb | 2021-02-01 02:06:11 -0800 | [diff] [blame] | 22 | #include "rtc_base/callback_list.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/socket_address.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | |
| 25 | namespace rtc { |
| 26 | class Network; |
| 27 | struct PacketOptions; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | } // namespace rtc |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 29 | |
| 30 | namespace cricket { |
| 31 | class Connection; |
| 32 | class IceMessage; |
| 33 | class StunMessage; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 34 | class StunStats; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | |
| 36 | enum ProtocolType { |
| 37 | PROTO_UDP, |
| 38 | PROTO_TCP, |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 39 | PROTO_SSLTCP, // Pseudo-TLS. |
| 40 | PROTO_TLS, |
| 41 | PROTO_LAST = PROTO_TLS |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Defines the interface for a port, which represents a local communication |
| 45 | // mechanism that can be used to create connections to similar mechanisms of |
| 46 | // the other client. Various types of ports will implement this interface. |
| 47 | class PortInterface { |
| 48 | public: |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 49 | virtual ~PortInterface(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | |
| 51 | virtual const std::string& Type() const = 0; |
Niels Möller | e0c6bdf | 2022-03-24 15:18:02 +0100 | [diff] [blame] | 52 | virtual const rtc::Network* Network() const = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | // Methods to set/get ICE role and tiebreaker values. |
| 55 | virtual void SetIceRole(IceRole role) = 0; |
| 56 | virtual IceRole GetIceRole() const = 0; |
| 57 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 58 | virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0; |
| 59 | virtual uint64_t IceTiebreaker() const = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | |
| 61 | virtual bool SharedSocket() const = 0; |
| 62 | |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 63 | virtual bool SupportsProtocol(const std::string& protocol) const = 0; |
| 64 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | // PrepareAddress will attempt to get an address for this port that other |
| 66 | // clients can send to. It may take some time before the address is ready. |
| 67 | // Once it is ready, we will send SignalAddressReady. If errors are |
| 68 | // preventing the port from getting an address, it may send |
| 69 | // SignalAddressError. |
| 70 | virtual void PrepareAddress() = 0; |
| 71 | |
| 72 | // Returns the connection to the given address or NULL if none exists. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | virtual Connection* GetConnection(const rtc::SocketAddress& remote_addr) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 74 | |
| 75 | // Creates a new connection to the given address. |
| 76 | enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | virtual Connection* CreateConnection(const Candidate& remote_candidate, |
| 78 | CandidateOrigin origin) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | |
| 80 | // Functions on the underlying socket(s). |
| 81 | virtual int SetOption(rtc::Socket::Option opt, int value) = 0; |
| 82 | virtual int GetOption(rtc::Socket::Option opt, int* value) = 0; |
| 83 | virtual int GetError() = 0; |
| 84 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 85 | virtual ProtocolType GetProtocol() const = 0; |
| 86 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 87 | virtual const std::vector<Candidate>& Candidates() const = 0; |
| 88 | |
| 89 | // Sends the given packet to the given address, provided that the address is |
| 90 | // that of a connection or an address that has sent to us already. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 91 | virtual int SendTo(const void* data, |
| 92 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | const rtc::SocketAddress& addr, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | const rtc::PacketOptions& options, |
| 95 | bool payload) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | |
| 97 | // Indicates that we received a successful STUN binding request from an |
| 98 | // address that doesn't correspond to any current connection. To turn this |
| 99 | // into a real connection, call CreateConnection. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | sigslot::signal6<PortInterface*, |
| 101 | const rtc::SocketAddress&, |
| 102 | ProtocolType, |
| 103 | IceMessage*, |
| 104 | const std::string&, |
| 105 | bool> |
| 106 | SignalUnknownAddress; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | |
| 108 | // Sends a response message (normal or error) to the given request. One of |
| 109 | // these methods should be called as a response to SignalUnknownAddress. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 110 | virtual void SendBindingErrorResponse(StunMessage* request, |
| 111 | const rtc::SocketAddress& addr, |
| 112 | int error_code, |
| 113 | const std::string& reason) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 114 | |
| 115 | // Signaled when this port decides to delete itself because it no longer has |
| 116 | // any usefulness. |
Lahiru Ginnaliya Gamathige | 3ba7beb | 2021-02-01 02:06:11 -0800 | [diff] [blame] | 117 | virtual void SubscribePortDestroyed( |
| 118 | std::function<void(PortInterface*)> callback) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | |
| 120 | // Signaled when Port discovers ice role conflict with the peer. |
| 121 | sigslot::signal1<PortInterface*> SignalRoleConflict; |
| 122 | |
| 123 | // Normally, packets arrive through a connection (or they result signaling of |
| 124 | // unknown address). Calling this method turns off delivery of packets |
| 125 | // through their respective connection and instead delivers every packet |
| 126 | // through this port. |
| 127 | virtual void EnablePortPackets() = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | sigslot:: |
| 129 | signal4<PortInterface*, const char*, size_t, const rtc::SocketAddress&> |
| 130 | SignalReadPacket; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 131 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 132 | // Emitted each time a packet is sent on this port. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 133 | sigslot::signal1<const rtc::SentPacket&> SignalSentPacket; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 134 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 135 | virtual std::string ToString() const = 0; |
| 136 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 137 | virtual void GetStunStats(absl::optional<StunStats>* stats) = 0; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 138 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 139 | protected: |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 140 | PortInterface(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace cricket |
| 144 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 145 | #endif // P2P_BASE_PORT_INTERFACE_H_ |