upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tollgate_core/src/tollgate_session.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-23 04:50:26 +0530
committerYour Name <you@example.com>2026-05-23 04:50:26 +0530
commit9330b01c28aebc865a3c0a51df8730196cb4152e (patch)
tree60c3ae35b2550c737c228e81fd342c8d07af11af /tollgate_core/src/tollgate_session.c
parent851801f50f1282ab1db5f8a241dbd245f59e447e (diff)
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.
Diffstat (limited to 'tollgate_core/src/tollgate_session.c')
-rw-r--r--tollgate_core/src/tollgate_session.c220
1 files changed, 0 insertions, 220 deletions
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 @@
1#include "tollgate_session.h"
2#include "tollgate_core.h"
3#include "tollgate_platform.h"
4#include "tollgate_firewall.h"
5#include <string.h>
6#include <stdio.h>
7
8static const char *TAG = "tg_session";
9static tg_session_t s_sessions[TG_SESSION_MAX_CLIENTS];
10static int s_session_count = 0;
11
12static void format_ip(uint32_t ip, char *buf, int buf_len)
13{
14 snprintf(buf, buf_len, "%d.%d.%d.%d",
15 (int)((ip >> 0) & 0xFF), (int)((ip >> 8) & 0xFF),
16 (int)((ip >> 16) & 0xFF), (int)((ip >> 24) & 0xFF));
17}
18
19static int64_t get_time_ms(void)
20{
21 const tollgate_platform_t *p = tollgate_core_get_platform();
22 if (p && p->get_time_ms) return p->get_time_ms();
23 return 0;
24}
25
26static void log_session(const char *verb, uint32_t client_ip, const char *mac, const char *detail)
27{
28 const tollgate_platform_t *p = tollgate_core_get_platform();
29 if (p && p->log_info) {
30 char ip_str[16];
31 format_ip(client_ip, ip_str, sizeof(ip_str));
32 p->log_info(TAG, "%s: %s mac=%s %s", verb, ip_str, mac ? mac : "unknown", detail ? detail : "");
33 }
34}
35
36int tg_session_init(void)
37{
38 memset(s_sessions, 0, sizeof(s_sessions));
39 s_session_count = 0;
40 const tollgate_platform_t *p = tollgate_core_get_platform();
41 if (p && p->log_info) p->log_info(TAG, "Session manager initialized");
42 return 0;
43}
44
45static void populate_mac(tg_session_t *session, uint32_t client_ip)
46{
47 const tollgate_platform_t *p = tollgate_core_get_platform();
48 if (p && p->mac_for_ip) {
49 if (!p->mac_for_ip(client_ip, session->mac, sizeof(session->mac))) {
50 session->mac[0] = '\0';
51 }
52 } else {
53 session->mac[0] = '\0';
54 }
55}
56
57tg_session_t *tg_session_create(uint32_t client_ip, uint64_t allotment_ms)
58{
59 tg_session_t *existing = tg_session_find_by_ip(client_ip);
60 if (existing) {
61 tg_session_extend(existing, allotment_ms);
62 return existing;
63 }
64
65 if (s_session_count >= TG_SESSION_MAX_CLIENTS) {
66 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
67 if (!s_sessions[i].active || tg_session_is_expired(&s_sessions[i])) {
68 tg_session_revoke(&s_sessions[i]);
69 break;
70 }
71 }
72 }
73
74 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
75 if (!s_sessions[i].active) {
76 s_sessions[i].client_ip = client_ip;
77 s_sessions[i].allotment_ms = allotment_ms;
78 s_sessions[i].start_time_ms = get_time_ms();
79 s_sessions[i].active = true;
80 s_sessions[i].payment_method = TG_PAYMENT_CASHU;
81 populate_mac(&s_sessions[i], client_ip);
82
83 s_session_count++;
84 tg_firewall_grant(client_ip);
85
86 char detail[64];
87 snprintf(detail, sizeof(detail), "allotment=%llums", (unsigned long long)allotment_ms);
88 log_session("created", client_ip,
89 s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", detail);
90 return &s_sessions[i];
91 }
92 }
93
94 const tollgate_platform_t *p = tollgate_core_get_platform();
95 if (p && p->log_warn) p->log_warn(TAG, "No free session slots");
96 return NULL;
97}
98
99tg_session_t *tg_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes)
100{
101 tg_session_t *s = tg_session_create(client_ip, 0);
102 if (s) {
103 s->allotment_bytes = allotment_bytes;
104 s->bytes_consumed = 0;
105 s->allotment_ms = (uint64_t)-1;
106 s->payment_method = TG_PAYMENT_BYTES;
107 char detail[64];
108 snprintf(detail, sizeof(detail), "allotment=%llu bytes", (unsigned long long)allotment_bytes);
109 log_session("bytes session", client_ip, s->mac[0] ? s->mac : "unknown", detail);
110 }
111 return s;
112}
113
114void tg_session_add_bytes(uint32_t client_ip, uint64_t bytes)
115{
116 tg_session_t *s = tg_session_find_by_ip(client_ip);
117 if (s && s->active) {
118 s->bytes_consumed += bytes;
119 }
120}
121
122tg_session_t *tg_session_find_by_ip(uint32_t client_ip)
123{
124 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
125 if (s_sessions[i].active && s_sessions[i].client_ip == client_ip) {
126 return &s_sessions[i];
127 }
128 }
129 return NULL;
130}
131
132tg_session_t *tg_session_find_by_mac(const char *mac)
133{
134 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
135 if (s_sessions[i].active && s_sessions[i].mac[0] != '\0' &&
136 strcmp(s_sessions[i].mac, mac) == 0) {
137 return &s_sessions[i];
138 }
139 }
140 return NULL;
141}
142
143void tg_session_extend(tg_session_t *session, uint64_t additional_ms)
144{
145 if (!session || !session->active) return;
146 session->allotment_ms += additional_ms;
147 char detail[64];
148 snprintf(detail, sizeof(detail), "+%llums (total=%llu)",
149 (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms);
150 log_session("extended", session->client_ip,
151 session->mac[0] ? session->mac : "unknown", detail);
152}
153
154bool tg_session_is_expired(const tg_session_t *session)
155{
156 if (!session || !session->active) return true;
157
158 const tollgate_platform_t *p = tollgate_core_get_platform();
159 if (p && p->get_metric) {
160 const char *metric = p->get_metric();
161 if (metric && strcmp(metric, "bytes") == 0) {
162 return session->bytes_consumed >= session->allotment_bytes;
163 }
164 }
165
166 int64_t elapsed = get_time_ms() - session->start_time_ms;
167 return elapsed >= (int64_t)session->allotment_ms;
168}
169
170static void check_expiry(void)
171{
172 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
173 if (s_sessions[i].active && tg_session_is_expired(&s_sessions[i])) {
174 log_session("expired", s_sessions[i].client_ip,
175 s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", NULL);
176 tg_session_revoke(&s_sessions[i]);
177 }
178 }
179}
180
181void tg_session_revoke(tg_session_t *session)
182{
183 if (!session || !session->active) return;
184 tg_firewall_revoke(session->client_ip);
185 session->active = false;
186 s_session_count--;
187}
188
189void tg_session_revoke_all(void)
190{
191 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
192 if (s_sessions[i].active) {
193 tg_session_revoke(&s_sessions[i]);
194 }
195 }
196}
197
198int tg_session_active_count(void)
199{
200 int count = 0;
201 for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
202 if (s_sessions[i].active) count++;
203 }
204 return count;
205}
206
207void tg_session_tick(void)
208{
209 check_expiry();
210}
211
212tg_session_t *tg_session_get_array(void)
213{
214 return s_sessions;
215}
216
217int tg_session_get_array_size(void)
218{
219 return TG_SESSION_MAX_CLIENTS;
220}