upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tollgate_core
diff options
context:
space:
mode:
Diffstat (limited to 'tollgate_core')
-rw-r--r--tollgate_core/CMakeLists.txt28
-rw-r--r--tollgate_core/include/tollgate_core.h90
-rw-r--r--tollgate_core/include/tollgate_platform.h68
-rw-r--r--tollgate_core/src/tollgate_cashu.c253
-rw-r--r--tollgate_core/src/tollgate_cashu.h40
-rw-r--r--tollgate_core/src/tollgate_core.c466
-rw-r--r--tollgate_core/src/tollgate_firewall.c166
-rw-r--r--tollgate_core/src/tollgate_firewall.h21
-rw-r--r--tollgate_core/src/tollgate_mining.c171
-rw-r--r--tollgate_core/src/tollgate_mining.h34
-rw-r--r--tollgate_core/src/tollgate_session.c220
-rw-r--r--tollgate_core/src/tollgate_session.h42
12 files changed, 0 insertions, 1599 deletions
diff --git a/tollgate_core/CMakeLists.txt b/tollgate_core/CMakeLists.txt
deleted file mode 100644
index 988167f..0000000
--- a/tollgate_core/CMakeLists.txt
+++ /dev/null
@@ -1,28 +0,0 @@
1cmake_minimum_required(VERSION 3.16)
2project(tollgate_core C)
3
4set(TG_CORE_SOURCES
5 src/tollgate_core.c
6 src/tollgate_cashu.c
7 src/tollgate_session.c
8 src/tollgate_mining.c
9)
10
11add_library(tollgate_core STATIC ${TG_CORE_SOURCES})
12
13target_include_directories(tollgate_core PUBLIC
14 ${CMAKE_CURRENT_SOURCE_DIR}/include
15 ${CMAKE_CURRENT_SOURCE_DIR}/src
16)
17
18find_package(PkgConfig REQUIRED)
19pkg_check_modules(CJSON REQUIRED libcjson)
20
21target_include_directories(tollgate_core PRIVATE
22 ${CJSON_INCLUDE_DIRS}
23 /usr/include
24)
25
26target_link_libraries(tollgate_core PUBLIC mbedcrypto cjson m)
27
28target_compile_options(tollgate_core PRIVATE -Wall -Wextra -Wno-unused-parameter)
diff --git a/tollgate_core/include/tollgate_core.h b/tollgate_core/include/tollgate_core.h
deleted file mode 100644
index bbae7cf..0000000
--- a/tollgate_core/include/tollgate_core.h
+++ /dev/null
@@ -1,90 +0,0 @@
1#ifndef TOLLGATE_CORE_H
2#define TOLLGATE_CORE_H
3
4#include "tollgate_platform.h"
5#include <stdbool.h>
6#include <stdint.h>
7
8int tollgate_core_init(const tollgate_platform_t *platform, uint32_t ap_ip);
9void tollgate_core_tick(void);
10
11int tollgate_core_dns_start(uint32_t upstream_dns);
12void tollgate_core_dns_stop(void);
13
14int tollgate_core_process_payment(uint32_t client_ip, const char *token_str);
15int tollgate_core_process_share(uint32_t client_ip, const char *job_id,
16 const char *nonce, const char *ntime, const char *version);
17
18void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip);
19void tollgate_core_client_disconnected(const uint8_t *mac);
20
21bool tollgate_core_is_client_allowed(uint32_t client_ip);
22bool tollgate_core_is_dns_running(void);
23
24char *tollgate_core_get_status_json(void);
25char *tollgate_core_get_config_json(void);
26
27int tollgate_core_active_session_count(void);
28int tollgate_core_allowed_client_count(void);
29int tollgate_core_firewall_revoke_all(void);
30void tollgate_core_firewall_set_mining_port(uint16_t port);
31void tollgate_core_firewall_set_sandbox_mint_access(bool enable);
32
33bool tollgate_core_is_owner(uint32_t client_ip);
34bool tollgate_core_is_owner_connected(void);
35
36double tollgate_core_get_hashprice(void);
37void tollgate_core_set_nbits(uint32_t nbits);
38const void *tollgate_core_get_current_job(void);
39void tollgate_core_set_job(const void *job);
40
41int tollgate_core_stratum_client_start(void);
42void tollgate_core_stratum_client_stop(void);
43
44int tollgate_core_stratum_proxy_init(uint16_t port);
45void tollgate_core_stratum_proxy_get_stats(void *out);
46
47void tollgate_core_mining_init(void);
48
49void tollgate_core_beacon_start(void);
50
51void tollgate_core_market_init(void);
52void tollgate_core_market_on_scan_result(const void *ie_data, int ie_len,
53 const uint8_t *bssid, int rssi);
54
55const void *tollgate_core_get_sessions_array(void);
56int tollgate_core_get_sessions_array_size(void);
57void *tollgate_core_find_session_by_ip(uint32_t ip);
58void *tollgate_core_find_session_by_mac(const char *mac);
59void *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms);
60void *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes);
61void tollgate_core_session_extend(void *session, uint64_t additional_ms);
62void tollgate_core_session_revoke(void *session);
63int tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes);
64bool tollgate_core_session_is_expired(const void *session);
65
66void tollgate_core_firewall_grant(uint32_t client_ip);
67void tollgate_core_firewall_revoke(uint32_t client_ip);
68int tollgate_core_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size);
69
70int tollgate_core_cashu_decode(const char *token_str, void *out);
71int tollgate_core_cashu_check_states(const char *mint_url, const void *token,
72 void *states, int *state_count);
73uint64_t tollgate_core_cashu_allotment(uint64_t amount, uint64_t price, uint64_t step_size);
74bool tollgate_core_cashu_is_mint_accepted(const char *mint_url);
75const char *tollgate_core_cashu_token_mint(const void *token);
76uint64_t tollgate_core_cashu_token_amount(const void *token);
77
78void tollgate_core_mining_update_hashrate(uint32_t client_ip, bool accepted);
79const void *tollgate_core_mining_get_client_stats(uint32_t client_ip);
80double tollgate_core_mining_get_hashprice(void);
81uint64_t tollgate_core_mining_shares_to_allotment_ms(double hashrate, double hashprice,
82 int price, int step_ms);
83uint64_t tollgate_core_mining_shares_to_allotment_bytes(double hashrate, double hashprice,
84 int price, int step_bytes);
85void tollgate_core_mining_set_nbits(uint32_t nbits);
86
87const tollgate_platform_t *tollgate_core_get_platform(void);
88uint32_t tollgate_core_get_ap_ip(void);
89
90#endif
diff --git a/tollgate_core/include/tollgate_platform.h b/tollgate_core/include/tollgate_platform.h
deleted file mode 100644
index b553a83..0000000
--- a/tollgate_core/include/tollgate_platform.h
+++ /dev/null
@@ -1,68 +0,0 @@
1#ifndef TOLLGATE_PLATFORM_H
2#define TOLLGATE_PLATFORM_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include <stddef.h>
7
8typedef struct {
9 uint16_t (*get_price_sats)(void);
10 int32_t (*get_step_ms)(void);
11 int64_t (*get_step_bytes)(void);
12 const char* (*get_mint_url)(void);
13 const char* (*get_metric)(void);
14
15 int64_t (*get_time_ms)(void);
16
17 void (*log_info)(const char *tag, const char *fmt, ...);
18 void (*log_warn)(const char *tag, const char *fmt, ...);
19 void (*log_error)(const char *tag, const char *fmt, ...);
20
21 bool (*wallet_receive)(const char *token);
22 bool (*wallet_send)(uint64_t amount, char *buf, size_t buf_len);
23 uint64_t (*wallet_balance)(void);
24
25 int (*http_post)(const char *url, const char *headers,
26 const char *body, int body_len,
27 char *resp, int resp_len);
28
29 bool (*create_task)(void (*fn)(void*), void *arg,
30 const char *name, int stack_bytes, int priority);
31
32 int (*socket_udp)(void);
33 int (*socket_tcp)(void);
34 int (*socket_bind)(int fd, uint32_t ip, uint16_t port);
35 int (*socket_listen)(int fd, int backlog);
36 int (*socket_accept)(int fd, uint32_t *client_ip, uint16_t *client_port);
37 int (*socket_recvfrom)(int fd, void *buf, int len,
38 uint32_t *src_ip, uint16_t *src_port);
39 int (*socket_sendto)(int fd, const void *buf, int len,
40 uint32_t dest_ip, uint16_t dest_port);
41 int (*socket_read)(int fd, void *buf, int len);
42 int (*socket_write)(int fd, const void *buf, int len);
43 void (*socket_close)(int fd);
44 void (*socket_set_recv_timeout)(int fd, int ms);
45
46 bool (*get_sta_mac_ip_list)(void *list_out, int max, int *count_out);
47 bool (*set_vendor_ie)(bool enable, const void *ie_data, int ie_len);
48 int (*arp_get_mac)(uint32_t ip, uint8_t *mac_out);
49 void (*napt_enable)(uint32_t ip, bool enable);
50
51 bool (*mining_enabled)(void);
52 const char* (*get_stratum_host)(void);
53 uint16_t (*get_stratum_port)(void);
54 const char* (*get_stratum_user)(void);
55 const char* (*get_stratum_pass)(void);
56 uint16_t (*get_mining_port)(void);
57 uint64_t (*get_hashprice_override)(void);
58
59 void (*fill_random)(void *buf, int len);
60
61 int (*get_accepted_mint_count)(void);
62 const char* (*get_accepted_mint)(int index);
63 bool (*is_mint_reachable)(const char *mint_url);
64 bool (*mac_for_ip)(uint32_t ip, char *mac_out, int mac_out_size);
65
66} tollgate_platform_t;
67
68#endif
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 @@
1#include "tollgate_cashu.h"
2#include "tollgate_core.h"
3#include "tollgate_platform.h"
4#include <stdlib.h>
5#include <string.h>
6#include <stdio.h>
7#include <cJSON.h>
8#include <mbedtls/base64.h>
9#include <mbedtls/sha256.h>
10
11static const char *TAG = "tg_cashu";
12
13static const char V3_PREFIX[] = "cashuA";
14static const size_t V3_PREFIX_LEN = 6;
15
16static int b64url_decode(const char *input, size_t input_len, char *out, size_t out_size, size_t *out_len)
17{
18 char *b64 = malloc(input_len + 4);
19 if (!b64) return -1;
20 size_t b64_len = input_len;
21 memcpy(b64, input, b64_len);
22 b64[b64_len] = '\0';
23
24 for (size_t i = 0; i < b64_len; i++) {
25 if (b64[i] == '-') b64[i] = '+';
26 else if (b64[i] == '_') b64[i] = '/';
27 }
28 while (b64_len % 4 != 0) {
29 b64[b64_len++] = '=';
30 }
31 b64[b64_len] = '\0';
32
33 size_t olen = 0;
34 int ret = mbedtls_base64_decode((unsigned char *)out, out_size, &olen,
35 (const unsigned char *)b64, b64_len);
36 free(b64);
37 if (ret != 0) return -1;
38 *out_len = olen;
39 return 0;
40}
41
42static int parse_proofs_array(cJSON *arr, tg_cashu_token_t *out)
43{
44 if (!cJSON_IsArray(arr)) return -1;
45 int count = cJSON_GetArraySize(arr);
46 if (count > TG_CASHU_MAX_PROOFS) return -1;
47
48 out->proof_count = 0;
49 out->total_amount = 0;
50 for (int i = 0; i < count; i++) {
51 cJSON *proof = cJSON_GetArrayItem(arr, i);
52 cJSON *amt = cJSON_GetObjectItemCaseSensitive(proof, "amount");
53 cJSON *id = cJSON_GetObjectItemCaseSensitive(proof, "id");
54 cJSON *secret = cJSON_GetObjectItemCaseSensitive(proof, "secret");
55 cJSON *c = cJSON_GetObjectItemCaseSensitive(proof, "C");
56
57 if (!amt || !cJSON_IsNumber(amt)) return -1;
58
59 out->proofs[i].amount = (uint64_t)amt->valuedouble;
60 out->total_amount += out->proofs[i].amount;
61
62 if (id && cJSON_IsString(id)) {
63 strncpy(out->proofs[i].id, id->valuestring, sizeof(out->proofs[i].id) - 1);
64 }
65 if (secret && cJSON_IsString(secret)) {
66 strncpy(out->proofs[i].secret, secret->valuestring, sizeof(out->proofs[i].secret) - 1);
67 }
68 if (c && cJSON_IsString(c)) {
69 strncpy(out->proofs[i].c, c->valuestring, sizeof(out->proofs[i].c) - 1);
70 }
71 out->proof_count++;
72 }
73 return 0;
74}
75
76int tg_cashu_decode_token(const char *token_str, tg_cashu_token_t *out)
77{
78 const tollgate_platform_t *p = tollgate_core_get_platform();
79 if (!token_str || !out) return -1;
80 memset(out, 0, sizeof(*out));
81
82 size_t len = strlen(token_str);
83 char *nl = strchr(token_str, '\n');
84 if (nl) len = nl - token_str;
85 char *cr = strchr(token_str, '\r');
86 if (cr && (cr - token_str) < (int)len) len = cr - token_str;
87 if (len <= V3_PREFIX_LEN) {
88 if (p && p->log_error) p->log_error(TAG, "Token too short");
89 return -1;
90 }
91 if (strncmp(token_str, V3_PREFIX, V3_PREFIX_LEN) != 0) {
92 if (p && p->log_error) p->log_error(TAG, "Token missing cashuA prefix");
93 return -1;
94 }
95
96 size_t b64_len = len - V3_PREFIX_LEN;
97 size_t decoded_size = (b64_len * 3) / 4 + 4;
98 char *json_buf = malloc(decoded_size);
99 if (!json_buf) return -1;
100 size_t json_len = 0;
101 if (b64url_decode(token_str + V3_PREFIX_LEN, b64_len,
102 json_buf, decoded_size - 1, &json_len) != 0) {
103 if (p && p->log_error) p->log_error(TAG, "Base64url decode failed");
104 free(json_buf);
105 return -1;
106 }
107 json_buf[json_len] = '\0';
108
109 cJSON *root = cJSON_Parse(json_buf);
110 free(json_buf);
111 if (!root) {
112 if (p && p->log_error) p->log_error(TAG, "JSON parse failed");
113 return -1;
114 }
115
116 cJSON *token_arr = cJSON_GetObjectItemCaseSensitive(root, "token");
117 if (token_arr && cJSON_IsArray(token_arr)) {
118 cJSON *first = cJSON_GetArrayItem(token_arr, 0);
119 if (!first) { cJSON_Delete(root); return -1; }
120
121 cJSON *mint = cJSON_GetObjectItemCaseSensitive(first, "mint");
122 if (mint && cJSON_IsString(mint)) {
123 strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1);
124 }
125
126 cJSON *proofs = cJSON_GetObjectItemCaseSensitive(first, "proofs");
127 if (proofs) {
128 if (parse_proofs_array(proofs, out) != 0) { cJSON_Delete(root); return -1; }
129 }
130 } else {
131 cJSON *mint = cJSON_GetObjectItemCaseSensitive(root, "mint");
132 if (mint && cJSON_IsString(mint)) {
133 strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1);
134 }
135
136 cJSON *proofs = cJSON_GetObjectItemCaseSensitive(root, "proofs");
137 if (proofs) {
138 if (parse_proofs_array(proofs, out) != 0) { cJSON_Delete(root); return -1; }
139 }
140 }
141
142 cJSON_Delete(root);
143
144 if (out->proof_count == 0) {
145 if (p && p->log_error) p->log_error(TAG, "No proofs in token");
146 return -1;
147 }
148
149 if (p && p->log_info) p->log_info(TAG, "Decoded token: %d proofs, total=%llu, mint=%s",
150 out->proof_count, (unsigned long long)out->total_amount, out->mint_url);
151 return 0;
152}
153
154static void sha256_hex(const char *data, size_t data_len, char *hex_out)
155{
156 unsigned char hash[32];
157 mbedtls_sha256((const unsigned char *)data, data_len, hash, 0);
158 for (int i = 0; i < 32; i++) {
159 sprintf(hex_out + i * 2, "%02x", hash[i]);
160 }
161 hex_out[64] = '\0';
162}
163
164int tg_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token,
165 tg_cashu_proof_state_t *states, int *state_count)
166{
167 const tollgate_platform_t *p = tollgate_core_get_platform();
168
169 cJSON *ys_arr = cJSON_CreateArray();
170 for (int i = 0; i < token->proof_count; i++) {
171 char y_hex[65];
172 sha256_hex(token->proofs[i].secret, strlen(token->proofs[i].secret), y_hex);
173 cJSON_AddItemToArray(ys_arr, cJSON_CreateString(y_hex));
174 strncpy(states[i].y_hex, y_hex, sizeof(states[i].y_hex) - 1);
175 states[i].spent = false;
176 }
177 *state_count = token->proof_count;
178
179 char *ys_json = cJSON_PrintUnformatted(ys_arr);
180 cJSON_Delete(ys_arr);
181
182 char *post_body = malloc(4096);
183 if (!post_body) { cJSON_free(ys_json); return -1; }
184 snprintf(post_body, 4096, "{\"Ys\":%s}", ys_json);
185 cJSON_free(ys_json);
186
187 char url[512];
188 snprintf(url, sizeof(url), "%s/v1/checkstate", mint_url);
189
190 if (!p || !p->http_post) {
191 free(post_body);
192 return -1;
193 }
194
195 char *resp_buf = malloc(8192);
196 if (!resp_buf) { free(post_body); return -1; }
197
198 int resp_len = p->http_post(url, "Content-Type: application/json",
199 post_body, (int)strlen(post_body),
200 resp_buf, 8191);
201 free(post_body);
202
203 if (resp_len <= 0) {
204 if (p && p->log_error) p->log_error(TAG, "checkstate HTTP failed: resp_len=%d", resp_len);
205 free(resp_buf);
206 return -1;
207 }
208 resp_buf[resp_len] = '\0';
209
210 cJSON *root = cJSON_Parse(resp_buf);
211 free(resp_buf);
212 if (!root) return -1;
213
214 cJSON *states_arr = cJSON_GetObjectItemCaseSensitive(root, "states");
215 if (!states_arr || !cJSON_IsArray(states_arr)) {
216 cJSON_Delete(root);
217 return -1;
218 }
219
220 int n = cJSON_GetArraySize(states_arr);
221 for (int i = 0; i < n && i < token->proof_count; i++) {
222 cJSON *s = cJSON_GetArrayItem(states_arr, i);
223 cJSON *state = cJSON_GetObjectItemCaseSensitive(s, "state");
224 if (state && cJSON_IsString(state)) {
225 states[i].spent = (strcmp(state->valuestring, "SPENT") == 0);
226 }
227 }
228
229 cJSON_Delete(root);
230 return 0;
231}
232
233uint64_t tg_cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step,
234 uint64_t step_size_ms)
235{
236 if (price_per_step == 0) return 0;
237 return (token_amount / price_per_step) * step_size_ms;
238}
239
240uint64_t tg_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step,
241 uint64_t step_size)
242{
243 if (price_per_step == 0) return 0;
244 return (token_amount / price_per_step) * step_size;
245}
246
247bool tg_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url)
248{
249 if (!mint_url || mint_url[0] == '\0') return false;
250 if (!accepted_mint_url || accepted_mint_url[0] == '\0') return false;
251 return (strstr(mint_url, accepted_mint_url) != NULL ||
252 strcmp(mint_url, accepted_mint_url) == 0);
253}
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 @@
1#ifndef TOLLGATE_CORE_CASHU_H
2#define TOLLGATE_CORE_CASHU_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#define TG_CASHU_MAX_PROOFS 10
8#define TG_CASHU_MAX_SECRET_LEN 128
9#define TG_CASHU_MAX_ID_LEN 68
10#define TG_CASHU_MAX_C_LEN 128
11
12typedef struct {
13 uint64_t amount;
14 char id[TG_CASHU_MAX_ID_LEN];
15 char secret[TG_CASHU_MAX_SECRET_LEN];
16 char c[TG_CASHU_MAX_C_LEN];
17} tg_cashu_proof_t;
18
19typedef struct {
20 tg_cashu_proof_t proofs[TG_CASHU_MAX_PROOFS];
21 int proof_count;
22 char mint_url[256];
23 uint64_t total_amount;
24} tg_cashu_token_t;
25
26typedef struct {
27 char y_hex[65];
28 bool spent;
29} tg_cashu_proof_state_t;
30
31int tg_cashu_decode_token(const char *token_str, tg_cashu_token_t *out);
32int tg_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token,
33 tg_cashu_proof_state_t *states, int *state_count);
34uint64_t tg_cashu_calculate_allotment_ms(uint64_t token_amount, uint64_t price_per_step,
35 uint64_t step_size_ms);
36uint64_t tg_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step,
37 uint64_t step_size);
38bool tg_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url);
39
40#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 @@
1#include "tollgate_core.h"
2#include "tollgate_platform.h"
3#include "tollgate_cashu.h"
4#include "tollgate_session.h"
5#include "tollgate_firewall.h"
6#include "tollgate_mining.h"
7#include <string.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11static const char *TAG = "tg_core";
12static const tollgate_platform_t *s_platform;
13static uint32_t s_ap_ip;
14
15static uint32_t s_owner_ip;
16static uint8_t s_owner_mac[6];
17static bool s_owner_connected;
18
19const tollgate_platform_t *tollgate_core_get_platform(void)
20{
21 return s_platform;
22}
23
24uint32_t tollgate_core_get_ap_ip(void)
25{
26 return s_ap_ip;
27}
28
29int tollgate_core_init(const tollgate_platform_t *platform, uint32_t ap_ip)
30{
31 if (!platform) return -1;
32
33 s_platform = platform;
34 s_ap_ip = ap_ip;
35 s_owner_connected = false;
36 memset(s_owner_mac, 0, sizeof(s_owner_mac));
37
38 tg_session_init();
39 tg_firewall_init(ap_ip);
40 tg_mining_init();
41
42 if (platform->log_info) {
43 char ip_str[16];
44 snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d",
45 (int)((ap_ip >> 0) & 0xFF), (int)((ap_ip >> 8) & 0xFF),
46 (int)((ap_ip >> 16) & 0xFF), (int)((ap_ip >> 24) & 0xFF));
47 platform->log_info(TAG, "TollGate core initialized, AP IP=%s", ip_str);
48 }
49 return 0;
50}
51
52void tollgate_core_tick(void)
53{
54 tg_session_tick();
55}
56
57int tollgate_core_process_payment(uint32_t client_ip, const char *token_str)
58{
59 if (!s_platform || !token_str) return -1;
60
61 const char *accepted_mint = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL;
62 if (!accepted_mint || accepted_mint[0] == '\0') {
63 if (s_platform->log_error) s_platform->log_error(TAG, "No mint URL configured");
64 return -1;
65 }
66
67 tg_cashu_token_t token;
68 if (tg_cashu_decode_token(token_str, &token) != 0) {
69 if (s_platform->log_error) s_platform->log_error(TAG, "Token decode failed");
70 return -1;
71 }
72
73 bool mint_ok = false;
74 if (s_platform->get_accepted_mint_count && s_platform->get_accepted_mint) {
75 int count = s_platform->get_accepted_mint_count();
76 for (int i = 0; i < count; i++) {
77 if (tg_cashu_is_mint_accepted(token.mint_url, s_platform->get_accepted_mint(i))) {
78 mint_ok = true;
79 break;
80 }
81 }
82 } else {
83 mint_ok = tg_cashu_is_mint_accepted(token.mint_url, accepted_mint);
84 }
85 if (!mint_ok) {
86 if (s_platform->log_error) s_platform->log_error(TAG, "Token mint not accepted");
87 return -1;
88 }
89
90 tg_cashu_proof_state_t states[TG_CASHU_MAX_PROOFS];
91 int state_count = 0;
92 if (tg_cashu_check_proof_states(token.mint_url, &token, states, &state_count) != 0) {
93 if (s_platform->log_error) s_platform->log_error(TAG, "Proof state check failed (continuing)");
94 } else {
95 for (int i = 0; i < state_count; i++) {
96 if (states[i].spent) {
97 if (s_platform->log_error) s_platform->log_error(TAG, "Proof %d is SPENT", i);
98 return -1;
99 }
100 }
101 }
102
103 if (s_platform->wallet_receive) {
104 if (!s_platform->wallet_receive(token_str)) {
105 if (s_platform->log_error) s_platform->log_error(TAG, "wallet_receive rejected token");
106 return -1;
107 }
108 }
109
110 const char *metric = (s_platform->get_metric) ? s_platform->get_metric() : "milliseconds";
111 uint64_t price = (s_platform->get_price_sats) ? s_platform->get_price_sats() : 21;
112 uint64_t step_size;
113
114 if (strcmp(metric, "bytes") == 0) {
115 step_size = (s_platform->get_step_bytes) ? (uint64_t)s_platform->get_step_bytes() : 22020096;
116 } else {
117 step_size = (s_platform->get_step_ms) ? (uint64_t)s_platform->get_step_ms() : 60000;
118 }
119
120 uint64_t allotment = tg_cashu_calculate_allotment(token.total_amount, price, step_size);
121 if (allotment == 0) {
122 if (s_platform->log_error) s_platform->log_error(TAG, "Token amount too small");
123 return -1;
124 }
125
126 if (strcmp(metric, "bytes") == 0) {
127 if (!tg_session_create_bytes(client_ip, allotment)) return -1;
128 } else {
129 if (!tg_session_create(client_ip, allotment)) return -1;
130 }
131
132 if (s_platform->log_info) s_platform->log_info(TAG, "Payment: %llu sats -> %llu %s",
133 (unsigned long long)token.total_amount, (unsigned long long)allotment, metric);
134 return 0;
135}
136
137int tollgate_core_process_share(uint32_t client_ip, const char *job_id,
138 const char *nonce, const char *ntime, const char *version)
139{
140 (void)job_id; (void)nonce; (void)ntime; (void)version;
141 if (!s_platform) return -1;
142
143 tg_mining_update_hashrate(client_ip, true);
144 const tg_mining_client_stats_t *stats = tg_mining_get_client_stats(client_ip);
145 if (!stats) return -1;
146
147 double hashprice = tg_mining_get_current_hashprice();
148 uint64_t override = (s_platform->get_hashprice_override) ? s_platform->get_hashprice_override() : 0;
149 if (override > 0) hashprice = tg_mining_calculate_hashprice_override(override);
150
151 const char *metric = (s_platform->get_metric) ? s_platform->get_metric() : "milliseconds";
152 int price = (s_platform->get_price_sats) ? s_platform->get_price_sats() : 21;
153
154 tg_session_t *existing = tg_session_find_by_ip(client_ip);
155 if (existing && existing->payment_method == TG_PAYMENT_MINING) {
156 uint64_t allotment;
157 if (strcmp(metric, "bytes") == 0) {
158 int step_bytes = (s_platform->get_step_bytes) ? (int)s_platform->get_step_bytes() : 22020096;
159 allotment = tg_mining_shares_to_allotment_bytes(stats->hashrate_ghs, hashprice, price, step_bytes);
160 existing->allotment_bytes += allotment;
161 } else {
162 int step_ms = (s_platform->get_step_ms) ? s_platform->get_step_ms() : 60000;
163 allotment = tg_mining_shares_to_allotment_ms(stats->hashrate_ghs, hashprice, price, step_ms);
164 tg_session_extend(existing, allotment);
165 }
166 return 0;
167 }
168
169 uint64_t allotment;
170 if (strcmp(metric, "bytes") == 0) {
171 int step_bytes = (s_platform->get_step_bytes) ? (int)s_platform->get_step_bytes() : 22020096;
172 allotment = tg_mining_shares_to_allotment_bytes(stats->hashrate_ghs, hashprice, price, step_bytes);
173 tg_session_t *s = tg_session_create_bytes(client_ip, allotment);
174 if (s) s->payment_method = TG_PAYMENT_MINING;
175 } else {
176 int step_ms = (s_platform->get_step_ms) ? s_platform->get_step_ms() : 60000;
177 allotment = tg_mining_shares_to_allotment_ms(stats->hashrate_ghs, hashprice, price, step_ms);
178 tg_session_t *s = tg_session_create(client_ip, allotment);
179 if (s) s->payment_method = TG_PAYMENT_MINING;
180 }
181
182 return 0;
183}
184
185void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip)
186{
187 if (!s_owner_connected) {
188 s_owner_connected = true;
189 s_owner_ip = client_ip;
190 if (mac) memcpy(s_owner_mac, mac, 6);
191 if (s_platform && s_platform->log_info) {
192 char ip_str[16];
193 snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d",
194 (int)((client_ip >> 0) & 0xFF), (int)((client_ip >> 8) & 0xFF),
195 (int)((client_ip >> 16) & 0xFF), (int)((client_ip >> 24) & 0xFF));
196 s_platform->log_info(TAG, "First client = owner: %s", ip_str);
197 }
198 return;
199 }
200 if (s_platform && s_platform->log_info) s_platform->log_info(TAG, "Client connected (non-owner)");
201}
202
203void tollgate_core_client_disconnected(const uint8_t *mac)
204{
205 if (!s_owner_connected) return;
206 if (mac && memcmp(s_owner_mac, mac, 6) == 0) {
207 s_owner_connected = false;
208 memset(s_owner_mac, 0, sizeof(s_owner_mac));
209 if (s_platform && s_platform->log_info) s_platform->log_info(TAG, "Owner disconnected");
210 }
211}
212
213bool tollgate_core_is_client_allowed(uint32_t client_ip)
214{
215 return tg_firewall_is_allowed(client_ip);
216}
217
218bool tollgate_core_is_dns_running(void)
219{
220 return false;
221}
222
223char *tollgate_core_get_status_json(void)
224{
225 const int BUFSIZE = 512;
226 char *json = malloc(BUFSIZE);
227 if (!json) return NULL;
228 snprintf(json, BUFSIZE,
229 "{\"ownerConnected\":%s,\"activeSessions\":%d,\"allowedClients\":%d,\"dnsRunning\":%s}",
230 s_owner_connected ? "true" : "false",
231 tg_session_active_count(),
232 tg_firewall_client_count(),
233 tollgate_core_is_dns_running() ? "true" : "false");
234 return json;
235}
236
237char *tollgate_core_get_config_json(void)
238{
239 const int BUFSIZE = 512;
240 char *json = malloc(BUFSIZE);
241 if (!json) return NULL;
242 int pos = 0;
243 pos += snprintf(json + pos, BUFSIZE - pos, "{");
244 if (s_platform) {
245 if (s_platform->get_price_sats)
246 pos += snprintf(json + pos, BUFSIZE - pos, "\"priceSats\":%d,", (int)s_platform->get_price_sats());
247 if (s_platform->get_step_ms)
248 pos += snprintf(json + pos, BUFSIZE - pos, "\"stepMs\":%d,", (int)s_platform->get_step_ms());
249 if (s_platform->get_mint_url)
250 pos += snprintf(json + pos, BUFSIZE - pos, "\"mintUrl\":\"%s\",", s_platform->get_mint_url());
251 if (s_platform->get_metric)
252 pos += snprintf(json + pos, BUFSIZE - pos, "\"metric\":\"%s\"", s_platform->get_metric());
253 }
254 pos += snprintf(json + pos, BUFSIZE - pos, "}");
255 return json;
256}
257
258int tollgate_core_active_session_count(void)
259{
260 return tg_session_active_count();
261}
262
263int tollgate_core_allowed_client_count(void)
264{
265 return tg_firewall_client_count();
266}
267
268int tollgate_core_firewall_revoke_all(void)
269{
270 tg_session_revoke_all();
271 return tg_firewall_revoke_all();
272}
273
274void tollgate_core_firewall_set_mining_port(uint16_t port)
275{
276 tg_firewall_set_mining_port(port);
277}
278
279void tollgate_core_firewall_set_sandbox_mint_access(bool enable)
280{
281 tg_firewall_set_sandbox_mint_access(enable);
282}
283
284bool tollgate_core_is_owner(uint32_t client_ip)
285{
286 return s_owner_connected && s_owner_ip == client_ip;
287}
288
289bool tollgate_core_is_owner_connected(void)
290{
291 return s_owner_connected;
292}
293
294double tollgate_core_get_hashprice(void)
295{
296 return tg_mining_get_current_hashprice();
297}
298
299void tollgate_core_set_nbits(uint32_t nbits)
300{
301 tg_mining_set_current_nbits(nbits);
302}
303
304const void *tollgate_core_get_current_job(void) { return NULL; }
305void tollgate_core_set_job(const void *job) { (void)job; }
306int tollgate_core_stratum_client_start(void) { return -1; }
307void tollgate_core_stratum_client_stop(void) { }
308int tollgate_core_stratum_proxy_init(uint16_t port) { (void)port; return -1; }
309void tollgate_core_stratum_proxy_get_stats(void *out) { (void)out; }
310void tollgate_core_beacon_start(void) { }
311void tollgate_core_market_init(void) { }
312void tollgate_core_market_on_scan_result(const void *ie_data, int ie_len, const uint8_t *bssid, int rssi)
313{
314 (void)ie_data; (void)ie_len; (void)bssid; (void)rssi;
315}
316
317const void *tollgate_core_get_sessions_array(void)
318{
319 return tg_session_get_array();
320}
321
322int tollgate_core_get_sessions_array_size(void)
323{
324 return tg_session_get_array_size();
325}
326
327void *tollgate_core_find_session_by_ip(uint32_t ip)
328{
329 return tg_session_find_by_ip(ip);
330}
331
332void *tollgate_core_find_session_by_mac(const char *mac)
333{
334 return tg_session_find_by_mac(mac);
335}
336
337void tollgate_core_session_extend(void *session, uint64_t additional_ms)
338{
339 tg_session_extend((tg_session_t *)session, additional_ms);
340}
341
342int tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes)
343{
344 tg_session_add_bytes(client_ip, bytes);
345 return 0;
346}
347
348void *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms)
349{
350 return tg_session_create(client_ip, allotment_ms);
351}
352
353void *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes)
354{
355 return tg_session_create_bytes(client_ip, allotment_bytes);
356}
357
358void tollgate_core_session_revoke(void *session)
359{
360 tg_session_revoke((tg_session_t *)session);
361}
362
363bool tollgate_core_session_is_expired(const void *session)
364{
365 return tg_session_is_expired((const tg_session_t *)session);
366}
367
368void tollgate_core_firewall_grant(uint32_t client_ip)
369{
370 tg_firewall_grant(client_ip);
371}
372
373void tollgate_core_firewall_revoke(uint32_t client_ip)
374{
375 tg_firewall_revoke(client_ip);
376}
377
378int tollgate_core_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size)
379{
380 return tg_firewall_get_mac_for_ip(client_ip, mac_out, mac_out_size);
381}
382
383int tollgate_core_cashu_decode(const char *token_str, void *out)
384{
385 return tg_cashu_decode_token(token_str, (tg_cashu_token_t *)out);
386}
387
388int tollgate_core_cashu_check_states(const char *mint_url, const void *token,
389 void *states, int *state_count)
390{
391 return tg_cashu_check_proof_states(mint_url, (const tg_cashu_token_t *)token,
392 (tg_cashu_proof_state_t *)states, state_count);
393}
394
395uint64_t tollgate_core_cashu_allotment(uint64_t amount, uint64_t price, uint64_t step_size)
396{
397 return tg_cashu_calculate_allotment(amount, price, step_size);
398}
399
400bool tollgate_core_cashu_is_mint_accepted(const char *mint_url)
401{
402 if (!s_platform) return false;
403 const char *accepted = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL;
404 if (!accepted) return false;
405 if (s_platform->get_accepted_mint_count && s_platform->get_accepted_mint) {
406 int count = s_platform->get_accepted_mint_count();
407 for (int i = 0; i < count; i++) {
408 if (tg_cashu_is_mint_accepted(mint_url, s_platform->get_accepted_mint(i)))
409 return true;
410 }
411 return false;
412 }
413 return tg_cashu_is_mint_accepted(mint_url, accepted);
414}
415
416const char *tollgate_core_cashu_token_mint(const void *token)
417{
418 const tg_cashu_token_t *t = (const tg_cashu_token_t *)token;
419 return t->mint_url;
420}
421
422uint64_t tollgate_core_cashu_token_amount(const void *token)
423{
424 const tg_cashu_token_t *t = (const tg_cashu_token_t *)token;
425 return t->total_amount;
426}
427
428void tollgate_core_mining_update_hashrate(uint32_t client_ip, bool accepted)
429{
430 tg_mining_update_hashrate(client_ip, accepted);
431}
432
433const void *tollgate_core_mining_get_client_stats(uint32_t client_ip)
434{
435 return tg_mining_get_client_stats(client_ip);
436}
437
438double tollgate_core_mining_get_hashprice(void)
439{
440 return tg_mining_get_current_hashprice();
441}
442
443uint64_t tollgate_core_mining_shares_to_allotment_ms(double hashrate, double hashprice,
444 int price, int step_ms)
445{
446 return tg_mining_shares_to_allotment_ms(hashrate, hashprice, price, step_ms);
447}
448
449uint64_t tollgate_core_mining_shares_to_allotment_bytes(double hashrate, double hashprice,
450 int price, int step_bytes)
451{
452 return tg_mining_shares_to_allotment_bytes(hashrate, hashprice, price, step_bytes);
453}
454
455void tollgate_core_mining_set_nbits(uint32_t nbits)
456{
457 tg_mining_set_current_nbits(nbits);
458}
459
460int tollgate_core_dns_start(uint32_t upstream_dns)
461{
462 (void)upstream_dns;
463 return -1;
464}
465
466void 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 @@
1#include "tollgate_firewall.h"
2#include "tollgate_core.h"
3#include "tollgate_platform.h"
4#include <string.h>
5#include <stdio.h>
6
7#define FW_MAX_CLIENTS 10
8
9static const char *TAG = "tg_fw";
10static uint32_t s_ap_ip;
11static uint16_t s_mining_port;
12static bool s_sandbox_mint;
13
14typedef struct {
15 uint32_t ip;
16 char mac[TG_FW_MAX_MAC_LEN];
17} fw_client_t;
18
19static fw_client_t s_clients[FW_MAX_CLIENTS];
20static int s_client_count = 0;
21
22static void log_fw(const char *verb, uint32_t client_ip, const char *mac)
23{
24 const tollgate_platform_t *p = tollgate_core_get_platform();
25 if (p && p->log_info) {
26 char ip_str[16];
27 snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d",
28 (int)((client_ip >> 0) & 0xFF), (int)((client_ip >> 8) & 0xFF),
29 (int)((client_ip >> 16) & 0xFF), (int)((client_ip >> 24) & 0xFF));
30 p->log_info(TAG, "%s %s mac=%s", verb, ip_str, mac ? mac : "unknown");
31 }
32}
33
34int tg_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size)
35{
36 const tollgate_platform_t *p = tollgate_core_get_platform();
37 if (!p) return -1;
38
39 if (p->mac_for_ip) {
40 if (p->mac_for_ip(client_ip, mac_out, mac_out_size)) {
41 return 0;
42 }
43 }
44 return -1;
45}
46
47int tg_firewall_init(uint32_t ap_ip)
48{
49 s_ap_ip = ap_ip;
50 memset(s_clients, 0, sizeof(s_clients));
51 s_client_count = 0;
52 s_mining_port = 0;
53 s_sandbox_mint = false;
54
55 const tollgate_platform_t *p = tollgate_core_get_platform();
56 if (p && p->napt_enable) p->napt_enable(ap_ip, true);
57
58 if (p && p->log_info) {
59 char ip_str[16];
60 snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d",
61 (int)((ap_ip >> 0) & 0xFF), (int)((ap_ip >> 8) & 0xFF),
62 (int)((ap_ip >> 16) & 0xFF), (int)((ap_ip >> 24) & 0xFF));
63 p->log_info(TAG, "Firewall initialized AP=%s NAT on, per-client filter", ip_str);
64 }
65 return 0;
66}
67
68static fw_client_t *find_client_by_ip(uint32_t client_ip)
69{
70 for (int i = 0; i < s_client_count; i++) {
71 if (s_clients[i].ip == client_ip) return &s_clients[i];
72 }
73 return NULL;
74}
75
76static fw_client_t *find_client_by_mac(const char *mac)
77{
78 for (int i = 0; i < s_client_count; i++) {
79 if (s_clients[i].mac[0] != '\0' && strcmp(s_clients[i].mac, mac) == 0) {
80 return &s_clients[i];
81 }
82 }
83 return NULL;
84}
85
86void tg_firewall_grant(uint32_t client_ip)
87{
88 fw_client_t *existing = find_client_by_ip(client_ip);
89 if (existing) return;
90
91 if (s_client_count >= FW_MAX_CLIENTS) {
92 const tollgate_platform_t *p = tollgate_core_get_platform();
93 if (p && p->log_warn) p->log_warn(TAG, "Max clients, cannot grant");
94 return;
95 }
96
97 fw_client_t *c = &s_clients[s_client_count];
98 c->ip = client_ip;
99 c->mac[0] = '\0';
100 tg_firewall_get_mac_for_ip(client_ip, c->mac, sizeof(c->mac));
101 s_client_count++;
102
103 log_fw("granted", client_ip, c->mac[0] ? c->mac : "unknown");
104}
105
106void tg_firewall_revoke(uint32_t client_ip)
107{
108 for (int i = 0; i < s_client_count; i++) {
109 if (s_clients[i].ip == client_ip) {
110 log_fw("revoked", client_ip, s_clients[i].mac[0] ? s_clients[i].mac : "unknown");
111 s_clients[i] = s_clients[s_client_count - 1];
112 s_client_count--;
113 return;
114 }
115 }
116}
117
118int tg_firewall_revoke_all(void)
119{
120 s_client_count = 0;
121 memset(s_clients, 0, sizeof(s_clients));
122 const tollgate_platform_t *p = tollgate_core_get_platform();
123 if (p && p->log_info) p->log_info(TAG, "All clients revoked");
124 return 0;
125}
126
127bool tg_firewall_is_allowed(uint32_t client_ip)
128{
129 return find_client_by_ip(client_ip) != NULL;
130}
131
132bool tg_firewall_is_mac_allowed(const char *mac)
133{
134 return find_client_by_mac(mac) != NULL;
135}
136
137int tg_firewall_client_count(void)
138{
139 return s_client_count;
140}
141
142void tg_firewall_set_mining_port(uint16_t port)
143{
144 s_mining_port = port;
145}
146
147void tg_firewall_set_sandbox_mint_access(bool enable)
148{
149 s_sandbox_mint = enable;
150}
151
152int tg_firewall_filter_packet(const uint8_t *payload, int payload_len)
153{
154 if (payload_len < 20) return -1;
155
156 uint32_t src_ip = (uint32_t)payload[12] | ((uint32_t)payload[13] << 8) |
157 ((uint32_t)payload[14] << 16) | ((uint32_t)payload[15] << 24);
158
159 uint32_t ap_subnet = s_ap_ip & 0x00FFFFFF;
160 uint32_t src_subnet = src_ip & 0x00FFFFFF;
161 if (src_subnet != ap_subnet) return 1;
162
163 if (tg_firewall_is_allowed(src_ip)) return 1;
164
165 return 0;
166}
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 @@
1#ifndef TOLLGATE_CORE_FIREWALL_H
2#define TOLLGATE_CORE_FIREWALL_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#define TG_FW_MAX_MAC_LEN 18
8
9int tg_firewall_init(uint32_t ap_ip);
10void tg_firewall_grant(uint32_t client_ip);
11void tg_firewall_revoke(uint32_t client_ip);
12int tg_firewall_revoke_all(void);
13bool tg_firewall_is_allowed(uint32_t client_ip);
14bool tg_firewall_is_mac_allowed(const char *mac);
15int tg_firewall_client_count(void);
16int tg_firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, int mac_out_size);
17void tg_firewall_set_mining_port(uint16_t port);
18void tg_firewall_set_sandbox_mint_access(bool enable);
19int tg_firewall_filter_packet(const uint8_t *payload, int payload_len);
20
21#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 @@
1#include "tollgate_mining.h"
2#include "tollgate_core.h"
3#include "tollgate_platform.h"
4#include <string.h>
5#include <math.h>
6
7static const char *TAG = "tg_mining";
8
9static tg_mining_client_stats_t s_clients[TG_MINING_MAX_CLIENTS];
10static int s_client_count = 0;
11static double s_current_hashprice = 0.0;
12static uint32_t s_current_nbits = 0;
13static uint64_t s_current_difficulty = 1;
14
15static int64_t get_time_ms(void)
16{
17 const tollgate_platform_t *p = tollgate_core_get_platform();
18 if (p && p->get_time_ms) return p->get_time_ms();
19 return 0;
20}
21
22uint64_t tg_mining_nbits_to_difficulty(uint32_t nbits)
23{
24 if (nbits == 0) return (uint64_t)-1;
25
26 uint32_t exponent = (nbits >> 24) & 0xFF;
27 uint32_t mantissa = nbits & 0x007FFFFF;
28
29 if (exponent <= 3) {
30 mantissa >>= (8 * (3 - exponent));
31 if (mantissa == 0) return (uint64_t)-1;
32 return 0x00000000FFFF0000ULL / mantissa;
33 }
34
35 uint64_t target = (uint64_t)mantissa << (8 * (exponent - 3));
36 if (target == 0) return (uint64_t)-1;
37
38 uint64_t pdiff = 0x00000000FFFF0000ULL;
39 uint64_t diff = pdiff / target;
40 if (diff == 0) diff = 1;
41 return diff;
42}
43
44double tg_mining_calculate_hashprice(uint32_t nbits)
45{
46 uint64_t diff = tg_mining_nbits_to_difficulty(nbits);
47 if (diff == 0 || diff == (uint64_t)-1) return 0.0;
48
49 double network_hashrate_th = (double)diff * 4294967296.0 / 1e12;
50 double daily_sats = (double)TG_MINING_BLOCK_SUBSIDY_SATS * (double)TG_MINING_BLOCKS_PER_DAY;
51 double sats_per_th_day = daily_sats / network_hashrate_th;
52 return sats_per_th_day / 1000.0;
53}
54
55double tg_mining_calculate_hashprice_override(uint64_t sats_per_ghs_day)
56{
57 return (double)sats_per_ghs_day;
58}
59
60int tg_mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len)
61{
62 (void)header80;
63 (void)nonce;
64 (void)target;
65 (void)target_len;
66 return 0;
67}
68
69uint64_t tg_mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice_sats_per_ghs_s,
70 int price_per_step, int step_size_ms)
71{
72 if (hashrate_ghs <= 0.0 || hashprice_sats_per_ghs_s <= 0.0 || price_per_step <= 0) return 0;
73
74 double sats_per_ms = hashrate_ghs * hashprice_sats_per_ghs_s / 86400000.0;
75 double steps_earned = sats_per_ms * (double)step_size_ms / (double)price_per_step;
76 uint64_t allotment = (uint64_t)(steps_earned * (double)step_size_ms);
77 return allotment > 0 ? allotment : 1;
78}
79
80uint64_t tg_mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice_sats_per_ghs_s,
81 int price_per_step, int step_size_bytes)
82{
83 if (hashrate_ghs <= 0.0 || hashprice_sats_per_ghs_s <= 0.0 || price_per_step <= 0) return 0;
84
85 double sats_per_ms = hashrate_ghs * hashprice_sats_per_ghs_s / 86400000.0;
86 double steps_earned = sats_per_ms * 1000.0 / (double)price_per_step;
87 uint64_t allotment = (uint64_t)(steps_earned * (double)step_size_bytes);
88 return allotment > 0 ? allotment : 1;
89}
90
91tg_mining_client_stats_t *tg_mining_get_or_create_client(uint32_t client_ip)
92{
93 for (int i = 0; i < s_client_count; i++) {
94 if (s_clients[i].ip == client_ip) return &s_clients[i];
95 }
96
97 if (s_client_count >= TG_MINING_MAX_CLIENTS) {
98 for (int i = 0; i < TG_MINING_MAX_CLIENTS; i++) {
99 int64_t age = get_time_ms() - s_clients[i].last_share_time_ms;
100 if (age > TG_MINING_SHARE_WINDOW_S * 2000) {
101 memset(&s_clients[i], 0, sizeof(tg_mining_client_stats_t));
102 s_clients[i].ip = client_ip;
103 s_clients[i].first_share_time_ms = get_time_ms();
104 return &s_clients[i];
105 }
106 }
107 return NULL;
108 }
109
110 tg_mining_client_stats_t *c = &s_clients[s_client_count];
111 memset(c, 0, sizeof(tg_mining_client_stats_t));
112 c->ip = client_ip;
113 c->first_share_time_ms = get_time_ms();
114 s_client_count++;
115 return c;
116}
117
118void tg_mining_update_hashrate(uint32_t client_ip, bool accepted)
119{
120 tg_mining_client_stats_t *stats = tg_mining_get_or_create_client(client_ip);
121 if (!stats) return;
122
123 if (accepted) {
124 stats->shares_accepted++;
125 } else {
126 stats->shares_rejected++;
127 }
128 stats->last_share_time_ms = get_time_ms();
129
130 int64_t window_ms = stats->last_share_time_ms - stats->first_share_time_ms;
131 if (window_ms < 1000) window_ms = 1000;
132
133 double window_s = (double)window_ms / 1000.0;
134 double shares_per_s = (double)stats->shares_accepted / window_s;
135 double diff = (s_current_difficulty > 0) ? (double)s_current_difficulty : 1.0;
136 stats->hashrate_ghs = shares_per_s * diff * 4294967296.0 / 1e9;
137}
138
139const tg_mining_client_stats_t *tg_mining_get_client_stats(uint32_t client_ip)
140{
141 for (int i = 0; i < s_client_count; i++) {
142 if (s_clients[i].ip == client_ip) return &s_clients[i];
143 }
144 return NULL;
145}
146
147double tg_mining_get_current_hashprice(void)
148{
149 return s_current_hashprice;
150}
151
152void tg_mining_set_current_nbits(uint32_t nbits)
153{
154 s_current_nbits = nbits;
155 s_current_difficulty = tg_mining_nbits_to_difficulty(nbits);
156 s_current_hashprice = tg_mining_calculate_hashprice(nbits);
157 const tollgate_platform_t *p = tollgate_core_get_platform();
158 if (p && p->log_info) p->log_info(TAG, "nbits: 0x%08lx, diff=%llu, hashprice=%.6f sat/GH/s/day",
159 (unsigned long)nbits, (unsigned long long)s_current_difficulty, s_current_hashprice);
160}
161
162void tg_mining_init(void)
163{
164 memset(s_clients, 0, sizeof(s_clients));
165 s_client_count = 0;
166 s_current_hashprice = 0.0;
167 s_current_nbits = 0;
168 s_current_difficulty = 1;
169 const tollgate_platform_t *p = tollgate_core_get_platform();
170 if (p && p->log_info) p->log_info(TAG, "Mining payment initialized");
171}
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 @@
1#ifndef TOLLGATE_CORE_MINING_H
2#define TOLLGATE_CORE_MINING_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#define TG_MINING_SHARE_WINDOW_S 30
8#define TG_MINING_BLOCK_SUBSIDY_SATS 312500000ULL
9#define TG_MINING_BLOCKS_PER_DAY 144ULL
10#define TG_MINING_MAX_CLIENTS 10
11
12typedef struct {
13 uint32_t ip;
14 uint64_t shares_accepted;
15 uint64_t shares_rejected;
16 int64_t first_share_time_ms;
17 int64_t last_share_time_ms;
18 double hashrate_ghs;
19} tg_mining_client_stats_t;
20
21uint64_t tg_mining_nbits_to_difficulty(uint32_t nbits);
22double tg_mining_calculate_hashprice(uint32_t nbits);
23double tg_mining_calculate_hashprice_override(uint64_t sats_per_ghs_day);
24int tg_mining_validate_share(const uint8_t *header80, uint32_t nonce, const uint8_t *target, int target_len);
25uint64_t tg_mining_shares_to_allotment_ms(double hashrate_ghs, double hashprice, int price, int step_ms);
26uint64_t tg_mining_shares_to_allotment_bytes(double hashrate_ghs, double hashprice, int price, int step_bytes);
27tg_mining_client_stats_t *tg_mining_get_or_create_client(uint32_t client_ip);
28void tg_mining_update_hashrate(uint32_t client_ip, bool accepted);
29const tg_mining_client_stats_t *tg_mining_get_client_stats(uint32_t client_ip);
30double tg_mining_get_current_hashprice(void);
31void tg_mining_set_current_nbits(uint32_t nbits);
32void tg_mining_init(void);
33
34#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 @@
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}
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 @@
1#ifndef TOLLGATE_CORE_SESSION_H
2#define TOLLGATE_CORE_SESSION_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#define TG_SESSION_MAX_CLIENTS 10
8#define TG_SESSION_MAX_MAC_LEN 18
9
10typedef enum {
11 TG_PAYMENT_CASHU,
12 TG_PAYMENT_MINING,
13 TG_PAYMENT_BYTES
14} tg_payment_method_t;
15
16typedef struct {
17 uint32_t client_ip;
18 char mac[TG_SESSION_MAX_MAC_LEN];
19 uint64_t allotment_ms;
20 int64_t start_time_ms;
21 uint64_t allotment_bytes;
22 uint64_t bytes_consumed;
23 tg_payment_method_t payment_method;
24 bool active;
25} tg_session_t;
26
27int tg_session_init(void);
28tg_session_t *tg_session_create(uint32_t client_ip, uint64_t allotment_ms);
29tg_session_t *tg_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes);
30void tg_session_add_bytes(uint32_t client_ip, uint64_t bytes);
31tg_session_t *tg_session_find_by_ip(uint32_t client_ip);
32tg_session_t *tg_session_find_by_mac(const char *mac);
33void tg_session_extend(tg_session_t *session, uint64_t additional_ms);
34bool tg_session_is_expired(const tg_session_t *session);
35void tg_session_revoke(tg_session_t *session);
36void tg_session_revoke_all(void);
37int tg_session_active_count(void);
38void tg_session_tick(void);
39tg_session_t *tg_session_get_array(void);
40int tg_session_get_array_size(void);
41
42#endif