# erdogancayir/ft_nmap architecture

> Architecture diagram of the GitHub repository erdogancayir/ft_nmap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/erdogancayir/ft_nmap
- Repository: https://github.com/erdogancayir/ft_nmap

## Overview

##### Project type and purpose
- This is a command-line, open-source network port scanner implemented in C (ft_nmap), inspired by nmap.
- Its purpose is to perform multiple TCP/UDP scan types, capture and analyze packets in real time using libpcap, and report port and service information.

##### File structure
- Top-level files:
- Makefile – build rules
- README.md – usage, features, architecture
- ip_targets.txt – sample target list
- inc/ – public headers defining data structures (job_queue, scan_config, scan_result, scan_type, tcp, udp)
- src/cli/ – command-line parsing and config freeing
- src/debug_files/ – helpers to dump internal config, jobs, results, packets
- src/main.c – program entry point
- src/network/ – raw-socket actions and banner grabbing (address resolution, source IP selection, OS guess, send TCP/UDP)
- src/pcap/ – packet capture/sniffer thread and packet handlers (ICMP/TCP/UDP), result aggregation
- src/worker_thread_pool/ – thread-pool implementation (job queue, worker threads, thread pool startup)

##### README’s architecture section
- Core components: Job Queue, Packet Capture, Scanning Engine
- Threading model: Main thread (producer), Worker threads (consumers), Sniffer thread

##### Key elements to capture in detail
- Separation of concerns: CLI vs. Engine vs. Capture vs. Results
- Threading boundaries: which code runs in main, workers, sniffer
- Producer-consumer interactions and synchronization (mutexes/conds implied)
- Where packet crafting occurs vs. where packet parsing occurs

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Interfaces
    RawSocket["Raw Sockets"]:::external
    PCAP["libpcap"]:::external

    %% Program Entry and CLI
    Main["Program Entry\n(src/main.c)"]:::core
    Parser["CLI Parser\n(src/cli/cli_parser.c)"]:::cli
    FreeConfig["Config Freeing\n(src/cli/free_config.c)"]:::cli
    ConfigStruct["Scan Config Struct\n(inc/scan_config.h)"]:::data

    %% Job Queue and Thread Pool
    QueueAPI["Job Queue API\n(inc/job_queue.h)"]:::core
    QueueImpl["Job Queue Impl\n(src/worker_thread_pool/job_queue.c)"]:::core
    ThreadPoolMgr["Thread Pool Manager\n(src/worker_thread_pool/start_thread_pool.c)"]:::thread
    WorkerThread["Worker Thread\n(src/worker_thread_pool/worker_thread.c)"]:::thread
    SnifferThread["Sniffer Thread\n(src/pcap/sniffer_thread.c)"]:::thread

    %% Network Layer
    subgraph "Network Abstraction"
        PacketCrafterTCP["Send TCP Packet\n(src/network/send_tcp_packet.c)"]:::network
        PacketCrafterUDP["Send UDP Packet\n(src/network/send_udp_packet.c)"]:::network
        subgraph "Network Helpers"
            Resolver["Address Resolver\n(src/network/adress_resolver.c)"]:::network
            SourceIP["Find Source IP\n(src/network/find_source_ip.c)"]:::network
            HostResolver["Hostname Resolver\n(src/network/hostname_resolver.c)"]:::network
            BannerGrab["Grab Banner\n(src/network/grab_banner.c)"]:::network
            OSGuess["Guess OS\n(src/network/guess_os.c)"]:::network
        end
    end

    %% Packet Capture and Handlers
    subgraph "Packet Capture"
        PacketSniffer["Packet Sniffer\n(src/pcap/sniffer_thread.c)"]:::thread
        PacketSniffer -->|captures| PCAP
        subgraph "Packet Handlers"
            ICMPHandler["ICMP Handler\n(src/pcap/packet_handlers/handle_icmp_packet.c)"]:::core
            TCPHandler["TCP Handler\n(src/pcap/packet_handlers/handle_tcp_packet.c)"]:::core
            UDPHandler["UDP Handler\n(src/pcap/packet_handlers/handle_udp_packet.c)"]:::core
            GenericHandler["Generic Handler\n(src/pcap/packet_handlers/handle_packet.c)"]:::core
        end
    end

    %% Shared Results and Aggregation
    SharedResults["Shared Results Store\n(inc/scan_result.h\nsrc/pcap/scan_results/shared_results.c)"]:::data
    AddResult["Add Scan Result\n(src/pcap/scan_results/add_scan_result.c)"]:::core
    Finalize["Finalize Unanswered Jobs\n(src/pcap/scan_results/finalize_unanswered_jobs.c)"]:::core
    PrintResults["Print Results\n(src/pcap/scan_results/print_results.c)"]:::core

    %% Data Flow
    Main --> Parser
    Parser --> ConfigStruct
    Parser --> FreeConfig
    ConfigStruct --> QueueAPI
    QueueAPI --> QueueImpl
    QueueImpl --> ThreadPoolMgr
    ThreadPoolMgr --> WorkerThread
    ThreadPoolMgr --> PacketSniffer

    WorkerThread --> PacketCrafterTCP
    WorkerThread --> PacketCrafterUDP
    PacketCrafterTCP --> RawSocket
    PacketCrafterUDP --> RawSocket

    WorkerThread --> Resolver
    WorkerThread --> SourceIP
    WorkerThread --> HostResolver
    WorkerThread --> BannerGrab
    WorkerThread --> OSGuess

    PacketSniffer --> ICMPHandler
    PacketSniffer --> TCPHandler
    PacketSniffer --> UDPHandler
    PacketSniffer --> GenericHandler

    ICMPHandler --> SharedResults
    TCPHandler --> SharedResults
    UDPHandler --> SharedResults
    GenericHandler --> SharedResults

    WorkerThread --> SharedResults

    SharedResults --> AddResult
    AddResult --> Finalize
    Finalize --> PrintResults

    %% Click Events
    click Main "https://github.com/erdogancayir/ft_nmap/blob/main/src/main.c"
    click Parser "https://github.com/erdogancayir/ft_nmap/blob/main/src/cli/cli_parser.c"
    click FreeConfig "https://github.com/erdogancayir/ft_nmap/blob/main/src/cli/free_config.c"
    click ConfigStruct "https://github.com/erdogancayir/ft_nmap/blob/main/inc/scan_config.h"
    click QueueAPI "https://github.com/erdogancayir/ft_nmap/blob/main/inc/job_queue.h"
    click QueueImpl "https://github.com/erdogancayir/ft_nmap/blob/main/src/worker_thread_pool/job_queue.c"
    click ThreadPoolMgr "https://github.com/erdogancayir/ft_nmap/blob/main/src/worker_thread_pool/start_thread_pool.c"
    click WorkerThread "https://github.com/erdogancayir/ft_nmap/blob/main/src/worker_thread_pool/worker_thread.c"
    click PacketCrafterTCP "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/send_tcp_packet.c"
    click PacketCrafterUDP "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/send_udp_packet.c"
    click Resolver "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/adress_resolver.c"
    click SourceIP "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/find_source_ip.c"
    click HostResolver "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/hostname_resolver.c"
    click BannerGrab "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/grab_banner.c"
    click OSGuess "https://github.com/erdogancayir/ft_nmap/blob/main/src/network/guess_os.c"
    click PacketSniffer "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/sniffer_thread.c"
    click ICMPHandler "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/packet_handlers/handle_icmp_packet.c"
    click TCPHandler "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/packet_handlers/handle_tcp_packet.c"
    click UDPHandler "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/packet_handlers/handle_udp_packet.c"
    click GenericHandler "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/packet_handlers/handle_packet.c"
    click SharedResults "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/scan_results/shared_results.c"
    click AddResult "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/scan_results/add_scan_result.c"
    click Finalize "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/scan_results/finalize_unanswered_jobs.c"
    click PrintResults "https://github.com/erdogancayir/ft_nmap/blob/main/src/pcap/scan_results/print_results.c"

    %% Styles
    classDef cli fill:#D6EAF8,stroke:#2980B9,color:#17202A;
    classDef core fill:#D5F5E3,stroke:#27AE60,color:#145A32;
    classDef data fill:#FCF3CF,stroke:#F1C40F,color:#7D6608;
    classDef thread fill:#FADBD8,stroke:#E74C3C,color:#641E16;
    classDef network fill:#E8F8F5,stroke:#1ABC9C,color:#0E6251;
    classDef external fill:#F5EEF8,stroke:#8E44AD,color:#512E5F;
```
