Skip to content

Commit

Permalink
port/linux: Address Issues with ENOBUF Failure in Netlink IP Address …
Browse files Browse the repository at this point in the history
…Retrieval

In some cases, it can lead to issues because if the IP addresses
are obtained through netlink and there is a failure due to ENOBUF,
the device may report incorrect endpoints during the discovery process.
  • Loading branch information
jkralik committed Jan 30, 2024
1 parent 965846c commit f4d26ee
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions port/linux/ipadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "api/oc_endpoint_internal.h"
#include "api/oc_network_events_internal.h"
#include "api/oc_message_internal.h"
#include "ip.h"
#include "ipadapter.h"
#include "ipcontext.h"
Expand Down Expand Up @@ -244,25 +245,6 @@ oc_get_ip_context_for_device(size_t device)
return dev;
}

static ssize_t
get_data_size(int sock)
{
size_t guess = 512;
ssize_t response_len;
do {
guess <<= 1;
uint8_t dummy[guess];
response_len = recv(sock, dummy, guess, MSG_PEEK);
if (response_len < 0) {
if (errno == EINTR) {
continue;
}
return -errno;
}
} while ((size_t)response_len == guess);
return response_len;
}

static ssize_t
get_data(int sock, uint8_t *buffer, size_t buffer_size)
{
Expand Down Expand Up @@ -327,24 +309,28 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, int port,

long prev_interface_index = -1;
bool done = false;
// message used to read data from netlink socket
oc_message_t *message = oc_allocate_message();
if (!message) {
close(nl_sock);
return false;
}
while (!done) {
ssize_t response_len = get_data_size(nl_sock);
if (response_len < 0) {
OC_ERR("failed to get data size (error %d)", (int)-response_len);
close(nl_sock);
return false;
}
uint8_t buffer[response_len];
response_len = get_data(nl_sock, buffer, sizeof(buffer));
ssize_t response_len =
get_data(nl_sock, message->data, oc_message_buffer_size(message));
if (response_len < 0) {
OC_ERR("failed to get data (error %d)", (int)-response_len);
close(nl_sock);
oc_message_unref(message);
return false;
}

response = (struct nlmsghdr *)buffer;
CLANG_IGNORE_WARNING_START
CLANG_IGNORE_WARNING("-Wcast-align")
response = (struct nlmsghdr *)message->data;
CLANG_IGNORE_WARNING_END
if (response->nlmsg_type == NLMSG_ERROR) {
close(nl_sock);
oc_message_unref(message);
return false;
}

Expand Down Expand Up @@ -416,6 +402,7 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, int port,
#endif /* OC_TCP */
oc_endpoint_t *new_ep = oc_memb_alloc(&g_device_eps);
if (!new_ep) {
oc_message_unref(message);
close(nl_sock);
return false;
}
Expand All @@ -431,6 +418,7 @@ get_interface_addresses(ip_context_t *dev, unsigned char family, int port,
}
}
close(nl_sock);
oc_message_unref(message);
return true;
}

Expand Down Expand Up @@ -549,23 +537,25 @@ oc_connectivity_get_endpoints(size_t device)
static int
process_interface_change_event(void)
{
ssize_t response_len = get_data_size(g_ifchange_sock);
if (response_len < 0) {
OC_ERR("failed reading payload size from netlink interface (error %d)",
(int)-response_len);
oc_message_t *message = oc_allocate_message();
if (!message) {
return -1;
}
uint8_t buffer[response_len];
response_len = get_data(g_ifchange_sock, buffer, sizeof(buffer));
ssize_t response_len =
get_data(g_ifchange_sock, message->data, oc_message_buffer_size(message));
if (response_len < 0) {
OC_ERR("failed reading payload from netlink interface (error %d)",
(int)-response_len);
oc_message_unref(message);
return -1;
}

struct nlmsghdr *response = (struct nlmsghdr *)buffer;
CLANG_IGNORE_WARNING_START
CLANG_IGNORE_WARNING("-Wcast-align")
struct nlmsghdr *response = (struct nlmsghdr *)message->data;
CLANG_IGNORE_WARNING_END
if (response->nlmsg_type == NLMSG_ERROR) {
OC_ERR("caught NLMSG_ERROR in payload from netlink interface");
oc_message_unref(message);
return -1;
}

Expand Down Expand Up @@ -662,7 +652,7 @@ process_interface_change_event(void)
}
}
}

oc_message_unref(message);
return success ? 0 : -1;
}

Expand Down

0 comments on commit f4d26ee

Please sign in to comment.