- Now when Window Maker calls wmsetbg to set the background, it will pass the
[wmaker-crm.git] / WINGs / host.c
blob212575ae60c100de195f2bca25b969781e9afbfc
1 /*
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"
24 #include <unistd.h>
25 #include <string.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include "WUtil.h"
34 /* For Solaris */
35 #ifndef INADDR_NONE
36 # define INADDR_NONE (-1)
37 #endif
39 /* Max hostname length (RFC 1123) */
40 #define W_MAXHOSTNAMELEN 255
43 typedef struct W_Host {
44 char *name;
46 WMArray *names;
47 WMArray *addresses;
49 int refCount;
50 } W_Host;
53 static WMHashTable *hostCache = NULL;
55 static Bool hostCacheEnabled = True;
59 static WMHost*
60 getHostFromCache(char *name)
62 if (!hostCache)
63 return NULL;
65 return WMHashGet(hostCache, name);
69 static WMHost*
70 getHostWithHostEntry(struct hostent *host, char *name)
72 WMHost *hPtr;
73 struct in_addr in;
74 int i;
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],
90 host->h_length);
91 WMAddToArray(hPtr->addresses, wstrdup(inet_ntoa(in)));
94 hPtr->refCount = 1;
96 if (hostCacheEnabled) {
97 if (!hostCache)
98 hostCache = WMCreateHashTable(WMStringPointerHashCallbacks);
99 hPtr->name = wstrdup(name);
100 wassertr(WMHashInsert(hostCache, hPtr->name, hPtr)==NULL);
101 hPtr->refCount++;
104 return hPtr;
108 WMHost*
109 WMGetCurrentHost()
111 char name[W_MAXHOSTNAMELEN+1];
113 if (gethostname(name, W_MAXHOSTNAMELEN) < 0) {
114 wsyserror("Cannot get current host name");
115 return NULL;
118 name[W_MAXHOSTNAMELEN] = 0;
120 return WMGetHostWithName(name);
124 WMHost*
125 WMGetHostWithName(char *name)
127 struct hostent *host;
128 WMHost *hPtr;
130 wassertrv(name!=NULL, NULL);
132 if (hostCacheEnabled) {
133 if ((hPtr = getHostFromCache(name)) != NULL) {
134 WMRetainHost(hPtr);
135 return hPtr;
139 host = gethostbyname(name);
140 if (host == NULL) {
141 return NULL;
144 hPtr = getHostWithHostEntry(host, name);
146 return hPtr;
150 WMHost*
151 WMGetHostWithAddress(char *address)
153 struct hostent *host;
154 struct in_addr in;
155 WMHost *hPtr;
157 wassertrv(address!=NULL, NULL);
159 if (hostCacheEnabled) {
160 if ((hPtr = getHostFromCache(address)) != NULL) {
161 WMRetainHost(hPtr);
162 return hPtr;
166 #ifndef HAVE_INET_ATON
167 if ((in.s_addr = inet_addr(address)) == INADDR_NONE)
168 return NULL;
169 #else
170 if (inet_aton(address, &in) == 0)
171 return NULL;
172 #endif
174 host = gethostbyaddr((char*)&in, sizeof(in), AF_INET);
175 if (host == NULL) {
176 return NULL;
179 hPtr = getHostWithHostEntry(host, address);
181 return hPtr;
185 WMHost*
186 WMRetainHost(WMHost *hPtr)
188 hPtr->refCount++;
189 return hPtr;
193 void
194 WMReleaseHost(WMHost *hPtr)
196 int i;
198 hPtr->refCount--;
200 if (hPtr->refCount > 0)
201 return;
203 WMFreeArray(hPtr->names);
204 WMFreeArray(hPtr->addresses);
206 if (hPtr->name) {
207 WMHashRemove(hostCache, hPtr->name);
208 wfree(hPtr->name);
211 wfree(hPtr);
215 void
216 WMSetHostCacheEnabled(Bool flag)
218 hostCacheEnabled = flag;
222 Bool
223 WMIsHostCacheEnabled()
225 return hostCacheEnabled;
229 void
230 WMFlushHostCache()
232 if (hostCache && WMCountHashTable(hostCache)>0) {
233 WMArray *hostArray = WMCreateArray(WMCountHashTable(hostCache));
234 WMHashEnumerator enumer = WMEnumerateHashTable(hostCache);
235 WMHost *hPtr;
236 int i;
238 while ((hPtr = WMNextHashEnumeratorItem(&enumer))) {
239 /* we can't release the host here, because we can't change the
240 * hash while using the enumerator functions. */
241 WMAddToArray(hostArray, hPtr);
243 for (i=0; i<WMGetArrayItemCount(hostArray); i++)
244 WMReleaseHost(WMGetFromArray(hostArray, i));
245 WMFreeArray(hostArray);
246 WMResetHashTable(hostCache);
251 Bool
252 WMIsHostEqualToHost(WMHost* hPtr, WMHost* aPtr)
254 int i, j;
255 char *adr1, *adr2;
257 wassertrv(hPtr!=NULL && aPtr!=NULL, False);
259 if (hPtr == aPtr)
260 return True;
262 for (i=0; i<WMGetArrayItemCount(aPtr->addresses); i++) {
263 adr1 = WMGetFromArray(aPtr->addresses, i);
264 // use WMFindInArray here --Dan
265 for (j=0; j<WMGetArrayItemCount(hPtr->addresses); j++) {
266 adr2 = WMGetFromArray(hPtr->addresses, j);
267 if (strcmp(adr1, adr2)==0)
268 return True;
272 return False;
276 char*
277 WMGetHostName(WMHost *hPtr)
279 return (WMGetArrayItemCount(hPtr->names) > 0 ?
280 WMGetFromArray(hPtr->names, 0) : NULL);
281 /*return WMGetFromArray(hPtr->names, 0);*/
285 WMArray*
286 WMGetHostNames(WMHost *hPtr)
288 return hPtr->names;
292 char*
293 WMGetHostAddress(WMHost *hPtr)
295 return (WMGetArrayItemCount(hPtr->addresses) > 0 ?
296 WMGetFromArray(hPtr->addresses, 0) : NULL);
300 WMArray*
301 WMGetHostAddresses(WMHost *hPtr)
303 return hPtr->addresses;