blob: 6ea63466c9d3e811a334f12e2fce3c4c945cb6b3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_P2P_BASE_PORTINTERFACE_H_
29#define TALK_P2P_BASE_PORTINTERFACE_H_
30
31#include <string>
32
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000033#include "talk/base/dscp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/base/socketaddress.h"
35#include "talk/p2p/base/transport.h"
36
37namespace talk_base {
38class Network;
39class PacketSocketFactory;
40}
41
42namespace cricket {
43class Connection;
44class IceMessage;
45class StunMessage;
46
47enum ProtocolType {
48 PROTO_UDP,
49 PROTO_TCP,
50 PROTO_SSLTCP,
51 PROTO_LAST = PROTO_SSLTCP
52};
53
54// Defines the interface for a port, which represents a local communication
55// mechanism that can be used to create connections to similar mechanisms of
56// the other client. Various types of ports will implement this interface.
57class PortInterface {
58 public:
59 virtual ~PortInterface() {}
60
61 virtual const std::string& Type() const = 0;
62 virtual talk_base::Network* Network() const = 0;
63
64 virtual void SetIceProtocolType(IceProtocolType protocol) = 0;
65 virtual IceProtocolType IceProtocol() const = 0;
66
67 // Methods to set/get ICE role and tiebreaker values.
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000068 virtual void SetIceRole(IceRole role) = 0;
69 virtual IceRole GetIceRole() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000071 virtual void SetIceTiebreaker(uint64 tiebreaker) = 0;
72 virtual uint64 IceTiebreaker() const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74 virtual bool SharedSocket() const = 0;
75
76 // PrepareAddress will attempt to get an address for this port that other
77 // clients can send to. It may take some time before the address is ready.
78 // Once it is ready, we will send SignalAddressReady. If errors are
79 // preventing the port from getting an address, it may send
80 // SignalAddressError.
81 virtual void PrepareAddress() = 0;
82
83 // Returns the connection to the given address or NULL if none exists.
84 virtual Connection* GetConnection(
85 const talk_base::SocketAddress& remote_addr) = 0;
86
87 // Creates a new connection to the given address.
88 enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE };
89 virtual Connection* CreateConnection(
90 const Candidate& remote_candidate, CandidateOrigin origin) = 0;
91
92 // Functions on the underlying socket(s).
93 virtual int SetOption(talk_base::Socket::Option opt, int value) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 virtual int GetOption(talk_base::Socket::Option opt, int* value) = 0;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000095 virtual int GetError() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 virtual const std::vector<Candidate>& Candidates() const = 0;
98
99 // Sends the given packet to the given address, provided that the address is
100 // that of a connection or an address that has sent to us already.
101 virtual int SendTo(const void* data, size_t size,
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000102 const talk_base::SocketAddress& addr,
103 talk_base::DiffServCodePoint dscp, bool payload) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105 // Indicates that we received a successful STUN binding request from an
106 // address that doesn't correspond to any current connection. To turn this
107 // into a real connection, call CreateConnection.
108 sigslot::signal6<PortInterface*, const talk_base::SocketAddress&,
109 ProtocolType, IceMessage*, const std::string&,
110 bool> SignalUnknownAddress;
111
112 // Sends a response message (normal or error) to the given request. One of
113 // these methods should be called as a response to SignalUnknownAddress.
114 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
115 virtual void SendBindingResponse(StunMessage* request,
116 const talk_base::SocketAddress& addr) = 0;
117 virtual void SendBindingErrorResponse(
118 StunMessage* request, const talk_base::SocketAddress& addr,
119 int error_code, const std::string& reason) = 0;
120
121 // Signaled when this port decides to delete itself because it no longer has
122 // any usefulness.
123 sigslot::signal1<PortInterface*> SignalDestroyed;
124
125 // Signaled when Port discovers ice role conflict with the peer.
126 sigslot::signal1<PortInterface*> SignalRoleConflict;
127
128 // Normally, packets arrive through a connection (or they result signaling of
129 // unknown address). Calling this method turns off delivery of packets
130 // through their respective connection and instead delivers every packet
131 // through this port.
132 virtual void EnablePortPackets() = 0;
133 sigslot::signal4<PortInterface*, const char*, size_t,
134 const talk_base::SocketAddress&> SignalReadPacket;
135
136 virtual std::string ToString() const = 0;
137
138 protected:
139 PortInterface() {}
140};
141
142} // namespace cricket
143
144#endif // TALK_P2P_BASE_PORTINTERFACE_H_