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