]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/l_net/l_net_wins.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / libs / l_net / l_net_wins.c
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 //===========================================================================
23 //
24 // Name:         l_net_wins.c
25 // Function:     WinSock
26 // Programmer:   MrElusive
27 // Last update:  -
28 // Tab Size:     3
29 // Notes:
30 //===========================================================================
31
32 #include <windows.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "l_net.h"
37 #include "l_net_wins.h"
38 //#include <winsock.h>
39 //#include "mpdosock.h"
40
41 #define WinError WinPrint
42
43 #define qtrue   1
44 #define qfalse  0
45
46 typedef struct tag_error_struct
47 {
48         int errnum;
49         LPSTR errstr;
50 } ERROR_STRUCT;
51
52 #define NET_NAMELEN         64
53
54 char my_tcpip_address[NET_NAMELEN];
55
56 #define DEFAULTnet_hostport 26000
57
58 #define MAXHOSTNAMELEN      256
59
60 static int net_acceptsocket = -1;       // socket for fielding new connections
61 static int net_controlsocket;
62 static int net_hostport;                // udp port number for acceptsocket
63 static int net_broadcastsocket = 0;
64 //static qboolean ifbcastinit = qfalse;
65 static struct sockaddr_s broadcastaddr;
66
67 static unsigned long myAddr;
68
69 WSADATA winsockdata;
70
71 ERROR_STRUCT errlist[] = {
72         {WSAEINTR,           "WSAEINTR - Interrupted"},
73         {WSAEBADF,           "WSAEBADF - Bad file number"},
74         {WSAEFAULT,          "WSAEFAULT - Bad address"},
75         {WSAEINVAL,          "WSAEINVAL - Invalid argument"},
76         {WSAEMFILE,          "WSAEMFILE - Too many open files"},
77
78 /*
79  *    Windows Sockets definitions of regular Berkeley error constants
80  */
81
82         {WSAEWOULDBLOCK,     "WSAEWOULDBLOCK - Socket marked as non-blocking"},
83         {WSAEINPROGRESS,     "WSAEINPROGRESS - Blocking call in progress"},
84         {WSAEALREADY,        "WSAEALREADY - Command already completed"},
85         {WSAENOTSOCK,        "WSAENOTSOCK - Descriptor is not a socket"},
86         {WSAEDESTADDRREQ,    "WSAEDESTADDRREQ - Destination address required"},
87         {WSAEMSGSIZE,        "WSAEMSGSIZE - Data size too large"},
88         {WSAEPROTOTYPE,      "WSAEPROTOTYPE - Protocol is of wrong type for this socket"},
89         {WSAENOPROTOOPT,     "WSAENOPROTOOPT - Protocol option not supported for this socket type"},
90         {WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT - Protocol is not supported"},
91         {WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT - Socket type not supported by this address family"},
92         {WSAEOPNOTSUPP,      "WSAEOPNOTSUPP - Option not supported"},
93         {WSAEPFNOSUPPORT,    "WSAEPFNOSUPPORT - "},
94         {WSAEAFNOSUPPORT,    "WSAEAFNOSUPPORT - Address family not supported by this protocol"},
95         {WSAEADDRINUSE,      "WSAEADDRINUSE - Address is in use"},
96         {WSAEADDRNOTAVAIL,   "WSAEADDRNOTAVAIL - Address not available from local machine"},
97         {WSAENETDOWN,        "WSAENETDOWN - Network subsystem is down"},
98         {WSAENETUNREACH,     "WSAENETUNREACH - Network cannot be reached"},
99         {WSAENETRESET,       "WSAENETRESET - Connection has been dropped"},
100         {WSAECONNABORTED,    "WSAECONNABORTED - Connection aborted"},
101         {WSAECONNRESET,      "WSAECONNRESET - Connection reset"},
102         {WSAENOBUFS,         "WSAENOBUFS - No buffer space available"},
103         {WSAEISCONN,         "WSAEISCONN - Socket is already connected"},
104         {WSAENOTCONN,        "WSAENOTCONN - Socket is not connected"},
105         {WSAESHUTDOWN,       "WSAESHUTDOWN - Socket has been shut down"},
106         {WSAETOOMANYREFS,    "WSAETOOMANYREFS - Too many references"},
107         {WSAETIMEDOUT,       "WSAETIMEDOUT - Command timed out"},
108         {WSAECONNREFUSED,    "WSAECONNREFUSED - Connection refused"},
109         {WSAELOOP,           "WSAELOOP - "},
110         {WSAENAMETOOLONG,    "WSAENAMETOOLONG - "},
111         {WSAEHOSTDOWN,       "WSAEHOSTDOWN - Host is down"},
112         {WSAEHOSTUNREACH,    "WSAEHOSTUNREACH - "},
113         {WSAENOTEMPTY,       "WSAENOTEMPTY - "},
114         {WSAEPROCLIM,        "WSAEPROCLIM - "},
115         {WSAEUSERS,          "WSAEUSERS - "},
116         {WSAEDQUOT,          "WSAEDQUOT - "},
117         {WSAESTALE,          "WSAESTALE - "},
118         {WSAEREMOTE,         "WSAEREMOTE - "},
119
120 /*
121  *    Extended Windows Sockets error constant definitions
122  */
123
124         {WSASYSNOTREADY,     "WSASYSNOTREADY - Network subsystem not ready"},
125         {WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED - Version not supported"},
126         {WSANOTINITIALISED,  "WSANOTINITIALISED - WSAStartup() has not been successfully called"},
127
128 /*
129  *    Other error constants.
130  */
131
132         {WSAHOST_NOT_FOUND,  "WSAHOST_NOT_FOUND - Host not found"},
133         {WSATRY_AGAIN,       "WSATRY_AGAIN - Host not found or SERVERFAIL"},
134         {WSANO_RECOVERY,     "WSANO_RECOVERY - Non-recoverable error"},
135         {WSANO_DATA,         "WSANO_DATA - (or WSANO_ADDRESS) - No data record of requested type"},
136         {-1,                 NULL}
137 };
138
139 #ifdef _DEBUG
140 void WinPrint( char *str, ... );
141 #else
142 void WinPrint( char *str, ... );
143 #endif
144
145 //===========================================================================
146 //
147 // Parameter:                           -
148 // Returns:                                     -
149 // Changes Globals:             -
150 //===========================================================================
151 char *WINS_ErrorMessage( int error ){
152         int search = 0;
153
154         if ( !error ) {
155                 return "No error occurred";
156         }
157
158         for ( search = 0; errlist[search].errstr; search++ )
159         {
160                 if ( error == errlist[search].errnum ) {
161                         return errlist[search].errstr;
162                 }
163         }  //end for
164
165         return "Unknown error";
166 } //end of the function WINS_ErrorMessage
167 //===========================================================================
168 //
169 // Parameter:                           -
170 // Returns:                                     -
171 // Changes Globals:             -
172 //===========================================================================
173 int WINS_Init( void ){
174         int i;
175         struct hostent *local;
176         char buff[MAXHOSTNAMELEN];
177         struct sockaddr_s addr;
178         char    *p;
179         int r;
180         WORD wVersionRequested;
181
182         wVersionRequested = MAKEWORD( 1, 1 );
183
184         r = WSAStartup( wVersionRequested, &winsockdata );
185
186         if ( r ) {
187                 WinPrint( "Winsock initialization failed.\n" );
188                 return -1;
189         }
190
191         /*
192            i = COM_CheckParm ("-udpport");
193            if (i == 0)*/
194         net_hostport = DEFAULTnet_hostport;
195         /*
196            else if (i < com_argc-1)
197             net_hostport = Q_atoi (com_argv[i+1]);
198            else
199             Sys_Error ("WINS_Init: you must specify a number after -udpport");
200          */
201
202         // determine my name & address
203         gethostname( buff, MAXHOSTNAMELEN );
204         local = gethostbyname( buff );
205         myAddr = *(int *)local->h_addr_list[0];
206
207         // if the quake hostname isn't set, set it to the machine name
208 //      if (Q_strcmp(hostname.string, "UNNAMED") == 0)
209         {
210                 // see if it's a text IP address (well, close enough)
211                 for ( p = buff; *p; p++ )
212                         if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
213                                 break;
214                         }
215
216                 // if it is a real name, strip off the domain; we only want the host
217                 if ( *p ) {
218                         for ( i = 0; i < 15; i++ )
219                                 if ( buff[i] == '.' ) {
220                                         break;
221                                 }
222                         buff[i] = 0;
223                 }
224 //              Cvar_Set ("hostname", buff);
225         }
226
227         if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == -1 ) {
228                 WinError( "WINS_Init: Unable to open control socket\n" );
229         }
230
231         ( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
232         ( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
233         ( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );
234
235         WINS_GetSocketAddr( net_controlsocket, &addr );
236         strcpy( my_tcpip_address,  WINS_AddrToString( &addr ) );
237         p = strrchr( my_tcpip_address, ':' );
238         if ( p ) {
239                 *p = 0;
240         }
241         WinPrint( "Winsock Initialized\n" );
242
243         return net_controlsocket;
244 } //end of the function WINS_Init
245 //===========================================================================
246 //
247 // Parameter:                           -
248 // Returns:                                     -
249 // Changes Globals:             -
250 //===========================================================================
251 char *WINS_MyAddress( void ){
252         return my_tcpip_address;
253 } //end of the function WINS_MyAddress
254 //===========================================================================
255 //
256 // Parameter:                           -
257 // Returns:                                     -
258 // Changes Globals:             -
259 //===========================================================================
260 void WINS_Shutdown( void ){
261         //WINS_Listen(0);
262         WINS_CloseSocket( net_controlsocket );
263         WSACleanup();
264         //
265         WinPrint( "Winsock Shutdown\n" );
266 } //end of the function WINS_Shutdown
267 //===========================================================================
268 //
269 // Parameter:                           -
270 // Returns:                                     -
271 // Changes Globals:             -
272 //===========================================================================
273 /*
274    void WINS_Listen(int state)
275    {
276     // enable listening
277     if (state)
278     {
279         if (net_acceptsocket != -1)
280             return;
281         if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
282             WinError ("WINS_Listen: Unable to open accept socket\n");
283         return;
284     }
285
286     // disable listening
287     if (net_acceptsocket == -1)
288         return;
289     WINS_CloseSocket (net_acceptsocket);
290     net_acceptsocket = -1;
291    } //end of the function WINS_Listen*/
292 //===========================================================================
293 //
294 // Parameter:                           -
295 // Returns:                                     -
296 // Changes Globals:             -
297 //===========================================================================
298 int WINS_OpenSocket( int port ){
299         int newsocket;
300         struct sockaddr_in address;
301         u_long _true = 1;
302
303         if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
304                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
305                 return -1;
306         } //end if
307
308         if ( ioctlsocket( newsocket, FIONBIO, &_true ) == -1 ) {
309                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
310                 closesocket( newsocket );
311                 return -1;
312         } //end if
313
314         memset( (char *) &address, 0, sizeof( address ) );
315         address.sin_family = AF_INET;
316         address.sin_addr.s_addr = INADDR_ANY;
317         address.sin_port = htons( (u_short)port );
318         if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
319                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
320                 closesocket( newsocket );
321                 return -1;
322         } //end if
323
324         return newsocket;
325 } //end of the function WINS_OpenSocket
326 //===========================================================================
327 //
328 // Parameter:                           -
329 // Returns:                                     -
330 // Changes Globals:             -
331 //===========================================================================
332 int WINS_OpenReliableSocket( int port ){
333         int newsocket;
334         struct sockaddr_in address;
335         BOOL _true = 0xFFFFFFFF;
336
337         //IPPROTO_TCP
338         //
339         if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
340                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
341                 return -1;
342         } //end if
343
344         memset( (char *) &address, 0, sizeof( address ) );
345         address.sin_family = AF_INET;
346         address.sin_addr.s_addr = htonl( INADDR_ANY );
347         address.sin_port = htons( (u_short)port );
348         if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
349                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
350                 closesocket( newsocket );
351                 return -1;
352         } //end if
353
354         //
355         if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
356                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
357                 WinPrint( "setsockopt error\n" );
358         } //end if
359
360         return newsocket;
361 } //end of the function WINS_OpenReliableSocket
362 //===========================================================================
363 //
364 // Parameter:                           -
365 // Returns:                                     -
366 // Changes Globals:             -
367 //===========================================================================
368 int WINS_Listen( int socket ){
369         u_long _true = 1;
370
371         if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
372                 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
373                 return -1;
374         } //end if
375         if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
376                 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
377                 return -1;
378         } //end if
379         return 0;
380 } //end of the function WINS_Listen
381 //===========================================================================
382 //
383 // Parameter:                           -
384 // Returns:                                     -
385 // Changes Globals:             -
386 //===========================================================================
387 int WINS_Accept( int socket, struct sockaddr_s *addr ){
388         int addrlen = sizeof( struct sockaddr_s );
389         int newsocket;
390         BOOL _true = 1;
391
392         newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
393         if ( newsocket == INVALID_SOCKET ) {
394                 if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
395                         return -1;
396                 }
397                 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
398                 return -1;
399         } //end if
400           //
401         if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
402                 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
403                 WinPrint( "setsockopt error\n" );
404         } //end if
405         return newsocket;
406 } //end of the function WINS_Accept
407 //===========================================================================
408 //
409 // Parameter:                           -
410 // Returns:                                     -
411 // Changes Globals:             -
412 //===========================================================================
413 int WINS_CloseSocket( int socket ){
414         /*
415            if (socket == net_broadcastsocket)
416             net_broadcastsocket = 0;
417          */
418 //      shutdown(socket, SD_SEND);
419
420         if ( closesocket( socket ) == SOCKET_ERROR ) {
421                 WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
422                 return SOCKET_ERROR;
423         } //end if
424         return 0;
425 } //end of the function WINS_CloseSocket
426 //===========================================================================
427 // this lets you type only as much of the net address as required, using
428 // the local network components to fill in the rest
429 //
430 // Parameter:                           -
431 // Returns:                                     -
432 // Changes Globals:             -
433 //===========================================================================
434 static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
435         char buff[256];
436         char *b;
437         int addr;
438         int num;
439         int mask;
440
441         buff[0] = '.';
442         b = buff;
443         strcpy( buff + 1, in );
444         if ( buff[1] == '.' ) {
445                 b++;
446         }
447
448         addr = 0;
449         mask = -1;
450         while ( *b == '.' )
451         {
452                 num = 0;
453                 if ( *++b < '0' || *b > '9' ) {
454                         return -1;
455                 }
456                 while ( !( *b < '0' || *b > '9' ) )
457                         num = num * 10 + *( b++ ) - '0';
458                 mask <<= 8;
459                 addr = ( addr << 8 ) + num;
460         }
461
462         hostaddr->sa_family = AF_INET;
463         ( (struct sockaddr_in *)hostaddr )->sin_port = htons( (u_short)net_hostport );
464         ( (struct sockaddr_in *)hostaddr )->sin_addr.s_addr = ( myAddr & htonl( mask ) ) | htonl( addr );
465
466         return 0;
467 } //end of the function PartialIPAddress
468 //===========================================================================
469 //
470 // Parameter:                           -
471 // Returns:                                     -
472 // Changes Globals:             -
473 //===========================================================================
474 int WINS_Connect( int socket, struct sockaddr_s *addr ){
475         int ret;
476         u_long _true2 = 0xFFFFFFFF;
477
478         ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
479         if ( ret == SOCKET_ERROR ) {
480                 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
481                 return -1;
482         } //end if
483         if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
484                 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
485                 return -1;
486         } //end if
487         return 0;
488 } //end of the function WINS_Connect
489 //===========================================================================
490 //
491 // Parameter:                           -
492 // Returns:                                     -
493 // Changes Globals:             -
494 //===========================================================================
495 int WINS_CheckNewConnections( void ){
496         char buf[4];
497
498         if ( net_acceptsocket == -1 ) {
499                 return -1;
500         }
501
502         if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
503                 return net_acceptsocket;
504         }
505         return -1;
506 } //end of the function WINS_CheckNewConnections
507 //===========================================================================
508 // returns the number of bytes read
509 // 0 if no bytes available
510 // -1 on failure
511 //
512 // Parameter:                           -
513 // Returns:                                     -
514 // Changes Globals:             -
515 //===========================================================================
516 int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
517         int addrlen = sizeof( struct sockaddr_s );
518         int ret, errno;
519
520         if ( addr ) {
521                 ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
522                 if ( ret == -1 ) {
523                         errno = WSAGetLastError();
524
525                         if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
526                                 return 0;
527                         }
528                 } //end if
529         } //end if
530         else
531         {
532                 ret = recv( socket, buf, len, 0 );
533                 if ( ret == SOCKET_ERROR ) {
534                         errno = WSAGetLastError();
535
536                         if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
537                                 return 0;
538                         }
539                 } //end if
540         } //end else
541         if ( ret == SOCKET_ERROR ) {
542                 WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
543         } //end if
544         return ret;
545 } //end of the function WINS_Read
546 //===========================================================================
547 //
548 // Parameter:                           -
549 // Returns:                                     -
550 // Changes Globals:             -
551 //===========================================================================
552 int WINS_MakeSocketBroadcastCapable( int socket ){
553         int i = 1;
554
555         // make this socket broadcast capable
556         if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
557                 return -1;
558         }
559         net_broadcastsocket = socket;
560
561         return 0;
562 } //end of the function WINS_MakeSocketBroadcastCapable
563 //===========================================================================
564 //
565 // Parameter:                           -
566 // Returns:                                     -
567 // Changes Globals:             -
568 //===========================================================================
569 int WINS_Broadcast( int socket, byte *buf, int len ){
570         int ret;
571
572         if ( socket != net_broadcastsocket ) {
573                 if ( net_broadcastsocket != 0 ) {
574                         WinError( "Attempted to use multiple broadcasts sockets\n" );
575                 }
576                 ret = WINS_MakeSocketBroadcastCapable( socket );
577                 if ( ret == -1 ) {
578                         WinPrint( "Unable to make socket broadcast capable\n" );
579                         return ret;
580                 }
581         }
582
583         return WINS_Write( socket, buf, len, &broadcastaddr );
584 } //end of the function WINS_Broadcast
585 //===========================================================================
586 // returns qtrue on success or qfalse on failure
587 //
588 // Parameter:                           -
589 // Returns:                                     -
590 // Changes Globals:             -
591 //===========================================================================
592 int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
593         int ret, written;
594
595         if ( addr ) {
596                 written = 0;
597                 while ( written < len )
598                 {
599                         ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
600                         if ( ret == SOCKET_ERROR ) {
601                                 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
602                                         return qfalse;
603                                 }
604                                 Sleep( 1000 );
605                         } //end if
606                         else
607                         {
608                                 written += ret;
609                         }
610                 }
611         } //end if
612         else
613         {
614                 written = 0;
615                 while ( written < len )
616                 {
617                         ret = send( socket, buf, len, 0 );
618                         if ( ret == SOCKET_ERROR ) {
619                                 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
620                                         return qfalse;
621                                 }
622                                 Sleep( 1000 );
623                         } //end if
624                         else
625                         {
626                                 written += ret;
627                         }
628                 }
629         } //end else
630         if ( ret == SOCKET_ERROR ) {
631                 WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
632         } //end if
633         return ( ret == len );
634 } //end of the function WINS_Write
635 //===========================================================================
636 //
637 // Parameter:                           -
638 // Returns:                                     -
639 // Changes Globals:             -
640 //===========================================================================
641 char *WINS_AddrToString( struct sockaddr_s *addr ){
642         static char buffer[22];
643         int haddr;
644
645         haddr = ntohl( ( (struct sockaddr_in *)addr )->sin_addr.s_addr );
646         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 ) );
647         return buffer;
648 } //end of the function WINS_AddrToString
649 //===========================================================================
650 //
651 // Parameter:                           -
652 // Returns:                                     -
653 // Changes Globals:             -
654 //===========================================================================
655 int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
656         int ha1, ha2, ha3, ha4, hp;
657         int ipaddr;
658
659         sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
660         ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
661
662         addr->sa_family = AF_INET;
663         ( (struct sockaddr_in *)addr )->sin_addr.s_addr = htonl( ipaddr );
664         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)hp );
665         return 0;
666 } //end of the function WINS_StringToAddr
667 //===========================================================================
668 //
669 // Parameter:                           -
670 // Returns:                                     -
671 // Changes Globals:             -
672 //===========================================================================
673 int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
674         int addrlen = sizeof( struct sockaddr_s );
675         unsigned int a;
676
677         memset( addr, 0, sizeof( struct sockaddr_s ) );
678         getsockname( socket, (struct sockaddr *)addr, &addrlen );
679         a = ( (struct sockaddr_in *)addr )->sin_addr.s_addr;
680         if ( a == 0 || a == inet_addr( "127.0.0.1" ) ) {
681                 ( (struct sockaddr_in *)addr )->sin_addr.s_addr = myAddr;
682         }
683
684         return 0;
685 } //end of the function WINS_GetSocketAddr
686 //===========================================================================
687 //
688 // Parameter:                           -
689 // Returns:                                     -
690 // Changes Globals:             -
691 //===========================================================================
692 int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
693         struct hostent *hostentry;
694
695         hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
696         if ( hostentry ) {
697                 strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
698                 return 0;
699         }
700
701         strcpy( name, WINS_AddrToString( addr ) );
702         return 0;
703 } //end of the function WINS_GetNameFromAddr
704 //===========================================================================
705 //
706 // Parameter:                           -
707 // Returns:                                     -
708 // Changes Globals:             -
709 //===========================================================================
710 int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
711         struct hostent *hostentry;
712
713         if ( name[0] >= '0' && name[0] <= '9' ) {
714                 return PartialIPAddress( name, addr );
715         }
716
717         hostentry = gethostbyname( name );
718         if ( !hostentry ) {
719                 return -1;
720         }
721
722         addr->sa_family = AF_INET;
723         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)net_hostport );
724         ( (struct sockaddr_in *)addr )->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
725
726         return 0;
727 } //end of the function WINS_GetAddrFromName
728 //===========================================================================
729 //
730 // Parameter:                           -
731 // Returns:                                     -
732 // Changes Globals:             -
733 //===========================================================================
734 int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
735         if ( addr1->sa_family != addr2->sa_family ) {
736                 return -1;
737         }
738
739         if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
740                 return -1;
741         }
742
743         if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
744                 return 1;
745         }
746
747         return 0;
748 } //end of the function WINS_AddrCompare
749 //===========================================================================
750 //
751 // Parameter:                           -
752 // Returns:                                     -
753 // Changes Globals:             -
754 //===========================================================================
755 int WINS_GetSocketPort( struct sockaddr_s *addr ){
756         return ntohs( ( (struct sockaddr_in *)addr )->sin_port );
757 } //end of the function WINS_GetSocketPort
758 //===========================================================================
759 //
760 // Parameter:                           -
761 // Returns:                                     -
762 // Changes Globals:             -
763 //===========================================================================
764 int WINS_SetSocketPort( struct sockaddr_s *addr, int port ){
765         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)port );
766         return 0;
767 } //end of the function WINS_SetSocketPort