~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to reTurn/client/TurnSocket.hxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef TURNSOCKET_HXX
 
2
#define TURNSOCKET_HXX
 
3
 
 
4
#if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
 
5
  && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
 
6
  && !defined(ASIO_ENABLE_CANCELIO)
 
7
#error You must define ASIO_ENABLE_CANCELIO in your build settings.
 
8
#endif
 
9
 
 
10
#include <vector>
 
11
#include <asio.hpp>
 
12
#include <rutil/Data.hxx>
 
13
#include <rutil/Mutex.hxx>
 
14
 
 
15
#include "../StunTuple.hxx"
 
16
#include "../StunMessage.hxx"
 
17
#include "../ChannelManager.hxx"
 
18
 
 
19
namespace reTurn {
 
20
 
 
21
class TurnSocket
 
22
{
 
23
public:
 
24
   static unsigned int UnspecifiedLifetime;
 
25
   static unsigned int UnspecifiedBandwidth;
 
26
   static unsigned short UnspecifiedToken;
 
27
   static asio::ip::address UnspecifiedIpAddress;
 
28
 
 
29
   explicit TurnSocket(const asio::ip::address& address = UnspecifiedIpAddress, 
 
30
                       unsigned short port = 0);
 
31
   virtual ~TurnSocket();
 
32
 
 
33
   virtual unsigned int getSocketDescriptor() = 0;
 
34
   virtual asio::error_code connect(const std::string& address, unsigned short port) = 0;  // !slg! modify port parameter later to be able to do SRV lookups
 
35
 
 
36
   // Note: Shared Secret requests have been deprecated by RFC5389, and not 
 
37
   //       widely implemented in RFC3489 - so not really needed at all
 
38
   asio::error_code requestSharedSecret(char* username, unsigned int usernameSize, 
 
39
                                        char* password, unsigned int passwordSize);
 
40
 
 
41
   // Set the username and password for all future requests
 
42
   void setUsernameAndPassword(const char* username, const char* password, bool shortTermAuth=false);
 
43
 
 
44
   // Stun Binding Method - use getReflexiveTuple() to get binding info
 
45
   asio::error_code bindRequest();
 
46
 
 
47
   // Turn Allocation Methods
 
48
   asio::error_code createAllocation(unsigned int lifetime = UnspecifiedLifetime,
 
49
                                     unsigned int bandwidth = UnspecifiedBandwidth,
 
50
                                     unsigned char requestedProps = StunMessage::PropsNone, 
 
51
                                     UInt64 reservationToken = UnspecifiedToken,
 
52
                                     StunTuple::TransportType requestedTransportType = StunTuple::None);
 
53
   asio::error_code refreshAllocation();
 
54
   asio::error_code destroyAllocation();
 
55
 
 
56
   // Accessors for properties associated with an allocation
 
57
   StunTuple& getRelayTuple();
 
58
   StunTuple& getReflexiveTuple();
 
59
   unsigned int getLifetime();
 
60
   unsigned int getBandwidth();
 
61
 
 
62
   // Methods to control active destination
 
63
   asio::error_code setActiveDestination(const asio::ip::address& address, unsigned short port);
 
64
   asio::error_code clearActiveDestination();
 
65
 
 
66
   // Turn Send Methods
 
67
   asio::error_code send(const char* buffer, unsigned int size);
 
68
   asio::error_code sendTo(const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
 
69
 
 
70
   // Receive Methods
 
71
   asio::error_code receive(char* buffer, unsigned int& size, unsigned int timeout, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
 
72
   asio::error_code receiveFrom(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int& size, unsigned int timeout);
 
73
 
 
74
protected:
 
75
   virtual asio::error_code rawWrite(const char* buffer, unsigned int size) = 0;
 
76
   virtual asio::error_code rawWrite(const std::vector<asio::const_buffer>& buffers) = 0;
 
77
   virtual asio::error_code rawRead(unsigned int timeout, unsigned int* bytesRead, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0) = 0;
 
78
   virtual void cancelSocket() = 0;
 
79
 
 
80
   // Local Binding Info
 
81
   StunTuple mLocalBinding;
 
82
   StunTuple mConnectedTuple;
 
83
 
 
84
   // Authentication Info
 
85
   resip::Data mUsername;
 
86
   resip::Data mPassword;
 
87
   resip::Data mHmacKey;
 
88
   resip::Data mRealm;
 
89
   resip::Data mNonce;
 
90
 
 
91
   // Turn Allocation Properties used in request
 
92
   unsigned int mRequestedLifetime;
 
93
   unsigned int mRequestedBandwidth;
 
94
   unsigned char mRequestedProps;
 
95
   UInt64 mReservationToken;
 
96
   StunTuple::TransportType mRequestedTransportType;
 
97
 
 
98
   // Turn Allocation Properties from response
 
99
   bool mHaveAllocation;
 
100
   time_t mAllocationRefreshTime;
 
101
   StunTuple mRelayTuple;
 
102
   StunTuple mReflexiveTuple;
 
103
   unsigned int mLifetime;
 
104
   unsigned int mBandwidth;
 
105
 
 
106
   ChannelManager mChannelManager;
 
107
   typedef std::map<unsigned short, time_t> ChannelBindingRefreshTimeMap;
 
108
   ChannelBindingRefreshTimeMap mChannelBindingRefreshTimes;
 
109
   RemotePeer* mActiveDestination;
 
110
 
 
111
   asio::io_service mIOService;
 
112
 
 
113
   // handlers and timers required to do a timed out read
 
114
   asio::deadline_timer mReadTimer;
 
115
   size_t mBytesRead;
 
116
   asio::error_code mReadErrorCode;
 
117
   void startReadTimer(unsigned int timeout);
 
118
   void handleRawRead(const asio::error_code& errorCode, size_t bytesRead);
 
119
   void handleRawReadTimeout(const asio::error_code& errorCode);
 
120
 
 
121
   // Read/Write Buffers
 
122
   char mReadBuffer[8192];
 
123
   char mWriteBuffer[8192];
 
124
   bool mConnected;
 
125
 
 
126
private:
 
127
   resip::Mutex mMutex;
 
128
   asio::error_code channelBind(RemotePeer& remotePeer);
 
129
   asio::error_code checkIfAllocationRefreshRequired();
 
130
   asio::error_code checkIfChannelBindingRefreshRequired();
 
131
   StunMessage* sendRequestAndGetResponse(StunMessage& request, asio::error_code& errorCode, bool addAuthInfo=true);
 
132
   asio::error_code sendTo(RemotePeer& remotePeer, const char* buffer, unsigned int size);
 
133
   asio::error_code handleStunMessage(StunMessage& stunMessage, char* buffer, unsigned int& size, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
 
134
   asio::error_code handleRawData(char* data, unsigned int dataSize,  unsigned int expectedSize, char* buffer, unsigned int& bufferSize);
 
135
};
 
136
 
 
137
 
138
 
 
139
#endif
 
140
 
 
141
 
 
142
/* ====================================================================
 
143
 
 
144
 Copyright (c) 2007-2008, Plantronics, Inc.
 
145
 All rights reserved.
 
146
 
 
147
 Redistribution and use in source and binary forms, with or without
 
148
 modification, are permitted provided that the following conditions are 
 
149
 met:
 
150
 
 
151
 1. Redistributions of source code must retain the above copyright 
 
152
    notice, this list of conditions and the following disclaimer. 
 
153
 
 
154
 2. Redistributions in binary form must reproduce the above copyright
 
155
    notice, this list of conditions and the following disclaimer in the
 
156
    documentation and/or other materials provided with the distribution. 
 
157
 
 
158
 3. Neither the name of Plantronics nor the names of its contributors 
 
159
    may be used to endorse or promote products derived from this 
 
160
    software without specific prior written permission. 
 
161
 
 
162
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 
163
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 
164
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 
165
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 
166
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 
167
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 
168
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 
169
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 
170
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 
171
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 
172
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
173
 
 
174
 ==================================================================== */