blob: 6b97fecace7af7911a3bc332c468452ccc8de7a8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_PORTINTERFACE_H_
12#define P2P_BASE_PORTINTERFACE_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <string>
Steve Anton6c38cc72017-11-29 10:25:58 -080015#include <vector>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "p2p/base/jseptransport.h"
18#include "rtc_base/asyncpacketsocket.h"
19#include "rtc_base/socketaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020
21namespace rtc {
22class Network;
23struct PacketOptions;
24}
25
26namespace cricket {
27class Connection;
28class IceMessage;
29class StunMessage;
30
31enum ProtocolType {
32 PROTO_UDP,
33 PROTO_TCP,
hnsl277b2502016-12-13 05:17:23 -080034 PROTO_SSLTCP, // Pseudo-TLS.
35 PROTO_TLS,
36 PROTO_LAST = PROTO_TLS
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037};
38
39// Defines the interface for a port, which represents a local communication
40// mechanism that can be used to create connections to similar mechanisms of
41// the other client. Various types of ports will implement this interface.
42class PortInterface {
43 public:
Steve Anton1cf1b7d2017-10-30 10:00:15 -070044 virtual ~PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045
46 virtual const std::string& Type() const = 0;
47 virtual rtc::Network* Network() const = 0;
48
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 // Methods to set/get ICE role and tiebreaker values.
50 virtual void SetIceRole(IceRole role) = 0;
51 virtual IceRole GetIceRole() const = 0;
52
Peter Boström0c4e06b2015-10-07 12:23:21 +020053 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
54 virtual uint64_t IceTiebreaker() const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055
56 virtual bool SharedSocket() const = 0;
57
Honghai Zhangf9945b22015-12-15 12:20:13 -080058 virtual bool SupportsProtocol(const std::string& protocol) const = 0;
59
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 // PrepareAddress will attempt to get an address for this port that other
61 // clients can send to. It may take some time before the address is ready.
62 // Once it is ready, we will send SignalAddressReady. If errors are
63 // preventing the port from getting an address, it may send
64 // SignalAddressError.
65 virtual void PrepareAddress() = 0;
66
67 // Returns the connection to the given address or NULL if none exists.
68 virtual Connection* GetConnection(
69 const rtc::SocketAddress& remote_addr) = 0;
70
71 // Creates a new connection to the given address.
72 enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE };
73 virtual Connection* CreateConnection(
74 const Candidate& remote_candidate, CandidateOrigin origin) = 0;
75
76 // Functions on the underlying socket(s).
77 virtual int SetOption(rtc::Socket::Option opt, int value) = 0;
78 virtual int GetOption(rtc::Socket::Option opt, int* value) = 0;
79 virtual int GetError() = 0;
80
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070081 virtual ProtocolType GetProtocol() const = 0;
82
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083 virtual const std::vector<Candidate>& Candidates() const = 0;
84
85 // Sends the given packet to the given address, provided that the address is
86 // that of a connection or an address that has sent to us already.
87 virtual int SendTo(const void* data, size_t size,
88 const rtc::SocketAddress& addr,
89 const rtc::PacketOptions& options, bool payload) = 0;
90
91 // Indicates that we received a successful STUN binding request from an
92 // address that doesn't correspond to any current connection. To turn this
93 // into a real connection, call CreateConnection.
94 sigslot::signal6<PortInterface*, const rtc::SocketAddress&,
95 ProtocolType, IceMessage*, const std::string&,
96 bool> SignalUnknownAddress;
97
98 // Sends a response message (normal or error) to the given request. One of
99 // these methods should be called as a response to SignalUnknownAddress.
100 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
101 virtual void SendBindingResponse(StunMessage* request,
102 const rtc::SocketAddress& addr) = 0;
103 virtual void SendBindingErrorResponse(
104 StunMessage* request, const rtc::SocketAddress& addr,
105 int error_code, const std::string& reason) = 0;
106
107 // Signaled when this port decides to delete itself because it no longer has
108 // any usefulness.
109 sigslot::signal1<PortInterface*> SignalDestroyed;
110
111 // Signaled when Port discovers ice role conflict with the peer.
112 sigslot::signal1<PortInterface*> SignalRoleConflict;
113
114 // Normally, packets arrive through a connection (or they result signaling of
115 // unknown address). Calling this method turns off delivery of packets
116 // through their respective connection and instead delivers every packet
117 // through this port.
118 virtual void EnablePortPackets() = 0;
119 sigslot::signal4<PortInterface*, const char*, size_t,
120 const rtc::SocketAddress&> SignalReadPacket;
121
stefanc1aeaf02015-10-15 07:26:07 -0700122 // Emitted each time a packet is sent on this port.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100123 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
stefanc1aeaf02015-10-15 07:26:07 -0700124
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 virtual std::string ToString() const = 0;
126
127 protected:
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700128 PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129};
130
131} // namespace cricket
132
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // P2P_BASE_PORTINTERFACE_H_