network: add stub for systems without BSD sockets, WATTCP support

This commit is contained in:
mittorn 2020-02-08 22:45:50 +07:00
parent 070272489d
commit 2b90790071
3 changed files with 137 additions and 33 deletions

View File

@ -17,8 +17,11 @@ GNU General Public License for more details.
#include "miniz.h"
#include "imagelib.h"
#include "mathlib.h"
#if !XASH_WIN32
#include <netinet/in.h>
#if defined(XASH_NO_NETWORK)
#include "platform/stub/net_stub.h"
#elif !XASH_WIN32
#include <netinet/in.h>
#endif
static const char png_sign[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};

View File

@ -20,7 +20,7 @@ GNU General Public License for more details.
#if XASH_WIN32
// Winsock
#include <WS2tcpip.h>
#else
#elif !defined XASH_NO_NETWORK
// BSD sockets
#include <sys/types.h>
#include <sys/socket.h>
@ -31,21 +31,7 @@ GNU General Public License for more details.
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#endif
#define NET_USE_FRAGMENTS
#define PORT_ANY -1
#define MAX_LOOPBACK 4
#define MASK_LOOPBACK (MAX_LOOPBACK - 1)
#define MAX_ROUTEABLE_PACKET 1400
#define SPLITPACKET_MIN_SIZE 508 // RFC 791: 576(min ip packet) - 60 (ip header) - 8 (udp header)
#define SPLITPACKET_MAX_SIZE 64000
#define NET_MAX_FRAGMENTS ( NET_MAX_FRAGMENT / (SPLITPACKET_MIN_SIZE - sizeof( SPLITPACKET )) )
#if !XASH_WIN32 // not available in XP
#define HAVE_GETADDRINFO
#define WSAGetLastError() errno
#define WSAEINTR EINTR
#define WSAEBADF EBADF
@ -84,6 +70,12 @@ GNU General Public License for more details.
#define WSAENAMETOOLONG ENAMETOOLONG
#define WSAEHOSTDOWN EHOSTDOWN
#ifndef XASH_DOS4GW
#define HAVE_GETADDRINFO
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#if XASH_EMSCRIPTEN
/* All socket operations are non-blocking already */
static int ioctl_stub( int d, unsigned long r, ... )
@ -95,12 +87,23 @@ static int ioctl_stub( int d, unsigned long r, ... )
#define ioctlsocket ioctl
#endif // XASH_EMSCRIPTEN
#define closesocket close
#define SOCKET int
#define INVALID_SOCKET -1
typedef socklen_t WSAsize_t;
#else // WIN32
typedef int WSAsize_t; // for some reason, MS has signed size. We won't exceed 2^32, probably, so just typedef it
#endif
#define SOCKET int
typedef int WSAsize_t;
#else
#include "platform/stub/net_stub.h"
#endif
#define NET_USE_FRAGMENTS
#define PORT_ANY -1
#define MAX_LOOPBACK 4
#define MASK_LOOPBACK (MAX_LOOPBACK - 1)
#define MAX_ROUTEABLE_PACKET 1400
#define SPLITPACKET_MIN_SIZE 508 // RFC 791: 576(min ip packet) - 60 (ip header) - 8 (udp header)
#define SPLITPACKET_MAX_SIZE 64000
#define NET_MAX_FRAGMENTS ( NET_MAX_FRAGMENT / (SPLITPACKET_MIN_SIZE - sizeof( SPLITPACKET )) )
typedef struct
{
@ -242,7 +245,7 @@ char *NET_ErrorString( void )
_inline qboolean NET_IsSocketError( int retval )
{
#if XASH_WIN32
#if XASH_WIN32 || XASH_DOS4GW
return retval == SOCKET_ERROR ? true : false;
#else
return retval < 0 ? true : false;
@ -251,7 +254,7 @@ _inline qboolean NET_IsSocketError( int retval )
_inline qboolean NET_IsSocketValid( int socket )
{
#if XASH_WIN32
#if XASH_WIN32 || XASH_DOS4GW
return socket != INVALID_SOCKET;
#else
return socket >= 0;
@ -335,7 +338,7 @@ int NET_GetHostByName( const char *hostname )
#endif
}
#if !defined XASH_NO_ASYNC_NS_RESOLVE && ( XASH_WIN32 || !XASH_EMSCRIPTEN )
#if !defined XASH_NO_ASYNC_NS_RESOLVE && ( XASH_WIN32 || !(XASH_EMSCRIPTEN || XASH_DOS4GW) )
#define CAN_ASYNC_NS_RESOLVE
#endif
@ -1135,6 +1138,7 @@ qboolean NET_QueuePacket( netsrc_t sock, netadr_t *from, byte *data, size_t *len
case WSAECONNRESET:
case WSAECONNREFUSED:
case WSAEMSGSIZE:
case WSAETIMEDOUT:
break;
default: // let's continue even after errors
Con_DPrintf( S_ERROR "NET_QueuePacket: %s from %s\n", NET_ErrorString(), NET_AdrToString( *from ));
@ -1394,19 +1398,20 @@ static int NET_Isocket( const char *net_interface, int port, qboolean multicast
return INVALID_SOCKET;
}
if( NET_IsSocketError( ioctlsocket( net_socket, FIONBIO, &_true ) ) )
if( NET_IsSocketError( ioctlsocket( net_socket, FIONBIO, (void*)&_true ) ) )
{
struct timeval timeout;
Con_DPrintf( S_WARN "NET_UDsocket: port: %d ioctl FIONBIO: %s\n", port, NET_ErrorString( ));
closesocket( net_socket );
return INVALID_SOCKET;
// try timeout instead of NBIO
timeout.tv_sec = timeout.tv_usec = 0;
setsockopt( net_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
}
// make it broadcast capable
if( NET_IsSocketError( setsockopt( net_socket, SOL_SOCKET, SO_BROADCAST, (char *)&_true, sizeof( _true ) ) ) )
{
Con_DPrintf( S_WARN "NET_UDsocket: port: %d setsockopt SO_BROADCAST: %s\n", port, NET_ErrorString( ));
closesocket( net_socket );
return INVALID_SOCKET;
}
if( Sys_CheckParm( "-reuse" ) || multicast )
@ -1639,6 +1644,7 @@ sleeps msec or until net socket is ready
*/
void NET_Sleep( int msec )
{
#ifndef XASH_NO_NETWORK
struct timeval timeout;
fd_set fdset;
int i = 0;
@ -1657,6 +1663,7 @@ void NET_Sleep( int msec )
timeout.tv_sec = msec / 1000;
timeout.tv_usec = (msec % 1000) * 1000;
select( i+1, &fdset, NULL, NULL, &timeout );
#endif
}
/*
@ -1703,7 +1710,7 @@ void NET_Init( void )
}
#if XASH_WIN32
if( WSAStartup( MAKEWORD( 1, 1 ), &net.winsockdata ))
if( WSAStartup( MAKEWORD( 1, 1 ), &net.winsockdata ) )
{
Con_DPrintf( S_ERROR "network initialization failed.\n" );
return;
@ -2136,7 +2143,7 @@ void HTTP_Run( void )
// You may skip this if not supported by system,
// but download will lock engine, maybe you will need to add manual returns
mode = 1;
ioctlsocket( curfile->socket, FIONBIO, &mode );
ioctlsocket( curfile->socket, FIONBIO, (void*)&mode );
#if XASH_LINUX
// SOCK_NONBLOCK is not portable, so use fcntl
fcntl( curfile->socket, F_SETFL, fcntl( curfile->socket, F_GETFL, 0 ) | O_NONBLOCK );
@ -2444,7 +2451,7 @@ static void HTTP_Clear_f( void )
FS_Close( file->file );
if( file->socket != -1 )
close ( file->socket );
closesocket( file->socket );
Mem_Free( file );
}

View File

@ -0,0 +1,94 @@
/*
net_stub.h - stub BSD sockets
Copyright (C) 2020 mittorn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define SOCKET int
typedef int WSAsize_t;
struct in_addr {unsigned long s_addr;};
struct sockaddr_in{ short sin_family;unsigned short sin_port;struct in_addr sin_addr;};
struct sockaddr {short sa_family;int stub[32];};
struct hostent {int h_addr_list[1];};
struct timeval {long tv_sec;long tv_usec;};
#define AF_INET 0
#define INADDR_BROADCAST 0
#define INADDR_ANY 0
//! Network to host conversion for a word.
#define ntohs(n) ( (((n) & 0xFF00) >> 8) | (((n) & 0x00FF) << 8) )
//! Host to network conversion for a word.
#define htons(n) ( (((n) & 0xFF00) >> 8) | (((n) & 0x00FF) << 8) )
//! Network to host conversion for a double word.
#define ntohl(n) ( (((n) & 0xFF000000) >> 24) | (((n) & 0x00FF0000) >> 8) \
| (((n) & 0x0000FF00) << 8) | (((n) & 0x000000FF) << 24) )
//! Host to network conversion for a double word.
#define htonl(n) ( (((n) & 0xFF000000) >> 24) | (((n) & 0x00FF0000) >> 8) \
| (((n) & 0x0000FF00) << 8) | (((n) & 0x000000FF) << 24) )
#define gethostbyname(...) NULL
#define inet_addr(...) (-1)
#define recvfrom(...) (-1)
#define sendto(...) (-1)
#define socket(...) (-1)
#define ioctlsocket(...) (-1)
#define setsockopt(...) (-1)
#define bind(...) (-1)
#define gethostname(...) (-1)
#define getsockname(...) (-1)
#define connect(...) (-1)
#define send(...) (-1)
#define recv(...) (-1)
#define bind(...) (-1)
#define closesocket(...) (-1)
#define select(...) (-1)
#define WSAGetLastError() (22) //ENETDOWN
#define WSAEINTR 1 //EINTR
#define WSAEBADF 2 //EBADF
#define WSAEACCES 3 //EACCES
#define WSAEFAULT 4 //EFAULT
#define WSAEINVAL 5 //EINVAL
#define WSAEMFILE 6 //EMFILE
#define WSAEWOULDBLOCK 7 //EWOULDBLOCK
#define WSAEINPROGRESS 8 //EINPROGRESS
#define WSAEALREADY 9 //EALREADY
#define WSAENOTSOCK 10 //ENOTSOCK
#define WSAEDESTADDRREQ 11 //EDESTADDRREQ
#define WSAEMSGSIZE 12 //EMSGSIZE
#define WSAEPROTOTYPE 13 //EPROTOTYPE
#define WSAENOPROTOOPT 14 //ENOPROTOOPT
#define WSAEPROTONOSUPPORT 15 //EPROTONOSUPPORT
#define WSAESOCKTNOSUPPORT 16 //ESOCKTNOSUPPORT
#define WSAEOPNOTSUPP 17 //EOPNOTSUPP
#define WSAEPFNOSUPPORT 18 //EPFNOSUPPORT
#define WSAEAFNOSUPPORT 19 //EAFNOSUPPORT
#define WSAEADDRINUSE 20 //EADDRINUSE
#define WSAEADDRNOTAVAIL 21 //EADDRNOTAVAIL
#define WSAENETDOWN 22 //ENETDOWN
#define WSAENETUNREACH 23 //ENETUNREACH
#define WSAENETRESET 24 //ENETRESET
#define WSAECONNABORTED 25 //ECONNABORTED
#define WSAECONNRESET 26 //ECONNRESET
#define WSAENOBUFS 27 //ENOBUFS
#define WSAEISCONN 28 //EISCONN
#define WSAENOTCONN 29 //ENOTCONN
#define WSAESHUTDOWN 30 //ESHUTDOWN
#define WSAETOOMANYREFS 31 //ETOOMANYREFS
#define WSAETIMEDOUT 32 //ETIMEDOUT
#define WSAECONNREFUSED 33 //ECONNREFUSED
#define WSAELOOP 34 //ELOOP
#define WSAENAMETOOLONG 35 //ENAMETOOLONG
#define WSAEHOSTDOWN 36 //EHOSTDOWN