2 * WINGs WMHost function library
4 * Copyright (c) 1999-2003 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.
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
36 # define INADDR_NONE (-1)
39 /* Max hostname length (RFC 1123) */
40 #define W_MAXHOSTNAMELEN 255
43 typedef struct W_Host
{
53 static WMHashTable
*hostCache
= NULL
;
55 static Bool hostCacheEnabled
= True
;
60 getHostFromCache(char *name
)
65 return WMHashGet(hostCache
, name
);
70 getHostWithHostEntry(struct hostent
*host
, char *name
)
76 hPtr
= (WMHost
*)wmalloc(sizeof(WMHost
));
77 memset(hPtr
, 0, sizeof(WMHost
));
79 hPtr
->names
= WMCreateArrayWithDestructor(1, wfree
);
80 hPtr
->addresses
= WMCreateArrayWithDestructor(1, wfree
);
82 WMAddToArray(hPtr
->names
, wstrdup(host
->h_name
));
84 for (i
=0; host
->h_aliases
[i
]!=NULL
; i
++) {
85 WMAddToArray(hPtr
->names
, wstrdup(host
->h_aliases
[i
]));
88 for (i
=0; host
->h_addr_list
[i
]!=NULL
; i
++) {
89 memcpy((void*)&in
.s_addr
, (const void*)host
->h_addr_list
[i
],
91 WMAddToArray(hPtr
->addresses
, wstrdup(inet_ntoa(in
)));
96 if (hostCacheEnabled
) {
98 hostCache
= WMCreateHashTable(WMStringPointerHashCallbacks
);
99 hPtr
->name
= wstrdup(name
);
100 wassertr(WMHashInsert(hostCache
, hPtr
->name
, hPtr
)==NULL
);
111 char name
[W_MAXHOSTNAMELEN
+1];
113 if (gethostname(name
, W_MAXHOSTNAMELEN
) < 0) {
114 wsyserror(_("Cannot get current host name"));
118 name
[W_MAXHOSTNAMELEN
] = 0;
120 return WMGetHostWithName(name
);
125 WMGetHostWithName(char *name
)
127 struct hostent
*host
;
130 wassertrv(name
!=NULL
, NULL
);
132 if (hostCacheEnabled
) {
133 if ((hPtr
= getHostFromCache(name
)) != NULL
) {
139 host
= gethostbyname(name
);
144 hPtr
= getHostWithHostEntry(host
, name
);
151 WMGetHostWithAddress(char *address
)
153 struct hostent
*host
;
157 wassertrv(address
!=NULL
, NULL
);
159 if (hostCacheEnabled
) {
160 if ((hPtr
= getHostFromCache(address
)) != NULL
) {
166 #ifndef HAVE_INET_ATON
167 if ((in
.s_addr
= inet_addr(address
)) == INADDR_NONE
)
170 if (inet_aton(address
, &in
) == 0)
174 host
= gethostbyaddr((char*)&in
, sizeof(in
), AF_INET
);
179 hPtr
= getHostWithHostEntry(host
, address
);
186 WMRetainHost(WMHost
*hPtr
)
194 WMReleaseHost(WMHost
*hPtr
)
198 if (hPtr
->refCount
> 0)
201 WMFreeArray(hPtr
->names
);
202 WMFreeArray(hPtr
->addresses
);
205 WMHashRemove(hostCache
, hPtr
->name
);
214 WMSetHostCacheEnabled(Bool flag
)
216 hostCacheEnabled
= ((flag
==0) ? 0 : 1);
221 WMIsHostCacheEnabled()
223 return hostCacheEnabled
;
230 if (hostCache
&& WMCountHashTable(hostCache
)>0) {
231 WMArray
*hostArray
= WMCreateArray(WMCountHashTable(hostCache
));
232 WMHashEnumerator enumer
= WMEnumerateHashTable(hostCache
);
236 while ((hPtr
= WMNextHashEnumeratorItem(&enumer
))) {
237 /* we can't release the host here, because we can't change the
238 * hash while using the enumerator functions. */
239 WMAddToArray(hostArray
, hPtr
);
241 for (i
=0; i
<WMGetArrayItemCount(hostArray
); i
++)
242 WMReleaseHost(WMGetFromArray(hostArray
, i
));
243 WMFreeArray(hostArray
);
244 WMResetHashTable(hostCache
);
250 matchAddress(void *item
, void *cdata
)
252 return (strcmp((char*) item
, (char*) cdata
)==0);
257 WMIsHostEqualToHost(WMHost
* hPtr
, WMHost
* aPtr
)
262 wassertrv(hPtr
!=NULL
&& aPtr
!=NULL
, False
);
267 for (i
=0; i
<WMGetArrayItemCount(aPtr
->addresses
); i
++) {
268 adr
= WMGetFromArray(aPtr
->addresses
, i
);
269 if (WMFindInArray(hPtr
->addresses
, matchAddress
, adr
) != WANotFound
) {
279 WMGetHostName(WMHost
*hPtr
)
281 return (WMGetArrayItemCount(hPtr
->names
) > 0 ?
282 WMGetFromArray(hPtr
->names
, 0) : NULL
);
283 /*return WMGetFromArray(hPtr->names, 0);*/
288 WMGetHostNames(WMHost
*hPtr
)
295 WMGetHostAddress(WMHost
*hPtr
)
297 return (WMGetArrayItemCount(hPtr
->addresses
) > 0 ?
298 WMGetFromArray(hPtr
->addresses
, 0) : NULL
);
303 WMGetHostAddresses(WMHost
*hPtr
)
305 return hPtr
->addresses
;