blob: 7df4543e6e90a353e634caf94a7d5a10513f9b3c [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
Danil Chapovalov00c71832018-06-15 15:58:38 +020017#include "absl/types/optional.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080018#include "api/candidate.h"
19#include "p2p/base/transportdescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/asyncpacketsocket.h"
21#include "rtc_base/socketaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
23namespace rtc {
24class Network;
25struct PacketOptions;
26}
27
28namespace cricket {
29class Connection;
30class IceMessage;
31class StunMessage;
Qingsi Wang72a43a12018-02-20 16:03:18 -080032class StunStats;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033
34enum ProtocolType {
35 PROTO_UDP,
36 PROTO_TCP,
hnsl277b2502016-12-13 05:17:23 -080037 PROTO_SSLTCP, // Pseudo-TLS.
38 PROTO_TLS,
39 PROTO_LAST = PROTO_TLS
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040};
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.
45class PortInterface {
46 public:
Steve Anton1cf1b7d2017-10-30 10:00:15 -070047 virtual ~PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048
49 virtual const std::string& Type() const = 0;
50 virtual rtc::Network* Network() const = 0;
51
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 // Methods to set/get ICE role and tiebreaker values.
53 virtual void SetIceRole(IceRole role) = 0;
54 virtual IceRole GetIceRole() const = 0;
55
Peter Boström0c4e06b2015-10-07 12:23:21 +020056 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
57 virtual uint64_t IceTiebreaker() const = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058
59 virtual bool SharedSocket() const = 0;
60
Honghai Zhangf9945b22015-12-15 12:20:13 -080061 virtual bool SupportsProtocol(const std::string& protocol) const = 0;
62
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 // 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(
72 const rtc::SocketAddress& remote_addr) = 0;
73
74 // Creates a new connection to the given address.
75 enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE };
76 virtual Connection* CreateConnection(
77 const Candidate& remote_candidate, CandidateOrigin origin) = 0;
78
79 // Functions on the underlying socket(s).
80 virtual int SetOption(rtc::Socket::Option opt, int value) = 0;
81 virtual int GetOption(rtc::Socket::Option opt, int* value) = 0;
82 virtual int GetError() = 0;
83
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070084 virtual ProtocolType GetProtocol() const = 0;
85
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086 virtual const std::vector<Candidate>& Candidates() const = 0;
87
88 // Sends the given packet to the given address, provided that the address is
89 // that of a connection or an address that has sent to us already.
90 virtual int SendTo(const void* data, size_t size,
91 const rtc::SocketAddress& addr,
92 const rtc::PacketOptions& options, bool payload) = 0;
93
94 // Indicates that we received a successful STUN binding request from an
95 // address that doesn't correspond to any current connection. To turn this
96 // into a real connection, call CreateConnection.
97 sigslot::signal6<PortInterface*, const rtc::SocketAddress&,
98 ProtocolType, IceMessage*, const std::string&,
99 bool> SignalUnknownAddress;
100
101 // Sends a response message (normal or error) to the given request. One of
102 // these methods should be called as a response to SignalUnknownAddress.
103 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
104 virtual void SendBindingResponse(StunMessage* request,
105 const rtc::SocketAddress& addr) = 0;
106 virtual void SendBindingErrorResponse(
107 StunMessage* request, const rtc::SocketAddress& addr,
108 int error_code, const std::string& reason) = 0;
109
110 // Signaled when this port decides to delete itself because it no longer has
111 // any usefulness.
112 sigslot::signal1<PortInterface*> SignalDestroyed;
113
114 // Signaled when Port discovers ice role conflict with the peer.
115 sigslot::signal1<PortInterface*> SignalRoleConflict;
116
117 // Normally, packets arrive through a connection (or they result signaling of
118 // unknown address). Calling this method turns off delivery of packets
119 // through their respective connection and instead delivers every packet
120 // through this port.
121 virtual void EnablePortPackets() = 0;
122 sigslot::signal4<PortInterface*, const char*, size_t,
123 const rtc::SocketAddress&> SignalReadPacket;
124
stefanc1aeaf02015-10-15 07:26:07 -0700125 // Emitted each time a packet is sent on this port.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100126 sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
stefanc1aeaf02015-10-15 07:26:07 -0700127
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 virtual std::string ToString() const = 0;
129
Danil Chapovalov00c71832018-06-15 15:58:38 +0200130 virtual void GetStunStats(absl::optional<StunStats>* stats) = 0;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800131
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 protected:
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700133 PortInterface();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134};
135
136} // namespace cricket
137
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200138#endif // P2P_BASE_PORTINTERFACE_H_