2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //===========================================================================
26 // Programmer: MrElusive
30 //===========================================================================
37 #include "l_net_wins.h"
38 //#include <winsock.h>
39 //#include "mpdosock.h"
41 #define WinError WinPrint
43 typedef struct tag_error_struct
49 #define NET_NAMELEN 64
51 char my_tcpip_address[NET_NAMELEN];
53 const int DEFAULTnet_hostport = 26000;
55 const int MAXHOSTNAMELEN = 256;
57 static int net_acceptsocket = -1; // socket for fielding new connections
58 static int net_controlsocket;
59 static int net_hostport; // udp port number for acceptsocket
60 static int net_broadcastsocket = 0;
61 //static qboolean ifbcastinit = qfalse;
62 static struct sockaddr_s broadcastaddr;
64 static unsigned long myAddr;
68 ERROR_STRUCT errlist[] = {
69 {WSAEINTR, "WSAEINTR - Interrupted"},
70 {WSAEBADF, "WSAEBADF - Bad file number"},
71 {WSAEFAULT, "WSAEFAULT - Bad address"},
72 {WSAEINVAL, "WSAEINVAL - Invalid argument"},
73 {WSAEMFILE, "WSAEMFILE - Too many open files"},
76 * Windows Sockets definitions of regular Berkeley error constants
79 {WSAEWOULDBLOCK, "WSAEWOULDBLOCK - Socket marked as non-blocking"},
80 {WSAEINPROGRESS, "WSAEINPROGRESS - Blocking call in progress"},
81 {WSAEALREADY, "WSAEALREADY - Command already completed"},
82 {WSAENOTSOCK, "WSAENOTSOCK - Descriptor is not a socket"},
83 {WSAEDESTADDRREQ, "WSAEDESTADDRREQ - Destination address required"},
84 {WSAEMSGSIZE, "WSAEMSGSIZE - Data size too large"},
85 {WSAEPROTOTYPE, "WSAEPROTOTYPE - Protocol is of wrong type for this socket"},
86 {WSAENOPROTOOPT, "WSAENOPROTOOPT - Protocol option not supported for this socket type"},
87 {WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT - Protocol is not supported"},
88 {WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT - Socket type not supported by this address family"},
89 {WSAEOPNOTSUPP, "WSAEOPNOTSUPP - Option not supported"},
90 {WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT - "},
91 {WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT - Address family not supported by this protocol"},
92 {WSAEADDRINUSE, "WSAEADDRINUSE - Address is in use"},
93 {WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL - Address not available from local machine"},
94 {WSAENETDOWN, "WSAENETDOWN - Network subsystem is down"},
95 {WSAENETUNREACH, "WSAENETUNREACH - Network cannot be reached"},
96 {WSAENETRESET, "WSAENETRESET - Connection has been dropped"},
97 {WSAECONNABORTED, "WSAECONNABORTED - Connection aborted"},
98 {WSAECONNRESET, "WSAECONNRESET - Connection reset"},
99 {WSAENOBUFS, "WSAENOBUFS - No buffer space available"},
100 {WSAEISCONN, "WSAEISCONN - Socket is already connected"},
101 {WSAENOTCONN, "WSAENOTCONN - Socket is not connected"},
102 {WSAESHUTDOWN, "WSAESHUTDOWN - Socket has been shut down"},
103 {WSAETOOMANYREFS, "WSAETOOMANYREFS - Too many references"},
104 {WSAETIMEDOUT, "WSAETIMEDOUT - Command timed out"},
105 {WSAECONNREFUSED, "WSAECONNREFUSED - Connection refused"},
106 {WSAELOOP, "WSAELOOP - "},
107 {WSAENAMETOOLONG, "WSAENAMETOOLONG - "},
108 {WSAEHOSTDOWN, "WSAEHOSTDOWN - Host is down"},
109 {WSAEHOSTUNREACH, "WSAEHOSTUNREACH - "},
110 {WSAENOTEMPTY, "WSAENOTEMPTY - "},
111 {WSAEPROCLIM, "WSAEPROCLIM - "},
112 {WSAEUSERS, "WSAEUSERS - "},
113 {WSAEDQUOT, "WSAEDQUOT - "},
114 {WSAESTALE, "WSAESTALE - "},
115 {WSAEREMOTE, "WSAEREMOTE - "},
118 * Extended Windows Sockets error constant definitions
121 {WSASYSNOTREADY, "WSASYSNOTREADY - Network subsystem not ready"},
122 {WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED - Version not supported"},
123 {WSANOTINITIALISED, "WSANOTINITIALISED - WSAStartup() has not been successfully called"},
126 * Other error constants.
129 {WSAHOST_NOT_FOUND, "WSAHOST_NOT_FOUND - Host not found"},
130 {WSATRY_AGAIN, "WSATRY_AGAIN - Host not found or SERVERFAIL"},
131 {WSANO_RECOVERY, "WSANO_RECOVERY - Non-recoverable error"},
132 {WSANO_DATA, "WSANO_DATA - (or WSANO_ADDRESS) - No data record of requested type"},
136 //===========================================================================
140 // Changes Globals: -
141 //===========================================================================
142 const char *WINS_ErrorMessage( int error ){
146 return "No error occurred";
149 for ( search = 0; errlist[search].errstr; search++ )
151 if ( error == errlist[search].errnum ) {
152 return errlist[search].errstr;
156 return "Unknown error";
157 } //end of the function WINS_ErrorMessage
158 //===========================================================================
162 // Changes Globals: -
163 //===========================================================================
164 int WINS_Init( void ){
166 struct hostent *local;
167 char buff[MAXHOSTNAMELEN];
168 struct sockaddr_s addr;
171 WORD wVersionRequested;
173 wVersionRequested = MAKEWORD( 1, 1 );
175 r = WSAStartup( wVersionRequested, &winsockdata );
178 WinPrint( "Winsock initialization failed.\n" );
183 i = COM_CheckParm ("-udpport");
185 net_hostport = DEFAULTnet_hostport;
187 else if (i < com_argc-1)
188 net_hostport = Q_atoi (com_argv[i+1]);
190 Sys_Error ("WINS_Init: you must specify a number after -udpport");
193 // determine my name & address
194 gethostname( buff, MAXHOSTNAMELEN );
195 local = gethostbyname( buff );
196 if(local && local->h_addr_list && local->h_addr_list[0])
197 myAddr = *(int *)local->h_addr_list[0];
199 myAddr = inet_addr("127.0.0.1");
201 // if the quake hostname isn't set, set it to the machine name
202 // if (Q_strcmp(hostname.string, "UNNAMED") == 0)
204 // see if it's a text IP address (well, close enough)
205 for ( p = buff; *p; p++ )
206 if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
210 // if it is a real name, strip off the domain; we only want the host
212 for ( i = 0; i < 15; i++ )
213 if ( buff[i] == '.' ) {
218 // Cvar_Set ("hostname", buff);
221 if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == -1 ) {
222 WinError( "WINS_Init: Unable to open control socket\n" );
225 ( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
226 ( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
227 ( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );
229 WINS_GetSocketAddr( net_controlsocket, &addr );
230 strcpy( my_tcpip_address, WINS_AddrToString( &addr ) );
231 p = strrchr( my_tcpip_address, ':' );
235 WinPrint( "Winsock Initialized\n" );
237 return net_controlsocket;
238 } //end of the function WINS_Init
239 //===========================================================================
243 // Changes Globals: -
244 //===========================================================================
245 char *WINS_MyAddress( void ){
246 return my_tcpip_address;
247 } //end of the function WINS_MyAddress
248 //===========================================================================
252 // Changes Globals: -
253 //===========================================================================
254 void WINS_Shutdown( void ){
256 WINS_CloseSocket( net_controlsocket );
259 //WinPrint("Winsock Shutdown\n");
260 } //end of the function WINS_Shutdown
261 //===========================================================================
265 // Changes Globals: -
266 //===========================================================================
268 void WINS_Listen(int state)
273 if (net_acceptsocket != -1)
275 if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
276 WinError ("WINS_Listen: Unable to open accept socket\n");
281 if (net_acceptsocket == -1)
283 WINS_CloseSocket (net_acceptsocket);
284 net_acceptsocket = -1;
285 } //end of the function WINS_Listen*/
286 //===========================================================================
290 // Changes Globals: -
291 //===========================================================================
292 int WINS_OpenSocket( int port ){
294 struct sockaddr_in address;
297 if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
298 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
302 if ( ioctlsocket( newsocket, FIONBIO, &_true ) == -1 ) {
303 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
304 closesocket( newsocket );
308 memset( (char *) &address, 0, sizeof( address ) );
309 address.sin_family = AF_INET;
310 address.sin_addr.s_addr = INADDR_ANY;
311 address.sin_port = htons( (u_short)port );
312 if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
313 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
314 closesocket( newsocket );
319 } //end of the function WINS_OpenSocket
320 //===========================================================================
324 // Changes Globals: -
325 //===========================================================================
326 int WINS_OpenReliableSocket( int port ){
328 struct sockaddr_in address;
329 BOOL _true = 0xFFFFFFFF;
333 if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
334 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
338 memset( (char *) &address, 0, sizeof( address ) );
339 address.sin_family = AF_INET;
340 address.sin_addr.s_addr = htonl( INADDR_ANY );
341 address.sin_port = htons( (u_short)port );
342 if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
343 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
344 closesocket( newsocket );
349 if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
350 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
351 WinPrint( "setsockopt error\n" );
355 } //end of the function WINS_OpenReliableSocket
356 //===========================================================================
360 // Changes Globals: -
361 //===========================================================================
362 int WINS_Listen( int socket ){
365 if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
366 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
369 if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
370 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
374 } //end of the function WINS_Listen
375 //===========================================================================
379 // Changes Globals: -
380 //===========================================================================
381 int WINS_Accept( int socket, struct sockaddr_s *addr ){
382 int addrlen = sizeof( struct sockaddr_s );
386 newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
387 if ( newsocket == INVALID_SOCKET ) {
388 if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
391 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
395 if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
396 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
397 WinPrint( "setsockopt error\n" );
400 } //end of the function WINS_Accept
401 //===========================================================================
405 // Changes Globals: -
406 //===========================================================================
407 int WINS_CloseSocket( int socket ){
409 if (socket == net_broadcastsocket)
410 net_broadcastsocket = 0;
412 // shutdown(socket, SD_SEND);
414 if ( closesocket( socket ) == SOCKET_ERROR ) {
415 WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
419 } //end of the function WINS_CloseSocket
420 //===========================================================================
421 // this lets you type only as much of the net address as required, using
422 // the local network components to fill in the rest
426 // Changes Globals: -
427 //===========================================================================
428 static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
437 strcpy( buff + 1, in );
438 if ( buff[1] == '.' ) {
447 if ( *++b < '0' || *b > '9' ) {
450 while ( !( *b < '0' || *b > '9' ) )
451 num = num * 10 + *( b++ ) - '0';
453 addr = ( addr << 8 ) + num;
456 hostaddr->sa_family = AF_INET;
457 ( (struct sockaddr_in *)hostaddr )->sin_port = htons( (u_short)net_hostport );
458 ( (struct sockaddr_in *)hostaddr )->sin_addr.s_addr = ( myAddr & htonl( mask ) ) | htonl( addr );
461 } //end of the function PartialIPAddress
462 //===========================================================================
466 // Changes Globals: -
467 //===========================================================================
468 int WINS_Connect( int socket, struct sockaddr_s *addr ){
470 u_long _true2 = 0xFFFFFFFF;
472 ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
473 if ( ret == SOCKET_ERROR ) {
474 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
477 if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
478 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
482 } //end of the function WINS_Connect
483 //===========================================================================
487 // Changes Globals: -
488 //===========================================================================
489 int WINS_CheckNewConnections( void ){
492 if ( net_acceptsocket == -1 ) {
496 if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
497 return net_acceptsocket;
500 } //end of the function WINS_CheckNewConnections
501 //===========================================================================
502 // returns the number of bytes read
503 // 0 if no bytes available
508 // Changes Globals: -
509 //===========================================================================
510 int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
511 int addrlen = sizeof( struct sockaddr_s );
515 ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
517 errno = WSAGetLastError();
519 if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
526 ret = recv( socket, buf, len, 0 );
527 if ( ret == SOCKET_ERROR ) {
528 errno = WSAGetLastError();
530 if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
535 if ( ret == SOCKET_ERROR ) {
536 WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
539 } //end of the function WINS_Read
540 //===========================================================================
544 // Changes Globals: -
545 //===========================================================================
546 int WINS_MakeSocketBroadcastCapable( int socket ){
549 // make this socket broadcast capable
550 if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
553 net_broadcastsocket = socket;
556 } //end of the function WINS_MakeSocketBroadcastCapable
557 //===========================================================================
561 // Changes Globals: -
562 //===========================================================================
563 int WINS_Broadcast( int socket, byte *buf, int len ){
566 if ( socket != net_broadcastsocket ) {
567 if ( net_broadcastsocket != 0 ) {
568 WinError( "Attempted to use multiple broadcasts sockets\n" );
570 ret = WINS_MakeSocketBroadcastCapable( socket );
572 WinPrint( "Unable to make socket broadcast capable\n" );
577 return WINS_Write( socket, buf, len, &broadcastaddr );
578 } //end of the function WINS_Broadcast
579 //===========================================================================
580 // returns qtrue on success or qfalse on failure
584 // Changes Globals: -
585 //===========================================================================
586 int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
591 while ( written < len )
593 ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
594 if ( ret == SOCKET_ERROR ) {
595 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
609 while ( written < len )
611 ret = send( socket, buf, len, 0 );
612 if ( ret == SOCKET_ERROR ) {
613 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
624 if ( ret == SOCKET_ERROR ) {
625 WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
627 return ( ret == len );
628 } //end of the function WINS_Write
629 //===========================================================================
633 // Changes Globals: -
634 //===========================================================================
635 char *WINS_AddrToString( struct sockaddr_s *addr ){
636 static char buffer[22];
639 haddr = ntohl( ( (struct sockaddr_in *)addr )->sin_addr.s_addr );
640 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 ) );
642 } //end of the function WINS_AddrToString
643 //===========================================================================
647 // Changes Globals: -
648 //===========================================================================
649 int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
650 int ha1, ha2, ha3, ha4, hp;
653 sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
654 ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
656 addr->sa_family = AF_INET;
657 ( (struct sockaddr_in *)addr )->sin_addr.s_addr = htonl( ipaddr );
658 ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)hp );
660 } //end of the function WINS_StringToAddr
661 //===========================================================================
665 // Changes Globals: -
666 //===========================================================================
667 int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
668 int addrlen = sizeof( struct sockaddr_s );
671 memset( addr, 0, sizeof( struct sockaddr_s ) );
672 getsockname( socket, (struct sockaddr *)addr, &addrlen );
673 a = ( (struct sockaddr_in *)addr )->sin_addr.s_addr;
674 if ( a == 0 || a == inet_addr( "127.0.0.1" ) ) {
675 ( (struct sockaddr_in *)addr )->sin_addr.s_addr = myAddr;
679 } //end of the function WINS_GetSocketAddr
680 //===========================================================================
684 // Changes Globals: -
685 //===========================================================================
686 int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
687 struct hostent *hostentry;
689 hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
691 strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
695 strcpy( name, WINS_AddrToString( addr ) );
697 } //end of the function WINS_GetNameFromAddr
698 //===========================================================================
702 // Changes Globals: -
703 //===========================================================================
704 int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
705 struct hostent *hostentry;
707 if ( name[0] >= '0' && name[0] <= '9' ) {
708 return PartialIPAddress( name, addr );
711 hostentry = gethostbyname( name );
716 addr->sa_family = AF_INET;
717 ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)net_hostport );
718 ( (struct sockaddr_in *)addr )->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
721 } //end of the function WINS_GetAddrFromName
722 //===========================================================================
726 // Changes Globals: -
727 //===========================================================================
728 int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
729 if ( addr1->sa_family != addr2->sa_family ) {
733 if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
737 if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
742 } //end of the function WINS_AddrCompare
743 //===========================================================================
747 // Changes Globals: -
748 //===========================================================================
749 int WINS_GetSocketPort( struct sockaddr_s *addr ){
750 return ntohs( ( (struct sockaddr_in *)addr )->sin_port );
751 } //end of the function WINS_GetSocketPort
752 //===========================================================================
756 // Changes Globals: -
757 //===========================================================================
758 int WINS_SetSocketPort( struct sockaddr_s *addr, int port ){
759 ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)port );
761 } //end of the function WINS_SetSocketPort