Skip to content

Commit

Permalink
Refractor iceAgentAddRemoteCandidate
Browse files Browse the repository at this point in the history
  • Loading branch information
lherman-cs committed Apr 28, 2020
1 parent cc8c6b2 commit 128d115
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 83 deletions.
25 changes: 13 additions & 12 deletions src/source/Ice/IceAgent.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ STATUS iceAgentAddRemoteCandidate(PIceAgent pIceAgent, PCHAR pIceCandidateString
PCHAR curr, tail, next;
UINT32 tokenLen, portValue, remoteCandidateCount, i, len;
BOOL foundIpAndPort = FALSE, freeIceCandidateIfFail = TRUE;
BOOL foundIp6 = FALSE;
CHAR ip6Buf[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
BOOL foundIp = FALSE;
CHAR ipBuf[KVS_IP_ADDRESS_STRING_BUFFER_LEN];
KvsIpAddress candidateIpAddr;

CHK(pIceAgent != NULL && pIceCandidateString != NULL, STATUS_NULL_ARG);
Expand All @@ -277,27 +277,28 @@ STATUS iceAgentAddRemoteCandidate(PIceAgent pIceAgent, PCHAR pIceCandidateString
tokenLen = (UINT32) (next - curr);
CHK(STRNCMPI("tcp", curr, tokenLen) != 0, STATUS_ICE_CANDIDATE_STRING_IS_TCP);

if (candidateIpAddr.address[0] != 0 || foundIp6) {
if (foundIp) {
CHK_STATUS(STRTOUI32(curr, curr + tokenLen, 10, &portValue));

candidateIpAddr.port = htons(portValue);
candidateIpAddr.family = foundIp6 ? KVS_IP_FAMILY_TYPE_IPV6 : KVS_IP_FAMILY_TYPE_IPV4;
} else if (STRNCHR(curr, tokenLen, '.') != NULL) {
CHK(tokenLen <= KVS_MAX_IPV4_ADDRESS_STRING_LEN, STATUS_ICE_CANDIDATE_STRING_INVALID_IP); // IPv4 is 15 characters at most
CHK_STATUS(populateIpFromString(&candidateIpAddr, curr));
} else {
len = MIN(next - curr, KVS_IP_ADDRESS_STRING_BUFFER_LEN - 1);
STRNCPY(ip6Buf, curr, len);
ip6Buf[len] = '\0';
foundIp6 = inet_pton(AF_INET6, ip6Buf, candidateIpAddr.address) == 1 ? TRUE : FALSE;
STRNCPY(ipBuf, curr, len);
ipBuf[len] = '\0';

if ((foundIp = inet_pton(AF_INET, ipBuf, candidateIpAddr.address) == 1 ? TRUE : FALSE)) {
candidateIpAddr.family = KVS_IP_FAMILY_TYPE_IPV4;
} else if ((foundIp = inet_pton(AF_INET6, ipBuf, candidateIpAddr.address) == 1 ? TRUE : FALSE)) {
candidateIpAddr.family = KVS_IP_FAMILY_TYPE_IPV6;
}
}

curr = next + 1;
foundIpAndPort = (candidateIpAddr.port != 0) && ((candidateIpAddr.address[0] != 0) || foundIp6);
foundIpAndPort = (candidateIpAddr.port != 0) && foundIp;
}

CHK(candidateIpAddr.port != 0, STATUS_ICE_CANDIDATE_STRING_MISSING_PORT);
CHK(candidateIpAddr.address[0] != 0 || foundIp6, STATUS_ICE_CANDIDATE_STRING_MISSING_IP);
CHK(foundIp, STATUS_ICE_CANDIDATE_STRING_MISSING_IP);

CHK_STATUS(findCandidateWithIp(&candidateIpAddr, pIceAgent->remoteCandidates, &pDuplicatedIceCandidate));
CHK(pDuplicatedIceCandidate == NULL, retStatus);
Expand Down
39 changes: 0 additions & 39 deletions src/source/Ice/IceUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,45 +185,6 @@ STATUS iceUtilsSendData(PBYTE buffer, UINT32 size,
return retStatus;
}

// only work with ipv4 right now
STATUS populateIpFromString(PKvsIpAddress pKvsIpAddress, PCHAR pBuff)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PCHAR curr, tail, next;
UINT8 octet = 0;
UINT32 ipValue;

CHK(pKvsIpAddress != NULL && pBuff != NULL, STATUS_NULL_ARG);
CHK(STRNLEN(pBuff, KVS_MAX_IPV4_ADDRESS_STRING_LEN) > 0, STATUS_INVALID_ARG);

curr = pBuff;
tail = pBuff + STRLEN(pBuff);
// first 3 octet should always end with a '.', the last octet may end with ' ' or '\0'
while ((next = STRNCHR(curr, tail - curr, '.')) != NULL && octet < 3) {
CHK_STATUS(STRTOUI32(curr, curr + (next - curr), 10, &ipValue));
pKvsIpAddress->address[octet] = (UINT8) ipValue;
octet++;

curr = next + 1;
}

// work with string containing just ip address and string that has ip address as substring
if ((next = STRNCHR(curr, tail - curr, ' ')) != NULL || (next = STRNCHR(curr, tail - curr, '\0')) != NULL) {
CHK_STATUS(STRTOUI32(curr, curr + (next - curr), 10, &ipValue));
pKvsIpAddress->address[octet] = (UINT8) ipValue;
octet++;
}

CHK(octet == 4, STATUS_ICE_CANDIDATE_STRING_INVALID_IP); // IPv4 MUST have 4 octets
CleanUp:

CHK_LOG_ERR(retStatus);

LEAVES();
return retStatus;
}

STATUS parseIceServer(PIceServer pIceServer, PCHAR url, PCHAR username, PCHAR credential)
{
ENTERS();
Expand Down
2 changes: 0 additions & 2 deletions src/source/Ice/IceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ STATUS iceUtilsPackageStunPacket(PStunPacket, PBYTE, UINT32, PBYTE, PUINT32);
STATUS iceUtilsSendStunPacket(PStunPacket, PBYTE, UINT32, PKvsIpAddress, PSocketConnection, struct __TurnConnection*, BOOL);
STATUS iceUtilsSendData(PBYTE, UINT32, PKvsIpAddress, PSocketConnection, struct __TurnConnection*, BOOL);

STATUS populateIpFromString(PKvsIpAddress, PCHAR);

typedef struct {
BOOL isTurn;
CHAR url[MAX_ICE_CONFIG_URI_LEN + 1];
Expand Down
27 changes: 0 additions & 27 deletions tst/IceApiTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,6 @@ namespace com { namespace amazonaws { namespace kinesis { namespace video { name
EXPECT_EQ(STATUS_SUCCESS, freeStunPacket(&pStunPacket));
EXPECT_EQ(STATUS_SUCCESS, freeTransactionIdStore(&pTransactionIdStore));
}

TEST_F(IceApiTest, IceUtilPopulateIpFromStringTest)
{
KvsIpAddress kvsIpAddress;
PCHAR ipString = (PCHAR) "16.213.65.123";
BYTE expectIpAddr[IPV4_ADDRESS_LENGTH] = {0x10, 0xD5, 0x41, 0x7B};
PCHAR ipString2 = (PCHAR) "255.255.255.255";
PCHAR ipString2Longer = (PCHAR) "255.255.255.255 34388 222.222.222.222";
BYTE expectIpAddr2[IPV4_ADDRESS_LENGTH] = {0xFF, 0xFF, 0xFF, 0xFF};

MEMSET(&kvsIpAddress, 0x0, SIZEOF(KvsIpAddress));

EXPECT_NE(STATUS_SUCCESS, populateIpFromString(NULL, NULL));
EXPECT_NE(STATUS_SUCCESS, populateIpFromString(&kvsIpAddress, NULL));
EXPECT_NE(STATUS_SUCCESS, populateIpFromString(&kvsIpAddress, (PCHAR) ""));
EXPECT_NE(STATUS_SUCCESS, populateIpFromString(NULL, ipString));

EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&kvsIpAddress, ipString));
EXPECT_EQ(0, MEMCMP(expectIpAddr, kvsIpAddress.address, IPV4_ADDRESS_LENGTH));

EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&kvsIpAddress, ipString2));
EXPECT_EQ(0, MEMCMP(expectIpAddr2, kvsIpAddress.address, IPV4_ADDRESS_LENGTH));

EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&kvsIpAddress, ipString2Longer));
EXPECT_EQ(0, MEMCMP(expectIpAddr2, kvsIpAddress.address, IPV4_ADDRESS_LENGTH));
}

}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tst/IceFunctionalityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace com { namespace amazonaws { namespace kinesis { namespace video { name
EXPECT_NE(STATUS_SUCCESS, findCandidateWithIp(&ipAddress, NULL, NULL));
EXPECT_NE(STATUS_SUCCESS, findCandidateWithIp(&ipAddress, &candidateList, NULL));

EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&ipAddress, (PCHAR) "127.0.0.1"));
EXPECT_EQ(1, inet_pton(AF_INET, (PCHAR) "127.0.0.1", &ipAddress.address));
ipAddress.port = 123;
ipAddress.family = KVS_IP_FAMILY_TYPE_IPV4;
EXPECT_EQ(STATUS_SUCCESS, findCandidateWithIp(&ipAddress, &candidateList, &pIceCandidate));
Expand All @@ -382,12 +382,12 @@ namespace com { namespace amazonaws { namespace kinesis { namespace video { name
EXPECT_EQ(NULL, pIceCandidate);

ipAddress.family = KVS_IP_FAMILY_TYPE_IPV4;
EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&ipAddress, (PCHAR) "127.0.0.2"));
EXPECT_EQ(1, inet_pton(AF_INET, (PCHAR) "127.0.0.2", &ipAddress.address));
EXPECT_EQ(STATUS_SUCCESS, findCandidateWithIp(&ipAddress, &candidateList, &pIceCandidate));
// address not match
EXPECT_EQ(NULL, pIceCandidate);

EXPECT_EQ(STATUS_SUCCESS, populateIpFromString(&ipAddress, (PCHAR) "127.0.0.1"));
EXPECT_EQ(1, inet_pton(AF_INET, (PCHAR) "127.0.0.1", &ipAddress.address));
ipAddress.port = 124;
EXPECT_EQ(STATUS_SUCCESS, findCandidateWithIp(&ipAddress, &candidateList, &pIceCandidate));
// port not match
Expand Down

0 comments on commit 128d115

Please sign in to comment.