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