Debian Codegraph @

libs/l_net/l_net_wins.c

raw symbols SCIP
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 typedef struct tag_error_struct
44 {
45 int errnum;
46 LPSTR errstr;
47 } ERROR_STRUCT;
48
49 #define NET_NAMELEN 64
50
51 char my_tcpip_address[NET_NAMELEN];
52
53 const int DEFAULTnet_hostport = 26000;
54
55 const int MAXHOSTNAMELEN = 256;
56
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;
63
64 static unsigned long myAddr;
65
66 WSADATA winsockdata;
67
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"},
74
75 /*
76 * Windows Sockets definitions of regular Berkeley error constants
77 */
78
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 - "},
116
117 /*
118 * Extended Windows Sockets error constant definitions
119 */
120
121 {WSASYSNOTREADY, "WSASYSNOTREADY - Network subsystem not ready"},
122 {WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED - Version not supported"},
123 {WSANOTINITIALISED, "WSANOTINITIALISED - WSAStartup() has not been successfully called"},
124
125 /*
126 * Other error constants.
127 */
128
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"},
133 {-1, NULL}
134 };
135
136 //===========================================================================
137 //
138 // Parameter: -
139 // Returns: -
140 // Changes Globals: -
141 //===========================================================================
142 const char *WINS_ErrorMessage( int error ){
143 int search = 0;
144
145 if ( !error ) {
146 return "No error occurred";
147 }
148
149 for ( search = 0; errlist[search].errstr; search++ )
150 {
151 if ( error == errlist[search].errnum ) {
152 return errlist[search].errstr;
153 }
154 } //end for
155
156 return "Unknown error";
157 } //end of the function WINS_ErrorMessage
158 //===========================================================================
159 //
160 // Parameter: -
161 // Returns: -
162 // Changes Globals: -
163 //===========================================================================
164 int WINS_Init( void ){
165 int i;
166 struct hostent *local;
167 char buff[MAXHOSTNAMELEN];
168 struct sockaddr_s addr;
169 char *p;
170 int r;
171 WORD wVersionRequested;
172
173 wVersionRequested = MAKEWORD( 1, 1 );
174
175 r = WSAStartup( wVersionRequested, &winsockdata );
176
177 if ( r ) {
178 WinPrint( "Winsock initialization failed.\n" );
179 return -1;
180 }
181
182 /*
183 i = COM_CheckParm ("-udpport");
184 if (i == 0)*/
185 net_hostport = DEFAULTnet_hostport;
186 /*
187 else if (i < com_argc-1)
188 net_hostport = Q_atoi (com_argv[i+1]);
189 else
190 Sys_Error ("WINS_Init: you must specify a number after -udpport");
191 */
192
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];
198 else
199 myAddr = inet_addr("127.0.0.1");
200
201 // if the quake hostname isn't set, set it to the machine name
202 // if (Q_strcmp(hostname.string, "UNNAMED") == 0)
203 {
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 != '.' ) {
207 break;
208 }
209
210 // if it is a real name, strip off the domain; we only want the host
211 if ( *p ) {
212 for ( i = 0; i < 15; i++ )
213 if ( buff[i] == '.' ) {
214 break;
215 }
216 buff[i] = 0;
217 }
218 // Cvar_Set ("hostname", buff);
219 }
220
221 if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == -1 ) {
222 WinError( "WINS_Init: Unable to open control socket\n" );
223 }
224
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 );
228
229 WINS_GetSocketAddr( net_controlsocket, &addr );
230 strcpy( my_tcpip_address, WINS_AddrToString( &addr ) );
231 p = strrchr( my_tcpip_address, ':' );
232 if ( p ) {
233 *p = 0;
234 }
235 WinPrint( "Winsock Initialized\n" );
236
237 return net_controlsocket;
238 } //end of the function WINS_Init
239 //===========================================================================
240 //
241 // Parameter: -
242 // Returns: -
243 // Changes Globals: -
244 //===========================================================================
245 char *WINS_MyAddress( void ){
246 return my_tcpip_address;
247 } //end of the function WINS_MyAddress
248 //===========================================================================
249 //
250 // Parameter: -
251 // Returns: -
252 // Changes Globals: -
253 //===========================================================================
254 void WINS_Shutdown( void ){
255 //WINS_Listen(0);
256 WINS_CloseSocket( net_controlsocket );
257 WSACleanup();
258 //
259 //WinPrint("Winsock Shutdown\n");
260 } //end of the function WINS_Shutdown
261 //===========================================================================
262 //
263 // Parameter: -
264 // Returns: -
265 // Changes Globals: -
266 //===========================================================================
267 /*
268 void WINS_Listen(int state)
269 {
270 // enable listening
271 if (state)
272 {
273 if (net_acceptsocket != -1)
274 return;
275 if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
276 WinError ("WINS_Listen: Unable to open accept socket\n");
277 return;
278 }
279
280 // disable listening
281 if (net_acceptsocket == -1)
282 return;
283 WINS_CloseSocket (net_acceptsocket);
284 net_acceptsocket = -1;
285 } //end of the function WINS_Listen*/
286 //===========================================================================
287 //
288 // Parameter: -
289 // Returns: -
290 // Changes Globals: -
291 //===========================================================================
292 int WINS_OpenSocket( int port ){
293 int newsocket;
294 struct sockaddr_in address;
295 u_long _true = 1;
296
297 if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
298 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
299 return -1;
300 } //end if
301
302 if ( ioctlsocket( newsocket, FIONBIO, &_true ) == -1 ) {
303 WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
304 closesocket( newsocket );
305 return -1;
306 } //end if
307
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 );
315 return -1;
316 } //end if
317
318 return newsocket;
319 } //end of the function WINS_OpenSocket
320 //===========================================================================
321 //
322 // Parameter: -
323 // Returns: -
324 // Changes Globals: -
325 //===========================================================================
326 int WINS_OpenReliableSocket( int port ){
327 int newsocket;
328 struct sockaddr_in address;
329 BOOL _true = 0xFFFFFFFF;
330
331 //IPPROTO_TCP
332 //
333 if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
334 WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
335 return -1;
336 } //end if
337
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 );
345 return -1;
346 } //end if
347
348 //
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" );
352 } //end if
353
354 return newsocket;
355 } //end of the function WINS_OpenReliableSocket
356 //===========================================================================
357 //
358 // Parameter: -
359 // Returns: -
360 // Changes Globals: -
361 //===========================================================================
362 int WINS_Listen( int socket ){
363 u_long _true = 1;
364
365 if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
366 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
367 return -1;
368 } //end if
369 if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
370 WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
371 return -1;
372 } //end if
373 return 0;
374 } //end of the function WINS_Listen
375 //===========================================================================
376 //
377 // Parameter: -
378 // Returns: -
379 // Changes Globals: -
380 //===========================================================================
381 int WINS_Accept( int socket, struct sockaddr_s *addr ){
382 int addrlen = sizeof( struct sockaddr_s );
383 int newsocket;
384 BOOL _true = 1;
385
386 newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
387 if ( newsocket == INVALID_SOCKET ) {
388 if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
389 return -1;
390 }
391 WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
392 return -1;
393 } //end if
394 //
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" );
398 } //end if
399 return newsocket;
400 } //end of the function WINS_Accept
401 //===========================================================================
402 //
403 // Parameter: -
404 // Returns: -
405 // Changes Globals: -
406 //===========================================================================
407 int WINS_CloseSocket( int socket ){
408 /*
409 if (socket == net_broadcastsocket)
410 net_broadcastsocket = 0;
411 */
412 // shutdown(socket, SD_SEND);
413
414 if ( closesocket( socket ) == SOCKET_ERROR ) {
415 WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
416 return SOCKET_ERROR;
417 } //end if
418 return 0;
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
423 //
424 // Parameter: -
425 // Returns: -
426 // Changes Globals: -
427 //===========================================================================
428 static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
429 char buff[256];
430 char *b;
431 int addr;
432 int num;
433 int mask;
434
435 buff[0] = '.';
436 b = buff;
437 strcpy( buff + 1, in );
438 if ( buff[1] == '.' ) {
439 b++;
440 }
441
442 addr = 0;
443 mask = -1;
444 while ( *b == '.' )
445 {
446 num = 0;
447 if ( *++b < '0' || *b > '9' ) {
448 return -1;
449 }
450 while ( !( *b < '0' || *b > '9' ) )
451 num = num * 10 + *( b++ ) - '0';
452 mask <<= 8;
453 addr = ( addr << 8 ) + num;
454 }
455
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 );
459
460 return 0;
461 } //end of the function PartialIPAddress
462 //===========================================================================
463 //
464 // Parameter: -
465 // Returns: -
466 // Changes Globals: -
467 //===========================================================================
468 int WINS_Connect( int socket, struct sockaddr_s *addr ){
469 int ret;
470 u_long _true2 = 0xFFFFFFFF;
471
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() ) );
475 return -1;
476 } //end if
477 if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
478 WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
479 return -1;
480 } //end if
481 return 0;
482 } //end of the function WINS_Connect
483 //===========================================================================
484 //
485 // Parameter: -
486 // Returns: -
487 // Changes Globals: -
488 //===========================================================================
489 int WINS_CheckNewConnections( void ){
490 char buf[4];
491
492 if ( net_acceptsocket == -1 ) {
493 return -1;
494 }
495
496 if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
497 return net_acceptsocket;
498 }
499 return -1;
500 } //end of the function WINS_CheckNewConnections
501 //===========================================================================
502 // returns the number of bytes read
503 // 0 if no bytes available
504 // -1 on failure
505 //
506 // Parameter: -
507 // Returns: -
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 );
512 int ret, errno;
513
514 if ( addr ) {
515 ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
516 if ( ret == -1 ) {
517 errno = WSAGetLastError();
518
519 if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
520 return 0;
521 }
522 } //end if
523 } //end if
524 else
525 {
526 ret = recv( socket, buf, len, 0 );
527 if ( ret == SOCKET_ERROR ) {
528 errno = WSAGetLastError();
529
530 if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
531 return 0;
532 }
533 } //end if
534 } //end else
535 if ( ret == SOCKET_ERROR ) {
536 WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
537 } //end if
538 return ret;
539 } //end of the function WINS_Read
540 //===========================================================================
541 //
542 // Parameter: -
543 // Returns: -
544 // Changes Globals: -
545 //===========================================================================
546 int WINS_MakeSocketBroadcastCapable( int socket ){
547 int i = 1;
548
549 // make this socket broadcast capable
550 if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
551 return -1;
552 }
553 net_broadcastsocket = socket;
554
555 return 0;
556 } //end of the function WINS_MakeSocketBroadcastCapable
557 //===========================================================================
558 //
559 // Parameter: -
560 // Returns: -
561 // Changes Globals: -
562 //===========================================================================
563 int WINS_Broadcast( int socket, byte *buf, int len ){
564 int ret;
565
566 if ( socket != net_broadcastsocket ) {
567 if ( net_broadcastsocket != 0 ) {
568 WinError( "Attempted to use multiple broadcasts sockets\n" );
569 }
570 ret = WINS_MakeSocketBroadcastCapable( socket );
571 if ( ret == -1 ) {
572 WinPrint( "Unable to make socket broadcast capable\n" );
573 return ret;
574 }
575 }
576
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
581 //
582 // Parameter: -
583 // Returns: -
584 // Changes Globals: -
585 //===========================================================================
586 int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
587 int ret, written;
588
589 if ( addr ) {
590 written = 0;
591 while ( written < len )
592 {
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 ) {
596 return qfalse;
597 }
598 Sleep( 1000 );
599 } //end if
600 else
601 {
602 written += ret;
603 }
604 }
605 } //end if
606 else
607 {
608 written = 0;
609 while ( written < len )
610 {
611 ret = send( socket, buf, len, 0 );
612 if ( ret == SOCKET_ERROR ) {
613 if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
614 return qfalse;
615 }
616 Sleep( 1000 );
617 } //end if
618 else
619 {
620 written += ret;
621 }
622 }
623 } //end else
624 if ( ret == SOCKET_ERROR ) {
625 WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
626 } //end if
627 return ( ret == len );
628 } //end of the function WINS_Write
629 //===========================================================================
630 //
631 // Parameter: -
632 // Returns: -
633 // Changes Globals: -
634 //===========================================================================
635 char *WINS_AddrToString( struct sockaddr_s *addr ){
636 static char buffer[22];
637 int haddr;
638
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 ) );
641 return buffer;
642 } //end of the function WINS_AddrToString
643 //===========================================================================
644 //
645 // Parameter: -
646 // Returns: -
647 // Changes Globals: -
648 //===========================================================================
649 int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
650 int ha1, ha2, ha3, ha4, hp;
651 int ipaddr;
652
653 sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
654 ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
655
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 );
659 return 0;
660 } //end of the function WINS_StringToAddr
661 //===========================================================================
662 //
663 // Parameter: -
664 // Returns: -
665 // Changes Globals: -
666 //===========================================================================
667 int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
668 int addrlen = sizeof( struct sockaddr_s );
669 unsigned int a;
670
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;
676 }
677
678 return 0;
679 } //end of the function WINS_GetSocketAddr
680 //===========================================================================
681 //
682 // Parameter: -
683 // Returns: -
684 // Changes Globals: -
685 //===========================================================================
686 int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
687 struct hostent *hostentry;
688
689 hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
690 if ( hostentry ) {
691 strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
692 return 0;
693 }
694
695 strcpy( name, WINS_AddrToString( addr ) );
696 return 0;
697 } //end of the function WINS_GetNameFromAddr
698 //===========================================================================
699 //
700 // Parameter: -
701 // Returns: -
702 // Changes Globals: -
703 //===========================================================================
704 int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
705 struct hostent *hostentry;
706
707 if ( name[0] >= '0' && name[0] <= '9' ) {
708 return PartialIPAddress( name, addr );
709 }
710
711 hostentry = gethostbyname( name );
712 if ( !hostentry ) {
713 return -1;
714 }
715
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];
719
720 return 0;
721 } //end of the function WINS_GetAddrFromName
722 //===========================================================================
723 //
724 // Parameter: -
725 // Returns: -
726 // Changes Globals: -
727 //===========================================================================
728 int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
729 if ( addr1->sa_family != addr2->sa_family ) {
730 return -1;
731 }
732
733 if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
734 return -1;
735 }
736
737 if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
738 return 1;
739 }
740
741 return 0;
742 } //end of the function WINS_AddrCompare
743 //===========================================================================
744 //
745 // Parameter: -
746 // Returns: -
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 //===========================================================================
753 //
754 // Parameter: -
755 // Returns: -
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 );
760 return 0;
761 } //end of the function WINS_SetSocketPort