Skip to content

Commit

Permalink
Introduce DNS resolve bypass feature
Browse files Browse the repository at this point in the history
Signed-off-by: Alex.Li <zhiqinli@amazon.com>
  • Loading branch information
codingspirit committed Jul 26, 2023
1 parent 1df5294 commit 08dddce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions samples/webrtc/source/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,48 @@ VOID onIceCandidateHandler(UINT64 customData, PCHAR candidateJson)
CHK_LOG_ERR(retStatus);
}

STATUS resolveIceServerUrl(PCHAR originalIceServerUrl, PCHAR resolvedIceServerUrl)
{
STATUS retStatus = STATUS_RESOLVE_HOSTNAME_FAILED;

SIZE_T resolvedIndex = 0;
BOOL inUrlRange = FALSE;
BOOL isIpResolved = FALSE;
CHAR replacement = '\0';

for (int i = 0; i < MAX_ICE_CONFIG_URI_LEN - 1; i++) {
replacement = originalIceServerUrl[i];

if (originalIceServerUrl[i] == ':') {
inUrlRange = !inUrlRange;
}

if (!isIpResolved && originalIceServerUrl[i] == '.') {
isIpResolved = TRUE;
}

if (!isIpResolved) {
if (inUrlRange && originalIceServerUrl[i] == '-') {
replacement = '.';
}
}

if (isIpResolved && inUrlRange) {
continue;
}

resolvedIceServerUrl[resolvedIndex] = replacement;
resolvedIndex++;
}

if (isIpResolved && !inUrlRange) {
resolvedIceServerUrl[resolvedIndex + 1] = '\0';
retStatus = STATUS_SUCCESS;
}

return retStatus;
}

STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcPeerConnection* ppRtcPeerConnection)
{
ENTERS();
Expand Down Expand Up @@ -403,7 +445,16 @@ STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcP
* It's recommended to not pass too many TURN iceServers to configuration because it will slow down ice gathering in non-trickle mode.
*/

#ifdef BYPASS_DNS_RESOLVE_TURN
CHAR resolvedIceServerUrl[MAX_ICE_CONFIG_URI_LEN];
if (resolveIceServerUrl(pIceConfigInfo->uris[j], resolvedIceServerUrl) == STATUS_SUCCESS) {
STRNCPY(configuration.iceServers[uriCount + 1].urls, resolvedIceServerUrl, MAX_ICE_CONFIG_URI_LEN);
} else {
STRNCPY(configuration.iceServers[uriCount + 1].urls, pIceConfigInfo->uris[j], MAX_ICE_CONFIG_URI_LEN);
}
#else
STRNCPY(configuration.iceServers[uriCount + 1].urls, pIceConfigInfo->uris[j], MAX_ICE_CONFIG_URI_LEN);
#endif
STRNCPY(configuration.iceServers[uriCount + 1].credential, pIceConfigInfo->password, MAX_ICE_CONFIG_CREDENTIAL_LEN);
STRNCPY(configuration.iceServers[uriCount + 1].username, pIceConfigInfo->userName, MAX_ICE_CONFIG_USER_NAME_LEN);

Expand Down
3 changes: 3 additions & 0 deletions samples/webrtc/source/Samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extern "C" {
/* Uncomment the following line in order to enable IoT credentials checks in the provided samples */
// #define IOT_CORE_ENABLE_CREDENTIALS 1

// Enable this macro to bypass turn server DNS resolve to improve the speed of connection establishment
#define BYPASS_DNS_RESOLVE_TURN

typedef enum {
SAMPLE_STREAMING_VIDEO_ONLY,
SAMPLE_STREAMING_AUDIO_VIDEO,
Expand Down

0 comments on commit 08dddce

Please sign in to comment.