SockDiag.cpp

1. Overview

Kernel socket diagnostic implementation. Queries NETLINK_SOCK_DIAG for matching TCP/UDP sockets and returns inode and UID information without scanning every process descriptor first.

The original source path is:

src/net/SockDiag.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::net

4. Dependencies and Included Interfaces

  • bsc/net/SockDiag.hpp
  • arpa/inet.h
  • cstring
  • errno.h
  • linux/inet_diag.h
  • linux/netlink.h
  • linux/sock_diag.h
  • netinet/in.h
  • sys/socket.h
  • unistd.h

5. Declared Types

  • struct timeval
  • struct nlmsghdr
  • struct inet_diag_req_v2
  • struct sockaddr_nl
  • struct sockaddr
  • struct inet_diag_msg

6. API and Functions

The file declares or implements the following callable entry points:

  • sockdiag_query(int family, int protocol, const std::string& src_ip, std::uint16_t src_port, const std::string& dst_ip, std::uint16_t dst_port)
  • sockdiag_lookup_tcp(const std::string& src_ip, std::uint16_t src_port, const std::string& dst_ip, std::uint16_t dst_port)
  • sockdiag_lookup_udp(const std::string& src_ip, std::uint16_t src_port, const std::string& dst_ip, std::uint16_t dst_port)

7. Constants and Compile-Time Controls

  • std::string& src_ip, std::uint16_t src_port, const std::string& dst_ip, std::uint16_t dst_port) { int fd = ::socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_SOCK_DIAG)
  • auto sent = ::sendto(fd, &msg, msg.nlh.nlmsg_len, 0, reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest))
  • auto rcv = ::recv(fd, buf, sizeof(buf), 0)
  • bool is_v6 = src_ip.find(':') != std::string::npos || dst_ip.find(':') != std::string::npos

8. External Resources and Protocol Values

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

  • bsc/net/SockDiag.hpp

9. Security Considerations

  • The component processes privileged operating-system or network data and should run with only the capabilities required by its backend.

10. Implementation Considerations

  • The file follows the daemon-wide convention of returning status values or optional objects instead of making recoverable conditions fatal.

11. Licensing and Attribution

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

12. Source Metrics

MetricValue
File size5437 bytes
Lines142
Detected API entries3
Detected types6