Files
utun2/lib/socket_compat.h
T
evgeny 38c9f229bc stcp: ed25519 pubkey exchange in handshake, fix instance destroy segfault, fix chat_tcp_test
- u_async.c: process immediate_queue BEFORE epoll_wait in uasync_poll
- stcp_link.c: defensive NULL etcp_conn/etcp_link in stcp_link_close
- utun_instance.c: move stcp_server_list_destroy_all to Phase J (before ETCP),
  add deferred drain in both Phase J and L
- stcp.h/c: STCP_HS_ENC_CLIENT 6→38, STCP_HS_ENC_SERVER 7→39,
  exchange ed25519_pubkey in both handshake directions
- etcp_connections.c: copy peer_ed25519 from STCP to ETCP_CONN in
  etcp_link_enter_ready_tcp; guard link->etcp in tcp_link_close_cb
- chat_sync.c: fix lk->conn NULL dereference for TCP links
- test_stcp.c: update stcp_server_create/stcp_client_connect callsites
- tools/chat_tcp_test: change transport udp→tcp
2026-08-07 00:48:27 +03:00

86 lines
2.0 KiB
C

/**
* Socket compatibility layer for cross-platform support (POSIX / Windows)
* MSYS2 UCRT64 compatible
*/
#ifndef SOCKET_COMPAT_H
#define SOCKET_COMPAT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef SOCKET socket_t;
#define SOCKET_INVALID INVALID_SOCKET
#define SOCKET_ERROR_CODE SOCKET_ERROR
// Error codes
#define ERR_WOULDBLOCK WSAEWOULDBLOCK
#define ERR_AGAIN WSAEWOULDBLOCK
#define ERR_INTR WSAEINTR
#else
// POSIX systems
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
typedef int socket_t;
#define SOCKET_INVALID (-1)
#define SOCKET_ERROR_CODE (-1)
#define ERR_WOULDBLOCK EWOULDBLOCK
#define ERR_AGAIN EAGAIN
#define ERR_INTR EINTR
#endif
// Platform initialization/cleanup
int socket_platform_init(void);
void socket_platform_cleanup(void);
// Socket operations
socket_t socket_create_udp(int family);
int socket_set_nonblocking(socket_t sock);
int socket_close_wrapper(socket_t sock);
static inline int socket_get_error(void) {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
// Socket options
int socket_set_buffers(socket_t sock, int sndbuf, int rcvbuf);
int socket_set_reuseaddr(socket_t sock, int reuse);
int socket_bind_to_device(socket_t sock, const char* ifname);
int socket_set_mark(socket_t sock, int mark);
// I/O operations
ssize_t socket_sendto(socket_t sock, const void* buf, size_t len,
const struct sockaddr* dest, socklen_t dest_len);
ssize_t socket_recvfrom(socket_t sock, void* buf, size_t len,
struct sockaddr* src, socklen_t* src_len);
// Utility
const char* socket_strerror(int err);
// взять порт из sockaddr_storage
uint16_t ss_get_port(const struct sockaddr_storage *addr);
#ifdef __cplusplus
}
#endif
#endif // SOCKET_COMPAT_H