This code implements a utility program in C that provides...

September 4, 2025 at 12:16 AM

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <regex.h> #include <sys/wait.h> // Funzione per validare l'input int validaInput(const char *input) { regex_t regex; int controllo = regcomp(&regex, "^[a-zA-Z0-9.]+$", REG_EXTENDED); if (controllo) { fprintf(stderr, "Errore compilazione regex\n"); return 0; } else { controllo = regexec(&regex, input, 0, NULL, 0); regfree(&regex); if (!controllo) { return 1; } else { return 0; } } } // Funzione per eseguire i comandi void eseguiComando(char *argv[]) { // Avvio un processo figlio per eseguire execvp pid_t pid = fork(); if (pid == 0) { execvp(argv[0], argv); perror("execvp"); exit(1); } else if (pid > 0) { // Processo padre atende la fine del processo figlio wait(NULL); } else { perror("fork"); } } // Funzione per controllare se il destinatario e online void ping() { char dest[128]; printf("Inserisci IP o Nome Destinazione: "); fgets(dest, sizeof(dest), stdin); dest[strcspn(dest, "\n")] = 0; // Controllo se il input e valido if (!validaInput(dest)) { printf("Input non valido!\n"); return; } // Creo il comando da eseguire e lo passo alla funzione eseguiCOmando char *argv[] = {"ping", "-c", "1", dest, NULL}; eseguiComando(argv); } // Funzione per tracciare il percorso fra utente e dest void traceroute() { char dest[128]; printf("Inserisci IP o Nome Destinazione: "); fgets(dest, sizeof(dest), stdin); dest[strcspn(dest, "\n")] = 0; // Controllo se il input e valido if (!validaInput(dest)) { printf("Input non valido!\n"); return; } // Creo il comando da eseguire e lo passo alla funzione eseguiCOmando char *argv[] = {"traceroute", "-I", dest, NULL}; eseguiComando(argv); } // Funzione per controllare la possibilita di inviare pacchetti fra utente e dest void controllaPorta() { char dest[128]; printf("Inserisci IP o Nome Destinazione: "); fgets(dest, sizeof(dest), stdin); dest[strcspn(dest, "\n")] = 0; // Controllo se il input e valido if (!validaInput(dest)) { printf("Input non valido!\n"); return; } // Creo il comando da eseguire e lo passo alla funzione eseguiCOmando char *argv[] = {"nc", "-zv", dest, "80", NULL}; eseguiComando(argv); } int main() { int scelta; while (1) { printf("\nMenu\n"); printf("1. Ping\n"); printf("2. Traccia Percorso\n"); printf("3. Controlla Porta 80\n"); printf("4. Esci\n"); printf("Scelta: "); scanf("%d", &scelta); getchar(); switch (scelta) { case 1: ping(); break; case 2: traceroute(); break; case 3: controllaPorta(); break; case 4: exit(0); default: printf("Scelta non valida.\n"); } } }

This code implements a utility program in C that provides network-related functionalities such as ping, traceroute, and checking if a port (specifically port 80) is open on a target destination. Here's what the code does in detail:

Components of the Code:

  1. Input Validation (validaInput):

    • The function validaInput checks if the user's input (destination address or domain) matches a specific pattern using regular expressions.
    • It validates that the input only contains alphanumeric characters (letters and digits), dots (.), making it a valid hostname or IP address.
    • If the pattern is valid, it returns 1 (true), otherwise, it returns 0 (false).
  2. Child Processes for Command Execution (eseguiComando):

    • The function eseguiComando takes an array of arguments (a command and its parameters) and executes it using the execvp system call.
    • It forks a child process using fork(). The child process runs the desired command, and the parent process waits for it to complete.
    • If there's an error in command execution, an error message is printed using perror.
  3. ping Function:

    • The user is prompted to input an IP address or hostname.
    • If the input is valid, the program builds a command to ping the destination (ping -c 1 <dest>), which sends one ICMP Echo Request to the destination.
    • The command is executed using eseguiComando.
  4. traceroute Function:

    • Similar to ping, the user is asked to input a destination address.
    • If valid, the program builds a command to perform a trace route (traceroute -I <dest>) using the traceroute utility.
    • The command is executed using eseguiComando.
  5. controllaPorta Function:

    • This function checks if port 80 is accessible/open on the given destination.
    • It uses the nc (Netcat) command in verbose mode (-zv) to scan port 80 of the destination (nc -zv <dest> 80).
    • The command is executed using eseguiComando.
  6. main Function and Menu:

    • The main function implements a looped menu system, allowing the user to repeatedly select one of the following options:
      1. Ping: Calls the ping function.
      2. Traccia Percorso (Trace Route): Calls the traceroute function.
      3. Controlla Porta 80: Calls the controllaPorta function to check port 80.
      4. Esci (Exit): Exits the program.
    • The user's choice is read as input, and based on the selection, the appropriate function is executed.

Key Notes:

  • Safety: The program validates user inputs to ensure only valid hostnames or IP addresses are processed.
  • Compatibility: The code assumes the presence of standard network utilities like ping, traceroute, and nc on the system.
  • Privileges: Some commands like traceroute might require superuser privileges, depending on the system configuration.
  • Error Handling: Basic error handling is implemented (e.g., handling invalid inputs, failed process creation, or command execution errors).

Summary:

This program is a utility for performing basic network diagnostics. It allows the user to:

  • Ping a destination to check its availability.
  • Trace the route to a destination to analyze the network path.
  • Check whether port 80 on a specific destination is open.

By using a menu-based interface, users can execute these functionalities interactively in a loop until they choose to exit the program.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node