14#define RESIPROCATE_SUBSYSTEM resip::Subsystem::MEDIA
18 : mPortRangeMin(portRangeMin),
19 mPortRangeMax(portRangeMax),
23 if(mPortRangeMin & 0x1)
29 if(mPortRangeMax & 0x1)
34 for(
unsigned int i = mPortRangeMin; i <= mPortRangeMax;)
36 mRTPPortFreeList.push_back(i);
39 mTotalPortPairs = (
unsigned int)mRTPPortFreeList.size();
40 StackLog(<<
"RTPPortManager: min even port=" << mPortRangeMin <<
", max even port=" << mPortRangeMax);
46 unsigned int port = 0;
47 if(!mRTPPortFreeList.empty())
49 port = mRTPPortFreeList.front();
50 mRTPPortFreeList.pop_front();
52 StackLog(<<
"allocateRTPPort: port == " << port);
65 sockaddr_storage storage;
66 memset(&storage, 0,
sizeof(storage));
70 sockaddr_in& v4 =
reinterpret_cast<sockaddr_in&
>(storage);
71 v4.sin_family = AF_INET;
72 v4.sin_port = htons((
unsigned short)port);
79 sockaddr_in6& v6 =
reinterpret_cast<sockaddr_in6&
>(storage);
80 v6.sin6_family = AF_INET6;
81 v6.sin6_port = htons((
unsigned short)port);
91 Socket fd = ::socket(storage.ss_family, SOCK_DGRAM, IPPROTO_UDP);
98 ::setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (
const char*)&on,
sizeof(on));
99#elif defined(USE_IPV6) && defined(__linux__)
100 if(storage.ss_family == AF_INET6)
103 ::setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
sizeof(on));
107 if(::bind(fd,
reinterpret_cast<sockaddr*
>(&storage), length) != 0)
111 usable = !(e == EADDRINUSE || e == WSAEACCES);
113 usable = !(e == EADDRINUSE || e == EACCES);
123 for(
size_t tries = mRTPPortFreeList.size(); tries > 0; --tries)
130 InfoLog(<<
"allocateRTPPort: RTP port " << port <<
", or the RTCP port after it, can't be bound on " << address
131 <<
" (eg. another socket has it): trying the next port");
132 mRTPPortFreeList.push_back(port);
134 WarningLog(<<
"allocateRTPPort: no free RTP port can be bound on " << address);
141 StackLog(<<
"freeRTPPort: port == " << port);
142 resip_assert(port >= mPortRangeMin && port <= mPortRangeMax);
144 mRTPPortFreeList.push_back(port);
#define WarningLog(args_)
static bool canBindUdp(const Data &address, unsigned int port)
#define resip_assert(x)
This file needs to work for both C and C++ code.
Handles cross-platform sockets compatibility.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
static bool isIpV6Address(const Data &ipAddress)
static int inet_pton(const Data &printableIp, struct in_addr &dst)
Convert from the presentation format of the IPv4 address to struct in_addr.
static bool isIpV4Address(const Data &ipAddress)
unsigned int allocateRTPPort()
Allocates an RTP port (even - the RTCP port is the one after it). Returns 0 if none is free.
RTPPortManager(int portRangeMin=17000, int portRangeMax=18000)
void freeRTPPort(unsigned int port)
dcm! – add UnusedChecking(_enum) below;
int closeSocket(Socket fd)