Skip to content

Commit

Permalink
[nozzle] cleanup and document nozzle_add_ip
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 Apr 20, 2018
1 parent 9e90d99 commit e71372b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
24 changes: 19 additions & 5 deletions libnozzle/libnozzle.c
Expand Up @@ -1000,27 +1000,40 @@ static int _find_ip(nozzle_t nozzle,

int nozzle_add_ip(nozzle_t nozzle, const char *ip_addr, const char *prefix, char **error_string)
{
int err = 0, found;
int err = 0, savederrno = 0;
int found = 0;
struct _ip *ip = NULL, *ip_prev = NULL, *ip_last = NULL;
int secondary = 0;

pthread_mutex_lock(&lib_mutex);
if ((!nozzle) || (!ip_addr) || (!prefix) || (!error_string)) {
errno = EINVAL;
return -1;
}

if ((!_check(nozzle)) || (!ip_addr) || (!prefix) || (!error_string)) {
savederrno = pthread_mutex_lock(&lib_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!_check(nozzle)) {
errno = EINVAL;
err = -1;
goto out_clean;
}

found = _find_ip(nozzle, ip_addr, prefix, &ip, &ip_prev);
if (found)
if (found) {
goto out_clean;
}

ip = malloc(sizeof(struct _ip));
if (!ip) {
savederrno = errno;
err = -1 ;
goto out_clean;
}

memset(ip, 0, sizeof(struct _ip));
strncpy(ip->ip_addr, ip_addr, MAX_IP_CHAR);
strncpy(ip->prefix, prefix, MAX_PREFIX_CHAR);
Expand All @@ -1041,6 +1054,7 @@ int nozzle_add_ip(nozzle_t nozzle, const char *ip_addr, const char *prefix, char
secondary = 1;
}
err = _set_ip(nozzle, "add", ip_addr, prefix, error_string, secondary);
savederrno = errno;
}

if (err) {
Expand All @@ -1060,7 +1074,7 @@ int nozzle_add_ip(nozzle_t nozzle, const char *ip_addr, const char *prefix, char

out_clean:
pthread_mutex_unlock(&lib_mutex);

errno = savederrno;
return err;
}

Expand Down
23 changes: 23 additions & 0 deletions libnozzle/libnozzle.h
Expand Up @@ -125,6 +125,29 @@ int nozzle_set_up(nozzle_t nozzle, char **error_preup, char **error_up);

int nozzle_set_down(nozzle_t nozzle, char **error_down, char **error_postdown);

/**
* nozzle_add_ip
* @brief equivalent of ip addr or ifconfig <ipaddress/prefix>
*
* nozzle - pointer to the nozzle struct
*
* ip_addr - string containing either an IPv4 or an IPv6 address.
* Please note that Linux will automatically remove any IPv6 addresses from an interface
* with MTU < 1280. libnozzle will cache those IPs and re-instate them when MTU is > 1280.
* MTU must be set via nozzle_set_mtu for IPv6 to be re-instated.
*
* prefix - 24, 64 or any valid network prefix for the requested address.
*
* error_string - pointers to string to record errors from ipaddr2 (Linux) or ifconfig (BSD).
* The string is malloc'ed, the caller needs to free this buffer.
*
* @return
* 0 on success
* -1 on error and error is set.
* error_string is set to NULL on success
* error_string will contain a string recording the execution error.
*/

int nozzle_add_ip(nozzle_t nozzle, const char *ip_addr, const char *prefix, char **error_string);
int nozzle_del_ip(nozzle_t nozzle, const char *ip_addr, const char *prefix, char **error_string);
int nozzle_get_ips(const nozzle_t nozzle, char **ip_addr_list, int *entries);
Expand Down

0 comments on commit e71372b

Please sign in to comment.