raop airplay lib: fixes by @aelmod for compilation on Windows

This commit is contained in:
fduncanh
2022-09-01 11:41:17 -04:00
parent 24da455071
commit 1871ea0f30
6 changed files with 74 additions and 24 deletions

View File

@@ -13,17 +13,28 @@
*/
#include <time.h>
#include <netinet/in.h>
#ifdef _WIN32
# include <winsock2.h>
#else
# include <netinet/in.h>
#endif
#include "byteutils.h"
#ifndef htonll
#ifdef SYS_ENDIAN_H
#include <sys/endian.h>
#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 <endian.h>
#endif
#define htonll(x) htobe64(x)
#define ntohll(x) be64toh(x)
# ifndef htonll
# ifdef SYS_ENDIAN_H
# include <sys/endian.h>
# else
# include <endian.h>
# endif
# define htonll(x) htobe64(x)
# define ntohll(x) be64toh(x)
# endif
#endif
// The functions in this file assume a little endian cpu architecture!

View File

@@ -46,8 +46,8 @@
# define USE_LIBDL 0
#endif
#if defined(WIN32) || USE_LIBDL
# ifdef WIN32
#if defined(_WIN32) || USE_LIBDL
# ifdef _WIN32
# include <stdint.h>
# 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);
}
}
}

View File

@@ -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));

View File

@@ -20,6 +20,12 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#ifdef _WIN32
#define CAST (char *)
#include <sys/time.h>
#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.

View File

@@ -20,7 +20,11 @@
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netinet/tcp.h>
#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;
}

View File

@@ -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);
}