/*
 * ==================================================================================
 * Main Application Entry Point - ICMP Covert Channel File Transfer System
 *
 * ISA Project
 * Author: Roman Necas (xnecasr00)
 * Date: 13. October 2025
 *
 * DESCRIPTION:
 * This is the main entry point for the ICMP covert channel file transfer application.
 * The program can operate in two modes:
 *
 * 1. CLIENT MODE: Reads a file, encrypts it using AES-256-CBC, and transmits it
 *    through ICMP Echo Request packets to a specified destination.
 *
 * 2. SERVER MODE: Listens for incoming ICMP Echo Request packets, receives encrypted
 *    data, decrypts it, and saves the file to disk.
 *
 * The application uses a custom protocol (IFTP - ICMP File Transfer Protocol) layered
 * on top of ICMP to provide file transfer capabilities through what appears to be
 * normal network diagnostic traffic.
 *
 * SECURITY:
 * - Files are encrypted with AES-256-CBC using the user's login name as the password
 * - PBKDF2 with 600,000 iterations is used for key derivation
 * - Each transfer uses unique IV (Initialization Vector) and salt values
 * - CRC32 checksums ensure packet integrity
 *
 * REQUIREMENTS:
 * - Root privileges (for raw socket access)
 * - OpenSSL library for cryptographic operations
 * - Linux operating system with raw socket support
 * ==================================================================================
 */

#include "secret.h"

/*
 * main - Application entry point
 *
 * Parses command-line arguments, initializes the error handling system,
 * and dispatches to either client or server mode based on the configuration.
 *
 * PROGRAM FLOW:
 * 1. Initialize error handling system
 * 2. Display program banner
 * 3. Parse and validate command-line arguments
 * 4. Execute client or server mode
 * 5. Report results and cleanup
 *
 * @param argc: Number of command-line arguments
 * @param argv: Array of command-line argument strings
 * @return: 0 on success, non-zero on failure
 */
int main(int argc, char *argv[]) {
    config_t config;
    int result = -1;

    /* Initialize error handling system for stderr output */
    error_init();

    /* Display minimal program banner */
    printf("ICMP Covert Channel File Transfer Tool\n");
    printf("========================================\n\n");

    /*
     * Parse command-line arguments
     * The parse_arguments() function validates all inputs and sets up the config
     * structure with either client mode (-r file -s host) or server mode (-l).
     * Returns > 0 if help was displayed, < 0 on error, 0 on success.
     */
    int parse_result = parse_arguments(argc, argv, &config);
    if (parse_result != 0) {
        if (parse_result > 0) {
            /* Help was displayed successfully */
            result = 0;
        } else {
            /* Parsing failed - error details already output to stderr */
            error_print_last();
        }
        goto cleanup;
    }

    /*
     * Execute appropriate mode based on configuration
     *
     * CLIENT MODE:
     * - Opens and reads the source file
     * - Initializes AES-256-CBC encryption with login-derived key
     * - Fragments file into chunks that fit in ICMP payload
     * - Transmits encrypted chunks via ICMP Echo Request
     * - Waits for ACK responses
     *
     * SERVER MODE:
     * - Opens raw ICMP socket for listening
     * - Receives ICMP Echo Request packets
     * - Extracts and validates IFTP protocol headers
     * - Reassembles fragmented packets
     * - Decrypts received data using login-derived key
     * - Writes file to disk
     * - Sends ICMP Echo Reply with ACK
     */
    if (config.mode == MODE_CLIENT) {
        printf("Mode: Client\n");
        result = client_run(&config);
    } else {
        printf("Mode: Server\n");
        result = server_run(&config);
    }

    /* Report final status to user */
    if (result == 0) {
        printf("\nOperation completed successfully.\n");
    } else {
        printf("\nOperation failed.\n");
        error_print_last();
    }

cleanup:
    /* Clean up allocated resources in configuration structure */
    cleanup_config(&config);

    /* Clean up error handling system */
    error_cleanup();

    return result;
}
