win32: socket: use winsock2.h instead of sys/socket.h

Windows has no POSIX sockets. Use the Windows provided winsock2.h
instead.

- include winsocket
- call WSAStartup and WSACleanup
- use SOCKET as file descriptor
- compare to INVALID_SOCKET or SOCKET_ERROR as recommended
- modify optflag type for setsockopts()
This commit is contained in:
NeroBurner 2020-02-18 13:43:15 +01:00 committed by Reinhold Gschweicher
parent 5eaa5324f5
commit ae5e359ec9
1 changed files with 77 additions and 0 deletions

View File

@ -21,17 +21,25 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#else // _WIN32
#include <winsock2.h>
#endif
#include <unistd.h>
#include "common/logger.h"
struct socket_t
{
struct socket_config_t config;
#ifndef _WIN32
int fd;
#else // _WIN32
SOCKET fd;
#endif
};
static int socket_open(socket_handle_t handle);
@ -54,6 +62,16 @@ int socket_init(socket_handle_t* handle, struct socket_config_t const* config)
logger_log(LOG_FATAL, "%s: could not allocate memory", __func__);
return -ENOMEM;
}
#ifdef _WIN32
WSADATA wsadata;
ret = WSAStartup(MAKEWORD(2,2), &wsadata);
if (ret != 0)
{
logger_log(LOG_FATAL, "%s: WSAStartup failed: %d", __func__, ret);
return -EINVAL;
}
(*handle)->fd = INVALID_SOCKET;
#endif
(*handle)->config = *config;
@ -82,6 +100,9 @@ int socket_release(socket_handle_t* handle)
free(*handle);
*handle = 0;
}
#ifdef _WIN32
WSACleanup();
#endif
return ret;
}
@ -94,7 +115,21 @@ int socket_is_broadcast_address(char const* ip)
int socket_open(socket_handle_t handle)
{
int ret = 0;
#ifndef _WIN32
int optflag = 0;
#else // _WIN32
// optflag is used as optval for setsockopt(), windows expects it to be a char
// posix expects a void *
// from https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt
// int setsockopt(
// SOCKET s,
// int level,
// int optname,
// const char *optval,
// int optlen
// );
char optflag = 0;
#endif
struct sockaddr_in si_me;
if (handle == 0)
@ -105,7 +140,11 @@ int socket_open(socket_handle_t handle)
logger_log(LOG_INFO, "%s: opening socket with port %d", __func__, handle->config.port);
#ifndef _WIN32
if (handle->fd != 0)
#else // _WIN32
if (handle->fd != INVALID_SOCKET)
#endif
{
ret = socket_close(handle);
if (ret != 0)
@ -116,6 +155,7 @@ int socket_open(socket_handle_t handle)
}
handle->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifndef _WIN32
if (handle->fd < 0)
{
logger_log(LOG_ERROR, "%s: unable to create socket", __func__);
@ -123,6 +163,15 @@ int socket_open(socket_handle_t handle)
handle->fd = 0;
return ret;
}
#else // _WIN32
if (handle->fd == INVALID_SOCKET)
{
logger_log(LOG_ERROR, "%s: unable to create socket", __func__);
ret = -1;
handle->fd = INVALID_SOCKET;
return ret;
}
#endif
if (handle->config.direction == SOCKET_IN)
{
@ -132,11 +181,19 @@ int socket_open(socket_handle_t handle)
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(handle->fd, (struct sockaddr const*)&si_me, sizeof(si_me));
#ifndef _WIN32
if (ret < 0)
#else // _WIN32
if (ret == SOCKET_ERROR)
#endif
{
logger_log(LOG_ERROR, "%s: unable to bind socket", __func__);
socket_close(handle);
#ifndef _WIN32
return errno;
#else // _WIN32
return WSAGetLastError();
#endif
}
}
else
@ -172,11 +229,19 @@ int socket_close(socket_handle_t handle)
logger_log(LOG_INFO, "%s: closing socket with port %d", __func__, handle->config.port);
#ifndef _WIN32
if (handle->fd != 0)
#else // _WIN32
if (handle->fd == INVALID_SOCKET)
#endif
{
ret = close(handle->fd);
handle->fd = 0;
#ifndef _WIN32
if (ret != 0)
#else // _WIN32
if (ret == SOCKET_ERROR)
#endif
{
logger_log(LOG_ERROR, "%s: unable to close socket", __func__);
return ret;
@ -190,7 +255,11 @@ int socket_read(socket_handle_t handle, char* buffer, size_t size)
{
int ret = 0;
struct sockaddr_in si_other;
#ifndef _WIN32
socklen_t slen = sizeof(si_other);
#else // _WIN32
int slen = sizeof(si_other);
#endif
logger_log(LOG_DEBUG, "%s invoked", __func__);
@ -202,7 +271,11 @@ int socket_read(socket_handle_t handle, char* buffer, size_t size)
logger_log(LOG_DEBUG, "%s ip %s", __func__, handle->config.ip_address);
#ifndef _WIN32
if (handle->fd == 0)
#else // _WIN32
if (handle->fd == INVALID_SOCKET)
#endif
{
logger_log(LOG_ERROR, "%s: socket is not open", __func__);
return -ENODEV;
@ -232,7 +305,11 @@ int socket_write(socket_handle_t handle, char const* buffer, size_t size)
{
int ret = 0;
struct sockaddr_in si_other;
#ifndef _WIN32
socklen_t slen = sizeof(si_other);
#else // _WIN32
int slen = sizeof(si_other);
#endif
logger_log(LOG_DEBUG, "%s invoked", __func__);