From: havoc Date: Fri, 28 Mar 2003 08:37:13 +0000 (+0000) Subject: net_wins.c is gone, winsock support merged into net_udp.c X-Git-Tag: xonotic-v0.1.0preview~6706 X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=commitdiff_plain;h=5fc5892a12b5e363a32ef7da5c2dc9cae5234f8f;ds=sidebyside net_wins.c is gone, winsock support merged into net_udp.c numerous other cleanups/changes also done to net_udp.c (to try to simplify it, hopefully work better, that kind of stuff) support for partial IP addresses has been removed (that is you say you can't simply type 'connect 1.123' and have it expand to xxx.xxx.1.123 where xxx.xxx came from your own IP address, however hostnames still work fine) also the automatic changing of the hostname cvar according to internet hostname has been removed, so servers will be called "UNNAMED" if they don't set it, rather than using their internet hostname git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2865 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/darkplaces.dsp b/darkplaces.dsp index 82014427..97e1d748 100644 --- a/darkplaces.dsp +++ b/darkplaces.dsp @@ -280,7 +280,7 @@ SOURCE=.\net_master.c # End Source File # Begin Source File -SOURCE=.\net_wins.c +SOURCE=.\net_udp.c # End Source File # Begin Source File diff --git a/makefile.mingw b/makefile.mingw index 1f9d3852..2b0d8fbb 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -40,7 +40,7 @@ builddate: .c.o: gcc $(CFLAGS) -c $*.c -darkplaces: $(OBJECTS) vid_wgl.o sys_win.o snd_win.o net_bsd.o net_wins.o cd_win.o conproc.o +darkplaces: $(OBJECTS) vid_wgl.o sys_win.o snd_win.o net_bsd.o net_udp.o cd_win.o conproc.o gcc -o $@ $^ $(LDFLAGS) diff --git a/makefile.mingwcross b/makefile.mingwcross index 4806003a..de72410a 100644 --- a/makefile.mingwcross +++ b/makefile.mingwcross @@ -42,8 +42,8 @@ SHAREDOBJECTS= builddate.o cmd.o collision.o common.o crc.o cvar.o \ OBJ_COMMON= $(CLIENTOBJECTS) $(SERVEROBJECTS) $(SHAREDOBJECTS) -OBJ_WGL= vid_wgl.o $(OBJ_CD) $(OBJ_SND) net_bsd.o net_wins.o sys_win.o -OBJ_DED= vid_null.o cd_null.o snd_null.o net_bsd.o net_wins.o sys_win.o +OBJ_WGL= vid_wgl.o $(OBJ_CD) $(OBJ_SND) net_bsd.o net_udp.o sys_win.o +OBJ_DED= vid_null.o cd_null.o snd_null.o net_bsd.o net_udp.o sys_win.o # Compilation diff --git a/net_udp.c b/net_udp.c index 480901f4..01a1d301 100644 --- a/net_udp.c +++ b/net_udp.c @@ -8,7 +8,7 @@ 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. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -17,13 +17,17 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// net_udp.c - #include "quakedef.h" - +#include "net_udp.h" +#ifdef WIN32 +#include "winquake.h" +#define MAXHOSTNAMELEN 256 +#else +#include #include #include #include +#include #include #include #include @@ -36,59 +40,68 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef NeXT #include #endif - -extern int gethostname (char *, int); -extern int close (int); +#endif static int net_acceptsocket = -1; // socket for fielding new connections static int net_controlsocket; static int net_broadcastsocket = 0; static struct qsockaddr broadcastaddr; -static unsigned long myAddr; - -#include "net_udp.h" +static union {int i;unsigned char d[4];} myAddr; //============================================================================= +#ifdef WIN32 +WSADATA winsockdata; +#endif + int UDP_Init (void) { - struct hostent *local; - char buff[MAXHOSTNAMELEN]; - struct qsockaddr addr; - char *colon; - + int i; + struct hostent *local = NULL; + char buff[MAXHOSTNAMELEN]; + if (COM_CheckParm ("-noudp")) return -1; - // determine my name & address - gethostname(buff, MAXHOSTNAMELEN); - local = gethostbyname(buff); - if (!local) - myAddr = htonl(INADDR_LOOPBACK); // default to the loopback address - else - myAddr = *(int *)local->h_addr_list[0]; - - // LordHavoc FIXME: get rid of this someday, just leave machines unnamed - // if the quake hostname isn't set, set it to the machine name - if (strcmp(hostname.string, "UNNAMED") == 0) +#ifdef WIN32 + if (WSAStartup (MAKEWORD(1, 1), &winsockdata)) + { + Con_SafePrintf ("Winsock initialization failed.\n"); + return -1; + } +#endif + + // loopback as a worst case fallback + myAddr.d[0] = 127;myAddr.d[1] = 0;myAddr.d[2] = 0;myAddr.d[3] = 1; + + if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc) + { + myAddr.i = inet_addr(com_argv[i+1]); + Con_Printf("Binding to IP Interface Address of %i.%i.%i.%i\n", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]); + } + else if (gethostname(buff, MAXHOSTNAMELEN) != -1) { - buff[15] = 0; - Cvar_Set ("hostname", buff); + buff[MAXHOSTNAMELEN - 1] = 0; + local = gethostbyname(buff); + if (local != NULL) + myAddr.i = *((int *)local->h_addr_list[0]); } + sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]); + if ((net_controlsocket = UDP_OpenSocket (0)) == -1) - Sys_Error("UDP_Init: Unable to open control socket\n"); + { + Con_Printf("UDP_Init: Unable to open control socket\n"); +#ifdef WIN32 + WSACleanup (); +#endif + return -1; + } ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET; ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST); - ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport); - - UDP_GetSocketAddr (net_controlsocket, &addr); - strcpy(my_tcpip_address, UDP_AddrToString (&addr)); - colon = strrchr (my_tcpip_address, ':'); - if (colon) - *colon = 0; + ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport); Con_Printf("UDP Initialized\n"); tcpipAvailable = true; @@ -102,6 +115,9 @@ void UDP_Shutdown (void) { UDP_Listen (false); UDP_CloseSocket (net_controlsocket); +#ifdef WIN32 + WSACleanup (); +#endif } //============================================================================= @@ -131,94 +147,55 @@ int UDP_OpenSocket (int port) { int newsocket; struct sockaddr_in address; - qboolean _true = true; if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) return -1; - if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1) - goto ErrorReturn; + { +#ifdef WIN32 + u_long _true = 1; + if (ioctlsocket (newsocket, FIONBIO, &_true) == -1) + { + closesocket (newsocket); +#else + char _true = 1; + if (ioctl (newsocket, FIONBIO, &_true) == -1) + { + close (newsocket); +#endif + Sys_Error("UDP_OpenSocket: unable to do a ioctl FIONBIO on the socket\n"); + } + } address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(port); - if( bind (newsocket, (void *)&address, sizeof(address)) == -1) - goto ErrorReturn; + address.sin_addr.s_addr = myAddr.i; + address.sin_port = htons((unsigned short)port); + if (bind(newsocket, (void *)&address, sizeof(address)) == -1) + { +#ifdef WIN32 + closesocket(newsocket); +#else + close(newsocket); +#endif + Sys_Error ("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address)); + } return newsocket; - -ErrorReturn: - close (newsocket); - return -1; } //============================================================================= int UDP_CloseSocket (int socket) { - if (socket == net_broadcastsocket) + if (net_broadcastsocket == socket) net_broadcastsocket = 0; +#ifdef WIN32 + return closesocket (socket); +#else return close (socket); +#endif } - -//============================================================================= -/* -============ -PartialIPAddress - -this lets you type only as much of the net address as required, using -the local network components to fill in the rest -============ -*/ -// LordHavoc FIXME: this whole function is stupid -static int PartialIPAddress (const char *in, struct qsockaddr *hostaddr) -{ - // LordHavoc FIXME: buff is stupid, it just ensures the address begins with a . for the parser - char buff[256]; - char *b; - int addr; - int num; - int mask; - int run; - int port; - - buff[0] = '.'; - b = buff; - strcpy(buff+1, in); - if (buff[1] == '.') - b++; - - addr = 0; - mask=-1; - while (*b == '.') - { - b++; - num = 0; - run = 0; - while (*b >= '0' && *b <= '9') - { - num = num*10 + *b++ - '0'; - if (num > 255) - return -1; - } - if (*b != '.' && *b != ':' && *b != 0) - return -1; - mask<<=8; - addr = (addr<<8) + num; - } - - if (*b++ == ':') - port = atoi(b); - else - port = net_hostport; - - hostaddr->sa_family = AF_INET; - ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port); - ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr); - - return 0; -} //============================================================================= int UDP_Connect (int socket, struct qsockaddr *addr) @@ -230,19 +207,26 @@ int UDP_Connect (int socket, struct qsockaddr *addr) int UDP_CheckNewConnections (void) { + char buf[4096]; +#ifndef WIN32 unsigned long available; struct sockaddr_in from; socklen_t fromlen; - char buff[1]; +#endif if (net_acceptsocket == -1) return -1; +#ifdef WIN32 + if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0) + return net_acceptsocket; +#else if (ioctl (net_acceptsocket, FIONREAD, &available) == -1) Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n"); if (available) return net_acceptsocket; - recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen); + recvfrom (net_acceptsocket, buf, 0, 0, (struct sockaddr *) &from, &fromlen); +#endif return -1; } @@ -268,8 +252,19 @@ int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr) int ret; ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen); - if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED)) - return 0; + if (ret == -1) + { +#ifdef WIN32 + int e = WSAGetLastError(); + if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED) + return 0; + Con_Printf("UDP_Read: WASGetLastError == %i\n", e); +#else + if (errno == EWOULDBLOCK || errno == ECONNREFUSED) + return 0; + Con_Printf("UDP_Read: errno == %i (%s)\n", errno, strerror(errno)); +#endif + } return ret; } @@ -277,7 +272,7 @@ int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr) int UDP_MakeSocketBroadcastCapable (int socket) { - int i = 1; + int i = 1; // make this socket broadcast capable if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0) @@ -315,8 +310,19 @@ int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr) int ret; ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr)); - if (ret == -1 && errno == EWOULDBLOCK) - return 0; + if (ret == -1) + { +#ifdef WIN32 + int e = WSAGetLastError(); + if (e == WSAEWOULDBLOCK) + return 0; + Con_Printf("UDP_Write: WASGetLastError == %i\n", e); +#else + if (errno == EWOULDBLOCK) + return 0; + Con_Printf("UDP_Write: errno == %i (%s)\n", errno, strerror(errno)); +#endif + } return ret; } @@ -324,11 +330,9 @@ int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr) char *UDP_AddrToString (const struct qsockaddr *addr) { - static char buffer[22]; - int haddr; - - haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr); - sprintf(buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port)); + static char buffer[22]; // only 22 needed (3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 5 + null) + unsigned char *ip = (char *)(&((struct sockaddr_in *)addr)->sin_addr.s_addr); + sprintf(buffer, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], ntohs(((struct sockaddr_in *)addr)->sin_port)); return buffer; } @@ -344,25 +348,28 @@ int UDP_StringToAddr (const char *string, struct qsockaddr *addr) addr->sa_family = AF_INET; ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr); - ((struct sockaddr_in *)addr)->sin_port = htons(hp); + ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp); return 0; } //============================================================================= -unsigned long inet_addr(const char *cp); int UDP_GetSocketAddr (int socket, struct qsockaddr *addr) { + int ret; int addrlen = sizeof(struct qsockaddr); - unsigned int a; - memset(addr, 0, sizeof(struct qsockaddr)); - getsockname(socket, (struct sockaddr *)addr, &addrlen); - a = ((struct sockaddr_in *)addr)->sin_addr.s_addr; - if (a == 0 || a == inet_addr("127.0.0.1")) - ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr; - - return 0; + ret = getsockname(socket, (struct sockaddr *)addr, &addrlen); + if (ret == -1) + { +#ifdef WIN32 + int e = WSAGetLastError(); + Con_Printf("UDP_GetSocketAddr: WASGetLastError == %i\n", e); +#else + Con_Printf("UDP_GetSocketAddr: errno == %i (%s)\n", errno, strerror(errno)); +#endif + } + return ret; } //============================================================================= @@ -389,14 +396,14 @@ int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr) struct hostent *hostentry; if (name[0] >= '0' && name[0] <= '9') - return PartialIPAddress (name, addr); + return UDP_StringToAddr (name, addr); hostentry = gethostbyname (name); if (!hostentry) return -1; addr->sa_family = AF_INET; - ((struct sockaddr_in *)addr)->sin_port = htons(net_hostport); + ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport); ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0]; return 0; @@ -428,7 +435,7 @@ int UDP_GetSocketPort (struct qsockaddr *addr) int UDP_SetSocketPort (struct qsockaddr *addr, int port) { - ((struct sockaddr_in *)addr)->sin_port = htons(port); + ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port); return 0; } diff --git a/net_wins.c b/net_wins.c deleted file mode 100644 index 0468e384..00000000 --- a/net_wins.c +++ /dev/null @@ -1,514 +0,0 @@ -/* -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 the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ -// net_wins.c - -#include "quakedef.h" -#include "winquake.h" - -#define MAXHOSTNAMELEN 256 - -static int net_acceptsocket = -1; // socket for fielding new connections -static int net_controlsocket; -static int net_broadcastsocket = 0; -static struct qsockaddr broadcastaddr; - -static unsigned long myAddr; - -#include "net_udp.h" - -WSADATA winsockdata; - -//============================================================================= - -static double blocktime; - -BOOL PASCAL FAR BlockingHook(void) -{ - MSG msg; - BOOL ret; - - if ((Sys_DoubleTime() - blocktime) > 2.0) - { - WSACancelBlockingCall(); - return false; - } - - /* get the next message, if any */ - ret = (BOOL) PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); - - /* if we got one, process it */ - if (ret) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - - /* true if we got a message */ - return ret; -} - - -void UDP_GetLocalAddress(void) -{ - struct hostent *local = NULL; - char buff[MAXHOSTNAMELEN]; - unsigned long addr; - - if (myAddr != INADDR_ANY) - return; - - if (gethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR) - return; - - blocktime = Sys_DoubleTime(); - WSASetBlockingHook(BlockingHook); - local = gethostbyname(buff); - WSAUnhookBlockingHook(); - if (local == NULL) - return; - - myAddr = *(int *)local->h_addr_list[0]; - - addr = ntohl(myAddr); - sprintf(my_tcpip_address, "%d.%d.%d.%d", (int) ((addr >> 24) & 0xff), (int) ((addr >> 16) & 0xff), (int) ((addr >> 8) & 0xff), (int) (addr & 0xff)); -} - - -int UDP_Init (void) -{ - int i; - char buff[MAXHOSTNAMELEN]; - char *p; - int r; - - if (COM_CheckParm ("-noudp")) - return -1; - - r = WSAStartup (MAKEWORD(1, 1), &winsockdata); - if (r) - { - Con_SafePrintf ("Winsock initialization failed.\n"); - return -1; - } - - // determine my name - if (gethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR) - { - Con_DPrintf ("Winsock TCP/IP Initialization failed.\n"); - WSACleanup (); - return -1; - } - - // if the quake hostname isn't set, set it to the machine name - if (strcmp(hostname.string, "UNNAMED") == 0) - { - // see if it's a text IP address (well, close enough) - for (p = buff; *p; p++) - if ((*p < '0' || *p > '9') && *p != '.') - break; - - // if it is a real name, strip off the domain; we only want the host - if (*p) - { - for (i = 0; i < 15; i++) - if (buff[i] == '.') - break; - buff[i] = 0; - } - Cvar_Set ("hostname", buff); - } - - i = COM_CheckParm ("-ip"); - if (i) - { - if (i < com_argc-1) - { - myAddr = inet_addr(com_argv[i+1]); - if (myAddr == INADDR_NONE) - Sys_Error ("%s is not a valid IP address", com_argv[i+1]); - strcpy(my_tcpip_address, com_argv[i+1]); - } - else - { - Sys_Error ("NET_Init: you must specify an IP address after -ip"); - } - } - else - { - myAddr = INADDR_ANY; - strcpy(my_tcpip_address, "INADDR_ANY"); - } - - if ((net_controlsocket = UDP_OpenSocket (0)) == -1) - { - Con_Printf("UDP_Init: Unable to open control socket\n"); - WSACleanup (); - return -1; - } - - ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET; - ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST; - ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport); - - Con_Printf("Winsock TCP/IP Initialized\n"); - tcpipAvailable = true; - - return net_controlsocket; -} - -//============================================================================= - -void UDP_Shutdown (void) -{ - UDP_Listen (false); - UDP_CloseSocket (net_controlsocket); - WSACleanup (); -} - -//============================================================================= - -void UDP_Listen (qboolean state) -{ - // enable listening - if (state) - { - if (net_acceptsocket != -1) - return; - UDP_GetLocalAddress(); - if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1) - Sys_Error ("UDP_Listen: Unable to open accept socket\n"); - return; - } - - // disable listening - if (net_acceptsocket == -1) - return; - UDP_CloseSocket (net_acceptsocket); - net_acceptsocket = -1; -} - -//============================================================================= - -int UDP_OpenSocket (int port) -{ - int newsocket; - struct sockaddr_in address; - u_long _true = 1; - - if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) - return -1; - - if (ioctlsocket (newsocket, FIONBIO, &_true) == -1) - goto ErrorReturn; - - address.sin_family = AF_INET; - address.sin_addr.s_addr = myAddr; - address.sin_port = htons((unsigned short)port); - if( bind (newsocket, (void *)&address, sizeof(address)) == 0) - return newsocket; - - Sys_Error ("Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address)); -ErrorReturn: - closesocket (newsocket); - return -1; -} - -//============================================================================= - -int UDP_CloseSocket (int socket) -{ - if (socket == net_broadcastsocket) - net_broadcastsocket = 0; - return closesocket (socket); -} - - -//============================================================================= -/* -============ -PartialIPAddress - -this lets you type only as much of the net address as required, using -the local network components to fill in the rest -============ -*/ -static int PartialIPAddress (const char *in, struct qsockaddr *hostaddr) -{ - char buff[256]; - char *b; - int addr; - int num; - int mask; - int run; - int port; - - buff[0] = '.'; - b = buff; - strcpy(buff+1, in); - if (buff[1] == '.') - b++; - - addr = 0; - mask=-1; - while (*b == '.') - { - b++; - num = 0; - run = 0; - while (!( *b < '0' || *b > '9')) - { - num = num*10 + *b++ - '0'; - if (++run > 3) - return -1; - } - if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0) - return -1; - if (num < 0 || num > 255) - return -1; - mask<<=8; - addr = (addr<<8) + num; - } - - if (*b++ == ':') - port = atoi(b); - else - port = net_hostport; - - hostaddr->sa_family = AF_INET; - ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port); - ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr); - - return 0; -} -//============================================================================= - -int UDP_Connect (int socket, struct qsockaddr *addr) -{ - return 0; -} - -//============================================================================= - -int UDP_CheckNewConnections (void) -{ - char buf[4096]; - - if (net_acceptsocket == -1) - return -1; - - if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0) - return net_acceptsocket; - return -1; -} - -//============================================================================= - -int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr) -{ - return UDP_Read (net_acceptsocket, buf, len, addr); -} - -//============================================================================= - -int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr) -{ - return UDP_Write (net_acceptsocket, buf, len, addr); -} - -//============================================================================= - -int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr) -{ - int addrlen = sizeof (struct qsockaddr); - int ret; - int errno; - - ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen); - if (ret == -1) - { - errno = WSAGetLastError(); - - if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED) - return 0; - - } - return ret; -} - -//============================================================================= - -int UDP_MakeSocketBroadcastCapable (int socket) -{ - int i = 1; - - // make this socket broadcast capable - if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0) - return -1; - net_broadcastsocket = socket; - - return 0; -} - -//============================================================================= - -int UDP_Broadcast (int socket, qbyte *buf, int len) -{ - int ret; - - if (socket != net_broadcastsocket) - { - if (net_broadcastsocket != 0) - Sys_Error("Attempted to use multiple broadcasts sockets\n"); - UDP_GetLocalAddress(); - ret = UDP_MakeSocketBroadcastCapable (socket); - if (ret == -1) - { - Con_Printf("Unable to make socket broadcast capable\n"); - return ret; - } - } - - return UDP_Write (socket, buf, len, &broadcastaddr); -} - -//============================================================================= - -int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr) -{ - int ret; - - ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr)); - if (ret == -1) - if (WSAGetLastError() == WSAEWOULDBLOCK) - return 0; - - return ret; -} - -//============================================================================= - -char *UDP_AddrToString (const struct qsockaddr *addr) -{ - static char buffer[22]; - int haddr; - - haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr); - sprintf(buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port)); - return buffer; -} - -//============================================================================= - -int UDP_StringToAddr (const char *string, struct qsockaddr *addr) -{ - int ha1, ha2, ha3, ha4, hp; - int ipaddr; - - sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp); - ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4; - - addr->sa_family = AF_INET; - ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr); - ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp); - return 0; -} - -//============================================================================= - -int UDP_GetSocketAddr (int socket, struct qsockaddr *addr) -{ - int addrlen = sizeof(struct qsockaddr); - unsigned int a; - - memset(addr, 0, sizeof(struct qsockaddr)); - getsockname(socket, (struct sockaddr *)addr, &addrlen); - a = ((struct sockaddr_in *)addr)->sin_addr.s_addr; - if (a == 0 || a == inet_addr("127.0.0.1")) - ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr; - - return 0; -} - -//============================================================================= - -int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name) -{ - struct hostent *hostentry; - - hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET); - if (hostentry) - { - strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1); - return 0; - } - - strcpy (name, UDP_AddrToString (addr)); - return 0; -} - -//============================================================================= - -int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr) -{ - struct hostent *hostentry; - - if (name[0] >= '0' && name[0] <= '9') - return PartialIPAddress (name, addr); - - hostentry = gethostbyname (name); - if (!hostentry) - return -1; - - addr->sa_family = AF_INET; - ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport); - ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0]; - - return 0; -} - -//============================================================================= - -int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2) -{ - if (addr1->sa_family != addr2->sa_family) - return -1; - - if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr) - return -1; - - if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port) - return 1; - - return 0; -} - -//============================================================================= - -int UDP_GetSocketPort (struct qsockaddr *addr) -{ - return ntohs(((struct sockaddr_in *)addr)->sin_port); -} - - -int UDP_SetSocketPort (struct qsockaddr *addr, int port) -{ - ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port); - return 0; -} -