This code implements a simple command-line utility written in C...

September 4, 2025 at 12:14 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 host/IP: "); 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; } char *argv[] = {"ping", "-c", "1", dest, NULL}; eseguiComando(argv); } void traceroute() { char dest[128]; printf("Inserisci host/IP: "); fgets(dest, sizeof(dest), stdin); dest[strcspn(dest, "\n")] = 0; if (!validaInput(dest)) { printf("Input non valido!\n"); return; } char *argv[] = {"traceroute", "-I", dest, NULL}; eseguiComando(argv); } void controllaPorta() { char dest[128]; printf("Inserisci host/IP: "); fgets(dest, sizeof(dest), stdin); dest[strcspn(dest, "\n")] = 0; if (!validaInput(dest)) { printf("Input non valido!\n"); return; } 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 simple command-line utility written in C that allows the user to perform network-related operations by launching system commands. Here's a step-by-step breakdown of what it does:

1. Introduction and Library Imports

  • The code imports libraries such as <stdio.h> (for I/O), <stdlib.h> (for memory management and process control), <string.h> (for string manipulation), <regex.h> (for regular expressions), <unistd.h> (for process control), and <sys/wait.h> (for waiting on child processes).

2. Features of the Program

The program offers the following functionalities:

  • Ping: Checks the availability of a specific host or IP address.
  • Traceroute: Traces the path packets take to reach a specified host or IP address.
  • Check Port 80: Verifies if port 80 on a specific host or IP address is open.

These operations are integrated into a menu-based system.


3. Key Components of the Code

a. validaInput: Input Validation

  • Uses regular expressions (regex) to validate user input.
  • Ensures the user input (host/IP) contains only valid characters: alphanumeric characters, underscores (_), hyphens (-), and dots (.).
  • Returns 1 if the input is valid; otherwise 0.

b. eseguiComando: Command Execution

  • Executes a command in a child process using execvp.
  • Uses fork to create a child process:
    • The child process replaces itself with the requested command (execvp).
    • The parent process waits for the child process to complete using wait.
  • Handles errors during both fork and execvp.

c. ping: Ping Functionality

  • Prompts the user for an IP address or host.
  • Validates the input using validaInput.
  • If valid, uses ping command with -c 1 to send a single ICMP echo request to the target.
  • Executes the command using eseguiComando.

d. traceroute: Traceroute Functionality

  • Similar to the ping function but uses the traceroute command with the -I flag.

e. controllaPorta: Port 80 Check

  • Similar to ping but uses the nc (netcat) command with the flags -zv to check if port 80 is open on the specified host.

4. main: Menu System

  • Implements a simple menu loop allowing the user to:
    1. Ping a host.
    2. Perform a traceroute.
    3. Check if port 80 is open.
    4. Exit the program.
  • The menu is displayed in a loop; the user selects an option by entering a number.
  • Based on the user's choice, the corresponding function is called.

5. How It Works

When this program runs:

  1. It displays a menu with options for ping, traceroute, and port 80 check.
  2. The user chooses an option, and the program:
    • Prompts for a host or IP address.
    • Validates the input using a regular expression.
    • If valid, executes the corresponding system command (ping, traceroute, or nc) using a child process.
  3. If the input is invalid or the command fails, appropriate error messages are displayed.
  4. The program keeps running until the user selects option 4 to exit.

6. Potential Issues and Considerations

  • Security concerns:
    • Although there is input validation, command-line injection vulnerabilities could still exist (e.g., if nc, ping, or traceroute execution is affected by untrusted input due to additional system configurations).
  • Dependency on external tools:
    • The program relies on system commands like ping, traceroute, and nc. If these are not installed on the system, the program will fail.
  • Platform-specific functionality:
    • Commands like traceroute and nc may behave differently on different systems (e.g., Linux vs. macOS).
  • Error handling:
    • The program doesn't handle all edge cases (e.g., invalid menu input like a non-numeric character for the menu option).

7. Summary

The code is a C program that acts as a simple network diagnostic tool with options to:

  • Ping a host to check its availability.
  • Run traceroute to trace the path to a host.
  • Verify if port 80 is open on a host. It leverages basic concepts such as regular expressions for input validation, process creation via fork, and system command execution using execvp.
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