reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ChannelManager.cxx
Go to the documentation of this file.
1#include "ChannelManager.hxx"
2#include <rutil/Random.hxx>
4#include <rutil/Logger.hxx>
5#include "ReTurnSubsystem.hxx"
6
7#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
8#define TURN_CHANNEL_BINDING_LIFETIME_SECONDS 600 // 10 minuntes
9//#define TURN_CHANNEL_BINDING_LIFETIME_SECONDS 60 // TESTING only
10
11using namespace std;
12
13namespace reTurn {
14
16{
17 // make starting channel number random; use the CSPRNG so the channel
18 // number is not predictable (CWE-338) - predictable channels aid TURN
19 // data injection. getCryptoRandom() fills a whole int, so it's often negative: take it as
20 // unsigned, or the remainder is negative and the channel number below MIN_CHANNEL_NUM.
21 unsigned int randInt = (unsigned int)resip::Random::getCryptoRandom();
22 mNextChannelNumber = MIN_CHANNEL_NUM + (unsigned short)(randInt % (MAX_CHANNEL_NUM-MIN_CHANNEL_NUM+1));
23}
24
29
31{
32 // Cleanup RemotePeer Memory
33 TupleRemotePeerMap::iterator it;
34 for (it = mTupleRemotePeerMap.begin(); it != mTupleRemotePeerMap.end(); it++)
35 {
36 delete it->second;
37 }
38 // Now clear both maps
39 mTupleRemotePeerMap.clear();
40 mChannelRemotePeerMap.clear();
41}
42
43unsigned short
44ChannelManager::getNextChannelNumber()
45{
46 if(mNextChannelNumber == MAX_CHANNEL_NUM)
47 {
48 mNextChannelNumber = MIN_CHANNEL_NUM;
49 }
50 else
51 {
52 mNextChannelNumber++;
53 }
54 return mNextChannelNumber;
55}
56
57RemotePeer*
59{
60 return createChannelBinding(peerTuple, getNextChannelNumber());
61}
62
64ChannelManager::createChannelBinding(const StunTuple& peerTuple, unsigned short channel)
65{
67
68 // Create New RemotePeer
69 RemotePeer* remotePeer = new RemotePeer(peerTuple, channel, TURN_CHANNEL_BINDING_LIFETIME_SECONDS);
70
71 // Add RemoteAddress to the appropriate maps
72 mTupleRemotePeerMap[peerTuple] = remotePeer;
73 mChannelRemotePeerMap[channel] = remotePeer;
74 return remotePeer;
75}
76
78ChannelManager::findRemotePeerByChannel(unsigned short channelNumber)
79{
80 ChannelRemotePeerMap::iterator it = mChannelRemotePeerMap.find(channelNumber);
81 if(it != mChannelRemotePeerMap.end())
82 {
83 if(!it->second->isExpired())
84 {
85 return it->second;
86 }
87 else
88 {
89 // cleanup expired channel binding
90 mTupleRemotePeerMap.erase(it->second->getPeerTuple());
91 delete it->second;
92 mChannelRemotePeerMap.erase(it);
93 }
94 }
95 return 0;
96}
97
100{
101 // Find RemotePeer
102 TupleRemotePeerMap::iterator it = mTupleRemotePeerMap.find(peerAddress);
103 if(it != mTupleRemotePeerMap.end())
104 {
105 if(!it->second->isExpired())
106 {
107 return it->second;
108 }
109 else
110 {
111 // cleanup expired channel binding
112 mChannelRemotePeerMap.erase(it->second->getChannel());
113 delete it->second;
114 mTupleRemotePeerMap.erase(it);
115 }
116 }
117 return 0;
118}
119
120} // namespace
121
122
123/* ====================================================================
124
125 Copyright (c) 2024-2026 SIP Spectrum, Inc http://www.sipspectrum.com
126 Copyright (c) 2007-2008, Plantronics, Inc.
127 All rights reserved.
128
129 Redistribution and use in source and binary forms, with or without
130 modification, are permitted provided that the following conditions are
131 met:
132
133 1. Redistributions of source code must retain the above copyright
134 notice, this list of conditions and the following disclaimer.
135
136 2. Redistributions in binary form must reproduce the above copyright
137 notice, this list of conditions and the following disclaimer in the
138 documentation and/or other materials provided with the distribution.
139
140 3. Neither the name of Plantronics nor the names of its contributors
141 may be used to endorse or promote products derived from this
142 software without specific prior written permission.
143
144 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
145 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
146 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
147 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
148 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
149 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
150 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
151 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
152 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
153 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
154 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155
156 ==================================================================== */
157
#define TURN_CHANNEL_BINDING_LIFETIME_SECONDS
#define MAX_CHANNEL_NUM
#define MIN_CHANNEL_NUM
#define resip_assert(x)
This file needs to work for both C and C++ code.
RemotePeer * findRemotePeerByChannel(unsigned short channelNumber)
RemotePeer * findRemotePeerByPeerAddress(const StunTuple &peerAddress)
RemotePeer * createChannelBinding(const StunTuple &peerTuple)
static int getCryptoRandom()
Definition Random.cxx:330