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

Integrate params_t to dig_params_t

refs #2137
parent 8a144ca6
......@@ -27,19 +27,16 @@
#include "libknot/util/wire.h" // knot_wire_set_rd
#include "libknot/packet/query.h" // knot_query_init
#include "utils/common/msg.h" // WARN
#include "utils/common/params.h" // params_t
#include "utils/common/netio.h" // get_socktype
#include "utils/common/exec.h" // print_packet
static knot_packet_t* create_query_packet(const params_t *params,
const query_t *query,
uint8_t **data,
size_t *data_len)
static knot_packet_t* create_query_packet(const dig_params_t *params,
const query_t *query,
uint8_t **data,
size_t *data_len)
{
knot_question_t q;
dig_params_t *ext_params = DIG_PARAM(params);
// Set packet buffer size.
int max_size = MAX_PACKET_SIZE;
if (get_socktype(params->protocol, query->qtype) != SOCK_STREAM) {
......@@ -56,7 +53,7 @@ static knot_packet_t* create_query_packet(const params_t *params,
}
// Set recursion bit to wireformat.
if (ext_params->options.rd_flag == true) {
if (params->options.rd_flag == true) {
knot_wire_set_rd(packet->wireformat);
} else {
knot_wire_flags_clear_rd(packet->wireformat);
......@@ -159,11 +156,11 @@ static bool check_id(const knot_packet_t *query, const knot_packet_t *reply)
return true;
}
static int check_rcode(const params_t *params, const knot_packet_t *reply)
static int check_rcode(const bool servfail_stop, const knot_packet_t *reply)
{
uint8_t rcode = knot_wire_get_rcode(reply->wireformat);
if (rcode == KNOT_RCODE_SERVFAIL && params->servfail_stop == true) {
if (rcode == KNOT_RCODE_SERVFAIL && servfail_stop == true) {
return -1;
}
......@@ -225,7 +222,7 @@ static bool last_serial_check(const uint32_t serial, const knot_packet_t *reply)
}
}
void process_query(const params_t *params, const query_t *query)
void process_query(const dig_params_t *params, const query_t *query)
{
float elapsed;
bool id_ok, stop;
......@@ -308,7 +305,7 @@ void process_query(const params_t *params, const query_t *query)
}
// Check rcode.
rcode = check_rcode(params, in_packet);
rcode = check_rcode(params->servfail_stop, in_packet);
// Servfail + stop if servfail -> stop processing.
if (rcode == -1) {
......@@ -404,7 +401,7 @@ void process_query(const params_t *params, const query_t *query)
id_ok = check_id(out_packet, in_packet);
// Check rcode.
rcode = check_rcode(params, in_packet);
rcode = check_rcode(params->servfail_stop, in_packet);
if (rcode != KNOT_RCODE_NOERROR) {
stop = true;
......@@ -445,7 +442,7 @@ void process_query(const params_t *params, const query_t *query)
knot_packet_free(&out_packet);
}
int dig_exec(const params_t *params)
int dig_exec(const dig_params_t *params)
{
node *query = NULL;
......@@ -453,12 +450,10 @@ int dig_exec(const params_t *params)
return KNOT_EINVAL;
}
dig_params_t *ext_params = DIG_PARAM(params);
switch (params->operation) {
case OPERATION_QUERY:
// Loop over query list.
WALK_LIST(query, ext_params->queries) {
WALK_LIST(query, params->queries) {
process_query(params, (query_t *)query);
}
......
......@@ -30,9 +30,9 @@
#include "utils/common/params.h" // params_t
#include "utils/dig/dig_params.h" // query_t
int dig_exec(const params_t *params);
int dig_exec(const dig_params_t *params);
void process_query(const params_t *params, const query_t *query);
void process_query(const dig_params_t *params, const query_t *query);
#endif // _DIG__DIG_EXEC_H_
......
......@@ -24,7 +24,7 @@ int main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
params_t params;
dig_params_t params;
switch (dig_parse(&params, argc, argv)) {
case KNOT_EOK:
if (dig_exec(&params) != KNOT_EOK) {
......
......@@ -66,44 +66,35 @@ void query_free(query_t *query)
free(query);
}
static int dig_init(params_t *params)
static int dig_init(dig_params_t *params)
{
memset(params, 0, sizeof(*params));
// Create dig specific data structure.
params->d = calloc(1, sizeof(dig_params_t));
if (!params->d) {
return KNOT_ENOMEM;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Initialize blank server list.
// Initialize servers and queries lists.
init_list(&params->servers);
init_list(&params->queries);
// Default values.
// Default settings.
params->operation = OPERATION_QUERY;
params->format = FORMAT_VERBOSE;
params->ip = IP_ALL;
params->protocol = PROTO_ALL;
params->port = strdup(DEFAULT_DNS_PORT);
params->udp_size = DEFAULT_UDP_SIZE;
params->class_num = -1;
params->type_num = -1;
params->xfr_serial = 0;
params->retries = DEFAULT_RETRIES_DIG;
params->wait = DEFAULT_TIMEOUT_DIG;
params->servfail_stop = false;
params->format = FORMAT_VERBOSE;
// Initialize list of queries.
init_list(&ext_params->queries);
params->class_num = -1;
params->type_num = -1;
params->xfr_serial = 0;
// Extended params.
ext_params->options.rd_flag = true;
// Default options.
params->options.rd_flag = true;
return KNOT_EOK;
}
void dig_clean(params_t *params)
void dig_clean(dig_params_t *params)
{
node *n = NULL, *nxt = NULL;
......@@ -111,28 +102,23 @@ void dig_clean(params_t *params)
return;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Clean up server list.
// Clean up servers.
WALK_LIST_DELSAFE(n, nxt, params->servers) {
server_free((server_t *)n);
}
// Clean up query list.
WALK_LIST_DELSAFE(n, nxt, ext_params->queries) {
// Clean up queries.
WALK_LIST_DELSAFE(n, nxt, params->queries) {
query_free((query_t *)n);
}
free(params->port);
// Destroy dig specific structure.
free(ext_params);
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
static int parse_name(const char *value, params_t *params)
static int parse_name(const char *value, dig_params_t *params)
{
query_t *query = NULL;
......@@ -149,12 +135,12 @@ static int parse_name(const char *value, params_t *params)
}
// Add new query to the queries.
add_tail(&DIG_PARAM(params)->queries, (node *)query);
add_tail(&params->queries, (node *)query);
return KNOT_EOK;
}
static int parse_reverse(const char *value, params_t *params)
static int parse_reverse(const char *value, dig_params_t *params)
{
query_t *query = NULL;
......@@ -176,30 +162,28 @@ static int parse_reverse(const char *value, params_t *params)
}
// Add new query to the queries.
add_tail(&DIG_PARAM(params)->queries, (node *)query);
add_tail(&params->queries, (node *)query);
return KNOT_EOK;
}
static void complete_queries(params_t *params)
static void complete_queries(dig_params_t *params)
{
query_t *query = NULL;
node *n = NULL;
dig_params_t *ext_params = DIG_PARAM(params);
// If there is no query, add default query: NS to ".".
if (list_size(&ext_params->queries) == 0) {
if (list_size(&params->queries) == 0) {
query = query_create(".", KNOT_RRTYPE_NS, KNOT_CLASS_IN);
if (query == NULL) {
WARN("can't create query . NS IN\n");
return;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
query = NULL;
}
WALK_LIST(n, ext_params->queries) {
WALK_LIST(n, params->queries) {
query_t *q = (query_t *)n;
if (q->qclass < 0) {
......@@ -220,7 +204,7 @@ static void complete_queries(params_t *params)
}
}
static int parse_class(const char *value, params_t *params)
static int parse_class(const char *value, dig_params_t *params)
{
uint16_t rclass;
......@@ -228,14 +212,12 @@ static int parse_class(const char *value, params_t *params)
return KNOT_EINVAL;
}
list *queries = &DIG_PARAM(params)->queries;
// Change default.
if (list_size(queries) == 0) {
if (list_size(&params->queries) == 0) {
params->class_num = rclass;
// Change current.
} else {
query_t *query = TAIL(*queries);
query_t *query = TAIL(params->queries);
query->qclass = rclass;
}
......@@ -243,7 +225,7 @@ static int parse_class(const char *value, params_t *params)
return KNOT_EOK;
}
static int parse_type(const char *value, params_t *params)
static int parse_type(const char *value, dig_params_t *params)
{
uint16_t rtype;
uint32_t serial;
......@@ -252,15 +234,13 @@ static int parse_type(const char *value, params_t *params)
return KNOT_EINVAL;
}
list *queries = &DIG_PARAM(params)->queries;
// Change default.
if (list_size(queries) == 0) {
if (list_size(&params->queries) == 0) {
params->type_num = rtype;
params->xfr_serial = serial;
// Change current.
} else {
query_t *query = TAIL(*queries);
query_t *query = TAIL(params->queries);
query->qtype = rtype;
query->xfr_serial = serial;
......@@ -279,16 +259,7 @@ static void dig_help(const bool verbose)
}
}
void dig_params_flag_norecurse(params_t *params)
{
if (params == NULL) {
return;
}
DIG_PARAM(params)->options.rd_flag = false;
}
static int parse_server(const char *value, params_t *params)
static int parse_server(const char *value, dig_params_t *params)
{
int ret = params_parse_server(value, &params->servers, params->port);
......@@ -300,7 +271,7 @@ static int parse_server(const char *value, params_t *params)
}
static int parse_opt1(const char *opt, const char *value,
params_t *params, int *index)
dig_params_t *params, int *index)
{
const char *val = value;
size_t len = strlen(opt);
......@@ -318,14 +289,14 @@ static int parse_opt1(const char *opt, const char *value,
return KNOT_ENOTSUP;
}
params_flag_ipv4(params);
params->ip = IP_4;
break;
case '6':
if (len > 1) {
return KNOT_ENOTSUP;
}
params_flag_ipv6(params);
params->ip = IP_6;
break;
case 'c':
if (val == NULL) {
......@@ -403,14 +374,14 @@ static int parse_opt1(const char *opt, const char *value,
return KNOT_EOK;
}
static int parse_opt2(const char *value, params_t *params)
static int parse_opt2(const char *value, dig_params_t *params)
{
ERR("invalid option: %s\n", value);
return KNOT_EOK;
}
static int parse_token(const char *value, params_t *params)
static int parse_token(const char *value, dig_params_t *params)
{
if (parse_type(value, params) == KNOT_EOK) {
......@@ -426,7 +397,7 @@ static int parse_token(const char *value, params_t *params)
return KNOT_ERROR;
}
int dig_parse(params_t *params, int argc, char *argv[])
int dig_parse(dig_params_t *params, int argc, char *argv[])
{
if (params == NULL || argv == NULL) {
return KNOT_EINVAL;
......@@ -477,4 +448,3 @@ int dig_parse(params_t *params, int argc, char *argv[])
return KNOT_EOK;
}
......@@ -31,11 +31,6 @@
#include "utils/common/params.h" // params_t
typedef struct {
/*!< Recursion desired flag. */
bool rd_flag;
} options_t;
/*! \brief Structure containing basic parameters for DNS query. */
typedef struct {
/*!< List node (for list container). */
......@@ -50,24 +45,52 @@ typedef struct {
uint32_t xfr_serial;
} query_t;
/*! \brief dig-specific params data. */
typedef struct {
/*!< Recursion desired flag. */
bool rd_flag;
} options_t;
/*! \brief Settings for dig. */
typedef struct {
/*!< List of nameservers to query to. */
list servers;
/*!< List of DNS queries to process. */
list queries;
/*!< Operation mode. */
operation_t operation;
/*!< Output format. */
format_t format;
/*!< Version of ip protocol to use. */
ip_version_t ip;
/*!< Type (TCP, UDP) protocol to use. */
protocol_t protocol;
/*!< Default port/service to connect to. */
char *port;
/*!< 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;
/*!< Default class number (16unsigned + -1 uninitialized). */
int32_t class_num;
/*!< Default type number (16unsigned + -1 uninitialized). */
int32_t type_num;
/*!< Default SOA serial for XFR. */
uint32_t xfr_serial;
/*!< Global options. */
options_t options;
} dig_params_t;
#define DIG_PARAM(p) ((dig_params_t*)p->d)
query_t* query_create(const char *qname,
const int32_t qtype,
const int32_t qclass);
void query_free(query_t *query);
int dig_parse(params_t *params, int argc, char *argv[]);
void dig_clean(params_t *params);
void dig_params_flag_norecurse(params_t *params);
int dig_parse(dig_params_t *params, int argc, char *argv[]);
void dig_clean(dig_params_t *params);
#endif // _DIG__DIG_PARAMS_H_
......
......@@ -23,7 +23,7 @@
#include "utils/dig/dig_params.h" // dig_params_t
#include "utils/dig/dig_exec.h" // process_query
int host_exec(const params_t *params)
int host_exec(const dig_params_t *params)
{
node *query = NULL;
......@@ -31,12 +31,10 @@ int host_exec(const params_t *params)
return KNOT_EINVAL;
}
dig_params_t *ext_params = DIG_PARAM(params);
switch (params->operation) {
case OPERATION_QUERY:
// Loop over query list.
WALK_LIST(query, ext_params->queries) {
WALK_LIST(query, params->queries) {
process_query(params, (query_t *)query);
}
......
......@@ -27,9 +27,9 @@
#ifndef _HOST__HOST_EXEC_H_
#define _HOST__HOST_EXEC_H_
#include "utils/common/params.h" // params_t
#include "utils/dig/dig_params.h" // dig_params_t
int host_exec(const params_t *params);
int host_exec(const dig_params_t *params);
#endif // _HOST__HOST_EXEC_H_
......
......@@ -17,15 +17,15 @@
#include <stdlib.h> // EXIT_FAILURE
#include "common/errcode.h" // KNOT_EOK
#include "utils/host/host_params.h" // params_t
#include "utils/host/host_params.h" // host_params_parse
#include "utils/host/host_exec.h" // host_exec
int main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
params_t params;
if (host_params_parse(&params, argc, argv) == KNOT_EOK) {
dig_params_t params;
if (host_parse(&params, argc, argv) == KNOT_EOK) {
if (host_exec(&params) != KNOT_EOK) {
ret = EXIT_FAILURE;
}
......@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
ret = EXIT_FAILURE;
}
host_params_clean(&params);
host_clean(&params);
return ret;
}
......@@ -31,44 +31,35 @@
#define DEFAULT_RETRIES_HOST 1
#define DEFAULT_TIMEOUT_HOST 1
static int host_params_init(params_t *params)
static int host_init(dig_params_t *params)
{
memset(params, 0, sizeof(*params));
// Create dig specific data structure.
params->d = calloc(1, sizeof(dig_params_t));
if (!params->d) {
return KNOT_ENOMEM;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Initialize blank server list.
// Initialize servers and queries lists.
init_list(&params->servers);
init_list(&params->queries);
// Default values.
// Default settings.
params->operation = OPERATION_QUERY;
params->format = FORMAT_HOST;
params->ip = IP_ALL;
params->protocol = PROTO_ALL;
params->port = strdup(DEFAULT_DNS_PORT);
params->udp_size = DEFAULT_UDP_SIZE;
params->class_num = KNOT_CLASS_IN;
params->type_num = -1;
params->xfr_serial = 0;
params->retries = DEFAULT_RETRIES_HOST;
params->wait = DEFAULT_TIMEOUT_HOST;
params->servfail_stop = false;
params->format = FORMAT_HOST;
// Initialize list of queries.
init_list(&ext_params->queries);
params->class_num = KNOT_CLASS_IN;
params->type_num = -1;
params->xfr_serial = 0;
// Extended params.
ext_params->options.rd_flag = true;
// Default options.
params->options.rd_flag = true;
return KNOT_EOK;
}
void host_params_clean(params_t *params)
void host_clean(dig_params_t *params)
{
node *n = NULL, *nxt = NULL;
......@@ -76,52 +67,28 @@ void host_params_clean(params_t *params)
return;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Clean up server list.
// Clean up servers.
WALK_LIST_DELSAFE(n, nxt, params->servers) {
server_free((server_t *)n);
}
// Clean up query list.
WALK_LIST_DELSAFE(n, nxt, ext_params->queries) {
// Clean up queries.
WALK_LIST_DELSAFE(n, nxt, params->queries) {
query_free((query_t *)n);
}
free(params->port);
// Destroy dig specific structure.
free(ext_params);
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
static void host_params_flag_all(params_t *params)
{
params->type_num = KNOT_RRTYPE_ANY;
params->format = FORMAT_VERBOSE;
}
static void host_params_flag_soa(params_t *params)
{
params->type_num = KNOT_RRTYPE_SOA;
params->operation = OPERATION_LIST_SOA;
}
static void host_params_flag_axfr(params_t *params)
{
params->type_num = KNOT_RRTYPE_AXFR;
}
static int host_params_parse_name(const char *name, params_t *params)
static int host_parse_name(const char *name, dig_params_t *params)
{
char *reverse = get_reverse_name(name);
char *fqd_name = NULL;
query_t *query;
dig_params_t *ext_params = DIG_PARAM(params);
// If name is not FQDN, append trailing dot.
fqd_name = get_fqd_name(name);
......@@ -143,7 +110,7 @@ static int host_params_parse_name(const char *name, params_t *params)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
} else {
// Add query for name and specified type.
query = query_create(fqd_name, params->type_num,
......@@ -157,7 +124,7 @@ static int host_params_parse_name(const char *name, params_t *params)
if (params->type_num == KNOT_RRTYPE_IXFR) {
query->xfr_serial = params->xfr_serial;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
}
// RR type is unknown, use defaults.
} else {
......@@ -169,7 +136,7 @@ static int host_params_parse_name(const char *name, params_t *params)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
// Add query for name and type AAAA.
query = query_create(fqd_name, KNOT_RRTYPE_AAAA,
......@@ -178,7 +145,7 @@ static int host_params_parse_name(const char *name, params_t *params)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
// Add query for name and type MX.
query = query_create(fqd_name, KNOT_RRTYPE_MX,
......@@ -187,7 +154,7 @@ static int host_params_parse_name(const char *name, params_t *params)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
} else {
// Add reverse query for address.
query = query_create(reverse, KNOT_RRTYPE_PTR,
......@@ -197,7 +164,7 @@ static int host_params_parse_name(const char *name, params_t *params)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&ext_params->queries, (node *)query);
add_tail(&params->queries, (node *)query);
}
}
......@@ -207,14 +174,14 @@ static int host_params_parse_name(const char *name, params_t *params)
return KNOT_EOK;
}
static void host_params_help(int argc, char *argv[])
static void host_help(int argc, char *argv[])
{
printf("Usage: %s [-aCdlrsTvw] [-4] [-6] [-c class] [-R retries]\n"
" %*c [-t type] [-W time] name [server]\n",
argv[0], (int)strlen(argv[0]), ' ');
}
int host_params_parse(params_t *params, int argc, char *argv[])
int host_parse(dig_params_t *params, int argc, char *argv[])
{
int opt = 0;
......@@ -222,7 +189,7 @@ int host_params_parse(params_t *params, int argc, char *argv[])
return KNOT_EINVAL;
}
if (host_params_init(params) != KNOT_EOK) {
if (host_init(params) != KNOT_EOK) {
return KNOT_ERROR;
}
......@@ -233,35 +200,37 @@ int host_params_parse(params_t *params, int argc, char *argv[])
while ((opt = getopt(argc, argv, "46aCdlrsTvwc:R:t:W:")) != -1) {
switch (opt) {
case '4':
params_flag_ipv4(params);
params->ip = IP_4;
break;
case '6':
params_flag_ipv6(params);
params->ip = IP_6;
break;
case 'a':
host_params_flag_all(params);
params->type_num = KNOT_RRTYPE_ANY;
params->format = FORMAT_VERBOSE;
break;
case 'C':
host_params_flag_soa(params);
params->type_num = KNOT_RRTYPE_SOA;
params->operation = OPERATION_LIST_SOA;
break;
case 'd':
case 'v': // Fall through.
params_flag_verbose(params);
params->format = FORMAT_VERBOSE;
break;
case 'l':
host_params_flag_axfr(params);
params->type_num = KNOT_RRTYPE_AXFR;
break;
case 'r':
dig_params_flag_norecurse(params);
params->options.rd_flag = false;
break;
case 's':
params_flag_servfail(params);
params->servfail_stop = true;
break;
case 'T':
params_flag_tcp(params);
params->protocol = PROTO_TCP;
break;
case 'w':
params_flag_nowait(params);
params->wait = -1;
break;