upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/session.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-23 04:10:28 +0530
committerYour Name <you@example.com>2026-05-23 04:10:28 +0530
commit851801f50f1282ab1db5f8a241dbd245f59e447e (patch)
tree3285d0714da89879acd21b8c3fb9afeff586ed1e /main/session.c
parent39aa27a9522aeb8f731f4d3de48939e75775900e (diff)
Phase 5: Wire main/ shims to components/tollgate_core
- session.c/h: thin shim casting session_t* <-> tg_session_t*, delegates to tollgate_core_session_* - firewall.c/h: keeps ESP-specific lwIP hooks, delegates allowlist to tollgate_core_fw_* - cashu.c/h: shim delegates to tollgate_core_cashu_*, multi-mint via config directly - mining_payment.c: shim delegates to tollgate_core_mining_* - tollgate_main.c: calls tollgate_core_init(tollgate_esp_get_platform(), ap_ip) in start_services() - tollgate_esp: removed target_sources for standalone sources, depends on tollgate_core component - tollgate_core component: added tollgate_core_get_platform(), tg_payment_method_t enum - Unit tests: updated Makefile for component source compilation, all 22 tests pass - Hardware verified: Board A boots, AP visible, 5/6 smoke tests pass (internet test needs upstream WiFi)
Diffstat (limited to 'main/session.c')
-rw-r--r--main/session.c142
1 files changed, 18 insertions, 124 deletions
diff --git a/main/session.c b/main/session.c
index feea272..3e3813a 100644
--- a/main/session.c
+++ b/main/session.c
@@ -1,192 +1,86 @@
1#include "session.h" 1#include "session.h"
2#include "firewall.h" 2#include "tollgate_core_session.h"
3#include "dns_server.h" 3#include "tollgate_core_firewall.h"
4#include "config.h" 4#include "tollgate_core.h"
5#include "esp_log.h" 5#include "esp_log.h"
6#include "freertos/FreeRTOS.h" 6#include "freertos/FreeRTOS.h"
7#include "freertos/task.h" 7#include "freertos/task.h"
8#include <string.h> 8#include <string.h>
9 9
10static const char *TAG = "session"; 10static const char *TAG = "session";
11static session_t s_sessions[SESSION_MAX_CLIENTS];
12static int s_session_count = 0;
13
14static int64_t get_time_ms(void)
15{
16 return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
17}
18 11
19esp_err_t session_manager_init(void) 12esp_err_t session_manager_init(void)
20{ 13{
21 memset(s_sessions, 0, sizeof(s_sessions)); 14 tollgate_core_session_init();
22 s_session_count = 0; 15 ESP_LOGI(TAG, "Session manager initialized (via tollgate_core)");
23 ESP_LOGI(TAG, "Session manager initialized");
24 return ESP_OK; 16 return ESP_OK;
25} 17}
26 18
27static void populate_mac(session_t *session, uint32_t client_ip)
28{
29 if (firewall_get_mac_for_ip(client_ip, session->mac, sizeof(session->mac)) != ESP_OK) {
30 session->mac[0] = '\0';
31 }
32}
33
34session_t *session_create(uint32_t client_ip, uint64_t allotment_ms) 19session_t *session_create(uint32_t client_ip, uint64_t allotment_ms)
35{ 20{
36 session_t *existing = session_find_by_ip(client_ip); 21 return (session_t *)tollgate_core_session_create(client_ip, allotment_ms);
37 if (existing) {
38 session_extend(existing, allotment_ms);
39 return existing;
40 }
41
42 if (s_session_count >= SESSION_MAX_CLIENTS) {
43 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) {
44 if (!s_sessions[i].active || session_is_expired(&s_sessions[i])) {
45 session_revoke(&s_sessions[i]);
46 break;
47 }
48 }
49 }
50
51 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) {
52 if (!s_sessions[i].active) {
53 s_sessions[i].client_ip = client_ip;
54 s_sessions[i].allotment_ms = allotment_ms;
55 s_sessions[i].start_time_ms = get_time_ms();
56 s_sessions[i].active = true;
57 s_sessions[i].payment_method = PAYMENT_METHOD_CASHU;
58 populate_mac(&s_sessions[i], client_ip);
59
60 s_session_count++;
61 firewall_grant_access(client_ip);
62
63 esp_ip4_addr_t ip = { .addr = client_ip };
64 ESP_LOGI(TAG, "Session created: " IPSTR " mac=%s allotment=%llums", IP2STR(&ip),
65 s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown",
66 (unsigned long long)allotment_ms);
67 return &s_sessions[i];
68 }
69 }
70
71 ESP_LOGW(TAG, "No free session slots");
72 return NULL;
73} 22}
74 23
75session_t *session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes) 24session_t *session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes)
76{ 25{
77 session_t *s = session_create(client_ip, 0); 26 return (session_t *)tollgate_core_session_create_bytes(client_ip, allotment_bytes);
78 if (s) {
79 s->allotment_bytes = allotment_bytes;
80 s->bytes_consumed = 0;
81 s->allotment_ms = INT64_MAX;
82 s->payment_method = PAYMENT_METHOD_BYTES;
83 esp_ip4_addr_t ip = { .addr = client_ip };
84 ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip),
85 (unsigned long long)allotment_bytes);
86 }
87 return s;
88} 27}
89 28
90void session_add_bytes(uint32_t client_ip, uint64_t bytes) 29void session_add_bytes(uint32_t client_ip, uint64_t bytes)
91{ 30{
92 session_t *s = session_find_by_ip(client_ip); 31 tollgate_core_session_add_bytes(client_ip, bytes);
93 if (s && s->active) {
94 s->bytes_consumed += bytes;
95 }
96} 32}
97 33
98session_t *session_find_by_ip(uint32_t client_ip) 34session_t *session_find_by_ip(uint32_t client_ip)
99{ 35{
100 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) { 36 return (session_t *)tollgate_core_session_find_by_ip(client_ip);
101 if (s_sessions[i].active && s_sessions[i].client_ip == client_ip) {
102 return &s_sessions[i];
103 }
104 }
105 return NULL;
106} 37}
107 38
108session_t *session_find_by_mac(const char *mac) 39session_t *session_find_by_mac(const char *mac)
109{ 40{
110 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) { 41 return (session_t *)tollgate_core_session_find_by_mac(mac);
111 if (s_sessions[i].active && s_sessions[i].mac[0] != '\0' &&
112 strcmp(s_sessions[i].mac, mac) == 0) {
113 return &s_sessions[i];
114 }
115 }
116 return NULL;
117} 42}
118 43
119void session_extend(session_t *session, uint64_t additional_ms) 44void session_extend(session_t *session, uint64_t additional_ms)
120{ 45{
121 if (!session || !session->active) return; 46 tollgate_core_session_extend((tg_session_t *)session, additional_ms);
122 session->allotment_ms += additional_ms;
123 esp_ip4_addr_t ip = { .addr = session->client_ip };
124 ESP_LOGI(TAG, "Session extended: " IPSTR " +%llums (total=%llu)", IP2STR(&ip),
125 (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms);
126} 47}
127 48
128bool session_is_expired(const session_t *session) 49bool session_is_expired(const session_t *session)
129{ 50{
130 if (!session || !session->active) return true; 51 return tollgate_core_session_is_expired((const tg_session_t *)session);
131
132 const tollgate_config_t *cfg = tollgate_config_get();
133 if (cfg && strcmp(cfg->metric, "bytes") == 0) {
134 return session->bytes_consumed >= session->allotment_bytes;
135 }
136
137 int64_t elapsed = get_time_ms() - session->start_time_ms;
138 return elapsed >= (int64_t)session->allotment_ms;
139} 52}
140 53
141void session_check_expiry(void) 54void session_check_expiry(void)
142{ 55{
143 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) {
144 if (s_sessions[i].active && session_is_expired(&s_sessions[i])) {
145 esp_ip4_addr_t ip = { .addr = s_sessions[i].client_ip };
146 ESP_LOGI(TAG, "Session expired: " IPSTR " mac=%s", IP2STR(&ip),
147 s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown");
148 session_revoke(&s_sessions[i]);
149 }
150 }
151} 56}
152 57
153void session_revoke(session_t *session) 58void session_revoke(session_t *session)
154{ 59{
155 if (!session || !session->active) return; 60 tollgate_core_session_revoke((tg_session_t *)session);
156 firewall_revoke_access(session->client_ip);
157 session->active = false;
158 s_session_count--;
159} 61}
160 62
161void session_revoke_all(void) 63void session_revoke_all(void)
162{ 64{
163 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) { 65 tollgate_core_session_revoke_all();
164 if (s_sessions[i].active) {
165 session_revoke(&s_sessions[i]);
166 }
167 }
168} 66}
169 67
170int session_active_count(void) 68int session_active_count(void)
171{ 69{
172 int count = 0; 70 return tollgate_core_active_session_count();
173 for (int i = 0; i < SESSION_MAX_CLIENTS; i++) {
174 if (s_sessions[i].active) count++;
175 }
176 return count;
177} 71}
178 72
179void session_tick(void) 73void session_tick(void)
180{ 74{
181 session_check_expiry(); 75 tollgate_core_tick();
182} 76}
183 77
184session_t *cvm_get_sessions_array(void) 78session_t *cvm_get_sessions_array(void)
185{ 79{
186 return s_sessions; 80 return (session_t *)tollgate_core_session_get_array();
187} 81}
188 82
189int cvm_get_sessions_count(void) 83int cvm_get_sessions_count(void)
190{ 84{
191 return SESSION_MAX_CLIENTS; 85 return tollgate_core_session_get_array_size();
192} 86}