Skip to content
API Documentation
BastionGuard™
Wire.hpp
Go to the documentation of this file.
1/*
2 * BastionGuard™ Secure Connection
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 * GPL-3.0
5 *
6 * Protocollo wire daemon ↔ UI.
7 *
8 * Trasporto: Unix domain socket (SOCK_STREAM).
9 * Framing: 4 byte little-endian length (uint32) + payload JSON UTF-8.
10 * Nessun terminatore, nessun escape: la lunghezza e' l'unico marker.
11 *
12 * La UI e' sempre il server. Il daemon si connette come client.
13 *
14 * Ogni messaggio ha due campi obbligatori:
15 * "type": stringa, identifica il tipo di messaggio
16 * "id": uint64, correlazione per richieste/risposte
17 *
18 * Direzione daemon → UI:
19 * hello -> primo messaggio, { type, id, daemon_version }
20 * ping -> heartbeat, { type, id, stats: {...} }
21 * ask_rule -> richiede decisione utente, { type, id, connection: {...} }
22 * La UI risponde con ask_rule_reply con stesso id.
23 * alert -> notifica informativa, { type, id, level, what, text, connection? }
24 *
25 * Direzione UI → daemon:
26 * pong -> risposta a ping, { type, id }
27 * ask_rule_reply -> risposta a ask_rule, { type, id, action, duration, rule_name? }
28 * notification -> notifica runtime, { type, id, kind, data? }
29 * kind ∈ { stop, reload_rules, change_config, enable_interception,
30 * disable_interception, log_level, ... }
31 */
32
33#pragma once
34#include <cstdint>
35#include <cstring>
36#include <string>
37#include <string_view>
38#include <vector>
39#include <optional>
40
41namespace bsc::proto {
42
43// Path di default del socket Unix della UI.
44inline constexpr const char* kDefaultSocketPath = "/tmp/bsd-daemon.sock";
45
46// Tipi di messaggi.
47namespace msg {
48 inline constexpr std::string_view kHello = "hello";
49 inline constexpr std::string_view kPing = "ping";
50 inline constexpr std::string_view kPong = "pong";
51 inline constexpr std::string_view kAskRule = "ask_rule";
52 inline constexpr std::string_view kAskRuleReply = "ask_rule_reply";
53 inline constexpr std::string_view kAlert = "alert";
54 inline constexpr std::string_view kNotification = "notification";
55} // namespace msg
56
57// Risoluzione socketPath: accetta sia "/tmp/foo.sock" sia "unix:///tmp/foo.sock".
58inline std::string resolve_socket_path(std::string uri) {
59 static const std::string prefix = "unix://";
60 if (uri.rfind(prefix, 0) == 0) uri = uri.substr(prefix.size());
61 return uri;
62}
63
64// Codifica un payload JSON in un frame [uint32 LE length][payload]. Inserisce
65// nel vettore `out` senza allocare di piu'.
66inline void frame_encode(std::string_view payload, std::vector<std::uint8_t>& out) {
67 const std::uint32_t len = static_cast<std::uint32_t>(payload.size());
68 out.resize(4 + payload.size());
69 out[0] = static_cast<std::uint8_t>(len & 0xff);
70 out[1] = static_cast<std::uint8_t>((len >> 8) & 0xff);
71 out[2] = static_cast<std::uint8_t>((len >> 16) & 0xff);
72 out[3] = static_cast<std::uint8_t>((len >> 24) & 0xff);
73 std::memcpy(out.data() + 4, payload.data(), payload.size());
74}
75
76// Legge 4 byte di lunghezza dal buffer. Restituisce -1 se il buffer non ha
77// almeno 4 byte.
78inline std::int64_t frame_peek_length(const std::uint8_t* buf, std::size_t len) {
79 if (len < 4) return -1;
80 return static_cast<std::uint32_t>(buf[0])
81 | (static_cast<std::uint32_t>(buf[1]) << 8)
82 | (static_cast<std::uint32_t>(buf[2]) << 16)
83 | (static_cast<std::uint32_t>(buf[3]) << 24);
84}
85
86// Dimensione massima accettabile di un frame (DoS guard). 4 MiB e' ampiamente
87// sufficiente per i messaggi del protocollo: AskRule col process tree piu'
88// grande mai visto sta sotto i 64 KiB.
89inline constexpr std::size_t kMaxFrameSize = 4u * 1024u * 1024u;
90
91} // namespace bsc::proto
constexpr std::string_view kHello
Definition Wire.hpp:48
constexpr std::string_view kAskRuleReply
Definition Wire.hpp:52
constexpr std::string_view kPing
Definition Wire.hpp:49
constexpr std::string_view kAskRule
Definition Wire.hpp:51
constexpr std::string_view kPong
Definition Wire.hpp:50
constexpr std::string_view kNotification
Definition Wire.hpp:54
constexpr std::string_view kAlert
Definition Wire.hpp:53
std::string resolve_socket_path(std::string uri)
Definition Wire.hpp:58
constexpr std::size_t kMaxFrameSize
Definition Wire.hpp:89
constexpr const char * kDefaultSocketPath
Definition Wire.hpp:44
std::int64_t frame_peek_length(const std::uint8_t *buf, std::size_t len)
Definition Wire.hpp:78
void frame_encode(std::string_view payload, std::vector< std::uint8_t > &out)
Definition Wire.hpp:66