From d640c95224a4bfc673c11cdfc307984f7b7235bb Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Tue, 3 Jun 2025 13:05:52 +0200 Subject: [PATCH] feature: print (wip) --- include/print.h | 5 +++++ include/scan.h | 4 ++++ src/main.c | 21 +-------------------- src/parsing.c | 8 +++----- src/print.c | 38 ++++++++++++++++++++++++++++++++++++-- src/response.c | 6 ++++-- src/thread.c | 2 +- 7 files changed, 54 insertions(+), 30 deletions(-) diff --git a/include/print.h b/include/print.h index 711f5a2..125f8fb 100644 --- a/include/print.h +++ b/include/print.h @@ -1,3 +1,8 @@ #pragma once +#include "parsing.h" +#include "response.h" + +void print_host_results(const char *ip_addr, const struct response *responses, + const struct option_lst *options, double scan_time); void print_usage(void); diff --git a/include/scan.h b/include/scan.h index ad37ff0..46d7735 100644 --- a/include/scan.h +++ b/include/scan.h @@ -14,6 +14,10 @@ typedef enum { SCAN_ALL, } e_scantype; +[[__maybe_unused__]] static const char *types_str[] = { + "NULL", "SYN", "ACK", "FIN", "XMAS", "UDP", +}; + struct scan { const struct host *host; const char *dest_addr; diff --git a/src/main.c b/src/main.c index 1f430b6..6d8ed92 100644 --- a/src/main.c +++ b/src/main.c @@ -24,29 +24,10 @@ static int scan_host(char *host, const struct option_lst *options) host); return 1; } - - printf("%s\n", ip_addr); struct response responses[1024] = {0}; if (create_threads(options, ip_addr, responses) < 0) return 1; - static const char *types_str[] = { - "NULL", "SYN", "ACK", "FIN", "XMAS", "UDP", - }; - for (uint16_t i = 0; i < 1024; i++) { - const e_scantype type = SCAN_SYN; - if (responses[i].states[type] == CLOSED) - continue; - printf("%d (%s): ", responses[i].port, - responses[i].service ? responses[i].service - : "undefined"); - if (responses[i].service) - free(responses[i].service); - // for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) { - printf("%s(%s) ", types_str[type], - states_str[responses[i].states[type]]); - // } - printf("\n"); - } + print_host_results(ip_addr, responses, options, 10); return 0; } diff --git a/src/parsing.c b/src/parsing.c index 85e80f3..0e54da2 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -8,6 +8,7 @@ #include #include "parsing.h" +#include "scan.h" void free_options(struct option_lst *options) { @@ -41,14 +42,10 @@ char *get_option_arg(const struct option_lst *options, e_flag flag) e_scantype parse_type(const char *arg) { - const char *types[] = { - "NULL", "SYN", "ACK", "FIN", "XMAS", "UDP", - }; - if (!arg) return SCAN_ALL; for (size_t i = 0; i < 6; i++) - if (!strcmp(arg, types[i])) + if (!strcmp(arg, types_str[i])) return i; return -1; } @@ -201,6 +198,7 @@ struct option_lst *parse_options(int ac, char *const *av) {"scan", required_argument, 0, 0}, {"max_retries", required_argument, 0, 0}, {"ttl", required_argument, 0, 0}, + {"open", no_argument, 0, 0}, }; int c; diff --git a/src/print.c b/src/print.c index 17ee1df..f001209 100644 --- a/src/print.c +++ b/src/print.c @@ -1,6 +1,40 @@ -#include "print.h" +#include + +#include "parsing.h" +#include "response.h" +#include "scan.h" void print_usage(void) { - // TODO print options list + // TODO +} + +static void print_port_state(const struct response *response, e_scantype type) +{ + if (type != SCAN_ALL && response->states[type] == CLOSED) + return; + printf("%-5d %-12s ", response->port, + response->service ? response->service : "Unassigned"); + if (type == SCAN_ALL) + for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++) + printf("%s(%s) ", types_str[i], + states_str[response->states[i]]); + else + printf("%s(%s) ", types_str[type], + states_str[response->states[type]]); + printf("\n"); +} + +void print_host_results(const char *ip_addr, const struct response *responses, + const struct option_lst *options, double scan_time) +{ + printf("Scan took %lf secs\n", scan_time); + printf("IP address: %s\n", ip_addr); + printf("Open ports:\n"); + uint16_t port_start = 1, port_end = 1024; + parse_ports(get_option_arg(options, FL_PORTS), &port_start, &port_end); + e_scantype type = parse_type(get_option_arg(options, FL_SCAN)); + for (uint16_t i = port_start; i < port_end - port_start; i++) { + print_port_state(&responses[i], type); + } } diff --git a/src/response.c b/src/response.c index c194ae9..4deb8d1 100644 --- a/src/response.c +++ b/src/response.c @@ -28,7 +28,8 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) "scan\n"); return; } - data->response->service = get_service_name(data->port, "tcp"); + if (data->response->service == NULL) + data->response->service = get_service_name(data->port, "tcp"); if (type == SCAN_SYN) { if (tcphdr->ack == 1 && tcphdr->syn == 1) data->response->states[type] = OPENED; @@ -52,7 +53,8 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data) "scan\n"); return; } - data->response->service = get_service_name(data->port, "udp"); + if (data->response->service == NULL) + data->response->service = get_service_name(data->port, "udp"); data->response->states[SCAN_UDP] = OPENED; } diff --git a/src/thread.c b/src/thread.c index aeb5cc7..5bc2f1a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -106,7 +106,7 @@ int create_threads(const struct option_lst *options, char *ip_addr, } uint8_t nb_threads = atoi(nb_threads_str); - if (port_end - port_start < nb_threads) { + if (port_end - 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;