1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-20 21:08:08 +01:00

Fixes in the host class

This commit is contained in:
dan
1999-12-14 21:35:28 +00:00
parent bf4c63d657
commit 087fc7c82c
2 changed files with 94 additions and 53 deletions

View File

@@ -132,11 +132,16 @@ static void
didInitialize(ConnectionDelegate *self, WMConnection *cPtr) didInitialize(ConnectionDelegate *self, WMConnection *cPtr)
{ {
int state = WMGetConnectionState(cPtr); int state = WMGetConnectionState(cPtr);
WMHost *host;
if (state == WCConnected) { if (state == WCConnected) {
fprintf(stderr, "connected to '%s:%s'\n", WMGetConnectionAddress(cPtr), host = WMGetHostWithAddress(WMGetConnectionAddress(cPtr));
fprintf(stderr, "connected to '%s:%s'\n",
host?WMGetHostName(host):WMGetConnectionAddress(cPtr),
WMGetConnectionService(cPtr)); WMGetConnectionService(cPtr));
initialized = 1; initialized = 1;
if (host)
WMReleaseHost(host);
return; return;
} else { } else {
wsyserrorwithcode(WCErrorCode, "Unable to connect"); wsyserrorwithcode(WCErrorCode, "Unable to connect");

View File

@@ -1,8 +1,8 @@
/* /*
* WINGs WMHost function library * WINGs WMHost function library
* *
* Copyright (c) 1999 Dan Pascu * Copyright (c) 1999 Dan Pascu
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@@ -35,8 +35,6 @@ typedef struct W_Host {
WMBag *names; WMBag *names;
WMBag *addresses; WMBag *addresses;
int nameCount;
int addressCount;
int refCount; int refCount;
} W_Host; } W_Host;
@@ -49,6 +47,56 @@ static Bool hostCacheEnabled = True;
/* Max hostname length (RFC 1123) */ /* Max hostname length (RFC 1123) */
#define W_MAXHOSTNAMELEN 255 #define W_MAXHOSTNAMELEN 255
static WMHost*
getHostFromCache(char *name)
{
if (!hostCache)
return NULL;
return WMHashGet(hostCache, name);
}
static WMHost*
getHostWithHostEntry(struct hostent *host, char *name)
{
WMHost *hPtr;
struct in_addr in;
int i;
hPtr = (WMHost*)wmalloc(sizeof(WMHost));
memset(hPtr, 0, sizeof(WMHost));
hPtr->names = WMCreateBag(1);
hPtr->addresses = WMCreateBag(1);
WMPutInBag(hPtr->names, wstrdup(host->h_name));
for (i=0; host->h_aliases[i]!=NULL; i++) {
WMPutInBag(hPtr->names, wstrdup(host->h_aliases[i]));
}
for (i=0; host->h_addr_list[i]!=NULL; i++) {
memcpy((void*)&in.s_addr, (const void*)host->h_addr_list[i],
host->h_length);
WMPutInBag(hPtr->addresses, wstrdup(inet_ntoa(in)));
}
hPtr->refCount = 1;
if (hostCacheEnabled) {
if (!hostCache)
hostCache = WMCreateHashTable(WMStringPointerHashCallbacks);
hPtr->name = wstrdup(name);
wassertr(WMHashInsert(hostCache, hPtr->name, hPtr)==NULL);
hPtr->refCount++;
}
return hPtr;
}
WMHost* WMHost*
WMGetCurrentHost() WMGetCurrentHost()
{ {
@@ -69,59 +117,26 @@ WMHost*
WMGetHostWithName(char* name) WMGetHostWithName(char* name)
{ {
struct hostent *host; struct hostent *host;
struct in_addr in;
WMHost *hPtr; WMHost *hPtr;
int i;
if (name == NULL) { if (name == NULL) {
wwarning("NULL host name in 'WMGetHostWithName()'"); wwarning("NULL host name in 'WMGetHostWithName()'");
return NULL; return NULL;
} }
if (!hostCache) if (hostCacheEnabled) {
hostCache = WMCreateHashTable(WMStringPointerHashCallbacks); if ((hPtr = getHostFromCache(name)) != NULL) {
WMRetainHost(hPtr);
hPtr = WMHashGet(hostCache, name); return hPtr;
if (hPtr) { }
WMRetainHost(hPtr);
return hPtr;
} }
host = gethostbyname(name); host = gethostbyname(name);
if (host == NULL) { if (host == NULL) {
wwarning("Invalid host name/address '%s'", name);
return NULL; return NULL;
} }
hPtr = (WMHost*)wmalloc(sizeof(WMHost)); hPtr = getHostWithHostEntry(host, name);
memset(hPtr, 0, sizeof(WMHost));
hPtr->names = WMCreateBag(1);
hPtr->addresses = WMCreateBag(1);
WMPutInBag(hPtr->names, wstrdup(host->h_name));
for (i=0; host->h_aliases[i]!=NULL; i++) {
WMPutInBag(hPtr->names, wstrdup(host->h_aliases[i]));
}
hPtr->nameCount = WMGetBagItemCount(hPtr->names);
for (i=0; host->h_addr_list[i]!=NULL; i++) {
memcpy((void*)&in.s_addr, (const void*)host->h_addr_list[i],
host->h_length);
WMPutInBag(hPtr->addresses, wstrdup(inet_ntoa(in)));
}
hPtr->addressCount = WMGetBagItemCount(hPtr->addresses);
hPtr->refCount = 1;
if (hostCacheEnabled) {
hPtr->name = wstrdup(name);
wassertr(WMHashInsert(hostCache, hPtr->name, hPtr)==NULL);
hPtr->refCount++;
}
return hPtr; return hPtr;
} }
@@ -130,20 +145,38 @@ WMGetHostWithName(char* name)
WMHost* WMHost*
WMGetHostWithAddress(char* address) WMGetHostWithAddress(char* address)
{ {
struct hostent *host;
struct in_addr in;
WMHost *hPtr;
if (address == NULL) { if (address == NULL) {
wwarning("NULL address in 'WMGetHostWithAddress()'"); wwarning("NULL address in 'WMGetHostWithAddress()'");
return NULL; return NULL;
} }
return WMGetHostWithName(address); if (hostCacheEnabled) {
#if 0 if ((hPtr = getHostFromCache(address)) != NULL) {
hostaddr.s_addr = inet_addr((char*)[address cString]); WMRetainHost(hPtr);
if (hostaddr.s_addr == -1) { return hPtr;
}
}
#ifndef HAVE_INET_ATON
if ((in.s_addr = inet_addr(address)) == INADDR_NONE)
return NULL;
#else
if (inet_aton(address, &in.s_addr) == 0)
return NULL;
#endif
host = gethostbyaddr((char*)&in, sizeof(in), AF_INET);
if (host == NULL) {
return NULL; return NULL;
} }
h = gethostbyaddr((char*)&hostaddr, sizeof(hostaddr), AF_INET); hPtr = getHostWithHostEntry(host, address);
#endif
return hPtr;
} }
@@ -228,7 +261,7 @@ WMIsHostEqualToHost(WMHost* hPtr, WMHost* aPtr)
if (hPtr == aPtr) if (hPtr == aPtr)
return True; return True;
for (i=0; i<aPtr->addressCount; i++) { for (i=0; i<WMGetBagItemCount(aPtr->addresses); i++) {
if (WMGetFirstInBag(hPtr->addresses, WMGetFromBag(aPtr->addresses, i))) if (WMGetFirstInBag(hPtr->addresses, WMGetFromBag(aPtr->addresses, i)))
return True; return True;
} }
@@ -240,7 +273,9 @@ WMIsHostEqualToHost(WMHost* hPtr, WMHost* aPtr)
char* char*
WMGetHostName(WMHost *hPtr) WMGetHostName(WMHost *hPtr)
{ {
return WMGetFromBag(hPtr->names, 0); return (WMGetBagItemCount(hPtr->names) > 0 ?
WMGetFromBag(hPtr->names, 0) : NULL);
/*return WMGetFromBag(hPtr->names, 0);*/
} }
@@ -254,7 +289,8 @@ WMGetHostNames(WMHost *hPtr)
char* char*
WMGetHostAddress(WMHost *hPtr) WMGetHostAddress(WMHost *hPtr)
{ {
return (hPtr->addressCount > 0 ? WMGetFromBag(hPtr->addresses, 0) : NULL); return (WMGetBagItemCount(hPtr->addresses) > 0 ?
WMGetFromBag(hPtr->addresses, 0) : NULL);
} }