From 1871ea0f30f0a87774fa532502abb988db4d181f Mon Sep 17 00:00:00 2001 From: fduncanh Date: Thu, 1 Sep 2022 11:41:17 -0400 Subject: [PATCH] raop airplay lib: fixes by @aelmod for compilation on Windows --- lib/byteutils.c | 27 +++++++++++++++++++-------- lib/dnssd.c | 9 +++++---- lib/netutils.c | 10 +++++++--- lib/raop_ntp.c | 17 +++++++++++++++-- lib/raop_rtp_mirror.c | 31 ++++++++++++++++++++++++------- lib/utils.c | 4 ++++ 6 files changed, 74 insertions(+), 24 deletions(-) diff --git a/lib/byteutils.c b/lib/byteutils.c index c15dee4..4a5bdea 100644 --- a/lib/byteutils.c +++ b/lib/byteutils.c @@ -13,17 +13,28 @@ */ #include -#include +#ifdef _WIN32 +# include +#else +# include +#endif + #include "byteutils.h" -#ifndef htonll -#ifdef SYS_ENDIAN_H -#include +#ifdef _WIN32 +# ifndef ntonll +# define ntohll(x) ((1==ntohl(1)) ? (x) : (((uint64_t)ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t)((x) >> 32))) +# endif #else -#include -#endif -#define htonll(x) htobe64(x) -#define ntohll(x) be64toh(x) +# ifndef htonll +# ifdef SYS_ENDIAN_H +# include +# else +# include +# endif +# define htonll(x) htobe64(x) +# define ntohll(x) be64toh(x) +# endif #endif // The functions in this file assume a little endian cpu architecture! diff --git a/lib/dnssd.c b/lib/dnssd.c index 8048ace..e6e5cbd 100644 --- a/lib/dnssd.c +++ b/lib/dnssd.c @@ -46,8 +46,8 @@ # define USE_LIBDL 0 #endif -#if defined(WIN32) || USE_LIBDL -# ifdef WIN32 +#if defined(_WIN32) || USE_LIBDL +# ifdef _WIN32 # include # if !defined(EFI32) && !defined(EFI64) # define DNSSD_STDCALL __stdcall @@ -60,8 +60,9 @@ # endif typedef struct _DNSServiceRef_t *DNSServiceRef; +#ifndef _WIN32 typedef union _TXTRecordRef_t { char PrivateData[16]; char *ForceNaturalAlignment; } TXTRecordRef; - +#endif typedef uint32_t DNSServiceFlags; typedef int32_t DNSServiceErrorType; @@ -403,4 +404,4 @@ dnssd_unregister_airplay(dnssd_t *dnssd) free(dnssd->name); free(dnssd->hw_addr); } -} \ No newline at end of file +} diff --git a/lib/netutils.c b/lib/netutils.c index 9e160c7..df54b64 100644 --- a/lib/netutils.c +++ b/lib/netutils.c @@ -92,8 +92,12 @@ netutils_init_socket(unsigned short *port, int use_ipv6, int use_udp) socklen_t socklen; int server_fd; int ret; +#ifndef _WIN32 int reuseaddr = 1; - +#else + const char reuseaddr = 1; +#endif + assert(port); server_fd = socket(family, type, proto); @@ -109,14 +113,14 @@ netutils_init_socket(unsigned short *port, int use_ipv6, int use_udp) memset(&saddr, 0, sizeof(saddr)); if (use_ipv6) { struct sockaddr_in6 *sin6ptr = (struct sockaddr_in6 *)&saddr; - int v6only = 1; /* Initialize sockaddr for bind */ sin6ptr->sin6_family = family; sin6ptr->sin6_addr = in6addr_any; sin6ptr->sin6_port = htons(*port); -#ifndef WIN32 +#ifndef _WIN32 + int v6only = 1; /* Make sure we only listen to IPv6 addresses */ setsockopt(server_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &v6only, sizeof(v6only)); diff --git a/lib/raop_ntp.c b/lib/raop_ntp.c index 9a2adb4..6fbbd5f 100644 --- a/lib/raop_ntp.c +++ b/lib/raop_ntp.c @@ -20,6 +20,12 @@ #include #include #include +#ifdef _WIN32 +#define CAST (char *) +#include +#else +#define CAST +#endif #include "raop.h" #include "threads.h" @@ -204,10 +210,11 @@ raop_ntp_init_socket(raop_ntp_t *raop_ntp, int use_ipv6) } // We're calling recvfrom without knowing whether there is any data, so we need a timeout + struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 300000; - if (setsockopt(tsock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + if (setsockopt(tsock, SOL_SOCKET, SO_RCVTIMEO, CAST &tv, sizeof(tv)) < 0) { goto sockets_cleanup; } @@ -227,8 +234,14 @@ raop_ntp_init_socket(raop_ntp_t *raop_ntp, int use_ipv6) static void raop_ntp_flush_socket(int fd) { +#ifdef _WIN32 +#define IOCTL ioctlsocket + u_long bytes_available = 0; +#else +#define IOCTL ioctl int bytes_available = 0; - while (ioctl(fd, FIONREAD, &bytes_available) == 0 && bytes_available > 0) +#endif + while (IOCTL(fd, FIONREAD, &bytes_available) == 0 && bytes_available > 0) { // We are guaranteed that we won't block, because bytes are available. // Read 1 byte. Extra bytes in the datagram will be discarded. diff --git a/lib/raop_rtp_mirror.c b/lib/raop_rtp_mirror.c index 6f125a5..34da7d4 100644 --- a/lib/raop_rtp_mirror.c +++ b/lib/raop_rtp_mirror.c @@ -20,7 +20,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include "raop.h" #include "netutils.h" @@ -32,7 +36,17 @@ #include "utils.h" #include "plist/plist.h" +#ifdef _WIN32 +#define CAST (char *) +#define TCP_KEEPIDLE SO_KEEPALIVE +#define TCP_KEEPINTVL SO_KEEPALIVE +#define TCP_KEEPCNT SO_KEEPALIVE +#else +#define CAST +#endif + #define SEC 1000000 + /* for MacOS, where SOL_TCP and TCP_KEEPIDLE are not defined */ #if !defined(SOL_TCP) && defined(IPPROTO_TCP) #define SOL_TCP IPPROTO_TCP @@ -240,25 +254,26 @@ raop_rtp_mirror_thread(void *arg) struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 5000; - if (setsockopt(stream_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + if (setsockopt(stream_fd, SOL_SOCKET, SO_RCVTIMEO, CAST &tv, sizeof(tv)) < 0) { logger_log(raop_rtp_mirror->logger, LOGGER_ERR, "raop_rtp_mirror could not set stream socket timeout %d %s", errno, strerror(errno)); break; } + int option; option = 1; - if (setsockopt(stream_fd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option)) < 0) { + if (setsockopt(stream_fd, SOL_SOCKET, SO_KEEPALIVE, CAST &option, sizeof(option)) < 0) { logger_log(raop_rtp_mirror->logger, LOGGER_WARNING, "raop_rtp_mirror could not set stream socket keepalive %d %s", errno, strerror(errno)); } option = 60; - if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPIDLE, &option, sizeof(option)) < 0) { + if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPIDLE, CAST &option, sizeof(option)) < 0) { logger_log(raop_rtp_mirror->logger, LOGGER_WARNING, "raop_rtp_mirror could not set stream socket keepalive time %d %s", errno, strerror(errno)); } option = 10; - if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPINTVL, &option, sizeof(option)) < 0) { + if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPINTVL, CAST &option, sizeof(option)) < 0) { logger_log(raop_rtp_mirror->logger, LOGGER_WARNING, "raop_rtp_mirror could not set stream socket keepalive interval %d %s", errno, strerror(errno)); } option = 6; - if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPCNT, &option, sizeof(option)) < 0) { + if (setsockopt(stream_fd, SOL_TCP, TCP_KEEPCNT, CAST &option, sizeof(option)) < 0) { logger_log(raop_rtp_mirror->logger, LOGGER_WARNING, "raop_rtp_mirror could not set stream socket keepalive probes %d %s", errno, strerror(errno)); } readstart = 0; @@ -268,7 +283,8 @@ raop_rtp_mirror_thread(void *arg) // The first 128 bytes are some kind of header for the payload that follows while (payload == NULL && readstart < 128) { - ret = recv(stream_fd, packet + readstart, 128 - readstart, 0); + unsigned char* pos = packet + readstart; + ret = recv(stream_fd, CAST pos, 128 - readstart, 0); if (ret <= 0) break; readstart = readstart + ret; } @@ -315,7 +331,8 @@ raop_rtp_mirror_thread(void *arg) while (readstart < payload_size) { // Payload data - ret = recv(stream_fd, payload + readstart, payload_size - readstart, 0); + unsigned char *pos = payload + readstart; + ret = recv(stream_fd, CAST pos, payload_size - readstart, 0); if (ret <= 0) break; readstart = readstart + ret; } diff --git a/lib/utils.c b/lib/utils.c index 8dd0cde..c100aed 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -218,7 +218,11 @@ void ntp_timestamp_to_time(uint64_t ntp_timestamp, char *timestamp, size_t maxsi time_t rawtime = (time_t) (ntp_timestamp / 1000000); struct tm ts = *localtime(&rawtime); assert(maxsize > 26); +#ifdef _WIN32 /*modification for compiling for Windows */ + strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", &ts); +#else strftime(timestamp, 20, "%F %T", &ts); +#endif snprintf(timestamp + 19, 8,".%6.6u", (unsigned int) ntp_timestamp % 1000000); }