42_ft_nmap/src/main.c
Starnakin 9ae1e29f71 core: use only one struct data instead of thead_data and scan_data
core: do parsing in the main, put all in struct general
fix: code support port > 1024
2025-06-03 16:49:41 +02:00

89 lines
1.8 KiB
C

#include <getopt.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dns.h"
#include "parsing.h"
#include "print.h"
#include "scan.h"
#include "thread.h"
#include "interface.h"
static int scan_host(struct scan *general)
{
struct sockaddr_in addr_con;
if (dns_lookup(general->dest_addr, general->dest_addr, &addr_con)) {
return -1;
}
struct response responses[1024] = {0};
general->responses = responses;
if (create_threads(general) < 0)
return -1;
print_host_results(general, 10);
return 0;
}
int main(int ac, char **av)
{
if (ac < 2) {
dprintf(2, "Usage: ft_nmap [Scan Type(s)] [Options] {target "
"specification}\n");
print_usage();
return 0;
}
struct option_lst *options = parse_options(ac, av);
if (options == NULL)
return 1;
struct scan general;
if (parsing(&general, options))
{
free_options(options);
return 1;
}
if (get_interface_name(&general.host) < 0)
return -1;
char *dest_addr = get_option_arg(options, FL_IP);
if (dest_addr) {
general.dest_addr = dest_addr;
int rv = scan_host(&general);
free_options(options);
return rv;
}
const char *hosts_path = get_option_arg(options, FL_FILE);
if (!hosts_path) {
dprintf(2, "ft_nmap: address/hostname required\n");
goto error;
}
FILE *hosts_file = fopen(hosts_path, "r");
if (hosts_file == NULL) {
dprintf(2, "ft_nmap: unable to open file '%s'\n", hosts_path);
goto error;
}
char line[256];
while (fgets(line, sizeof(line), hosts_file)) {
line[strcspn(line, "\n")] = '\0';
general.dest_addr = line;
if (scan_host(&general) < 0) {
fclose(hosts_file);
goto error;
}
}
fclose(hosts_file);
free_options(options);
return EXIT_SUCCESS;
error:
free_options(options);
return EXIT_FAILURE;
}