Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aaaa08

Browse files
committed
- added usage of range selection class to assist with allowed/denied ranges of values to ice gatherer
1 parent 377e235 commit 9aaaa08

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

ortc/cpp/ortc_ICEGatherer.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,29 @@ namespace ortc
397397
ISettings::setBool(ORTC_SETTING_GATHERER_GATHER_PASSIVE_TCP_CANDIDATES, true);
398398

399399
ISettings::setUInt(ORTC_SETTING_GATHERER_RECHECK_IP_ADDRESSES_IN_SECONDS, 60);
400+
401+
{
402+
zsLib::RangeSelection<WORD> range;
403+
#ifdef _WIN32
404+
range.allow(5000, 65535);
405+
range.deny(443, 443);
406+
range.deny(500, 500);
407+
range.deny(1900, 1900);
408+
range.deny(2869, 2869);
409+
range.deny(3074, 3074);
410+
range.deny(3076, 3076);
411+
range.deny(4016, 4016);
412+
range.deny(4211, 4211);
413+
range.deny(4222, 4223);
414+
range.deny(4500, 4500);
415+
range.deny(4600, 4601);
416+
range.deny(5355, 5355);
417+
range.deny(49152, 57343);
418+
#else
419+
range.allow(5000, 65535);
420+
#endif //_WIN32
421+
range.exportToSetting(ORTC_SETTING_GATHERER_PORT_RESTRICTIONS);
422+
}
400423
}
401424

402425
};
@@ -573,7 +596,8 @@ namespace ortc
573596
mMaxTotalBuffers(ISettings::getUInt(ORTC_SETTING_GATHERER_MAX_TOTAL_INCOMING_PACKET_BUFFERING)),
574597
mMaxTCPBufferingSizePendingConnection(ISettings::getUInt(ORTC_SETTING_GATHERER_MAX_PENDING_OUTGOING_TCP_SOCKET_BUFFERING_IN_BYTES)),
575598
mMaxTCPBufferingSizeConnected(ISettings::getUInt(ORTC_SETTING_GATHERER_MAX_CONNECTED_TCP_SOCKET_BUFFERING_IN_BYTES)),
576-
mGatherPassiveTCP(ISettings::getBool(ORTC_SETTING_GATHERER_GATHER_PASSIVE_TCP_CANDIDATES))
599+
mGatherPassiveTCP(ISettings::getBool(ORTC_SETTING_GATHERER_GATHER_PASSIVE_TCP_CANDIDATES)),
600+
mPortRestriction(RangeSelection::createFromSetting(ORTC_SETTING_GATHERER_PORT_RESTRICTIONS))
577601
{
578602
mSTUNPacketParseOptions = STUNPacket::ParseOptions(STUNPacket::RFC_AllowAll, false, "ortc::ICEGatherer", mID);
579603

@@ -4927,6 +4951,10 @@ namespace ortc
49274951
} else {
49284952
ZS_LOG_WARNING(Debug, log("will not attempt to rebind to default port") + ZS_PARAM("ip address", ioBindIP.string()))
49294953
}
4954+
} else if (!firstAttempt) {
4955+
WORD selectedPort = mPortRestriction.getRandomPosition(IHelper::random(0, std::numeric_limits<size_t>::max()));
4956+
ioBindIP.setPort(selectedPort);
4957+
ZS_LOG_DEBUG(log("will attempt to bind to chosen port") + ZS_PARAM("ip address", ioBindIP.string()))
49304958
}
49314959

49324960
socket->bind(ioBindIP);
@@ -4946,6 +4974,10 @@ namespace ortc
49464974
WORD bindPort = local.getPort();
49474975
ioBindIP.setPort(bindPort);
49484976
if (0 == mDefaultPort) {
4977+
if (!mPortRestriction.isAllowed(mDefaultPort)) {
4978+
ZS_LOG_WARNING(Detail, log("OS selected a port that is within the denied ports allowed (will attempt rebind on random, non OS chosen, and non denied port)") + ZS_PARAM("port", bindPort));
4979+
ZS_THROW_CUSTOM_PROPERTIES_1(Socket::Exceptions::Unspecified, 0, String("OS port selection was within denied port range: " + string(bindPort)));
4980+
}
49494981
mDefaultPort = bindPort;
49504982
ZS_LOG_TRACE(log("selected default bind port") + ZS_PARAMIZE(mDefaultPort))
49514983
}

ortc/internal/ortc_ICEGatherer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
#include <zsLib/MessageQueueAssociator.h>
4747
#include <zsLib/Socket.h>
4848
#include <zsLib/ITimer.h>
49+
#include <zsLib/RangeSelection.h>
4950

5051
#include <cryptopp/queue.h>
5152

5253
#define ORTC_SETTING_GATHERER_INTERFACE_NAME_MAPPING "ortc/gatherer/interface-name-mapping"
5354
#define ORTC_SETTING_GATHERER_USERNAME_FRAG_LENGTH "ortc/gatherer/username-frag-length"
5455
#define ORTC_SETTING_GATHERER_PASSWORD_LENGTH "ortc/gatherer/password-length"
5556

57+
#define ORTC_SETTING_GATHERER_PORT_RESTRICTIONS "ortc/gatherer/port-restrictions" // use zsLib::RangeSelection<uint16_t> to set this setting
58+
5659
#define ORTC_SETTING_GATHERER_CANDIDATE_TYPE_PREFERENCE_PRIORITY_PREFIX "ortc/gatherer/canadidate-type-priority-" // (0..126) << (24)
5760
#define ORTC_SETTING_GATHERER_PROTOCOL_TYPE_PREFERENCE_PRIORITY_PREFIX "ortc/gatherer/protocol-type-priority-" // (0..0x3) << (24-2)
5861
#define ORTC_SETTING_GATHERER_INTERFACE_TYPE_PREFERENCE_PRIORITY_PREFIX "ortc/gatherer/interface-type-priority-" // (0..0xF) << (24-6)
@@ -320,6 +323,7 @@ namespace ortc
320323
typedef std::map<LocalCandidateRemoteIPPair, RoutePtr> LocalCandidateRemoteIPRouteMap;
321324

322325
typedef CryptoPP::ByteQueue ByteQueue;
326+
typedef zsLib::RangeSelection<WORD> RangeSelection;
323327

324328
public:
325329
struct ConstructorOptions
@@ -1070,6 +1074,7 @@ namespace ortc
10701074
String mHostsHash;
10711075
String mOptionsHash;
10721076

1077+
RangeSelection mPortRestriction;
10731078
WORD mDefaultPort {0};
10741079

10751080
String mLastFixedHostPortsHostsHash;

0 commit comments

Comments
 (0)