2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 extern cvar_t hostname;
27 #define MAXHOSTNAMELEN 256
29 static int net_acceptsocket = -1; // socket for fielding new connections
30 static int net_controlsocket;
31 static int net_broadcastsocket = 0;
32 static struct qsockaddr broadcastaddr;
34 static unsigned long myAddr;
36 qboolean winsock_lib_initialized;
38 int (PASCAL FAR *pWSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
39 int (PASCAL FAR *pWSACleanup)(void);
40 int (PASCAL FAR *pWSAGetLastError)(void);
41 SOCKET (PASCAL FAR *psocket)(int af, int type, int protocol);
42 int (PASCAL FAR *pioctlsocket)(SOCKET s, long cmd, u_long FAR *argp);
43 int (PASCAL FAR *psetsockopt)(SOCKET s, int level, int optname,
44 const char FAR * optval, int optlen);
45 int (PASCAL FAR *precvfrom)(SOCKET s, char FAR * buf, int len, int flags,
46 struct sockaddr FAR *from, int FAR * fromlen);
47 int (PASCAL FAR *psendto)(SOCKET s, const char FAR * buf, int len, int flags,
48 const struct sockaddr FAR *to, int tolen);
49 int (PASCAL FAR *pclosesocket)(SOCKET s);
50 int (PASCAL FAR *pgethostname)(char FAR * name, int namelen);
51 struct hostent FAR * (PASCAL FAR *pgethostbyname)(const char FAR * name);
52 struct hostent FAR * (PASCAL FAR *pgethostbyaddr)(const char FAR * addr,
54 int (PASCAL FAR *pgetsockname)(SOCKET s, struct sockaddr FAR *name,
59 int winsock_initialized = 0;
62 //=============================================================================
64 static double blocktime;
66 BOOL PASCAL FAR BlockingHook(void)
71 if ((Sys_FloatTime() - blocktime) > 2.0)
73 WSACancelBlockingCall();
77 /* get the next message, if any */
78 ret = (BOOL) PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
80 /* if we got one, process it */
82 TranslateMessage(&msg);
83 DispatchMessage(&msg);
86 /* TRUE if we got a message */
91 void WINS_GetLocalAddress()
93 struct hostent *local = NULL;
94 char buff[MAXHOSTNAMELEN];
97 if (myAddr != INADDR_ANY)
100 if (pgethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
103 blocktime = Sys_FloatTime();
104 WSASetBlockingHook(BlockingHook);
105 local = pgethostbyname(buff);
106 WSAUnhookBlockingHook();
110 myAddr = *(int *)local->h_addr_list[0];
112 addr = ntohl(myAddr);
113 sprintf(my_tcpip_address, "%d.%d.%d.%d", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
120 char buff[MAXHOSTNAMELEN];
123 WORD wVersionRequested;
126 // initialize the Winsock function vectors (we do this instead of statically linking
127 // so we can run on Win 3.1, where there isn't necessarily Winsock)
128 hInst = LoadLibrary("wsock32.dll");
132 Con_SafePrintf ("Failed to load winsock.dll\n");
133 winsock_lib_initialized = false;
137 winsock_lib_initialized = true;
139 pWSAStartup = (void *)GetProcAddress(hInst, "WSAStartup");
140 pWSACleanup = (void *)GetProcAddress(hInst, "WSACleanup");
141 pWSAGetLastError = (void *)GetProcAddress(hInst, "WSAGetLastError");
142 psocket = (void *)GetProcAddress(hInst, "socket");
143 pioctlsocket = (void *)GetProcAddress(hInst, "ioctlsocket");
144 psetsockopt = (void *)GetProcAddress(hInst, "setsockopt");
145 precvfrom = (void *)GetProcAddress(hInst, "recvfrom");
146 psendto = (void *)GetProcAddress(hInst, "sendto");
147 pclosesocket = (void *)GetProcAddress(hInst, "closesocket");
148 pgethostname = (void *)GetProcAddress(hInst, "gethostname");
149 pgethostbyname = (void *)GetProcAddress(hInst, "gethostbyname");
150 pgethostbyaddr = (void *)GetProcAddress(hInst, "gethostbyaddr");
151 pgetsockname = (void *)GetProcAddress(hInst, "getsockname");
153 if (!pWSAStartup || !pWSACleanup || !pWSAGetLastError ||
154 !psocket || !pioctlsocket || !psetsockopt ||
155 !precvfrom || !psendto || !pclosesocket ||
156 !pgethostname || !pgethostbyname || !pgethostbyaddr ||
159 Con_SafePrintf ("Couldn't GetProcAddress from winsock.dll\n");
163 if (COM_CheckParm ("-noudp"))
166 if (winsock_initialized == 0)
168 wVersionRequested = MAKEWORD(1, 1);
170 r = pWSAStartup (MAKEWORD(1, 1), &winsockdata);
174 Con_SafePrintf ("Winsock initialization failed.\n");
178 winsock_initialized++;
181 if (pgethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
183 Con_DPrintf ("Winsock TCP/IP Initialization failed.\n");
184 if (--winsock_initialized == 0)
189 // if the quake hostname isn't set, set it to the machine name
190 if (strcmp(hostname.string, "UNNAMED") == 0)
192 // see if it's a text IP address (well, close enough)
193 for (p = buff; *p; p++)
194 if ((*p < '0' || *p > '9') && *p != '.')
197 // if it is a real name, strip off the domain; we only want the host
200 for (i = 0; i < 15; i++)
205 Cvar_Set ("hostname", buff);
208 i = COM_CheckParm ("-ip");
213 myAddr = inet_addr(com_argv[i+1]);
214 if (myAddr == INADDR_NONE)
215 Sys_Error ("%s is not a valid IP address", com_argv[i+1]);
216 strcpy(my_tcpip_address, com_argv[i+1]);
220 Sys_Error ("NET_Init: you must specify an IP address after -ip");
226 strcpy(my_tcpip_address, "INADDR_ANY");
229 if ((net_controlsocket = WINS_OpenSocket (0)) == -1)
231 Con_Printf("WINS_Init: Unable to open control socket\n");
232 if (--winsock_initialized == 0)
237 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
238 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
239 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
241 Con_Printf("Winsock TCP/IP Initialized\n");
242 tcpipAvailable = true;
244 return net_controlsocket;
247 //=============================================================================
249 void WINS_Shutdown (void)
252 WINS_CloseSocket (net_controlsocket);
253 if (--winsock_initialized == 0)
257 //=============================================================================
259 void WINS_Listen (qboolean state)
264 if (net_acceptsocket != -1)
266 WINS_GetLocalAddress();
267 if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
268 Sys_Error ("WINS_Listen: Unable to open accept socket\n");
273 if (net_acceptsocket == -1)
275 WINS_CloseSocket (net_acceptsocket);
276 net_acceptsocket = -1;
279 //=============================================================================
281 int WINS_OpenSocket (int port)
284 struct sockaddr_in address;
287 if ((newsocket = psocket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
290 if (pioctlsocket (newsocket, FIONBIO, &_true) == -1)
293 address.sin_family = AF_INET;
294 address.sin_addr.s_addr = myAddr;
295 address.sin_port = htons((unsigned short)port);
296 if( bind (newsocket, (void *)&address, sizeof(address)) == 0)
299 Sys_Error ("Unable to bind to %s", WINS_AddrToString((struct qsockaddr *)&address));
301 pclosesocket (newsocket);
305 //=============================================================================
307 int WINS_CloseSocket (int socket)
309 if (socket == net_broadcastsocket)
310 net_broadcastsocket = 0;
311 return pclosesocket (socket);
315 //=============================================================================
320 this lets you type only as much of the net address as required, using
321 the local network components to fill in the rest
324 static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
347 while (!( *b < '0' || *b > '9'))
349 num = num*10 + *b++ - '0';
353 if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
355 if (num < 0 || num > 255)
358 addr = (addr<<8) + num;
366 hostaddr->sa_family = AF_INET;
367 ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port);
368 ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
372 //=============================================================================
374 int WINS_Connect (int socket, struct qsockaddr *addr)
379 //=============================================================================
381 int WINS_CheckNewConnections (void)
385 if (net_acceptsocket == -1)
388 if (precvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) > 0)
390 return net_acceptsocket;
395 //=============================================================================
397 int WINS_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
399 int addrlen = sizeof (struct qsockaddr);
403 ret = precvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
406 errno = pWSAGetLastError();
408 if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
415 //=============================================================================
417 int WINS_MakeSocketBroadcastCapable (int socket)
421 // make this socket broadcast capable
422 if (psetsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
424 net_broadcastsocket = socket;
429 //=============================================================================
431 int WINS_Broadcast (int socket, byte *buf, int len)
435 if (socket != net_broadcastsocket)
437 if (net_broadcastsocket != 0)
438 Sys_Error("Attempted to use multiple broadcasts sockets\n");
439 WINS_GetLocalAddress();
440 ret = WINS_MakeSocketBroadcastCapable (socket);
443 Con_Printf("Unable to make socket broadcast capable\n");
448 return WINS_Write (socket, buf, len, &broadcastaddr);
451 //=============================================================================
453 int WINS_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
457 ret = psendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
459 if (pWSAGetLastError() == WSAEWOULDBLOCK)
465 //=============================================================================
467 char *WINS_AddrToString (struct qsockaddr *addr)
469 static char buffer[22];
472 haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
473 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));
477 //=============================================================================
479 int WINS_StringToAddr (char *string, struct qsockaddr *addr)
481 int ha1, ha2, ha3, ha4, hp;
484 sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
485 ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
487 addr->sa_family = AF_INET;
488 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
489 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
493 //=============================================================================
495 int WINS_GetSocketAddr (int socket, struct qsockaddr *addr)
497 int addrlen = sizeof(struct qsockaddr);
500 memset(addr, 0, sizeof(struct qsockaddr));
501 pgetsockname(socket, (struct sockaddr *)addr, &addrlen);
502 a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
503 if (a == 0 || a == inet_addr("127.0.0.1"))
504 ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
509 //=============================================================================
511 int WINS_GetNameFromAddr (struct qsockaddr *addr, char *name)
513 struct hostent *hostentry;
515 hostentry = pgethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
518 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
522 strcpy (name, WINS_AddrToString (addr));
526 //=============================================================================
528 int WINS_GetAddrFromName(char *name, struct qsockaddr *addr)
530 struct hostent *hostentry;
532 if (name[0] >= '0' && name[0] <= '9')
533 return PartialIPAddress (name, addr);
535 hostentry = pgethostbyname (name);
539 addr->sa_family = AF_INET;
540 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
541 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
546 //=============================================================================
548 int WINS_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
550 if (addr1->sa_family != addr2->sa_family)
553 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
556 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
562 //=============================================================================
564 int WINS_GetSocketPort (struct qsockaddr *addr)
566 return ntohs(((struct sockaddr_in *)addr)->sin_port);
570 int WINS_SetSocketPort (struct qsockaddr *addr, int port)
572 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
576 //=============================================================================