add: parsing
This commit is contained in:
33
src/dns.c
33
src/dns.c
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "host.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -8,7 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
int dns_lookup(const char *hostname, char *ipstr)
|
int dns_lookup(struct hostenv *host)
|
||||||
{
|
{
|
||||||
struct addrinfo filter;
|
struct addrinfo filter;
|
||||||
|
|
||||||
@ -16,48 +17,34 @@ int dns_lookup(const char *hostname, char *ipstr)
|
|||||||
filter.ai_family = AF_INET;
|
filter.ai_family = AF_INET;
|
||||||
|
|
||||||
struct addrinfo *responses;
|
struct addrinfo *responses;
|
||||||
if (getaddrinfo(hostname, NULL, &filter, &responses) != 0)
|
if (getaddrinfo(host->hostname, NULL, &filter, &responses) != 0)
|
||||||
{
|
{
|
||||||
print_err("dns lookup failed");
|
print_err("dns lookup failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo *first = responses;
|
struct addrinfo *first = responses;
|
||||||
struct in_addr *addr = &(((struct sockaddr_in *) responses->ai_addr)->sin_addr);
|
struct in_addr *addr = &(((struct sockaddr_in *) first->ai_addr)->sin_addr);
|
||||||
|
|
||||||
inet_ntop(first->ai_family, addr, ipstr, sizeof(char) * INET_ADDRSTRLEN);
|
inet_ntop(first->ai_family, addr, host->ipstr, sizeof(char) * INET_ADDRSTRLEN);
|
||||||
|
|
||||||
freeaddrinfo(responses);
|
freeaddrinfo(responses);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *dns_reverse_lookup(const char *ipstr)
|
int dns_reverse_lookup(struct hostenv *host)
|
||||||
{
|
{
|
||||||
struct sockaddr_in sa;
|
|
||||||
char host[NI_MAXHOST];
|
|
||||||
|
|
||||||
bzero(&sa, sizeof(sa));
|
|
||||||
sa.sin_family = AF_INET;
|
|
||||||
|
|
||||||
printf("%s\n", ipstr);
|
|
||||||
|
|
||||||
if (inet_pton(AF_INET, ipstr, &sa.sin_addr) <= 0)
|
|
||||||
{
|
|
||||||
print_err("inet_pton failed");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret = getnameinfo(
|
int ret = getnameinfo(
|
||||||
(struct sockaddr *)&sa, sizeof(sa),
|
(struct sockaddr *)&host->ip, sizeof(host->ip),
|
||||||
host, sizeof(host),
|
host->reverse_dns, sizeof(host->reverse_dns),
|
||||||
NULL, 0, NI_NAMEREQD);
|
NULL, 0, NI_NAMEREQD);
|
||||||
|
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
print_err("reverse dns failed.");
|
print_err("reverse dns failed.");
|
||||||
return NULL;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return strdup(host);
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int dns_lookup(const char *hostname, char *ipstr);
|
#include "host.h"
|
||||||
char *dns_reverse_lookup(const char *ipstr);
|
|
||||||
|
int dns_lookup(struct hostenv *host);
|
||||||
|
int dns_reverse_lookup(struct hostenv *host);
|
||||||
@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
struct host {
|
struct hostenv {
|
||||||
char ip[INET_ADDRSTRLEN];
|
char ipstr[INET_ADDRSTRLEN];
|
||||||
|
struct sockaddr_in ip;
|
||||||
char *hostname;
|
char *hostname;
|
||||||
char *reverse_dns;
|
char reverse_dns[NI_MAXHOST];
|
||||||
};
|
};
|
||||||
77
src/main.c
77
src/main.c
@ -1,7 +1,9 @@
|
|||||||
#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 <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -32,30 +34,81 @@ 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')
|
||||||
|
{
|
||||||
|
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)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
(void) ac;
|
(void) ac;
|
||||||
(void) av;
|
struct setting settings;
|
||||||
|
|
||||||
struct host host;
|
get_setting(av + 1, &settings);
|
||||||
|
|
||||||
host.hostname = "localhost";
|
|
||||||
|
|
||||||
size_t payload_size = 20;
|
size_t payload_size = 20;
|
||||||
size_t packet_size = sizeof(struct icmphdr) + payload_size;
|
size_t packet_size = sizeof(struct icmphdr) + payload_size;
|
||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
if (dns_lookup(host.hostname, host.ip))
|
if (dns_lookup(&settings.dest))
|
||||||
return 5;
|
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;
|
dns_reverse_lookup(&settings.dest);
|
||||||
dest.sin_port = htons(0);
|
|
||||||
inet_pton(AF_INET, host.ip, &dest.sin_addr);
|
|
||||||
|
|
||||||
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
|
|
||||||
@ -89,7 +142,7 @@ int main(int ac, char **av)
|
|||||||
struct timeval stop, start;
|
struct timeval stop, start;
|
||||||
|
|
||||||
gettimeofday(&start, NULL);
|
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.");
|
print_err("error: send packet failed.");
|
||||||
free(packet);
|
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);
|
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;
|
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);
|
sleep(1);
|
||||||
|
|
||||||
|
|||||||
@ -6,31 +6,22 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static struct param parameters[] = {
|
|
||||||
{NULL, "?", OPTION, false},
|
|
||||||
{NULL, "v", OPTION, false},
|
|
||||||
{NULL, "f", OPTION, false},
|
|
||||||
{NULL, "n", OPTION, false},
|
|
||||||
{NULL, "w", ARGUMENT, NULL},
|
|
||||||
{NULL, "W",ARGUMENT, NULL},
|
|
||||||
{"ttl", NULL, ARGUMENT, "116"},
|
|
||||||
{NULL, NULL, NULL, NULL},
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct param *get_parameter(struct param parameters[], const char *str)
|
static struct param *get_parameter(struct param parameters[], const char *str)
|
||||||
{
|
{
|
||||||
size_t count = str[0] == '-' + str[1] == '-';
|
size_t count = (str[0] == '-') + (str[1] == '-');
|
||||||
for (size_t i = 0; parameters[i].name || parameters[i].alias; i++)
|
for (size_t i = 0; parameters[i].name || parameters[i].alias; i++)
|
||||||
{
|
{
|
||||||
if (count == 2 && !strcmp(parameters[i].name, str + count))
|
if (count == 2 && parameters[i].name != NULL && !strcmp(parameters[i].name, str + count))
|
||||||
return parameters + i;
|
return parameters + i;
|
||||||
else if (count == 1 && !strcmp(parameters[i].alias, str + count))
|
else if (count == 1 && parameters[i].alias != NULL && !strcmp(parameters[i].alias, str + count))
|
||||||
return parameters + i;
|
return parameters + i;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parsing(const char * const *av)
|
char *parsing(char * const *av, struct param parameters[])
|
||||||
{
|
{
|
||||||
char *host;
|
char *host;
|
||||||
for (size_t i = 0; av[i]; i++)
|
for (size_t i = 0; av[i]; i++)
|
||||||
@ -55,5 +46,5 @@ int parsing(const char * const *av)
|
|||||||
host = (char*) av[i];
|
host = (char*) av[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return host;
|
||||||
}
|
}
|
||||||
@ -11,3 +11,5 @@ struct param {
|
|||||||
e_type type;
|
e_type type;
|
||||||
void *value;
|
void *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char *parsing(char * const *av, struct param parameters[]);
|
||||||
17
src/setting.h
Normal file
17
src/setting.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "host.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct setting
|
||||||
|
{
|
||||||
|
bool help;
|
||||||
|
bool verbose;
|
||||||
|
bool flood;
|
||||||
|
bool numeric_only;
|
||||||
|
size_t timeout;
|
||||||
|
size_t ttl;
|
||||||
|
struct hostenv dest;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user