Reclaim outbound slots on peer disconnect: - TcpClient_ThreadProc fired on_disconnect but never cleared the outbound slot, closed the fd, joined the io thread, or freed the connection, so a dead peer permanently held its outboundClients[] slot. After MAX_CONS (32) churned connections the node could make no new outbound connections, and leaked fds/threads/memory. (The inbound side already self-reclaimed.) - Add a reaper (Node_ReapDeadOutbound) on the maintenance thread: under outboundLock it detaches dead (disconnect-notified) slots, then joins the io thread and destroys/frees each connection outside the lock. - Guard against use-after-free with a pin count on tcp_connection_t (TcpConnection_Pin/Unpin). The only cross-thread consumer holding a raw connection pointer across a blocking op is the `sync` command (via Node_GetBestOutboundPeer); it now pins the peer and unpins when done, and the reaper skips pinned connections. Discovery's snapshots run on the reaper's own thread, so they need no pin. - Node_GetBestOutboundPeer/GetClientList/GetPeerEndpoints skip disconnect-notified connections so a dead peer is never handed out. - Node_Destroy stops+joins the maintenance thread before tearing down outbound clients, so the reaper can't race shutdown. Strike disconnected peers from the discovery peer list: - Add NodeDiscovery_RemovePeer + Node_HandlePeerDisconnect: on disconnect, remove the peer from the known-peer table once no live connection (inbound or outbound) to its listen endpoint remains (Node_HasLiveConnectionTo; disconnect-notified conns don't count, so both directions dropping at once is handled). Wired into Node_Server_OnDisconnect and Node_Client_OnDisconnect.
90 lines
3.1 KiB
C
90 lines
3.1 KiB
C
#ifndef TCPCONNECTION_H
|
|
#define TCPCONNECTION_H
|
|
|
|
#include <arpa/inet.h>
|
|
#include <pthread.h>
|
|
#include <stdatomic.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <sys/socket.h>
|
|
|
|
#define TCP_IO_BUFFER_SIZE 1500
|
|
#define TCP_FRAME_HEADER_SIZE 4U
|
|
#define TCP_MAX_FRAME_PAYLOAD (1024U * 1024U)
|
|
|
|
typedef enum {
|
|
TCP_CONNECTION_ROLE_INBOUND = 0,
|
|
TCP_CONNECTION_ROLE_OUTBOUND = 1
|
|
} tcp_connection_role_t;
|
|
|
|
typedef struct tcp_connection_t tcp_connection_t;
|
|
|
|
struct tcp_connection_t {
|
|
int sockFd;
|
|
sa_family_t addrFamily;
|
|
struct sockaddr_storage peerAddr;
|
|
uint32_t connectionId;
|
|
tcp_connection_role_t role;
|
|
|
|
// Peer's advertised TCP/UDP listen port (learned from HELLO/ACK_HELLO). 0 until known.
|
|
// For OUTBOUND connections the peerAddr port already is the listen port; this matters for INBOUND peers.
|
|
uint16_t peerListenPort;
|
|
|
|
pthread_t ioThread;
|
|
pthread_mutex_t sendLock;
|
|
pthread_mutex_t stateLock;
|
|
|
|
bool closing;
|
|
bool disconnectedNotified;
|
|
|
|
// Non-zero while another thread holds a raw pointer to this connection taken from a
|
|
// lock-protected snapshot and used after releasing the lock. The reaper must not free a
|
|
// pinned connection. See TcpConnection_Pin/Unpin.
|
|
atomic_int pinCount;
|
|
|
|
unsigned char* dataBuf;
|
|
size_t dataBufLen;
|
|
size_t dataBufCap;
|
|
|
|
unsigned char headerBuf[TCP_FRAME_HEADER_SIZE];
|
|
size_t headerBytesRead;
|
|
uint32_t expectedPayloadLen;
|
|
unsigned char* frameBuf;
|
|
size_t frameBytesRead;
|
|
|
|
void (*on_data)(tcp_connection_t* conn);
|
|
void (*on_disconnect)(tcp_connection_t* conn);
|
|
void* owner;
|
|
};
|
|
|
|
int TcpConnection_Init(tcp_connection_t* conn, int sockFd, const struct sockaddr_storage* peerAddr, tcp_connection_role_t role);
|
|
void TcpConnection_Destroy(tcp_connection_t* conn);
|
|
|
|
int TcpConnection_SetDataBuffer(tcp_connection_t* conn, const unsigned char* data, size_t len);
|
|
|
|
void TcpConnection_ResetFramingState(tcp_connection_t* conn);
|
|
int TcpConnection_FeedFramedData(tcp_connection_t* conn, const unsigned char* input, size_t inputLen);
|
|
|
|
// Returns the peer's canonical IP string (strips ::ffff: IPv4-mapped prefix).
|
|
// Writes at most bufLen bytes to buf. Returns buf on success, NULL on failure.
|
|
const char* TcpConnection_GetPeerAddrStr(const tcp_connection_t* conn, char* buf, size_t bufLen);
|
|
|
|
// Returns non-zero if both connections have the same peer IP address.
|
|
// Handles AF_INET vs AF_INET6 mismatches via IPv4-mapped normalisation.
|
|
int TcpConnection_PeerAddrEqual(const tcp_connection_t* a, const tcp_connection_t* b);
|
|
|
|
int TcpConnection_SendRaw(int sockFd, const void* data, size_t len);
|
|
int TcpConnection_SendFramed(tcp_connection_t* conn, const void* payload, size_t payloadLen);
|
|
|
|
void TcpConnection_RequestClose(tcp_connection_t* conn);
|
|
void TcpConnection_MarkDisconnectNotified(tcp_connection_t* conn);
|
|
bool TcpConnection_IsDisconnectNotified(tcp_connection_t* conn);
|
|
|
|
// Pin/unpin a connection so a background reaper won't free it while a caller still holds a raw
|
|
// pointer to it (e.g. across a blocking operation after releasing the collection lock).
|
|
void TcpConnection_Pin(tcp_connection_t* conn);
|
|
void TcpConnection_Unpin(tcp_connection_t* conn);
|
|
|
|
#endif
|