add: description to paring struct && create setting.c
This commit is contained in:
64
src/main.c
64
src/main.c
@@ -1,7 +1,6 @@
|
|||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include "host.h"
|
#include "host.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "parsing.h"
|
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "setting.h"
|
#include "setting.h"
|
||||||
#include "statistics.h"
|
#include "statistics.h"
|
||||||
@@ -26,7 +25,6 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <ctype.h>
|
|
||||||
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
||||||
|
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
@@ -37,68 +35,6 @@ static void signal_handler(int code)
|
|||||||
loop = false;
|
loop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parsing_number(const char *str, size_t min, size_t max, size_t *out)
|
|
||||||
{
|
|
||||||
size_t number = 0;
|
|
||||||
const char *start = str;
|
|
||||||
|
|
||||||
while (*start != '\0')
|
|
||||||
{
|
|
||||||
if (!isdigit(*start))
|
|
||||||
goto value_error;
|
|
||||||
size_t tmp = number * 10 + *start - '0';
|
|
||||||
if (tmp < number)
|
|
||||||
goto range_error;
|
|
||||||
number = tmp;
|
|
||||||
start++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (min > number || number > max)
|
|
||||||
goto range_error;
|
|
||||||
|
|
||||||
*out = number;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
range_error:
|
|
||||||
print_err("invalid argument: '%s': out of range: %zu <= value <= %zu", str, min, max);
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
value_error:
|
|
||||||
print_err("invalid argument: in '%s' '%c' is not a digit", str, *start);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_setting(char * const *av, struct setting *settings)
|
|
||||||
{
|
|
||||||
struct param parameters[] = {
|
|
||||||
{NULL, "?", OPTION, false},
|
|
||||||
{NULL, "v", OPTION, false},
|
|
||||||
{NULL, "n", OPTION, false},
|
|
||||||
{"ttl", NULL, ARGUMENT, "116"},
|
|
||||||
{NULL, "s", ARGUMENT, "56"},
|
|
||||||
{NULL, "p", ARGUMENT, "0"},
|
|
||||||
{NULL, NULL, 0, NULL},
|
|
||||||
};
|
|
||||||
char *hostname = parsing(av, parameters);
|
|
||||||
if (hostname == NULL)
|
|
||||||
return 1;
|
|
||||||
settings->dest.hostname = hostname;
|
|
||||||
|
|
||||||
settings->help = parameters[0].value;
|
|
||||||
settings->verbose = parameters[1].value;
|
|
||||||
settings->numeric_only = parameters[2].value;
|
|
||||||
size_t ttl;
|
|
||||||
if (parsing_number(parameters[3].value, 1, 255, &ttl))
|
|
||||||
return 2;
|
|
||||||
settings->ttl = ttl;
|
|
||||||
if (parsing_number(parameters[4].value, 0, 2147483647, &settings->payload_size))
|
|
||||||
return 3;
|
|
||||||
if (parsing_number(parameters[5].value, 0, 2147483647, &settings->preload))
|
|
||||||
return 4;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int create_socket(struct setting const *settings)
|
static int create_socket(struct setting const *settings)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ struct param {
|
|||||||
const char *alias;
|
const char *alias;
|
||||||
e_type type;
|
e_type type;
|
||||||
void *value;
|
void *value;
|
||||||
|
const char *desc;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *parsing(char * const *av, struct param parameters[]);
|
char *parsing(char * const *av, struct param parameters[]);
|
||||||
66
src/setting.c
Normal file
66
src/setting.c
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#include "setting.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "print.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
static int parsing_number(const char *str, size_t min, size_t max, size_t *out)
|
||||||
|
{
|
||||||
|
size_t number = 0;
|
||||||
|
const char *start = str;
|
||||||
|
|
||||||
|
while (*start != '\0')
|
||||||
|
{
|
||||||
|
if (!isdigit(*start))
|
||||||
|
goto value_error;
|
||||||
|
size_t tmp = number * 10 + *start - '0';
|
||||||
|
if (tmp < number)
|
||||||
|
goto range_error;
|
||||||
|
number = tmp;
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (min > number || number > max)
|
||||||
|
goto range_error;
|
||||||
|
|
||||||
|
*out = number;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
range_error:
|
||||||
|
print_err("invalid argument: '%s': out of range: %zu <= value <= %zu", str, min, max);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
value_error:
|
||||||
|
print_err("invalid argument: in '%s' '%c' is not a digit", str, *start);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_setting(char * const *av, struct setting *settings)
|
||||||
|
{
|
||||||
|
struct param parameters[] = {
|
||||||
|
{NULL, "?", OPTION, false, "Display help"},
|
||||||
|
{NULL, "v", OPTION, false, "Set verbose"},
|
||||||
|
{NULL, "n", OPTION, false, "Numeric only, display only host ip"},
|
||||||
|
{"ttl", NULL, ARGUMENT, "116", "Change ttl (defautl=116)"},
|
||||||
|
{NULL, "s", ARGUMENT, "56", "send packets before get recv"},
|
||||||
|
{NULL, "p", ARGUMENT, "0", "send packet with a specific payload size"},
|
||||||
|
{NULL, NULL, 0, NULL, NULL},
|
||||||
|
};
|
||||||
|
char *hostname = parsing(av, parameters);
|
||||||
|
if (hostname == NULL)
|
||||||
|
return 1;
|
||||||
|
settings->dest.hostname = hostname;
|
||||||
|
|
||||||
|
settings->help = parameters[0].value;
|
||||||
|
settings->verbose = parameters[1].value;
|
||||||
|
settings->numeric_only = parameters[2].value;
|
||||||
|
size_t ttl;
|
||||||
|
if (parsing_number(parameters[3].value, 1, 255, &ttl))
|
||||||
|
return 2;
|
||||||
|
settings->ttl = ttl;
|
||||||
|
if (parsing_number(parameters[4].value, 0, 2147483647, &settings->payload_size))
|
||||||
|
return 3;
|
||||||
|
if (parsing_number(parameters[5].value, 0, 2147483647, &settings->preload))
|
||||||
|
return 4;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -18,3 +18,5 @@ struct setting
|
|||||||
struct hostenv dest;
|
struct hostenv dest;
|
||||||
size_t preload;
|
size_t preload;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int get_setting(char * const *av, struct setting *settings);
|
||||||
Reference in New Issue
Block a user