TCP Node boilerplate; CLI interface

This commit is contained in:
2026-04-23 16:24:26 +02:00
parent d631eb190d
commit 9c99eec3a8
13 changed files with 1635 additions and 578 deletions

View File

@@ -26,7 +26,7 @@ void Chain_Wipe(blockchain_t* chain);
// I/O
bool Chain_SaveToFile(blockchain_t* chain, const char* dirpath, uint256_t currentSupply, uint64_t currentReward);
bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* outCurrentSupply, uint32_t* outDifficultyTarget, uint64_t* outCurrentReward, uint8_t* outLastSavedHash);
bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* outCurrentSupply, uint32_t* outDifficultyTarget, uint64_t* outCurrentReward, uint8_t* outLastSavedHash, bool loadTransactions);
// Difficulty
uint32_t Chain_ComputeNextTarget(blockchain_t* chain, uint32_t currentTarget);

View File

@@ -4,26 +4,47 @@
#ifndef _WIN32
// POSIX
#include <tcpd/tcpconnection.h>
#include <tcpd/tcpclient.h>
#include <tcpd/tcpserver.h>
#endif
#include <dynarr.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <constants.h>
#include <packettype.h>
#include <stddef.h>
typedef struct {
tcp_server_t* server;
// TODO: Add the list of clients as well
tcp_client_t outboundClients[MAX_CONS];
size_t outboundCount;
void (*on_connect)(tcp_connection_t* conn, void* user);
void (*on_data)(tcp_connection_t* conn, const unsigned char* data, size_t len, void* user);
void (*on_disconnect)(tcp_connection_t* conn, void* user);
void* callbackUser;
} net_node_t;
net_node_t* Node_Create();
void Node_Destroy(net_node_t* node);
void Node_SetCallbacks(
net_node_t* node,
void (*on_connect)(tcp_connection_t* conn, void* user),
void (*on_data)(tcp_connection_t* conn, const unsigned char* data, size_t len, void* user),
void (*on_disconnect)(tcp_connection_t* conn, void* user),
void* user
);
int Node_ConnectPeer(net_node_t* node, const char* ip, unsigned short port);
int Node_ConnectStartupPeers(net_node_t* node, const char** ips, const unsigned short* ports, size_t peersCount);
int Node_SendPacket(net_node_t* node, tcp_connection_t* conn, packet_type_t packetType, const void* payload, size_t payloadLen);
// Callback logic
void Node_Server_OnConnect(tcp_connection_t* client);
void Node_Server_OnData(tcp_connection_t* client);
void Node_Server_OnDisconnect(tcp_connection_t* client);
void Node_Client_OnConnect(tcp_connection_t* client);
void Node_Client_OnData(tcp_connection_t* client);
void Node_Client_OnDisconnect(tcp_connection_t* client);
#endif

View File

@@ -1,9 +1,17 @@
#ifndef PACKETTYPE_H
#define PACKETTYPE_H
#include <stdint.h>
typedef enum {
PACKET_TYPE_NONE = 0,
// Add more here
} PacketType;
PACKET_TYPE_REQUEST = 1,
PACKET_TYPE_RESPONSE = 2,
PACKET_TYPE_MAX = 3
} packet_type_t;
static inline int PacketType_IsValid(uint8_t packetType) {
return packetType > PACKET_TYPE_NONE && packetType < PACKET_TYPE_MAX;
}
#endif

33
include/tcpd/tcpclient.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <arpa/inet.h>
#include <stddef.h>
#include <tcpd/tcpconnection.h>
typedef struct {
tcp_connection_t* connection;
void (*on_connect)(tcp_connection_t* conn);
void (*on_data)(tcp_connection_t* conn);
void (*on_disconnect)(tcp_connection_t* conn);
void* owner;
} tcp_client_t;
int TcpClient_Init(tcp_client_t* client);
void TcpClient_Destroy(tcp_client_t* client);
int TcpClient_Connect(
tcp_client_t* client,
const char* peerIp,
unsigned short peerPort,
void (*on_connect)(tcp_connection_t* conn),
void (*on_data)(tcp_connection_t* conn),
void (*on_disconnect)(tcp_connection_t* conn),
void* owner
);
int TcpClient_Send(tcp_client_t* client, const void* data, size_t len);
void TcpClient_Disconnect(tcp_client_t* client);
#endif

View File

@@ -1,29 +1,64 @@
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <dynarr.h>
#define MTU 1500
#define TCP_IO_BUFFER_SIZE 1500
#define TCP_FRAME_HEADER_SIZE 4U
#define TCP_MAX_FRAME_PAYLOAD (1024U * 1024U)
struct tcp_connection_t {
int clientFd;
struct sockaddr_in clientAddr;
uint32_t clientId;
unsigned char dataBuf[MTU];
ssize_t dataBufLen;
void (*on_data)(struct tcp_connection_t* client);
void (*on_disconnect)(struct tcp_connection_t* client);
pthread_t clientThread;
};
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;
struct sockaddr_in peerAddr;
uint32_t connectionId;
tcp_connection_role_t role;
pthread_t ioThread;
pthread_mutex_t sendLock;
pthread_mutex_t stateLock;
bool closing;
bool disconnectedNotified;
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_in* 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);
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);
#endif

View File

@@ -1,23 +1,18 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <stddef.h>
#include <tcpd/tcpconnection.h>
#include <numgen.h>
#include <dynarr.h>
typedef struct {
int sockFd;
struct sockaddr_in addr;
int opt;
int isRunning;
void* owner;
// Called before the client thread runs
void (*on_connect)(tcp_connection_t* client);
@@ -27,8 +22,9 @@ typedef struct {
void (*on_disconnect)(tcp_connection_t* client);
// max clients
size_t clients;
size_t maxClients;
tcp_connection_t** clientsArrPtr;
pthread_mutex_t clientsMutex;
pthread_t svrThread;
} tcp_server_t;
@@ -46,8 +42,8 @@ void TcpServer_Destroy(tcp_server_t* ptr);
void TcpServer_Init(tcp_server_t* ptr, unsigned short port, const char* addr);
void TcpServer_Start(tcp_server_t* ptr, int maxcons);
void TcpServer_Stop(tcp_server_t* ptr);
void TcpServer_Send(tcp_server_t* ptr, tcp_connection_t* cli, void* data, size_t len);
void Generic_SendSocket(int sock, void* data, size_t len);
int TcpServer_Send(tcp_server_t* ptr, tcp_connection_t* cli, const void* data, size_t len);
void Generic_SendSocket(int sock, const void* data, size_t len);
void TcpServer_Disconnect(tcp_server_t* ptr, tcp_connection_t* cli);
void TcpServer_KillClient(tcp_server_t* ptr, tcp_connection_t* cli);