Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

4.5 KB
Raw Blame
/* SPDX-License-Identifier: GPL-3.0-or-later
   Copyright (C) 2026 Maurizio Cammalleri */
/*
 * mcp_policy.c - where a connection made on someone else's word may go
 * (see mcp_policy.h). The ranges are IANA's special-purpose registries.
 */
#include "common/mcp_policy.h"

#include <netinet/in.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>

static int v4_public(uint32_t a)
{
    static const struct {
        uint32_t net;
        int bits;
    } bad[] = {
        {0x00000000u, 8},  /* this network */
        {0x0a000000u, 8},  /* private */
        {0x64400000u, 10}, /* shared, carrier-grade NAT */
        {0x7f000000u, 8},  /* loopback */
        {0xa9fe0000u, 16}, /* link-local, the clouds' metadata */
        {0xac100000u, 12}, /* private */
        {0xc0000000u, 24}, /* protocol assignments */
        {0xc0000200u, 24}, /* documentation */
        {0xc0586300u, 24}, /* 6to4 relays */
        {0xc0a80000u, 16}, /* private */
        {0xc6120000u, 15}, /* benchmarking */
        {0xc6336400u, 24}, /* documentation */
        {0xcb007100u, 24}, /* documentation */
        {0xe0000000u, 4},  /* multicast */
        {0xf0000000u, 4},  /* reserved, broadcast */
    };
    for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)
        if ((a >> (32 - bad[i].bits)) == (bad[i].net >> (32 - bad[i].bits)))
            return 0;
    return 1;
}

static uint32_t be32(const uint8_t *p)
{
    return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 |
           p[3];
}

static int v6_public(const uint8_t *b)
{
    static const uint8_t zero[12] = {0};
    static const uint8_t mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255};
    static const uint8_t nat64[12] = {0, 0x64, 0xff, 0x9b};
    if (!memcmp(b, mapped, 12) || !memcmp(b, nat64, 12))
        return v4_public(be32(b + 12));
    if (!memcmp(b, zero, 12)) /* ::, ::1, IPv4-compatible (deprecated) */
        return 0;
    if (b[0] == 0x20 && b[1] == 0x02) /* 6to4: the IPv4 address after */
        return v4_public(be32(b + 2));
    if ((b[0] & 0xfe) == 0xfc) /* unique local */
        return 0;
    if (b[0] == 0xfe && (b[1] & 0xc0) == 0x80) /* link-local */
        return 0;
    if (b[0] == 0xfe && (b[1] & 0xc0) == 0xc0) /* site-local (deprecated) */
        return 0;
    if (b[0] == 0xff) /* multicast */
        return 0;
    if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x0d && b[3] == 0xb8)
        return 0; /* documentation */
    if (b[0] == 0x01 && b[1] == 0x00 && !memcmp(b + 2, zero, 6))
        return 0; /* discard */
    if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x00)
        return 0; /* Teredo: an IPv4 address behind, hidden */
    if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && (b[3] & 0xf0) == 0x20)
        return 0; /* ORCHIDv2 */
    if (b[0] == 0x20 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x02 &&
        b[4] == 0 && b[5] == 0)
        return 0;     /* benchmarking */
    if (b[0] == 0x5f) /* 5f00::/16, segment routing */
        return 0;
    return 1;
}

int janas_addr_public(const struct sockaddr *sa)
{
    if (!sa)
        return 0;
    if (sa->sa_family == AF_INET) {
        const struct sockaddr_in *s = (const struct sockaddr_in *)sa;
        return v4_public(be32((const uint8_t *)&s->sin_addr));
    }
    if (sa->sa_family == AF_INET6) {
        const struct sockaddr_in6 *s = (const struct sockaddr_in6 *)sa;
        return v6_public((const uint8_t *)&s->sin6_addr);
    }
    return 0;
}

/* Does an entry ("host", "host:port", "[v6]:port", "[v6]") name host:port? */
static int entry_names(const char *e, const char *host, const char *port)
{
    const char *h = e, *he, *p = NULL;
    if (*e == '[') {
        h = e + 1;
        he = strchr(h, ']');
        if (!he)
            return 0;
        if (he[1] == ':')
            p = he + 2;
        else if (he[1])
            return 0;
    } else {
        he = strrchr(e, ':');
        if (he && strchr(e, ':') != he) /* a bare v6 address, no port */
            he = NULL;
        if (he)
            p = he + 1;
        else
            he = e + strlen(e);
    }
    size_t n = (size_t)(he - h);
    if (strlen(host) != n || strncasecmp(h, host, n) != 0)
        return 0;
    return !p || !strcmp(p, port);
}

int janas_net_allowed(const struct janas_net_policy *p, const char *host,
                      const char *port, const struct sockaddr *sa)
{
    if (!p || !p->public_only || janas_addr_public(sa))
        return 1;
    for (size_t i = 0; i < p->n_allow; i++)
        if (entry_names(p->allow[i], host, port))
            return 1;
    return 0;
}