From 954f59649a3a3ab3463c993914bc965e0093144b Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Tue, 3 Jun 2025 15:34:46 +0200 Subject: [PATCH] core: remove nb_threads from struct scan and pass it as a parameter --- include/scan.h | 1 - include/thread.h | 5 +---- src/main.c | 18 +++++++++++------- src/parsing.c | 9 +++++---- src/thread.c | 34 ++++++++++++++++++---------------- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/include/scan.h b/include/scan.h index 98ff0b7..38ba961 100644 --- a/include/scan.h +++ b/include/scan.h @@ -27,7 +27,6 @@ struct scan { uint8_t ttl; uint8_t max_retries; struct response *responses; - uint8_t nb_threads; }; int scan(struct scan *data); diff --git a/include/thread.h b/include/thread.h index cbd7aa3..2c47053 100644 --- a/include/thread.h +++ b/include/thread.h @@ -2,9 +2,6 @@ #include -#include "host.h" -#include "parsing.h" -#include "response.h" #include "scan.h" -int create_threads(struct scan *general); +int create_threads(struct scan *general, uint8_t nb_threads); diff --git a/src/main.c b/src/main.c index 550ffe3..8b47e84 100644 --- a/src/main.c +++ b/src/main.c @@ -7,13 +7,13 @@ #include #include "dns.h" +#include "interface.h" #include "parsing.h" #include "print.h" #include "scan.h" #include "thread.h" -#include "interface.h" -static int scan_host(struct scan *general) +static int scan_host(struct scan *general, uint8_t nb_threads) { struct sockaddr_in addr_con; if (dns_lookup(general->dest_addr, general->dest_addr, &addr_con)) { @@ -21,7 +21,7 @@ static int scan_host(struct scan *general) } struct response responses[1024] = {0}; general->responses = responses; - if (create_threads(general) < 0) + if (create_threads(general, nb_threads) < 0) return -1; print_host_results(general, 10); return 0; @@ -41,8 +41,7 @@ int main(int ac, char **av) return 1; struct scan general; - if (parsing(&general, options)) - { + if (parsing(&general, options)) { free_options(options); return 1; } @@ -50,10 +49,15 @@ int main(int ac, char **av) if (get_interface_name(&general.host) < 0) return -1; + char *nb_threads_str = get_option_arg(options, FL_SPEEDUP); + uint8_t nb_threads = 1; + if (nb_threads_str) + nb_threads = atoi(nb_threads_str); + char *dest_addr = get_option_arg(options, FL_IP); if (dest_addr) { general.dest_addr = dest_addr; - int rv = scan_host(&general); + int rv = scan_host(&general, nb_threads); free_options(options); return rv; } @@ -73,7 +77,7 @@ int main(int ac, char **av) while (fgets(line, sizeof(line), hosts_file)) { line[strcspn(line, "\n")] = '\0'; general.dest_addr = line; - if (scan_host(&general) < 0) { + if (scan_host(&general, nb_threads) < 0) { fclose(hosts_file); goto error; } diff --git a/src/parsing.c b/src/parsing.c index 471c1d6..9c7c24c 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -231,7 +231,8 @@ int parsing(struct scan *general, const struct option_lst *options) general->port_end = 1024; if (option_isset(options, FL_FAST)) general->port_end = 128; - if (parse_ports(get_option_arg(options, FL_PORTS), &general->port_start, &general->port_end)) + if (parse_ports(get_option_arg(options, FL_PORTS), &general->port_start, + &general->port_end)) return -1; general->type = parse_type(get_option_arg(options, FL_TYPE)); if ((int)general->type == -1) @@ -241,11 +242,11 @@ int parsing(struct scan *general, const struct option_lst *options) const char *ttl = get_option_arg(options, FL_TTL); general->ttl = ttl ? atoi(ttl) : 48; const char *nb_threads_str = get_option_arg(options, FL_SPEEDUP); - general->nb_threads = nb_threads_str ? atoi(nb_threads_str) : 1; - if (general->port_end - general->port_start + 1 < general->nb_threads) { + uint8_t nb_threads = nb_threads_str ? atoi(nb_threads_str) : 1; + if (general->port_end - general->port_start + 1 < nb_threads) { dprintf(2, "ft_nmap: number of threads to use must be superior " "or equals to the ports range\n"); return -1; } return 0; -} \ No newline at end of file +} diff --git a/src/thread.c b/src/thread.c index 9e54205..67b080b 100644 --- a/src/thread.c +++ b/src/thread.c @@ -7,8 +7,7 @@ #include #include -#include "interface.h" -#include "parsing.h" +#include "response.h" #include "scan.h" #include "thread.h" @@ -35,7 +34,8 @@ void *routine(void *p_data) port <= thread_data->port_end; port++) { scan_data.port_start = port; scan_data.port_end = port; - scan_data.responses = &thread_data->responses[port - thread_data->port_start]; + scan_data.responses = + &thread_data->responses[port - thread_data->port_start]; if (scan(&scan_data)) { free(p_data); return NULL; @@ -45,25 +45,28 @@ void *routine(void *p_data) return NULL; } -static struct scan *init_threads_data(const struct scan *general) +static struct scan *init_threads_data(const struct scan *general, + uint8_t nb_threads) { - struct scan *threads_data = malloc(sizeof(struct scan) * general->nb_threads); + struct scan *threads_data = malloc(sizeof(struct scan) * nb_threads); if (!threads_data) { dprintf(2, "ft_nmap: allocation of threads failed\n"); return NULL; } const uint16_t port_range = general->port_end - general->port_start + 1; - const uint16_t ports_per_thread = port_range / general->nb_threads; - uint16_t remaining_ports = port_range % general->nb_threads; + const uint16_t ports_per_thread = port_range / nb_threads; + uint16_t remaining_ports = port_range % nb_threads; uint16_t port_start = general->port_start; - for (uint8_t i = 0; i < general->nb_threads; i++) { + for (uint8_t i = 0; i < nb_threads; i++) { memcpy(&threads_data[i], general, sizeof(struct scan)); threads_data[i].port_start = port_start + i * ports_per_thread; threads_data[i].port_end = (port_start - 1) + (i + 1) * ports_per_thread + (remaining_ports ? 1 : 0); - threads_data[i].responses = general->responses + (threads_data[i].port_start - general->port_start); + threads_data[i].responses = + general->responses + + (threads_data[i].port_start - general->port_start); if (remaining_ports) { remaining_ports--; port_start++; @@ -73,19 +76,18 @@ static struct scan *init_threads_data(const struct scan *general) return threads_data; } -int create_threads(struct scan *general) +int create_threads(struct scan *general, uint8_t nb_threads) { - if (general->nb_threads == 1) { + if (nb_threads == 1) { g_start = true; routine(general); return 0; } - struct scan *threads_data = - init_threads_data(general); + struct scan *threads_data = init_threads_data(general, nb_threads); if (threads_data == NULL) return -1; - pthread_t *threads = malloc(sizeof(pthread_t) * threads_data->nb_threads); + pthread_t *threads = malloc(sizeof(pthread_t) * nb_threads); if (!threads) { free(threads_data); return -1; @@ -94,7 +96,7 @@ int create_threads(struct scan *general) pthread_mutex_init(&g_start_mtx, NULL); pthread_mutex_init(&g_getservent, NULL); - for (uint8_t i = 0; i < general->nb_threads; i++) { + for (uint8_t i = 0; i < nb_threads; i++) { if (pthread_create(&threads[i], NULL, routine, &threads_data[i])) { dprintf(2, "ft_nmap: error during pthread_create()\n"); @@ -105,7 +107,7 @@ int create_threads(struct scan *general) g_start = true; pthread_mutex_unlock(&g_start_mtx); - for (uint8_t i = 0; i < general->nb_threads; i++) { + for (uint8_t i = 0; i < nb_threads; i++) { if (pthread_join(threads[i], NULL)) { dprintf(2, "ft_nmap: error during pthread_join()\n"); return -1;