core: remove nb_threads from struct scan and pass it as a parameter

This commit is contained in:
0x35c 2025-06-03 15:34:46 +02:00
parent 9ae1e29f71
commit 954f59649a
5 changed files with 35 additions and 32 deletions

View File

@ -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);

View File

@ -2,9 +2,6 @@
#include <stdint.h>
#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);

View File

@ -7,13 +7,13 @@
#include <string.h>
#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;
}

View File

@ -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;
}
}

View File

@ -7,8 +7,7 @@
#include <string.h>
#include <unistd.h>
#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;