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