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