← Back to Research Index View Raw Source

lane-vm-rss-socket-kern-2026-11-14.cpp

// lane_vm_rss.cpp — C++ compliant RSS 2.0 for Lane-VM + bare-metal socket-kern
// © Albert Lane. All Rights Reserved. Proprietary and Confidential.
// https://albertlane.net
// LICENSE: Lane-VM Proprietary License. NOT open-source. NOT MIT.
//
// Why this replaces the bridge:
//   HTTP RSS = simplex + poll. You needed 2 feeds + a bridge to fake duplex.
//   socket-kern RSS = one persistent socket, length-prefixed RSS frames,
//   send + recv concurrently. True full-duplex, no bridge, no polling.
//
// Stack:
//   - RSS 2.0 spec (Harvard / RSS Advisory Board): channel requires
//     title/link/description, item requires title OR description
//   - Dates: RFC 822 / RFC 2822 (pubDate, lastBuildDate) — NOT ISO-8601
//   - URIs: RFC 3986
//   - XML 1.0: well-formed, escaped, UTF-8
//   - Transport: bare-metal socket-kern (POSIX sockets, length-prefixed frames)
//     Frame: [4-byte BE length][UTF-8 RSS XML bytes]
//     Frame types are inside XML: <rss> = data, <ping/> = keepalive
//   - Scheme: rss://ghost-rss.py — ghost-rss.py is the Python endpoint
//     that speaks this same framing over the same socket-kern.
//
// Build:  g++ -std=c++17 -O2 -pthread -o lane_vm_rss lane_vm_rss.cpp
// Run:    ./lane_vm_rss --serve 7799      (socket-kern server)
//         ./lane_vm_rss --connect 127.0.0.1 7799 (smoke client)

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iostream>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <vector>

namespace lane_vm {

// [STRIPPED 75 bytes]
// XML + RFC 822 helpers — C++ compliant
// [STRIPPED 75 bytes]

inline std::string xml_escape(const std::string& s) {
    std::string out;
    out.reserve(s.size() + 16);
    for (char c : s) {
        switch (c) {
            case '&': out += "&amp;"; break;
            case '<': out += "&lt;"; break;
            case '>': out += "&gt;"; break;
            case '"': out += "&quot;"; break;
            case '\'': out += "&apos;"; break;
            default: out += c; break;
        }
    }
    return out;
}

inline std::string xml_unescape(const std::string& s) {
    std::string out = s;
    auto rep = [&](const std::string& a, const std::string& b) {
        size_t p = 0;
        while ((p = out.find(a, p)) != std::string::npos) {
            out.replace(p, a.size(), b);
            p += b.size();
        }
    };
    // Order matters: &amp; last on escape, first on unescape
    rep("&amp;", "&");
    rep("&lt;", "<");
    rep("&gt;", ">");
    rep("&quot;", "\"");
    rep("&apos;", "'");
    return out;
}

// RFC 822 / RFC 2822: "Sat, 14 Nov 2026 21:52:15 +0000" — no colon in offset
inline std::string rfc822_now() {
    std::time_t t = std::time(nullptr);
    std::tm tmv{};
    gmtime_r(&t, &tmv);
    char buf[128];
    // %a, %d %b %Y %H:%M:%S +0000
    std::strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S +0000", &tmv);
    return std::string(buf);
}

inline std::string tag_text(const std::string& xml, const std::string& tag) {
    std::string open = "<" + tag + ">";
    std::string close = "</" + tag + ">";
    size_t a = xml.find(open);
    if (a == std::string::npos) return "";
    a += open.size();
    size_t b = xml.find(close, a);
    if (b == std::string::npos) return "";
    return xml_unescape(xml.substr(a, b - a));
}

// [STRIPPED 75 bytes]
// RSS 2.0 model
// [STRIPPED 75 bytes]

struct RSSItem {
    std::string title;
    std::string link;
    std::string description;
    std::string pubDate; // RFC 822, empty = now
    std::string guid;    // if empty, = link
    bool isPermalink = true;
};

struct RSSFeed {
    std::string title;
    std::string link;
    std::string description;
    std::string language = "en-US";
    std::string lastBuildDate; // RFC 822, empty = now
    std::string generator = "Lane-VM RSS C++ 1.0.0 (https://albertlane.net)";
    std::string copyright = "© Albert Lane. All Rights Reserved. Proprietary and Confidential.";
    int ttl = 60;
    std::vector<RSSItem> items;

    std::string to_xml() const {
        std::string x;
        x += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        x += "<!-- © Albert Lane. All Rights Reserved. Proprietary and Confidential. -->\n";
        x += "<rss version=\"2.0\">\n<channel>\n";
        x += "  <title>" + xml_escape(title) + "</title>\n";
        x += "  <link>" + xml_escape(link) + "</link>\n";
        x += "  <description>" + xml_escape(description) + "</description>\n";
        x += "  <language>" + xml_escape(language) + "</language>\n";
        x += "  <lastBuildDate>" + xml_escape(lastBuildDate.empty() ? rfc822_now() : lastBuildDate) + "</lastBuildDate>\n";
        x += "  <generator>" + xml_escape(generator) + "</generator>\n";
        x += "  <copyright>" + xml_escape(copyright) + "</copyright>\n";
        x += "  <docs>https://cyber.harvard.edu/rss/rss.html</docs>\n";
        x += "  <ttl>" + std::to_string(ttl) + "</ttl>\n";
        for (auto &it : items) {
            x += "  <item>\n";
            x += "    <title>" + xml_escape(it.title) + "</title>\n";
            x += "    <link>" + xml_escape(it.link) + "</link>\n";
            x += "    <description>" + xml_escape(it.description) + "</description>\n";
            std::string pd = it.pubDate.empty() ? rfc822_now() : it.pubDate;
            x += "    <pubDate>" + xml_escape(pd) + "</pubDate>\n";
            std::string g = it.guid.empty() ? it.link : it.guid;
            x += "    <guid isPermaLink=\"" + std::string(it.isPermalink ? "true" : "false") + "\">" + xml_escape(g) + "</guid>\n";
            x += "  </item>\n";
        }
        x += "</channel>\n</rss>\n";
        return x;
    }

    static RSSFeed from_xml(const std::string& xml) {
        RSSFeed f;
        f.title = tag_text(xml, "title");
        // Channel title is first <title>, item titles are later — for smoke test
        // we parse channel-level fields by taking the segment before first <item>
        size_t itemPos = xml.find("<item>");
        std::string head = (itemPos == std::string::npos) ? xml : xml.substr(0, itemPos);
        f.title = tag_text(head, "title");
        f.link = tag_text(head, "link");
        f.description = tag_text(head, "description");
        f.lastBuildDate = tag_text(head, "lastBuildDate");
        // Items: naive scan, sufficient for Lane-VM controlled feeds
        size_t p = 0;
        while (true) {
            size_t s = xml.find("<item>", p);
            if (s == std::string::npos) break;
            size_t e = xml.find("</item>", s);
            if (e == std::string::npos) break;
            std::string body = xml.substr(s, e - s);
            RSSItem it;
            it.title = tag_text(body, "title");
            it.link = tag_text(body, "link");
            it.description = tag_text(body, "description");
            it.pubDate = tag_text(body, "pubDate");
            it.guid = tag_text(body, "guid");
            // isPermaLink attribute
            if (body.find("isPermaLink=\"false\"") != std::string::npos) it.isPermalink = false;
            f.items.push_back(it);
            p = e + 7;
        }
        return f;
    }
};

// [STRIPPED 75 bytes]
// Bare-metal socket-kern — minimal kernel socket abstraction
// [STRIPPED 75 bytes]
//   kern_socket_open / send_frame / recv_frame / close
//   No HTTP. No polling. One socket = full duplex.
//   Frame: uint32 BE length + bytes. Max 4 MiB per frame.

class SocketKern {
public:
    int fd = -1;
    std::atomic<bool> alive{false};

    ~SocketKern() { close(); }

    bool connect_to(const std::string& host, int port) {
        fd = ::socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0) return false;
        sockaddr_in addr{};
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        if (::inet_pton(AF_INET, host.c_str(), &addr.sin_addr) <= 0) return false;
        if (::connect(fd, (sockaddr*)&addr, sizeof(addr)) != 0) return false;
        alive = true;
        return true;
    }

    bool listen_on(int port, int backlog = 8) {
        fd = ::socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0) return false;
        int opt = 1;
        ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
        sockaddr_in addr{};
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = INADDR_ANY;
        addr.sin_port = htons(port);
        if (::bind(fd, (sockaddr*)&addr, sizeof(addr)) != 0) return false;
        if (::listen(fd, backlog) != 0) return false;
        alive = true;
        return true;
    }

    // Accept one connection, returns a new SocketKern (caller owns)
    SocketKern* accept_one() {
        int c = ::accept(fd, nullptr, nullptr);
        if (c < 0) return nullptr;
        auto* k = new SocketKern();
        k->fd = c;
        k->alive = true;
        return k;
    }

    bool send_all(const uint8_t* data, size_t len) {
        size_t sent = 0;
        while (sent < len) {
            ssize_t n = ::send(fd, data + sent, len - sent, 0);
            if (n <= 0) return false;
            sent += (size_t)n;
        }
        return true;
    }

    bool recv_all(uint8_t* buf, size_t len) {
        size_t got = 0;
        while (got < len) {
            ssize_t n = ::recv(fd, buf + got, len - got, 0);
            if (n <= 0) return false;
            got += (size_t)n;
        }
        return true;
    }

    bool send_frame(const std::string& payload) {
        if ((int)payload.size() > 4 * 1024 * 1024) return false;
        uint32_t len = htonl((uint32_t)payload.size());
        if (!send_all((uint8_t*)&len, 4)) return false;
        return send_all((uint8_t*)payload.data(), payload.size());
    }

    bool recv_frame(std::string& out) {
        uint32_t netlen = 0;
        if (!recv_all((uint8_t*)&netlen, 4)) return false;
        uint32_t len = ntohl(netlen);
        if (len > 4 * 1024 * 1024) return false;
        out.resize(len);
        if (len == 0) return true;
        return recv_all((uint8_t*)out.data(), len);
    }

    bool send_rss(const RSSFeed& feed) { return send_frame(feed.to_xml()); }
    bool recv_rss(RSSFeed& out) {
        std::string xml;
        if (!recv_frame(xml)) return false;
        out = RSSFeed::from_xml(xml);
        return true;
    }

    void close() {
        if (fd >= 0) { ::close(fd); fd = -1; }
        alive = false;
    }
};

} // namespace lane_vm

// [STRIPPED 75 bytes]
// Smoke: --serve / --connect
// [STRIPPED 75 bytes]
int main(int argc, char** argv) {
    using namespace lane_vm;
    if (argc >= 2 && std::string(argv[1]) == "--serve") {
        int port = argc >= 3 ? std::atoi(argv[2]) : 7799;
        SocketKern server;
        if (!server.listen_on(port)) { std::cerr << "listen failed\n"; return 1; }
        std::cout << "Lane-VM socket-kern listening on " << port << " rss://ghost-rss.py\n";
        auto* conn = server.accept_one();
        if (!conn) return 1;
        RSSFeed in;
        if (conn->recv_rss(in)) {
            std::cout << "SERVER got feed: " << in.title << " items=" << in.items.size() << "\n";
            for (auto &it : in.items) std::cout << "  item: " << it.title << " guid=" << it.guid << "\n";
            RSSFeed reply;
            reply.title = "Lane-VM Node B — inbox";
            reply.link = "rss://ghost-rss.py/nodes/B/inbox";
            reply.description = "Ack from B over socket-kern, full duplex.";
            reply.items.push_back({"ack B — got it, replying on same socket", "rss://ghost-rss.py/nodes/A/inbox", "ack payload", rfc822_now(), "ack-1", false});
            conn->send_rss(reply);
            std::cout << "SERVER sent ack\n";
        }
        delete conn;
        return 0;
    }
    if (argc >= 2 && std::string(argv[1]) == "--connect") {
        std::string host = argc >= 3 ? argv[2] : "127.0.0.1";
        int port = argc >= 4 ? std::atoi(argv[3]) : 7799;
        SocketKern client;
        if (!client.connect_to(host, port)) { std::cerr << "connect failed\n"; return 1; }
        RSSFeed feed;
        feed.title = "Lane-VM Node A — outbox";
        feed.link = "rss://ghost-rss.py/nodes/A/outbox";
        feed.description = "Hello B — private, VM must not read it (envelope in prod).";
        feed.items.push_back({"hello B — socket-kern full duplex", "rss://ghost-rss.py/nodes/B/inbox", "hello payload", rfc822_now(), "msg-1", false});
        client.send_rss(feed);
        std::cout << "CLIENT sent feed\n";
        RSSFeed reply;
        if (client.recv_rss(reply)) {
            std::cout << "CLIENT got reply: " << reply.title << " items=" << reply.items.size() << "\n";
            for (auto &it : reply.items) std::cout << "  item: " << it.title << "\n";
        }
        return 0;
    }
    std::cout << "Usage: ./lane_vm_rss --serve [port] | --connect [host] [port]\n";
    return 0;
}