- secure_channel: add sc_stream_state + sc_stream_init/sc_stream_xor/sc_stream_cleanup
AES-128-CTR streaming cipher for incremental encrypt/decrypt
- stcp.h/c: shared stcp_conn lifecycle, set_on_close, rx/tx queue setup
- stcp_server: listen/accept, handshake (obfuscated pubkey + ECDH + stream), DATA phase
- stcp_client: connect, handshake, DATA phase
- Protocol: key_block(72) + enc(data+crc) + padding for handshake
size(2 LE) + enc(data+crc) for data messages
- Tests: test_stream_cipher (AES-CTR correctness)
test_stcp (6 cases: sizes 0..65535, 200 msgs, wrong key,
close detection, multi-client, interleaved send/recv)
- Fix: 0-length message handling in tx_queue callbacks
36 lines
1.1 KiB
C
36 lines
1.1 KiB
C
// stcp.c — shared stcp_conn lifecycle
|
|
#include "stcp.h"
|
|
#include "../lib/ll_queue.h"
|
|
#include "../lib/mem.h"
|
|
#include "../lib/debug_config.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void stcp_conn_set_tx_queue(struct stcp_conn *c, struct ll_queue *q) {
|
|
if (!c || !q) return;
|
|
c->tx_queue = q;
|
|
if (c->tx_cb) queue_set_callback(q, c->tx_cb, c);
|
|
}
|
|
|
|
void stcp_conn_set_rx_queue(struct stcp_conn *c, struct ll_queue *q) {
|
|
if (!c || !q) return;
|
|
c->rx_queue = q;
|
|
}
|
|
|
|
void stcp_conn_set_on_close(struct stcp_conn *c, void (*cb)(struct stcp_conn *conn, int err, void *arg), void *arg) {
|
|
if (!c || !cb) return;
|
|
c->on_close = cb;
|
|
c->close_arg = arg;
|
|
}
|
|
|
|
void stcp_conn_free(struct stcp_conn *c) {
|
|
if (!c) return;
|
|
if (c->socket_id) { uasync_remove_socket_t(c->ua, c->sock); c->socket_id = NULL; }
|
|
if (c->sock != SOCKET_INVALID) { socket_close_wrapper(c->sock); c->sock = SOCKET_INVALID; }
|
|
if (c->recv_buf) { u_free(c->recv_buf); c->recv_buf = NULL; }
|
|
if (c->send_buf) { u_free(c->send_buf); c->send_buf = NULL; }
|
|
sc_stream_cleanup(&c->stream_send);
|
|
sc_stream_cleanup(&c->stream_recv);
|
|
if (c->allocated) u_free(c);
|
|
}
|