UIServer

1. Overview

Local IPC server that listens on a Unix-domain socket, exchanges framed JSON messages with the BastionGuard daemon, converts messages into typed connection and alert structures, sends verdict replies, queues notifications, and dispatches events onto the GTK main thread.

File role: Implementation. The file contains 426 lines and belongs to the ipc/UIServer.cpp path.

The companion file is ipc/UIServer.hpp.


2. Component Responsibilities

  • Implement the behavior declared by the corresponding header or entry-point contract.
  • Normalize and bind the Unix-socket path
  • Accept the daemon connection in a worker thread
  • Read length-prefixed frames
  • Parse JSON messages into ConnectionInfo or AlertInfo
  • Dispatch UI signals through GLib idle/dispatcher mechanisms
  • Send rule replies and queued notifications
  • Detect disconnects and return to the accept loop

3. Dependencies and Integration

Project-level dependencies:

  • UIServer.hpp
  • ../util/AppResolver.hpp
  • bsc/proto/Wire.hpp
  • glibmm/main.h
  • nlohmann/json.hpp
  • errno.h
  • sys/socket.h
  • sys/stat.h
  • sys/un.h
  • unistd.h

System and library dependencies:

  • atomic
  • chrono
  • condition_variable
  • cstring
  • deque
  • filesystem
  • iostream
  • mutex
  • thread

4. Declared Types

UIServer

Declares a class derived from or associated with :Impl.


5. Signals and Event Integration

The file exposes or connects the following signal-related elements:

  • void dispatch_connected() { Glib::signal_idle().connect_once([this]() { owner_->signal_daemon_connected.emit(); }); }
  • void dispatch_disconnected() { Glib::signal_idle().connect_once([this]() { owner_->signal_daemon_disconnected.emit(); }); }
  • Glib::signal_idle().connect_once([this, info]() { owner_->signal_new_connection.emit(info); });
  • Glib::signal_idle().connect_once([this, a]() { owner_->signal_alert.emit(a); });

GTK-facing callbacks must execute on the main UI context. Components with worker threads use dispatchers or idle callbacks to cross the thread boundary.


6. Functions and Methods

The file exposes or implements 17 detected function or method signatures.

1. UIServer

UIServer::UIServer() : impl_(std::make_unique<Impl>(this))

Implements the uiserver operation declared by this component.

2. ~UIServer

UIServer::~UIServer()

Releases resources owned by the object and stops any active background integration.

3. start

void UIServer::start(const std::string& socketPath)

Starts start and activates the associated runtime resources.

4. stop

void UIServer::stop()

Stops stop and releases or signals its active resources.

5. sendReply

void UIServer::sendReply(ReplyAction action)

Sends reply to the connected peer or subsystem.

6. queueNotification

void UIServer::queueNotification(int type, const std::string& data)

Queues notification for deferred processing.

7. connection_info_from_json

static ConnectionInfo connection_info_from_json(const json& j)

Implements the connection info from json operation declared by this component.

8. Impl

Impl(UIServer* owner) : owner_(owner)

Implements the impl operation declared by this component.

9. stop

void stop()

Stops stop and releases or signals its active resources.

10. accept_loop

void accept_loop()

Runs the accept loop until shutdown or disconnection.

11. client_session

void client_session(int fd)

Implements the client session operation declared by this component.

12. flush_pending_notifs

void flush_pending_notifs()

Implements the flush pending notifs operation declared by this component.

13. send_reply

void send_reply(ReplyAction action)

Sends reply to the connected peer or subsystem.

14. dispatch_connected

void dispatch_connected()

Implements the dispatch connected operation declared by this component.

15. dispatch_disconnected

void dispatch_disconnected()

Implements the dispatch disconnected operation declared by this component.

16. dispatch_new_connection

void dispatch_new_connection(const ConnectionInfo& info)

Implements the dispatch new connection operation declared by this component.

17. dispatch_alert

void dispatch_alert(const AlertInfo& a)

Implements the dispatch alert operation declared by this component.


7. Runtime Flow

  1. Normalize and bind the Unix-socket path
  2. Accept the daemon connection in a worker thread
  3. Read length-prefixed frames
  4. Parse JSON messages into ConnectionInfo or AlertInfo
  5. Dispatch UI signals through GLib idle/dispatcher mechanisms
  6. Send rule replies and queued notifications
  7. Detect disconnects and return to the accept loop

8. Concurrency and Main-Loop Behavior

  • Uses one or more C++ worker threads.
  • Protects shared state with a mutex.
  • Uses a condition variable for worker wake-up and shutdown coordination.
  • Uses atomic state for cross-thread lifecycle control.
  • Marshals results or callbacks onto the GLib/GTK main context.

9. Runtime and Security Considerations

  • Uses a local Unix-domain socket rather than a network listener.
  • Reads exact framed message lengths before parsing payloads.
  • Separates the socket worker thread from the GTK signal thread.
  • Represents user decisions with a closed ReplyAction enumeration.
  • JSON input and output must be treated as external data and validated before applying decisions.
  • Socket lifecycle and framing errors are handled without performing GTK operations directly from the I/O thread.

10. Error Handling and Operational Notes

  • Exceptions are caught locally in one or more operations to prevent application-wide termination.
  • Diagnostic failures are written to standard error or the GLib logging channel.
  • Boolean-returning operations use false to report recoverable failure.

11. Source Integrity

Relative source path: ipc/UIServer.cpp

SHA-256:

26d0c1a2e4c16f4b18bf97395e6514f4c7a8b9784d811572d796c1c9c94bda08

12. Licensing

The source header identifies this component as distributed under the GNU General Public License version 3. BastionGuard branding and trademarks remain subject to the notices stated in the source file.