fix: data races with getservent
feature: time elapsed in the print
This commit is contained in:
parent
eaf5913c29
commit
31530c83a4
15
src/main.c
15
src/main.c
@ -5,6 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
@ -19,11 +20,19 @@ static int scan_host(struct scan *general, uint8_t nb_threads)
|
|||||||
if (dns_lookup(general->dest_addr, general->dest_addr, &addr_con)) {
|
if (dns_lookup(general->dest_addr, general->dest_addr, &addr_con)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct response responses[1024] = {0};
|
struct response responses[1024] = {0};
|
||||||
|
struct timespec start, end;
|
||||||
|
|
||||||
general->responses = responses;
|
general->responses = responses;
|
||||||
|
printf("Scanning...\n");
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
if (create_threads(general, nb_threads) < 0)
|
if (create_threads(general, nb_threads) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
print_host_results(general, 10);
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
|
double time_elapsed =
|
||||||
|
(end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
|
||||||
|
print_host_results(general, time_elapsed);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,8 +46,10 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct option_lst *options = parse_options(ac, av);
|
struct option_lst *options = parse_options(ac, av);
|
||||||
if (options == NULL)
|
if (options == NULL) {
|
||||||
|
print_usage();
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (option_isset(options, FL_HELP)) {
|
if (option_isset(options, FL_HELP)) {
|
||||||
print_usage();
|
print_usage();
|
||||||
|
@ -68,7 +68,6 @@ static void print_port_state(uint16_t port, e_scantype type,
|
|||||||
|
|
||||||
void print_host_results(const struct scan *general, double scan_time)
|
void print_host_results(const struct scan *general, double scan_time)
|
||||||
{
|
{
|
||||||
printf("Scan took %lf secs\n", scan_time);
|
|
||||||
printf("IP address: %s\n", general->dest_addr);
|
printf("IP address: %s\n", general->dest_addr);
|
||||||
printf("Opened ports:\n");
|
printf("Opened ports:\n");
|
||||||
for (uint16_t port = general->port_start; port < general->port_end;
|
for (uint16_t port = general->port_start; port < general->port_end;
|
||||||
@ -87,4 +86,5 @@ void print_host_results(const struct scan *general, double scan_time)
|
|||||||
if (!is_port_opened(response->states, general->type))
|
if (!is_port_opened(response->states, general->type))
|
||||||
print_port_state(port, general->type, response);
|
print_port_state(port, general->type, response);
|
||||||
}
|
}
|
||||||
|
printf("\nScan took %lf secs\n", scan_time);
|
||||||
}
|
}
|
||||||
|
@ -12,24 +12,27 @@ extern pthread_mutex_t g_getservent;
|
|||||||
|
|
||||||
static char *get_service_name(int port, char *proto)
|
static char *get_service_name(int port, char *proto)
|
||||||
{
|
{
|
||||||
|
char *name = NULL;
|
||||||
pthread_mutex_lock(&g_getservent);
|
pthread_mutex_lock(&g_getservent);
|
||||||
struct servent *servent = getservbyport(htons(port), proto);
|
struct servent *servent = getservbyport(htons(port), proto);
|
||||||
|
if (servent)
|
||||||
|
name = strdup(servent->s_name);
|
||||||
pthread_mutex_unlock(&g_getservent);
|
pthread_mutex_unlock(&g_getservent);
|
||||||
if (!servent)
|
return name;
|
||||||
return NULL;
|
|
||||||
return strdup(servent->s_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
|
void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
|
||||||
{
|
{
|
||||||
const e_scantype type = data->type;
|
const e_scantype type = data->type;
|
||||||
if (type == SCAN_UDP) {
|
if (type == SCAN_UDP) {
|
||||||
dprintf(2, "ft_nmap: error: received a TCP responses for an UDP "
|
dprintf(2,
|
||||||
"scan\n");
|
"ft_nmap: error: received a TCP responses for an UDP "
|
||||||
|
"scan\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data->responses->service == NULL)
|
if (data->responses->service == NULL)
|
||||||
data->responses->service = get_service_name(data->port_start, "tcp");
|
data->responses->service =
|
||||||
|
get_service_name(data->port_start, "tcp");
|
||||||
if (type == SCAN_SYN) {
|
if (type == SCAN_SYN) {
|
||||||
if (tcphdr->ack == 1 && tcphdr->syn == 1)
|
if (tcphdr->ack == 1 && tcphdr->syn == 1)
|
||||||
data->responses->states[type] = OPENED;
|
data->responses->states[type] = OPENED;
|
||||||
@ -49,12 +52,14 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data)
|
|||||||
{
|
{
|
||||||
(void)udphdr;
|
(void)udphdr;
|
||||||
if (data->type != SCAN_UDP) {
|
if (data->type != SCAN_UDP) {
|
||||||
dprintf(2, "ft_nmap: error: received an UDP responses for a TCP "
|
dprintf(2,
|
||||||
"scan\n");
|
"ft_nmap: error: received an UDP responses for a TCP "
|
||||||
|
"scan\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data->responses->service == NULL)
|
if (data->responses->service == NULL)
|
||||||
data->responses->service = get_service_name(data->port_start, "udp");
|
data->responses->service =
|
||||||
|
get_service_name(data->port_start, "udp");
|
||||||
data->responses->states[SCAN_UDP] = OPENED;
|
data->responses->states[SCAN_UDP] = OPENED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +69,8 @@ void icmp_response(const struct icmphdr *icmphdr, const struct scan *data)
|
|||||||
|
|
||||||
data->responses->service = get_service_name(data->port_start, "udp");
|
data->responses->service = get_service_name(data->port_start, "udp");
|
||||||
if (data->responses->service == NULL)
|
if (data->responses->service == NULL)
|
||||||
data->responses->service = get_service_name(data->port_start, "tcp");
|
data->responses->service =
|
||||||
|
get_service_name(data->port_start, "tcp");
|
||||||
if (type == SCAN_SYN && icmphdr->type == 3)
|
if (type == SCAN_SYN && icmphdr->type == 3)
|
||||||
data->responses->states[type] = FILTERED;
|
data->responses->states[type] = FILTERED;
|
||||||
else if (type == SCAN_ACK && icmphdr->type == 3)
|
else if (type == SCAN_ACK && icmphdr->type == 3)
|
||||||
@ -87,7 +93,8 @@ void no_response(const struct scan *data)
|
|||||||
|
|
||||||
data->responses->service = get_service_name(data->port_start, "udp");
|
data->responses->service = get_service_name(data->port_start, "udp");
|
||||||
if (data->responses->service == NULL)
|
if (data->responses->service == NULL)
|
||||||
data->responses->service = get_service_name(data->port_start, "tcp");
|
data->responses->service =
|
||||||
|
get_service_name(data->port_start, "tcp");
|
||||||
if (type == SCAN_SYN)
|
if (type == SCAN_SYN)
|
||||||
data->responses->states[type] = FILTERED;
|
data->responses->states[type] = FILTERED;
|
||||||
else if (type == SCAN_ACK)
|
else if (type == SCAN_ACK)
|
||||||
|
Loading…
Reference in New Issue
Block a user