upleb.uk

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

summaryrefslogtreecommitdiff
path: root/components/tollgate_core/src/tollgate_core_session.c
blob: 48d32e7c8628355b7843220c0c815ecec8342871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "tollgate_core_session.h"
#include "tollgate_core_firewall.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <string.h>

static const char *TAG = "tg_core_session";
static tg_session_t s_sessions[TG_SESSION_MAX_CLIENTS];
static int s_session_count = 0;

static const tollgate_platform_t *s_platform;

void tollgate_core_session_set_platform(const tollgate_platform_t *platform)
{
    s_platform = platform;
}

static int64_t get_time_ms(void)
{
    if (s_platform && s_platform->get_time_ms) {
        return s_platform->get_time_ms();
    }
    return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS;
}

esp_err_t tollgate_core_session_init(void)
{
    memset(s_sessions, 0, sizeof(s_sessions));
    s_session_count = 0;
    ESP_LOGI(TAG, "Session manager initialized");
    return ESP_OK;
}

static void populate_mac(tg_session_t *session, uint32_t client_ip)
{
    if (tollgate_core_fw_get_mac_for_ip(client_ip, session->mac, sizeof(session->mac)) != ESP_OK) {
        session->mac[0] = '\0';
    }
}

tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms)
{
    tg_session_t *existing = tollgate_core_session_find_by_ip(client_ip);
    if (existing) {
        tollgate_core_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 || tollgate_core_session_is_expired(&s_sessions[i])) {
                tollgate_core_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;
            populate_mac(&s_sessions[i], client_ip);

            s_session_count++;
            tollgate_core_fw_grant(client_ip);

            esp_ip4_addr_t ip = { .addr = client_ip };
            ESP_LOGI(TAG, "Session created: " IPSTR " mac=%s allotment=%llums", IP2STR(&ip),
                     s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown",
                     (unsigned long long)allotment_ms);
            return &s_sessions[i];
        }
    }

    ESP_LOGW(TAG, "No free session slots");
    return NULL;
}

tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes)
{
    tg_session_t *s = tollgate_core_session_create(client_ip, 0);
    if (s) {
        s->allotment_bytes = allotment_bytes;
        s->bytes_consumed = 0;
        s->allotment_ms = INT64_MAX;
        esp_ip4_addr_t ip = { .addr = client_ip };
        ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip),
                 (unsigned long long)allotment_bytes);
    }
    return s;
}

void tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes)
{
    tg_session_t *s = tollgate_core_session_find_by_ip(client_ip);
    if (s && s->active) {
        s->bytes_consumed += bytes;
    }
}

tg_session_t *tollgate_core_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 *tollgate_core_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 tollgate_core_session_extend(tg_session_t *session, uint64_t additional_ms)
{
    if (!session || !session->active) return;
    session->allotment_ms += additional_ms;
    esp_ip4_addr_t ip = { .addr = session->client_ip };
    ESP_LOGI(TAG, "Session extended: " IPSTR " +%llums (total=%llu)", IP2STR(&ip),
             (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms);
}

bool tollgate_core_session_is_expired(const tg_session_t *session)
{
    if (!session || !session->active) return true;

    if (s_platform && s_platform->get_metric) {
        const char *metric = s_platform->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 && tollgate_core_session_is_expired(&s_sessions[i])) {
            esp_ip4_addr_t ip = { .addr = s_sessions[i].client_ip };
            ESP_LOGI(TAG, "Session expired: " IPSTR " mac=%s", IP2STR(&ip),
                     s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown");
            tollgate_core_session_revoke(&s_sessions[i]);
        }
    }
}

void tollgate_core_session_revoke(tg_session_t *session)
{
    if (!session || !session->active) return;
    tollgate_core_fw_revoke(session->client_ip);
    session->active = false;
    s_session_count--;
}

void tollgate_core_session_revoke_all(void)
{
    for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) {
        if (s_sessions[i].active) {
            tollgate_core_session_revoke(&s_sessions[i]);
        }
    }
}

int tollgate_core_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 tollgate_core_session_tick(void)
{
    check_expiry();
}

tg_session_t *tollgate_core_session_get_array(void)
{
    return s_sessions;
}

int tollgate_core_session_get_array_size(void)
{
    return TG_SESSION_MAX_CLIENTS;
}