Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / host.c
Commit [+]AuthorDateLineData
9c59becb dan1999-12-14 01:07:24 +00001/*
2 * WINGs WMHost function library
087fc7c8 dan1999-12-14 21:35:28 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1999-2003 Dan Pascu
087fc7c8 dan1999-12-14 21:35:28 +00005 *
9c59becb dan1999-12-14 01:07:24 +00006 * 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.
10 *
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.
15 *
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.
19 */
20
882b9a8e kojima2001-07-23 20:31:32 +000021#include "wconfig.h"
22
9c59becb dan1999-12-14 01:07:24 +000023#include <unistd.h>
24#include <string.h>
25#include <netdb.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <arpa/inet.h>
29
30#include "WUtil.h"
31
badfd68a dan2000-04-17 21:52:14 +000032/* For Solaris */
33#ifndef INADDR_NONE
cf3bb1fd dan2000-04-17 21:24:58 +000034# define INADDR_NONE (-1)
15734b91 dan2000-03-30 02:38:22 +000035#endif
36
badfd68a dan2000-04-17 21:52:14 +000037/* Max hostname length (RFC 1123) */
38#define W_MAXHOSTNAMELEN 255
39
9c59becb dan1999-12-14 01:07:24 +000040typedef struct W_Host {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020041 char *name;
9c59becb dan1999-12-14 01:07:24 +000042
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020043 WMArray *names;
44 WMArray *addresses;
9c59becb dan1999-12-14 01:07:24 +000045
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020046 int refCount;
9c59becb dan1999-12-14 01:07:24 +000047} W_Host;
48
9c59becb dan1999-12-14 01:07:24 +000049static WMHashTable *hostCache = NULL;
9c59becb dan1999-12-14 01:07:24 +000050
badfd68a dan2000-04-17 21:52:14 +000051static Bool hostCacheEnabled = True;
9c59becb dan1999-12-14 01:07:24 +000052
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020053static WMHost *getHostFromCache(char *name)
087fc7c8 dan1999-12-14 21:35:28 +000054{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020055 if (!hostCache)
56 return NULL;
9c59becb dan1999-12-14 01:07:24 +000057
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020058 return WMHashGet(hostCache, name);
9c59becb dan1999-12-14 01:07:24 +000059}
60
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020061static WMHost *getHostWithHostEntry(struct hostent *host, char *name)
9c59becb dan1999-12-14 01:07:24 +000062{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020063 WMHost *hPtr;
64 struct in_addr in;
65 int i;
9c59becb dan1999-12-14 01:07:24 +000066
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020067 hPtr = (WMHost *) wmalloc(sizeof(WMHost));
68 memset(hPtr, 0, sizeof(WMHost));
9c59becb dan1999-12-14 01:07:24 +000069
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020070 hPtr->names = WMCreateArrayWithDestructor(1, wfree);
71 hPtr->addresses = WMCreateArrayWithDestructor(1, wfree);
9c59becb dan1999-12-14 01:07:24 +000072
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020073 WMAddToArray(hPtr->names, wstrdup(host->h_name));
9c59becb dan1999-12-14 01:07:24 +000074
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020075 for (i = 0; host->h_aliases[i] != NULL; i++) {
76 WMAddToArray(hPtr->names, wstrdup(host->h_aliases[i]));
77 }
9c59becb dan1999-12-14 01:07:24 +000078
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020079 for (i = 0; host->h_addr_list[i] != NULL; i++) {
80 memcpy((void *)&in.s_addr, (const void *)host->h_addr_list[i], host->h_length);
81 WMAddToArray(hPtr->addresses, wstrdup(inet_ntoa(in)));
82 }
9c59becb dan1999-12-14 01:07:24 +000083
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020084 hPtr->refCount = 1;
9c59becb dan1999-12-14 01:07:24 +000085
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020086 if (hostCacheEnabled) {
87 if (!hostCache)
88 hostCache = WMCreateHashTable(WMStringPointerHashCallbacks);
89 hPtr->name = wstrdup(name);
90 wassertr(WMHashInsert(hostCache, hPtr->name, hPtr) == NULL);
91 hPtr->refCount++;
92 }
9c59becb dan1999-12-14 01:07:24 +000093
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020094 return hPtr;
9c59becb dan1999-12-14 01:07:24 +000095}
96
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020097WMHost *WMGetCurrentHost()
087fc7c8 dan1999-12-14 21:35:28 +000098{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099 char name[W_MAXHOSTNAMELEN + 1];
087fc7c8 dan1999-12-14 21:35:28 +0000100
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200101 if (gethostname(name, W_MAXHOSTNAMELEN) < 0) {
102 wsyserror(_("Cannot get current host name"));
103 return NULL;
104 }
087fc7c8 dan1999-12-14 21:35:28 +0000105
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200106 name[W_MAXHOSTNAMELEN] = 0;
087fc7c8 dan1999-12-14 21:35:28 +0000107
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200108 return WMGetHostWithName(name);
087fc7c8 dan1999-12-14 21:35:28 +0000109}
110
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200111WMHost *WMGetHostWithName(char *name)
087fc7c8 dan1999-12-14 21:35:28 +0000112{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200113 struct hostent *host;
114 WMHost *hPtr;
087fc7c8 dan1999-12-14 21:35:28 +0000115
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200116 wassertrv(name != NULL, NULL);
087fc7c8 dan1999-12-14 21:35:28 +0000117
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200118 if (hostCacheEnabled) {
119 if ((hPtr = getHostFromCache(name)) != NULL) {
120 WMRetainHost(hPtr);
121 return hPtr;
122 }
123 }
087fc7c8 dan1999-12-14 21:35:28 +0000124
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200125 host = gethostbyname(name);
126 if (host == NULL) {
127 return NULL;
128 }
087fc7c8 dan1999-12-14 21:35:28 +0000129
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130 hPtr = getHostWithHostEntry(host, name);
087fc7c8 dan1999-12-14 21:35:28 +0000131
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200132 return hPtr;
087fc7c8 dan1999-12-14 21:35:28 +0000133}
134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200135WMHost *WMGetHostWithAddress(char *address)
9c59becb dan1999-12-14 01:07:24 +0000136{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 struct hostent *host;
138 struct in_addr in;
139 WMHost *hPtr;
140
141 wassertrv(address != NULL, NULL);
142
143 if (hostCacheEnabled) {
144 if ((hPtr = getHostFromCache(address)) != NULL) {
145 WMRetainHost(hPtr);
146 return hPtr;
147 }
148 }
087fc7c8 dan1999-12-14 21:35:28 +0000149#ifndef HAVE_INET_ATON
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200150 if ((in.s_addr = inet_addr(address)) == INADDR_NONE)
151 return NULL;
087fc7c8 dan1999-12-14 21:35:28 +0000152#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200153 if (inet_aton(address, &in) == 0)
154 return NULL;
9c59becb dan1999-12-14 01:07:24 +0000155#endif
087fc7c8 dan1999-12-14 21:35:28 +0000156
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200157 host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
158 if (host == NULL) {
159 return NULL;
160 }
087fc7c8 dan1999-12-14 21:35:28 +0000161
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200162 hPtr = getHostWithHostEntry(host, address);
087fc7c8 dan1999-12-14 21:35:28 +0000163
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200164 return hPtr;
9c59becb dan1999-12-14 01:07:24 +0000165}
166
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200167WMHost *WMRetainHost(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000168{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200169 hPtr->refCount++;
170 return hPtr;
9c59becb dan1999-12-14 01:07:24 +0000171}
172
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200173void WMReleaseHost(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000174{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 hPtr->refCount--;
9c59becb dan1999-12-14 01:07:24 +0000176
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200177 if (hPtr->refCount > 0)
178 return;
9c59becb dan1999-12-14 01:07:24 +0000179
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200180 WMFreeArray(hPtr->names);
181 WMFreeArray(hPtr->addresses);
9c59becb dan1999-12-14 01:07:24 +0000182
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 if (hPtr->name) {
184 WMHashRemove(hostCache, hPtr->name);
185 wfree(hPtr->name);
186 }
9c59becb dan1999-12-14 01:07:24 +0000187
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200188 wfree(hPtr);
9c59becb dan1999-12-14 01:07:24 +0000189}
190
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200191void WMSetHostCacheEnabled(Bool flag)
9c59becb dan1999-12-14 01:07:24 +0000192{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200193 hostCacheEnabled = ((flag == 0) ? 0 : 1);
9c59becb dan1999-12-14 01:07:24 +0000194}
195
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200196Bool WMIsHostCacheEnabled()
9c59becb dan1999-12-14 01:07:24 +0000197{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200198 return hostCacheEnabled;
9c59becb dan1999-12-14 01:07:24 +0000199}
200
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200201void WMFlushHostCache()
9c59becb dan1999-12-14 01:07:24 +0000202{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203 if (hostCache && WMCountHashTable(hostCache) > 0) {
204 WMArray *hostArray = WMCreateArray(WMCountHashTable(hostCache));
205 WMHashEnumerator enumer = WMEnumerateHashTable(hostCache);
206 WMHost *hPtr;
207 int i;
208
209 while ((hPtr = WMNextHashEnumeratorItem(&enumer))) {
210 /* we can't release the host here, because we can't change the
211 * hash while using the enumerator functions. */
212 WMAddToArray(hostArray, hPtr);
213 }
214 for (i = 0; i < WMGetArrayItemCount(hostArray); i++)
215 WMReleaseHost(WMGetFromArray(hostArray, i));
216 WMFreeArray(hostArray);
217 WMResetHashTable(hostCache);
218 }
9c59becb dan1999-12-14 01:07:24 +0000219}
220
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200221static int matchAddress(void *item, void *cdata)
e0235bb6 dan2001-04-10 22:00:41 +0000222{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200223 return (strcmp((char *)item, (char *)cdata) == 0);
e0235bb6 dan2001-04-10 22:00:41 +0000224}
225
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200226Bool WMIsHostEqualToHost(WMHost * hPtr, WMHost * aPtr)
9c59becb dan1999-12-14 01:07:24 +0000227{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200228 char *adr;
229 int i;
9c59becb dan1999-12-14 01:07:24 +0000230
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200231 wassertrv(hPtr != NULL && aPtr != NULL, False);
9c59becb dan1999-12-14 01:07:24 +0000232
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200233 if (hPtr == aPtr)
234 return True;
9c59becb dan1999-12-14 01:07:24 +0000235
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200236 for (i = 0; i < WMGetArrayItemCount(aPtr->addresses); i++) {
237 adr = WMGetFromArray(aPtr->addresses, i);
238 if (WMFindInArray(hPtr->addresses, matchAddress, adr) != WANotFound) {
239 return True;
240 }
241 }
9c59becb dan1999-12-14 01:07:24 +0000242
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200243 return False;
9c59becb dan1999-12-14 01:07:24 +0000244}
245
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200246char *WMGetHostName(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000247{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200248 return (WMGetArrayItemCount(hPtr->names) > 0 ? WMGetFromArray(hPtr->names, 0) : NULL);
249 /*return WMGetFromArray(hPtr->names, 0); */
9c59becb dan1999-12-14 01:07:24 +0000250}
251
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200252WMArray *WMGetHostNames(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000253{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200254 return hPtr->names;
9c59becb dan1999-12-14 01:07:24 +0000255}
256
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200257char *WMGetHostAddress(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000258{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200259 return (WMGetArrayItemCount(hPtr->addresses) > 0 ? WMGetFromArray(hPtr->addresses, 0) : NULL);
9c59becb dan1999-12-14 01:07:24 +0000260}
261
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200262WMArray *WMGetHostAddresses(WMHost * hPtr)
9c59becb dan1999-12-14 01:07:24 +0000263{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200264 return hPtr->addresses;
9c59becb dan1999-12-14 01:07:24 +0000265}