Commit 2d21a3bc authored by Marek Vavrusa's avatar Marek Vavrusa

nsupdate skeleton with some basic flags.

Moved common params parsing code to common.
parent 08753bc0
......@@ -144,3 +144,56 @@ char* get_reverse_name(const char *name)
}
}
void params_flag_tcp(params_t *params)
{
params->protocol = PROTO_TCP;
}
void params_flag_verbose(params_t *params)
{
params->verbose = true;
}
int params_parse_interval(const char *value, int32_t *dst)
{
char *end;
/* Convert string to number. */
long num = strtol(value, &end, 10);
/* Check for bad string (empty or incorrect). */
if (end == value || *end != '\0') {
ERR("bad interval value\n");
return KNOT_ERROR;
} else if (num < 1) {
num = 1;
WARN("interval is too short, using %ld seconds\n", num);
/* Reduce maximal value. Poll takes signed int in milliseconds.. */
} else if (num > INT32_MAX) {
num = INT32_MAX / 1000;
WARN("interval is too long, using %ld seconds\n", num);
}
*dst = num;
return KNOT_EOK;
}
int params_parse_num(const char *value, uint32_t *dst)
{
char *end;
// Convert string to number.
unsigned long num = strtoul(value, &end, 10);
// Check for bad string.
if (end == value || *end != '\0' || num > UINT32_MAX) {
ERR("bad numeric value\n");
return KNOT_ERROR;
}
*dst = num;
return KNOT_EOK;
}
......@@ -32,8 +32,9 @@
#include "common/lists.h" // node
#define DEFAULT_UDP_SIZE 512
#define MAX_PACKET_SIZE 65535
#define DEFAULT_PORT 53
#define DEFAULT_UDP_SIZE 512
#define MAX_PACKET_SIZE 65535
/*! \brief Structure containing basic parameters for DNS query. */
typedef struct {
......@@ -73,6 +74,8 @@ typedef struct {
list servers;
/*!< List of DNS queries to process. */
list queries;
/*!< List of files with query data. */
list qfiles;
/*!< Operation mode. */
host_mode_t mode;
......@@ -92,12 +95,19 @@ typedef struct {
uint32_t udp_size;
/*!< Number of UDP retries. */
uint32_t retries;
/*!< Number of TCP retries. */
/*!< Wait for reply in seconds (-1 means forever). */
int32_t wait;
/*!< Stop quering if servfail. */
bool servfail_stop;
/*!< Verbose mode. */
bool verbose;
/*!< Default port. */
unsigned port;
/*!< Default address. */
const char* addr;
} params_t;
query_t* create_query(const char *name, const uint16_t type);
......@@ -110,6 +120,14 @@ int parse_type(const char *rtype, int32_t *type_num, int64_t *ixfr_serial);
char* get_reverse_name(const char *name);
void params_flag_tcp(params_t *params);
void params_flag_verbose(params_t *params);
int params_parse_interval(const char *value, int32_t *dst);
int params_parse_num(const char *value, uint32_t *dst);
#endif // _UTILS__PARAMS_H_
/*! @} */
......@@ -22,8 +22,9 @@
#include "common/errcode.h" // KNOT_ENOENT
#include "common/lists.h" // list
#define RESOLV_FILE "/etc/resolv.conf"
#define SEP_CHARS "\n\t "
#define DEFAULT_DNS_PORT "53"
#define RESOLV_FILE "/etc/resolv.conf"
#define SEP_CHARS "\n\t "
server_t* create_server(const char *name, const char *service)
{
......
......@@ -31,7 +31,6 @@
#define DEFAULT_IPV4_NAME "127.0.0.1"
#define DEFAULT_IPV6_NAME "::1"
#define DEFAULT_DNS_PORT "53"
/*! \brief Structure containing nameserver information. */
typedef struct {
......
......@@ -100,11 +100,6 @@ static void host_params_flag_nonrecursive(params_t *params)
params->recursion = false;
}
static void host_params_flag_tcp(params_t *params)
{
params->protocol = PROTO_TCP;
}
static void host_params_flag_ipv4(params_t *params)
{
params->ip = IP_4;
......@@ -120,59 +115,11 @@ static void host_params_flag_servfail(params_t *params)
params->servfail_stop = true;
}
static void host_params_flag_verbose(params_t *params)
{
params->verbose = true;
}
static void host_params_flag_nowait(params_t *params)
{
params->wait = -1;
}
static int host_params_parse_retries(const char *value, uint32_t *retries)
{
char *end;
// Convert string to number.
unsigned long num = strtoul(value, &end, 10);
// Check for bad string.
if (end == value || *end != '\0' || num > UINT32_MAX) {
ERR("bad retries value\n");
return KNOT_ERROR;
}
*retries = num;
return KNOT_EOK;
}
static int host_params_parse_wait(const char *value, int32_t *wait)
{
char *end;
// Convert string to number.
long num = strtol(value, &end, 10);
// Check for bad string (empty or incorrect).
if (end == value || *end != '\0') {
ERR("bad wait value\n");
return KNOT_ERROR;
} else if (num < 1) {
num = 1;
WARN("wait interval is too short, using %ld seconds\n", num);
// Reduce maximal value. Poll takes signed int in milliseconds.
} else if (num > INT32_MAX) {
num = INT32_MAX / 1000;
WARN("wait interval is too long, using %ld seconds\n", num);
}
*wait = num;
return KNOT_EOK;
}
static int host_params_parse_name(params_t *params, const char *name)
{
char *reverse = get_reverse_name(name);
......@@ -297,7 +244,7 @@ int host_params_parse(params_t *params, int argc, char *argv[])
host_params_flag_nonrecursive(params);
break;
case 'T':
host_params_flag_tcp(params);
params_flag_tcp(params);
break;
case '4':
host_params_flag_ipv4(params);
......@@ -312,14 +259,13 @@ int host_params_parse(params_t *params, int argc, char *argv[])
host_params_flag_nowait(params);
break;
case 'R':
if (host_params_parse_retries(optarg,
&(params->retries))
if (params_parse_num(optarg, &(params->retries))
!= KNOT_EOK) {
return KNOT_EINVAL;
}
break;
case 'W':
if (host_params_parse_wait(optarg, &(params->wait))
if (params_parse_interval(optarg, &(params->wait))
!= KNOT_EOK) {
return KNOT_EINVAL;
}
......@@ -339,7 +285,7 @@ int host_params_parse(params_t *params, int argc, char *argv[])
break;
case 'd':
case 'v': // Fall through.
host_params_flag_verbose(params);
params_flag_verbose(params);
break;
default:
host_params_help(argc, argv);
......
......@@ -13,3 +13,46 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include "utils/nsupdate/nsupdate_exec.h"
#include "utils/common/msg.h"
#include "common/errcode.h"
static int nsupdate_process(const params_t *params, FILE *fp)
{
return 0;
}
int nsupdate_exec(const params_t *params)
{
if (!params) {
return KNOT_EINVAL;
}
/* If not file specified, use stdin. */
if (EMPTY_LIST(params->qfiles)) {
return nsupdate_process(params, stdin);
}
/* Read from each specified file. */
strnode_t *n = NULL;
WALK_LIST(n, params->qfiles) {
if (strcmp(n->str, "-") == 0) {
nsupdate_process(params, stdin);
continue;
}
FILE *fp = fopen(n->str, "r");
if (!fp) {
ERR("could not open '%s': %s\n",
n->str, strerror(errno));
return KNOT_ERROR;
}
nsupdate_process(params, fp);
fclose(fp);
}
return KNOT_EOK;
}
......@@ -12,7 +12,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
/*!
* \file nsupdate_exec.h
*
......@@ -24,9 +24,17 @@
* @{
*/
#ifndef _HOST__NSUPDATE_EXEC_H_
#define _HOST__NSUPDATE_EXEC_H_
#ifndef _NSUPDATE__NSUPDATE_EXEC_H_
#define _NSUPDATE__NSUPDATE_EXEC_H_
#include <stdbool.h> // bool
#include <stdint.h> // uint16_t
#include "utils/nsupdate/nsupdate_params.h"
#endif // _HOST__NSUPDATE_EXEC_H_
int nsupdate_exec(const params_t *params);
#endif // _NSUPDATE__NSUPDATE_EXEC_H_
/*! @} */
......@@ -17,9 +17,24 @@
#include <stdlib.h> // EXIT_FAILURE
#include "common/errcode.h" // KNOT_EOK
#include "utils/nsupdate/nsupdate_params.h" // params_t
#include "utils/nsupdate/nsupdate_exec.h" // host_exec
int main(int argc, char *argv[])
{
return EXIT_SUCCESS;
int ret = EXIT_SUCCESS;
params_t params;
if (nsupdate_params_parse(&params, argc, argv) == KNOT_EOK) {
if (nsupdate_exec(&params) != KNOT_EOK) {
ret = EXIT_FAILURE;
}
} else {
ret = EXIT_FAILURE;
}
nsupdate_params_clean(&params);
return ret;
}
......@@ -14,5 +14,97 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include "utils/nsupdate/nsupdate_params.h"
#include "utils/common/msg.h"
#include "libknot/util/descriptor.h"
#include "common/errcode.h"
#define DEFAULT_RETRIES 3
static void nsupdate_params_init(params_t *params)
{
memset(params, 0, sizeof(*params));
// Lists
init_list(&params->qfiles);
// Default values.
params->port = DEFAULT_PORT;
params->protocol = PROTO_ALL;
params->udp_size = DEFAULT_UDP_SIZE;
params->retries = DEFAULT_RETRIES;
params->wait = DEFAULT_WAIT_INTERVAL;
params->verbose = false;
}
void nsupdate_params_clean(params_t *params)
{
if (params == NULL) {
return;
}
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
static void nsupdate_params_help(int argc, char *argv[])
{
printf("Usage: %s [-d] [-v] [-p port] [-t timeout] [-r retries] "
"[filename]\n", argv[0]);
}
int nsupdate_params_parse(params_t *params, int argc, char *argv[])
{
int opt = 0;
if (params == NULL || argv == NULL) {
return KNOT_EINVAL;
}
nsupdate_params_init(params);
/* Command line options processing. */
while ((opt = getopt(argc, argv, "dvp:t:r:")) != -1) {
switch (opt) {
case 'd':
params_flag_verbose(params);
break;
case 'v':
params_flag_tcp(params);
break;
case 'r':
if (params_parse_num(optarg, &params->retries)
!= KNOT_EOK) {
return KNOT_EINVAL;
}
break;
case 't':
if (params_parse_interval(optarg, &params->wait)
!= KNOT_EOK) {
return KNOT_EINVAL;
}
break;
default:
nsupdate_params_help(argc, argv);
return KNOT_ENOTSUP;
}
}
/* Process non-option parameters. */
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(&params->qfiles, &n->n);
}
return KNOT_EOK;
}
......@@ -27,6 +27,15 @@
#ifndef _NSUPDATE_PARAMS_H_
#define _NSUPDATE_PARAMS_H_
#include <stdbool.h>
#include <stdint.h>
#include "common/lists.h" // list
#include "utils/common/params.h" // protocol_t
int nsupdate_params_parse(params_t *params, int argc, char *argv[]);
void nsupdate_params_clean(params_t *params);
#endif // _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