Skip to content

Commit

Permalink
[libnozzle] cleanup nozzle_get_ips public API
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 15, 2018
1 parent e750718 commit b3ae1e6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 104 deletions.
11 changes: 1 addition & 10 deletions libnozzle/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <netlink/netlink.h>
#endif
#include <net/if.h>
#include "libnozzle.h"

struct nozzle_lib_config {
struct nozzle_iface *head;
Expand All @@ -24,16 +25,6 @@ struct nozzle_lib_config {
#endif
};

#define IPADDR_CHAR_MAX 128
#define PREFIX_CHAR_MAX 4

struct nozzle_ip {
char ipaddr[IPADDR_CHAR_MAX + 1];
char prefix[PREFIX_CHAR_MAX + 1];
int domain; /* AF_INET or AF_INET6 */
struct nozzle_ip *next;
};

#define MACADDR_CHAR_MAX 18

/*
Expand Down
99 changes: 25 additions & 74 deletions libnozzle/libnozzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,80 +408,7 @@ static int _set_ip(nozzle_t nozzle,
}

/*
* public API
*/

int nozzle_get_ips(const nozzle_t nozzle, char **ipaddr_list, int *entries)
{
int err = 0, savederrno = 0;
int found = 0;
char *ip_list = NULL;
int size = 0, offset = 0, len;
struct nozzle_ip *ip = NULL;

if ((!ipaddr_list) || (!entries)) {
errno = EINVAL;
return -1;
}

savederrno = pthread_mutex_lock(&config_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!is_valid_nozzle(nozzle)) {
savederrno = EINVAL;
goto out_clean;
}

ip = nozzle->ip;

while (ip) {
found++;
ip = ip->next;
}

if (!found) {
*ipaddr_list = NULL;
*entries = 0;
goto out_clean;
}

size = found * (IPADDR_CHAR_MAX + PREFIX_CHAR_MAX + 2);

ip_list = malloc(size);
if (!ip_list) {
savederrno = errno;
err = -1;
goto out_clean;
}

memset(ip_list, 0, size);

ip = nozzle->ip;

while (ip) {
len = strlen(ip->ipaddr);
memmove(ip_list + offset, ip->ipaddr, len);
offset = offset + len + 1;
len = strlen(ip->prefix);
memmove(ip_list + offset, ip->prefix, len);
offset = offset + len + 1;
ip = ip->next;
}

*ipaddr_list = ip_list;
*entries = found;

out_clean:
pthread_mutex_unlock(&config_mutex);
errno = savederrno;
return err;
}

/*
* functions below should be completed
* Exported public API
*/

nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath)
Expand Down Expand Up @@ -1241,3 +1168,27 @@ int nozzle_del_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix)
errno = savederrno;
return err;
}

int nozzle_get_ips(const nozzle_t nozzle, struct nozzle_ip **nozzle_ip)
{
int err = 0, savederrno = 0;

savederrno = pthread_mutex_lock(&config_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!is_valid_nozzle(nozzle)) {
err = -1;
savederrno = EINVAL;
goto out_clean;
}

*nozzle_ip = nozzle->ip;

out_clean:
pthread_mutex_unlock(&config_mutex);
errno = savederrno;
return err;
}
26 changes: 18 additions & 8 deletions libnozzle/libnozzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,37 @@ int nozzle_add_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix);

int nozzle_del_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix);


#define IPADDR_CHAR_MAX 128
#define PREFIX_CHAR_MAX 4

struct nozzle_ip {
char ipaddr[IPADDR_CHAR_MAX + 1];
char prefix[PREFIX_CHAR_MAX + 1];
int domain; /* AF_INET or AF_INET6 */
struct nozzle_ip *next;
};

/**
* nozzle_get_ips
* @brief retrive the list of all configured ips for a given interface
*
* TODO: change to use a ipaddr_list struct!
*
* nozzle - pointer to the nozzle struct
*
* ipaddr_list - list of strings containing either an IPv4 or an IPv6 address and their prefixes.
*
* entries - entries recorded.
* nozzle_ip - pointer to the head of a list of nozzle_ip structs.
* The last IP will have next = NULL.
* nozzle_ip can be NULL if there are no IP addresses
* associated with this nozzle device.
* *DO NOT* free those structs as they are used internally
* for IP address tracking.
*
* @return
* 0 on success
* -1 on error and errno is set.
* ipaddr_list is a malloc'ed buffer that the user needs to parse and free after use. ipaddr_list can
* be NULL if entries is 0.
*
*/

int nozzle_get_ips(const nozzle_t nozzle, char **ipaddr_list, int *entries);
int nozzle_get_ips(const nozzle_t nozzle, struct nozzle_ip **nozzle_ip);

/**
* nozzle_get_mtu
Expand Down
25 changes: 13 additions & 12 deletions libnozzle/tests/nozzle_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,10 @@ static int check_knet_set_del_ip(void)
char device_name[IFNAMSIZ];
size_t size = IFNAMSIZ;
char verifycmd[1024];
int err=0;
int err = 0;
nozzle_t nozzle;
char *ip_list = NULL;
int ip_list_entries = 0, i, offset = 0;
struct nozzle_ip *ip_list = NULL, *ip_list_tmp = NULL;
int ip_list_entries = 0, i;
char *error_string = NULL;

printf("Testing interface add/remove ip\n");
Expand Down Expand Up @@ -1048,26 +1048,27 @@ static int check_knet_set_del_ip(void)

printf("Get ip list from libnozzle:\n");

if (nozzle_get_ips(nozzle, &ip_list, &ip_list_entries) < 0) {
if (nozzle_get_ips(nozzle, &ip_list) < 0) {
printf("Not enough mem?\n");
err=-1;
goto out_clean;
}

ip_list_tmp = ip_list;
ip_list_entries = 0;

while(ip_list_tmp) {
ip_list_entries++;
printf("Found IP %s %s in libnozzle db\n", ip_list_tmp->ipaddr, ip_list_tmp->prefix);
ip_list_tmp = ip_list_tmp->next;
}

if (ip_list_entries != 2) {
printf("Didn't get enough ip back from libnozzle?\n");
err=-1;
goto out_clean;
}

for (i = 1; i <= ip_list_entries; i++) {
printf("Found IP %s %s in libnozzle db\n", ip_list + offset, ip_list + offset + strlen(ip_list + offset) + 1);
offset = offset + strlen(ip_list) + 1;
offset = offset + strlen(ip_list + offset) + 1;
}

free(ip_list);

printf("Deleting ip: %s/24\n", testipv4_1);

err = nozzle_del_ip(nozzle, testipv4_1, "24");
Expand Down

0 comments on commit b3ae1e6

Please sign in to comment.