]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/l_net/l_net_wins.c
l_net: don't segfault of gethostbyname(gethostname()) fails
[xonotic/netradiant.git] / libs / l_net / l_net_wins.c
1 /*
2    Copyright (C) 1999-2006 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 //===========================================================================
140 //
141 // Parameter:                           -
142 // Returns:                                     -
143 // Changes Globals:             -
144 //===========================================================================
145 const char *WINS_ErrorMessage( int error ){
146         int search = 0;
147
148         if ( !error ) {
149                 return "No error occurred";
150         }
151
152         for ( search = 0; errlist[search].errstr; search++ )
153         {
154                 if ( error == errlist[search].errnum ) {
155                         return errlist[search].errstr;
156                 }
157         }  //end for
158
159         return "Unknown error";
160 } //end of the function WINS_ErrorMessage
161 //===========================================================================
162 //
163 // Parameter:                           -
164 // Returns:                                     -
165 // Changes Globals:             -
166 //===========================================================================
167 int WINS_Init( void ){
168         int i;
169         struct hostent *local;
170         char buff[MAXHOSTNAMELEN];
171         struct sockaddr_s addr;
172         char    *p;
173         int r;
174         WORD wVersionRequested;
175
176         wVersionRequested = MAKEWORD( 1, 1 );
177
178         r = WSAStartup( wVersionRequested, &winsockdata );
179
180         if ( r ) {
181                 WinPrint( "Winsock initialization failed.\n" );
182                 return -1;
183         }
184
185         /*
186            i = COM_CheckParm ("-udpport");
187            if (i == 0)*/
188         net_hostport = DEFAULTnet_hostport;
189         /*
190            else if (i < com_argc-1)
191             net_hostport = Q_atoi (com_argv[i+1]);
192            else
193             Sys_Error ("WINS_Init: you must specify a number after -udpport");
194          */
195
196         // determine my name & address
197         gethostname( buff, MAXHOSTNAMELEN );
198         local = gethostbyname( buff );
199         if(local && local->h_addr_list && local->h_addr_list[0])
200                 myAddr = *(int *)local->h_addr_list[0];
201         else
202                 myAddr = inet_ntoa("127.0.0.1");
203
204         // if the quake hostname isn't set, set it to the machine name
205 //      if (Q_strcmp(hostname.string, "UNNAMED") == 0)
206         {
207                 // see if it's a text IP address (well, close enough)
208                 for ( p = buff; *p; p++ )
209                         if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
210                                 break;
211                         }
212
213                 // if it is a real name, strip off the domain; we only want the host
214                 if ( *p ) {
215                         for ( i = 0; i < 15; i++ )
216                                 if ( buff[i] == '.' ) {
217                                         break;
218                                 }
219                         buff[i] = 0;
220                 }
221 //              Cvar_Set ("hostname", buff);
222         }
223
224         if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == -1 ) {
225                 WinError( "WINS_Init: Unable to open control socket\n" );
226         }
227
228         ( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
229         ( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
230         ( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );
231
232         WINS_GetSocketAddr( net_controlsocket, &addr );
233         strcpy( my_tcpip_address,  WINS_AddrToString( &addr ) );
234         p = strrchr( my_tcpip_address, ':' );
235         if ( p ) {
236                 *p = 0;
237         }
238         WinPrint( "Winsock Initialized\n" );
239
240         return net_controlsocket;
241 } //end of the function WINS_Init
242 //===========================================================================
243 //
244 // Parameter:                           -
245 // Returns:                                     -
246 // Changes Globals:             -
247 //===========================================================================
248 char *WINS_MyAddress( void ){
249         return my_tcpip_address;
250 } //end of the function WINS_MyAddress
251 //===========================================================================
252 //
253 // Parameter:                           -
254 // Returns:                                     -
255 // Changes Globals:             -
256 //===========================================================================
257 void WINS_Shutdown( void ){
258         //WINS_Listen(0);
259         WINS_CloseSocket( net_controlsocket );
260         WSACleanup();
261         //
262         //WinPrint("Winsock Shutdown\n");
263 } //end of the function WINS_Shutdown
264 //===========================================================================
265 //
266 // Parameter:                           -
267 // Returns:                                     -
268 // Changes Globals:             -
269 //===========================================================================
270 /*
271    void WINS_Listen(int state)
272    {
273     // enable listening
274     if (state)
275     {
276         if (net_acceptsocket != -1)
277             return;
278         if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
279             WinError ("WINS_Listen: Unable to open accept socket\n");
280         return;
281     }
282
283     // disable listening
284     if (net_acceptsocket == -1)
285         return;
286     WINS_CloseSocket (net_acceptsocket);
287     net_acceptsocket = -1;
288    } //end of the function WINS_Listen*/
289 //===========================================================================
290 //
291 // Parameter:                           -
292 // Returns:                                     -
293 // Changes Globals:             -
294 //===========================================================================
295 int WINS_OpenSocket( int port ){
296         int newsocket;
297         struct sockaddr_in address;
298         u_long _true = 1;
299
300         if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
301                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
302                 return -1;
303         } //end if
304
305         if ( ioctlsocket( newsocket, FIONBIO, &_true ) == -1 ) {
306                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
307                 closesocket( newsocket );
308                 return -1;
309         } //end if
310
311         memset( (char *) &address, 0, sizeof( address ) );
312         address.sin_family = AF_INET;
313         address.sin_addr.s_addr = INADDR_ANY;
314         address.sin_port = htons( (u_short)port );
315         if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
316                 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
317                 closesocket( newsocket );
318                 return -1;
319         } //end if
320
321         return newsocket;
322 } //end of the function WINS_OpenSocket
323 //===========================================================================
324 //
325 // Parameter:                           -
326 // Returns:                                     -
327 // Changes Globals:             -
328 //===========================================================================
329 int WINS_OpenReliableSocket( int port ){
330         int newsocket;
331         struct sockaddr_in address;
332         BOOL _true = 0xFFFFFFFF;
333
334         //IPPROTO_TCP
335         //
336         if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
337                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
338                 return -1;
339         } //end if
340
341         memset( (char *) &address, 0, sizeof( address ) );
342         address.sin_family = AF_INET;
343         address.sin_addr.s_addr = htonl( INADDR_ANY );
344         address.sin_port = htons( (u_short)port );
345         if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
346                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
347                 closesocket( newsocket );
348                 return -1;
349         } //end if
350
351         //
352         if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
353                 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
354                 WinPrint( "setsockopt error\n" );
355         } //end if
356
357         return newsocket;
358 } //end of the function WINS_OpenReliableSocket
359 //===========================================================================
360 //
361 // Parameter:                           -
362 // Returns:                                     -
363 // Changes Globals:             -
364 //===========================================================================
365 int WINS_Listen( int socket ){
366         u_long _true = 1;
367
368         if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
369                 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
370                 return -1;
371         } //end if
372         if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
373                 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
374                 return -1;
375         } //end if
376         return 0;
377 } //end of the function WINS_Listen
378 //===========================================================================
379 //
380 // Parameter:                           -
381 // Returns:                                     -
382 // Changes Globals:             -
383 //===========================================================================
384 int WINS_Accept( int socket, struct sockaddr_s *addr ){
385         int addrlen = sizeof( struct sockaddr_s );
386         int newsocket;
387         BOOL _true = 1;
388
389         newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
390         if ( newsocket == INVALID_SOCKET ) {
391                 if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
392                         return -1;
393                 }
394                 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
395                 return -1;
396         } //end if
397           //
398         if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
399                 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
400                 WinPrint( "setsockopt error\n" );
401         } //end if
402         return newsocket;
403 } //end of the function WINS_Accept
404 //===========================================================================
405 //
406 // Parameter:                           -
407 // Returns:                                     -
408 // Changes Globals:             -
409 //===========================================================================
410 int WINS_CloseSocket( int socket ){
411         /*
412            if (socket == net_broadcastsocket)
413             net_broadcastsocket = 0;
414          */
415 //      shutdown(socket, SD_SEND);
416
417         if ( closesocket( socket ) == SOCKET_ERROR ) {
418                 WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
419                 return SOCKET_ERROR;
420         } //end if
421         return 0;
422 } //end of the function WINS_CloseSocket
423 //===========================================================================
424 // this lets you type only as much of the net address as required, using
425 // the local network components to fill in the rest
426 //
427 // Parameter:                           -
428 // Returns:                                     -
429 // Changes Globals:             -
430 //===========================================================================
431 static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
432         char buff[256];
433         char *b;
434         int addr;
435         int num;
436         int mask;
437
438         buff[0] = '.';
439         b = buff;
440         strcpy( buff + 1, in );
441         if ( buff[1] == '.' ) {
442                 b++;
443         }
444
445         addr = 0;
446         mask = -1;
447         while ( *b == '.' )
448         {
449                 num = 0;
450                 if ( *++b < '0' || *b > '9' ) {
451                         return -1;
452                 }
453                 while ( !( *b < '0' || *b > '9' ) )
454                         num = num * 10 + *( b++ ) - '0';
455                 mask <<= 8;
456                 addr = ( addr << 8 ) + num;
457         }
458
459         hostaddr->sa_family = AF_INET;
460         ( (struct sockaddr_in *)hostaddr )->sin_port = htons( (u_short)net_hostport );
461         ( (struct sockaddr_in *)hostaddr )->sin_addr.s_addr = ( myAddr & htonl( mask ) ) | htonl( addr );
462
463         return 0;
464 } //end of the function PartialIPAddress
465 //===========================================================================
466 //
467 // Parameter:                           -
468 // Returns:                                     -
469 // Changes Globals:             -
470 //===========================================================================
471 int WINS_Connect( int socket, struct sockaddr_s *addr ){
472         int ret;
473         u_long _true2 = 0xFFFFFFFF;
474
475         ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
476         if ( ret == SOCKET_ERROR ) {
477                 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
478                 return -1;
479         } //end if
480         if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
481                 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
482                 return -1;
483         } //end if
484         return 0;
485 } //end of the function WINS_Connect
486 //===========================================================================
487 //
488 // Parameter:                           -
489 // Returns:                                     -
490 // Changes Globals:             -
491 //===========================================================================
492 int WINS_CheckNewConnections( void ){
493         char buf[4];
494
495         if ( net_acceptsocket == -1 ) {
496                 return -1;
497         }
498
499         if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
500                 return net_acceptsocket;
501         }
502         return -1;
503 } //end of the function WINS_CheckNewConnections
504 //===========================================================================
505 // returns the number of bytes read
506 // 0 if no bytes available
507 // -1 on failure
508 //
509 // Parameter:                           -
510 // Returns:                                     -
511 // Changes Globals:             -
512 //===========================================================================
513 int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
514         int addrlen = sizeof( struct sockaddr_s );
515         int ret, errno;
516
517         if ( addr ) {
518                 ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
519                 if ( ret == -1 ) {
520                         errno = WSAGetLastError();
521
522                         if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
523                                 return 0;
524                         }
525                 } //end if
526         } //end if
527         else
528         {
529                 ret = recv( socket, buf, len, 0 );
530                 if ( ret == SOCKET_ERROR ) {
531                         errno = WSAGetLastError();
532
533                         if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
534                                 return 0;
535                         }
536                 } //end if
537         } //end else
538         if ( ret == SOCKET_ERROR ) {
539                 WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
540         } //end if
541         return ret;
542 } //end of the function WINS_Read
543 //===========================================================================
544 //
545 // Parameter:                           -
546 // Returns:                                     -
547 // Changes Globals:             -
548 //===========================================================================
549 int WINS_MakeSocketBroadcastCapable( int socket ){
550         int i = 1;
551
552         // make this socket broadcast capable
553         if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
554                 return -1;
555         }
556         net_broadcastsocket = socket;
557
558         return 0;
559 } //end of the function WINS_MakeSocketBroadcastCapable
560 //===========================================================================
561 //
562 // Parameter:                           -
563 // Returns:                                     -
564 // Changes Globals:             -
565 //===========================================================================
566 int WINS_Broadcast( int socket, byte *buf, int len ){
567         int ret;
568
569         if ( socket != net_broadcastsocket ) {
570                 if ( net_broadcastsocket != 0 ) {
571                         WinError( "Attempted to use multiple broadcasts sockets\n" );
572                 }
573                 ret = WINS_MakeSocketBroadcastCapable( socket );
574                 if ( ret == -1 ) {
575                         WinPrint( "Unable to make socket broadcast capable\n" );
576                         return ret;
577                 }
578         }
579
580         return WINS_Write( socket, buf, len, &broadcastaddr );
581 } //end of the function WINS_Broadcast
582 //===========================================================================
583 // returns qtrue on success or qfalse on failure
584 //
585 // Parameter:                           -
586 // Returns:                                     -
587 // Changes Globals:             -
588 //===========================================================================
589 int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
590         int ret, written;
591
592         if ( addr ) {
593                 written = 0;
594                 while ( written < len )
595                 {
596                         ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
597                         if ( ret == SOCKET_ERROR ) {
598                                 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
599                                         return qfalse;
600                                 }
601                                 Sleep( 1000 );
602                         } //end if
603                         else
604                         {
605                                 written += ret;
606                         }
607                 }
608         } //end if
609         else
610         {
611                 written = 0;
612                 while ( written < len )
613                 {
614                         ret = send( socket, buf, len, 0 );
615                         if ( ret == SOCKET_ERROR ) {
616                                 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
617                                         return qfalse;
618                                 }
619                                 Sleep( 1000 );
620                         } //end if
621                         else
622                         {
623                                 written += ret;
624                         }
625                 }
626         } //end else
627         if ( ret == SOCKET_ERROR ) {
628                 WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
629         } //end if
630         return ( ret == len );
631 } //end of the function WINS_Write
632 //===========================================================================
633 //
634 // Parameter:                           -
635 // Returns:                                     -
636 // Changes Globals:             -
637 //===========================================================================
638 char *WINS_AddrToString( struct sockaddr_s *addr ){
639         static char buffer[22];
640         int haddr;
641
642         haddr = ntohl( ( (struct sockaddr_in *)addr )->sin_addr.s_addr );
643         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 ) );
644         return buffer;
645 } //end of the function WINS_AddrToString
646 //===========================================================================
647 //
648 // Parameter:                           -
649 // Returns:                                     -
650 // Changes Globals:             -
651 //===========================================================================
652 int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
653         int ha1, ha2, ha3, ha4, hp;
654         int ipaddr;
655
656         sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
657         ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
658
659         addr->sa_family = AF_INET;
660         ( (struct sockaddr_in *)addr )->sin_addr.s_addr = htonl( ipaddr );
661         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)hp );
662         return 0;
663 } //end of the function WINS_StringToAddr
664 //===========================================================================
665 //
666 // Parameter:                           -
667 // Returns:                                     -
668 // Changes Globals:             -
669 //===========================================================================
670 int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
671         int addrlen = sizeof( struct sockaddr_s );
672         unsigned int a;
673
674         memset( addr, 0, sizeof( struct sockaddr_s ) );
675         getsockname( socket, (struct sockaddr *)addr, &addrlen );
676         a = ( (struct sockaddr_in *)addr )->sin_addr.s_addr;
677         if ( a == 0 || a == inet_addr( "127.0.0.1" ) ) {
678                 ( (struct sockaddr_in *)addr )->sin_addr.s_addr = myAddr;
679         }
680
681         return 0;
682 } //end of the function WINS_GetSocketAddr
683 //===========================================================================
684 //
685 // Parameter:                           -
686 // Returns:                                     -
687 // Changes Globals:             -
688 //===========================================================================
689 int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
690         struct hostent *hostentry;
691
692         hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
693         if ( hostentry ) {
694                 strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
695                 return 0;
696         }
697
698         strcpy( name, WINS_AddrToString( addr ) );
699         return 0;
700 } //end of the function WINS_GetNameFromAddr
701 //===========================================================================
702 //
703 // Parameter:                           -
704 // Returns:                                     -
705 // Changes Globals:             -
706 //===========================================================================
707 int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
708         struct hostent *hostentry;
709
710         if ( name[0] >= '0' && name[0] <= '9' ) {
711                 return PartialIPAddress( name, addr );
712         }
713
714         hostentry = gethostbyname( name );
715         if ( !hostentry ) {
716                 return -1;
717         }
718
719         addr->sa_family = AF_INET;
720         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)net_hostport );
721         ( (struct sockaddr_in *)addr )->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
722
723         return 0;
724 } //end of the function WINS_GetAddrFromName
725 //===========================================================================
726 //
727 // Parameter:                           -
728 // Returns:                                     -
729 // Changes Globals:             -
730 //===========================================================================
731 int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
732         if ( addr1->sa_family != addr2->sa_family ) {
733                 return -1;
734         }
735
736         if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
737                 return -1;
738         }
739
740         if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
741                 return 1;
742         }
743
744         return 0;
745 } //end of the function WINS_AddrCompare
746 //===========================================================================
747 //
748 // Parameter:                           -
749 // Returns:                                     -
750 // Changes Globals:             -
751 //===========================================================================
752 int WINS_GetSocketPort( struct sockaddr_s *addr ){
753         return ntohs( ( (struct sockaddr_in *)addr )->sin_port );
754 } //end of the function WINS_GetSocketPort
755 //===========================================================================
756 //
757 // Parameter:                           -
758 // Returns:                                     -
759 // Changes Globals:             -
760 //===========================================================================
761 int WINS_SetSocketPort( struct sockaddr_s *addr, int port ){
762         ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)port );
763         return 0;
764 } //end of the function WINS_SetSocketPort