diff --git a/src/source/Ice/IceAgent.c b/src/source/Ice/IceAgent.c index a067867d37..90bfc4efe0 100644 --- a/src/source/Ice/IceAgent.c +++ b/src/source/Ice/IceAgent.c @@ -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); @@ -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); diff --git a/src/source/Ice/IceUtils.c b/src/source/Ice/IceUtils.c index 00866a8152..5c4c6c4400 100644 --- a/src/source/Ice/IceUtils.c +++ b/src/source/Ice/IceUtils.c @@ -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(); diff --git a/src/source/Ice/IceUtils.h b/src/source/Ice/IceUtils.h index e406c545d5..dc465692a8 100644 --- a/src/source/Ice/IceUtils.h +++ b/src/source/Ice/IceUtils.h @@ -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]; diff --git a/tst/IceApiTest.cpp b/tst/IceApiTest.cpp index 46435b9fc3..c71af726e2 100644 --- a/tst/IceApiTest.cpp +++ b/tst/IceApiTest.cpp @@ -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)); - } - } } } diff --git a/tst/IceFunctionalityTest.cpp b/tst/IceFunctionalityTest.cpp index a847c53c7c..bd9da0ac12 100644 --- a/tst/IceFunctionalityTest.cpp +++ b/tst/IceFunctionalityTest.cpp @@ -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)); @@ -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