ZeroTierOne/diagnostic/node_state_interfaces_bsd.cpp
Aaron Johnson 45e3223591 feat: Add comprehensive JSON diagnostic output with schema validation
Implements structured JSON diagnostic output for node state export with full
schema documentation. This feature provides machine-readable diagnostics for
automated analysis, monitoring, and AI/MCP integration.

Key changes:
- Add `zerotier-cli diagnostic` command for JSON node state export
- Add `zerotier-cli dump -j` as alias for JSON output
- Add `zerotier-cli diagnostic --schema` to print JSON schema
- Implement platform-specific interface collection (Linux, BSD, macOS, Windows)
- Create modular diagnostic/ directory with isolated try/catch error handling
- Add comprehensive JSON schema (diagnostic_schema.json) for validation
- Include build-time schema embedding for offline access
- Add Python and Rust scripts for schema embedding during build
- Update build systems to compile new diagnostic modules

The diagnostic output includes:
- Node configuration and identity
- Network memberships and settings
- Interface states and IP addresses
- Peer connections and statistics
- Moon orbits
- Controller networks (if applicable)

All diagnostic collection is wrapped in try/catch blocks to ensure partial
failures don't prevent overall output generation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-08 12:40:06 -07:00

63 lines
No EOL
2.5 KiB
C++

#include "diagnostic/node_state_interfaces_bsd.hpp"
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <vector>
void addNodeStateInterfacesJson(nlohmann::json& j) {
try {
std::vector<nlohmann::json> interfaces_json;
struct ifaddrs *ifap, *ifa;
if (getifaddrs(&ifap) != 0) {
j["network_interfaces"] = "ERROR: getifaddrs failed";
return;
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) continue;
nlohmann::json iface_json;
iface_json["name"] = ifa->ifa_name;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock >= 0) {
struct ifreq ifr;
strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
if (ioctl(sock, SIOCGIFMTU, &ifr) == 0) {
iface_json["mtu"] = ifr.ifr_mtu;
}
if (ifa->ifa_addr->sa_family == AF_LINK) {
struct sockaddr_dl* sdl = (struct sockaddr_dl*)ifa->ifa_addr;
unsigned char* mac = (unsigned char*)LLADDR(sdl);
char macStr[32];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
iface_json["mac"] = macStr;
}
close(sock);
}
std::vector<std::string> addresses;
if (ifa->ifa_addr->sa_family == AF_INET) {
char addr[INET_ADDRSTRLEN];
struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_addr;
inet_ntop(AF_INET, &(sa->sin_addr), addr, INET_ADDRSTRLEN);
addresses.push_back(addr);
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
char addr[INET6_ADDRSTRLEN];
struct sockaddr_in6* sa6 = (struct sockaddr_in6*)ifa->ifa_addr;
inet_ntop(AF_INET6, &(sa6->sin6_addr), addr, INET6_ADDRSTRLEN);
addresses.push_back(addr);
}
iface_json["addresses"] = addresses;
interfaces_json.push_back(iface_json);
}
freeifaddrs(ifap);
j["network_interfaces"] = interfaces_json;
} catch (const std::exception& e) {
j["network_interfaces"] = std::string("Exception: ") + e.what();
} catch (...) {
j["network_interfaces"] = "Unknown error retrieving interfaces";
}
}