123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /*
- 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_mpath.c
- #include <dpmi.h>
- #include "quakedef.h"
- #include "mpdosock.h"
- short flat_selector;
- int WSAGetLastError(void);
- void sockets_flush(void);
- extern cvar_t hostname;
- #define MAXHOSTNAMELEN 256
- static int net_acceptsocket = -1; // socket for fielding new connections
- static int net_controlsocket;
- static int net_broadcastsocket = 0;
- //static qboolean ifbcastinit = false;
- static struct qsockaddr broadcastaddr;
- static unsigned long myAddr;
- #include "net_mp.h"
- //=============================================================================
- int MPATH_Init (void)
- {
- int i;
- struct hostent *local = NULL;
- char buff[MAXHOSTNAMELEN];
- struct qsockaddr addr;
- char *p;
- if (COM_CheckParm ("-mpath") == 0)
- return -1;
- flat_selector = __dpmi_allocate_ldt_descriptors(1);
- if (flat_selector == -1) {
- Con_Printf("MPATH_Init: Can't get flat selector\n");
- return -1;
- }
- if (__dpmi_set_segment_base_address(flat_selector, 0) == -1) {
- Con_Printf("MPATH_Init: Can't seg flat base!\n");
- return -1;
- }
- if (__dpmi_set_segment_limit(flat_selector, 0xffffffff) == -1) {
- Con_Printf("MPATH_Init: Can't set segment limit\n");
- return -1;
- }
- // determine my name & address
- if (gethostname(buff, MAXHOSTNAMELEN) == 0)
- local = gethostbyname(buff);
- if (local)
- {
- myAddr = *(int *)local->h_addr_list[0];
- // if the quake hostname isn't set, set it to the machine name
- if (Q_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);
- }
- }
- if ((net_controlsocket = MPATH_OpenSocket (0)) == -1)
- Sys_Error("MPATH_Init: Unable to open control socket\n");
- ((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(net_hostport);
- MPATH_GetSocketAddr (net_controlsocket, &addr);
- Q_strcpy(my_tcpip_address, MPATH_AddrToString (&addr));
- p = Q_strrchr (my_tcpip_address, ':');
- if (p)
- *p = 0;
- Con_Printf("MPath Initialized\n");
- tcpipAvailable = true;
- return net_controlsocket;
- }
- //=============================================================================
- void MPATH_Shutdown (void)
- {
- MPATH_Listen (false);
- MPATH_CloseSocket (net_controlsocket);
- }
- //=============================================================================
- void MPATH_Listen (qboolean state)
- {
- // enable listening
- if (state)
- {
- if (net_acceptsocket != -1)
- return;
- if ((net_acceptsocket = MPATH_OpenSocket (net_hostport)) == -1)
- Sys_Error ("MPATH_Listen: Unable to open accept socket\n");
- return;
- }
- // disable listening
- if (net_acceptsocket == -1)
- return;
- MPATH_CloseSocket (net_acceptsocket);
- net_acceptsocket = -1;
- }
- //=============================================================================
- int MPATH_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 = INADDR_ANY;
- address.sin_port = htons(port);
- if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
- goto ErrorReturn;
- return newsocket;
- ErrorReturn:
- closesocket (newsocket);
- return -1;
- }
- //=============================================================================
- int MPATH_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 (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 = Q_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 MPATH_Connect (int socket, struct qsockaddr *addr)
- {
- return 0;
- }
- //=============================================================================
- int MPATH_CheckNewConnections (void)
- {
- char buf[4];
- if (net_acceptsocket == -1)
- return -1;
- if (recvfrom (net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL) > 0)
- return net_acceptsocket;
- return -1;
- }
- //=============================================================================
- int MPATH_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
- {
- int addrlen = sizeof (struct qsockaddr);
- int ret;
- ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
- if (ret == -1)
- {
- int errno = WSAGetLastError();
- if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
- return 0;
- }
- return ret;
- }
- //=============================================================================
- int MPATH_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 MPATH_Broadcast (int socket, byte *buf, int len)
- {
- int ret;
- if (socket != net_broadcastsocket)
- {
- if (net_broadcastsocket != 0)
- Sys_Error("Attempted to use multiple broadcasts sockets\n");
- ret = MPATH_MakeSocketBroadcastCapable (socket);
- if (ret == -1)
- {
- Con_Printf("Unable to make socket broadcast capable\n");
- return ret;
- }
- }
- return MPATH_Write (socket, buf, len, &broadcastaddr);
- }
- //=============================================================================
- int MPATH_Write (int socket, byte *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;
- sockets_flush();
- return ret;
- }
- //=============================================================================
- char *MPATH_AddrToString (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 MPATH_StringToAddr (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(hp);
- return 0;
- }
- //=============================================================================
- int MPATH_GetSocketAddr (int socket, struct qsockaddr *addr)
- {
- int addrlen = sizeof(struct qsockaddr);
- unsigned int a;
- Q_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 MPATH_GetNameFromAddr (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)
- {
- Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
- return 0;
- }
- Q_strcpy (name, MPATH_AddrToString (addr));
- return 0;
- }
- //=============================================================================
- int MPATH_GetAddrFromName(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(net_hostport);
- ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
- return 0;
- }
- //=============================================================================
- int MPATH_AddrCompare (struct qsockaddr *addr1, 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 MPATH_GetSocketPort (struct qsockaddr *addr)
- {
- return ntohs(((struct sockaddr_in *)addr)->sin_port);
- }
- int MPATH_SetSocketPort (struct qsockaddr *addr, int port)
- {
- ((struct sockaddr_in *)addr)->sin_port = htons(port);
- return 0;
- }
- //=============================================================================
|