diff options
Diffstat (limited to 'components')
| -rw-r--r-- | components/tollgate_core/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | components/tollgate_core/idf_component.yml | 6 | ||||
| -rw-r--r-- | components/tollgate_core/include/tollgate_core.h | 34 | ||||
| -rw-r--r-- | components/tollgate_core/include/tollgate_platform.h | 17 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core.c | 236 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_cashu.c | 262 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_cashu.h | 42 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_dns.c | 316 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_dns.h | 13 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_firewall.c | 165 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_firewall.h | 25 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_session.c | 200 | ||||
| -rw-r--r-- | components/tollgate_core/src/tollgate_core_session.h | 47 |
13 files changed, 1372 insertions, 0 deletions
diff --git a/components/tollgate_core/CMakeLists.txt b/components/tollgate_core/CMakeLists.txt new file mode 100644 index 0000000..fc6b6f1 --- /dev/null +++ b/components/tollgate_core/CMakeLists.txt | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | idf_component_register( | ||
| 2 | SRCS "src/tollgate_core.c" | ||
| 3 | "src/tollgate_core_cashu.c" | ||
| 4 | "src/tollgate_core_dns.c" | ||
| 5 | "src/tollgate_core_firewall.c" | ||
| 6 | "src/tollgate_core_session.c" | ||
| 7 | INCLUDE_DIRS "include" "src" | ||
| 8 | REQUIRES lwip json esp_http_client mbedtls log esp_netif | ||
| 9 | PRIV_REQUIRES esp_wifi) | ||
diff --git a/components/tollgate_core/idf_component.yml b/components/tollgate_core/idf_component.yml new file mode 100644 index 0000000..112aa18 --- /dev/null +++ b/components/tollgate_core/idf_component.yml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | version: "1.0.0" | ||
| 2 | description: TollGate core component — Cashu payment processing, per-client DNS/firewall, session management for paid WiFi hotspots | ||
| 3 | url: https://github.com/nicoulaj/esp32-tollgate | ||
| 4 | dependencies: | ||
| 5 | idf: | ||
| 6 | version: ">=5.4.0" | ||
diff --git a/components/tollgate_core/include/tollgate_core.h b/components/tollgate_core/include/tollgate_core.h new file mode 100644 index 0000000..c47ebeb --- /dev/null +++ b/components/tollgate_core/include/tollgate_core.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_H | ||
| 2 | #define TOLLGATE_CORE_H | ||
| 3 | |||
| 4 | #include "tollgate_platform.h" | ||
| 5 | #include "esp_err.h" | ||
| 6 | #include "esp_netif.h" | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | |||
| 10 | esp_err_t tollgate_core_init(const tollgate_platform_t *platform, esp_ip4_addr_t ap_ip); | ||
| 11 | |||
| 12 | esp_err_t tollgate_core_dns_start(esp_ip4_addr_t upstream_dns); | ||
| 13 | void tollgate_core_dns_stop(void); | ||
| 14 | |||
| 15 | esp_err_t tollgate_core_process_payment(uint32_t client_ip, const char *token_str); | ||
| 16 | |||
| 17 | void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip); | ||
| 18 | void tollgate_core_client_disconnected(const uint8_t *mac); | ||
| 19 | |||
| 20 | void tollgate_core_tick(void); | ||
| 21 | |||
| 22 | bool tollgate_core_is_client_allowed(uint32_t client_ip); | ||
| 23 | bool tollgate_core_is_dns_running(void); | ||
| 24 | |||
| 25 | char *tollgate_core_get_status_json(void); | ||
| 26 | char *tollgate_core_get_config_json(void); | ||
| 27 | |||
| 28 | int tollgate_core_active_session_count(void); | ||
| 29 | int tollgate_core_allowed_client_count(void); | ||
| 30 | |||
| 31 | bool tollgate_core_is_owner(uint32_t client_ip); | ||
| 32 | bool tollgate_core_is_owner_connected(void); | ||
| 33 | |||
| 34 | #endif | ||
diff --git a/components/tollgate_core/include/tollgate_platform.h b/components/tollgate_core/include/tollgate_platform.h new file mode 100644 index 0000000..f60f1f9 --- /dev/null +++ b/components/tollgate_core/include/tollgate_platform.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef TOLLGATE_PLATFORM_H | ||
| 2 | #define TOLLGATE_PLATFORM_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | uint16_t (*get_price_sats)(void); | ||
| 9 | int32_t (*get_step_ms)(void); | ||
| 10 | const char * (*get_mint_url)(void); | ||
| 11 | const char * (*get_metric)(void); | ||
| 12 | int32_t (*get_step_bytes)(void); | ||
| 13 | int64_t (*get_time_ms)(void); | ||
| 14 | bool (*spend_proofs)(const char *raw_token_json); | ||
| 15 | } tollgate_platform_t; | ||
| 16 | |||
| 17 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core.c b/components/tollgate_core/src/tollgate_core.c new file mode 100644 index 0000000..a731f48 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core.c | |||
| @@ -0,0 +1,236 @@ | |||
| 1 | #include "tollgate_core.h" | ||
| 2 | #include "tollgate_core_cashu.h" | ||
| 3 | #include "tollgate_core_dns.h" | ||
| 4 | #include "tollgate_core_firewall.h" | ||
| 5 | #include "tollgate_core_session.h" | ||
| 6 | #include "esp_log.h" | ||
| 7 | #include "cJSON.h" | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | static const char *TAG = "tg_core"; | ||
| 11 | |||
| 12 | static const tollgate_platform_t *s_platform; | ||
| 13 | static esp_ip4_addr_t s_ap_ip; | ||
| 14 | |||
| 15 | static uint32_t s_owner_ip; | ||
| 16 | static uint8_t s_owner_mac[6]; | ||
| 17 | static bool s_owner_connected; | ||
| 18 | |||
| 19 | esp_err_t tollgate_core_init(const tollgate_platform_t *platform, esp_ip4_addr_t ap_ip) | ||
| 20 | { | ||
| 21 | if (!platform) { | ||
| 22 | ESP_LOGE(TAG, "Platform callbacks required"); | ||
| 23 | return ESP_FAIL; | ||
| 24 | } | ||
| 25 | |||
| 26 | s_platform = platform; | ||
| 27 | s_ap_ip = ap_ip; | ||
| 28 | s_owner_connected = false; | ||
| 29 | memset(s_owner_mac, 0, sizeof(s_owner_mac)); | ||
| 30 | |||
| 31 | if (!platform->spend_proofs) { | ||
| 32 | ESP_LOGW(TAG, "spend_proofs is NULL — double-spend window open until wallet integrated"); | ||
| 33 | } | ||
| 34 | |||
| 35 | tollgate_core_session_set_platform(platform); | ||
| 36 | tollgate_core_session_init(); | ||
| 37 | tollgate_core_fw_init(ap_ip); | ||
| 38 | |||
| 39 | ESP_LOGI(TAG, "TollGate core initialized, AP IP=" IPSTR, IP2STR(&ap_ip)); | ||
| 40 | return ESP_OK; | ||
| 41 | } | ||
| 42 | |||
| 43 | esp_err_t tollgate_core_dns_start(esp_ip4_addr_t upstream_dns) | ||
| 44 | { | ||
| 45 | return tollgate_core_dns_start_internal(s_ap_ip, upstream_dns); | ||
| 46 | } | ||
| 47 | |||
| 48 | esp_err_t tollgate_core_process_payment(uint32_t client_ip, const char *token_str) | ||
| 49 | { | ||
| 50 | if (!s_platform || !token_str) return ESP_FAIL; | ||
| 51 | |||
| 52 | const char *accepted_mint = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL; | ||
| 53 | if (!accepted_mint || accepted_mint[0] == '\0') { | ||
| 54 | ESP_LOGE(TAG, "No mint URL configured"); | ||
| 55 | return ESP_FAIL; | ||
| 56 | } | ||
| 57 | |||
| 58 | tg_cashu_token_t token; | ||
| 59 | esp_err_t ret = tollgate_core_cashu_decode_token(token_str, &token); | ||
| 60 | if (ret != ESP_OK) { | ||
| 61 | ESP_LOGE(TAG, "Token decode failed"); | ||
| 62 | return ESP_FAIL; | ||
| 63 | } | ||
| 64 | |||
| 65 | if (!tollgate_core_cashu_is_mint_accepted(token.mint_url, accepted_mint)) { | ||
| 66 | ESP_LOGE(TAG, "Token mint '%s' not accepted (expected '%s')", token.mint_url, accepted_mint); | ||
| 67 | return ESP_FAIL; | ||
| 68 | } | ||
| 69 | |||
| 70 | tg_cashu_proof_state_t states[TG_CASHU_MAX_PROOFS]; | ||
| 71 | int state_count = 0; | ||
| 72 | ret = tollgate_core_cashu_check_proof_states(token.mint_url, &token, states, &state_count); | ||
| 73 | if (ret != ESP_OK) { | ||
| 74 | ESP_LOGE(TAG, "Proof state check failed"); | ||
| 75 | return ESP_FAIL; | ||
| 76 | } | ||
| 77 | |||
| 78 | for (int i = 0; i < state_count; i++) { | ||
| 79 | if (states[i].spent) { | ||
| 80 | ESP_LOGE(TAG, "Proof %d is SPENT — rejecting", i); | ||
| 81 | return ESP_FAIL; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | if (s_platform->spend_proofs) { | ||
| 86 | if (!s_platform->spend_proofs(token_str)) { | ||
| 87 | ESP_LOGE(TAG, "spend_proofs rejected the token"); | ||
| 88 | return ESP_FAIL; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | const char *metric = s_platform->get_metric ? s_platform->get_metric() : "milliseconds"; | ||
| 93 | uint64_t price = s_platform->get_price_sats ? s_platform->get_price_sats() : 21; | ||
| 94 | uint64_t step_size; | ||
| 95 | |||
| 96 | if (strcmp(metric, "bytes") == 0) { | ||
| 97 | step_size = s_platform->get_step_bytes ? (uint64_t)s_platform->get_step_bytes() : 22020096; | ||
| 98 | } else { | ||
| 99 | step_size = s_platform->get_step_ms ? (uint64_t)s_platform->get_step_ms() : 60000; | ||
| 100 | } | ||
| 101 | |||
| 102 | uint64_t allotment = tollgate_core_cashu_calculate_allotment(token.total_amount, price, step_size); | ||
| 103 | if (allotment == 0) { | ||
| 104 | ESP_LOGE(TAG, "Token amount %llu too small for price %llu", | ||
| 105 | (unsigned long long)token.total_amount, (unsigned long long)price); | ||
| 106 | return ESP_FAIL; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (strcmp(metric, "bytes") == 0) { | ||
| 110 | if (!tollgate_core_session_create_bytes(client_ip, allotment)) { | ||
| 111 | return ESP_FAIL; | ||
| 112 | } | ||
| 113 | } else { | ||
| 114 | if (!tollgate_core_session_create(client_ip, allotment)) { | ||
| 115 | return ESP_FAIL; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | ESP_LOGI(TAG, "Payment processed: %llu sats → %llu %s allotment for client", | ||
| 120 | (unsigned long long)token.total_amount, (unsigned long long)allotment, metric); | ||
| 121 | return ESP_OK; | ||
| 122 | } | ||
| 123 | |||
| 124 | void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip) | ||
| 125 | { | ||
| 126 | if (!s_owner_connected) { | ||
| 127 | s_owner_connected = true; | ||
| 128 | s_owner_ip = client_ip; | ||
| 129 | if (mac) memcpy(s_owner_mac, mac, 6); | ||
| 130 | |||
| 131 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 132 | ESP_LOGI(TAG, "First client = owner: " IPSTR, IP2STR(&ip)); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | ESP_LOGI(TAG, "Client connected (non-owner): " IPSTR, IP2STR(&(esp_ip4_addr_t){.addr=client_ip})); | ||
| 137 | } | ||
| 138 | |||
| 139 | void tollgate_core_client_disconnected(const uint8_t *mac) | ||
| 140 | { | ||
| 141 | if (!s_owner_connected) return; | ||
| 142 | |||
| 143 | if (mac && memcmp(s_owner_mac, mac, 6) == 0) { | ||
| 144 | ESP_LOGI(TAG, "Owner disconnected — reassigning"); | ||
| 145 | s_owner_connected = false; | ||
| 146 | memset(s_owner_mac, 0, sizeof(s_owner_mac)); | ||
| 147 | |||
| 148 | int fw_count = tollgate_core_fw_client_count(); | ||
| 149 | if (fw_count > 0) { | ||
| 150 | tg_session_t *sessions = tollgate_core_session_get_array(); | ||
| 151 | for (int i = 0; i < tollgate_core_session_get_array_size(); i++) { | ||
| 152 | if (sessions[i].active && sessions[i].mac[0] != '\0') { | ||
| 153 | if (memcmp(sessions[i].mac, mac, 6) != 0) { | ||
| 154 | s_owner_connected = true; | ||
| 155 | s_owner_ip = sessions[i].client_ip; | ||
| 156 | ESP_LOGI(TAG, "New owner: " IPSTR, IP2STR(&(esp_ip4_addr_t){.addr=s_owner_ip})); | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | |||
| 165 | ESP_LOGI(TAG, "Client disconnected"); | ||
| 166 | } | ||
| 167 | |||
| 168 | void tollgate_core_tick(void) | ||
| 169 | { | ||
| 170 | tollgate_core_session_tick(); | ||
| 171 | } | ||
| 172 | |||
| 173 | bool tollgate_core_is_client_allowed(uint32_t client_ip) | ||
| 174 | { | ||
| 175 | return tollgate_core_fw_is_allowed(client_ip); | ||
| 176 | } | ||
| 177 | |||
| 178 | bool tollgate_core_is_dns_running(void) | ||
| 179 | { | ||
| 180 | return tollgate_core_dns_is_running(); | ||
| 181 | } | ||
| 182 | |||
| 183 | char *tollgate_core_get_status_json(void) | ||
| 184 | { | ||
| 185 | cJSON *root = cJSON_CreateObject(); | ||
| 186 | cJSON_AddBoolToObject(root, "ownerConnected", s_owner_connected); | ||
| 187 | cJSON_AddNumberToObject(root, "activeSessions", tollgate_core_session_active_count()); | ||
| 188 | cJSON_AddNumberToObject(root, "allowedClients", tollgate_core_fw_client_count()); | ||
| 189 | cJSON_AddBoolToObject(root, "dnsRunning", tollgate_core_dns_is_running()); | ||
| 190 | |||
| 191 | char *json = cJSON_PrintUnformatted(root); | ||
| 192 | cJSON_Delete(root); | ||
| 193 | return json; | ||
| 194 | } | ||
| 195 | |||
| 196 | char *tollgate_core_get_config_json(void) | ||
| 197 | { | ||
| 198 | cJSON *root = cJSON_CreateObject(); | ||
| 199 | |||
| 200 | if (s_platform) { | ||
| 201 | if (s_platform->get_price_sats) | ||
| 202 | cJSON_AddNumberToObject(root, "priceSats", s_platform->get_price_sats()); | ||
| 203 | if (s_platform->get_step_ms) | ||
| 204 | cJSON_AddNumberToObject(root, "stepMs", s_platform->get_step_ms()); | ||
| 205 | if (s_platform->get_mint_url) | ||
| 206 | cJSON_AddStringToObject(root, "mintUrl", s_platform->get_mint_url()); | ||
| 207 | if (s_platform->get_metric) | ||
| 208 | cJSON_AddStringToObject(root, "metric", s_platform->get_metric()); | ||
| 209 | if (s_platform->get_step_bytes) | ||
| 210 | cJSON_AddNumberToObject(root, "stepBytes", s_platform->get_step_bytes()); | ||
| 211 | } | ||
| 212 | |||
| 213 | char *json = cJSON_PrintUnformatted(root); | ||
| 214 | cJSON_Delete(root); | ||
| 215 | return json; | ||
| 216 | } | ||
| 217 | |||
| 218 | int tollgate_core_active_session_count(void) | ||
| 219 | { | ||
| 220 | return tollgate_core_session_active_count(); | ||
| 221 | } | ||
| 222 | |||
| 223 | int tollgate_core_allowed_client_count(void) | ||
| 224 | { | ||
| 225 | return tollgate_core_fw_client_count(); | ||
| 226 | } | ||
| 227 | |||
| 228 | bool tollgate_core_is_owner(uint32_t client_ip) | ||
| 229 | { | ||
| 230 | return s_owner_connected && s_owner_ip == client_ip; | ||
| 231 | } | ||
| 232 | |||
| 233 | bool tollgate_core_is_owner_connected(void) | ||
| 234 | { | ||
| 235 | return s_owner_connected; | ||
| 236 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_cashu.c b/components/tollgate_core/src/tollgate_core_cashu.c new file mode 100644 index 0000000..cf2bf5d --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_cashu.c | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | #include "tollgate_core_cashu.h" | ||
| 2 | #include "esp_log.h" | ||
| 3 | #include "esp_http_client.h" | ||
| 4 | #include "cJSON.h" | ||
| 5 | #include "mbedtls/base64.h" | ||
| 6 | #include "mbedtls/sha256.h" | ||
| 7 | #include "esp_crt_bundle.h" | ||
| 8 | |||
| 9 | static const char *TAG = "tg_core_cashu"; | ||
| 10 | |||
| 11 | static const char V3_PREFIX[] = "cashuA"; | ||
| 12 | static const size_t V3_PREFIX_LEN = 6; | ||
| 13 | |||
| 14 | static int b64url_decode(const char *input, size_t input_len, char *out, size_t out_size, size_t *out_len) | ||
| 15 | { | ||
| 16 | char *b64 = malloc(input_len + 4); | ||
| 17 | if (!b64) return -1; | ||
| 18 | size_t b64_len = input_len; | ||
| 19 | memcpy(b64, input, b64_len); | ||
| 20 | b64[b64_len] = '\0'; | ||
| 21 | |||
| 22 | for (size_t i = 0; i < b64_len; i++) { | ||
| 23 | if (b64[i] == '-') b64[i] = '+'; | ||
| 24 | else if (b64[i] == '_') b64[i] = '/'; | ||
| 25 | } | ||
| 26 | while (b64_len % 4 != 0) { | ||
| 27 | b64[b64_len++] = '='; | ||
| 28 | } | ||
| 29 | b64[b64_len] = '\0'; | ||
| 30 | |||
| 31 | size_t olen = 0; | ||
| 32 | int ret = mbedtls_base64_decode((unsigned char *)out, out_size, &olen, | ||
| 33 | (const unsigned char *)b64, b64_len); | ||
| 34 | free(b64); | ||
| 35 | if (ret != 0) return -1; | ||
| 36 | *out_len = olen; | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | static esp_err_t parse_proofs_array(cJSON *arr, tg_cashu_token_t *out) | ||
| 41 | { | ||
| 42 | if (!cJSON_IsArray(arr)) return ESP_FAIL; | ||
| 43 | int count = cJSON_GetArraySize(arr); | ||
| 44 | if (count > TG_CASHU_MAX_PROOFS) return ESP_FAIL; | ||
| 45 | |||
| 46 | out->proof_count = 0; | ||
| 47 | out->total_amount = 0; | ||
| 48 | for (int i = 0; i < count; i++) { | ||
| 49 | cJSON *proof = cJSON_GetArrayItem(arr, i); | ||
| 50 | cJSON *amt = cJSON_GetObjectItemCaseSensitive(proof, "amount"); | ||
| 51 | cJSON *id = cJSON_GetObjectItemCaseSensitive(proof, "id"); | ||
| 52 | cJSON *secret = cJSON_GetObjectItemCaseSensitive(proof, "secret"); | ||
| 53 | cJSON *c = cJSON_GetObjectItemCaseSensitive(proof, "C"); | ||
| 54 | |||
| 55 | if (!amt || !cJSON_IsNumber(amt)) return ESP_FAIL; | ||
| 56 | |||
| 57 | out->proofs[i].amount = (uint64_t)amt->valuedouble; | ||
| 58 | out->total_amount += out->proofs[i].amount; | ||
| 59 | |||
| 60 | if (id && cJSON_IsString(id)) { | ||
| 61 | strncpy(out->proofs[i].id, id->valuestring, sizeof(out->proofs[i].id) - 1); | ||
| 62 | } | ||
| 63 | if (secret && cJSON_IsString(secret)) { | ||
| 64 | strncpy(out->proofs[i].secret, secret->valuestring, sizeof(out->proofs[i].secret) - 1); | ||
| 65 | } | ||
| 66 | if (c && cJSON_IsString(c)) { | ||
| 67 | strncpy(out->proofs[i].c, c->valuestring, sizeof(out->proofs[i].c) - 1); | ||
| 68 | } | ||
| 69 | out->proof_count++; | ||
| 70 | } | ||
| 71 | return ESP_OK; | ||
| 72 | } | ||
| 73 | |||
| 74 | esp_err_t tollgate_core_cashu_decode_token(const char *token_str, tg_cashu_token_t *out) | ||
| 75 | { | ||
| 76 | if (!token_str || !out) return ESP_FAIL; | ||
| 77 | memset(out, 0, sizeof(*out)); | ||
| 78 | |||
| 79 | size_t len = strlen(token_str); | ||
| 80 | char *nl = strchr(token_str, '\n'); | ||
| 81 | if (nl) len = nl - token_str; | ||
| 82 | char *cr = strchr(token_str, '\r'); | ||
| 83 | if (cr && (cr - token_str) < (int)len) len = cr - token_str; | ||
| 84 | if (len <= V3_PREFIX_LEN) { | ||
| 85 | ESP_LOGE(TAG, "Token too short"); | ||
| 86 | return ESP_FAIL; | ||
| 87 | } | ||
| 88 | if (strncmp(token_str, V3_PREFIX, V3_PREFIX_LEN) != 0) { | ||
| 89 | ESP_LOGE(TAG, "Token missing cashuA prefix"); | ||
| 90 | return ESP_FAIL; | ||
| 91 | } | ||
| 92 | |||
| 93 | size_t b64_len = len - V3_PREFIX_LEN; | ||
| 94 | size_t decoded_size = (b64_len * 3) / 4 + 4; | ||
| 95 | char *json_buf = malloc(decoded_size); | ||
| 96 | if (!json_buf) return ESP_FAIL; | ||
| 97 | size_t json_len = 0; | ||
| 98 | if (b64url_decode(token_str + V3_PREFIX_LEN, b64_len, | ||
| 99 | json_buf, decoded_size - 1, &json_len) != 0) { | ||
| 100 | ESP_LOGE(TAG, "Base64url decode failed"); | ||
| 101 | free(json_buf); | ||
| 102 | return ESP_FAIL; | ||
| 103 | } | ||
| 104 | json_buf[json_len] = '\0'; | ||
| 105 | |||
| 106 | cJSON *root = cJSON_Parse(json_buf); | ||
| 107 | free(json_buf); | ||
| 108 | if (!root) { | ||
| 109 | ESP_LOGE(TAG, "JSON parse failed"); | ||
| 110 | return ESP_FAIL; | ||
| 111 | } | ||
| 112 | |||
| 113 | cJSON *token_arr = cJSON_GetObjectItemCaseSensitive(root, "token"); | ||
| 114 | if (token_arr && cJSON_IsArray(token_arr)) { | ||
| 115 | cJSON *first = cJSON_GetArrayItem(token_arr, 0); | ||
| 116 | if (!first) { cJSON_Delete(root); return ESP_FAIL; } | ||
| 117 | |||
| 118 | cJSON *mint = cJSON_GetObjectItemCaseSensitive(first, "mint"); | ||
| 119 | if (mint && cJSON_IsString(mint)) { | ||
| 120 | strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); | ||
| 121 | } | ||
| 122 | |||
| 123 | cJSON *proofs = cJSON_GetObjectItemCaseSensitive(first, "proofs"); | ||
| 124 | if (proofs) { | ||
| 125 | esp_err_t ret = parse_proofs_array(proofs, out); | ||
| 126 | if (ret != ESP_OK) { cJSON_Delete(root); return ret; } | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | cJSON *mint = cJSON_GetObjectItemCaseSensitive(root, "mint"); | ||
| 130 | if (mint && cJSON_IsString(mint)) { | ||
| 131 | strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); | ||
| 132 | } | ||
| 133 | |||
| 134 | cJSON *proofs = cJSON_GetObjectItemCaseSensitive(root, "proofs"); | ||
| 135 | if (proofs) { | ||
| 136 | esp_err_t ret = parse_proofs_array(proofs, out); | ||
| 137 | if (ret != ESP_OK) { cJSON_Delete(root); return ret; } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | cJSON_Delete(root); | ||
| 142 | |||
| 143 | if (out->proof_count == 0) { | ||
| 144 | ESP_LOGE(TAG, "No proofs in token"); | ||
| 145 | return ESP_FAIL; | ||
| 146 | } | ||
| 147 | |||
| 148 | ESP_LOGI(TAG, "Decoded token: %d proofs, total=%llu, mint=%s", | ||
| 149 | out->proof_count, (unsigned long long)out->total_amount, out->mint_url); | ||
| 150 | return ESP_OK; | ||
| 151 | } | ||
| 152 | |||
| 153 | static void sha256_hex(const char *data, size_t data_len, char *hex_out) | ||
| 154 | { | ||
| 155 | uint8_t hash[32]; | ||
| 156 | mbedtls_sha256((const unsigned char *)data, data_len, hash, 0); | ||
| 157 | for (int i = 0; i < 32; i++) { | ||
| 158 | sprintf(hex_out + i * 2, "%02x", hash[i]); | ||
| 159 | } | ||
| 160 | hex_out[64] = '\0'; | ||
| 161 | } | ||
| 162 | |||
| 163 | esp_err_t tollgate_core_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, | ||
| 164 | tg_cashu_proof_state_t *states, int *state_count) | ||
| 165 | { | ||
| 166 | cJSON *ys_arr = cJSON_CreateArray(); | ||
| 167 | for (int i = 0; i < token->proof_count; i++) { | ||
| 168 | char y_hex[65]; | ||
| 169 | sha256_hex(token->proofs[i].secret, strlen(token->proofs[i].secret), y_hex); | ||
| 170 | cJSON_AddItemToArray(ys_arr, cJSON_CreateString(y_hex)); | ||
| 171 | strncpy(states[i].y_hex, y_hex, sizeof(states[i].y_hex) - 1); | ||
| 172 | states[i].spent = false; | ||
| 173 | } | ||
| 174 | *state_count = token->proof_count; | ||
| 175 | |||
| 176 | char *ys_json = cJSON_PrintUnformatted(ys_arr); | ||
| 177 | cJSON_Delete(ys_arr); | ||
| 178 | |||
| 179 | char *post_body = malloc(4096); | ||
| 180 | if (!post_body) { cJSON_free(ys_json); return ESP_FAIL; } | ||
| 181 | snprintf(post_body, 4096, "{\"Ys\":%s}", ys_json); | ||
| 182 | cJSON_free(ys_json); | ||
| 183 | |||
| 184 | char url[512]; | ||
| 185 | snprintf(url, sizeof(url), "%s/v1/checkstate", mint_url); | ||
| 186 | |||
| 187 | char *resp_buf = malloc(8192); | ||
| 188 | if (!resp_buf) { free(post_body); return ESP_FAIL; } | ||
| 189 | |||
| 190 | esp_http_client_config_t config = { | ||
| 191 | .url = url, | ||
| 192 | .method = HTTP_METHOD_POST, | ||
| 193 | .timeout_ms = 15000, | ||
| 194 | .crt_bundle_attach = esp_crt_bundle_attach, | ||
| 195 | }; | ||
| 196 | esp_http_client_handle_t client = esp_http_client_init(&config); | ||
| 197 | if (!client) { free(post_body); free(resp_buf); return ESP_FAIL; } | ||
| 198 | |||
| 199 | esp_http_client_set_header(client, "Content-Type", "application/json"); | ||
| 200 | esp_err_t err = esp_http_client_open(client, strlen(post_body)); | ||
| 201 | if (err != ESP_OK) { | ||
| 202 | ESP_LOGE(TAG, "checkstate open failed: %s", esp_err_to_name(err)); | ||
| 203 | esp_http_client_cleanup(client); | ||
| 204 | free(post_body); | ||
| 205 | free(resp_buf); | ||
| 206 | return ESP_FAIL; | ||
| 207 | } | ||
| 208 | int written = esp_http_client_write(client, post_body, strlen(post_body)); | ||
| 209 | free(post_body); | ||
| 210 | ESP_LOGI(TAG, "checkstate written %d bytes", written); | ||
| 211 | |||
| 212 | int content_length = esp_http_client_fetch_headers(client); | ||
| 213 | int status = esp_http_client_get_status_code(client); | ||
| 214 | ESP_LOGI(TAG, "checkstate headers: status=%d, content_length=%d", status, content_length); | ||
| 215 | |||
| 216 | int resp_len = esp_http_client_read(client, resp_buf, 8191); | ||
| 217 | ESP_LOGI(TAG, "checkstate read: resp_len=%d", resp_len); | ||
| 218 | esp_http_client_cleanup(client); | ||
| 219 | |||
| 220 | if (status != 200 || resp_len <= 0) { | ||
| 221 | ESP_LOGE(TAG, "checkstate failed: status=%d, resp_len=%d", status, resp_len); | ||
| 222 | free(resp_buf); | ||
| 223 | return ESP_FAIL; | ||
| 224 | } | ||
| 225 | resp_buf[resp_len] = '\0'; | ||
| 226 | |||
| 227 | cJSON *root_resp = cJSON_Parse(resp_buf); | ||
| 228 | free(resp_buf); | ||
| 229 | if (!root_resp) return ESP_FAIL; | ||
| 230 | |||
| 231 | cJSON *states_arr = cJSON_GetObjectItemCaseSensitive(root_resp, "states"); | ||
| 232 | if (!states_arr || !cJSON_IsArray(states_arr)) { | ||
| 233 | cJSON_Delete(root_resp); | ||
| 234 | return ESP_FAIL; | ||
| 235 | } | ||
| 236 | |||
| 237 | int n = cJSON_GetArraySize(states_arr); | ||
| 238 | for (int i = 0; i < n && i < token->proof_count; i++) { | ||
| 239 | cJSON *s = cJSON_GetArrayItem(states_arr, i); | ||
| 240 | cJSON *state = cJSON_GetObjectItemCaseSensitive(s, "state"); | ||
| 241 | if (state && cJSON_IsString(state)) { | ||
| 242 | states[i].spent = (strcmp(state->valuestring, "SPENT") == 0); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | cJSON_Delete(root_resp); | ||
| 247 | return ESP_OK; | ||
| 248 | } | ||
| 249 | |||
| 250 | uint64_t tollgate_core_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, | ||
| 251 | uint64_t step_size) | ||
| 252 | { | ||
| 253 | if (price_per_step == 0) return 0; | ||
| 254 | return (token_amount / price_per_step) * step_size; | ||
| 255 | } | ||
| 256 | |||
| 257 | bool tollgate_core_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url) | ||
| 258 | { | ||
| 259 | if (!mint_url || mint_url[0] == '\0') return false; | ||
| 260 | if (!accepted_mint_url || accepted_mint_url[0] == '\0') return false; | ||
| 261 | return (strcmp(mint_url, accepted_mint_url) == 0); | ||
| 262 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_cashu.h b/components/tollgate_core/src/tollgate_core_cashu.h new file mode 100644 index 0000000..70d2a02 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_cashu.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_CASHU_H | ||
| 2 | #define TOLLGATE_CORE_CASHU_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | |||
| 8 | #define TG_CASHU_MAX_PROOFS 10 | ||
| 9 | #define TG_CASHU_MAX_SECRET_LEN 128 | ||
| 10 | #define TG_CASHU_MAX_ID_LEN 68 | ||
| 11 | #define TG_CASHU_MAX_C_LEN 128 | ||
| 12 | |||
| 13 | typedef struct { | ||
| 14 | uint64_t amount; | ||
| 15 | char id[TG_CASHU_MAX_ID_LEN]; | ||
| 16 | char secret[TG_CASHU_MAX_SECRET_LEN]; | ||
| 17 | char c[TG_CASHU_MAX_C_LEN]; | ||
| 18 | } tg_cashu_proof_t; | ||
| 19 | |||
| 20 | typedef struct { | ||
| 21 | tg_cashu_proof_t proofs[TG_CASHU_MAX_PROOFS]; | ||
| 22 | int proof_count; | ||
| 23 | char mint_url[256]; | ||
| 24 | uint64_t total_amount; | ||
| 25 | } tg_cashu_token_t; | ||
| 26 | |||
| 27 | typedef struct { | ||
| 28 | char y_hex[65]; | ||
| 29 | bool spent; | ||
| 30 | } tg_cashu_proof_state_t; | ||
| 31 | |||
| 32 | esp_err_t tollgate_core_cashu_decode_token(const char *token_str, tg_cashu_token_t *out); | ||
| 33 | |||
| 34 | esp_err_t tollgate_core_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, | ||
| 35 | tg_cashu_proof_state_t *states, int *state_count); | ||
| 36 | |||
| 37 | uint64_t tollgate_core_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, | ||
| 38 | uint64_t step_size); | ||
| 39 | |||
| 40 | bool tollgate_core_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url); | ||
| 41 | |||
| 42 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_dns.c b/components/tollgate_core/src/tollgate_core_dns.c new file mode 100644 index 0000000..84322e6 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_dns.c | |||
| @@ -0,0 +1,316 @@ | |||
| 1 | #include "tollgate_core_dns.h" | ||
| 2 | #include "esp_log.h" | ||
| 3 | #include "freertos/FreeRTOS.h" | ||
| 4 | #include "freertos/task.h" | ||
| 5 | #include "lwip/sockets.h" | ||
| 6 | #include "lwip/netdb.h" | ||
| 7 | #include <string.h> | ||
| 8 | #include <sys/param.h> | ||
| 9 | |||
| 10 | #define MAX_AUTH_IPS 10 | ||
| 11 | #define DNS_BUF_SIZE 512 | ||
| 12 | #define DNS_PORT 53 | ||
| 13 | #define DOT_PORT 853 | ||
| 14 | #define DNS_TASK_STACK 4096 | ||
| 15 | #define DOT_TASK_STACK 3072 | ||
| 16 | #define DNS_TASK_PRIO 5 | ||
| 17 | #define DOT_TASK_PRIO 5 | ||
| 18 | #define DNS_FORWARD_TIMEOUT_MS 2000 | ||
| 19 | #define NXDOMAIN_TTL 30 | ||
| 20 | #define HIJACK_TTL 10 | ||
| 21 | |||
| 22 | static const char *TAG = "tg_core_dns"; | ||
| 23 | |||
| 24 | #pragma pack(push, 1) | ||
| 25 | typedef struct { | ||
| 26 | uint16_t id; | ||
| 27 | uint16_t flags; | ||
| 28 | uint16_t qdcount; | ||
| 29 | uint16_t ancount; | ||
| 30 | uint16_t nscount; | ||
| 31 | uint16_t arcount; | ||
| 32 | } dns_header_t; | ||
| 33 | #pragma pack(pop) | ||
| 34 | |||
| 35 | #pragma pack(push, 1) | ||
| 36 | typedef struct { | ||
| 37 | uint16_t name; | ||
| 38 | uint16_t type; | ||
| 39 | uint16_t class; | ||
| 40 | uint32_t ttl; | ||
| 41 | uint16_t len; | ||
| 42 | uint32_t addr; | ||
| 43 | } dns_answer_t; | ||
| 44 | #pragma pack(pop) | ||
| 45 | |||
| 46 | typedef struct { | ||
| 47 | uint32_t ip; | ||
| 48 | } auth_entry_t; | ||
| 49 | |||
| 50 | static auth_entry_t s_auth_list[MAX_AUTH_IPS]; | ||
| 51 | static int s_auth_count = 0; | ||
| 52 | static TaskHandle_t s_dns_task = NULL; | ||
| 53 | static TaskHandle_t s_dot_task = NULL; | ||
| 54 | static volatile bool s_dns_running = false; | ||
| 55 | static esp_ip4_addr_t s_ap_ip; | ||
| 56 | static esp_ip4_addr_t s_upstream_dns; | ||
| 57 | |||
| 58 | static bool is_authenticated(uint32_t ip) | ||
| 59 | { | ||
| 60 | for (int i = 0; i < s_auth_count; i++) { | ||
| 61 | if (s_auth_list[i].ip == ip) return true; | ||
| 62 | } | ||
| 63 | return false; | ||
| 64 | } | ||
| 65 | |||
| 66 | static void parse_dns_name(const uint8_t *buf, int buf_len, int offset, char *out, int out_len) | ||
| 67 | { | ||
| 68 | int pos = offset; | ||
| 69 | int out_pos = 0; | ||
| 70 | int jumped = 0; | ||
| 71 | int jump_pos = 0; | ||
| 72 | while (pos < buf_len && out_pos < out_len - 1) { | ||
| 73 | uint8_t len = buf[pos]; | ||
| 74 | if (len == 0) break; | ||
| 75 | if ((len & 0xC0) == 0xC0) { | ||
| 76 | if (!jumped) jump_pos = pos + 2; | ||
| 77 | pos = ((len & 0x3F) << 8) | buf[pos + 1]; | ||
| 78 | jumped = 1; | ||
| 79 | continue; | ||
| 80 | } | ||
| 81 | if (out_pos > 0 && out_pos < out_len - 1) out[out_pos++] = '.'; | ||
| 82 | pos++; | ||
| 83 | for (int i = 0; i < len && pos < buf_len && out_pos < out_len - 1; i++) { | ||
| 84 | out[out_pos++] = buf[pos++]; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | out[out_pos] = '\0'; | ||
| 88 | } | ||
| 89 | |||
| 90 | static int build_nxdomain(uint8_t *response, int req_len) | ||
| 91 | { | ||
| 92 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 93 | hdr->flags = htons(0x8403); | ||
| 94 | hdr->ancount = 0; | ||
| 95 | hdr->nscount = 0; | ||
| 96 | hdr->arcount = 0; | ||
| 97 | return req_len; | ||
| 98 | } | ||
| 99 | |||
| 100 | static int build_redirect_response(uint8_t *response, int req_len) | ||
| 101 | { | ||
| 102 | memmove(response, response, req_len); | ||
| 103 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 104 | hdr->flags = htons(0x8180); | ||
| 105 | hdr->ancount = htons(1); | ||
| 106 | hdr->nscount = 0; | ||
| 107 | hdr->arcount = 0; | ||
| 108 | int resp_len = req_len; | ||
| 109 | dns_answer_t ans; | ||
| 110 | ans.name = htons(0xC00C); | ||
| 111 | ans.type = htons(1); | ||
| 112 | ans.class = htons(1); | ||
| 113 | ans.ttl = htonl(HIJACK_TTL); | ||
| 114 | ans.len = htons(4); | ||
| 115 | ans.addr = s_ap_ip.addr; | ||
| 116 | memcpy(response + resp_len, &ans, sizeof(ans)); | ||
| 117 | resp_len += sizeof(ans); | ||
| 118 | return resp_len; | ||
| 119 | } | ||
| 120 | |||
| 121 | static int forward_dns(const uint8_t *req, int req_len, uint8_t *resp, int resp_buf_len, | ||
| 122 | uint16_t txn_id) | ||
| 123 | { | ||
| 124 | int upstream_sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 125 | if (upstream_sock < 0) return -1; | ||
| 126 | |||
| 127 | struct timeval tv = { .tv_sec = DNS_FORWARD_TIMEOUT_MS / 1000, .tv_usec = (DNS_FORWARD_TIMEOUT_MS % 1000) * 1000 }; | ||
| 128 | setsockopt(upstream_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | ||
| 129 | |||
| 130 | struct sockaddr_in upstream_addr = { | ||
| 131 | .sin_family = AF_INET, | ||
| 132 | .sin_port = htons(DNS_PORT), | ||
| 133 | .sin_addr.s_addr = s_upstream_dns.addr, | ||
| 134 | }; | ||
| 135 | |||
| 136 | sendto(upstream_sock, req, req_len, 0, (struct sockaddr *)&upstream_addr, sizeof(upstream_addr)); | ||
| 137 | |||
| 138 | int n = recvfrom(upstream_sock, resp, resp_buf_len, 0, NULL, NULL); | ||
| 139 | close(upstream_sock); | ||
| 140 | |||
| 141 | if (n > 0) { | ||
| 142 | if (n >= sizeof(dns_header_t)) { | ||
| 143 | dns_header_t *hdr = (dns_header_t *)resp; | ||
| 144 | hdr->id = htons(txn_id); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return n; | ||
| 148 | } | ||
| 149 | |||
| 150 | static void dns_server_task(void *arg) | ||
| 151 | { | ||
| 152 | int sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 153 | if (sock < 0) { | ||
| 154 | ESP_LOGE(TAG, "Failed to create DNS socket"); | ||
| 155 | s_dns_running = false; | ||
| 156 | vTaskDelete(NULL); | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | struct sockaddr_in bind_addr = { | ||
| 161 | .sin_family = AF_INET, | ||
| 162 | .sin_port = htons(DNS_PORT), | ||
| 163 | .sin_addr.s_addr = INADDR_ANY, | ||
| 164 | }; | ||
| 165 | if (bind(sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | ||
| 166 | ESP_LOGE(TAG, "Failed to bind DNS socket"); | ||
| 167 | close(sock); | ||
| 168 | s_dns_running = false; | ||
| 169 | vTaskDelete(NULL); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | ESP_LOGI(TAG, "DNS server started on port %d, AP IP=" IPSTR ", upstream DNS=" IPSTR, | ||
| 174 | DNS_PORT, IP2STR(&s_ap_ip), IP2STR(&s_upstream_dns)); | ||
| 175 | |||
| 176 | uint8_t rx_buf[DNS_BUF_SIZE]; | ||
| 177 | uint8_t tx_buf[DNS_BUF_SIZE + sizeof(dns_answer_t)]; | ||
| 178 | |||
| 179 | while (s_dns_running) { | ||
| 180 | struct sockaddr_in client_addr; | ||
| 181 | socklen_t client_len = sizeof(client_addr); | ||
| 182 | int n = recvfrom(sock, rx_buf, sizeof(rx_buf), 0, | ||
| 183 | (struct sockaddr *)&client_addr, &client_len); | ||
| 184 | if (n < (int)sizeof(dns_header_t)) continue; | ||
| 185 | |||
| 186 | uint32_t client_ip = client_addr.sin_addr.s_addr; | ||
| 187 | dns_header_t *hdr = (dns_header_t *)rx_buf; | ||
| 188 | uint16_t txn_id = ntohs(hdr->id); | ||
| 189 | bool is_query = (ntohs(hdr->flags) & 0x8000) == 0; | ||
| 190 | uint16_t qdcount = ntohs(hdr->qdcount); | ||
| 191 | |||
| 192 | if (!is_query || qdcount == 0) continue; | ||
| 193 | |||
| 194 | int q_offset = sizeof(dns_header_t); | ||
| 195 | while (q_offset < n && rx_buf[q_offset] != 0) { | ||
| 196 | q_offset += rx_buf[q_offset] + 1; | ||
| 197 | } | ||
| 198 | if (q_offset + 5 > n) continue; | ||
| 199 | uint16_t qtype = (rx_buf[q_offset + 1] << 8) | rx_buf[q_offset + 2]; | ||
| 200 | int req_len = q_offset + 5; | ||
| 201 | |||
| 202 | if (is_authenticated(client_ip)) { | ||
| 203 | int resp_len = forward_dns(rx_buf, req_len, tx_buf, sizeof(tx_buf), txn_id); | ||
| 204 | if (resp_len > 0) { | ||
| 205 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 206 | } | ||
| 207 | } else { | ||
| 208 | char qname[256] = {0}; | ||
| 209 | parse_dns_name(rx_buf, n, sizeof(dns_header_t), qname, sizeof(qname)); | ||
| 210 | ESP_LOGI(TAG, "Hijack DNS from " IPSTR ": %s (type=%d)", | ||
| 211 | IP2STR(&(esp_ip4_addr_t){.addr=client_ip}), qname, qtype); | ||
| 212 | if (qtype == 1) { | ||
| 213 | int resp_len = build_redirect_response(rx_buf, req_len); | ||
| 214 | memcpy(tx_buf, rx_buf, resp_len); | ||
| 215 | dns_header_t *resp_hdr = (dns_header_t *)tx_buf; | ||
| 216 | resp_hdr->id = htons(txn_id); | ||
| 217 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 218 | } else { | ||
| 219 | int resp_len = build_nxdomain(rx_buf, req_len); | ||
| 220 | memcpy(tx_buf, rx_buf, resp_len); | ||
| 221 | dns_header_t *resp_hdr = (dns_header_t *)tx_buf; | ||
| 222 | resp_hdr->id = htons(txn_id); | ||
| 223 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | close(sock); | ||
| 229 | ESP_LOGI(TAG, "DNS server stopped"); | ||
| 230 | vTaskDelete(NULL); | ||
| 231 | } | ||
| 232 | |||
| 233 | static void dot_reject_task(void *arg) | ||
| 234 | { | ||
| 235 | int sock = socket(AF_INET, SOCK_STREAM, 0); | ||
| 236 | if (sock < 0) { | ||
| 237 | ESP_LOGE(TAG, "Failed to create DoT reject socket"); | ||
| 238 | vTaskDelete(NULL); | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | int opt = 1; | ||
| 243 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | ||
| 244 | |||
| 245 | struct sockaddr_in bind_addr = { | ||
| 246 | .sin_family = AF_INET, | ||
| 247 | .sin_port = htons(DOT_PORT), | ||
| 248 | .sin_addr.s_addr = INADDR_ANY, | ||
| 249 | }; | ||
| 250 | if (bind(sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | ||
| 251 | ESP_LOGE(TAG, "Failed to bind DoT reject socket on port %d", DOT_PORT); | ||
| 252 | close(sock); | ||
| 253 | vTaskDelete(NULL); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | listen(sock, 1); | ||
| 258 | ESP_LOGI(TAG, "DoT reject server on port %d (forces DNS fallback to port 53)", DOT_PORT); | ||
| 259 | |||
| 260 | while (s_dns_running) { | ||
| 261 | struct sockaddr_in client_addr; | ||
| 262 | socklen_t client_len = sizeof(client_addr); | ||
| 263 | int client_sock = accept(sock, (struct sockaddr *)&client_addr, &client_len); | ||
| 264 | if (client_sock >= 0) { | ||
| 265 | struct linger ling = { .l_onoff = 1, .l_linger = 0 }; | ||
| 266 | setsockopt(client_sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); | ||
| 267 | close(client_sock); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | close(sock); | ||
| 272 | ESP_LOGI(TAG, "DoT reject server stopped"); | ||
| 273 | vTaskDelete(NULL); | ||
| 274 | } | ||
| 275 | |||
| 276 | esp_err_t tollgate_core_dns_start_internal(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns) | ||
| 277 | { | ||
| 278 | if (s_dns_running) return ESP_OK; | ||
| 279 | s_ap_ip = ap_ip; | ||
| 280 | s_upstream_dns = upstream_dns; | ||
| 281 | s_dns_running = true; | ||
| 282 | xTaskCreate(dns_server_task, "dns_server", DNS_TASK_STACK, NULL, DNS_TASK_PRIO, &s_dns_task); | ||
| 283 | xTaskCreate(dot_reject_task, "dot_reject", DOT_TASK_STACK, NULL, DOT_TASK_PRIO, &s_dot_task); | ||
| 284 | return ESP_OK; | ||
| 285 | } | ||
| 286 | |||
| 287 | void tollgate_core_dns_stop(void) | ||
| 288 | { | ||
| 289 | s_dns_running = false; | ||
| 290 | vTaskDelay(pdMS_TO_TICKS(200)); | ||
| 291 | s_dns_task = NULL; | ||
| 292 | } | ||
| 293 | |||
| 294 | void tollgate_core_dns_set_authenticated(uint32_t client_ip, bool authenticated) | ||
| 295 | { | ||
| 296 | if (authenticated) { | ||
| 297 | if (is_authenticated(client_ip)) return; | ||
| 298 | if (s_auth_count < MAX_AUTH_IPS) { | ||
| 299 | s_auth_list[s_auth_count].ip = client_ip; | ||
| 300 | s_auth_count++; | ||
| 301 | } | ||
| 302 | } else { | ||
| 303 | for (int i = 0; i < s_auth_count; i++) { | ||
| 304 | if (s_auth_list[i].ip == client_ip) { | ||
| 305 | s_auth_list[i] = s_auth_list[s_auth_count - 1]; | ||
| 306 | s_auth_count--; | ||
| 307 | return; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | bool tollgate_core_dns_is_running(void) | ||
| 314 | { | ||
| 315 | return s_dns_running; | ||
| 316 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_dns.h b/components/tollgate_core/src/tollgate_core_dns.h new file mode 100644 index 0000000..60aaaaf --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_dns.h | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_DNS_H | ||
| 2 | #define TOLLGATE_CORE_DNS_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include "esp_netif.h" | ||
| 6 | #include <stdbool.h> | ||
| 7 | |||
| 8 | esp_err_t tollgate_core_dns_start_internal(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns); | ||
| 9 | void tollgate_core_dns_stop(void); | ||
| 10 | void tollgate_core_dns_set_authenticated(uint32_t client_ip, bool authenticated); | ||
| 11 | bool tollgate_core_dns_is_running(void); | ||
| 12 | |||
| 13 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_firewall.c b/components/tollgate_core/src/tollgate_core_firewall.c new file mode 100644 index 0000000..e5d61d8 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_firewall.c | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | #include "tollgate_core_firewall.h" | ||
| 2 | #include "tollgate_core_dns.h" | ||
| 3 | #include "esp_log.h" | ||
| 4 | #include "esp_wifi.h" | ||
| 5 | #include "esp_wifi_ap_get_sta_list.h" | ||
| 6 | #include "lwip/lwip_napt.h" | ||
| 7 | #include "lwip/etharp.h" | ||
| 8 | #include "lwip/netif.h" | ||
| 9 | #include "lwip/prot/ip4.h" | ||
| 10 | #include <string.h> | ||
| 11 | |||
| 12 | #define MAX_CLIENTS 10 | ||
| 13 | |||
| 14 | static const char *TAG = "tg_core_fw"; | ||
| 15 | static esp_ip4_addr_t s_ap_ip; | ||
| 16 | |||
| 17 | typedef struct { | ||
| 18 | uint32_t ip; | ||
| 19 | char mac[TG_FW_MAX_MAC_LEN]; | ||
| 20 | } fw_client_t; | ||
| 21 | |||
| 22 | static fw_client_t s_clients[MAX_CLIENTS]; | ||
| 23 | static int s_client_count = 0; | ||
| 24 | |||
| 25 | esp_err_t tollgate_core_fw_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) | ||
| 26 | { | ||
| 27 | wifi_sta_list_t sta_list; | ||
| 28 | if (esp_wifi_ap_get_sta_list(&sta_list) == ESP_OK) { | ||
| 29 | wifi_sta_mac_ip_list_t ip_mac_list; | ||
| 30 | if (esp_wifi_ap_get_sta_list_with_ip(&sta_list, &ip_mac_list) == ESP_OK) { | ||
| 31 | for (int i = 0; i < ip_mac_list.num; i++) { | ||
| 32 | if (ip_mac_list.sta[i].ip.addr == client_ip) { | ||
| 33 | snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 34 | ip_mac_list.sta[i].mac[0], ip_mac_list.sta[i].mac[1], | ||
| 35 | ip_mac_list.sta[i].mac[2], ip_mac_list.sta[i].mac[3], | ||
| 36 | ip_mac_list.sta[i].mac[4], ip_mac_list.sta[i].mac[5]); | ||
| 37 | return ESP_OK; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | ip4_addr_t *entry_ip = NULL; | ||
| 44 | struct netif *entry_netif = NULL; | ||
| 45 | struct eth_addr *entry_eth = NULL; | ||
| 46 | ssize_t i = 0; | ||
| 47 | while (etharp_get_entry(i, &entry_ip, &entry_netif, &entry_eth) == ERR_OK) { | ||
| 48 | if (entry_ip && entry_ip->addr == client_ip && entry_eth) { | ||
| 49 | snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 50 | entry_eth->addr[0], entry_eth->addr[1], entry_eth->addr[2], | ||
| 51 | entry_eth->addr[3], entry_eth->addr[4], entry_eth->addr[5]); | ||
| 52 | return ESP_OK; | ||
| 53 | } | ||
| 54 | i++; | ||
| 55 | } | ||
| 56 | return ESP_FAIL; | ||
| 57 | } | ||
| 58 | |||
| 59 | esp_err_t tollgate_core_fw_init(esp_ip4_addr_t ap_ip) | ||
| 60 | { | ||
| 61 | s_ap_ip = ap_ip; | ||
| 62 | memset(s_clients, 0, sizeof(s_clients)); | ||
| 63 | s_client_count = 0; | ||
| 64 | ip_napt_enable(s_ap_ip.addr, 1); | ||
| 65 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR " (NAT always on, per-client filter)", IP2STR(&s_ap_ip)); | ||
| 66 | return ESP_OK; | ||
| 67 | } | ||
| 68 | |||
| 69 | int tollgate_core_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) | ||
| 70 | { | ||
| 71 | (void)dest_addr_hostorder; | ||
| 72 | if (p->len < IP_HLEN) return -1; | ||
| 73 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 74 | uint32_t src_ip_h = lwip_ntohl(iphdr->src.addr); | ||
| 75 | uint32_t ap_subnet = lwip_ntohl(s_ap_ip.addr) & 0xFFFFFF00; | ||
| 76 | if ((src_ip_h & 0xFFFFFF00) != ap_subnet) { | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | if (tollgate_core_fw_is_allowed(iphdr->src.addr)) { | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | static fw_client_t *find_client_by_ip(uint32_t client_ip) | ||
| 86 | { | ||
| 87 | for (int i = 0; i < s_client_count; i++) { | ||
| 88 | if (s_clients[i].ip == client_ip) return &s_clients[i]; | ||
| 89 | } | ||
| 90 | return NULL; | ||
| 91 | } | ||
| 92 | |||
| 93 | static fw_client_t *find_client_by_mac(const char *mac) | ||
| 94 | { | ||
| 95 | for (int i = 0; i < s_client_count; i++) { | ||
| 96 | if (s_clients[i].mac[0] != '\0' && strcmp(s_clients[i].mac, mac) == 0) { | ||
| 97 | return &s_clients[i]; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | return NULL; | ||
| 101 | } | ||
| 102 | |||
| 103 | void tollgate_core_fw_grant(uint32_t client_ip) | ||
| 104 | { | ||
| 105 | fw_client_t *existing = find_client_by_ip(client_ip); | ||
| 106 | if (existing) { | ||
| 107 | existing->ip = client_ip; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | if (s_client_count >= MAX_CLIENTS) { | ||
| 111 | ESP_LOGW(TAG, "Max clients reached, cannot grant access"); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | fw_client_t *client = &s_clients[s_client_count]; | ||
| 116 | client->ip = client_ip; | ||
| 117 | client->mac[0] = '\0'; | ||
| 118 | tollgate_core_fw_get_mac_for_ip(client_ip, client->mac, sizeof(client->mac)); | ||
| 119 | s_client_count++; | ||
| 120 | |||
| 121 | tollgate_core_dns_set_authenticated(client_ip, true); | ||
| 122 | |||
| 123 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 124 | ESP_LOGI(TAG, "Access granted to " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 125 | client->mac[0] ? client->mac : "unknown"); | ||
| 126 | } | ||
| 127 | |||
| 128 | void tollgate_core_fw_revoke(uint32_t client_ip) | ||
| 129 | { | ||
| 130 | for (int i = 0; i < s_client_count; i++) { | ||
| 131 | if (s_clients[i].ip == client_ip) { | ||
| 132 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 133 | ESP_LOGI(TAG, "Access revoked for " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 134 | s_clients[i].mac[0] ? s_clients[i].mac : "unknown"); | ||
| 135 | s_clients[i] = s_clients[s_client_count - 1]; | ||
| 136 | s_client_count--; | ||
| 137 | tollgate_core_dns_set_authenticated(client_ip, false); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | void tollgate_core_fw_revoke_all(void) | ||
| 144 | { | ||
| 145 | for (int i = 0; i < s_client_count; i++) { | ||
| 146 | tollgate_core_dns_set_authenticated(s_clients[i].ip, false); | ||
| 147 | } | ||
| 148 | s_client_count = 0; | ||
| 149 | ESP_LOGI(TAG, "All client access revoked"); | ||
| 150 | } | ||
| 151 | |||
| 152 | bool tollgate_core_fw_is_allowed(uint32_t client_ip) | ||
| 153 | { | ||
| 154 | return find_client_by_ip(client_ip) != NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | bool tollgate_core_fw_is_mac_allowed(const char *mac) | ||
| 158 | { | ||
| 159 | return find_client_by_mac(mac) != NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | int tollgate_core_fw_client_count(void) | ||
| 163 | { | ||
| 164 | return s_client_count; | ||
| 165 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_firewall.h b/components/tollgate_core/src/tollgate_core_firewall.h new file mode 100644 index 0000000..f06c801 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_firewall.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_FIREWALL_H | ||
| 2 | #define TOLLGATE_CORE_FIREWALL_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include "esp_netif.h" | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | |||
| 9 | struct pbuf; | ||
| 10 | |||
| 11 | #define TG_FW_MAX_MAC_LEN 18 | ||
| 12 | |||
| 13 | esp_err_t tollgate_core_fw_init(esp_ip4_addr_t ap_ip); | ||
| 14 | void tollgate_core_fw_grant(uint32_t client_ip); | ||
| 15 | void tollgate_core_fw_revoke(uint32_t client_ip); | ||
| 16 | void tollgate_core_fw_revoke_all(void); | ||
| 17 | bool tollgate_core_fw_is_allowed(uint32_t client_ip); | ||
| 18 | bool tollgate_core_fw_is_mac_allowed(const char *mac); | ||
| 19 | int tollgate_core_fw_client_count(void); | ||
| 20 | |||
| 21 | esp_err_t tollgate_core_fw_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size); | ||
| 22 | |||
| 23 | int tollgate_core_ip4_canforward_filter(struct pbuf *p, uint32_t dest_addr_hostorder); | ||
| 24 | |||
| 25 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_session.c b/components/tollgate_core/src/tollgate_core_session.c new file mode 100644 index 0000000..48d32e7 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_session.c | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | #include "tollgate_core_session.h" | ||
| 2 | #include "tollgate_core_firewall.h" | ||
| 3 | #include "esp_log.h" | ||
| 4 | #include "freertos/FreeRTOS.h" | ||
| 5 | #include "freertos/task.h" | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | static const char *TAG = "tg_core_session"; | ||
| 9 | static tg_session_t s_sessions[TG_SESSION_MAX_CLIENTS]; | ||
| 10 | static int s_session_count = 0; | ||
| 11 | |||
| 12 | static const tollgate_platform_t *s_platform; | ||
| 13 | |||
| 14 | void tollgate_core_session_set_platform(const tollgate_platform_t *platform) | ||
| 15 | { | ||
| 16 | s_platform = platform; | ||
| 17 | } | ||
| 18 | |||
| 19 | static int64_t get_time_ms(void) | ||
| 20 | { | ||
| 21 | if (s_platform && s_platform->get_time_ms) { | ||
| 22 | return s_platform->get_time_ms(); | ||
| 23 | } | ||
| 24 | return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; | ||
| 25 | } | ||
| 26 | |||
| 27 | esp_err_t tollgate_core_session_init(void) | ||
| 28 | { | ||
| 29 | memset(s_sessions, 0, sizeof(s_sessions)); | ||
| 30 | s_session_count = 0; | ||
| 31 | ESP_LOGI(TAG, "Session manager initialized"); | ||
| 32 | return ESP_OK; | ||
| 33 | } | ||
| 34 | |||
| 35 | static void populate_mac(tg_session_t *session, uint32_t client_ip) | ||
| 36 | { | ||
| 37 | if (tollgate_core_fw_get_mac_for_ip(client_ip, session->mac, sizeof(session->mac)) != ESP_OK) { | ||
| 38 | session->mac[0] = '\0'; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms) | ||
| 43 | { | ||
| 44 | tg_session_t *existing = tollgate_core_session_find_by_ip(client_ip); | ||
| 45 | if (existing) { | ||
| 46 | tollgate_core_session_extend(existing, allotment_ms); | ||
| 47 | return existing; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (s_session_count >= TG_SESSION_MAX_CLIENTS) { | ||
| 51 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 52 | if (!s_sessions[i].active || tollgate_core_session_is_expired(&s_sessions[i])) { | ||
| 53 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 54 | break; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 60 | if (!s_sessions[i].active) { | ||
| 61 | s_sessions[i].client_ip = client_ip; | ||
| 62 | s_sessions[i].allotment_ms = allotment_ms; | ||
| 63 | s_sessions[i].start_time_ms = get_time_ms(); | ||
| 64 | s_sessions[i].active = true; | ||
| 65 | populate_mac(&s_sessions[i], client_ip); | ||
| 66 | |||
| 67 | s_session_count++; | ||
| 68 | tollgate_core_fw_grant(client_ip); | ||
| 69 | |||
| 70 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 71 | ESP_LOGI(TAG, "Session created: " IPSTR " mac=%s allotment=%llums", IP2STR(&ip), | ||
| 72 | s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", | ||
| 73 | (unsigned long long)allotment_ms); | ||
| 74 | return &s_sessions[i]; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | ESP_LOGW(TAG, "No free session slots"); | ||
| 79 | return NULL; | ||
| 80 | } | ||
| 81 | |||
| 82 | tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes) | ||
| 83 | { | ||
| 84 | tg_session_t *s = tollgate_core_session_create(client_ip, 0); | ||
| 85 | if (s) { | ||
| 86 | s->allotment_bytes = allotment_bytes; | ||
| 87 | s->bytes_consumed = 0; | ||
| 88 | s->allotment_ms = INT64_MAX; | ||
| 89 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 90 | ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip), | ||
| 91 | (unsigned long long)allotment_bytes); | ||
| 92 | } | ||
| 93 | return s; | ||
| 94 | } | ||
| 95 | |||
| 96 | void tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes) | ||
| 97 | { | ||
| 98 | tg_session_t *s = tollgate_core_session_find_by_ip(client_ip); | ||
| 99 | if (s && s->active) { | ||
| 100 | s->bytes_consumed += bytes; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | tg_session_t *tollgate_core_session_find_by_ip(uint32_t client_ip) | ||
| 105 | { | ||
| 106 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 107 | if (s_sessions[i].active && s_sessions[i].client_ip == client_ip) { | ||
| 108 | return &s_sessions[i]; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return NULL; | ||
| 112 | } | ||
| 113 | |||
| 114 | tg_session_t *tollgate_core_session_find_by_mac(const char *mac) | ||
| 115 | { | ||
| 116 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 117 | if (s_sessions[i].active && s_sessions[i].mac[0] != '\0' && | ||
| 118 | strcmp(s_sessions[i].mac, mac) == 0) { | ||
| 119 | return &s_sessions[i]; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | return NULL; | ||
| 123 | } | ||
| 124 | |||
| 125 | void tollgate_core_session_extend(tg_session_t *session, uint64_t additional_ms) | ||
| 126 | { | ||
| 127 | if (!session || !session->active) return; | ||
| 128 | session->allotment_ms += additional_ms; | ||
| 129 | esp_ip4_addr_t ip = { .addr = session->client_ip }; | ||
| 130 | ESP_LOGI(TAG, "Session extended: " IPSTR " +%llums (total=%llu)", IP2STR(&ip), | ||
| 131 | (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms); | ||
| 132 | } | ||
| 133 | |||
| 134 | bool tollgate_core_session_is_expired(const tg_session_t *session) | ||
| 135 | { | ||
| 136 | if (!session || !session->active) return true; | ||
| 137 | |||
| 138 | if (s_platform && s_platform->get_metric) { | ||
| 139 | const char *metric = s_platform->get_metric(); | ||
| 140 | if (metric && strcmp(metric, "bytes") == 0) { | ||
| 141 | return session->bytes_consumed >= session->allotment_bytes; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | int64_t elapsed = get_time_ms() - session->start_time_ms; | ||
| 146 | return elapsed >= (int64_t)session->allotment_ms; | ||
| 147 | } | ||
| 148 | |||
| 149 | static void check_expiry(void) | ||
| 150 | { | ||
| 151 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 152 | if (s_sessions[i].active && tollgate_core_session_is_expired(&s_sessions[i])) { | ||
| 153 | esp_ip4_addr_t ip = { .addr = s_sessions[i].client_ip }; | ||
| 154 | ESP_LOGI(TAG, "Session expired: " IPSTR " mac=%s", IP2STR(&ip), | ||
| 155 | s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown"); | ||
| 156 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void tollgate_core_session_revoke(tg_session_t *session) | ||
| 162 | { | ||
| 163 | if (!session || !session->active) return; | ||
| 164 | tollgate_core_fw_revoke(session->client_ip); | ||
| 165 | session->active = false; | ||
| 166 | s_session_count--; | ||
| 167 | } | ||
| 168 | |||
| 169 | void tollgate_core_session_revoke_all(void) | ||
| 170 | { | ||
| 171 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 172 | if (s_sessions[i].active) { | ||
| 173 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | int tollgate_core_session_active_count(void) | ||
| 179 | { | ||
| 180 | int count = 0; | ||
| 181 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 182 | if (s_sessions[i].active) count++; | ||
| 183 | } | ||
| 184 | return count; | ||
| 185 | } | ||
| 186 | |||
| 187 | void tollgate_core_session_tick(void) | ||
| 188 | { | ||
| 189 | check_expiry(); | ||
| 190 | } | ||
| 191 | |||
| 192 | tg_session_t *tollgate_core_session_get_array(void) | ||
| 193 | { | ||
| 194 | return s_sessions; | ||
| 195 | } | ||
| 196 | |||
| 197 | int tollgate_core_session_get_array_size(void) | ||
| 198 | { | ||
| 199 | return TG_SESSION_MAX_CLIENTS; | ||
| 200 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_session.h b/components/tollgate_core/src/tollgate_core_session.h new file mode 100644 index 0000000..444b9fa --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_session.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_SESSION_H | ||
| 2 | #define TOLLGATE_CORE_SESSION_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include "tollgate_platform.h" | ||
| 6 | #include <stdint.h> | ||
| 7 | #include <stdbool.h> | ||
| 8 | |||
| 9 | #define TG_SESSION_MAX_CLIENTS 10 | ||
| 10 | #define TG_SESSION_MAX_MAC_LEN 18 | ||
| 11 | |||
| 12 | typedef struct { | ||
| 13 | uint32_t client_ip; | ||
| 14 | char mac[TG_SESSION_MAX_MAC_LEN]; | ||
| 15 | uint64_t allotment_ms; | ||
| 16 | int64_t start_time_ms; | ||
| 17 | uint64_t allotment_bytes; | ||
| 18 | uint64_t bytes_consumed; | ||
| 19 | bool active; | ||
| 20 | } tg_session_t; | ||
| 21 | |||
| 22 | esp_err_t tollgate_core_session_init(void); | ||
| 23 | void tollgate_core_session_set_platform(const tollgate_platform_t *platform); | ||
| 24 | |||
| 25 | tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms); | ||
| 26 | tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes); | ||
| 27 | |||
| 28 | void tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes); | ||
| 29 | |||
| 30 | tg_session_t *tollgate_core_session_find_by_ip(uint32_t client_ip); | ||
| 31 | tg_session_t *tollgate_core_session_find_by_mac(const char *mac); | ||
| 32 | |||
| 33 | void tollgate_core_session_extend(tg_session_t *session, uint64_t additional_ms); | ||
| 34 | |||
| 35 | bool tollgate_core_session_is_expired(const tg_session_t *session); | ||
| 36 | |||
| 37 | void tollgate_core_session_revoke(tg_session_t *session); | ||
| 38 | void tollgate_core_session_revoke_all(void); | ||
| 39 | |||
| 40 | int tollgate_core_session_active_count(void); | ||
| 41 | |||
| 42 | void tollgate_core_session_tick(void); | ||
| 43 | |||
| 44 | tg_session_t *tollgate_core_session_get_array(void); | ||
| 45 | int tollgate_core_session_get_array_size(void); | ||
| 46 | |||
| 47 | #endif | ||