42_ft_nmap/src/print.c
0x35c 31530c83a4 fix: data races with getservent
feature: time elapsed in the print
2025-06-05 11:10:42 +02:00

91 lines
2.8 KiB
C

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "response.h"
#include "scan.h"
void print_usage(void)
{
printf("--help: Print this help screen\n");
printf("--ports: ports to scan (eg: 42-69 or 666)\n");
printf("--ip: ip address to scan in dot format\n");
printf("--file: File name containing IP addresses to scan\n");
printf("--speedup: [250 max] number of parallel threads to use\n");
printf("--scan: NULL/SYN/FIN/XMAS/ACK/UDP\n");
}
void print_config(const struct scan *general, const char *hosts_path,
uint8_t nb_threads)
{
printf("Scan configurations\n");
if (hosts_path)
printf("Target hosts list filename: %s\n", hosts_path);
else
printf("Target ip address: %s\n", general->dest_addr);
if (general->port_start != general->port_end)
printf("Number of scans to be performed: %d\n",
general->port_end - general->port_start + 1);
printf("Scans to be performed: ");
if (general->type == SCAN_ALL)
for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++)
printf("%s ", types_str[type]);
else
printf("%s", types_str[general->type]);
printf("\n");
if (nb_threads > 1)
printf("No of threads: %d\n", nb_threads);
}
bool is_port_opened(const e_state states[6], e_scantype type)
{
if (type == SCAN_ALL) {
for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++)
if (states[i] == OPENED)
return true;
return false;
}
return states[type] == OPENED;
}
static void print_port_state(uint16_t port, e_scantype type,
const struct response *response)
{
printf("%-5d %-12s ", port,
response->service ? response->service : "Unassigned");
if (response->service)
free(response->service);
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 struct scan *general, double scan_time)
{
printf("IP address: %s\n", general->dest_addr);
printf("Opened ports:\n");
for (uint16_t port = general->port_start; port < general->port_end;
port++) {
const struct response *response =
&general->responses[port - general->port_start];
if (is_port_opened(response->states, general->type))
print_port_state(port, general->type, response);
}
printf("\n");
printf("Closed/filtered/unfiltered ports:\n");
for (uint16_t port = general->port_start; port <= general->port_end;
port++) {
const struct response *response =
&general->responses[port - general->port_start];
if (!is_port_opened(response->states, general->type))
print_port_state(port, general->type, response);
}
printf("\nScan took %lf secs\n", scan_time);
}