reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TurnManager.hxx
Go to the documentation of this file.
1#ifndef TURNMANAGER_HXX
2#define TURNMANAGER_HXX
3
4#include <map>
5#include <set>
6#include <asio.hpp>
7#include <rutil/Data.hxx>
8#ifdef USE_SSL
9#include <asio/ssl.hpp>
10#endif
11#include "ReTurnConfig.hxx"
12#include "StunTuple.hxx"
13
14namespace reTurn {
15
17{
18public:
19 explicit TurnManager(asio::io_context& ioService, const ReTurnConfig& config); // ioService used to start timers
21
22 asio::io_context& getIOService() { return mIOService; }
23
24 unsigned short allocateAnyPort(StunTuple::TransportType transport);
25 unsigned short allocateEvenPort(StunTuple::TransportType transport);
26 unsigned short allocateOddPort(StunTuple::TransportType transport);
27 unsigned short allocateEvenPortPair(StunTuple::TransportType transport);
28 bool allocatePort(StunTuple::TransportType transport, unsigned short port, bool reserved = false);
29 void deallocatePort(StunTuple::TransportType transport, unsigned short port);
30
31 const ReTurnConfig& getConfig() { return mConfig; }
32
35 bool allocationQuotaReached(const resip::Data& username) const;
36 void addAllocation(const resip::Data& username);
37 void removeAllocation(const resip::Data& username);
38
41 bool addConnection(const asio::ip::address& address);
42 void removeConnection(const asio::ip::address& address);
43
44private:
45 std::map<resip::Data, unsigned long> mAllocationsPerUser;
46 std::map<asio::ip::address, unsigned long> mConnectionsPerIp;
47 std::set<asio::ip::address> mConnectionLimitLogged; // addresses refused since they reached the limit
48
49
50 typedef enum
51 {
52 PortStateUnallocated,
53 PortStateAllocated,
54 PortStateReserved
55 } PortState;
56 typedef std::map<unsigned short, PortState> PortAllocationMap;
57 PortAllocationMap mUdpAllocationPorts; // .slg. expand to be a map/hash table per ip address/interface
58 PortAllocationMap mTcpAllocationPorts;
59 unsigned short mLastAllocatedUdpPort;
60 unsigned short mLastAllocatedTcpPort;
61 PortAllocationMap& getPortAllocationMap(StunTuple::TransportType transport);
62 unsigned short advanceLastAllocatedPort(StunTuple::TransportType transport, unsigned int numToAdvance = 1);
63
64 asio::io_context& mIOService;
65 const ReTurnConfig& mConfig;
66};
67
68}
69
70#endif
71
72
73/* ====================================================================
74
75 Copyright (c) 2008-2026 SIP Spectrum, Inc. https://www.sipspectrum.com
76 Copyright (c) 2007-2008, Plantronics, Inc.
77 All rights reserved.
78
79 Redistribution and use in source and binary forms, with or without
80 modification, are permitted provided that the following conditions are
81 met:
82
83 1. Redistributions of source code must retain the above copyright
84 notice, this list of conditions and the following disclaimer.
85
86 2. Redistributions in binary form must reproduce the above copyright
87 notice, this list of conditions and the following disclaimer in the
88 documentation and/or other materials provided with the distribution.
89
90 3. Neither the name of Plantronics nor the names of its contributors
91 may be used to endorse or promote products derived from this
92 software without specific prior written permission.
93
94 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
95 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
96 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
97 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
98 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
100 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
101 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
103 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
104 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105
106 ==================================================================== */
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)
unsigned short allocateAnyPort(StunTuple::TransportType transport)
void removeConnection(const asio::ip::address &address)
const ReTurnConfig & getConfig()
asio::io_context & getIOService()
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.
Definition Data.hxx:90
void list()
Definition userAdmin.cxx:66