14#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
19#define TURN_PERMISSION_LIFETIME_SECONDS 300
22#ifdef BOOST_ASIO_HAS_STD_CHRONO
23using namespace std::chrono;
26using namespace std::chrono;
38 unsigned int lifetime) :
39 mKey(clientLocalTuple, clientRemoteTuple),
40 mClientAuth(clientAuth),
41 mRequestedTuple(requestedTuple),
42 mTurnManager(turnManager),
43 mTurnAllocationManager(turnAllocationManager),
44 mAllocationTimer(turnManager.getIOService()),
45 mLocalTurnSocket(localTurnSocket),
46 mBadChannelErrorLogged(false),
47 mNoPermissionToPeerLogged(false),
48 mNoPermissionFromPeerLogged(false)
50 InfoLog(<<
"TurnAllocation created: clientLocal=" << clientLocalTuple <<
" clientRemote=" <<
51 clientRemoteTuple <<
" allocation=" << requestedTuple <<
" lifetime=" << lifetime);
78 TurnPermissionMap::iterator it;
79 for(it = mTurnPermissionMap.begin(); it != mTurnPermissionMap.end(); it++)
100 mUdpRelayServer = std::make_shared<UdpRelayServer>(mTurnManager.
getIOService(), *
this);
101 if(!mUdpRelayServer->startReceiving())
110 ErrLog(<<
"Only UDP relay's are currently implemented!");
123 mUdpRelayServer->stop();
124 mUdpRelayServer =
nullptr;
126 mAllocationTimer.cancel();
135 mExpires = time(0) + lifetime;
138 mAllocationTimer.expires_after(seconds(lifetime));
145 TurnPermissionMap::iterator it = mTurnPermissionMap.find(address);
146 if(it != mTurnPermissionMap.end())
148 if(it->second->isExpired())
151 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" exipred address=" << it->first.to_string());
153 mTurnPermissionMap.erase(it);
164 TurnPermissionMap::iterator it = mTurnPermissionMap.find(address);
166 if(it != mTurnPermissionMap.end())
168 turnPermission = it->second;
173 InfoLog(<<
"Permission for " << address.to_string() <<
" created: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
179 InfoLog(<<
"Permission for " << address.to_string() <<
" refreshed: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
202 if(mBadChannelErrorLogged)
204 DebugLog(<<
"sendDataToPeer bad channel number - discarding data: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
205 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" channelNumber=" << channelNumber);
209 mBadChannelErrorLogged =
true;
211 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" channelNumber=" << channelNumber);
220 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" peerAddress=" << peerAddress);
226 if(mNoPermissionToPeerLogged)
228 DebugLog(<<
"Turn send indication for destination=" << peerAddress.
getAddress() <<
", but no permission installed. Dropping.");
232 mNoPermissionToPeerLogged =
true;
233 WarningLog(<<
"Turn send indication for destination=" << peerAddress.
getAddress() <<
", but no permission installed. Dropping.");
240 mUdpRelayServer->doSend(peerAddress, data, isFramed ? 4 : 0);
246 WarningLog(<<
"Turn send indication with no data for non-UDP transport. Dropping.");
261 if(mNoPermissionFromPeerLogged)
263 DebugLog(<<
"Data received from peer=" << peerAddress <<
", but no permission installed. Dropping.");
267 mNoPermissionFromPeerLogged =
true;
268 WarningLog(<<
"Data received from peer=" << peerAddress <<
", but no permission installed. Dropping.");
281 " channelNumber=" << (
int)remotePeer->
getChannel());
290 dataInd.
setTurnData(data->data(), (
unsigned int)data->size());
293 unsigned int bufferSize = (
unsigned int)data->size() + 8 + 36 + 8 ;
295 unsigned int size = dataInd.
stunEncodeMessage((
char*)buffer->data(), bufferSize);
296 buffer->truncate(size);
314 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" channelNumber=" << channelNumber <<
" peerAddress=" << peerAddress);
320 InfoLog(<<
"Channel " << channelNumber <<
" binding to " << peerAddress <<
" refreshed: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
328 WarningLog(<<
"addChannelBinding failed since peerAddress is alredy in use: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
329 mKey.
getClientRemoteTuple() <<
" allocation=" << mRequestedTuple <<
" channelNumber=" << channelNumber <<
" peerAddress=" << peerAddress);
334 InfoLog(<<
"Channel " << channelNumber <<
" binding to " << peerAddress <<
" created: clientLocal=" << mKey.
getClientLocalTuple() <<
" clientRemote=" <<
#define WarningLog(args_)
#define resip_assert(x)
This file needs to work for both C and C++ code.
#define TURN_PERMISSION_LIFETIME_SECONDS
static std::shared_ptr< DataBuffer > allocateBuffer(size_t size)
Utility API.
virtual void doSend(const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
Use these if you already operating within the ioService thread.
virtual void registerAsyncSocketBaseHandler(AsyncSocketBaseHandler *handler)
RemotePeer * findRemotePeerByChannel(unsigned short channelNumber)
RemotePeer * findRemotePeerByPeerAddress(const StunTuple &peerAddress)
RemotePeer * createChannelBinding(const StunTuple &peerTuple)
unsigned short getChannel() const
const StunTuple & getPeerTuple() const
const resip::Data & getClientUsername() const
void setTurnData(const char *data, unsigned int len)
void createHeader(uint16_t stunclass, uint16_t method)
StunAtrAddress mTurnXorPeerAddress[TURN_MAX_XOR_PEER_ADDR]
static const uint16_t TurnDataMethod
static void setStunAtrAddressFromTuple(StunAtrAddress &address, const StunTuple &tuple)
int mCntTurnXorPeerAddress
unsigned int stunEncodeMessage(char *buf, unsigned int bufLen)
static const uint16_t StunClassIndication
TransportType getTransportType() const
const asio::ip::address & getAddress() const
unsigned int getPort() const
const StunTuple & getClientRemoteTuple() const
const StunTuple & getClientLocalTuple() const
void allocationExpired(const asio::error_code &e, const TurnAllocationKey &turnAllocationKey)
void removeTurnAllocation(const TurnAllocationKey &turnAllocationKey)
bool addChannelBinding(const StunTuple &peerAddress, unsigned short channelNumber)
void sendDataToPeer(unsigned short channelNumber, const std::shared_ptr< DataBuffer > &data, bool isFramed)
void onSocketDestroyed() override
TurnAllocation(TurnManager &turnManager, TurnAllocationManager &turnAllocationManager, AsyncSocketBase *localTurnSocket, const StunTuple &clientLocalTuple, const StunTuple &clientRemoteTuple, const StunAuth &clientAuth, const StunTuple &requestedTuple, unsigned int lifetime)
void refresh(unsigned int lifetime)
void sendDataToClient(const StunTuple &peerAddress, const std::shared_ptr< DataBuffer > &data)
bool existsPermission(const asio::ip::address &address)
void refreshPermission(const asio::ip::address &address)
void removeAllocation(const resip::Data &username)
asio::io_context & getIOService()
void deallocatePort(StunTuple::TransportType transport, unsigned short port)
void addAllocation(const resip::Data &username)
dcm! – add UnusedChecking(_enum) below;