From: Egor Date: Thu, 19 Oct 2023 10:56:46 +0000 (+0300) Subject: rewrite netsock X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=13cfc100b44994ffb8b61c56db8273d5d7c077ed;p=patchyircd.git rewrite netsock --- diff --git a/NetSock/netsock.c b/NetSock/netsock.c index f2dd72b..51abc5c 100644 --- a/NetSock/netsock.c +++ b/NetSock/netsock.c @@ -1,29 +1,34 @@ #include "netsock.h" -// Windows -#if defined(_WIN32) -// Variables -struct WSAData net_wd; -// Funcs -#define net_errno() (WSAGetLastError()) -const char *net_strerror(int e) { +#ifdef _WIN32 +void net_init(void) +{ + WSADATA wsaData; + int err = WSAStartup(MAKEWORD(2, 0), &wsaData); + if (err) { + fprintf(stderr, "Winsock API startup failed: %s\n", net_strerror(err)); + exit(EXIT_FAILURE); + } + fprintf(stderr, "[NetSock]: WinSock initialized V%d.%d!\n",LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion)); +} +void net_cleanup(void) +{ + WSACleanup(); +} +const char *net_strerror(int e) +{ static char msg[256]; - if (!FormatMessageA(\ - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, e, 0, msg, 256, NULL)) + if (!FormatMessageA( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, e, 0, msg, 256, NULL)) sprintf(msg, "WSA Error %d", e); return msg; } -int net_nonblock(SOCKET fd) { - u_long v = 1; - return ioctlsocket(fd, FIONBIO, &v); -} +#endif -// POSIX -#else -// Funcs -int net_nonblock(SOCKET fd) { -#if defined(O_NONBLOCK) +int net_nonblock(SOCKET fd) +{ +#if !defined(_WIN32) && defined(O_NONBLOCK) int f = fcntl(fd, F_GETFL); if (f == -1) return -1; @@ -33,62 +38,11 @@ int net_nonblock(SOCKET fd) { return ioctlsocket(fd, FIONBIO, &v); #endif } -#endif -// Funcs void net_perror(const char *p) { - if (p && *p) { + if (p && *p) fprintf(stderr, "%s: %s\n", p, net_strerror(net_errno())); - } else { - fprintf(stderr, "%s\n", net_strerror(net_errno())); - } -} - -// Init and shutdown -void net_init(void) { - fprintf(stderr, "[NetSock]: NetSock initializing...\n"); - // Initialize WSA -#if defined(_WIN32) - int WStartCode = WSAStartup(MAKEWORD(2, 0), &net_wd); - if (WStartCode) { - fprintf(stderr, "Winsock API startup failed: %s\n", net_strerror(WStartCode)); - exit(EXIT_FAILURE); - } - fprintf(stderr, "[NetSock]: WinSock initialized V%d.%d!\n",LOBYTE(net_wd.wVersion), HIBYTE(net_wd.wVersion)); -#endif -} -void net_cleanup(void) { - fprintf(stderr, "[NetSock]: NetSock cleaning up...\n"); - // Initialize WSA -#if defined(_WIN32) - WSACleanup(); - fprintf(stderr, "[NetSock]: WinSock cleaned up! (probably)\n"); -#endif -} - -// Misc -ssize_t send_log(SOCKET socket, const char *buffer, size_t length, int flags) { - fprintf(stderr, "SERVER %"PRISocket": %.*s", socket, (int)length, (char*)buffer); - if (socket != INVALID_SOCKET) - return send(socket, buffer, length, flags); else - return -1; -} -ssize_t recv_log(SOCKET socket, char *buffer, size_t length, int flags) { - ssize_t bytesRead = recv(socket,buffer,length,flags); - if(bytesRead > 0) { - fprintf(stderr, "CLIENT %"PRISocket": %.*s", socket, (int)length, (char*)buffer); - } - return bytesRead; -} -void net_sleep(unsigned int sec) { - struct timeval sleeptime; - sleeptime.tv_sec = sec; - select(0, NULL, NULL, NULL, &sleeptime); -} -void net_usleep(unsigned int usec) { - struct timeval sleeptime; - sleeptime.tv_usec = usec; - select(0, NULL, NULL, NULL, &sleeptime); + fprintf(stderr, "%s\n", net_strerror(net_errno())); } diff --git a/NetSock/netsock.h b/NetSock/netsock.h index 28a493b..78c7fb7 100644 --- a/NetSock/netsock.h +++ b/NetSock/netsock.h @@ -1,39 +1,66 @@ -#ifndef _netsock_h -#define _netsock_h +#ifndef NETSOCK_H_ +#define NETSOCK_H_ + +/* API comparision table + * | POSIX | Win32 | NetSock | + * |--------------------|-------------------|------------------| + * | int | SOCKET | SOCKET | + * | "%d" | (complicated) | "%"PRISocket | + * | -1 | INVALID_SOCKET | INVALID_SOCKET | + * | errno | WSAGetLastError() | net_errno() | + * | strerror | (complicated) | net_strerror() | + * | perror | (complicated) | net_perror() | + * | EAGAIN | WSAEWOULDBLOCK | net_wouldblock() | + * | (none) | WSAStartup() | net_init() | + * | (none) | WSACleanup() | net_cleanup() | + * | close() | closesocket() | closesocket() | + * | ioctl() | ioctlsocket() | ioctlsocket() | + * | SHUT_RD | SD_RECEIVE | SHUT_RD | + * | SHUT_WR | SD_SEND | SHUT_WR | + * | SHUT_RDWR | SD_BOTH | SHUT_RDWR | + * | O_NONBLOCK/FIONBIO | FIONBIO | net_nonblock() | + * | read/recv | recv | recv | + * | write/send | send | send | + */ + +/* All implementations besides glibc and 4.4BSD allow redefining this constant. + * Win32 has 64 as default, Linux has 1024. Increase the limit on Win32. */ #define FD_SETSIZE 1024 -#if _WIN32 + +/* Set target Win32 version to Vista. + * In particular, inet_ntop and AI_NUMERICSERV require this. */ +#ifdef _WIN32 #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #endif -// Standard headers +/* Standard headers */ #include #include #include #include -// Windows -#if defined(_WIN32) +#ifdef _WIN32 +/* Win32 */ +/* Include winsock2.h first, because windows.h defauts to Winsock 1 */ #include #include #include #ifdef _WIN64 -#define PRISocket "llu" // The format string for SOCKET +#define PRISocket "llu" #else -#define PRISocket "u" // The format string for SOCKET +#define PRISocket "u" #endif #define net_errno() (WSAGetLastError()) -#define net_wouldblock() (WSAGetLastError() == WSAEWOULDBLOCK) -// Funcs const char *net_strerror(int e); -// Variables -extern struct WSAData net_wd; +#define net_wouldblock() (WSAGetLastError() == WSAEWOULDBLOCK) +void net_init(void); +void net_cleanup(void); #define SHUT_RD SD_RECEIVE #define SHUT_WR SD_SEND #define SHUT_RDWR SD_BOTH - -// POSIX #else +/* POSIX */ #include #include #include @@ -42,27 +69,19 @@ extern struct WSAData net_wd; #include #include #include -//typedef unsigned long long SOCKET; +#include typedef int SOCKET; -#define PRISocket "d" // The format string for SOCKET +#define PRISocket "d" +#define INVALID_SOCKET (-1) #define net_errno() ((int)errno) #define net_strerror(e) strerror((e)) #define net_wouldblock() (errno == EAGAIN || errno == EWOULDBLOCK) +#define net_init() +#define net_cleanup() #define closesocket close #define ioctlsocket ioctl -#define INVALID_SOCKET -1 -#define SD_RECEIVE SHUT_RD -#define SD_SEND SHUT_WR -#define SD_BOTH SHUT_RDWR #endif -// Funcs -int net_nonblock(SOCKET fd); void net_perror(const char *p); -void net_init(void); -void net_cleanup(void); -ssize_t send_log(SOCKET socket, const char *buffer, size_t length, int flags); -ssize_t recv_log(SOCKET socket, char *buffer, size_t length, int flags); -void net_sleep(unsigned int sec); -void net_usleep(unsigned int usec); +int net_nonblock(SOCKET fd); #endif diff --git a/common.h b/common.h index 8ece287..accd4c1 100644 --- a/common.h +++ b/common.h @@ -97,9 +97,4 @@ extern struct sockaddr_in6 serverAddrV6; extern size_t serverAddrV4len; extern size_t serverAddrV6len; -#ifndef _MSC_VER -#pragma GCC poison recv_log -#pragma GCC poison send_log -#endif - #endif diff --git a/main.c b/main.c index 8f555a6..c0793ed 100644 --- a/main.c +++ b/main.c @@ -162,7 +162,7 @@ void accept_from_sock(struct listener *listener) if (listener->ctx) { #ifdef HAS_TLS if (tls_accept_socket(listener->ctx, &cctx, newSocket) == -1) { - close(newSocket); + closesocket(newSocket); fprintf(stderr, "tls_accept_socket: %s\n", tls_error(listener->ctx)); continue; } diff --git a/net.c b/net.c index e5c1788..906bfc8 100644 --- a/net.c +++ b/net.c @@ -1,9 +1,6 @@ #include "net.h" #include "irc.h" #include "ircfmt.h" -#ifndef _WIN32 -#include -#endif struct client_data *allocClient() {