Skip to content

Commit

Permalink
dhcp6: added lease-xml output to tester
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomaschewski committed Feb 20, 2014
1 parent eae4613 commit 8363d71
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 37 deletions.
61 changes: 38 additions & 23 deletions dhcp6/main.c
Expand Up @@ -60,6 +60,8 @@ enum {
OPT_TEST,
OPT_TEST_TIMEOUT,
OPT_TEST_REQUEST,
OPT_TEST_OUTPUT,
OPT_TEST_OUTFMT,
};

static struct option options[] = {
Expand All @@ -82,7 +84,8 @@ static struct option options[] = {
{ "test", no_argument, NULL, OPT_TEST },
{ "test-request", required_argument, NULL, OPT_TEST_REQUEST },
{ "test-timeout", required_argument, NULL, OPT_TEST_TIMEOUT },

{ "test-output", required_argument, NULL, OPT_TEST_OUTPUT },
{ "test-format", required_argument, NULL, OPT_TEST_OUTFMT },
{ NULL, no_argument, NULL, 0 }
};

Expand All @@ -105,11 +108,8 @@ static void dhcp6_supplicant(void);
int
main(int argc, char **argv)
{
unsigned int opt_test_run = 0;
unsigned int opt_test_timeout = -1U;
const char * opt_test_request = NULL;
const char * opt_test_ifname = NULL;
int c;
dhcp6_tester_t * tester = NULL;
int c, status = NI_WICKED_RC_USAGE;

ni_log_init();
program_name = ni_basename(argv[0]);
Expand All @@ -118,6 +118,7 @@ main(int argc, char **argv)
switch (c) {
/* common */
case OPT_HELP:
status = NI_WICKED_RC_SUCCESS;
default:
usage:
fprintf(stderr,
Expand Down Expand Up @@ -147,24 +148,25 @@ main(int argc, char **argv)
" --test-timeout <timeout in sec> (default: 10)\n"
, program_name
);
return (c == OPT_HELP ? NI_LSB_RC_SUCCESS : NI_LSB_RC_USAGE);
return status;

case OPT_VERSION:
printf("%s %s\n", program_name, PACKAGE_VERSION);
return NI_LSB_RC_SUCCESS;
return NI_WICKED_RC_SUCCESS;

case OPT_CONFIGFILE:
if (!ni_set_global_config_path(optarg)) {
fprintf(stderr, "Unable to set config file '%s': %m\n", optarg);
return NI_LSB_RC_ERROR;
fprintf(stderr, "Unable to set config file '%s': %m\n",
optarg);
return NI_WICKED_RC_ERROR;
}
break;

case OPT_DEBUG:
if (!strcmp(optarg, "help")) {
printf("Supported debug facilities:\n");
ni_debug_help();
return NI_LSB_RC_SUCCESS;
return NI_WICKED_RC_SUCCESS;
}
if (ni_enable_debug(optarg) < 0) {
fprintf(stderr, "Bad debug facility \"%s\"\n", optarg);
Expand Down Expand Up @@ -200,25 +202,38 @@ main(int argc, char **argv)
/* test run */
case OPT_TEST:
opt_foreground = TRUE;
opt_test_run = 1;
tester = dhcp6_tester_init();
break;

case OPT_TEST_REQUEST:
if (!tester || ni_string_empty(optarg))
goto usage;
tester->request = optarg;
break;

case OPT_TEST_TIMEOUT:
if (!opt_test_run || ni_parse_uint(optarg, &opt_test_timeout, 0) < 0)
if (!tester || ni_parse_uint(optarg,
&tester->timeout, 0) < 0)
goto usage;
break;

case OPT_TEST_REQUEST:
if (!opt_test_run || ni_string_empty(optarg))
case OPT_TEST_OUTPUT:
if (!tester || ni_string_empty(optarg))
goto usage;
tester->output = optarg;
break;

case OPT_TEST_OUTFMT:
if (!tester || !dhcp6_tester_set_outfmt(optarg,
&tester->outfmt))
goto usage;
opt_test_request = optarg;
break;
}
}

if (opt_test_run) {
if (tester) {
if (optind < argc && !ni_string_empty(argv[optind])) {
opt_test_ifname = argv[optind++];
tester->ifname = argv[optind++];
} else {
fprintf(stderr, "Missing interface argument\n");
goto usage;
Expand All @@ -234,7 +249,7 @@ main(int argc, char **argv)
goto usage;
}
}
else if (opt_foreground && opt_test_run) {
else if (opt_foreground && tester) {
ni_log_destination(program_name, "stderr");
}
else if (opt_systemd || getppid() == 1 || !opt_foreground) { /* syslog only */
Expand All @@ -245,7 +260,7 @@ main(int argc, char **argv)
}

if (ni_init("dhcp6") < 0)
return NI_LSB_RC_ERROR;
return NI_WICKED_RC_ERROR;

if (opt_recover_state && ni_string_empty(opt_state_file)) {
static char dirname[PATH_MAX];
Expand All @@ -258,12 +273,12 @@ main(int argc, char **argv)
/* We're using randomized timeouts. Seed the RNG */
ni_srandom();

if (opt_test_run) {
return dhcp6_tester_run(opt_test_ifname, opt_test_request, opt_test_timeout);
if (tester) {
return dhcp6_tester_run(tester);
}

dhcp6_supplicant();
return NI_LSB_RC_SUCCESS;
return NI_WICKED_RC_SUCCESS;
}


Expand Down
77 changes: 65 additions & 12 deletions dhcp6/tester.c
Expand Up @@ -43,8 +43,29 @@
#include "duid.h"


static int dhcp6_tester_status;
static dhcp6_tester_t dhcp6_tester_opts;
static int dhcp6_tester_status;

dhcp6_tester_t *
dhcp6_tester_init(void)
{
memset(&dhcp6_tester_opts, 0, sizeof(dhcp6_tester_opts));
dhcp6_tester_opts.outfmt = DHCP6_TESTER_OUT_LEASE_INFO;
dhcp6_tester_opts.timeout = 30; /* 30 seconds */
return &dhcp6_tester_opts;
}

ni_bool_t
dhcp6_tester_set_outfmt(const char *outfmt, unsigned int *type)
{
static const ni_intmap_t __outfmt_map[] = {
{ "lease-xml", DHCP6_TESTER_OUT_LEASE_XML },
{ "leaseinfo", DHCP6_TESTER_OUT_LEASE_INFO },
{ "info", DHCP6_TESTER_OUT_LEASE_INFO },
{ NULL, DHCP6_TESTER_OUT_LEASE_INFO },
};
return ni_parse_uint_mapped(outfmt, __outfmt_map, type) == 0;
}

static void
dhcp6_tester_protocol_event(enum ni_dhcp6_event ev, const ni_dhcp6_device_t *dev,
Expand All @@ -57,7 +78,34 @@ dhcp6_tester_protocol_event(enum ni_dhcp6_event ev, const ni_dhcp6_device_t *dev
switch (ev) {
case NI_DHCP6_EVENT_ACQUIRED:
if (lease && lease->state == NI_ADDRCONF_STATE_GRANTED) {
ni_leaseinfo_dump(stdout, lease, dev->ifname, NULL);
FILE *fp = stdout;

if (dhcp6_tester_opts.output != NULL) {
fp = fopen(dhcp6_tester_opts.output, "w");
if (!fp) {
ni_error("Cannot open %s for output",
dhcp6_tester_opts.output);
dhcp6_tester_status = NI_WICKED_RC_ERROR;
return;
}
}
if (dhcp6_tester_opts.outfmt == DHCP6_TESTER_OUT_LEASE_XML) {
xml_node_t *xml = NULL;

if (ni_addrconf_lease_to_xml(lease, &xml) != 0) {
if (dhcp6_tester_opts.output)
fclose(fp);
dhcp6_tester_status = NI_WICKED_RC_ERROR;
return;
}
xml_node_print(xml, fp);
xml_node_free(xml);
} else {
ni_leaseinfo_dump(fp, lease, dev->ifname, NULL);
}
fflush(fp);
if (dhcp6_tester_opts.output)
fclose(fp);
dhcp6_tester_status = 0;
}
break;
Expand Down Expand Up @@ -160,7 +208,7 @@ dhcp6_tester_req_init(ni_dhcp6_request_t *req, const char *request)
}

int
dhcp6_tester_run(const char *ifname, const char *request, unsigned int timeout)
dhcp6_tester_run(dhcp6_tester_t *opts)
{
ni_netconfig_t *nc;
ni_netdev_t *ifp;
Expand All @@ -169,37 +217,41 @@ dhcp6_tester_run(const char *ifname, const char *request, unsigned int timeout)
char *errdetail = NULL;
int rv;

dhcp6_tester_status = 2;
if (!opts || ni_string_empty(opts->ifname))
ni_fatal("Invalid start parameters!");

dhcp6_tester_opts = *opts;
dhcp6_tester_status = NI_WICKED_RC_ERROR;

if (!(nc = ni_global_state_handle(1)))
ni_fatal("Cannot refresh interface list!");

if (!(ifp = ni_netdev_by_name(nc, ifname)))
ni_fatal("Cannot find interface with name '%s'", ifname);
if (!(ifp = ni_netdev_by_name(nc, opts->ifname)))
ni_fatal("Cannot find interface with name '%s'", opts->ifname);

switch (ifp->link.hwaddr.type) {
case ARPHRD_ETHER:
break;
default:
ni_fatal("Interface type not supported yet");
ni_fatal("Interface type of %s not supported yet", opts->ifname);
break;
}

if (!(dev = ni_dhcp6_device_new(ifp->name, &ifp->link)))
ni_fatal("Cannot allocate dhcp6 client for '%s'", ifname);
ni_fatal("Cannot allocate dhcp6 client for '%s'", opts->ifname);

ni_dhcp6_set_event_handler(dhcp6_tester_protocol_event);

if (!(req = ni_dhcp6_request_new())) {
ni_error("Cannot allocate dhcp6 request");
ni_error("Cannot allocate dhcp6 request for '%s'", opts->ifname);
goto failure;
}

if (!dhcp6_tester_req_init(req, request))
if (!dhcp6_tester_req_init(req, opts->request))
goto failure;

if (timeout != -1U)
req->acquire_timeout = timeout;
if (opts->timeout && opts->timeout != -1U)
req->acquire_timeout = opts->timeout;

if ((rv = ni_dhcp6_acquire(dev, req, &errdetail)) < 0) {
ni_error("%s: DHCPv6 acquire request %s failed: %s%s[%s]",
Expand All @@ -211,6 +263,7 @@ dhcp6_tester_run(const char *ifname, const char *request, unsigned int timeout)
goto failure;
}

dhcp6_tester_status = NI_WICKED_RC_IN_PROGRESS;
while (!ni_caught_terminal_signal()) {
long timeout;

Expand Down
19 changes: 17 additions & 2 deletions dhcp6/tester.h
Expand Up @@ -25,7 +25,22 @@
#ifndef __WICKED_DHCP6_TESTER_H__
#define __WICKED_DHCP6_TESTER_H__

extern int dhcp6_tester_run(const char *ifname, const char *request,
unsigned int timeout);
enum {
DHCP6_TESTER_OUT_LEASE_INFO,
DHCP6_TESTER_OUT_LEASE_XML,
};

typedef struct dhcp6_tester {
const char * ifname;
unsigned int timeout;
const char * request;
const char * output;
unsigned int outfmt;
} dhcp6_tester_t;

extern dhcp6_tester_t * dhcp6_tester_init(void);
extern ni_bool_t dhcp6_tester_set_outfmt(const char *, unsigned int *);

extern int dhcp6_tester_run(dhcp6_tester_t *);

#endif /* __WICKED_DHCP6_TESTER_H__ */

0 comments on commit 8363d71

Please sign in to comment.