From 9330b01c28aebc865a3c0a51df8730196cb4152e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 23 May 2026 04:50:26 +0530 Subject: Phase 6a-6d: Consolidate and clean up 6a: Delete dead standalone tollgate_core/ (12 files, never compiled) 6b: Rewrite dns_server.c as thin shim to component's tollgate_core_dns - Fix component DNS to bind to AP IP instead of INADDR_ANY 6c: Rewrite stratum_proxy.c as thin shim with struct cast to component types 6d: Move sandbox logic into component's tollgate_core_firewall - Add tollgate_core_fw_set_sandbox_ports/set_sandbox_mint_access - Add is_sandbox_allowed() to component's ip4_canforward_filter - Clean main/firewall.c to delegate filter to component - Remove redundant DNS auth double-calls from main firewall Add ROADMAP.md with full extraction plan and checklists All 21 unit tests pass. ESP-IDF build passes. --- tollgate_core/src/tollgate_cashu.c | 253 ------------------ tollgate_core/src/tollgate_cashu.h | 40 --- tollgate_core/src/tollgate_core.c | 466 ---------------------------------- tollgate_core/src/tollgate_firewall.c | 166 ------------ tollgate_core/src/tollgate_firewall.h | 21 -- tollgate_core/src/tollgate_mining.c | 171 ------------- tollgate_core/src/tollgate_mining.h | 34 --- tollgate_core/src/tollgate_session.c | 220 ---------------- tollgate_core/src/tollgate_session.h | 42 --- 9 files changed, 1413 deletions(-) delete mode 100644 tollgate_core/src/tollgate_cashu.c delete mode 100644 tollgate_core/src/tollgate_cashu.h delete mode 100644 tollgate_core/src/tollgate_core.c delete mode 100644 tollgate_core/src/tollgate_firewall.c delete mode 100644 tollgate_core/src/tollgate_firewall.h delete mode 100644 tollgate_core/src/tollgate_mining.c delete mode 100644 tollgate_core/src/tollgate_mining.h delete mode 100644 tollgate_core/src/tollgate_session.c delete mode 100644 tollgate_core/src/tollgate_session.h (limited to 'tollgate_core/src') diff --git a/tollgate_core/src/tollgate_cashu.c b/tollgate_core/src/tollgate_cashu.c deleted file mode 100644 index 55f4953..0000000 --- a/tollgate_core/src/tollgate_cashu.c +++ /dev/null @@ -1,253 +0,0 @@ -#include "tollgate_cashu.h" -#include "tollgate_core.h" -#include "tollgate_platform.h" -#include -#include -#include -#include -#include -#include - -static const char *TAG = "tg_cashu"; - -static const char V3_PREFIX[] = "cashuA"; -static const size_t V3_PREFIX_LEN = 6; - -static int b64url_decode(const char *input, size_t input_len, char *out, size_t out_size, size_t *out_len) -{ - char *b64 = malloc(input_len + 4); - if (!b64) return -1; - size_t b64_len = input_len; - memcpy(b64, input, b64_len); - b64[b64_len] = '\0'; - - for (size_t i = 0; i < b64_len; i++) { - if (b64[i] == '-') b64[i] = '+'; - else if (b64[i] == '_') b64[i] = '/'; - } - while (b64_len % 4 != 0) { - b64[b64_len++] = '='; - } - b64[b64_len] = '\0'; - - size_t olen = 0; - int ret = mbedtls_base64_decode((unsigned char *)out, out_size, &olen, - (const unsigned char *)b64, b64_len); - free(b64); - if (ret != 0) return -1; - *out_len = olen; - return 0; -} - -static int parse_proofs_array(cJSON *arr, tg_cashu_token_t *out) -{ - if (!cJSON_IsArray(arr)) return -1; - int count = cJSON_GetArraySize(arr); - if (count > TG_CASHU_MAX_PROOFS) return -1; - - out->proof_count = 0; - out->total_amount = 0; - for (int i = 0; i < count; i++) { - cJSON *proof = cJSON_GetArrayItem(arr, i); - cJSON *amt = cJSON_GetObjectItemCaseSensitive(proof, "amount"); - cJSON *id = cJSON_GetObjectItemCaseSensitive(proof, "id"); - cJSON *secret = cJSON_GetObjectItemCaseSensitive(proof, "secret"); - cJSON *c = cJSON_GetObjectItemCaseSensitive(proof, "C"); - - if (!amt || !cJSON_IsNumber(amt)) return -1; - - out->proofs[i].amount = (uint64_t)amt->valuedouble; - out->total_amount += out->proofs[i].amount; - - if (id && cJSON_IsString(id)) { - strncpy(out->proofs[i].id, id->valuestring, sizeof(out->proofs[i].id) - 1); - } - if (secret && cJSON_IsString(secret)) { - strncpy(out->proofs[i].secret, secret->valuestring, sizeof(out->proofs[i].secret) - 1); - } - if (c && cJSON_IsString(c)) { - strncpy(out->proofs[i].c, c->valuestring, sizeof(out->proofs[i].c) - 1); - } - out->proof_count++; - } - return 0; -} - -int tg_cashu_decode_token(const char *token_str, tg_cashu_token_t *out) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (!token_str || !out) return -1; - memset(out, 0, sizeof(*out)); - - size_t len = strlen(token_str); - char *nl = strchr(token_str, '\n'); - if (nl) len = nl - token_str; - char *cr = strchr(token_str, '\r'); - if (cr && (cr - token_str) < (int)len) len = cr - token_str; - if (len <= V3_PREFIX_LEN) { - if (p && p->log_error) p->log_error(TAG, "Token too short"); - return -1; - } - if (strncmp(token_str, V3_PREFIX, V3_PREFIX_LEN) != 0) { - if (p && p->log_error) p->log_error(TAG, "Token missing cashuA prefix"); - return -1; - } - - size_t b64_len = len - V3_PREFIX_LEN; - size_t decoded_size = (b64_len * 3) / 4 + 4; - char *json_buf = malloc(decoded_size); - if (!json_buf) return -1; - size_t json_len = 0; - if (b64url_decode(token_str + V3_PREFIX_LEN, b64_len, - json_buf, decoded_size - 1, &json_len) != 0) { - if (p && p->log_error) p->log_error(TAG, "Base64url decode failed"); - free(json_buf); - return -1; - } - json_buf[json_len] = '\0'; - - cJSON *root = cJSON_Parse(json_buf); - free(json_buf); - if (!root) { - if (p && p->log_error) p->log_error(TAG, "JSON parse failed"); - return -1; - } - - cJSON *token_arr = cJSON_GetObjectItemCaseSensitive(root, "token"); - if (token_arr && cJSON_IsArray(token_arr)) { - cJSON *first = cJSON_GetArrayItem(token_arr, 0); - if (!first) { cJSON_Delete(root); return -1; } - - cJSON *mint = cJSON_GetObjectItemCaseSensitive(first, "mint"); - if (mint && cJSON_IsString(mint)) { - strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); - } - - cJSON *proofs = cJSON_GetObjectItemCaseSensitive(first, "proofs"); - if (proofs) { - if (parse_proofs_array(proofs, out) != 0) { cJSON_Delete(root); return -1; } - } - } else { - cJSON *mint = cJSON_GetObjectItemCaseSensitive(root, "mint"); - if (mint && cJSON_IsString(mint)) { - strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); - } - - cJSON *proofs = cJSON_GetObjectItemCaseSensitive(root, "proofs"); - if (proofs) { - if (parse_proofs_array(proofs, out) != 0) { cJSON_Delete(root); return -1; } - } - } - - cJSON_Delete(root); - - if (out->proof_count == 0) { - if (p && p->log_error) p->log_error(TAG, "No proofs in token"); - return -1; - } - - if (p && p->log_info) p->log_info(TAG, "Decoded token: %d proofs, total=%llu, mint=%s", - out->proof_count, (unsigned long long)out->total_amount, out->mint_url); - return 0; -} - -static void sha256_hex(const char *data, size_t data_len, char *hex_out) -{ - unsigned char hash[32]; - mbedtls_sha256((const unsigned char *)data, data_len, hash, 0); - for (int i = 0; i < 32; i++) { - sprintf(hex_out + i * 2, "%02x", hash[i]); - } - hex_out[64] = '\0'; -} - -int tg_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, - tg_cashu_proof_state_t *states, int *state_count) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - - cJSON *ys_arr = cJSON_CreateArray(); - for (int i = 0; i < token->proof_count; i++) { - char y_hex[65]; - sha256_hex(token->proofs[i].secret, strlen(token->proofs[i].secret), y_hex); - cJSON_AddItemToArray(ys_arr, cJSON_CreateString(y_hex)); - strncpy(states[i].y_hex, y_hex, sizeof(states[i].y_hex) - 1); - states[i].spent = false; - } - *state_count = token->proof_count; - - char *ys_json = cJSON_PrintUnformatted(ys_arr); - cJSON_Delete(ys_arr); - - char *post_body = malloc(4096); - if (!post_body) { cJSON_free(ys_json); return -1; } - snprintf(post_body, 4096, "{\"Ys\":%s}", ys_json); - cJSON_free(ys_json); - - char url[512]; - snprintf(url, sizeof(url), "%s/v1/checkstate", mint_url); - - if (!p || !p->http_post) { - free(post_body); - return -1; - } - - char *resp_buf = malloc(8192); - if (!resp_buf) { free(post_body); return -1; } - - int resp_len = p->http_post(url, "Content-Type: application/json", - post_body, (int)strlen(post_body), - resp_buf, 8191); - free(post_body); - - if (resp_len <= 0) { - if (p && p->log_error) p->log_error(TAG, "checkstate HTTP failed: resp_len=%d", resp_len); - free(resp_buf); - return -1; - } - resp_buf[resp_len] = '\0'; - - cJSON *root = cJSON_Parse(resp_buf); - free(resp_buf); - if (!root) return -1; - - cJSON *states_arr = cJSON_GetObjectItemCaseSensitive(root, "states"); - if (!states_arr || !cJSON_IsArray(states_arr)) { - cJSON_Delete(root); - return -1; - } - - int n = cJSON_GetArraySize(states_arr); - for (int i = 0; i < n && i < token->proof_count; i++) { - cJSON *s = cJSON_GetArrayItem(states_arr, i); - cJSON *state = cJSON_GetObjectItemCaseSensitive(s, "state"); - if (state && cJSON_IsString(state)) { - states[i].spent = (strcmp(state->valuestring, "SPENT") == 0); - } - } - - cJSON_Delete(root); - return 0; -} - -uint64_t tg_cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step, - uint64_t step_size_ms) -{ - if (price_per_step == 0) return 0; - return (token_amount / price_per_step) * step_size_ms; -} - -uint64_t tg_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, - uint64_t step_size) -{ - if (price_per_step == 0) return 0; - return (token_amount / price_per_step) * step_size; -} - -bool tg_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url) -{ - if (!mint_url || mint_url[0] == '\0') return false; - if (!accepted_mint_url || accepted_mint_url[0] == '\0') return false; - return (strstr(mint_url, accepted_mint_url) != NULL || - strcmp(mint_url, accepted_mint_url) == 0); -} diff --git a/tollgate_core/src/tollgate_cashu.h b/tollgate_core/src/tollgate_cashu.h deleted file mode 100644 index 9785e98..0000000 --- a/tollgate_core/src/tollgate_cashu.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef TOLLGATE_CORE_CASHU_H -#define TOLLGATE_CORE_CASHU_H - -#include -#include - -#define TG_CASHU_MAX_PROOFS 10 -#define TG_CASHU_MAX_SECRET_LEN 128 -#define TG_CASHU_MAX_ID_LEN 68 -#define TG_CASHU_MAX_C_LEN 128 - -typedef struct { - uint64_t amount; - char id[TG_CASHU_MAX_ID_LEN]; - char secret[TG_CASHU_MAX_SECRET_LEN]; - char c[TG_CASHU_MAX_C_LEN]; -} tg_cashu_proof_t; - -typedef struct { - tg_cashu_proof_t proofs[TG_CASHU_MAX_PROOFS]; - int proof_count; - char mint_url[256]; - uint64_t total_amount; -} tg_cashu_token_t; - -typedef struct { - char y_hex[65]; - bool spent; -} tg_cashu_proof_state_t; - -int tg_cashu_decode_token(const char *token_str, tg_cashu_token_t *out); -int tg_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, - tg_cashu_proof_state_t *states, int *state_count); -uint64_t tg_cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step, - uint64_t step_size_ms); -uint64_t tg_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, - uint64_t step_size); -bool tg_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url); - -#endif diff --git a/tollgate_core/src/tollgate_core.c b/tollgate_core/src/tollgate_core.c deleted file mode 100644 index c7c2902..0000000 --- a/tollgate_core/src/tollgate_core.c +++ /dev/null @@ -1,466 +0,0 @@ -#include "tollgate_core.h" -#include "tollgate_platform.h" -#include "tollgate_cashu.h" -#include "tollgate_session.h" -#include "tollgate_firewall.h" -#include "tollgate_mining.h" -#include -#include -#include - -static const char *TAG = "tg_core"; -static const tollgate_platform_t *s_platform; -static uint32_t s_ap_ip; - -static uint32_t s_owner_ip; -static uint8_t s_owner_mac[6]; -static bool s_owner_connected; - -const tollgate_platform_t *tollgate_core_get_platform(void) -{ - return s_platform; -} - -uint32_t tollgate_core_get_ap_ip(void) -{ - return s_ap_ip; -} - -int tollgate_core_init(const tollgate_platform_t *platform, uint32_t ap_ip) -{ - if (!platform) return -1; - - s_platform = platform; - s_ap_ip = ap_ip; - s_owner_connected = false; - memset(s_owner_mac, 0, sizeof(s_owner_mac)); - - tg_session_init(); - tg_firewall_init(ap_ip); - tg_mining_init(); - - if (platform->log_info) { - char ip_str[16]; - snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", - (int)((ap_ip >> 0) & 0xFF), (int)((ap_ip >> 8) & 0xFF), - (int)((ap_ip >> 16) & 0xFF), (int)((ap_ip >> 24) & 0xFF)); - platform->log_info(TAG, "TollGate core initialized, AP IP=%s", ip_str); - } - return 0; -} - -void tollgate_core_tick(void) -{ - tg_session_tick(); -} - -int tollgate_core_process_payment(uint32_t client_ip, const char *token_str) -{ - if (!s_platform || !token_str) return -1; - - const char *accepted_mint = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL; - if (!accepted_mint || accepted_mint[0] == '\0') { - if (s_platform->log_error) s_platform->log_error(TAG, "No mint URL configured"); - return -1; - } - - tg_cashu_token_t token; - if (tg_cashu_decode_token(token_str, &token) != 0) { - if (s_platform->log_error) s_platform->log_error(TAG, "Token decode failed"); - return -1; - } - - bool mint_ok = false; - if (s_platform->get_accepted_mint_count && s_platform->get_accepted_mint) { - int count = s_platform->get_accepted_mint_count(); - for (int i = 0; i < count; i++) { - if (tg_cashu_is_mint_accepted(token.mint_url, s_platform->get_accepted_mint(i))) { - mint_ok = true; - break; - } - } - } else { - mint_ok = tg_cashu_is_mint_accepted(token.mint_url, accepted_mint); - } - if (!mint_ok) { - if (s_platform->log_error) s_platform->log_error(TAG, "Token mint not accepted"); - return -1; - } - - tg_cashu_proof_state_t states[TG_CASHU_MAX_PROOFS]; - int state_count = 0; - if (tg_cashu_check_proof_states(token.mint_url, &token, states, &state_count) != 0) { - if (s_platform->log_error) s_platform->log_error(TAG, "Proof state check failed (continuing)"); - } else { - for (int i = 0; i < state_count; i++) { - if (states[i].spent) { - if (s_platform->log_error) s_platform->log_error(TAG, "Proof %d is SPENT", i); - return -1; - } - } - } - - if (s_platform->wallet_receive) { - if (!s_platform->wallet_receive(token_str)) { - if (s_platform->log_error) s_platform->log_error(TAG, "wallet_receive rejected token"); - return -1; - } - } - - const char *metric = (s_platform->get_metric) ? s_platform->get_metric() : "milliseconds"; - uint64_t price = (s_platform->get_price_sats) ? s_platform->get_price_sats() : 21; - uint64_t step_size; - - if (strcmp(metric, "bytes") == 0) { - step_size = (s_platform->get_step_bytes) ? (uint64_t)s_platform->get_step_bytes() : 22020096; - } else { - step_size = (s_platform->get_step_ms) ? (uint64_t)s_platform->get_step_ms() : 60000; - } - - uint64_t allotment = tg_cashu_calculate_allotment(token.total_amount, price, step_size); - if (allotment == 0) { - if (s_platform->log_error) s_platform->log_error(TAG, "Token amount too small"); - return -1; - } - - if (strcmp(metric, "bytes") == 0) { - if (!tg_session_create_bytes(client_ip, allotment)) return -1; - } else { - if (!tg_session_create(client_ip, allotment)) return -1; - } - - if (s_platform->log_info) s_platform->log_info(TAG, "Payment: %llu sats -> %llu %s", - (unsigned long long)token.total_amount, (unsigned long long)allotment, metric); - return 0; -} - -int tollgate_core_process_share(uint32_t client_ip, const char *job_id, - const char *nonce, const char *ntime, const char *version) -{ - (void)job_id; (void)nonce; (void)ntime; (void)version; - if (!s_platform) return -1; - - tg_mining_update_hashrate(client_ip, true); - const tg_mining_client_stats_t *stats = tg_mining_get_client_stats(client_ip); - if (!stats) return -1; - - double hashprice = tg_mining_get_current_hashprice(); - uint64_t override = (s_platform->get_hashprice_override) ? s_platform->get_hashprice_override() : 0; - if (override > 0) hashprice = tg_mining_calculate_hashprice_override(override); - - const char *metric = (s_platform->get_metric) ? s_platform->get_metric() : "milliseconds"; - int price = (s_platform->get_price_sats) ? s_platform->get_price_sats() : 21; - - tg_session_t *existing = tg_session_find_by_ip(client_ip); - if (existing && existing->payment_method == TG_PAYMENT_MINING) { - uint64_t allotment; - if (strcmp(metric, "bytes") == 0) { - int step_bytes = (s_platform->get_step_bytes) ? (int)s_platform->get_step_bytes() : 22020096; - allotment = tg_mining_shares_to_allotment_bytes(stats->hashrate_ghs, hashprice, price, step_bytes); - existing->allotment_bytes += allotment; - } else { - int step_ms = (s_platform->get_step_ms) ? s_platform->get_step_ms() : 60000; - allotment = tg_mining_shares_to_allotment_ms(stats->hashrate_ghs, hashprice, price, step_ms); - tg_session_extend(existing, allotment); - } - return 0; - } - - uint64_t allotment; - if (strcmp(metric, "bytes") == 0) { - int step_bytes = (s_platform->get_step_bytes) ? (int)s_platform->get_step_bytes() : 22020096; - allotment = tg_mining_shares_to_allotment_bytes(stats->hashrate_ghs, hashprice, price, step_bytes); - tg_session_t *s = tg_session_create_bytes(client_ip, allotment); - if (s) s->payment_method = TG_PAYMENT_MINING; - } else { - int step_ms = (s_platform->get_step_ms) ? s_platform->get_step_ms() : 60000; - allotment = tg_mining_shares_to_allotment_ms(stats->hashrate_ghs, hashprice, price, step_ms); - tg_session_t *s = tg_session_create(client_ip, allotment); - if (s) s->payment_method = TG_PAYMENT_MINING; - } - - return 0; -} - -void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip) -{ - if (!s_owner_connected) { - s_owner_connected = true; - s_owner_ip = client_ip; - if (mac) memcpy(s_owner_mac, mac, 6); - if (s_platform && s_platform->log_info) { - char ip_str[16]; - snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", - (int)((client_ip >> 0) & 0xFF), (int)((client_ip >> 8) & 0xFF), - (int)((client_ip >> 16) & 0xFF), (int)((client_ip >> 24) & 0xFF)); - s_platform->log_info(TAG, "First client = owner: %s", ip_str); - } - return; - } - if (s_platform && s_platform->log_info) s_platform->log_info(TAG, "Client connected (non-owner)"); -} - -void tollgate_core_client_disconnected(const uint8_t *mac) -{ - if (!s_owner_connected) return; - if (mac && memcmp(s_owner_mac, mac, 6) == 0) { - s_owner_connected = false; - memset(s_owner_mac, 0, sizeof(s_owner_mac)); - if (s_platform && s_platform->log_info) s_platform->log_info(TAG, "Owner disconnected"); - } -} - -bool tollgate_core_is_client_allowed(uint32_t client_ip) -{ - return tg_firewall_is_allowed(client_ip); -} - -bool tollgate_core_is_dns_running(void) -{ - return false; -} - -char *tollgate_core_get_status_json(void) -{ - const int BUFSIZE = 512; - char *json = malloc(BUFSIZE); - if (!json) return NULL; - snprintf(json, BUFSIZE, - "{\"ownerConnected\":%s,\"activeSessions\":%d,\"allowedClients\":%d,\"dnsRunning\":%s}", - s_owner_connected ? "true" : "false", - tg_session_active_count(), - tg_firewall_client_count(), - tollgate_core_is_dns_running() ? "true" : "false"); - return json; -} - -char *tollgate_core_get_config_json(void) -{ - const int BUFSIZE = 512; - char *json = malloc(BUFSIZE); - if (!json) return NULL; - int pos = 0; - pos += snprintf(json + pos, BUFSIZE - pos, "{"); - if (s_platform) { - if (s_platform->get_price_sats) - pos += snprintf(json + pos, BUFSIZE - pos, "\"priceSats\":%d,", (int)s_platform->get_price_sats()); - if (s_platform->get_step_ms) - pos += snprintf(json + pos, BUFSIZE - pos, "\"stepMs\":%d,", (int)s_platform->get_step_ms()); - if (s_platform->get_mint_url) - pos += snprintf(json + pos, BUFSIZE - pos, "\"mintUrl\":\"%s\",", s_platform->get_mint_url()); - if (s_platform->get_metric) - pos += snprintf(json + pos, BUFSIZE - pos, "\"metric\":\"%s\"", s_platform->get_metric()); - } - pos += snprintf(json + pos, BUFSIZE - pos, "}"); - return json; -} - -int tollgate_core_active_session_count(void) -{ - return tg_session_active_count(); -} - -int tollgate_core_allowed_client_count(void) -{ - return tg_firewall_client_count(); -} - -int tollgate_core_firewall_revoke_all(void) -{ - tg_session_revoke_all(); - return tg_firewall_revoke_all(); -} - -void tollgate_core_firewall_set_mining_port(uint16_t port) -{ - tg_firewall_set_mining_port(port); -} - -void tollgate_core_firewall_set_sandbox_mint_access(bool enable) -{ - tg_firewall_set_sandbox_mint_access(enable); -} - -bool tollgate_core_is_owner(uint32_t client_ip) -{ - return s_owner_connected && s_owner_ip == client_ip; -} - -bool tollgate_core_is_owner_connected(void) -{ - return s_owner_connected; -} - -double tollgate_core_get_hashprice(void) -{ - return tg_mining_get_current_hashprice(); -} - -void tollgate_core_set_nbits(uint32_t nbits) -{ - tg_mining_set_current_nbits(nbits); -} - -const void *tollgate_core_get_current_job(void) { return NULL; } -void tollgate_core_set_job(const void *job) { (void)job; } -int tollgate_core_stratum_client_start(void) { return -1; } -void tollgate_core_stratum_client_stop(void) { } -int tollgate_core_stratum_proxy_init(uint16_t port) { (void)port; return -1; } -void tollgate_core_stratum_proxy_get_stats(void *out) { (void)out; } -void tollgate_core_beacon_start(void) { } -void tollgate_core_market_init(void) { } -void tollgate_core_market_on_scan_result(const void *ie_data, int ie_len, const uint8_t *bssid, int rssi) -{ - (void)ie_data; (void)ie_len; (void)bssid; (void)rssi; -} - -const void *tollgate_core_get_sessions_array(void) -{ - return tg_session_get_array(); -} - -int tollgate_core_get_sessions_array_size(void) -{ - return tg_session_get_array_size(); -} - -void *tollgate_core_find_session_by_ip(uint32_t ip) -{ - return tg_session_find_by_ip(ip); -} - -void *tollgate_core_find_session_by_mac(const char *mac) -{ - return tg_session_find_by_mac(mac); -} - -void tollgate_core_session_extend(void *session, uint64_t additional_ms) -{ - tg_session_extend((tg_session_t *)session, additional_ms); -} - -int tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes) -{ - tg_session_add_bytes(client_ip, bytes); - return 0; -} - -void *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms) -{ - return tg_session_create(client_ip, allotment_ms); -} - -void *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes) -{ - return tg_session_create_bytes(client_ip, allotment_bytes); -} - -void tollgate_core_session_revoke(void *session) -{ - tg_session_revoke((tg_session_t *)session); -} - -bool tollgate_core_session_is_expired(const void *session) -{ - return tg_session_is_expired((const tg_session_t *)session); -} - -void tollgate_core_firewall_grant(uint32_t client_ip) -{ - tg_firewall_grant(client_ip); -} - -void tollgate_core_firewall_revoke(uint32_t client_ip) -{ - tg_firewall_revoke(client_ip); -} - -int tollgate_core_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size) -{ - return tg_firewall_get_mac_for_ip(client_ip, mac_out, mac_out_size); -} - -int tollgate_core_cashu_decode(const char *token_str, void *out) -{ - return tg_cashu_decode_token(token_str, (tg_cashu_token_t *)out); -} - -int tollgate_core_cashu_check_states(const char *mint_url, const void *token, - void *states, int *state_count) -{ - return tg_cashu_check_proof_states(mint_url, (const tg_cashu_token_t *)token, - (tg_cashu_proof_state_t *)states, state_count); -} - -uint64_t tollgate_core_cashu_allotment(uint64_t amount, uint64_t price, uint64_t step_size) -{ - return tg_cashu_calculate_allotment(amount, price, step_size); -} - -bool tollgate_core_cashu_is_mint_accepted(const char *mint_url) -{ - if (!s_platform) return false; - const char *accepted = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL; - if (!accepted) return false; - if (s_platform->get_accepted_mint_count && s_platform->get_accepted_mint) { - int count = s_platform->get_accepted_mint_count(); - for (int i = 0; i < count; i++) { - if (tg_cashu_is_mint_accepted(mint_url, s_platform->get_accepted_mint(i))) - return true; - } - return false; - } - return tg_cashu_is_mint_accepted(mint_url, accepted); -} - -const char *tollgate_core_cashu_token_mint(const void *token) -{ - const tg_cashu_token_t *t = (const tg_cashu_token_t *)token; - return t->mint_url; -} - -uint64_t tollgate_core_cashu_token_amount(const void *token) -{ - const tg_cashu_token_t *t = (const tg_cashu_token_t *)token; - return t->total_amount; -} - -void tollgate_core_mining_update_hashrate(uint32_t client_ip, bool accepted) -{ - tg_mining_update_hashrate(client_ip, accepted); -} - -const void *tollgate_core_mining_get_client_stats(uint32_t client_ip) -{ - return tg_mining_get_client_stats(client_ip); -} - -double tollgate_core_mining_get_hashprice(void) -{ - return tg_mining_get_current_hashprice(); -} - -uint64_t tollgate_core_mining_shares_to_allotment_ms(double hashrate, double hashprice, - int price, int step_ms) -{ - return tg_mining_shares_to_allotment_ms(hashrate, hashprice, price, step_ms); -} - -uint64_t tollgate_core_mining_shares_to_allotment_bytes(double hashrate, double hashprice, - int price, int step_bytes) -{ - return tg_mining_shares_to_allotment_bytes(hashrate, hashprice, price, step_bytes); -} - -void tollgate_core_mining_set_nbits(uint32_t nbits) -{ - tg_mining_set_current_nbits(nbits); -} - -int tollgate_core_dns_start(uint32_t upstream_dns) -{ - (void)upstream_dns; - return -1; -} - -void tollgate_core_dns_stop(void) { } diff --git a/tollgate_core/src/tollgate_firewall.c b/tollgate_core/src/tollgate_firewall.c deleted file mode 100644 index 8111be8..0000000 --- a/tollgate_core/src/tollgate_firewall.c +++ /dev/null @@ -1,166 +0,0 @@ -#include "tollgate_firewall.h" -#include "tollgate_core.h" -#include "tollgate_platform.h" -#include -#include - -#define FW_MAX_CLIENTS 10 - -static const char *TAG = "tg_fw"; -static uint32_t s_ap_ip; -static uint16_t s_mining_port; -static bool s_sandbox_mint; - -typedef struct { - uint32_t ip; - char mac[TG_FW_MAX_MAC_LEN]; -} fw_client_t; - -static fw_client_t s_clients[FW_MAX_CLIENTS]; -static int s_client_count = 0; - -static void log_fw(const char *verb, uint32_t client_ip, const char *mac) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) { - char ip_str[16]; - snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", - (int)((client_ip >> 0) & 0xFF), (int)((client_ip >> 8) & 0xFF), - (int)((client_ip >> 16) & 0xFF), (int)((client_ip >> 24) & 0xFF)); - p->log_info(TAG, "%s %s mac=%s", verb, ip_str, mac ? mac : "unknown"); - } -} - -int tg_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (!p) return -1; - - if (p->mac_for_ip) { - if (p->mac_for_ip(client_ip, mac_out, mac_out_size)) { - return 0; - } - } - return -1; -} - -int tg_firewall_init(uint32_t ap_ip) -{ - s_ap_ip = ap_ip; - memset(s_clients, 0, sizeof(s_clients)); - s_client_count = 0; - s_mining_port = 0; - s_sandbox_mint = false; - - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->napt_enable) p->napt_enable(ap_ip, true); - - if (p && p->log_info) { - char ip_str[16]; - snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", - (int)((ap_ip >> 0) & 0xFF), (int)((ap_ip >> 8) & 0xFF), - (int)((ap_ip >> 16) & 0xFF), (int)((ap_ip >> 24) & 0xFF)); - p->log_info(TAG, "Firewall initialized AP=%s NAT on, per-client filter", ip_str); - } - return 0; -} - -static fw_client_t *find_client_by_ip(uint32_t client_ip) -{ - for (int i = 0; i < s_client_count; i++) { - if (s_clients[i].ip == client_ip) return &s_clients[i]; - } - return NULL; -} - -static fw_client_t *find_client_by_mac(const char *mac) -{ - for (int i = 0; i < s_client_count; i++) { - if (s_clients[i].mac[0] != '\0' && strcmp(s_clients[i].mac, mac) == 0) { - return &s_clients[i]; - } - } - return NULL; -} - -void tg_firewall_grant(uint32_t client_ip) -{ - fw_client_t *existing = find_client_by_ip(client_ip); - if (existing) return; - - if (s_client_count >= FW_MAX_CLIENTS) { - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_warn) p->log_warn(TAG, "Max clients, cannot grant"); - return; - } - - fw_client_t *c = &s_clients[s_client_count]; - c->ip = client_ip; - c->mac[0] = '\0'; - tg_firewall_get_mac_for_ip(client_ip, c->mac, sizeof(c->mac)); - s_client_count++; - - log_fw("granted", client_ip, c->mac[0] ? c->mac : "unknown"); -} - -void tg_firewall_revoke(uint32_t client_ip) -{ - for (int i = 0; i < s_client_count; i++) { - if (s_clients[i].ip == client_ip) { - log_fw("revoked", client_ip, s_clients[i].mac[0] ? s_clients[i].mac : "unknown"); - s_clients[i] = s_clients[s_client_count - 1]; - s_client_count--; - return; - } - } -} - -int tg_firewall_revoke_all(void) -{ - s_client_count = 0; - memset(s_clients, 0, sizeof(s_clients)); - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) p->log_info(TAG, "All clients revoked"); - return 0; -} - -bool tg_firewall_is_allowed(uint32_t client_ip) -{ - return find_client_by_ip(client_ip) != NULL; -} - -bool tg_firewall_is_mac_allowed(const char *mac) -{ - return find_client_by_mac(mac) != NULL; -} - -int tg_firewall_client_count(void) -{ - return s_client_count; -} - -void tg_firewall_set_mining_port(uint16_t port) -{ - s_mining_port = port; -} - -void tg_firewall_set_sandbox_mint_access(bool enable) -{ - s_sandbox_mint = enable; -} - -int tg_firewall_filter_packet(const uint8_t *payload, int payload_len) -{ - if (payload_len < 20) return -1; - - uint32_t src_ip = (uint32_t)payload[12] | ((uint32_t)payload[13] << 8) | - ((uint32_t)payload[14] << 16) | ((uint32_t)payload[15] << 24); - - uint32_t ap_subnet = s_ap_ip & 0x00FFFFFF; - uint32_t src_subnet = src_ip & 0x00FFFFFF; - if (src_subnet != ap_subnet) return 1; - - if (tg_firewall_is_allowed(src_ip)) return 1; - - return 0; -} diff --git a/tollgate_core/src/tollgate_firewall.h b/tollgate_core/src/tollgate_firewall.h deleted file mode 100644 index 7df8a45..0000000 --- a/tollgate_core/src/tollgate_firewall.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef TOLLGATE_CORE_FIREWALL_H -#define TOLLGATE_CORE_FIREWALL_H - -#include -#include - -#define TG_FW_MAX_MAC_LEN 18 - -int tg_firewall_init(uint32_t ap_ip); -void tg_firewall_grant(uint32_t client_ip); -void tg_firewall_revoke(uint32_t client_ip); -int tg_firewall_revoke_all(void); -bool tg_firewall_is_allowed(uint32_t client_ip); -bool tg_firewall_is_mac_allowed(const char *mac); -int tg_firewall_client_count(void); -int tg_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size); -void tg_firewall_set_mining_port(uint16_t port); -void tg_firewall_set_sandbox_mint_access(bool enable); -int tg_firewall_filter_packet(const uint8_t *payload, int payload_len); - -#endif diff --git a/tollgate_core/src/tollgate_mining.c b/tollgate_core/src/tollgate_mining.c deleted file mode 100644 index 8a4a778..0000000 --- a/tollgate_core/src/tollgate_mining.c +++ /dev/null @@ -1,171 +0,0 @@ -#include "tollgate_mining.h" -#include "tollgate_core.h" -#include "tollgate_platform.h" -#include -#include - -static const char *TAG = "tg_mining"; - -static tg_mining_client_stats_t s_clients[TG_MINING_MAX_CLIENTS]; -static int s_client_count = 0; -static double s_current_hashprice = 0.0; -static uint32_t s_current_nbits = 0; -static uint64_t s_current_difficulty = 1; - -static int64_t get_time_ms(void) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->get_time_ms) return p->get_time_ms(); - return 0; -} - -uint64_t tg_mining_nbits_to_difficulty(uint32_t nbits) -{ - if (nbits == 0) return (uint64_t)-1; - - uint32_t exponent = (nbits >> 24) & 0xFF; - uint32_t mantissa = nbits & 0x007FFFFF; - - if (exponent <= 3) { - mantissa >>= (8 * (3 - exponent)); - if (mantissa == 0) return (uint64_t)-1; - return 0x00000000FFFF0000ULL / mantissa; - } - - uint64_t target = (uint64_t)mantissa << (8 * (exponent - 3)); - if (target == 0) return (uint64_t)-1; - - uint64_t pdiff = 0x00000000FFFF0000ULL; - uint64_t diff = pdiff / target; - if (diff == 0) diff = 1; - return diff; -} - -double tg_mining_calculate_hashprice(uint32_t nbits) -{ - uint64_t diff = tg_mining_nbits_to_difficulty(nbits); - if (diff == 0 || diff == (uint64_t)-1) return 0.0; - - double network_hashrate_th = (double)diff * 4294967296.0 / 1e12; - double daily_sats = (double)TG_MINING_BLOCK_SUBSIDY_SATS * (double)TG_MINING_BLOCKS_PER_DAY; - double sats_per_th_day = daily_sats / network_hashrate_th; - return sats_per_th_day / 1000.0; -} - -double tg_mining_calculate_hashprice_override(uint64_t sats_per_ghs_day) -{ - return (double)sats_per_ghs_day; -} - -int tg_mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len) -{ - (void)header80; - (void)nonce; - (void)target; - (void)target_len; - return 0; -} - -uint64_t tg_mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice_sats_per_ghs_s, - int price_per_step, int step_size_ms) -{ - if (hashrate_ghs <= 0.0 || hashprice_sats_per_ghs_s <= 0.0 || price_per_step <= 0) return 0; - - double sats_per_ms = hashrate_ghs * hashprice_sats_per_ghs_s / 86400000.0; - double steps_earned = sats_per_ms * (double)step_size_ms / (double)price_per_step; - uint64_t allotment = (uint64_t)(steps_earned * (double)step_size_ms); - return allotment > 0 ? allotment : 1; -} - -uint64_t tg_mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice_sats_per_ghs_s, - int price_per_step, int step_size_bytes) -{ - if (hashrate_ghs <= 0.0 || hashprice_sats_per_ghs_s <= 0.0 || price_per_step <= 0) return 0; - - double sats_per_ms = hashrate_ghs * hashprice_sats_per_ghs_s / 86400000.0; - double steps_earned = sats_per_ms * 1000.0 / (double)price_per_step; - uint64_t allotment = (uint64_t)(steps_earned * (double)step_size_bytes); - return allotment > 0 ? allotment : 1; -} - -tg_mining_client_stats_t *tg_mining_get_or_create_client(uint32_t client_ip) -{ - for (int i = 0; i < s_client_count; i++) { - if (s_clients[i].ip == client_ip) return &s_clients[i]; - } - - if (s_client_count >= TG_MINING_MAX_CLIENTS) { - for (int i = 0; i < TG_MINING_MAX_CLIENTS; i++) { - int64_t age = get_time_ms() - s_clients[i].last_share_time_ms; - if (age > TG_MINING_SHARE_WINDOW_S * 2000) { - memset(&s_clients[i], 0, sizeof(tg_mining_client_stats_t)); - s_clients[i].ip = client_ip; - s_clients[i].first_share_time_ms = get_time_ms(); - return &s_clients[i]; - } - } - return NULL; - } - - tg_mining_client_stats_t *c = &s_clients[s_client_count]; - memset(c, 0, sizeof(tg_mining_client_stats_t)); - c->ip = client_ip; - c->first_share_time_ms = get_time_ms(); - s_client_count++; - return c; -} - -void tg_mining_update_hashrate(uint32_t client_ip, bool accepted) -{ - tg_mining_client_stats_t *stats = tg_mining_get_or_create_client(client_ip); - if (!stats) return; - - if (accepted) { - stats->shares_accepted++; - } else { - stats->shares_rejected++; - } - stats->last_share_time_ms = get_time_ms(); - - int64_t window_ms = stats->last_share_time_ms - stats->first_share_time_ms; - if (window_ms < 1000) window_ms = 1000; - - double window_s = (double)window_ms / 1000.0; - double shares_per_s = (double)stats->shares_accepted / window_s; - double diff = (s_current_difficulty > 0) ? (double)s_current_difficulty : 1.0; - stats->hashrate_ghs = shares_per_s * diff * 4294967296.0 / 1e9; -} - -const tg_mining_client_stats_t *tg_mining_get_client_stats(uint32_t client_ip) -{ - for (int i = 0; i < s_client_count; i++) { - if (s_clients[i].ip == client_ip) return &s_clients[i]; - } - return NULL; -} - -double tg_mining_get_current_hashprice(void) -{ - return s_current_hashprice; -} - -void tg_mining_set_current_nbits(uint32_t nbits) -{ - s_current_nbits = nbits; - s_current_difficulty = tg_mining_nbits_to_difficulty(nbits); - s_current_hashprice = tg_mining_calculate_hashprice(nbits); - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) p->log_info(TAG, "nbits: 0x%08lx, diff=%llu, hashprice=%.6f sat/GH/s/day", - (unsigned long)nbits, (unsigned long long)s_current_difficulty, s_current_hashprice); -} - -void tg_mining_init(void) -{ - memset(s_clients, 0, sizeof(s_clients)); - s_client_count = 0; - s_current_hashprice = 0.0; - s_current_nbits = 0; - s_current_difficulty = 1; - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) p->log_info(TAG, "Mining payment initialized"); -} diff --git a/tollgate_core/src/tollgate_mining.h b/tollgate_core/src/tollgate_mining.h deleted file mode 100644 index 39681a5..0000000 --- a/tollgate_core/src/tollgate_mining.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef TOLLGATE_CORE_MINING_H -#define TOLLGATE_CORE_MINING_H - -#include -#include - -#define TG_MINING_SHARE_WINDOW_S 30 -#define TG_MINING_BLOCK_SUBSIDY_SATS 312500000ULL -#define TG_MINING_BLOCKS_PER_DAY 144ULL -#define TG_MINING_MAX_CLIENTS 10 - -typedef struct { - uint32_t ip; - uint64_t shares_accepted; - uint64_t shares_rejected; - int64_t first_share_time_ms; - int64_t last_share_time_ms; - double hashrate_ghs; -} tg_mining_client_stats_t; - -uint64_t tg_mining_nbits_to_difficulty(uint32_t nbits); -double tg_mining_calculate_hashprice(uint32_t nbits); -double tg_mining_calculate_hashprice_override(uint64_t sats_per_ghs_day); -int tg_mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len); -uint64_t tg_mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice, int price, int step_ms); -uint64_t tg_mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice, int price, int step_bytes); -tg_mining_client_stats_t *tg_mining_get_or_create_client(uint32_t client_ip); -void tg_mining_update_hashrate(uint32_t client_ip, bool accepted); -const tg_mining_client_stats_t *tg_mining_get_client_stats(uint32_t client_ip); -double tg_mining_get_current_hashprice(void); -void tg_mining_set_current_nbits(uint32_t nbits); -void tg_mining_init(void); - -#endif diff --git a/tollgate_core/src/tollgate_session.c b/tollgate_core/src/tollgate_session.c deleted file mode 100644 index 667dbd0..0000000 --- a/tollgate_core/src/tollgate_session.c +++ /dev/null @@ -1,220 +0,0 @@ -#include "tollgate_session.h" -#include "tollgate_core.h" -#include "tollgate_platform.h" -#include "tollgate_firewall.h" -#include -#include - -static const char *TAG = "tg_session"; -static tg_session_t s_sessions[TG_SESSION_MAX_CLIENTS]; -static int s_session_count = 0; - -static void format_ip(uint32_t ip, char *buf, int buf_len) -{ - snprintf(buf, buf_len, "%d.%d.%d.%d", - (int)((ip >> 0) & 0xFF), (int)((ip >> 8) & 0xFF), - (int)((ip >> 16) & 0xFF), (int)((ip >> 24) & 0xFF)); -} - -static int64_t get_time_ms(void) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->get_time_ms) return p->get_time_ms(); - return 0; -} - -static void log_session(const char *verb, uint32_t client_ip, const char *mac, const char *detail) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) { - char ip_str[16]; - format_ip(client_ip, ip_str, sizeof(ip_str)); - p->log_info(TAG, "%s: %s mac=%s %s", verb, ip_str, mac ? mac : "unknown", detail ? detail : ""); - } -} - -int tg_session_init(void) -{ - memset(s_sessions, 0, sizeof(s_sessions)); - s_session_count = 0; - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_info) p->log_info(TAG, "Session manager initialized"); - return 0; -} - -static void populate_mac(tg_session_t *session, uint32_t client_ip) -{ - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->mac_for_ip) { - if (!p->mac_for_ip(client_ip, session->mac, sizeof(session->mac))) { - session->mac[0] = '\0'; - } - } else { - session->mac[0] = '\0'; - } -} - -tg_session_t *tg_session_create(uint32_t client_ip, uint64_t allotment_ms) -{ - tg_session_t *existing = tg_session_find_by_ip(client_ip); - if (existing) { - tg_session_extend(existing, allotment_ms); - return existing; - } - - if (s_session_count >= TG_SESSION_MAX_CLIENTS) { - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (!s_sessions[i].active || tg_session_is_expired(&s_sessions[i])) { - tg_session_revoke(&s_sessions[i]); - break; - } - } - } - - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (!s_sessions[i].active) { - s_sessions[i].client_ip = client_ip; - s_sessions[i].allotment_ms = allotment_ms; - s_sessions[i].start_time_ms = get_time_ms(); - s_sessions[i].active = true; - s_sessions[i].payment_method = TG_PAYMENT_CASHU; - populate_mac(&s_sessions[i], client_ip); - - s_session_count++; - tg_firewall_grant(client_ip); - - char detail[64]; - snprintf(detail, sizeof(detail), "allotment=%llums", (unsigned long long)allotment_ms); - log_session("created", client_ip, - s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", detail); - return &s_sessions[i]; - } - } - - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->log_warn) p->log_warn(TAG, "No free session slots"); - return NULL; -} - -tg_session_t *tg_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes) -{ - tg_session_t *s = tg_session_create(client_ip, 0); - if (s) { - s->allotment_bytes = allotment_bytes; - s->bytes_consumed = 0; - s->allotment_ms = (uint64_t)-1; - s->payment_method = TG_PAYMENT_BYTES; - char detail[64]; - snprintf(detail, sizeof(detail), "allotment=%llu bytes", (unsigned long long)allotment_bytes); - log_session("bytes session", client_ip, s->mac[0] ? s->mac : "unknown", detail); - } - return s; -} - -void tg_session_add_bytes(uint32_t client_ip, uint64_t bytes) -{ - tg_session_t *s = tg_session_find_by_ip(client_ip); - if (s && s->active) { - s->bytes_consumed += bytes; - } -} - -tg_session_t *tg_session_find_by_ip(uint32_t client_ip) -{ - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (s_sessions[i].active && s_sessions[i].client_ip == client_ip) { - return &s_sessions[i]; - } - } - return NULL; -} - -tg_session_t *tg_session_find_by_mac(const char *mac) -{ - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (s_sessions[i].active && s_sessions[i].mac[0] != '\0' && - strcmp(s_sessions[i].mac, mac) == 0) { - return &s_sessions[i]; - } - } - return NULL; -} - -void tg_session_extend(tg_session_t *session, uint64_t additional_ms) -{ - if (!session || !session->active) return; - session->allotment_ms += additional_ms; - char detail[64]; - snprintf(detail, sizeof(detail), "+%llums (total=%llu)", - (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms); - log_session("extended", session->client_ip, - session->mac[0] ? session->mac : "unknown", detail); -} - -bool tg_session_is_expired(const tg_session_t *session) -{ - if (!session || !session->active) return true; - - const tollgate_platform_t *p = tollgate_core_get_platform(); - if (p && p->get_metric) { - const char *metric = p->get_metric(); - if (metric && strcmp(metric, "bytes") == 0) { - return session->bytes_consumed >= session->allotment_bytes; - } - } - - int64_t elapsed = get_time_ms() - session->start_time_ms; - return elapsed >= (int64_t)session->allotment_ms; -} - -static void check_expiry(void) -{ - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (s_sessions[i].active && tg_session_is_expired(&s_sessions[i])) { - log_session("expired", s_sessions[i].client_ip, - s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", NULL); - tg_session_revoke(&s_sessions[i]); - } - } -} - -void tg_session_revoke(tg_session_t *session) -{ - if (!session || !session->active) return; - tg_firewall_revoke(session->client_ip); - session->active = false; - s_session_count--; -} - -void tg_session_revoke_all(void) -{ - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (s_sessions[i].active) { - tg_session_revoke(&s_sessions[i]); - } - } -} - -int tg_session_active_count(void) -{ - int count = 0; - for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { - if (s_sessions[i].active) count++; - } - return count; -} - -void tg_session_tick(void) -{ - check_expiry(); -} - -tg_session_t *tg_session_get_array(void) -{ - return s_sessions; -} - -int tg_session_get_array_size(void) -{ - return TG_SESSION_MAX_CLIENTS; -} diff --git a/tollgate_core/src/tollgate_session.h b/tollgate_core/src/tollgate_session.h deleted file mode 100644 index 4008b24..0000000 --- a/tollgate_core/src/tollgate_session.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef TOLLGATE_CORE_SESSION_H -#define TOLLGATE_CORE_SESSION_H - -#include -#include - -#define TG_SESSION_MAX_CLIENTS 10 -#define TG_SESSION_MAX_MAC_LEN 18 - -typedef enum { - TG_PAYMENT_CASHU, - TG_PAYMENT_MINING, - TG_PAYMENT_BYTES -} tg_payment_method_t; - -typedef struct { - uint32_t client_ip; - char mac[TG_SESSION_MAX_MAC_LEN]; - uint64_t allotment_ms; - int64_t start_time_ms; - uint64_t allotment_bytes; - uint64_t bytes_consumed; - tg_payment_method_t payment_method; - bool active; -} tg_session_t; - -int tg_session_init(void); -tg_session_t *tg_session_create(uint32_t client_ip, uint64_t allotment_ms); -tg_session_t *tg_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes); -void tg_session_add_bytes(uint32_t client_ip, uint64_t bytes); -tg_session_t *tg_session_find_by_ip(uint32_t client_ip); -tg_session_t *tg_session_find_by_mac(const char *mac); -void tg_session_extend(tg_session_t *session, uint64_t additional_ms); -bool tg_session_is_expired(const tg_session_t *session); -void tg_session_revoke(tg_session_t *session); -void tg_session_revoke_all(void); -int tg_session_active_count(void); -void tg_session_tick(void); -tg_session_t *tg_session_get_array(void); -int tg_session_get_array_size(void); - -#endif -- cgit v1.2.3