Skip to content

Commit

Permalink
use oc_endpoint_to_string64 to utilize stack
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Sep 15, 2023
1 parent 150c124 commit b52d603
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 113 deletions.
10 changes: 4 additions & 6 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,9 @@ collection_encode_links(const oc_collection_t *collection,
continue;
}
oc_rep_object_array_start_item(eps);
oc_string_t ep;
if (oc_endpoint_to_string(eps, &ep) == 0) {
oc_string64_t ep;
if (oc_endpoint_to_string64(eps, &ep)) {
oc_rep_set_text_string_v1(eps, ep, oc_string(ep), oc_string_len(ep));
oc_free_string(&ep);
}
oc_rep_object_array_end_item(eps);
}
Expand Down Expand Up @@ -856,10 +855,9 @@ oc_handle_collection_linked_list_request(oc_request_t *request)
continue;
}
oc_rep_object_array_start_item(eps);
oc_string_t ep;
if (oc_endpoint_to_string(eps, &ep) == 0) {
oc_string64_t ep;
if (oc_endpoint_to_string64(eps, &ep)) {
oc_rep_set_text_string_v1(eps, ep, oc_string(ep), oc_string_len(ep));
oc_free_string(&ep);
}
oc_rep_object_array_end_item(eps);
}
Expand Down
22 changes: 11 additions & 11 deletions api/oc_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ discovery_encode_endpoint(CborEncoder *eps, const oc_endpoint_t *ep,
int latency)
{
oc_rep_begin_object(eps, ep);
oc_string_t ep_str;
if (oc_endpoint_to_string(ep, &ep_str) == 0) {
oc_string64_t ep_str;
if (oc_endpoint_to_string64(ep, &ep_str)) {
g_err |= oc_rep_encode_text_string(&ep_map, "ep", OC_CHAR_ARRAY_LEN("ep"));
g_err |= oc_rep_encode_text_string(&ep_map, oc_string(ep_str),
oc_string_len(ep_str));
oc_free_string(&ep_str);
}
if (latency > 0) {
g_err |=
Expand Down Expand Up @@ -1058,14 +1057,15 @@ oc_wkcore_discovery_handler(oc_request_t *request,
oc_string_t uri;
memset(&uri, 0, sizeof(oc_string_t));
while (eps != NULL) {
if (eps->flags & SECURED) {
oc_string_t ep;
if (oc_endpoint_to_string(eps, &ep) == 0) {
length = clf_add_str_to_buffer(oc_string(ep), oc_string_len(ep));
response_length += length;
oc_free_string(&ep);
break;
}
if ((eps->flags & SECURED) == 0) {
eps = eps->next;
continue;
}
oc_string64_t ep;
if (oc_endpoint_to_string64(eps, &ep)) {
length = clf_add_str_to_buffer(oc_string(ep), oc_string_len(ep));
response_length += length;
break;
}
eps = eps->next;
}
Expand Down
71 changes: 59 additions & 12 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,50 @@ oc_endpoint_set_di(oc_endpoint_t *endpoint, const oc_uuid_t *di)
memcpy(endpoint->di.id, di->id, sizeof(di->id));
}

const char *
oc_endpoint_flags_to_scheme(unsigned flags)
bool
oc_endpoint_flags_to_scheme(unsigned flags, char *buf, size_t *buf_len)
{
if (buf_len == NULL) {
return false;
}
size_t size = *buf_len;
#ifdef OC_TCP
if ((flags & TCP) != 0) {
if ((flags & SECURED) != 0) {
return OC_SCHEME_COAPS_TCP;
*buf_len = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAPS_TCP);
if (buf != NULL && *buf_len <= size) {
memcpy(buf, OC_SCHEME_COAPS_TCP, *buf_len);
return true;
}
return false;
}
*buf_len = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAP_TCP);
if (buf != NULL && *buf_len <= size) {
memcpy(buf, OC_SCHEME_COAP_TCP, *buf_len);
return true;
}
return OC_SCHEME_COAP_TCP;
return false;
}
#endif
if ((flags & SECURED) != 0) {
return OC_SCHEME_COAPS;
*buf_len = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAPS);
if (buf != NULL && *buf_len <= size) {
memcpy(buf, OC_SCHEME_COAPS, *buf_len);
return true;
}
return false;
}
return OC_SCHEME_COAP;
*buf_len = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAP);
if (buf != NULL && *buf_len <= size) {
memcpy(buf, OC_SCHEME_COAP, *buf_len);
return true;
}
return false;
}

int
oc_endpoint_host(const oc_endpoint_t *endpoint, char *buffer,
uint32_t buffer_size)
size_t buffer_size)
{
#ifdef OC_IPV4
if ((endpoint->flags & IPV4) != 0) {
Expand All @@ -100,7 +124,7 @@ oc_endpoint_host(const oc_endpoint_t *endpoint, char *buffer,

int
oc_endpoint_to_cstring(const oc_endpoint_t *endpoint, char *buffer,
uint32_t buffer_size)
size_t buffer_size)
{
#ifdef OC_IPV4
if ((endpoint->flags & IPV4) != 0) {
Expand All @@ -122,15 +146,38 @@ oc_endpoint_to_string(const oc_endpoint_t *endpoint, oc_string_t *endpoint_str)
return -1;
}

char ip[OC_IPV6_MAXSTRLEN] = { 0 };
if (oc_endpoint_to_cstring(endpoint, ip, OC_ARRAY_SIZE(ip)) != 0) {
oc_string64_t ep_str;
if (!oc_endpoint_to_string64(endpoint, &ep_str)) {
return -1;
}
oc_concat_strings(endpoint_str, oc_endpoint_flags_to_scheme(endpoint->flags),
ip);
oc_new_string(endpoint_str, oc_string(ep_str), oc_string_len(ep_str));
return 0;
}

bool
oc_endpoint_to_string64(const oc_endpoint_t *endpoint,
oc_string64_t *endpoint_str)
{
if (!endpoint || !endpoint_str) {
return false;
}
memset(endpoint_str, 0, sizeof(oc_string64_t));
size_t cap = oc_string64_cap(*endpoint_str);
if (!oc_endpoint_flags_to_scheme(endpoint->flags, oc_string(*endpoint_str),
&cap)) {
return false;
}
endpoint_str->size = cap;
cap = oc_string64_cap(*endpoint_str);
int ep_len = oc_endpoint_to_cstring(
endpoint, oc_string(*endpoint_str) + oc_string_len(*endpoint_str) + 1, cap);
if (ep_len < 0) {
return false;
}
endpoint_str->size += (size_t)ep_len + 1;
return true;
}

int
oc_endpoint_port(const oc_endpoint_t *endpoint)
{
Expand Down
19 changes: 15 additions & 4 deletions api/oc_endpoint_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "util/oc_compiler.h"

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -40,8 +42,17 @@ extern "C" {

#define OC_SCHEME_OCF "ocf://"

/** @brief Get scheme string for transport flags */
const char *oc_endpoint_flags_to_scheme(unsigned flags) OC_RETURNS_NONNULL;
/**
* @brief Get scheme string for transport flags
*
* @param flags type of endpoint
* @param buf to store the scheme
* @param buf_len length of the buffer and will be set to the length of the
* used/needed buffer.
* @return true if the buf has been set with valid scheme. For false the buf_len
* will contain the needed length
*/
bool oc_endpoint_flags_to_scheme(unsigned flags, char *buf, size_t *buf_len);

/**
* @brief Convert the endpoint to a human readable string (e.g.
Expand All @@ -53,11 +64,11 @@ const char *oc_endpoint_flags_to_scheme(unsigned flags) OC_RETURNS_NONNULL;
* @return int 0 success
*/
int oc_endpoint_to_cstring(const oc_endpoint_t *endpoint, char *buffer,
uint32_t buffer_size) OC_NONNULL();
size_t buffer_size) OC_NONNULL();

/** @brief Get host of the endpoint as string */
int oc_endpoint_host(const oc_endpoint_t *endpoint, char *buffer,
uint32_t buffer_size) OC_NONNULL();
size_t buffer_size) OC_NONNULL();

/** @brief Get port of the endpoint */
int oc_endpoint_port(const oc_endpoint_t *endpoint) OC_NONNULL();
Expand Down
5 changes: 2 additions & 3 deletions api/oc_introspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ oc_introspection_wk_get_uri(size_t device, int interface_index,
if ((interface_index == -1 ||
eps->interface_index == (unsigned)interface_index) &&
(eps->flags == flags)) {
oc_string_t ep;
if (oc_endpoint_to_string(eps, &ep) == 0) {
oc_string64_t ep;
if (oc_endpoint_to_string64(eps, &ep)) {
oc_concat_strings(uri, oc_string(ep), OC_INTROSPECTION_DATA_URI);
oc_free_string(&ep);
return true;
}
}
Expand Down
12 changes: 5 additions & 7 deletions api/oc_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,9 @@ get_ns_properties(const oc_resource_t *resource, oc_interface_mask_t iface_mask,
/*
* pushtarget
*/
oc_string_t ep;
oc_string64_t ep;
oc_string_t full_uri;
if (oc_endpoint_to_string(&ns_instance->pushtarget_ep, &ep) < 0) {
if (!oc_endpoint_to_string64(&ns_instance->pushtarget_ep, &ep)) {
/* handle NULL pushtarget... */
#if 0
char ipv6addrstr[50], ipv4addrstr[50];
Expand All @@ -594,8 +594,6 @@ get_ns_properties(const oc_resource_t *resource, oc_interface_mask_t iface_mask,
oc_string(ns_instance->targetpath));
else
oc_new_string(&full_uri, oc_string(ep), oc_string_len(ep));

oc_free_string(&ep);
}

oc_rep_set_text_string(root, pushtarget, oc_string(full_uri));
Expand Down Expand Up @@ -2428,9 +2426,10 @@ push_update(oc_ns_t *ns_instance)
return false;
}
#ifdef OC_PUSHDEBUG
oc_string_t ep, full_uri;
oc_string64_t ep;
oc_string_t full_uri;

oc_endpoint_to_string(&ns_instance->pushtarget_ep, &ep);
oc_endpoint_to_string64(&ns_instance->pushtarget_ep, &ep);
if (oc_string_len(ns_instance->targetpath)) {
oc_concat_strings(&full_uri, oc_string(ep),
oc_string(ns_instance->targetpath));
Expand All @@ -2440,7 +2439,6 @@ push_update(oc_ns_t *ns_instance)

OC_PUSH_DBG("push \"%s\" ====> \"%s\"", oc_string(src_rsc->uri),
oc_string(full_uri));
oc_free_string(&ep);
oc_free_string(&full_uri);
#endif
OC_PUSH_DBG("state of Push Proxy (\"%s\") is changed (%s => %s)",
Expand Down
Loading

0 comments on commit b52d603

Please sign in to comment.