UIClient.cpp

1. Overview

Unix-socket UI protocol client. Maintains a reconnecting framed JSON connection to the desktop UI, queues alerts while disconnected, dispatches notifications and correlates synchronous rule responses.

The original source path is:

src/ui/UIClient.cpp

2. Role in the Daemon

This file participates in the privileged BastionGuard Secure Connection daemon. It uses the shared bsc models and follows the daemon-wide fallback strategy: optional high-fidelity backends are used when available, while portable polling or stub behavior preserves service startup.


3. Namespaces

  • bsc::ui

4. Dependencies and Included Interfaces

  • bsc/ui/UIClient.hpp
  • bsc/logging/Logger.hpp
  • bsc/proto/Wire.hpp
  • nlohmann/json.hpp
  • atomic
  • chrono
  • condition_variable
  • cstring
  • deque
  • errno.h
  • mutex
  • poll.h
  • sys/socket.h
  • sys/un.h
  • thread
  • unistd.h
  • unordered_map

5. Declared Types

  • class UIClient
  • struct PendingAsk

6. API and Functions

The file declares or implements the following callable entry points:

  • connection_to_json(const bsc::model::Connection& c, json& j)
  • op_type_from_string(const std::string& s)
  • operator_from_json(const json& j)
  • rule_from_json(const json& j)
  • Impl(std::string uri) : socket_path_(bsc::proto::resolve_socket_path(std::move(uri)))
  • ~Impl()
  • start()
  • stop()
  • is_connected()
  • connect_once()
  • reconnect_loop()
  • write_frame(const std::string& payload)
  • read_exact(int fd, std::uint8_t* buf, std::size_t n)
  • read_until_eof()
  • dispatch_incoming(const json& j)
  • send_hello()
  • flush_queued_alerts()
  • send_or_queue_alert(const json& j)
  • UIClient::UIClient(std::string socket_uri) : impl_(std::make_unique<Impl>(std::move(socket_uri)))
  • UIClient::start()
  • UIClient::stop()
  • UIClient::is_connected()
  • UIClient::set_notification_handler(std::function<void(const NotificationEvent&)> cb)
  • UIClient::ask_rule(const bsc::model::Connection& conn)
  • UIClient::ping(const bsc::stats::Statistics& stats)
  • UIClient::post_alert(const std::string& text)
  • UIClient::post_connection_alert(const bsc::model::Connection& conn, const std::string& action, const std::string& rule_name)
  • int connect_once()

7. Constants and Compile-Time Controls

  • std::int64_t len = bsc::proto::frame_peek_length(len_buf, 4)
  • std::string type = j.value("type", std::string{})
  • auto id = j.value("id", std::uint64_t{0})
  • std::string payload = j.dump()
  • auto id = impl_->next_id_++
  • bool ok = pending->cv.wait_for(lk, std::chrono::seconds(30), [&]() { return pending->ready
  • std::string& action, const std::string& rule_name) { json j = { {"type", bsc::proto::msg::kAlert}, {"id", impl_->next_id_++}, {"level", action == "deny" ? "warning" : "info"}, {"what", "connection"}, }

8. Operational Flow

  1. Resolve the configured unix:// endpoint and continuously attempt reconnection.
  2. Exchange length-prefixed JSON frames and send an initial hello message.
  3. Queue alerts while disconnected and flush them after reconnection.
  4. Dispatch asynchronous notifications and correlate user rule decisions with waiting requests.

9. Wire Protocol

The client uses a Unix-domain socket resolved from the configured URI and exchanges framed JSON messages. It sends hello, ping and alert documents, receives daemon-control notifications and waits for rule responses associated with connection prompts.


10. External Resources and Protocol Values

Relevant paths, endpoints, command names or policy values referenced by this file include:

  • bsc/ui/UIClient.hpp
  • bsc/logging/Logger.hpp
  • bsc/proto/Wire.hpp
  • allow

11. Concurrency and Lifecycle

  • Uses one or more worker threads.
  • Protects mutable shared data with std::mutex.
  • Uses atomic flags for lifecycle or activity state.
  • Uses a condition variable for request/response or worker coordination.

12. Security Considerations

  • The Unix socket is a privileged decision channel; filesystem permissions and peer authentication are critical.
  • Synchronous UI decisions must tolerate disconnects and timeouts without deadlocking packet processing.
  • Connection payloads can contain executable paths, arguments and environment metadata.

13. Implementation Considerations

  • Shared state is protected by mutexes; callers should avoid holding external locks while invoking callbacks.
  • The file creates or manages worker threads and requires deterministic stop/join behavior during daemon shutdown.
  • Atomic flags are used for low-cost lifecycle or activity state coordination.
  • JSON input is an administrative trust boundary; type errors and malformed documents must remain contained.

14. Licensing and Attribution

  • Copyright (C) 2025–2026 Calogero Scarnà
  • GPL-3.0

15. Source Metrics

MetricValue
File size17012 bytes
Lines485
Detected API entries28
Detected types2