|
| 1 | +#include "src/localserv/fildesh_compat_socket.h" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#ifndef _MSC_VER |
| 6 | +#include <arpa/inet.h> |
| 7 | +#include <errno.h> |
| 8 | +#include <sys/time.h> |
| 9 | +#include <sys/types.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <unistd.h> |
| 12 | +#else |
| 13 | +/* winsock2.h included via header */ |
| 14 | +typedef int socklen_t; |
| 15 | +#endif |
| 16 | + |
| 17 | +#include <fildesh/fildesh_compat_errno.h> |
| 18 | + |
| 19 | +int fildesh_compat_socket_init(void) { |
| 20 | +#ifdef _MSC_VER |
| 21 | + WSADATA wsa_data; |
| 22 | + if (0 != WSAStartup(MAKEWORD(2,2), &wsa_data)) { |
| 23 | + fildesh_compat_errno_trace(); |
| 24 | + return -1; |
| 25 | + } |
| 26 | +#endif |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +void fildesh_compat_socket_cleanup(void) { |
| 31 | +#ifdef _MSC_VER |
| 32 | + WSACleanup(); |
| 33 | +#endif |
| 34 | +} |
| 35 | + |
| 36 | +int fildesh_compat_socket_ok(FildeshCompat_socket fd) { |
| 37 | +#ifndef _MSC_VER |
| 38 | + return fd > 0; |
| 39 | +#else |
| 40 | + return fd != INVALID_SOCKET; |
| 41 | +#endif |
| 42 | +} |
| 43 | + |
| 44 | +void fildesh_compat_socket_close(FildeshCompat_socket fd) { |
| 45 | + if (!fildesh_compat_socket_ok(fd)) {return;} |
| 46 | +#ifndef _MSC_VER |
| 47 | + shutdown(fd, SHUT_RDWR); |
| 48 | + close(fd); |
| 49 | +#else |
| 50 | + shutdown(fd, SD_BOTH); |
| 51 | + closesocket(fd); |
| 52 | +#endif |
| 53 | +} |
| 54 | + |
| 55 | +void fildesh_compat_socket_set_timeout(FildeshCompat_socket fd, int seconds) { |
| 56 | +#ifdef _MSC_VER |
| 57 | + DWORD timeout = seconds * 1000; |
| 58 | + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)); |
| 59 | +#else |
| 60 | + struct timeval tv; |
| 61 | + tv.tv_sec = seconds; |
| 62 | + tv.tv_usec = 0; |
| 63 | + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); |
| 64 | +#endif |
| 65 | +} |
| 66 | + |
| 67 | +FildeshCompat_socket |
| 68 | +fildesh_compat_socket_setup_server(const char* hostname, int* port) |
| 69 | +{ |
| 70 | + /* Create a socket. */ |
| 71 | + FildeshCompat_socket sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 72 | + int yes = 1; |
| 73 | + struct sockaddr_in addr; |
| 74 | + |
| 75 | + (void)hostname; |
| 76 | + |
| 77 | + if (!fildesh_compat_socket_ok(sockfd)) { |
| 78 | + fildesh_compat_errno_trace(); |
| 79 | + return FILDESH_COMPAT_SOCKET_INVALID; |
| 80 | + } |
| 81 | + |
| 82 | + if (0 != setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(yes))) { |
| 83 | + fildesh_compat_errno_trace(); |
| 84 | + } |
| 85 | + |
| 86 | + /* Bind the socket to a port. */ |
| 87 | + addr.sin_family = AF_INET; |
| 88 | + addr.sin_port = htons(*port); |
| 89 | + addr.sin_addr.s_addr = INADDR_ANY; |
| 90 | + if (0 != bind(sockfd, (struct sockaddr *)&addr, sizeof(addr))) { |
| 91 | + fildesh_compat_errno_trace(); |
| 92 | + fildesh_compat_socket_close(sockfd); |
| 93 | + return FILDESH_COMPAT_SOCKET_INVALID; |
| 94 | + } |
| 95 | + |
| 96 | + /* Listen for connections. */ |
| 97 | + if (0 != listen(sockfd, 5)) { |
| 98 | + fildesh_compat_errno_trace(); |
| 99 | + fildesh_compat_socket_close(sockfd); |
| 100 | + return FILDESH_COMPAT_SOCKET_INVALID; |
| 101 | + } |
| 102 | + |
| 103 | + if (*port == 0) { |
| 104 | + socklen_t len = sizeof(addr); |
| 105 | + if (getsockname(sockfd, (struct sockaddr *)&addr, &len) == -1) { |
| 106 | + fildesh_compat_errno_trace(); |
| 107 | + fildesh_compat_socket_close(sockfd); |
| 108 | + return FILDESH_COMPAT_SOCKET_INVALID; |
| 109 | + } |
| 110 | + *port = ntohs(addr.sin_port); |
| 111 | + } |
| 112 | + |
| 113 | + return sockfd; |
| 114 | +} |
| 115 | + |
| 116 | +FildeshCompat_socket fildesh_compat_socket_accept(FildeshCompat_socket fd) { |
| 117 | + return accept(fd, NULL, NULL); |
| 118 | +} |
| 119 | + |
| 120 | +int fildesh_compat_socket_recv(FildeshCompat_socket fd, void* buf, size_t len) { |
| 121 | + return (int)recv(fd, (char*)buf, (int)len, 0); |
| 122 | +} |
| 123 | + |
| 124 | +int fildesh_compat_socket_send(FildeshCompat_socket fd, const void* buf, size_t len) { |
| 125 | + return (int)send(fd, (const char*)buf, (int)len, 0); |
| 126 | +} |
| 127 | + |
| 128 | +int fildesh_compat_socket_recv_error_is_benign(void) { |
| 129 | +#ifndef _MSC_VER |
| 130 | + if (errno == EAGAIN || errno == EWOULDBLOCK) { return 1; } |
| 131 | +#else |
| 132 | + int err = WSAGetLastError(); |
| 133 | + if (err == WSAEWOULDBLOCK || err == WSAETIMEDOUT) { return 1; } |
| 134 | +#endif |
| 135 | + return 0; |
| 136 | +} |
0 commit comments