8#define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
15 mLastAllocatedUdpPort(config.mAllocationPortRangeMin-1),
16 mLastAllocatedTcpPort(config.mAllocationPortRangeMin-1),
17 mIOService(ioService),
23 mUdpAllocationPorts[i] = PortStateUnallocated;
24 mTcpAllocationPorts[i] = PortStateUnallocated;
30 InfoLog(<<
"Turn Manager destroyed.");
40 std::map<resip::Data, unsigned long>::const_iterator it = mAllocationsPerUser.find(username);
47 ++mAllocationsPerUser[username];
53 std::map<resip::Data, unsigned long>::iterator it = mAllocationsPerUser.find(username);
54 if(it != mAllocationsPerUser.end() && --it->second == 0)
56 mAllocationsPerUser.erase(it);
63 unsigned long& count = mConnectionsPerIp[address];
67 if(mConnectionLimitLogged.insert(address).second)
69 WarningLog(<<
"Already " << count <<
" TCP/TLS connections from " << address.to_string() <<
" (MaxTcpConnectionsPerIp), closing new ones");
73 DebugLog(<<
"Already " << count <<
" TCP/TLS connections from " << address.to_string() <<
", closing a new one");
84 std::map<asio::ip::address, unsigned long>::iterator it = mConnectionsPerIp.find(address);
85 if(it != mConnectionsPerIp.end())
89 mConnectionLimitLogged.erase(address);
93 mConnectionsPerIp.erase(it);
101 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
102 unsigned short startPortToCheck = advanceLastAllocatedPort(transport);
103 unsigned short portToCheck = startPortToCheck;
104 while(portAllocationMap[portToCheck] != PortStateUnallocated)
106 portToCheck = advanceLastAllocatedPort(transport);
107 if(portToCheck == startPortToCheck)
return 0;
109 portAllocationMap[portToCheck] = PortStateAllocated;
116 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
117 unsigned short startPortToCheck = advanceLastAllocatedPort(transport);
119 while(startPortToCheck % 2 != 0)
121 startPortToCheck = advanceLastAllocatedPort(transport);
123 unsigned short portToCheck = startPortToCheck;
124 while(portAllocationMap[portToCheck] != PortStateUnallocated)
126 portToCheck = advanceLastAllocatedPort(transport, 2);
127 if(portToCheck == startPortToCheck)
return 0;
129 portAllocationMap[portToCheck] = PortStateAllocated;
137 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
138 unsigned short startPortToCheck = advanceLastAllocatedPort(transport);
140 while(startPortToCheck % 2 != 1)
142 startPortToCheck = advanceLastAllocatedPort(transport);
144 unsigned short portToCheck = startPortToCheck;
145 while(portAllocationMap[portToCheck] != PortStateUnallocated)
147 portToCheck = advanceLastAllocatedPort(transport, 2);
148 if(portToCheck == startPortToCheck)
return 0;
150 portAllocationMap[portToCheck] = PortStateAllocated;
157 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
158 unsigned short startPortToCheck = advanceLastAllocatedPort(transport);
160 while(startPortToCheck % 2 != 0 ||
161 startPortToCheck + 1 == 0 ||
164 startPortToCheck = advanceLastAllocatedPort(transport);
166 unsigned short portToCheck = startPortToCheck;
167 while(portAllocationMap[portToCheck] != PortStateUnallocated || portAllocationMap[portToCheck+1] != PortStateUnallocated)
169 portToCheck = advanceLastAllocatedPort(transport, 2);
170 if(portToCheck == startPortToCheck)
return 0;
172 portAllocationMap[portToCheck] = PortStateAllocated;
173 portAllocationMap[portToCheck+1] = PortStateReserved;
182 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
185 if(portAllocationMap[port] == PortStateReserved)
187 portAllocationMap[port] = PortStateAllocated;
193 if(portAllocationMap[port] == PortStateUnallocated)
195 portAllocationMap[port] = PortStateAllocated;
208 PortAllocationMap& portAllocationMap = getPortAllocationMap(transport);
209 portAllocationMap[port] = PortStateUnallocated;
212 if(port % 2 == 0 && portAllocationMap[port+1] == PortStateReserved)
214 portAllocationMap[port+1] = PortStateUnallocated;
219TurnManager::PortAllocationMap&
226 return mTcpAllocationPorts;
229 return mUdpAllocationPorts;
240 mLastAllocatedTcpPort+=numToAdvance;
245 else if(mLastAllocatedTcpPort == 0 )
249 return mLastAllocatedTcpPort;
252 mLastAllocatedUdpPort+=numToAdvance;
257 else if(mLastAllocatedUdpPort == 0 )
261 return mLastAllocatedUdpPort;
#define WarningLog(args_)
unsigned short mAllocationPortRangeMax
unsigned long mMaxTcpConnectionsPerIp
unsigned short mAllocationPortRangeMin
unsigned long mMaxAllocationsPerUser
unsigned short allocateEvenPort(StunTuple::TransportType transport)
bool allocatePort(StunTuple::TransportType transport, unsigned short port, bool reserved=false)
unsigned short allocateEvenPortPair(StunTuple::TransportType transport)
void removeAllocation(const resip::Data &username)
TurnManager(asio::io_context &ioService, const ReTurnConfig &config)
unsigned short allocateAnyPort(StunTuple::TransportType transport)
void removeConnection(const asio::ip::address &address)
void deallocatePort(StunTuple::TransportType transport, unsigned short port)
bool addConnection(const asio::ip::address &address)
TCP and TLS connections per client IP address (MaxTcpConnectionsPerIp): addConnection returns false,...
unsigned short allocateOddPort(StunTuple::TransportType transport)
void addAllocation(const resip::Data &username)
bool allocationQuotaReached(const resip::Data &username) const
Allocations per user, over all transports: TurnAllocation adds and removes its own (MaxAllocationsPer...
An alternative to std::string, encapsulates an arbitrary buffer of bytes.