Skip to content

Commit

Permalink
[nozzle] move ipv4 broadcast to internals
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 Aug 3, 2018
1 parent 670fa17 commit 6058f83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
47 changes: 24 additions & 23 deletions libnozzle/internals.c
Expand Up @@ -18,6 +18,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>

#include "libnozzle.h"
#include "internals.h"
Expand Down Expand Up @@ -160,6 +161,29 @@ int run_updown(const nozzle_t nozzle, const char *action, char **error_string)
return err;
}

char *generate_v4_broadcast(const char *ipaddr, const char *prefix)
{
int prefix_len;
struct in_addr mask;
struct in_addr broadcast;
struct in_addr address;

prefix_len = atoi(prefix);

if ((prefix_len > 32) || (prefix_len < 0))
return NULL;

if (inet_pton(AF_INET, ipaddr, &address) <= 0)
return NULL;

mask.s_addr = htonl(~((1 << (32 - prefix_len)) - 1));

memset(&broadcast, 0, sizeof(broadcast));
broadcast.s_addr = (address.s_addr & mask.s_addr) | ~mask.s_addr;

return strdup(inet_ntoa(broadcast));
}

#if 0
static void _close(nozzle_t nozzle)
{
Expand Down Expand Up @@ -627,29 +651,6 @@ int nozzle_set_down(nozzle_t nozzle, char **error_down, char **error_postdown)
return err;
}

static char *_get_v4_broadcast(const char *ipaddr, const char *prefix)
{
int prefix_len;
struct in_addr mask;
struct in_addr broadcast;
struct in_addr address;

prefix_len = atoi(prefix);

if ((prefix_len > 32) || (prefix_len < 0))
return NULL;

if (inet_pton(AF_INET, ipaddr, &address) <= 0)
return NULL;

mask.s_addr = htonl(~((1 << (32 - prefix_len)) - 1));

memset(&broadcast, 0, sizeof(broadcast));
broadcast.s_addr = (address.s_addr & mask.s_addr) | ~mask.s_addr;

return strdup(inet_ntoa(broadcast));
}

static int _set_ip(nozzle_t nozzle, const char *command,
const char *ipaddr, const char *prefix,
char **error_string, int secondary)
Expand Down
2 changes: 2 additions & 0 deletions libnozzle/internals.h
Expand Up @@ -63,4 +63,6 @@ struct nozzle_iface {
int execute_bin_sh_command(const char *command, char **error_string);
int run_updown(const nozzle_t nozzle, const char *action, char **error_string);

char *generate_v4_broadcast(const char *ipaddr, const char *prefix);

#endif
28 changes: 3 additions & 25 deletions libnozzle/libnozzle.c
Expand Up @@ -19,7 +19,6 @@
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <limits.h>
#include <stdio.h>
Expand Down Expand Up @@ -119,29 +118,6 @@ static int _set_down(nozzle_t nozzle, char **error_down, char **error_postdown)
return err;
}

static char *_get_v4_broadcast(const char *ipaddr, const char *prefix)
{
int prefix_len;
struct in_addr mask;
struct in_addr broadcast;
struct in_addr address;

prefix_len = atoi(prefix);

if ((prefix_len > 32) || (prefix_len < 0))
return NULL;

if (inet_pton(AF_INET, ipaddr, &address) <= 0)
return NULL;

mask.s_addr = htonl(~((1 << (32 - prefix_len)) - 1));

memset(&broadcast, 0, sizeof(broadcast));
broadcast.s_addr = (address.s_addr & mask.s_addr) | ~mask.s_addr;

return strdup(inet_ntoa(broadcast));
}

static int _set_ip(nozzle_t nozzle, const char *command,
const char *ipaddr, const char *prefix,
char **error_string, int secondary)
Expand All @@ -156,7 +132,7 @@ static int _set_ip(nozzle_t nozzle, const char *command,
#endif

if (!strchr(ipaddr, ':')) {
broadcast = _get_v4_broadcast(ipaddr, prefix);
broadcast = generate_v4_broadcast(ipaddr, prefix);
if (!broadcast) {
errno = EINVAL;
return -1;
Expand Down Expand Up @@ -239,6 +215,8 @@ static int _find_ip(nozzle_t nozzle,

/*
* internal helpers below should be completed
*
* keep all ioctl work within this file
*/

static int is_valid_nozzle(const nozzle_t nozzle)
Expand Down

0 comments on commit 6058f83

Please sign in to comment.