wip: file of hosts as an argument
This commit is contained in:
parent
623b3ad0d7
commit
331d1077b9
@ -4,12 +4,13 @@
|
|||||||
|
|
||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
|
||||||
#define NB_OPTIONS 5
|
#define NB_OPTIONS 6
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
FL_HELP,
|
FL_HELP,
|
||||||
FL_PORTS,
|
FL_PORTS,
|
||||||
FL_IP,
|
FL_IP,
|
||||||
|
FL_FILE,
|
||||||
FL_SPEEDUP,
|
FL_SPEEDUP,
|
||||||
FL_SCAN,
|
FL_SCAN,
|
||||||
} e_flag;
|
} e_flag;
|
||||||
@ -21,6 +22,6 @@ struct option_lst {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct option_lst *parse_options(int ac, char *const *av);
|
struct option_lst *parse_options(int ac, char *const *av);
|
||||||
const char *get_option_arg(const struct option_lst *options, e_flag flag);
|
char *get_option_arg(const struct option_lst *options, e_flag flag);
|
||||||
int parse_ports(const char *arg, uint16_t *start, uint16_t *end);
|
int parse_ports(const char *arg, uint16_t *start, uint16_t *end);
|
||||||
e_scantype parse_type(const char *arg);
|
e_scantype parse_type(const char *arg);
|
||||||
|
45
src/main.c
45
src/main.c
@ -13,6 +13,25 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
|
static int scan_host(char *host, const struct option_lst *options)
|
||||||
|
{
|
||||||
|
char ip_addr[INET_ADDRSTRLEN];
|
||||||
|
struct sockaddr_in addr_con;
|
||||||
|
host[strcspn(host, "\n")] = '\0';
|
||||||
|
if (dns_lookup(ip_addr, host, &addr_con)) {
|
||||||
|
dprintf(2,
|
||||||
|
"ft_nmap: failed to retrieve ip address from "
|
||||||
|
"'%s'\n",
|
||||||
|
host);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct response responses[1024] = {0};
|
||||||
|
if (create_threads(options, ip_addr, responses) < 0)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
if (ac < 2) {
|
if (ac < 2) {
|
||||||
@ -26,23 +45,29 @@ int main(int ac, char **av)
|
|||||||
if (options == NULL)
|
if (options == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
char ip_addr[INET_ADDRSTRLEN];
|
char *host = get_option_arg(options, FL_IP);
|
||||||
struct sockaddr_in addr_con;
|
if (host)
|
||||||
|
return scan_host(host, options);
|
||||||
|
|
||||||
const char *host = get_option_arg(options, FL_IP);
|
const char *hosts_path = get_option_arg(options, FL_FILE);
|
||||||
if (!host) {
|
if (!hosts_path) {
|
||||||
dprintf(2, "ft_nmap: address/hostname required\n");
|
dprintf(2, "ft_nmap: address/hostname required\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (dns_lookup(ip_addr, host, &addr_con)) {
|
|
||||||
dprintf(2, "ft_nmap: failed to retrieve ip address from '%s'\n",
|
FILE *hosts_file = fopen(hosts_path, "r");
|
||||||
host);
|
if (hosts_file == NULL) {
|
||||||
|
dprintf(2, "ft_nmap: unable to open file '%s'\n", hosts_path);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
char line[256];
|
||||||
struct response responses[1024] = {0};
|
while (fgets(line, sizeof(line), hosts_file)) {
|
||||||
if (create_threads(options, ip_addr, responses) < 0)
|
if (scan_host(line, options) < 0) {
|
||||||
|
fclose(hosts_file);
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(hosts_file);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
|
|
||||||
const char *get_option_arg(const struct option_lst *options, e_flag flag)
|
char *get_option_arg(const struct option_lst *options, e_flag flag)
|
||||||
{
|
{
|
||||||
if (!options)
|
if (!options)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -35,6 +35,11 @@ e_scantype parse_type(const char *arg)
|
|||||||
int parse_ports(const char *arg, uint16_t *p_start, uint16_t *p_end)
|
int parse_ports(const char *arg, uint16_t *p_start, uint16_t *p_end)
|
||||||
{
|
{
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
if (!arg) {
|
||||||
|
*p_start = 1;
|
||||||
|
*p_end = 1024;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
for (; isdigit(arg[i]); i++)
|
for (; isdigit(arg[i]); i++)
|
||||||
;
|
;
|
||||||
int start = atoi(arg);
|
int start = atoi(arg);
|
||||||
@ -133,6 +138,7 @@ struct option_lst *parse_options(int ac, char *const *av)
|
|||||||
{"help", no_argument, 0, 0},
|
{"help", no_argument, 0, 0},
|
||||||
{"ports", required_argument, 0, 0},
|
{"ports", required_argument, 0, 0},
|
||||||
{"ip", required_argument, 0, 0},
|
{"ip", required_argument, 0, 0},
|
||||||
|
{"file", required_argument, 0, 0},
|
||||||
{"speedup", required_argument, 0, 0},
|
{"speedup", required_argument, 0, 0},
|
||||||
{"scan", required_argument, 0, 0},
|
{"scan", required_argument, 0, 0},
|
||||||
};
|
};
|
||||||
|
12
src/thread.c
12
src/thread.c
@ -103,6 +103,7 @@ int create_threads(const struct option_lst *options, char *ip_addr,
|
|||||||
init_threads_data(options, ip_addr, &host, responses, 1);
|
init_threads_data(options, ip_addr, &host, responses, 1);
|
||||||
thread_data->port_start = port_start;
|
thread_data->port_start = port_start;
|
||||||
thread_data->port_end = port_end;
|
thread_data->port_end = port_end;
|
||||||
|
g_start = true;
|
||||||
routine(thread_data);
|
routine(thread_data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -126,10 +127,13 @@ int create_threads(const struct option_lst *options, char *ip_addr,
|
|||||||
uint16_t remaining_ports = (port_end - port_start) % g_nb_threads;
|
uint16_t remaining_ports = (port_end - port_start) % g_nb_threads;
|
||||||
for (uint8_t i = 0; i < g_nb_threads; i++) {
|
for (uint8_t i = 0; i < g_nb_threads; i++) {
|
||||||
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 =
|
threads_data[i].port_end = port_start +
|
||||||
port_start + (i + 1) * ports_per_thread;
|
(i + 1) * ports_per_thread +
|
||||||
// TODO implement remaining_ports
|
(remaining_ports ? 1 : 0);
|
||||||
(void)remaining_ports;
|
if (remaining_ports) {
|
||||||
|
remaining_ports--;
|
||||||
|
port_start++;
|
||||||
|
}
|
||||||
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");
|
||||||
|
Loading…
Reference in New Issue
Block a user