2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_bw.c
|
|
|
|
|
|
|
|
@description@
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dpmi.h>
|
|
|
|
|
|
|
|
#include "dosisms.h"
|
|
|
|
|
|
|
|
|
|
|
|
// this section is general Unix stuff that we need
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
#define EIO 5 /* I/O error */
|
2001-02-19 21:15:25 +00:00
|
|
|
#define EBADS 9
|
2001-02-26 06:48:02 +00:00
|
|
|
#define EWOULDBLOCK 35 /* function would block */
|
|
|
|
#define EMSGSIZE 40 /* message to big for buffers */
|
|
|
|
#define EPROTONOSUPPORT 43 /* Protocol not supported */
|
|
|
|
#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
|
|
|
|
#define EPFNOSUPPORT 46 /* Protocol family not supported */
|
|
|
|
#define EAFNOSUPPORT 47 /* Address family not supported */
|
|
|
|
#define ECONNABORTED 53 /* User requested hangup */
|
|
|
|
#define ENOBUFS 55 /* No buffers available */
|
|
|
|
#define EISCONN 56 /* Socket has closed */
|
|
|
|
#define ENOTCONN 57 /* Socket is not connected */
|
|
|
|
#define ESHUTDOWN 58 /* Socket is closed */
|
|
|
|
#define ETOOMANYREFS 59 /* Too many sockets open */
|
|
|
|
#define ETIMEDOUT 60 /* Connection timed out */
|
|
|
|
#define ECONNREFUSED 61 /* Connection refused */
|
|
|
|
|
|
|
|
#define AF_INET 2 /* internet */
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#define PF_INET AF_INET
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
#define SOCK_STREAM 1 /* stream */
|
|
|
|
#define SOCK_DGRAM 2 /* datagram */
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#define IPPROTO_TCP 6
|
|
|
|
#define IPPROTO_UDP 17
|
|
|
|
|
|
|
|
#define INADDR_ANY 0
|
|
|
|
|
|
|
|
#define SIOCDONE 0x7300
|
|
|
|
#define FIONREAD 0x667f
|
|
|
|
#define FIONBIO 0x667e
|
|
|
|
#define FIONWIN 0x1000
|
|
|
|
#define FIONTIN 0x2000
|
|
|
|
|
|
|
|
#define BRDINIT 0
|
|
|
|
#define BRDADDR 10
|
|
|
|
|
|
|
|
#define MAXHOSTNAMELEN 256
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
#define SOL_SOCKET 0xffff /* options for socket level */
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Option flags per-socket.
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
#define SO_DEBUG 0x0001 /* turn on debugging info recording */
|
|
|
|
#define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
|
|
|
|
#define SO_REUSEADDR 0x0004 /* allow local address reuse */
|
|
|
|
#define SO_KEEPALIVE 0x0008 /* keep connections alive */
|
|
|
|
#define SO_DONTROUTE 0x0010 /* just use interface addresses */
|
|
|
|
#define SO_BROADCAST 0x0020 /* permit sending of broadcast msgs */
|
|
|
|
#define SO_USELOOPBACK 0x0040 /* bypass hardware when possible */
|
|
|
|
#define SO_LINGER 0x0080 /* linger on close if data present */
|
|
|
|
#define SO_OOBINLINE 0x0100 /* leave received OOB data in line */
|
|
|
|
#define SO_USEPRIV 0x4000 /* allocate from privileged port area
|
|
|
|
*/
|
|
|
|
#define SO_CANTSIG 0x8000 /* prevent SIGPIPE on SS_CANTSENDMORE
|
|
|
|
*/
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Additional options, not kept in so_options.
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
#define SO_SNDBUF 0x1001 /* send buffer size */
|
|
|
|
#define SO_RCVBUF 0x1002 /* receive buffer size */
|
|
|
|
#define SO_SNDLOWAT 0x1003 /* send low-water mark */
|
|
|
|
#define SO_RCVLOWAT 0x1004 /* receive low-water mark */
|
|
|
|
#define SO_SNDTIMEO 0x1005 /* send timeout */
|
|
|
|
#define SO_RCVTIMEO 0x1006 /* receive timeout */
|
|
|
|
#define SO_ERROR 0x1007 /* get error status and clear */
|
|
|
|
#define SO_TYPE 0x1008 /* get socket type */
|
|
|
|
|
|
|
|
|
|
|
|
struct in_addr {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
unsigned char s_b1, s_b2, s_b3, s_b4;
|
|
|
|
} S_un_b;
|
|
|
|
struct {
|
|
|
|
unsigned short s_w1, s_w2;
|
|
|
|
} S_un_w;
|
2001-02-19 21:15:25 +00:00
|
|
|
unsigned long S_addr;
|
|
|
|
} S_un;
|
|
|
|
};
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
#define s_addr S_un.S_addr /* can be used for most tcp & ip code
|
|
|
|
*/
|
|
|
|
#define s_host S_un.S_un_b.s_b2 /* host on imp */
|
|
|
|
#define s_net S_un.S_un_b.s_b1 /* network */
|
|
|
|
#define s_imp S_un.S_un_w.s_w2 /* imp */
|
|
|
|
#define s_impno S_un.S_un_b.s_b4 /* imp # */
|
|
|
|
#define s_lh S_un.S_un_b.s_b3 /* logical host */
|
|
|
|
|
|
|
|
struct sockaddr_in {
|
|
|
|
short sin_family;
|
|
|
|
unsigned short sin_port;
|
|
|
|
struct in_addr sin_addr;
|
|
|
|
char sin_zero[8];
|
2001-02-19 21:15:25 +00:00
|
|
|
};
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
struct hostent {
|
|
|
|
char *h_name; /* official name of host */
|
|
|
|
char **h_aliases; /* alias list */
|
|
|
|
int h_addrtype; /* host address type */
|
|
|
|
int h_length; /* length of address */
|
|
|
|
char **h_addr_list; /* list of addresses from name server
|
|
|
|
*/
|
|
|
|
#define h_addr h_addr_list[0] /* address, for backward compatiblity
|
|
|
|
*/
|
2001-02-19 21:15:25 +00:00
|
|
|
};
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *inet_ntoa (struct in_addr in);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
// this section is B&W specific constants & structures
|
|
|
|
|
|
|
|
#define BW_IOCTL_BIND 0
|
|
|
|
#define BW_IOCTL_CLEAROPTIONS 5
|
|
|
|
#define BW_IOCTL_SETOPTIONS 6
|
|
|
|
#define BW_IOCTL_PEEK 7
|
|
|
|
#define BW_IOCTL_SETWINMASK 8
|
|
|
|
|
|
|
|
#define BW_OPTION_BLOCKING 0x01
|
|
|
|
#define BW_OPTION_REUSEBUFFERS 0x80
|
|
|
|
|
|
|
|
#define BW_ERR_USR_HANGUP 50
|
|
|
|
#define BW_ERR_HANGUP 51
|
|
|
|
#define BW_ERR_NET_ERR 52
|
|
|
|
#define BW_ERR_IS_CLOSED 53
|
|
|
|
#define BW_ERR_TIME_OUT 54
|
|
|
|
#define BW_ERR_RESET 55
|
|
|
|
#define BW_ERR_FULL 56
|
|
|
|
#define BW_ERR_BLOCK 57
|
|
|
|
#define BW_ERR_SHUTDOWN 58
|
|
|
|
|
|
|
|
#pragma pack(1)
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
char state; // always 1
|
|
|
|
short localPort;
|
|
|
|
struct in_addr localAddr;
|
|
|
|
char reason; // always 0
|
|
|
|
char options;
|
|
|
|
short dataAvailable;
|
2001-02-19 21:15:25 +00:00
|
|
|
} BW_UDPinfo_t;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
char reserved1[6];
|
|
|
|
unsigned short info2Offset;
|
|
|
|
char reserved2[18];
|
|
|
|
struct in_addr remoteAddr;
|
2001-02-19 21:15:25 +00:00
|
|
|
} BW_UDPreadInfo1_t;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
short remotePort;
|
|
|
|
char reserved1[2];
|
|
|
|
unsigned short dataLenPlus8;
|
|
|
|
char reserved2[2];
|
|
|
|
char data[1]; // actual size is <dataLenPlus8> - 8
|
|
|
|
//
|
2001-02-19 21:15:25 +00:00
|
|
|
} BW_UDPreadInfo2_t;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
char reserved1[2];
|
|
|
|
short remotePort;
|
|
|
|
unsigned short dataLen;
|
|
|
|
struct in_addr remoteAddr;
|
|
|
|
char reserved2[42];
|
|
|
|
char data[1]; // actual size is <datalen>
|
2001-02-19 21:15:25 +00:00
|
|
|
} BW_writeInfo_t;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
typedef struct {
|
|
|
|
short ioport;
|
|
|
|
byte dma;
|
|
|
|
byte vector;
|
|
|
|
byte irq;
|
|
|
|
short bufferSize;
|
|
|
|
short maxWindow;
|
|
|
|
short timeZone;
|
|
|
|
byte myType;
|
|
|
|
int inetAddr;
|
|
|
|
short value;
|
|
|
|
byte subnetMask;
|
|
|
|
short etherPointer;
|
|
|
|
short logserverPointer;
|
|
|
|
short nameserverPointer;
|
|
|
|
short printserverPointer;
|
|
|
|
short timeserverPointer;
|
|
|
|
short gatewayPointer;
|
|
|
|
short driverSegment;
|
|
|
|
byte transferSize;
|
|
|
|
char cardName[9];
|
2001-02-19 21:15:25 +00:00
|
|
|
} BW_ethdevinfo_t;
|
|
|
|
|
|
|
|
#pragma pack()
|
|
|
|
|
|
|
|
#define LOWMEM_SIZE 4096
|
|
|
|
|
|
|
|
static unsigned char *lowmem_buffer;
|
2001-02-26 06:48:02 +00:00
|
|
|
static int lowmem_bufseg;
|
|
|
|
static int lowmem_bufoff;
|
2001-02-19 21:15:25 +00:00
|
|
|
static BW_ethdevinfo_t ethdevinfo;
|
2001-02-26 06:48:02 +00:00
|
|
|
static int netmask;
|
2001-02-19 21:15:25 +00:00
|
|
|
static struct in_addr bcastaddr;
|
|
|
|
|
|
|
|
extern regs_t regs;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int net_acceptsocket = -1; // socket for fielding new
|
|
|
|
|
|
|
|
// connections
|
|
|
|
static int net_controlsocket = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#include "net_bw.h"
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
|
|
|
BW_ioctl (int s, char *msg, int msglen)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_memcpy (lowmem_buffer, msg, msglen);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
regs.x.ax = 0x4403;
|
|
|
|
regs.x.bx = s;
|
|
|
|
regs.x.cx = msglen;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
|
|
|
regs.x.ds = lowmem_bufseg;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21))
|
2001-02-19 21:15:25 +00:00
|
|
|
return regs.x.ax;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
|
|
|
BW_TranslateError (int error)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
switch (error) {
|
|
|
|
case BW_ERR_USR_HANGUP:
|
|
|
|
return ECONNABORTED;
|
|
|
|
case BW_ERR_HANGUP:
|
|
|
|
return EISCONN;
|
|
|
|
case BW_ERR_NET_ERR:
|
|
|
|
return ENOTCONN;
|
|
|
|
case BW_ERR_IS_CLOSED:
|
|
|
|
return ENOTCONN;
|
|
|
|
case BW_ERR_TIME_OUT:
|
|
|
|
return ETIMEDOUT;
|
|
|
|
case BW_ERR_RESET:
|
|
|
|
return ECONNREFUSED;
|
|
|
|
case BW_ERR_FULL:
|
|
|
|
return ETOOMANYREFS;
|
|
|
|
case BW_ERR_BLOCK:
|
|
|
|
return EWOULDBLOCK;
|
|
|
|
case BW_ERR_SHUTDOWN:
|
|
|
|
return ESHUTDOWN;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
|
|
|
GetEthdevinfo (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int fd;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_strcpy ((char *) lowmem_buffer, "ETHDEV27");
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.ax = 0x3d42;
|
|
|
|
regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21))
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
fd = regs.x.ax;
|
|
|
|
|
|
|
|
regs.x.ax = 0x4401;
|
|
|
|
regs.x.bx = fd;
|
|
|
|
regs.x.dx = 0x60;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
regs.h.ah = 0x3f;
|
2001-02-26 06:48:02 +00:00
|
|
|
regs.x.cx = sizeof (ethdevinfo);
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.es = regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
|
|
|
regs.x.bx = fd;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21))
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_memcpy (ðdevinfo, lowmem_buffer, regs.x.ax);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
regs.h.ah = 0x3e;
|
|
|
|
regs.x.bx = fd;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_Init (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
struct qsockaddr addr;
|
2001-02-26 06:48:02 +00:00
|
|
|
char *colon;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (COM_CheckParm ("-noudp"))
|
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
lowmem_buffer = dos_getmemory (LOWMEM_SIZE);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!lowmem_buffer)
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error ("not enough low memory\n");
|
|
|
|
lowmem_bufoff = ptr2real (lowmem_buffer) & 0xf;
|
|
|
|
lowmem_bufseg = ptr2real (lowmem_buffer) >> 4;
|
|
|
|
|
|
|
|
if (GetEthdevinfo ()) {
|
|
|
|
Con_DPrintf ("Beame & Whiteside TCP/IP not detected\n");
|
|
|
|
dos_freememory (lowmem_buffer);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
netmask = 0xffffffff >> (32 - ethdevinfo.subnetMask);
|
|
|
|
bcastaddr.s_addr = (ethdevinfo.inetAddr & netmask) | (~netmask);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((net_controlsocket = BW_OpenSocket (0)) == -1) {
|
|
|
|
dos_freememory (lowmem_buffer);
|
2001-02-19 21:15:25 +00:00
|
|
|
Con_DPrintf ("BW_Init unable to open control socket; disabled\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BW_GetSocketAddr (net_controlsocket, &addr);
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_strcpy (my_tcpip_address, BW_AddrToString (&addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
colon = Q_strrchr (my_tcpip_address, ':');
|
|
|
|
if (colon)
|
|
|
|
*colon = 0;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
Con_Printf ("BW_Init: UDP initialized\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
BW_Shutdown (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
BW_Listen (false);
|
|
|
|
BW_CloseSocket (net_controlsocket);
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_freememory (lowmem_buffer);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
BW_Listen (qboolean state)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
// enable listening
|
2001-02-26 06:48:02 +00:00
|
|
|
if (state) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (net_acceptsocket != -1)
|
|
|
|
return;
|
|
|
|
if ((net_acceptsocket = BW_OpenSocket (net_hostport)) == -1)
|
|
|
|
Sys_Error ("BW_Listen: Unable to open accept socket\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
|
|
|
BW_CloseSocket (net_acceptsocket);
|
|
|
|
net_acceptsocket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/*
|
|
|
|
OpenSocket returns a handle to a network socket that has been opened,
|
|
|
|
set to nonblocking, and bound to <port>. Additional socket options
|
|
|
|
should be set here if they are needed. -1 is returned on failure.
|
|
|
|
*/
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_OpenSocket (int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int s;
|
|
|
|
int ret;
|
|
|
|
int deadman = 3 * 1024;
|
|
|
|
static int dynamic = 1024;
|
|
|
|
static char reuse_msg[2] = { BW_IOCTL_SETOPTIONS, BW_OPTION_REUSEBUFFERS };
|
|
|
|
static char bind_msg[3] = { BW_IOCTL_BIND, 0, 0 };
|
|
|
|
static char nonblock_msg[2] = { BW_IOCTL_CLEAROPTIONS, BW_OPTION_BLOCKING };
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// allocate a UDP socket
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_strcpy ((char *) lowmem_buffer, "UDP-IP10");
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.ax = 0x3d42;
|
|
|
|
regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21)) {
|
|
|
|
Con_Printf ("BW_OpenSocket failed: %u\n",
|
|
|
|
BW_TranslateError (regs.x.ax));
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s = regs.x.ax;
|
|
|
|
|
|
|
|
// set file descriptor to raw mode
|
|
|
|
regs.x.ax = 0x4401;
|
|
|
|
regs.x.bx = s;
|
|
|
|
regs.x.dx = 0x60;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (BW_ioctl (s, reuse_msg, 2)) {
|
|
|
|
Con_Printf ("BW_OpenSocket ioctl(reuse) failed\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (BW_ioctl (s, nonblock_msg, 2)) {
|
|
|
|
Con_Printf ("BW_OpenSocket ioctl(nonblocking) failed\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// if a socket was specified, bind to it and return
|
2001-02-26 06:48:02 +00:00
|
|
|
if (port) {
|
|
|
|
*(short *) &bind_msg[1] = port;
|
|
|
|
if (BW_ioctl (s, bind_msg, 3)) {
|
|
|
|
BW_CloseSocket (s);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
// B&W does NOT do dynamic allocation, so if port == 0 we must fake it
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
port = dynamic++;
|
|
|
|
if (dynamic == 4096)
|
|
|
|
dynamic = 1024;
|
|
|
|
deadman--;
|
2001-02-26 06:48:02 +00:00
|
|
|
*(short *) &bind_msg[1] = port;
|
|
|
|
ret = BW_ioctl (s, bind_msg, 3);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
while (ret && deadman);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_CloseSocket (int socket)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
regs.h.ah = 0x3e;
|
|
|
|
regs.x.bx = socket;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21)) {
|
|
|
|
Con_Printf ("BW_CloseSocket %u failed: %u\n", socket,
|
|
|
|
BW_TranslateError (regs.x.ax));
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_Connect (int socket, struct qsockaddr *hostaddr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_CheckNewConnections (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (net_acceptsocket == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// see if there's anything waiting
|
|
|
|
regs.x.ax = 0x4406;
|
|
|
|
regs.x.bx = net_acceptsocket;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (regs.x.ax == 0)
|
|
|
|
return -1;
|
|
|
|
return net_acceptsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_Read (int s, byte * buf, int len, struct qsockaddr *from)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
BW_UDPreadInfo1_t *info1;
|
|
|
|
BW_UDPreadInfo2_t *info2;
|
|
|
|
|
|
|
|
// ask if there's anything waiting
|
|
|
|
regs.x.ax = 0x4406;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (regs.x.ax == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// there was, so let's get it
|
|
|
|
regs.h.ah = 0x3f;
|
|
|
|
regs.x.cx = /* len + 53 */ LOWMEM_SIZE;
|
|
|
|
regs.x.es = regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21)) {
|
|
|
|
Con_Printf ("BW UDP read error: %u\n", BW_TranslateError (regs.x.ax));
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
info1 = (BW_UDPreadInfo1_t *) lowmem_buffer;
|
|
|
|
info2 = (BW_UDPreadInfo2_t *) (lowmem_buffer + info1->info2Offset);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (from) {
|
2001-02-19 21:15:25 +00:00
|
|
|
from->sa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) from)->sin_addr = info1->remoteAddr;
|
|
|
|
((struct sockaddr_in *) from)->sin_port = htons (info2->remotePort);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = info2->dataLenPlus8 - 8;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (len > NET_DATAGRAMSIZE) {
|
|
|
|
Con_Printf ("BW UDP read packet too large: %u\n", len);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_memcpy (buf, info2->data, len);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_Broadcast (int s, byte * msg, int len)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
BW_writeInfo_t *writeInfo;
|
|
|
|
|
|
|
|
// ask if we're clear to send
|
|
|
|
regs.x.ax = 0x4407;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (regs.x.ax == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// yes, let's do it
|
2001-02-26 06:48:02 +00:00
|
|
|
writeInfo = (BW_writeInfo_t *) lowmem_buffer;
|
2001-02-19 21:15:25 +00:00
|
|
|
writeInfo->remoteAddr = bcastaddr;
|
|
|
|
writeInfo->remotePort = net_hostport;
|
|
|
|
writeInfo->dataLen = len;
|
|
|
|
if (len > NET_DATAGRAMSIZE)
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error ("BW UDP write packet too large: %u\n", len);
|
|
|
|
Q_memcpy (writeInfo->data, msg, len);
|
2001-02-19 21:15:25 +00:00
|
|
|
writeInfo->data[len] = 0;
|
|
|
|
regs.h.ah = 0x40;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
regs.x.cx = len + sizeof (BW_writeInfo_t);
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.es = regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21)) {
|
|
|
|
Con_Printf ("BW_Broadcast failed: %u\n", BW_TranslateError (regs.x.ax));
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_Write (int s, byte * msg, int len, struct qsockaddr *to)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
BW_writeInfo_t *writeInfo;
|
|
|
|
|
|
|
|
// ask if we're clear to send
|
|
|
|
regs.x.ax = 0x4407;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (regs.x.ax == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// yes, let's do it
|
2001-02-26 06:48:02 +00:00
|
|
|
writeInfo = (BW_writeInfo_t *) lowmem_buffer;
|
|
|
|
writeInfo->remoteAddr = ((struct sockaddr_in *) to)->sin_addr;
|
|
|
|
writeInfo->remotePort = ntohs (((struct sockaddr_in *) to)->sin_port);
|
2001-02-19 21:15:25 +00:00
|
|
|
writeInfo->dataLen = len;
|
|
|
|
if (len > NET_DATAGRAMSIZE)
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error ("BW UDP write packet too large: %u\n", len);
|
|
|
|
Q_memcpy (writeInfo->data, msg, len);
|
2001-02-19 21:15:25 +00:00
|
|
|
writeInfo->data[len] = 0;
|
|
|
|
regs.h.ah = 0x40;
|
|
|
|
regs.x.bx = s;
|
2001-02-26 06:48:02 +00:00
|
|
|
regs.x.cx = len + sizeof (BW_writeInfo_t);
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.es = regs.x.ds = lowmem_bufseg;
|
|
|
|
regs.x.dx = lowmem_bufoff;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (dos_int86 (0x21)) {
|
|
|
|
Con_Printf ("BW_Write failed: %u\n", BW_TranslateError (regs.x.ax));
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *
|
|
|
|
BW_AddrToString (struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
static char buffer[22];
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d:%d",
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_net,
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_host,
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_lh,
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_impno,
|
|
|
|
ntohs (((struct sockaddr_in *) addr)->sin_port)
|
2001-02-19 21:15:25 +00:00
|
|
|
);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_StringToAddr (char *string, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ha1, ha2, ha3, ha4, hp;
|
|
|
|
int ipaddr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
sscanf (string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
|
2001-02-19 21:15:25 +00:00
|
|
|
ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
|
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl (ipaddr);
|
|
|
|
((struct sockaddr_in *) addr)->sin_port = htons ((short) hp);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_GetSocketAddr (int socket, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
regs.x.ax = 0x4402;
|
|
|
|
regs.x.bx = socket;
|
2001-02-26 06:48:02 +00:00
|
|
|
regs.x.cx = sizeof (BW_UDPinfo_t);
|
2001-02-19 21:15:25 +00:00
|
|
|
regs.x.dx = lowmem_bufoff;
|
|
|
|
regs.x.ds = lowmem_bufseg;
|
2001-02-26 06:48:02 +00:00
|
|
|
dos_int86 (0x21);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_addr =
|
|
|
|
((BW_UDPinfo_t *) lowmem_buffer)->localAddr.s_addr;
|
|
|
|
((struct sockaddr_in *) addr)->sin_port =
|
|
|
|
htons (((BW_UDPinfo_t *) lowmem_buffer)->localPort);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_GetNameFromAddr (struct qsockaddr *addr, char *name)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_strcpy (name, BW_AddrToString (addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_GetAddrFromName (char *name, struct qsockaddr *hostaddr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
char *b;
|
|
|
|
int addr;
|
|
|
|
int num;
|
|
|
|
int mask;
|
|
|
|
int run;
|
|
|
|
int port;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (name[0] < '0' || name[0] > '9')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buff[0] = '.';
|
|
|
|
b = buff;
|
2001-02-26 06:48:02 +00:00
|
|
|
Q_strcpy (buff + 1, name);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (buff[1] == '.')
|
|
|
|
b++;
|
|
|
|
|
|
|
|
addr = 0;
|
|
|
|
mask = -1;
|
2001-02-26 06:48:02 +00:00
|
|
|
while (*b == '.') {
|
2001-02-19 21:15:25 +00:00
|
|
|
b++;
|
|
|
|
num = 0;
|
|
|
|
run = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
while (!(*b < '0' || *b > '9')) {
|
|
|
|
num = num * 10 + *b++ - '0';
|
|
|
|
if (++run > 3)
|
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
|
|
|
|
return -1;
|
|
|
|
if (num < 0 || num > 255)
|
|
|
|
return -1;
|
2001-02-26 06:48:02 +00:00
|
|
|
mask <<= 8;
|
|
|
|
addr = (addr << 8) + num;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
addr = htonl (addr);
|
|
|
|
mask = htonl (mask);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (*b++ == ':')
|
2001-02-26 06:48:02 +00:00
|
|
|
port = Q_atoi (b);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
|
|
|
hostaddr->sa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) hostaddr)->sin_port = htons ((short) port);
|
|
|
|
((struct sockaddr_in *) hostaddr)->sin_addr.s_addr =
|
|
|
|
((ethdevinfo.inetAddr & mask) | addr);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (addr1->sa_family != addr2->sa_family)
|
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (((struct sockaddr_in *) addr1)->sin_addr.s_addr !=
|
|
|
|
((struct sockaddr_in *) addr2)->sin_addr.s_addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (((struct sockaddr_in *) addr1)->sin_port !=
|
|
|
|
((struct sockaddr_in *) addr2)->sin_port)
|
2001-02-19 21:15:25 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_GetSocketPort (struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
return ntohs (((struct sockaddr_in *) addr)->sin_port);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
BW_SetSocketPort (struct qsockaddr *addr, int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) addr)->sin_port = htons (port);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|