feature: better print output

fix: remove useless mutex for the routine start
This commit is contained in:
0x35c 2025-06-05 10:44:15 +02:00
parent 954f59649a
commit eaf5913c29
6 changed files with 74 additions and 27 deletions

View File

@ -27,6 +27,7 @@ struct option_lst {
struct option_lst *parse_options(int ac, char *const *av);
char *get_option_arg(const struct option_lst *options, e_flag flag);
bool option_isset(const struct option_lst *options, e_flag flag);
e_scantype parse_type(const char *arg);
void free_options(struct option_lst *options);
int parsing(struct scan *general, const struct option_lst *options);

View File

@ -3,5 +3,7 @@
#include "parsing.h"
#include "response.h"
void print_host_results(const struct scan *general, double scan_time);
void print_usage(void);
void print_config(const struct scan *general, const char *hosts_path,
uint8_t nb_threads);
void print_host_results(const struct scan *general, double scan_time);

View File

@ -40,6 +40,11 @@ int main(int ac, char **av)
if (options == NULL)
return 1;
if (option_isset(options, FL_HELP)) {
print_usage();
return 0;
}
struct scan general;
if (parsing(&general, options)) {
free_options(options);
@ -57,6 +62,7 @@ int main(int ac, char **av)
char *dest_addr = get_option_arg(options, FL_IP);
if (dest_addr) {
general.dest_addr = dest_addr;
print_config(&general, NULL, nb_threads);
int rv = scan_host(&general, nb_threads);
free_options(options);
return rv;
@ -73,6 +79,8 @@ int main(int ac, char **av)
dprintf(2, "ft_nmap: unable to open file '%s'\n", hosts_path);
goto error;
}
print_config(&general, hosts_path, nb_threads);
char line[256];
while (fgets(line, sizeof(line), hosts_file)) {
line[strcspn(line, "\n")] = '\0';

View File

@ -199,7 +199,6 @@ struct option_lst *parse_options(int ac, char *const *av)
{"scan", required_argument, 0, 0},
{"max_retries", required_argument, 0, 0},
{"ttl", required_argument, 0, 0},
{"open", no_argument, 0, 0},
};
int c;

View File

@ -1,21 +1,61 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "parsing.h"
#include "response.h"
#include "scan.h"
void print_usage(void)
{
// TODO
printf("--help: Print this help screen\n");
printf("--ports: ports to scan (eg: 42-69 or 666)\n");
printf("--ip: ip address to scan in dot format\n");
printf("--file: File name containing IP addresses to scan\n");
printf("--speedup: [250 max] number of parallel threads to use\n");
printf("--scan: NULL/SYN/FIN/XMAS/ACK/UDP\n");
}
static void print_port_state(uint16_t port, e_scantype type, const struct response * response)
void print_config(const struct scan *general, const char *hosts_path,
uint8_t nb_threads)
{
printf("Scan configurations\n");
if (hosts_path)
printf("Target hosts list filename: %s\n", hosts_path);
else
printf("Target ip address: %s\n", general->dest_addr);
if (general->port_start != general->port_end)
printf("Number of scans to be performed: %d\n",
general->port_end - general->port_start + 1);
printf("Scans to be performed: ");
if (general->type == SCAN_ALL)
for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++)
printf("%s ", types_str[type]);
else
printf("%s", types_str[general->type]);
printf("\n");
if (nb_threads > 1)
printf("No of threads: %d\n", nb_threads);
}
bool is_port_opened(const e_state states[6], e_scantype type)
{
if (type == SCAN_ALL) {
for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++)
if (states[i] == OPENED)
return true;
return false;
}
return states[type] == OPENED;
}
static void print_port_state(uint16_t port, e_scantype type,
const struct response *response)
{
if (type != SCAN_ALL && response->states[type] == CLOSED)
return;
printf("%-5d %-12s ", port,
response->service ? response->service : "Unassigned");
if (response->service)
free(response->service);
if (type == SCAN_ALL)
for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++)
printf("%s(%s) ", types_str[i],
@ -30,8 +70,21 @@ 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("Open ports:\n");
for (uint16_t port = general->port_start; port < general->port_end; port++) {
print_port_state(port, general->type, &general->responses[port - general->port_start]);
printf("Opened ports:\n");
for (uint16_t port = general->port_start; port < general->port_end;
port++) {
const struct response *response =
&general->responses[port - general->port_start];
if (is_port_opened(response->states, general->type))
print_port_state(port, general->type, response);
}
printf("\n");
printf("Closed/filtered/unfiltered ports:\n");
for (uint16_t port = general->port_start; port <= general->port_end;
port++) {
const struct response *response =
&general->responses[port - general->port_start];
if (!is_port_opened(response->states, general->type))
print_port_state(port, general->type, response);
}
}

View File

@ -11,21 +11,10 @@
#include "scan.h"
#include "thread.h"
bool g_start = false;
pthread_mutex_t g_start_mtx;
pthread_mutex_t g_getservent;
void *routine(void *p_data)
{
while (1) {
pthread_mutex_lock(&g_start_mtx);
bool start = g_start;
pthread_mutex_unlock(&g_start_mtx);
if (start)
break;
usleep(100);
}
struct scan *thread_data = p_data;
struct scan scan_data;
memcpy(&scan_data, thread_data, sizeof(struct scan));
@ -79,7 +68,6 @@ static struct scan *init_threads_data(const struct scan *general,
int create_threads(struct scan *general, uint8_t nb_threads)
{
if (nb_threads == 1) {
g_start = true;
routine(general);
return 0;
}
@ -93,7 +81,6 @@ int create_threads(struct scan *general, uint8_t nb_threads)
return -1;
}
pthread_mutex_init(&g_start_mtx, NULL);
pthread_mutex_init(&g_getservent, NULL);
for (uint8_t i = 0; i < nb_threads; i++) {
@ -103,9 +90,6 @@ int create_threads(struct scan *general, uint8_t nb_threads)
return -1;
}
}
pthread_mutex_lock(&g_start_mtx);
g_start = true;
pthread_mutex_unlock(&g_start_mtx);
for (uint8_t i = 0; i < nb_threads; i++) {
if (pthread_join(threads[i], NULL)) {