Skip to content

Commit

Permalink
api: add support for IP addresses with scope id
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 17, 2022
1 parent 95365df commit d48a96d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
23 changes: 16 additions & 7 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ oc_endpoint_to_string(oc_endpoint_t *endpoint, oc_string_t *endpoint_str)
}

#ifdef OC_IPV4
static void
static int
oc_parse_ipv4_address(const char *address, size_t len, oc_endpoint_t *endpoint)
{
if (!address || !endpoint) {
return;
return -1;
}
uint8_t *addr = endpoint->addr.ipv4.address;
size_t str_idx = 0, addr_idx = 0;
Expand All @@ -209,6 +209,7 @@ oc_parse_ipv4_address(const char *address, size_t len, oc_endpoint_t *endpoint)
str_idx += next_seg - &address[str_idx] + 1;
}
}
return 0;
}
#endif /* OC_IPV4 */

Expand Down Expand Up @@ -284,11 +285,11 @@ hex_to_bin(const char *hex, size_t len)
return b;
}

static void
static int
oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint)
{
if (!address || !endpoint) {
return;
return -1;
}
uint8_t *addr = endpoint->addr.ipv6.address;
memset(addr, 0, OC_IPV6_ADDRLEN);
Expand Down Expand Up @@ -331,7 +332,8 @@ oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint)
str_idx++;
} break;
default:
break;
OC_ERR("invalid ipv6 address segment(%s)", next_seg);
return -1;
}
}
if (split != -1) {
Expand All @@ -344,6 +346,7 @@ oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint)
addr_idx--;
}
}
return 0;
}

typedef struct endpoint_uri_t
Expand Down Expand Up @@ -546,13 +549,19 @@ oc_parse_endpoint_string(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
if (host_len > 1 && address[0] == '[' && address[host_len - 1] == ']') {
endpoint->flags = ep_uri.scheme_flags | IPV6;
endpoint->addr.ipv6.port = ep_uri.port;
oc_parse_ipv6_address(&address[1], host_len - 2, endpoint);
if (oc_parse_ipv6_address(&address[1], host_len - 2, endpoint) != 0) {
OC_ERR("invalid ipv6 address(%s)", address);
return -1;
}
}
#ifdef OC_IPV4
else {
endpoint->flags = ep_uri.scheme_flags | IPV4;
endpoint->addr.ipv4.port = ep_uri.port;
oc_parse_ipv4_address(address, host_len, endpoint);
if (oc_parse_ipv4_address(address, host_len, endpoint) != 0) {
OC_ERR("invalid ipv4 address(%s)", address);
return -1;
}
}
#else /* OC_IPV4 */
else {
Expand Down
56 changes: 45 additions & 11 deletions api/unittest/eptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@
#include <WinSock2.h>
#endif /* _WIN32 */

TEST(OCEndpoints, StringToEndpoint)
{
class TestOCEndpoint : public testing::Test {
protected:
void SetUp() override
{
#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif /* _WIN32 */
}

void TearDown() override
{
#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */
}
};

TEST_F(TestOCEndpoint, StringToEndpointBadFormat)
{
/* bad format */
std::vector<const char *> espu = {
nullptr,
Expand Down Expand Up @@ -59,7 +72,10 @@ TEST(OCEndpoints, StringToEndpoint)
oc_free_string(&s);
oc_free_string(&uri);
}
}

TEST_F(TestOCEndpoint, StringToEndpointCoap)
{
#ifdef OC_IPV4
std::vector<const char *> spu0 = { "coaps://10.211.55.3:56789/a/light" };
for (size_t i = 0; i < spu0.size(); i++) {
Expand Down Expand Up @@ -113,9 +129,6 @@ TEST(OCEndpoints, StringToEndpoint)
memset(&empty, 0, sizeof(oc_endpoint_t));
EXPECT_TRUE(memcmp(&empty, &ep, sizeof(oc_endpoint_t)) == 0);
EXPECT_EQ(nullptr, oc_string(uri));
#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */
oc_free_string(&s);
oc_free_string(&uri);
continue;
Expand Down Expand Up @@ -161,8 +174,11 @@ TEST(OCEndpoints, StringToEndpoint)
oc_free_string(&s);
oc_free_string(&uri);
}
}

#ifdef OC_TCP
TEST_F(TestOCEndpoint, StringToEndpointCoapTcp)
{
#ifdef OC_IPV4
std::vector<const char *> spu2 = {
"coaps+tcp://10.211.55.3/a/light",
Expand Down Expand Up @@ -279,12 +295,18 @@ TEST(OCEndpoints, StringToEndpoint)
oc_free_string(&s);
oc_free_string(&uri);
}
}
#endif /* OC_TCP */

TEST_F(TestOCEndpoint, StringToEndpointNoURI)
{
// test dns lookup when uri is NULL
std::vector<const char *> spu4 = {
#ifdef OC_IPV4
"coap://10.211.55.3:56789/a/light",
#ifdef OC_TCP
"coaps+tcp://10.211.55.3/a/light",
#endif /* OC_TCP */
#endif /* OC_IPV4 */
#if defined(OC_IPV4) || defined(OC_DNS_LOOKUP_IPV6)
"coap://openconnectivity.org/alpha",
Expand All @@ -300,10 +322,22 @@ TEST(OCEndpoints, StringToEndpoint)
EXPECT_EQ(ret, 0) << "spu4[" << i << "] " << spu4[i];
oc_free_string(&s);
}
#endif /* OC_TCP */
#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */
}

TEST_F(TestOCEndpoint, StringToEndpointWithScope)
{
std::vector<std::string> spu5 = {
"coap://[ff02::158%1]",
};
for (size_t i = 0; i < spu5.size(); i++) {
oc_string_t s;
oc_new_string(&s, spu5[i].c_str(), spu5[i].length());
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
int ret = oc_string_to_endpoint(&s, &ep, NULL);
EXPECT_EQ(ret, 0) << "spu5[" << i << "] " << spu5[i];
oc_free_string(&s);
}
}

TEST(OCEndpoints, EndpointStringParsePath)
Expand Down

0 comments on commit d48a96d

Please sign in to comment.