You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.3 KiB
89 lines
2.3 KiB
// stcp.h — Streaming TCP: shared structures, constants, connection lifecycle |
|
#ifndef STCP_H |
|
#define STCP_H |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
|
|
#include "secure_channel.h" |
|
#include "crc32.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/socket_compat.h" |
|
#include "../lib/ll_queue.h" |
|
#include "../lib/debug_config.h" |
|
#include <stdint.h> |
|
|
|
#define STCP_MAX_MSG_SIZE 65535 |
|
#define STCP_RECV_BUF_INIT 8192 |
|
#define STCP_RECV_BUF_MAX 131072 |
|
#define STCP_HS_TIMEOUT 5000 |
|
#define STCP_CONNECT_TIMEOUT 10000 |
|
|
|
#define STCP_HS_CLIENT_MIN (SC_PUBKEY_ENC_SIZE + 6) // 72 + 2(padding_size enc) + 4(crc) = 78 |
|
#define STCP_HS_SERVER_MIN (SC_PUBKEY_ENC_SIZE + 7) // 72 + 1(status) + 2(padding_size) + 4(crc) = 79 |
|
#define STCP_HS_ENC_CLIENT 6 // padding_size(2) + CRC32(4) |
|
#define STCP_HS_ENC_SERVER 7 // status(1) + padding_size(2) + CRC32(4) |
|
|
|
#define STCP_STREAM_CLIENT_SEND 0 |
|
#define STCP_STREAM_SERVER_SEND 1 |
|
|
|
enum stcp_state { |
|
STCP_STATE_INIT, |
|
STCP_STATE_HS_CLIENT_SENT, |
|
STCP_STATE_HS_SERVER_WAIT, |
|
STCP_STATE_DATA, |
|
STCP_STATE_CLOSED, |
|
STCP_STATE_ERROR |
|
}; |
|
|
|
struct stcp_conn { |
|
socket_t sock; |
|
struct UASYNC *ua; |
|
void *socket_id; |
|
|
|
enum stcp_state state; |
|
uint8_t is_server; |
|
uint8_t allocated; // 1 = allocated by u_calloc, free in stcp_conn_free |
|
|
|
uint8_t session_key[SC_SESSION_KEY_SIZE]; |
|
struct sc_stream_state stream_send; |
|
struct sc_stream_state stream_recv; |
|
|
|
struct SC_MYKEYS my_keys; |
|
uint8_t peer_pubkey[SC_PUBKEY_SIZE]; |
|
uint8_t peer_pubkey_set; |
|
|
|
struct ll_queue *rx_queue; |
|
struct ll_queue *tx_queue; |
|
void (*tx_cb)(struct ll_queue *q, void *arg); |
|
|
|
uint8_t *recv_buf; |
|
size_t recv_buf_len; |
|
size_t recv_buf_cap; |
|
|
|
uint8_t *send_buf; |
|
size_t send_len; |
|
size_t send_offset; |
|
|
|
size_t hs_expected_len; |
|
uint8_t hs_key_processed; |
|
|
|
void (*on_ready)(struct stcp_conn *conn, void *arg); |
|
void *ready_arg; |
|
|
|
void (*on_close)(struct stcp_conn *conn, int err, void *arg); |
|
void *close_arg; |
|
}; |
|
|
|
void stcp_conn_free(struct stcp_conn *c); |
|
void stcp_conn_set_tx_queue(struct stcp_conn *c, struct ll_queue *q); |
|
void stcp_conn_set_rx_queue(struct stcp_conn *c, struct ll_queue *q); |
|
void stcp_conn_set_on_close(struct stcp_conn *c, void (*cb)(struct stcp_conn *conn, int err, void *arg), void *arg); |
|
|
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
#endif
|
|
|