Fix LAN discovery for SRB2 servers

This commit is contained in:
Gustaf Alhäll 2023-11-22 19:41:35 +01:00
parent 7962b243de
commit 977cd0db24
No known key found for this signature in database
GPG key ID: 6C1F67D690CDEDFD

View file

@ -154,6 +154,8 @@ typedef union
#define ERRSOCKET (-1) #define ERRSOCKET (-1)
#endif #endif
#define IPV6_MULTICAST_ADDRESS "ff15::57e1:1a12"
// define socklen_t in DOS/Windows if it is not already defined // define socklen_t in DOS/Windows if it is not already defined
#ifdef USE_WINSOCK1 #ifdef USE_WINSOCK1
typedef int socklen_t; typedef int socklen_t;
@ -621,6 +623,7 @@ static inline ssize_t SOCK_SendToAddr(SOCKET_TYPE socket, mysockaddr_t *sockaddr
socklen_t d6 = (socklen_t)sizeof(struct sockaddr_in6); socklen_t d6 = (socklen_t)sizeof(struct sockaddr_in6);
#endif #endif
socklen_t d, da = (socklen_t)sizeof(mysockaddr_t); socklen_t d, da = (socklen_t)sizeof(mysockaddr_t);
ssize_t status;
switch (sockaddr->any.sa_family) switch (sockaddr->any.sa_family)
{ {
@ -631,7 +634,12 @@ static inline ssize_t SOCK_SendToAddr(SOCKET_TYPE socket, mysockaddr_t *sockaddr
default: d = da; break; default: d = da; break;
} }
return sendto(socket, (char *)&doomcom->data, doomcom->datalength, 0, &sockaddr->any, d); status = sendto(socket, (char *)&doomcom->data, doomcom->datalength, 0, &sockaddr->any, d);
if (status == -1)
{
CONS_Alert(CONS_WARNING, "Unable to send packet to %s: %s\n", SOCK_AddrToStr(sockaddr), strerror(errno));
}
return status;
} }
static void SOCK_Send(void) static void SOCK_Send(void)
@ -770,6 +778,24 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen
return (SOCKET_TYPE)ERRSOCKET; return (SOCKET_TYPE)ERRSOCKET;
} }
#ifdef HAVE_IPV6
if (family == AF_INET6)
{
// we need to set all of this *after* binding to an address!
if (memcmp(&straddr.ip6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) //IN6_ARE_ADDR_EQUAL
{
struct ipv6_mreq maddr;
inet_pton(AF_INET6, IPV6_MULTICAST_ADDRESS, &maddr.ipv6mr_multiaddr);
maddr.ipv6mr_interface = 0;
if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &maddr, sizeof(maddr)) != 0)
{
CONS_Alert(CONS_WARNING, M_GetText("Could not register multicast address\n"));
}
}
}
#endif
#ifdef FIONBIO #ifdef FIONBIO
// make it non blocking // make it non blocking
opt = true; opt = true;
@ -950,65 +976,28 @@ static boolean UDP_Socket(void)
// ip + udp // ip + udp
packetheaderlength = 20 + 8; // for stats packetheaderlength = 20 + 8; // for stats
hints.ai_family = AF_INET; clientaddress[s].any.sa_family = AF_INET;
gaie = I_getaddrinfo("127.0.0.1", "0", &hints, &ai); clientaddress[s].ip4.sin_port = htons(0);
if (gaie == 0) clientaddress[s].ip4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); //GetLocalAddress(); // my own ip
{ s++;
runp = ai;
while (runp != NULL && s < MAXNETNODES+1)
{
memcpy(&clientaddress[s], runp->ai_addr, runp->ai_addrlen);
s++;
runp = runp->ai_next;
}
I_freeaddrinfo(ai);
}
else
{
clientaddress[s].any.sa_family = AF_INET;
clientaddress[s].ip4.sin_port = htons(0);
clientaddress[s].ip4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); //GetLocalAddress(); // my own ip
s++;
}
s = 0; s = 0;
// setup broadcast adress to BROADCASTADDR entry // setup broadcast adress to BROADCASTADDR entry
gaie = I_getaddrinfo("255.255.255.255", "0", &hints, &ai); broadcastaddress[s].any.sa_family = AF_INET;
if (gaie == 0) broadcastaddress[s].ip4.sin_port = htons(atoi(DEFAULTPORT));
{ broadcastaddress[s].ip4.sin_addr.s_addr = htonl(INADDR_BROADCAST);
runp = ai; s++;
while (runp != NULL && s < MAXNETNODES+1)
{
memcpy(&broadcastaddress[s], runp->ai_addr, runp->ai_addrlen);
s++;
runp = runp->ai_next;
}
I_freeaddrinfo(ai);
}
else
{
broadcastaddress[s].any.sa_family = AF_INET;
broadcastaddress[s].ip4.sin_port = htons(0);
broadcastaddress[s].ip4.sin_addr.s_addr = htonl(INADDR_BROADCAST);
s++;
}
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
if (b_ipv6) if (b_ipv6)
{ {
hints.ai_family = AF_INET6; broadcastaddress[s].any.sa_family = AF_INET6;
gaie = I_getaddrinfo("ff02::1", "0", &hints, &ai); broadcastaddress[s].ip6.sin6_port = htons(atoi(DEFAULTPORT));
if (gaie == 0) broadcastaddress[s].ip6.sin6_flowinfo = 0;
{ inet_pton(AF_INET6, IPV6_MULTICAST_ADDRESS, &broadcastaddress[s].ip6.sin6_addr);
runp = ai; broadcastaddress[s].ip6.sin6_scope_id = 0;
while (runp != NULL && s < MAXNETNODES+1) s++;
{
memcpy(&broadcastaddress[s], runp->ai_addr, runp->ai_addrlen);
s++;
runp = runp->ai_next;
}
I_freeaddrinfo(ai);
}
} }
#endif #endif