2 * WINGs WMHost function library
4 * Copyright (c) 1999-2000 Dan Pascu
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "../src/config.h"
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
34 /* For some Solaris systems */
35 #if !defined(HAVE_INET_ATON) && !defined(INADDR_NONE)
36 # define INADDR_NONE (-1)
40 typedef struct W_Host
{
50 static WMHashTable
*hostCache
= NULL
;
51 static Bool hostCacheEnabled
= True
;
54 /* Max hostname length (RFC 1123) */
55 #define W_MAXHOSTNAMELEN 255
59 getHostFromCache(char *name
)
64 return WMHashGet(hostCache
, name
);
69 getHostWithHostEntry(struct hostent
*host
, char *name
)
75 hPtr
= (WMHost
*)wmalloc(sizeof(WMHost
));
76 memset(hPtr
, 0, sizeof(WMHost
));
78 hPtr
->names
= WMCreateBag(1);
79 hPtr
->addresses
= WMCreateBag(1);
81 WMPutInBag(hPtr
->names
, wstrdup(host
->h_name
));
83 for (i
=0; host
->h_aliases
[i
]!=NULL
; i
++) {
84 WMPutInBag(hPtr
->names
, wstrdup(host
->h_aliases
[i
]));
87 for (i
=0; host
->h_addr_list
[i
]!=NULL
; i
++) {
88 memcpy((void*)&in
.s_addr
, (const void*)host
->h_addr_list
[i
],
90 WMPutInBag(hPtr
->addresses
, wstrdup(inet_ntoa(in
)));
95 if (hostCacheEnabled
) {
97 hostCache
= WMCreateHashTable(WMStringPointerHashCallbacks
);
98 hPtr
->name
= wstrdup(name
);
99 wassertr(WMHashInsert(hostCache
, hPtr
->name
, hPtr
)==NULL
);
110 char name
[W_MAXHOSTNAMELEN
+1];
112 if (gethostname(name
, W_MAXHOSTNAMELEN
) < 0) {
113 wsyserror("Cannot get current host name");
117 name
[W_MAXHOSTNAMELEN
] = '\0';
119 return WMGetHostWithName(name
);
124 WMGetHostWithName(char* name
)
126 struct hostent
*host
;
130 wwarning("NULL host name in 'WMGetHostWithName()'");
134 if (hostCacheEnabled
) {
135 if ((hPtr
= getHostFromCache(name
)) != NULL
) {
141 host
= gethostbyname(name
);
146 hPtr
= getHostWithHostEntry(host
, name
);
153 WMGetHostWithAddress(char* address
)
155 struct hostent
*host
;
159 if (address
== NULL
) {
160 wwarning("NULL address in 'WMGetHostWithAddress()'");
164 if (hostCacheEnabled
) {
165 if ((hPtr
= getHostFromCache(address
)) != NULL
) {
171 #ifndef HAVE_INET_ATON
172 if ((in
.s_addr
= inet_addr(address
)) == INADDR_NONE
)
175 if (inet_aton(address
, &in
) == 0)
179 host
= gethostbyaddr((char*)&in
, sizeof(in
), AF_INET
);
184 hPtr
= getHostWithHostEntry(host
, address
);
191 WMRetainHost(WMHost
*hPtr
)
199 WMReleaseHost(WMHost
*hPtr
)
205 if (hPtr
->refCount
> 0)
208 for (i
=0; i
<WMGetBagItemCount(hPtr
->names
); i
++)
209 wfree(WMGetFromBag(hPtr
->names
, i
));
210 for (i
=0; i
<WMGetBagItemCount(hPtr
->addresses
); i
++)
211 wfree(WMGetFromBag(hPtr
->addresses
, i
));
213 WMFreeBag(hPtr
->names
);
214 WMFreeBag(hPtr
->addresses
);
217 WMHashRemove(hostCache
, hPtr
->name
);
226 WMSetHostCacheEnabled(Bool flag
)
228 hostCacheEnabled
= flag
;
233 WMIsHostCacheEnabled()
235 return hostCacheEnabled
;
242 if (hostCache
&& WMCountHashTable(hostCache
)>0) {
243 WMBag
*hostBag
= WMCreateBag(WMCountHashTable(hostCache
));
244 WMHashEnumerator enumer
= WMEnumerateHashTable(hostCache
);
248 while ((hPtr
= WMNextHashEnumeratorItem(&enumer
))) {
249 /* we can't release the host here, because we can't change the
250 * hash while using the enumerator functions. */
251 WMPutInBag(hostBag
, hPtr
);
253 for (i
=0; i
<WMGetBagItemCount(hostBag
); i
++)
254 WMReleaseHost(WMGetFromBag(hostBag
, i
));
256 WMResetHashTable(hostCache
);
262 WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* aPtr
)
267 wassertrv(hPtr
!=NULL
&& aPtr
!=NULL
, False
);
272 for (i
=0; i
<WMGetBagItemCount(aPtr
->addresses
); i
++) {
273 adr1
= WMGetFromBag(aPtr
->addresses
, i
);
274 for (j
=0; j
<WMGetBagItemCount(hPtr
->addresses
); j
++) {
275 adr2
= WMGetFromBag(hPtr
->addresses
, j
);
276 if (strcmp(adr1
, adr2
)==0)
286 WMGetHostName(WMHost
*hPtr
)
288 return (WMGetBagItemCount(hPtr
->names
) > 0 ?
289 WMGetFromBag(hPtr
->names
, 0) : NULL
);
290 /*return WMGetFromBag(hPtr->names, 0);*/
295 WMGetHostNames(WMHost
*hPtr
)
302 WMGetHostAddress(WMHost
*hPtr
)
304 return (WMGetBagItemCount(hPtr
->addresses
) > 0 ?
305 WMGetFromBag(hPtr
->addresses
, 0) : NULL
);
310 WMGetHostAddresses(WMHost
*hPtr
)
312 return hPtr
->addresses
;