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