add: parsing
This commit is contained in:
77
src/main.c
77
src/main.c
@@ -1,7 +1,9 @@
|
||||
#include "dns.h"
|
||||
#include "host.h"
|
||||
#include "packet.h"
|
||||
#include "parsing.h"
|
||||
#include "print.h"
|
||||
#include "setting.h"
|
||||
#include <netinet/ip.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
@@ -32,30 +34,81 @@ void signal_handler(int code)
|
||||
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')
|
||||
{
|
||||
size_t tmp = number * 10 + *start - '0';
|
||||
if (tmp < number)
|
||||
goto value_error;
|
||||
number = tmp;
|
||||
start++;
|
||||
}
|
||||
|
||||
if (min > number || number > max)
|
||||
goto value_error;
|
||||
|
||||
*out = number;
|
||||
|
||||
return 0;
|
||||
|
||||
value_error:
|
||||
print_err("invalid argument: '%s': out of range: %zu <= value <= %zu", str, min, max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int get_setting(char * const *av, struct setting *setting)
|
||||
{
|
||||
struct param parameters[] = {
|
||||
{NULL, "?", OPTION, false},
|
||||
{NULL, "v", OPTION, false},
|
||||
{NULL, "f", OPTION, false},
|
||||
{NULL, "n", OPTION, false},
|
||||
{NULL, "W",ARGUMENT, "0"},
|
||||
{"ttl", NULL, ARGUMENT, "116"},
|
||||
{NULL, NULL, 0, NULL},
|
||||
};
|
||||
char *hostname = parsing(av, parameters);
|
||||
if (hostname == NULL)
|
||||
return 1;
|
||||
setting->dest.hostname = hostname;
|
||||
|
||||
setting->help = parameters[0].value;
|
||||
setting->verbose = parameters[1].value;
|
||||
setting->flood = parameters[2].value;
|
||||
setting->numeric_only = parameters[3].value;
|
||||
if (parsing_number(parameters[4].value, 0, 2147483647, &setting->timeout))
|
||||
return 1;
|
||||
if (parsing_number(parameters[5].value, 0, 255, &setting->ttl))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
(void) ac;
|
||||
(void) av;
|
||||
struct setting settings;
|
||||
|
||||
struct host host;
|
||||
|
||||
host.hostname = "localhost";
|
||||
get_setting(av + 1, &settings);
|
||||
|
||||
size_t payload_size = 20;
|
||||
size_t packet_size = sizeof(struct icmphdr) + payload_size;
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
if (dns_lookup(host.hostname, host.ip))
|
||||
if (dns_lookup(&settings.dest))
|
||||
return 5;
|
||||
|
||||
host.reverse_dns = dns_reverse_lookup(host.ip);
|
||||
|
||||
settings.dest.ip.sin_family = AF_INET;
|
||||
settings.dest.ip.sin_port = htons(0);
|
||||
inet_pton(AF_INET, settings.dest.ipstr, &settings.dest.ip.sin_addr);
|
||||
|
||||
struct sockaddr_in dest;
|
||||
|
||||
dest.sin_family = AF_INET;
|
||||
dest.sin_port = htons(0);
|
||||
inet_pton(AF_INET, host.ip, &dest.sin_addr);
|
||||
dns_reverse_lookup(&settings.dest);
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||
|
||||
@@ -89,7 +142,7 @@ int main(int ac, char **av)
|
||||
struct timeval stop, start;
|
||||
|
||||
gettimeofday(&start, NULL);
|
||||
if (sendto(sockfd, packet, packet_size, 0, (struct sockaddr *) &dest, sizeof(dest)) == -1)
|
||||
if (sendto(sockfd, packet, packet_size, 0, (struct sockaddr *) &settings.dest.ip, sizeof(settings.dest.ip)) == -1)
|
||||
{
|
||||
print_err("error: send packet failed.");
|
||||
free(packet);
|
||||
@@ -113,7 +166,7 @@ int main(int ac, char **av)
|
||||
|
||||
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + ((double)stop.tv_usec - (double) start.tv_usec) / 1000);
|
||||
struct icmphdr *hdr = (struct icmphdr *) packet;
|
||||
printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr) + sizeof(struct ether_header), host.reverse_dns, host.ip, htons(hdr->un.echo.sequence), 116, time_interval);
|
||||
printf("%zu bytes from %s (%s): icmp_seq=%d, ttl=%u, time=%fms\n", payload_size + sizeof(struct icmphdr) + sizeof(struct iphdr) + sizeof(struct ether_header), settings.dest.reverse_dns, settings.dest.ipstr, htons(hdr->un.echo.sequence), 116, time_interval);
|
||||
|
||||
sleep(1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user