Commit 2c6e4460 authored by Daniel Salzman's avatar Daniel Salzman

Integrate params_t to nsupdate_params_t

And remove params_t.

refs #2137
parent b788c3da
......@@ -23,7 +23,7 @@
#include <stdio.h>
#include <stdlib.h> // free
#include <netinet/in.h> // in_addr
#include <netinet/in.h> // in_addr
#include <arpa/inet.h> // inet_pton
#include <sys/socket.h> // AF_INET (BSD)
......@@ -53,14 +53,14 @@ static knot_dname_t* create_fqdn_from_str(const char *str, size_t len)
/* Table of known keys in private-key-format */
static const char *pkey_tbl[] = {
"\x09" "Activate:",
"\x0a" "Algorithm:",
"\x05" "Bits:",
"\x08" "Created:",
"\x04" "Key:",
"\x13" "Private-key-format:",
"\x08" "Publish:",
NULL
"\x09" "Activate:",
"\x0a" "Algorithm:",
"\x05" "Bits:",
"\x08" "Created:",
"\x04" "Key:",
"\x13" "Private-key-format:",
"\x08" "Publish:",
NULL
};
enum {
......@@ -183,60 +183,6 @@ char* get_fqd_name(const char *name)
return fqd_name;
}
void params_flag_ipv4(params_t *params)
{
if (params == NULL) {
return;
}
params->ip = IP_4;
}
void params_flag_ipv6(params_t *params)
{
if (params == NULL) {
return;
}
params->ip = IP_6;
}
void params_flag_servfail(params_t *params)
{
if (params == NULL) {
return;
}
params->servfail_stop = true;
}
void params_flag_nowait(params_t *params)
{
if (params == NULL) {
return;
}
params->wait = -1;
}
void params_flag_tcp(params_t *params)
{
if (params == NULL) {
return;
}
params->protocol = PROTO_TCP;
}
void params_flag_verbose(params_t *params)
{
if (params == NULL) {
return;
}
params->format = FORMAT_VERBOSE;
}
int params_parse_port(const char *value, char **port)
{
char *new_port = strdup(value);
......
......@@ -27,10 +27,9 @@
#ifndef _UTILS__PARAMS_H_
#define _UTILS__PARAMS_H_
#include <stdbool.h> // bool
#include <stdint.h> // uint16_t
#include "common/lists.h" // node
#include "common/lists.h" // list
#include "libknot/tsig.h" // knot_key_t
#define DEFAULT_IPV4_NAME "127.0.0.1"
......@@ -53,15 +52,6 @@ typedef enum {
PROTO_UDP
} protocol_t;
typedef enum {
/*!< Classic queries in list. */
OPERATION_QUERY,
/*!< Query for NS and all authoritative SOA records. */
OPERATION_LIST_SOA,
/*!< Default mode for nsupdate. */
OPERATION_UPDATE,
} operation_t;
typedef enum {
/*!< Short dig output. */
FORMAT_DIG,
......@@ -75,58 +65,10 @@ typedef enum {
FORMAT_MULTILINE,
} format_t;
/*! \brief Structure containing parameters. */
typedef struct {
/*!< List of nameservers to query to. */
list servers;
/*!< Operation mode. */
operation_t operation;
/*!< Version of ip protocol to use. */
ip_t ip;
/*!< Type (TCP, UDP) protocol to use. */
protocol_t protocol;
/*!< Default port/service to connect to. */
char *port;
/*!< Default class number (16unsigned + -1 uninitialized). */
int32_t class_num;
/*!< Default type number (16unsigned + -1 uninitialized). */
int32_t type_num;
/*!< Default TTL. */
uint32_t ttl;
/*!< Default SOA serial for XFR. */
uint32_t xfr_serial;
/*!< UDP buffer size. */
uint32_t udp_size;
/*!< Number of UDP retries. */
uint32_t retries;
/*!< Wait for network response in seconds (-1 means forever). */
int32_t wait;
/*!< Stop quering if servfail. */
bool servfail_stop;
/*!< Output format. */
format_t format;
/*!< TSIG key used. */
knot_key_t key;
/*!< Implementation specific data. */
void *d;
} params_t;
char* get_reverse_name(const char *name);
char* get_fqd_name(const char *name);
void params_flag_ipv4(params_t *params);
void params_flag_ipv6(params_t *params);
void params_flag_servfail(params_t *params);
void params_flag_nowait(params_t *params);
void params_flag_tcp(params_t *params);
void params_flag_verbose(params_t *params);
int params_parse_port(const char *value, char **port);
int params_parse_class(const char *value, uint16_t *rclass);
......
......@@ -45,6 +45,15 @@ typedef struct {
uint32_t xfr_serial;
} query_t;
typedef enum {
/*!< Classic queries in list. */
OPERATION_QUERY,
/*!< Query for NS and all authoritative SOA records. */
OPERATION_LIST_SOA,
/*!< Default mode for nsupdate. */
OPERATION_UPDATE,
} operation_t;
typedef struct {
/*!< Recursion desired flag. */
bool rd_flag;
......
This diff is collapsed.
......@@ -27,9 +27,9 @@
#ifndef _NSUPDATE__NSUPDATE_EXEC_H_
#define _NSUPDATE__NSUPDATE_EXEC_H_
#include "utils/nsupdate/nsupdate_params.h"
#include "utils/nsupdate/nsupdate_params.h" // nsupdate_params_t
int nsupdate_exec(params_t *params);
int nsupdate_exec(nsupdate_params_t *params);
#endif // _NSUPDATE__NSUPDATE_EXEC_H_
......
......@@ -24,8 +24,8 @@ int main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
params_t params;
if (nsupdate_params_parse(&params, argc, argv) == KNOT_EOK) {
nsupdate_params_t params;
if (nsupdate_parse(&params, argc, argv) == KNOT_EOK) {
if (nsupdate_exec(&params) != KNOT_EOK) {
ret = EXIT_FAILURE;
}
......@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
ret = EXIT_FAILURE;
}
nsupdate_params_clean(&params);
nsupdate_clean(&params);
return ret;
}
......@@ -23,9 +23,9 @@
#include "utils/nsupdate/nsupdate_params.h"
#include "utils/common/msg.h"
#include "utils/common/netio.h"
#include "libknot/util/descriptor.h"
#include "common/errcode.h"
#include "libknot/packet/packet.h"
#include "libknot/util/descriptor.h"
#define DEFAULT_RETRIES_NSUPDATE 3
#define DEFAULT_TIMEOUT_NSUPDATE 1
......@@ -59,97 +59,72 @@ static int parser_set_default(scanner_t *s, const char *fmt, ...)
return KNOT_EOK;
}
static int nsupdate_params_init(params_t *params)
static int nsupdate_init(nsupdate_params_t *params)
{
memset(params, 0, sizeof(*params));
/* Specific data ptr. */
params->d = malloc(sizeof(nsupdate_params_t));
if (!params->d) return KNOT_ENOMEM;
memset(params->d, 0, sizeof(nsupdate_params_t));
nsupdate_params_t *npar = NSUP_PARAM(params);
/* Lists */
init_list(&params->servers);
init_list(&npar->qfiles);
/* Initialize list. */
init_list(&params->qfiles);
/* Default values. */
params->class_num = KNOT_CLASS_IN;
params->operation = OPERATION_UPDATE;
/* Default settings. */
params->format = FORMAT_NSUPDATE;
params->ip = IP_ALL;
params->protocol = PROTO_ALL;
params->port = strdup(DEFAULT_DNS_PORT);
params->udp_size = DEFAULT_UDP_SIZE;
params->retries = DEFAULT_RETRIES_NSUPDATE;
params->wait = DEFAULT_TIMEOUT_NSUPDATE;
params->format = FORMAT_NSUPDATE;
params->class_num = KNOT_CLASS_IN;
params->type_num = KNOT_RRTYPE_SOA;
/* Create default server. */
if (params_parse_server(DEFAULT_IPV4_NAME, &params->servers,
params->port)
!= KNOT_EOK) {
return KNOT_EINVAL;
}
params->server = server_create(DEFAULT_IPV4_NAME, DEFAULT_DNS_PORT);
if (!params->server) return KNOT_ENOMEM;
/* Initialize RR parser. */
npar->rrp = scanner_create(".");
if (!npar->rrp) return KNOT_ENOMEM;
npar->rrp->process_record = parse_rr;
npar->rrp->process_error = parse_err;
npar->rrp->default_class = params->class_num;
nsupdate_params_set_ttl(params, 0);
nsupdate_params_set_origin(params, ".");
params->rrp = scanner_create(".");
if (!params->rrp) return KNOT_ENOMEM;
params->rrp->process_record = parse_rr;
params->rrp->process_error = parse_err;
params->rrp->default_class = params->class_num;
nsupdate_set_ttl(params, 0);
nsupdate_set_origin(params, ".");
return KNOT_EOK;
}
void nsupdate_params_clean(params_t *params)
void nsupdate_clean(nsupdate_params_t *params)
{
strnode_t *n = NULL, *nxt = NULL;
if (params == NULL) {
return;
}
/* Free specific structure. */
nsupdate_params_t* npar = NSUP_PARAM(params);
if (npar) {
free(npar->zone);
if (npar->rrp) {
scanner_free(npar->rrp);
}
/* Free qfiles. */
strnode_t *n = NULL, *nxt = NULL;
WALK_LIST_DELSAFE(n, nxt, npar->qfiles) {
free(n);
}
free(npar);
params->d = NULL;
}
server_free(params->server);
server_free(params->srcif);
free(params->zone);
scanner_free(params->rrp);
knot_packet_free(&params->pkt);
knot_packet_free(&params->resp);
/* Free server list. */
server_t *n = NULL, *nxt = NULL;
WALK_LIST_DELSAFE(n, nxt, params->servers) {
server_free(n);
/* Free qfiles. */
WALK_LIST_DELSAFE(n, nxt, params->qfiles) {
free(n);
}
free(params->port);
/* Free TSIG key. */
knot_dname_free(&params->key.name);
free(params->key.secret);
/* Clean up the structure. */
memset(params, 0, sizeof(params_t));
memset(params, 0, sizeof(*params));
}
static void nsupdate_params_help(int argc, char *argv[])
static void nsupdate_help(int argc, char *argv[])
{
printf("Usage: %s [-d] [-v] [-y [hmac:]name:key] [-p port] "
"[-t timeout] [-r retries] [filename]\n",
argv[0]);
}
int nsupdate_params_parse(params_t *params, int argc, char *argv[])
int nsupdate_parse(nsupdate_params_t *params, int argc, char *argv[])
{
int opt = 0;
int ret = KNOT_EOK;
......@@ -158,29 +133,26 @@ int nsupdate_params_parse(params_t *params, int argc, char *argv[])
return KNOT_EINVAL;
}
ret = nsupdate_params_init(params);
ret = nsupdate_init(params);
if (ret != KNOT_EOK) {
return ret;
}
/* Fetch default server. */
server_t *srv = TAIL(params->servers);
/* Command line options processing. */
while ((opt = getopt(argc, argv, "dDvp:t:r:y:k:")) != -1) {
switch (opt) {
case 'd':
case 'D': /* Extra debugging. */
msg_enable_debug(1);
params_flag_verbose(params);
params->format = FORMAT_VERBOSE;
break;
case 'v':
params_flag_tcp(params);
params->protocol = PROTO_TCP;
break;
case 'p':
free(srv->service);
srv->service = strdup(optarg);
if (!srv->service) {
free(params->server->service);
params->server->service = strdup(optarg);
if (!params->server->service) {
ERR("failed to set default port '%s'\n", optarg);
return KNOT_ENOMEM;
}
......@@ -202,29 +174,27 @@ int nsupdate_params_parse(params_t *params, int argc, char *argv[])
if (ret != KNOT_EOK) return ret;
break;
default:
nsupdate_params_help(argc, argv);
nsupdate_help(argc, argv);
return KNOT_ENOTSUP;
}
}
/* Process non-option parameters. */
nsupdate_params_t* npar = NSUP_PARAM(params);
for (; optind < argc; ++optind) {
strnode_t *n = malloc(sizeof(strnode_t));
if (!n) { /* Params will be cleaned on exit. */
return KNOT_ENOMEM;
}
n->str = argv[optind];
add_tail(&npar->qfiles, &n->n);
add_tail(&params->qfiles, &n->n);
}
return ret;
}
int nsupdate_params_set_ttl(params_t *params, uint32_t ttl)
int nsupdate_set_ttl(nsupdate_params_t *params, const uint32_t ttl)
{
nsupdate_params_t* npar = NSUP_PARAM(params);
int ret = parser_set_default(npar->rrp, "$TTL %u\n", ttl);
int ret = parser_set_default(params->rrp, "$TTL %u\n", ttl);
if (ret == KNOT_EOK) {
params->ttl = ttl;
} else {
......@@ -233,13 +203,12 @@ int nsupdate_params_set_ttl(params_t *params, uint32_t ttl)
return ret;
}
int nsupdate_params_set_origin(params_t *params, const char *origin)
int nsupdate_set_origin(nsupdate_params_t *params, const char *origin)
{
nsupdate_params_t* npar = NSUP_PARAM(params);
int ret = parser_set_default(npar->rrp, "$ORIGIN %s\n", origin);
int ret = parser_set_default(params->rrp, "$ORIGIN %s\n", origin);
if (ret == KNOT_EOK) {
if (npar->zone) free(npar->zone);
npar->zone = strdup(origin);
if (params->zone) free(params->zone);
params->zone = strdup(origin);
} else {
ERR("failed to set default origin, %s\n", knot_strerror(ret));
}
......
......@@ -30,10 +30,10 @@
#include <stdint.h>
#include "common/lists.h" // list
#include "utils/common/params.h" // protocol_t
#include "zscanner/scanner.h" // scanner_t
#include "libknot/packet/packet.h" // knot_packet_t
#include "utils/common/netio.h"
#include "zscanner/scanner.h" // scanner_t
#include "utils/common/netio.h" // server_t
#include "utils/common/params.h" // protocol_t
/*! Parser init string. */
#define PARSER_INIT_STR "$ORIGIN %s\n$TTL %u\n"
......@@ -42,6 +42,26 @@
typedef struct {
/*!< List of files with query data. */
list qfiles;
/*!< List of nameservers to query to. */
server_t *server;
/*!< Output format. */
format_t format;
/*!< Local interface (optional). */
server_t *srcif;
/*!< Operation mode. */
ip_t ip;
/*!< Type (TCP, UDP) protocol to use. */
protocol_t protocol;
/*!< Default class number. */
uint16_t class_num;
/*!< Default type number. */
uint16_t type_num;
/*!< Default TTL. */
uint32_t ttl;
/*!< Number of UDP retries. */
uint32_t retries;
/*!< Wait for network response in seconds (-1 means forever). */
int32_t wait;
/*!< Current zone. */
char *zone;
/*!< RR parser. */
......@@ -52,15 +72,14 @@ typedef struct {
knot_packet_t *resp;
/*!< Buffer for response. */
uint8_t rwire[MAX_PACKET_SIZE];
/*!< Local interface (optional). */
server_t *srcif;
/*!< TSIG key used. */
knot_key_t key;
} nsupdate_params_t;
#define NSUP_PARAM(p) ((nsupdate_params_t*)p->d)
int nsupdate_params_parse(params_t *params, int argc, char *argv[]);
int nsupdate_params_set_ttl(params_t *params, uint32_t ttl);
int nsupdate_params_set_origin(params_t *params, const char *origin);
void nsupdate_params_clean(params_t *params);
int nsupdate_parse(nsupdate_params_t *params, int argc, char *argv[]);
int nsupdate_set_ttl(nsupdate_params_t *params, const uint32_t ttl);
int nsupdate_set_origin(nsupdate_params_t *params, const char *origin);
void nsupdate_clean(nsupdate_params_t *params);
#endif // _NSUPDATE__NSUPDATE_PARAMS_H_
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment