Commit 3b9a1db4 authored by Daniel Salzman's avatar Daniel Salzman

Dig/host code reorganization

refs #2137
parent 59d405a4
......@@ -63,8 +63,12 @@ khost_SOURCES = \
utils/common/rr-serialize.h \
utils/common/rr-serialize.c \
utils/host/host_main.c \
utils/dig/dig_params.c \
utils/dig/dig_params.h \
utils/host/host_params.c \
utils/host/host_params.h \
utils/dig/dig_exec.c \
utils/dig/dig_exec.h \
utils/host/host_exec.c \
utils/host/host_exec.h
......
This diff is collapsed.
......@@ -28,12 +28,25 @@
#define _UTILS__EXEC_H_
#include "libknot/packet/packet.h" // knot_packet_t
#include "utils/common/params.h" // params_t
#include "utils/common/params.h" // format_t
knot_lookup_table_t rcodes[];
extern knot_lookup_table_t opcodes[];
extern knot_lookup_table_t rcodes[];
extern knot_lookup_table_t rtypes[];
knot_packet_t* create_empty_packet(knot_packet_prealloc_type_t t, int max_size);
void process_query(const params_t *params, const query_t *query);
void print_header_xfr(const format_t format, const knot_rr_type_t type);
void print_data_xfr(const format_t format,
const knot_packet_t *packet);
void print_footer_xfr(const format_t format,
const size_t total_len,
const int sockfd,
const float elapsed,
const size_t msg_count);
void print_packet(const format_t format,
const knot_packet_t *packet,
const size_t wire_len,
......
......@@ -25,7 +25,7 @@
#include "common/mempattern.h" // strcdup
#include "libknot/util/descriptor.h" // KNOT_RRTYPE
#include "utils/common/msg.h" // WARN
#include "libknot/dname.h"
#include "libknot/dname.h" // knot_dname_t
#define IPV4_REVERSE_DOMAIN "in-addr.arpa."
#define IPV6_REVERSE_DOMAIN "ip6.arpa."
......@@ -43,39 +43,6 @@ static knot_dname_t* create_fqdn_from_str(const char *str, size_t len)
return d;
}
query_t* query_create(const char *name, const uint16_t type)
{
// Create output structure.
query_t *query = calloc(1, sizeof(query_t));
// Check output.
if (query == NULL) {
return NULL;
}
// Fill output.
query->name = strdup(name);
query->type = type;
query->xfr_serial = -1;
return query;
}
void query_set_serial(query_t *query, const uint32_t serial)
{
query->xfr_serial = serial;
}
void query_free(query_t *query)
{
if (query == NULL) {
return;
}
free(query->name);
free(query);
}
int parse_class(const char *rclass, uint16_t *class_num)
{
*class_num = knot_rrclass_from_string(rclass);
......
......@@ -42,18 +42,6 @@
#define SEP_CHARS "\n\t "
/*! \brief Structure containing basic parameters for DNS query. */
typedef struct {
/*!< List node (for list container). */
node n;
/*!< Name to query on. */
char *name;
/*!< Type number to query on. */
uint16_t type;
/*!< SOA serial for XFR. */
int64_t xfr_serial;
} query_t;
typedef enum {
IP_ALL,
IP_4,
......@@ -92,9 +80,6 @@ typedef enum {
typedef struct {
/*!< List of nameservers to query to. */
list servers;
/*!< List of DNS queries to process. */
list queries;
/*!< Operation mode. */
operation_t operation;
/*!< Version of ip protocol to use. */
......@@ -127,13 +112,6 @@ typedef struct {
void* d;
} params_t;
query_t* query_create(const char *name, const uint16_t type);
void query_free(query_t *query);
void query_set_serial(query_t *query, const uint32_t serial);
int parse_class(const char *rclass, uint16_t *class_num);
int parse_type(const char *rtype, int32_t *type_num, int64_t *ixfr_serial);
......
This diff is collapsed.
......@@ -28,9 +28,12 @@
#define _DIG__DIG_EXEC_H_
#include "utils/common/params.h" // params_t
#include "utils/dig/dig_params.h" // query_t
int dig_exec(const params_t *params);
void process_query(const params_t *params, const query_t *query);
#endif // _DIG__DIG_EXEC_H_
/*! @} */
......@@ -27,9 +27,41 @@
#include "utils/common/msg.h" // WARN
#include "utils/common/params.h" // parse_class
#include "utils/common/resolv.h" // get_nameservers
#include "utils/common/netio.h"
static void dig_params_init(params_t *params)
query_t* query_create(const char *name, const uint16_t type)
{
// Create output structure.
query_t *query = calloc(1, sizeof(query_t));
// Check output.
if (query == NULL) {
return NULL;
}
// Fill output.
query->name = strdup(name);
query->type = type;
query->xfr_serial = -1;
return query;
}
void query_set_serial(query_t *query, const uint32_t serial)
{
query->xfr_serial = serial;
}
void query_free(query_t *query)
{
if (query == NULL) {
return;
}
free(query->name);
free(query);
}
static int dig_params_init(params_t *params)
{
memset(params, 0, sizeof(*params));
......@@ -38,8 +70,15 @@ static void dig_params_init(params_t *params)
WARN("can't read any default nameservers\n");
}
// 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 list of queries.
init_list(&params->queries);
init_list(&ext_params->queries);
// Default values.
params->operation = OPERATION_QUERY;
......@@ -54,6 +93,8 @@ static void dig_params_init(params_t *params)
params->wait = DEFAULT_WAIT_INTERVAL;
params->servfail_stop = false;
params->format = FORMAT_VERBOSE;
return KNOT_EOK;
}
void dig_params_clean(params_t *params)
......@@ -64,16 +105,21 @@ void dig_params_clean(params_t *params)
return;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Clean up server list.
WALK_LIST_DELSAFE(n, nxt, params->servers) {
server_free((server_t *)n);
}
// Clean up query list.
WALK_LIST_DELSAFE(n, nxt, params->queries) {
WALK_LIST_DELSAFE(n, nxt, ext_params->queries) {
query_free((query_t *)n);
}
// Destroy dig specific structure.
free(ext_params);
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
......@@ -126,6 +172,8 @@ static int dig_params_parse_name(params_t *params, const char *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);
......@@ -146,7 +194,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
} else {
// Add query for name and specified type.
query = query_create(fqd_name, params->type_num);
......@@ -159,7 +207,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
if (params->type_num == KNOT_RRTYPE_IXFR) {
query_set_serial(query, params->xfr_serial);
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
}
// RR type is unknown, use defaults.
} else {
......@@ -170,7 +218,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
// Add query for name and type AAAA.
query = query_create(fqd_name, KNOT_RRTYPE_AAAA);
......@@ -178,7 +226,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
// Add query for name and type MX.
query = query_create(fqd_name, KNOT_RRTYPE_MX);
......@@ -186,7 +234,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
} else {
// Add reverse query for address.
query = query_create(reverse, KNOT_RRTYPE_PTR);
......@@ -195,7 +243,7 @@ static int dig_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
}
}
......@@ -242,7 +290,9 @@ int dig_params_parse(params_t *params, int argc, char *argv[])
return KNOT_EINVAL;
}
dig_params_init(params);
if (dig_params_init(params) != KNOT_EOK) {
return KNOT_ERROR;
}
// Command line options processing.
while ((opt = getopt(argc, argv, "46aCdlrsTvwc:R:t:W:")) != -1) {
......
......@@ -18,7 +18,7 @@
*
* \author Daniel Salzman <daniel.salzman@nic.cz>
*
* \brief Dig command line parameters.
* \brief dig command line parameters.
*
* \addtogroup knot_utils
* @{
......@@ -29,13 +29,31 @@
#include "utils/common/params.h" // params_t
/*! \brief Structure containing basic parameters for DNS query. */
typedef struct {
/*!< List node (for list container). */
node n;
/*!< Name to query on. */
char *name;
/*!< Type number to query on. */
uint16_t type;
/*!< SOA serial for XFR. */
int64_t xfr_serial;
} query_t;
/*! \brief dig-specific params data. */
typedef struct {
/*!< List of DNS queries to process. */
list queries;
} dig_params_t;
#define DIG_PARAM(p) ((dig_params_t*)p->d)
int host_params_parse(params_t *params, int argc, char *argv[]);
void host_params_clean(params_t *params);
query_t* query_create(const char *name, const uint16_t type);
void query_free(query_t *query);
void query_set_serial(query_t *query, const uint32_t serial);
int dig_params_parse(params_t *params, int argc, char *argv[]);
void dig_params_clean(params_t *params);
#endif // _DIG__DIG_PARAMS_H_
......
......@@ -20,8 +20,8 @@
#include "common/errcode.h" // KNOT_EOK
#include "utils/common/msg.h" // WARN
#include "utils/common/params.h" // params_t
#include "utils/common/exec.h" // process_query
#include "utils/dig/dig_params.h" // dig_params_t
#include "utils/dig/dig_exec.h" // process_query
int host_exec(const params_t *params)
{
......@@ -31,10 +31,12 @@ 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, params->queries) {
WALK_LIST(query, ext_params->queries) {
process_query(params, (query_t *)query);
}
......
......@@ -25,11 +25,10 @@
#include "common/errcode.h" // KNOT_EOK
#include "libknot/util/descriptor.h" // KNOT_CLASS_IN
#include "utils/common/msg.h" // WARN
#include "utils/common/params.h" // parse_class
#include "utils/dig/dig_params.h" // dig_params_t
#include "utils/common/resolv.h" // get_nameservers
#include "utils/common/netio.h"
static void host_params_init(params_t *params)
static int host_params_init(params_t *params)
{
memset(params, 0, sizeof(*params));
......@@ -38,8 +37,15 @@ static void host_params_init(params_t *params)
WARN("can't read any default nameservers\n");
}
// 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 list of queries.
init_list(&params->queries);
init_list(&ext_params->queries);
// Default values.
params->operation = OPERATION_QUERY;
......@@ -54,6 +60,8 @@ static void host_params_init(params_t *params)
params->wait = DEFAULT_WAIT_INTERVAL;
params->servfail_stop = false;
params->format = FORMAT_HOST;
return KNOT_EOK;
}
void host_params_clean(params_t *params)
......@@ -64,16 +72,21 @@ void host_params_clean(params_t *params)
return;
}
dig_params_t *ext_params = DIG_PARAM(params);
// Clean up server list.
WALK_LIST_DELSAFE(n, nxt, params->servers) {
server_free((server_t *)n);
}
// Clean up query list.
WALK_LIST_DELSAFE(n, nxt, params->queries) {
WALK_LIST_DELSAFE(n, nxt, ext_params->queries) {
query_free((query_t *)n);
}
// Destroy dig specific structure.
free(ext_params);
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
......@@ -126,6 +139,8 @@ static int host_params_parse_name(params_t *params, const char *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);
......@@ -146,7 +161,7 @@ static int host_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
} else {
// Add query for name and specified type.
query = query_create(fqd_name, params->type_num);
......@@ -159,7 +174,7 @@ static int host_params_parse_name(params_t *params, const char *name)
if (params->type_num == KNOT_RRTYPE_IXFR) {
query_set_serial(query, params->xfr_serial);
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
}
// RR type is unknown, use defaults.
} else {
......@@ -170,7 +185,7 @@ static int host_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
// Add query for name and type AAAA.
query = query_create(fqd_name, KNOT_RRTYPE_AAAA);
......@@ -178,7 +193,7 @@ static int host_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
// Add query for name and type MX.
query = query_create(fqd_name, KNOT_RRTYPE_MX);
......@@ -186,7 +201,7 @@ static int host_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
} else {
// Add reverse query for address.
query = query_create(reverse, KNOT_RRTYPE_PTR);
......@@ -195,7 +210,7 @@ static int host_params_parse_name(params_t *params, const char *name)
free(fqd_name);
return KNOT_ENOMEM;
}
add_tail(&params->queries, (node *)query);
add_tail(&ext_params->queries, (node *)query);
}
}
......@@ -242,7 +257,9 @@ int host_params_parse(params_t *params, int argc, char *argv[])
return KNOT_EINVAL;
}
host_params_init(params);
if (host_params_init(params) != KNOT_EOK) {
return KNOT_ERROR;
}
// Command line options processing.
while ((opt = getopt(argc, argv, "46aCdlrsTvwc:R:t:W:")) != -1) {
......
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