Skip to content

Commit

Permalink
utils: add ni_netdev_ref_array_t and basic utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomaschewski committed Mar 28, 2022
1 parent c1896e6 commit e7aec13
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/wicked/netinfo.h
Expand Up @@ -257,6 +257,17 @@ extern ni_netdev_t * ni_netdev_ref_bind_ifindex(ni_netdev_ref_t *, ni_netconfig_
extern ni_netdev_t * ni_netdev_ref_bind_ifname (ni_netdev_ref_t *, ni_netconfig_t *);
extern void ni_netdev_ref_destroy(ni_netdev_ref_t *);

extern ni_bool_t ni_netdev_ref_array_init(ni_netdev_ref_array_t *);
extern const ni_netdev_ref_t *
ni_netdev_ref_array_at(const ni_netdev_ref_array_t *, unsigned int);
extern const ni_netdev_ref_t *
ni_netdev_ref_array_find_name(const ni_netdev_ref_array_t *, const char *);
extern const ni_netdev_ref_t *
ni_netdev_ref_array_find_index(const ni_netdev_ref_array_t *, unsigned int);
extern const ni_netdev_ref_t *
ni_netdev_ref_array_append(ni_netdev_ref_array_t *, const char *, unsigned int);
extern void ni_netdev_ref_array_destroy(ni_netdev_ref_array_t *);

extern ni_netdev_req_t *ni_netdev_req_new(void);
extern void ni_netdev_req_free(ni_netdev_req_t *req);

Expand Down
7 changes: 7 additions & 0 deletions include/wicked/types.h
Expand Up @@ -76,6 +76,13 @@ typedef struct ni_netdev_ref {
char * name; /* by ifname */
} ni_netdev_ref_t;

#define NI_NETDEV_REF_ARRAY_INIT { .count = 0, .data = NULL }

typedef struct ni_netdev_ref_array {
unsigned int count;
ni_netdev_ref_t * data;
} ni_netdev_ref_array_t;

typedef struct ni_dbus_server ni_dbus_server_t;
typedef struct ni_dbus_client ni_dbus_client_t;

Expand Down
111 changes: 111 additions & 0 deletions src/netinfo.c
Expand Up @@ -35,6 +35,9 @@
#include "dhcp.h"
#include <gcrypt.h>


#define NI_NETDEV_REF_ARRAY_CHUNK 16

extern void ni_addrconf_updater_free(ni_addrconf_updater_t **);

typedef struct ni_netconfig_filter {
Expand Down Expand Up @@ -1008,6 +1011,114 @@ ni_netdev_ref_destroy(ni_netdev_ref_t *ref)
}
}

ni_bool_t
ni_netdev_ref_array_init(ni_netdev_ref_array_t *array)
{
if (array) {
memset(array, 0, sizeof(*array));
return TRUE;
}
return FALSE;
}

const ni_netdev_ref_t *
ni_netdev_ref_array_at(const ni_netdev_ref_array_t *array, unsigned int i)
{
if (!array || i >= array->count)
return NULL;
return &array->data[i];
}

const ni_netdev_ref_t *
ni_netdev_ref_array_find_index(const ni_netdev_ref_array_t *array, unsigned int index)
{
const ni_netdev_ref_t *ref;
unsigned int i;

if (!array)
return NULL;

for (i = 0; i < array->count; ++i) {
ref = &array->data[i];
if (ref->index == index)
return ref;
}
return NULL;
}

const ni_netdev_ref_t *
ni_netdev_ref_array_find_name(const ni_netdev_ref_array_t *array, const char *name)
{
const ni_netdev_ref_t *ref;
unsigned int i;

if (!array)
return NULL;

for (i = 0; i < array->count; ++i) {
ref = &array->data[i];
if (ni_string_eq(ref->name, name))
return ref;
}
return NULL;
}

static ni_bool_t
ni_netdev_ref_array_realloc(ni_netdev_ref_array_t *array, unsigned int count)
{
ni_netdev_ref_t *newdata;
size_t newsize;
unsigned int i;

if ((UINT_MAX - array->count) <= count)
return FALSE;

newsize = array->count + count;
if ((SIZE_MAX / sizeof(*newdata)) < newsize)
return FALSE;

newdata = realloc(array->data, newsize * sizeof(*newdata));
if (!newdata)
return FALSE;

array->data = newdata;
for (i = array->count; i < newsize; ++i) {
array->data[i].index = 0;
array->data[i].name = NULL;
}
return TRUE;
}

const ni_netdev_ref_t *
ni_netdev_ref_array_append(ni_netdev_ref_array_t *array, const char *name, unsigned int index)
{
ni_netdev_ref_t *item;

if (!array || ((array->count % NI_NETDEV_REF_ARRAY_CHUNK) == 0 &&
!ni_netdev_ref_array_realloc(array, NI_NETDEV_REF_ARRAY_CHUNK)))
return NULL;

item = &array->data[array->count++];
ni_netdev_ref_set(item, name, index);
return item;
}

void
ni_netdev_ref_array_destroy(ni_netdev_ref_array_t *array)
{
ni_netdev_ref_t *item;

if (array) {
while (array->count) {
array->count--;
item = &array->data[array->count];
ni_netdev_ref_destroy(item);
}
free(array->data);
array->data = NULL;
}
}

/*
* Handle netdev request port config
*/
Expand Down

0 comments on commit e7aec13

Please sign in to comment.