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