reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TurnSocket.hxx
Go to the documentation of this file.
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 <asio.hpp>
11#ifdef USE_SSL
12#include <asio/ssl.hpp>
13#endif
14#include <deque>
15#include <functional>
16
17#include <vector>
18
19#include <rutil/Data.hxx>
20#include <rutil/Mutex.hxx>
21
22#include "reTurn/StunTuple.hxx"
25
26namespace reTurn {
27
29{
30public:
31 static unsigned int UnspecifiedLifetime;
32 static unsigned int UnspecifiedBandwidth;
33 static unsigned short UnspecifiedToken;
34 static asio::ip::address UnspecifiedIpAddress;
35
36 explicit TurnSocket(const asio::ip::address& address = UnspecifiedIpAddress,
37 unsigned short port = 0);
38 virtual ~TurnSocket();
39
40 virtual unsigned int getSocketDescriptor() = 0;
41 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
42
43 // Note: Shared Secret requests have been deprecated by RFC5389, and not
44 // widely implemented in RFC3489 - so not really needed at all
45 asio::error_code requestSharedSecret(char* username, unsigned int usernameSize,
46 char* password, unsigned int passwordSize);
47
48 // Set the username and password for all future requests
49 void setUsernameAndPassword(const char* username, const char* password, bool shortTermAuth=false);
50
51 // Software attribute
52 void setSoftware(const char* software);
53
54 // Stun Binding Method - use getReflexiveTuple() to get binding info
55 asio::error_code bindRequest();
56
57 // Turn Allocation Methods
58 asio::error_code createAllocation(unsigned int lifetime = UnspecifiedLifetime,
59 unsigned int bandwidth = UnspecifiedBandwidth,
63 asio::error_code refreshAllocation();
64 asio::error_code destroyAllocation();
65
66 // Accessors for properties associated with an allocation
69 unsigned int getLifetime();
70 unsigned int getBandwidth();
71
72 // Methods to control active destination
73 asio::error_code setActiveDestination(const asio::ip::address& address, unsigned short port);
74 asio::error_code clearActiveDestination();
75
76 // Turn Send Methods
77 asio::error_code send(const char* buffer, unsigned int size);
78 asio::error_code sendTo(const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size);
79
80 // Receive Methods
81 asio::error_code receive(char* buffer, unsigned int& size, unsigned int timeout, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
82 asio::error_code receiveFrom(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int& size, unsigned int timeout);
83
84protected:
85 virtual asio::error_code rawWrite(const char* buffer, unsigned int size) = 0;
86 virtual asio::error_code rawWrite(const std::vector<asio::const_buffer>& buffers) = 0;
87 virtual asio::error_code rawRead(unsigned int timeout, unsigned int* bytesRead, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0) = 0;
88 virtual void cancelSocket() = 0;
89
90 // Local Binding Info
93
94 // Authentication Info
100
101 // Attributes
103
104 // Turn Allocation Properties used in request
105 unsigned int mRequestedLifetime;
107 unsigned char mRequestedProps;
110
111 // Turn Allocation Properties from response
116 unsigned int mLifetime;
117 unsigned int mBandwidth;
118
120 typedef std::map<unsigned short, time_t> ChannelBindingRefreshTimeMap;
123
124 asio::io_context mIOService;
125
126 // handlers and timers required to do a timed out read
127 asio::steady_timer mReadTimer;
129 asio::error_code mReadErrorCode;
130 void startReadTimer(unsigned int timeout);
131 void handleRawRead(const asio::error_code& errorCode, size_t bytesRead);
132 void handleRawReadTimeout(const asio::error_code& errorCode);
133
134 // Read/Write Buffers
135 char mReadBuffer[8192];
136 char mWriteBuffer[8192];
138
139 // Peers' data (ChannelData, and Data indications) that arrived while waiting for a response,
140 // for receive() - the size of mReadBuffer that holds one is passed to queueReceived()
141 std::deque<std::vector<char>> mReceivedWhileWaiting;
142 static const size_t MaxReceivedWhileWaiting = 100;
143 void queueReceived(unsigned int size);
144
145private:
146 // Recursive: eg. destroyAllocation() calls refreshAllocation(), and sendTo() and receive() refresh
147 // the allocation when it is due
149 asio::error_code channelBind(RemotePeer& remotePeer);
150 asio::error_code checkIfAllocationRefreshRequired();
151 asio::error_code checkIfChannelBindingRefreshRequired();
152 StunMessage* sendRequestAndGetResponse(StunMessage& request, asio::error_code& errorCode, bool addAuthInfo=true);
153 asio::error_code sendTo(RemotePeer& remotePeer, const char* buffer, unsigned int size);
154 asio::error_code handleStunMessage(StunMessage& stunMessage, char* buffer, unsigned int& size, asio::ip::address* sourceAddress=0, unsigned short* sourcePort=0);
155 asio::error_code handleRawData(char* data, unsigned int dataSize, unsigned int expectedSize, char* buffer, unsigned int& bufferSize);
156};
157
158}
159
160#endif
161
162
163/* ====================================================================
164
165 Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
166 Copyright (c) 2007-2008, Plantronics, Inc.
167 All rights reserved.
168
169 Redistribution and use in source and binary forms, with or without
170 modification, are permitted provided that the following conditions are
171 met:
172
173 1. Redistributions of source code must retain the above copyright
174 notice, this list of conditions and the following disclaimer.
175
176 2. Redistributions in binary form must reproduce the above copyright
177 notice, this list of conditions and the following disclaimer in the
178 documentation and/or other materials provided with the distribution.
179
180 3. Neither the name of Plantronics nor the names of its contributors
181 may be used to endorse or promote products derived from this
182 software without specific prior written permission.
183
184 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
187 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
188 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
189 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
190 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
194 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195
196 ==================================================================== */
static const uint8_t PropsNone
resip::Data mNonce
virtual asio::error_code rawWrite(const std::vector< asio::const_buffer > &buffers)=0
void setSoftware(const char *software)
char mWriteBuffer[8192]
asio::error_code destroyAllocation()
virtual void cancelSocket()=0
void handleRawRead(const asio::error_code &errorCode, size_t bytesRead)
resip::Data mUsername
StunTuple & getReflexiveTuple()
StunTuple mReflexiveTuple
asio::io_context mIOService
asio::steady_timer mReadTimer
unsigned int mBandwidth
asio::error_code receive(char *buffer, unsigned int &size, unsigned int timeout, asio::ip::address *sourceAddress=0, unsigned short *sourcePort=0)
resip::Data mHmacKey
static unsigned int UnspecifiedBandwidth
ChannelManager mChannelManager
static asio::ip::address UnspecifiedIpAddress
asio::error_code sendTo(const asio::ip::address &address, unsigned short port, const char *buffer, unsigned int size)
resip::Data mPassword
StunTuple mLocalBinding
virtual ~TurnSocket()
virtual asio::error_code rawWrite(const char *buffer, unsigned int size)=0
unsigned int mLifetime
virtual asio::error_code connect(const std::string &address, unsigned short port)=0
asio::error_code receiveFrom(const asio::ip::address &address, unsigned short port, char *buffer, unsigned int &size, unsigned int timeout)
resip::Data mRealm
asio::error_code mReadErrorCode
asio::error_code clearActiveDestination()
virtual unsigned int getSocketDescriptor()=0
time_t mAllocationRefreshTime
ChannelBindingRefreshTimeMap mChannelBindingRefreshTimes
asio::error_code bindRequest()
unsigned int getLifetime()
asio::error_code requestSharedSecret(char *username, unsigned int usernameSize, char *password, unsigned int passwordSize)
unsigned char mRequestedProps
char mReadBuffer[8192]
RemotePeer * mActiveDestination
void startReadTimer(unsigned int timeout)
void setUsernameAndPassword(const char *username, const char *password, bool shortTermAuth=false)
asio::error_code refreshAllocation()
virtual asio::error_code rawRead(unsigned int timeout, unsigned int *bytesRead, asio::ip::address *sourceAddress=0, unsigned short *sourcePort=0)=0
uint64_t mReservationToken
asio::error_code setActiveDestination(const asio::ip::address &address, unsigned short port)
static unsigned short UnspecifiedToken
unsigned int mRequestedLifetime
static const size_t MaxReceivedWhileWaiting
static unsigned int UnspecifiedLifetime
asio::error_code createAllocation(unsigned int lifetime=UnspecifiedLifetime, unsigned int bandwidth=UnspecifiedBandwidth, unsigned char requestedProps=StunMessage::PropsNone, uint64_t reservationToken=UnspecifiedToken, StunTuple::TransportType requestedTransportType=StunTuple::None)
StunTuple & getRelayTuple()
StunTuple mConnectedTuple
asio::error_code send(const char *buffer, unsigned int size)
unsigned int mRequestedBandwidth
unsigned int getBandwidth()
resip::Data mSoftware
void handleRawReadTimeout(const asio::error_code &errorCode)
std::deque< std::vector< char > > mReceivedWhileWaiting
std::map< unsigned short, time_t > ChannelBindingRefreshTimeMap
StunTuple::TransportType mRequestedTransportType
void queueReceived(unsigned int size)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
std::recursive_mutex RecursiveMutex
Definition Mutex.hxx:38
unsigned __int64 uint64_t
Definition stdint.h:95
void list()
Definition userAdmin.cxx:66