core: remove nb_threads from struct scan and pass it as a parameter
This commit is contained in:
parent
9ae1e29f71
commit
954f59649a
@ -27,7 +27,6 @@ struct scan {
|
|||||||
uint8_t ttl;
|
uint8_t ttl;
|
||||||
uint8_t max_retries;
|
uint8_t max_retries;
|
||||||
struct response *responses;
|
struct response *responses;
|
||||||
uint8_t nb_threads;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int scan(struct scan *data);
|
int scan(struct scan *data);
|
||||||
|
@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "host.h"
|
|
||||||
#include "parsing.h"
|
|
||||||
#include "response.h"
|
|
||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
|
||||||
int create_threads(struct scan *general);
|
int create_threads(struct scan *general, uint8_t nb_threads);
|
||||||
|
18
src/main.c
18
src/main.c
@ -7,13 +7,13 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
|
#include "interface.h"
|
||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
#include "thread.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;
|
struct sockaddr_in addr_con;
|
||||||
if (dns_lookup(general->dest_addr, general->dest_addr, &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};
|
struct response responses[1024] = {0};
|
||||||
general->responses = responses;
|
general->responses = responses;
|
||||||
if (create_threads(general) < 0)
|
if (create_threads(general, nb_threads) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
print_host_results(general, 10);
|
print_host_results(general, 10);
|
||||||
return 0;
|
return 0;
|
||||||
@ -41,8 +41,7 @@ int main(int ac, char **av)
|
|||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
struct scan general;
|
struct scan general;
|
||||||
if (parsing(&general, options))
|
if (parsing(&general, options)) {
|
||||||
{
|
|
||||||
free_options(options);
|
free_options(options);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -50,10 +49,15 @@ int main(int ac, char **av)
|
|||||||
if (get_interface_name(&general.host) < 0)
|
if (get_interface_name(&general.host) < 0)
|
||||||
return -1;
|
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);
|
char *dest_addr = get_option_arg(options, FL_IP);
|
||||||
if (dest_addr) {
|
if (dest_addr) {
|
||||||
general.dest_addr = dest_addr;
|
general.dest_addr = dest_addr;
|
||||||
int rv = scan_host(&general);
|
int rv = scan_host(&general, nb_threads);
|
||||||
free_options(options);
|
free_options(options);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
@ -73,7 +77,7 @@ int main(int ac, char **av)
|
|||||||
while (fgets(line, sizeof(line), hosts_file)) {
|
while (fgets(line, sizeof(line), hosts_file)) {
|
||||||
line[strcspn(line, "\n")] = '\0';
|
line[strcspn(line, "\n")] = '\0';
|
||||||
general.dest_addr = line;
|
general.dest_addr = line;
|
||||||
if (scan_host(&general) < 0) {
|
if (scan_host(&general, nb_threads) < 0) {
|
||||||
fclose(hosts_file);
|
fclose(hosts_file);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
@ -231,7 +231,8 @@ int parsing(struct scan *general, const struct option_lst *options)
|
|||||||
general->port_end = 1024;
|
general->port_end = 1024;
|
||||||
if (option_isset(options, FL_FAST))
|
if (option_isset(options, FL_FAST))
|
||||||
general->port_end = 128;
|
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;
|
return -1;
|
||||||
general->type = parse_type(get_option_arg(options, FL_TYPE));
|
general->type = parse_type(get_option_arg(options, FL_TYPE));
|
||||||
if ((int)general->type == -1)
|
if ((int)general->type == -1)
|
||||||
@ -241,8 +242,8 @@ int parsing(struct scan *general, const struct option_lst *options)
|
|||||||
const char *ttl = get_option_arg(options, FL_TTL);
|
const char *ttl = get_option_arg(options, FL_TTL);
|
||||||
general->ttl = ttl ? atoi(ttl) : 48;
|
general->ttl = ttl ? atoi(ttl) : 48;
|
||||||
const char *nb_threads_str = get_option_arg(options, FL_SPEEDUP);
|
const char *nb_threads_str = get_option_arg(options, FL_SPEEDUP);
|
||||||
general->nb_threads = nb_threads_str ? atoi(nb_threads_str) : 1;
|
uint8_t nb_threads = nb_threads_str ? atoi(nb_threads_str) : 1;
|
||||||
if (general->port_end - general->port_start + 1 < general->nb_threads) {
|
if (general->port_end - general->port_start + 1 < nb_threads) {
|
||||||
dprintf(2, "ft_nmap: number of threads to use must be superior "
|
dprintf(2, "ft_nmap: number of threads to use must be superior "
|
||||||
"or equals to the ports range\n");
|
"or equals to the ports range\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
34
src/thread.c
34
src/thread.c
@ -7,8 +7,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "interface.h"
|
#include "response.h"
|
||||||
#include "parsing.h"
|
|
||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
@ -35,7 +34,8 @@ void *routine(void *p_data)
|
|||||||
port <= thread_data->port_end; port++) {
|
port <= thread_data->port_end; port++) {
|
||||||
scan_data.port_start = port;
|
scan_data.port_start = port;
|
||||||
scan_data.port_end = 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)) {
|
if (scan(&scan_data)) {
|
||||||
free(p_data);
|
free(p_data);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -45,25 +45,28 @@ void *routine(void *p_data)
|
|||||||
return NULL;
|
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) {
|
if (!threads_data) {
|
||||||
dprintf(2, "ft_nmap: allocation of threads failed\n");
|
dprintf(2, "ft_nmap: allocation of threads failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint16_t port_range = general->port_end - general->port_start + 1;
|
const uint16_t port_range = general->port_end - general->port_start + 1;
|
||||||
const uint16_t ports_per_thread = port_range / general->nb_threads;
|
const uint16_t ports_per_thread = port_range / nb_threads;
|
||||||
uint16_t remaining_ports = port_range % general->nb_threads;
|
uint16_t remaining_ports = port_range % nb_threads;
|
||||||
uint16_t port_start = general->port_start;
|
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));
|
memcpy(&threads_data[i], general, sizeof(struct scan));
|
||||||
threads_data[i].port_start = port_start + i * ports_per_thread;
|
threads_data[i].port_start = port_start + i * ports_per_thread;
|
||||||
threads_data[i].port_end = (port_start - 1) +
|
threads_data[i].port_end = (port_start - 1) +
|
||||||
(i + 1) * ports_per_thread +
|
(i + 1) * ports_per_thread +
|
||||||
(remaining_ports ? 1 : 0);
|
(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) {
|
if (remaining_ports) {
|
||||||
remaining_ports--;
|
remaining_ports--;
|
||||||
port_start++;
|
port_start++;
|
||||||
@ -73,19 +76,18 @@ static struct scan *init_threads_data(const struct scan *general)
|
|||||||
return threads_data;
|
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;
|
g_start = true;
|
||||||
routine(general);
|
routine(general);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct scan *threads_data =
|
struct scan *threads_data = init_threads_data(general, nb_threads);
|
||||||
init_threads_data(general);
|
|
||||||
if (threads_data == NULL)
|
if (threads_data == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
pthread_t *threads = malloc(sizeof(pthread_t) * threads_data->nb_threads);
|
pthread_t *threads = malloc(sizeof(pthread_t) * nb_threads);
|
||||||
if (!threads) {
|
if (!threads) {
|
||||||
free(threads_data);
|
free(threads_data);
|
||||||
return -1;
|
return -1;
|
||||||
@ -94,7 +96,7 @@ int create_threads(struct scan *general)
|
|||||||
pthread_mutex_init(&g_start_mtx, NULL);
|
pthread_mutex_init(&g_start_mtx, NULL);
|
||||||
pthread_mutex_init(&g_getservent, 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,
|
if (pthread_create(&threads[i], NULL, routine,
|
||||||
&threads_data[i])) {
|
&threads_data[i])) {
|
||||||
dprintf(2, "ft_nmap: error during pthread_create()\n");
|
dprintf(2, "ft_nmap: error during pthread_create()\n");
|
||||||
@ -105,7 +107,7 @@ int create_threads(struct scan *general)
|
|||||||
g_start = true;
|
g_start = true;
|
||||||
pthread_mutex_unlock(&g_start_mtx);
|
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)) {
|
if (pthread_join(threads[i], NULL)) {
|
||||||
dprintf(2, "ft_nmap: error during pthread_join()\n");
|
dprintf(2, "ft_nmap: error during pthread_join()\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user