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 11 #ifndef P2P_BASE_PORT_INTERFACE_H_ 12 #define P2P_BASE_PORT_INTERFACE_H_ 13 14 #include <string> 15 #include <utility> 16 #include <vector> 17 18 #include "absl/strings/string_view.h" 19 #include "absl/types/optional.h" 20 #include "api/candidate.h" 21 #include "p2p/base/transport_description.h" 22 #include "rtc_base/async_packet_socket.h" 23 #include "rtc_base/callback_list.h" 24 #include "rtc_base/socket_address.h" 25 26 namespace rtc { 27 class Network; 28 struct PacketOptions; 29 } // namespace rtc 30 31 namespace cricket { 32 class Connection; 33 class IceMessage; 34 class StunMessage; 35 class StunStats; 36 37 enum ProtocolType { 38 PROTO_UDP, 39 PROTO_TCP, 40 PROTO_SSLTCP, // Pseudo-TLS. 41 PROTO_TLS, 42 PROTO_LAST = PROTO_TLS 43 }; 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. 48 class PortInterface { 49 public: 50 virtual ~PortInterface(); 51 52 virtual const std::string& Type() const = 0; 53 virtual const rtc::Network* Network() const = 0; 54 55 // Methods to set/get ICE role and tiebreaker values. 56 virtual void SetIceRole(IceRole role) = 0; 57 virtual IceRole GetIceRole() const = 0; 58 59 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0; 60 virtual uint64_t IceTiebreaker() const = 0; 61 62 virtual bool SharedSocket() const = 0; 63 64 virtual bool SupportsProtocol(absl::string_view protocol) const = 0; 65 66 // PrepareAddress will attempt to get an address for this port that other 67 // clients can send to. It may take some time before the address is ready. 68 // Once it is ready, we will send SignalAddressReady. If errors are 69 // preventing the port from getting an address, it may send 70 // SignalAddressError. 71 virtual void PrepareAddress() = 0; 72 73 // Returns the connection to the given address or NULL if none exists. 74 virtual Connection* GetConnection(const rtc::SocketAddress& remote_addr) = 0; 75 76 // Creates a new connection to the given address. 77 enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE }; 78 virtual Connection* CreateConnection(const Candidate& remote_candidate, 79 CandidateOrigin origin) = 0; 80 81 // Functions on the underlying socket(s). 82 virtual int SetOption(rtc::Socket::Option opt, int value) = 0; 83 virtual int GetOption(rtc::Socket::Option opt, int* value) = 0; 84 virtual int GetError() = 0; 85 86 virtual ProtocolType GetProtocol() const = 0; 87 88 virtual const std::vector<Candidate>& Candidates() const = 0; 89 90 // Sends the given packet to the given address, provided that the address is 91 // that of a connection or an address that has sent to us already. 92 virtual int SendTo(const void* data, 93 size_t size, 94 const rtc::SocketAddress& addr, 95 const rtc::PacketOptions& options, 96 bool payload) = 0; 97 98 // Indicates that we received a successful STUN binding request from an 99 // address that doesn't correspond to any current connection. To turn this 100 // into a real connection, call CreateConnection. 101 sigslot::signal6<PortInterface*, 102 const rtc::SocketAddress&, 103 ProtocolType, 104 IceMessage*, 105 const std::string&, 106 bool> 107 SignalUnknownAddress; 108 109 // Sends a response message (normal or error) to the given request. One of 110 // these methods should be called as a response to SignalUnknownAddress. 111 virtual void SendBindingErrorResponse(StunMessage* message, 112 const rtc::SocketAddress& addr, 113 int error_code, 114 absl::string_view reason) = 0; 115 116 // Signaled when this port decides to delete itself because it no longer has 117 // any usefulness. 118 virtual void SubscribePortDestroyed( 119 std::function<void(PortInterface*)> callback) = 0; 120 121 // Signaled when Port discovers ice role conflict with the peer. 122 sigslot::signal1<PortInterface*> SignalRoleConflict; 123 124 // Normally, packets arrive through a connection (or they result signaling of 125 // unknown address). Calling this method turns off delivery of packets 126 // through their respective connection and instead delivers every packet 127 // through this port. 128 virtual void EnablePortPackets() = 0; 129 sigslot:: 130 signal4<PortInterface*, const char*, size_t, const rtc::SocketAddress&> 131 SignalReadPacket; 132 133 // Emitted each time a packet is sent on this port. 134 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket; 135 136 virtual std::string ToString() const = 0; 137 138 virtual void GetStunStats(absl::optional<StunStats>* stats) = 0; 139 140 protected: 141 PortInterface(); 142 }; 143 144 } // namespace cricket 145 146 #endif // P2P_BASE_PORT_INTERFACE_H_ 147