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.
 
 
 
 
 
 

191 lines
7.2 KiB

/*
* etcpmon_protocol.h - ETCP Monitor Protocol Definitions
*
* Binary protocol for monitoring ETCP connections
* Format: [2 bytes: msg_size] [1 byte: msg_type] [payload...]
*
* Period: 100ms (10 updates per second)
*/
#ifndef ETCPMON_PROTOCOL_H
#define ETCPMON_PROTOCOL_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================
* Protocol Constants
* ============================================================================ */
#define ETCPMON_PROTOCOL_VERSION 1
#define ETCPMON_MAX_MSG_SIZE 4096
#define ETCPMON_MAX_CONN_NAME 32
#define ETCPMON_MAX_CONNECTIONS 256
#define ETCPMON_MAX_LINKS 16
/* Update interval in milliseconds */
#define ETCPMON_UPDATE_INTERVAL_MS 100
/* ============================================================================
* Message Types
* ============================================================================ */
/* Client -> Server commands */
#define ETCPMON_CMD_LIST_CONN 0x01 /* Request connection list */
#define ETCPMON_CMD_SELECT_CONN 0x02 /* Select connection by peer_node_id */
#define ETCPMON_CMD_GET_METRICS 0x03 /* Request metrics for selected */
#define ETCPMON_CMD_DISCONNECT 0x04 /* Disconnect from monitoring */
/* Server -> Client responses */
#define ETCPMON_RSP_CONN_LIST 0x81 /* Connection list response */
#define ETCPMON_RSP_METRICS 0x82 /* Metrics response */
#define ETCPMON_RSP_ERROR 0xFF /* Error response */
/* Error codes for RSP_ERROR */
#define ETCPMON_ERR_INVALID_CMD 0x01 /* Unknown command */
#define ETCPMON_ERR_INVALID_CONN 0x02 /* Invalid connection ID */
#define ETCPMON_ERR_NO_CONN_SELECTED 0x03 /* No connection selected */
#define ETCPMON_ERR_SERVER_BUSY 0x04 /* Server too busy */
/* ============================================================================
* Message Header
* ============================================================================ */
#pragma pack(push, 1)
struct etcpmon_msg_header {
uint16_t size; /* Total message size including header */
uint8_t type; /* Message type (command or response) */
};
/* ============================================================================
* Commands (Client -> Server)
* ============================================================================ */
/* CMD_LIST_CONN: No payload required */
/* CMD_SELECT_CONN: Select connection to monitor */
struct etcpmon_cmd_select {
uint64_t peer_node_id; /* peer_node_id of connection to monitor */
};
/* CMD_GET_METRICS: No payload required (uses previously selected connection) */
/* CMD_DISCONNECT: No payload required */
/* ============================================================================
* Responses (Server -> Client)
* ============================================================================ */
/* RSP_CONN_LIST: List of active connections */
struct etcpmon_conn_info {
uint64_t peer_node_id; /* Connection identifier */
char name[ETCPMON_MAX_CONN_NAME]; /* Null-terminated name */
};
struct etcpmon_rsp_conn_list {
uint8_t count; /* Number of connections */
/* Followed by count * struct etcpmon_conn_info */
};
/* RSP_METRICS: Full metrics for ETCP connection and its links */
/* ETCP Connection metrics */
struct etcpmon_etcp_metrics {
uint32_t rtt_last; /* Last RTT (0.1ms units) */
uint32_t rtt_avg_10; /* Average RTT over 10 packets */
uint32_t rtt_avg_100; /* Average RTT over 100 packets */
uint32_t jitter; /* Jitter (max-min RTT) */
uint64_t bytes_sent_total; /* Total bytes sent */
uint32_t retrans_count; /* Retransmission count */
uint32_t ack_count; /* ACK packets received */
uint32_t unacked_bytes; /* Current unacknowledged bytes */
uint32_t optimal_inflight; /* Target inflight window */
uint8_t links_count; /* Number of active links */
};
/* Link metrics */
struct etcpmon_link_metrics {
uint8_t local_link_id; /* Local link identifier */
uint8_t status; /* 1 = up, 0 = down */
uint32_t encrypt_errors; /* Encryption errors */
uint32_t decrypt_errors; /* Decryption errors */
uint32_t send_errors; /* Send errors */
uint32_t recv_errors; /* Receive errors */
uint64_t total_encrypted; /* Total bytes encrypted/sent */
uint64_t total_decrypted; /* Total bytes decrypted/received */
uint32_t bandwidth; /* Configured bandwidth (Kbps) */
uint32_t nat_changes_count; /* NAT address changes */
};
/* TUN interface metrics */
struct etcpmon_tun_metrics {
uint64_t bytes_read; /* Bytes read from TUN */
uint64_t bytes_written; /* Bytes written to TUN */
uint32_t packets_read; /* Packets read from TUN */
uint32_t packets_written; /* Packets written to TUN */
uint32_t read_errors; /* Read error count */
uint32_t write_errors; /* Write error count */
};
struct etcpmon_rsp_metrics {
struct etcpmon_etcp_metrics etcp; /* ETCP connection metrics */
struct etcpmon_tun_metrics tun; /* TUN interface metrics */
/* Followed by etcp.links_count * struct etcpmon_link_metrics */
};
/* RSP_ERROR: Error response */
struct etcpmon_rsp_error {
uint8_t error_code; /* Error code */
/* Followed by null-terminated error message string */
};
#pragma pack(pop)
/* ============================================================================
* Helper Macros
* ============================================================================ */
/* Calculate size of connection list response */
#define ETCPMON_CONN_LIST_SIZE(count) \
(sizeof(struct etcpmon_msg_header) + sizeof(struct etcpmon_rsp_conn_list) + \
(count) * sizeof(struct etcpmon_conn_info))
/* Calculate size of metrics response */
#define ETCPMON_METRICS_SIZE(links_count) \
(sizeof(struct etcpmon_msg_header) + sizeof(struct etcpmon_rsp_metrics) + \
(links_count) * sizeof(struct etcpmon_link_metrics))
/* Calculate size of error response */
#define ETCPMON_ERROR_SIZE(msg_len) \
(sizeof(struct etcpmon_msg_header) + sizeof(struct etcpmon_rsp_error) + (msg_len) + 1)
/* ============================================================================
* Utility Functions (optional, for both client and server)
* ============================================================================ */
/* Build message header */
static inline void etcpmon_build_header(struct etcpmon_msg_header* hdr,
uint16_t payload_size,
uint8_t msg_type) {
hdr->size = sizeof(struct etcpmon_msg_header) + payload_size;
hdr->type = msg_type;
}
/* Validate message header */
static inline int etcpmon_validate_header(const struct etcpmon_msg_header* hdr) {
if (hdr->size < sizeof(struct etcpmon_msg_header) ||
hdr->size > ETCPMON_MAX_MSG_SIZE) {
return -1;
}
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* ETCPMON_PROTOCOL_H */