2 * WINGs WMHost function library
4 * Copyright (c) 1999 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.
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
33 typedef struct W_Host
{
43 static WMHashTable
*hostCache
= NULL
;
44 static Bool hostCacheEnabled
= True
;
47 /* Max hostname length (RFC 1123) */
48 #define W_MAXHOSTNAMELEN 255
52 getHostFromCache(char *name
)
57 return WMHashGet(hostCache
, name
);
62 getHostWithHostEntry(struct hostent
*host
, char *name
)
68 hPtr
= (WMHost
*)wmalloc(sizeof(WMHost
));
69 memset(hPtr
, 0, sizeof(WMHost
));
71 hPtr
->names
= WMCreateBag(1);
72 hPtr
->addresses
= WMCreateBag(1);
74 WMPutInBag(hPtr
->names
, wstrdup(host
->h_name
));
76 for (i
=0; host
->h_aliases
[i
]!=NULL
; i
++) {
77 WMPutInBag(hPtr
->names
, wstrdup(host
->h_aliases
[i
]));
80 for (i
=0; host
->h_addr_list
[i
]!=NULL
; i
++) {
81 memcpy((void*)&in
.s_addr
, (const void*)host
->h_addr_list
[i
],
83 WMPutInBag(hPtr
->addresses
, wstrdup(inet_ntoa(in
)));
88 if (hostCacheEnabled
) {
90 hostCache
= WMCreateHashTable(WMStringPointerHashCallbacks
);
91 hPtr
->name
= wstrdup(name
);
92 wassertr(WMHashInsert(hostCache
, hPtr
->name
, hPtr
)==NULL
);
103 char name
[W_MAXHOSTNAMELEN
+1];
105 if (gethostname(name
, W_MAXHOSTNAMELEN
) < 0) {
106 wsyserror("Cannot get current host name");
110 name
[W_MAXHOSTNAMELEN
] = '\0';
112 return WMGetHostWithName(name
);
117 WMGetHostWithName(char* name
)
119 struct hostent
*host
;
123 wwarning("NULL host name in 'WMGetHostWithName()'");
127 if (hostCacheEnabled
) {
128 if ((hPtr
= getHostFromCache(name
)) != NULL
) {
134 host
= gethostbyname(name
);
139 hPtr
= getHostWithHostEntry(host
, name
);
146 WMGetHostWithAddress(char* address
)
148 struct hostent
*host
;
152 if (address
== NULL
) {
153 wwarning("NULL address in 'WMGetHostWithAddress()'");
157 if (hostCacheEnabled
) {
158 if ((hPtr
= getHostFromCache(address
)) != NULL
) {
164 #ifndef HAVE_INET_ATON
165 if ((in
.s_addr
= inet_addr(address
)) == INADDR_NONE
)
168 if (inet_aton(address
, &in
.s_addr
) == 0)
172 host
= gethostbyaddr((char*)&in
, sizeof(in
), AF_INET
);
177 hPtr
= getHostWithHostEntry(host
, address
);
184 WMRetainHost(WMHost
*hPtr
)
192 WMReleaseHost(WMHost
*hPtr
)
198 if (hPtr
->refCount
> 0)
201 for (i
=0; i
<WMGetBagItemCount(hPtr
->names
); i
++)
202 wfree(WMGetFromBag(hPtr
->names
, i
));
203 for (i
=0; i
<WMGetBagItemCount(hPtr
->addresses
); i
++)
204 wfree(WMGetFromBag(hPtr
->addresses
, i
));
206 WMFreeBag(hPtr
->names
);
207 WMFreeBag(hPtr
->addresses
);
210 WMHashRemove(hostCache
, hPtr
->name
);
219 WMSetHostCacheEnabled(Bool flag
)
221 hostCacheEnabled
= flag
;
226 WMIsHostCacheEnabled()
228 return hostCacheEnabled
;
235 if (hostCache
&& WMCountHashTable(hostCache
)>0) {
236 WMBag
*hostBag
= WMCreateBag(WMCountHashTable(hostCache
));
237 WMHashEnumerator enumer
= WMEnumerateHashTable(hostCache
);
241 while ((hPtr
= WMNextHashEnumeratorItem(&enumer
))) {
242 /* we can't release the host here, because we can't change the
243 * hash while using the enumerator functions. */
244 WMPutInBag(hostBag
, hPtr
);
246 for (i
=0; i
<WMGetBagItemCount(hostBag
); i
++)
247 WMReleaseHost(WMGetFromBag(hostBag
, i
));
249 WMResetHashTable(hostCache
);
255 WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* aPtr
)
260 wassertrv(hPtr
!=NULL
&& aPtr
!=NULL
, False
);
265 for (i
=0; i
<WMGetBagItemCount(aPtr
->addresses
); i
++) {
266 adr1
= WMGetFromBag(aPtr
->addresses
, i
);
267 for (j
=0; j
<WMGetBagItemCount(hPtr
->addresses
); j
++) {
268 adr2
= WMGetFromBag(hPtr
->addresses
, j
);
269 if (strcmp(adr1
, adr2
)==0)
279 WMGetHostName(WMHost
*hPtr
)
281 return (WMGetBagItemCount(hPtr
->names
) > 0 ?
282 WMGetFromBag(hPtr
->names
, 0) : NULL
);
283 /*return WMGetFromBag(hPtr->names, 0);*/
288 WMGetHostNames(WMHost
*hPtr
)
295 WMGetHostAddress(WMHost
*hPtr
)
297 return (WMGetBagItemCount(hPtr
->addresses
) > 0 ?
298 WMGetFromBag(hPtr
->addresses
, 0) : NULL
);
303 WMGetHostAddresses(WMHost
*hPtr
)
305 return hPtr
->addresses
;