# irontec/sngrep architecture

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

- Interactive diagram: https://gitdiagram.com/irontec/sngrep
- Repository: https://github.com/irontec/sngrep

## Overview

##### Project type and purpose
- This is an open-source CLI application (sngrep) that captures network traffic (SIP/RTP), parses it, applies filters, and displays call-flow and message details in a curses-based UI.
- Key indicators: presence of src/capture_*.c, src/sip*.c, src/rtp.c, src/curses/*, pkg/* packaging files, and standard GNU build scripts.

##### File structure
- Top-level directories:
– src/               Core C modules (capture, parsing, filtering, UI, utils)
– src/curses/        UI components (panels, call flow/list/raw, stats, settings)
– config/            Default config (sngreprc)
– tests/             Unit tests and sample pcap files
– doc/               Man pages and HTML snippets
– pkg/               Packaging (apk, deb, rpm)
– build scripts: configure.ac, Makefile.am, CMakeLists.txt, bootstrap.sh
- Architectural pattern: layered, modular C code with clear separation of capture, parsing, filtering, session management, and presentation (MVC-like).

##### Extract additional insights (README incomplete)

1) Engine
- Modules: capture.c, capture_eep.c, capture_gnutls.c, capture_openssl.c
- Depends on libpcap, optionally GnuTLS/OpenSSL
2) Packet Parser & Session Manager
- packet.c, sip.c, sip_msg.c, sip_call.c, sip_attr.c, rtp.c
- Builds SIP/RTP sessions and attributes
3) Filter Engine
- filter.c
- Applies user-defined BPF/SIP filters
4) Data Models & Utilities
- .c, media.c, option.c, setting.c, util.c, hash.c, vector.c
5) UI Layer (ncurses)
- ui_manager, ui_call_list, ui_call_flow, ui_call_raw, ui_stats, ui_filter, ui_settings, etc.
- Keybindings (keybinding.c), panels, scrollbars
6) Configuration & Persistence
- config/sngreprc, src/setting.c, src/option.c
- Save captured sessions (ui_save.c)
7) Build & Packaging
- Autotools/CMake (configure.ac, Makefile.am), packaging under pkg/
8) Tests
- Unit tests invoking core modules against test pcap traces

- Engine → Packet Parser
- Packet Parser ↔ Filter Engine (filter may drop or pass packets)
- Filtered packets → Session Manager (groups, call/session state)
- UI Layer → Capture Engine (start/stop capture via commands)
- UI Layer → Filter Engine (update filter expressions at runtime)
- Configuration → Capture Engine, Filter Engine, UI Layer (load on startup)
- Tests → Individual modules (validate behavior)

c. Architectural patterns & principles
- Layered architecture: separation of capture, processing, and presentation
- Event-driven UI loop (ncurses manager dispatches keybindings)
- Modular design: each subsystem (capture, parsing, UI) in its own compile unit

d. Key technologies/frameworks/libraries
- libpcap (packet capture)
- ncurses (text UI)
- GnuTLS/OpenSSL (if encrypted capture support)
- Autotools/CMake (build system)
- Standard C (POSIX)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    subgraph "External Libraries"
        direction TB
        LibPcap["libpcap"]:::external
        Ncurses["ncurses"]:::external
        Crypto["GnuTLS/OpenSSL"]:::external
    end

    %% Core Engine Layers
    subgraph "Core Engine" 
        direction TB
        CaptureEng["Capture Engine"]:::core
        PacketParser["Packet Parser & Session Manager"]:::core
        FilterEng["Filter Engine"]:::core
        DataUtils["Data Models & Utilities"]:::core
    end

    %% UI Layer
    subgraph "UI Layer (ncurses)"
        direction TB
        UILayer["UI Manager & Panels"]:::ui
    end

    %% Configuration & Persistence
    Config["Configuration & Persistence"]:::core

    %% Build & Packaging
    BuildPkg["Build & Packaging"]:::build

    %% Tests
    Tests["Tests & Pcap Samples"]:::build

    %% Relationships
    CaptureEng -->|"packets"| PacketParser
    PacketParser -->|"parsed packets"| FilterEng
    FilterEng -->|"filtered packets"| PacketParser
    FilterEng -->|"passed packets"| DataUtils
    PacketParser -->|"session data"| DataUtils
    DataUtils -->|"session updates"| UILayer
    UILayer -->|"start/stop capture"| CaptureEng
    UILayer -->|"update filters"| FilterEng
    Config -->|"load settings"| CaptureEng
    Config -->|"load settings"| FilterEng
    Config -->|"load settings"| UILayer
    BuildPkg --> CaptureEng
    BuildPkg --> PacketParser
    BuildPkg --> FilterEng
    BuildPkg --> UILayer
    Tests --> CaptureEng
    Tests --> PacketParser
    Tests --> FilterEng
    Tests --> UILayer

    %% External integration
    CaptureEng --> LibPcap
    CaptureEng --> Crypto
    UILayer --> Ncurses

    %% Click Events
    click CaptureEng "https://github.com/irontec/sngrep/blob/master/src/capture.c"
    click PacketParser "https://github.com/irontec/sngrep/blob/master/src/packet.c"
    click FilterEng "https://github.com/irontec/sngrep/blob/master/src/filter.c"
    click DataUtils "https://github.com/irontec/sngrep/blob/master/src/group.c"
    click UILayer "https://github.com/irontec/sngrep/blob/master/src/curses/ui_manager.c"
    click Config "https://github.com/irontec/sngrep/tree/master/config/sngreprc"
    click BuildPkg "https://github.com/irontec/sngrep/blob/master/configure.ac"
    click Tests "https://github.com/irontec/sngrep/blob/master/tests/Makefile.am"

    %% Styles
    classDef core fill:#e0f7e9,stroke:#2e7d32,stroke-width:2px
    classDef ui fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
    classDef external fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
    classDef build fill:#eeeeee,stroke:#616161,stroke-width:2px
```
