/[resiprocate]/main/reflow/Flow.hxx
ViewVC logotype

Contents of /main/reflow/Flow.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10728 - (show annotations) (download)
Tue Dec 10 07:48:27 2013 UTC (6 years, 1 month ago) by dpocock
File MIME type: text/plain
File size: 10180 byte(s)
reflow: return const char* pointers
1 #if !defined(Flow_hxx)
2 #define Flow_hxx
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <map>
9 #include <rutil/TimeLimitFifo.hxx>
10 #include <rutil/Mutex.hxx>
11
12 #ifdef WIN32
13 #include <srtp.h>
14 #else
15 #include <srtp/srtp.h>
16 #endif
17 #include <boost/shared_ptr.hpp>
18
19 #include "reTurn/client/TurnAsyncUdpSocket.hxx"
20 #include "reTurn/client/TurnAsyncTcpSocket.hxx"
21 #include "reTurn/client/TurnAsyncTlsSocket.hxx"
22 #include "reTurn/client/TurnAsyncSocketHandler.hxx"
23 #include "reTurn/StunMessage.hxx"
24 #include "FakeSelectSocketDescriptor.hxx"
25 #include "dtls_wrapper/DtlsSocket.hxx"
26
27 using namespace reTurn;
28
29 namespace flowmanager
30 {
31
32 /**
33 This class represents a Flow that is created by the Flow Manager. A flow is a
34 bi-directional stream of data for communicating with an endpoint, that may use
35 UDP, TCP or TLS over TCP. A flow may also use a Turn Allocation to transmit
36 data to/from an endpoint.
37
38 Author: Scott Godin (sgodin AT SipSpectrum DOT com)
39 */
40 class MediaStream;
41 class Flow;
42
43 class Flow : public TurnAsyncSocketHandler
44 {
45 public:
46
47 enum FlowState
48 {
49 Unconnected,
50 ConnectingServer,
51 Connecting,
52 Binding,
53 Allocating,
54 Connected,
55 Ready
56 };
57
58 Flow(asio::io_service& ioService,
59 asio::ssl::context& sslContext,
60 unsigned int componentId,
61 const StunTuple& localBinding,
62 MediaStream& mediaStream);
63 ~Flow();
64
65 void activateFlow(UInt8 allocationProps = StunMessage::PropsNone);
66 void activateFlow(UInt64 reservationToken);
67
68 bool isReady() { return mFlowState == Ready; }
69
70 /// Returns a socket descriptor that can be used in a select call
71 /// WARNING - this descriptor should not be used for any other purpose
72 /// - do NOT set socket options, or send, receive from this descriptor,
73 /// instead use the Flow api's
74 unsigned int getSelectSocketDescriptor();
75
76 unsigned int getSocketDescriptor(); // returns the real socket descriptor - used to correlate callbacks
77
78 /// Turn Send Methods
79 /// WARNING - if using Secure media, then there must be room at the
80 /// end of the passed in buffer for the SRTP HMAC code to be appended
81 /// ***It would be good to make this safer***
82 void send(char* buffer, unsigned int size);
83 void sendTo(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int size);
84 void rawSendTo(const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
85
86 /// Receive Methods
87 asio::error_code receive(char* buffer, unsigned int& size, unsigned int timeout, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
88 asio::error_code receiveFrom(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int& size, unsigned int timeout);
89
90 /// Used to set where this flow should be sending to
91 void setActiveDestination(const char* address, unsigned short port);
92
93 /// Dtls-Srtp Methods
94
95 /// Starts the dtls client handshake process - (must call setActiveDestination first)
96 /// Call this method if this client has negotiated the "Active" role via SDP
97 void startDtlsClient(const char* address, unsigned short port);
98
99 /// This method should be called when remote fingerprint is discovered
100 /// via SDP negotiation. After this is called only dtls-srtp connections
101 /// with a matching fingerprint will be maintained.
102 void setRemoteSDPFingerprint(const resip::Data& fingerprint);
103
104 /// Retrieves the stored remote SDP Fingerprint.
105 const resip::Data getRemoteSDPFingerprint();
106
107 const StunTuple& getLocalTuple();
108 StunTuple getSessionTuple(); // returns either local, reflexive, or relay tuple depending on NatTraversalMode
109 StunTuple getRelayTuple();
110 StunTuple getReflexiveTuple();
111 UInt64 getReservationToken();
112 unsigned int getComponentId() { return mComponentId; }
113
114 private:
115 asio::io_service& mIOService;
116 asio::ssl::context& mSslContext;
117
118 // Note: these member variables are set at creation time and never changed, thus
119 // they do not require mutex protection
120 unsigned int mComponentId;
121 StunTuple mLocalBinding;
122
123 // MediaStream that this Flow belongs too
124 MediaStream& mMediaStream;
125
126 // mTurnSocket has it's own threading protection
127 boost::shared_ptr<TurnAsyncSocket> mTurnSocket;
128
129 // These are only set once, then accessed - thus they do not require mutex protection
130 UInt8 mAllocationProps;
131 UInt64 mReservationToken;
132
133 // Mutex to protect the following members that may be get/set from multiple threads
134 resip::Mutex mMutex;
135 StunTuple mReflexiveTuple;
136 StunTuple mRelayTuple;
137 resip::Data mRemoteSDPFingerprint;
138
139 // Map to store all DtlsSockets - in forking cases there can be more than one
140 std::map<reTurn::StunTuple, dtls::DtlsSocket*> mDtlsSockets;
141 dtls::DtlsSocket* getDtlsSocket(const reTurn::StunTuple& endpoint);
142 dtls::DtlsSocket* createDtlsSocketClient(const StunTuple& endpoint);
143 dtls::DtlsSocket* createDtlsSocketServer(const StunTuple& endpoint);
144
145 volatile FlowState mFlowState;
146 void changeFlowState(FlowState newState);
147 const char* flowStateToString(FlowState state);
148
149 class ReceivedData
150 {
151 public:
152 ReceivedData(const asio::ip::address& address, unsigned short port, boost::shared_ptr<DataBuffer>& data) :
153 mAddress(address), mPort(port), mData(data) {}
154 ~ReceivedData() {}
155
156 asio::ip::address mAddress;
157 unsigned short mPort;
158 boost::shared_ptr<DataBuffer> mData;
159 };
160 // FIFO for received data
161 typedef resip::TimeLimitFifo<ReceivedData> ReceivedDataFifo;
162 ReceivedDataFifo mReceivedDataFifo;
163
164 // Helpers to perform SRTP protection/unprotection
165 bool processSendData(char* buffer, unsigned int& size, const asio::ip::address& address, unsigned short port);
166 asio::error_code processReceivedData(char* buffer, unsigned int& size, ReceivedData* receivedData, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
167 FakeSelectSocketDescriptor mFakeSelectSocketDescriptor;
168
169 virtual void onConnectSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port);
170 virtual void onConnectFailure(unsigned int socketDesc, const asio::error_code& e);
171
172 virtual void onSharedSecretSuccess(unsigned int socketDesc, const char* username, unsigned int usernameSize, const char* password, unsigned int passwordSize);
173 virtual void onSharedSecretFailure(unsigned int socketDesc, const asio::error_code& e);
174
175 virtual void onBindSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& stunServerTuple);
176 virtual void onBindFailure(unsigned int socketDesc, const asio::error_code& e, const StunTuple& stunServerTuple);
177
178 virtual void onAllocationSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& relayTuple, unsigned int lifetime, unsigned int bandwidth, UInt64 reservationToken);
179 virtual void onAllocationFailure(unsigned int socketDesc, const asio::error_code& e);
180
181 virtual void onRefreshSuccess(unsigned int socketDesc, unsigned int lifetime);
182 virtual void onRefreshFailure(unsigned int socketDesc, const asio::error_code& e);
183
184 virtual void onSetActiveDestinationSuccess(unsigned int socketDesc);
185 virtual void onSetActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e);
186 virtual void onClearActiveDestinationSuccess(unsigned int socketDesc);
187 virtual void onClearActiveDestinationFailure(unsigned int socketDesc, const asio::error_code &e);
188
189 virtual void onChannelBindRequestSent(unsigned int socketDesc, unsigned short channelNumber);
190 virtual void onChannelBindSuccess(unsigned int socketDesc, unsigned short channelNumber);
191 virtual void onChannelBindFailure(unsigned int socketDesc, const asio::error_code& e);
192
193 //virtual void onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
194 virtual void onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, boost::shared_ptr<DataBuffer>& data);
195 virtual void onReceiveFailure(unsigned int socketDesc, const asio::error_code& e);
196
197 virtual void onSendSuccess(unsigned int socketDesc);
198 virtual void onSendFailure(unsigned int socketDesc, const asio::error_code& e);
199
200 virtual void onIncomingBindRequestProcessed(unsigned int socketDesc, const StunTuple& sourceTuple);
201 };
202
203 }
204
205 #endif
206
207
208 /* ====================================================================
209
210 Copyright (c) 2007-2008, Plantronics, Inc.
211 All rights reserved.
212
213 Redistribution and use in source and binary forms, with or without
214 modification, are permitted provided that the following conditions are
215 met:
216
217 1. Redistributions of source code must retain the above copyright
218 notice, this list of conditions and the following disclaimer.
219
220 2. Redistributions in binary form must reproduce the above copyright
221 notice, this list of conditions and the following disclaimer in the
222 documentation and/or other materials provided with the distribution.
223
224 3. Neither the name of Plantronics nor the names of its contributors
225 may be used to endorse or promote products derived from this
226 software without specific prior written permission.
227
228 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
229 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
230 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
232 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
233 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
238 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
239
240 ==================================================================== */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27