Skip to content

Commit

Permalink
dco-freebsd: dynamically re-allocate buffer if it's too small
Browse files Browse the repository at this point in the history
It's possible for the buffer we provide for OVPN_GET_PEER_STATS to be
too small. Handle the error, re-allocate a larger buffer and try again
rather than failing.

Signed-off-by: Kristof Provost <[email protected]>
Acked-by: Gert Doering <[email protected]>
Message-Id: <[email protected]>
URL: https://www.mail-archive.com/[email protected]/msg28128.html
Signed-off-by: Gert Doering <[email protected]>
(cherry picked from commit 6267693)
  • Loading branch information
kprovost authored and cron2 committed Feb 7, 2024
1 parent 6bed72d commit d8faf56
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/openvpn/dco_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m)
{

struct ifdrv drv;
uint8_t buf[4096];
uint8_t *buf = NULL;
size_t buf_size = 4096;
nvlist_t *nvl;
const nvlist_t *const *nvpeers;
size_t npeers;
Expand All @@ -712,17 +713,28 @@ dco_get_peer_stats_multi(dco_context_t *dco, struct multi_context *m)
CLEAR(drv);
snprintf(drv.ifd_name, IFNAMSIZ, "%s", dco->ifname);
drv.ifd_cmd = OVPN_GET_PEER_STATS;
drv.ifd_len = sizeof(buf);

retry:
buf = realloc(buf, buf_size);
drv.ifd_len = buf_size;
drv.ifd_data = buf;

ret = ioctl(dco->fd, SIOCGDRVSPEC, &drv);
if (ret && errno == ENOSPC)
{
buf_size *= 2;
goto retry;
}

if (ret)
{
free(buf);
msg(M_WARN | M_ERRNO, "Failed to get peer stats");
return -EINVAL;
}

nvl = nvlist_unpack(buf, drv.ifd_len, 0);
free(buf);
if (!nvl)
{
msg(M_WARN, "Failed to unpack nvlist");
Expand Down

0 comments on commit d8faf56

Please sign in to comment.