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