1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-01-07 22:34:18 +01:00

- removed calls to wsyserror() and wwarning() inside the WMHost and

WMConnection code. do call by yourself wsyserrorwithcode() passing
  WCErrorCode, whenever a call don't return the expected value, and
  WCErrorCode > 0. If WCErrorCode==0 that is not an system error,
  condition, so don't call wsyserrorwithcode() in that case.
- added some assertions where appropriate
This commit is contained in:
dan
2000-11-24 10:31:10 +00:00
parent a9b75e03e2
commit ad95610321
3 changed files with 29 additions and 53 deletions

View File

@@ -67,6 +67,12 @@ changes since wmaker 0.62.1:
close on exec flag set automatically to 'True' by the library. Unless close on exec flag set automatically to 'True' by the library. Unless
you want to let the socket of some connection to survive across an exec* you want to let the socket of some connection to survive across an exec*
call, you need not to call this function. call, you need not to call this function.
- removed all the wsyserror() and wwarning() calls from host.c and
connection.c and replaced where appropriate with assertions. If a function
returns some invalid result, you can still get the system error message if
you need, by calling wsyserrorwithcode() and passing WCErrorCode, if
WCErrorCode > 0. If WCErrorCode==0, that is not a system error, and
wsyserrorwithcode() should not be called in this case.
changes since wmaker 0.62.0: changes since wmaker 0.62.0:

View File

@@ -23,7 +23,8 @@
* TODO: * TODO:
* - decide if we want to support connections with external sockets, else * - decide if we want to support connections with external sockets, else
* clean up the structure of the unneeded members. * clean up the structure of the unneeded members.
* - decide what to do with all wsyserror() and wwarning() calls. * - decide what to do with all wsyserror() and wwarning() calls that are
* still there.
* *
*/ */
@@ -197,6 +198,7 @@ inputHandler(int fd, int mask, void *clientData) /*FOLD00*/
int result; int result;
int len = sizeof(result); int len = sizeof(result);
WCErrorCode = 0;
if (getsockopt(cPtr->sock, SOL_SOCKET, SO_ERROR, if (getsockopt(cPtr->sock, SOL_SOCKET, SO_ERROR,
(void*)&result, &len) == 0 && result != 0) { (void*)&result, &len) == 0 && result != 0) {
cPtr->state = WCFailed; cPtr->state = WCFailed;
@@ -256,7 +258,7 @@ setSocketNonBlocking(int sock, Bool flag) /*FOLD00*/
state = fcntl(sock, F_GETFL, 0); state = fcntl(sock, F_GETFL, 0);
if (state < 0) { if (state < 0) {
/*wsyserror("Failed to get socket flags with fcntl."); should we do this? -Dan*/ /* set WCErrorCode here? -Dan*/
return False; return False;
} }
@@ -273,7 +275,7 @@ setSocketNonBlocking(int sock, Bool flag) /*FOLD00*/
} }
if (fcntl(sock, F_SETFL, state) < 0) { if (fcntl(sock, F_SETFL, state) < 0) {
/*wsyserror("Failed to set socket flags with fcntl."); should we do this? -Dan */ /* set WCErrorCode here? -Dan*/
return False; return False;
} }
@@ -299,7 +301,7 @@ getSocketAddress(char* name, char* service, char* protocol) /*FOLD00*/
static struct sockaddr_in socketaddr; static struct sockaddr_in socketaddr;
struct servent *sp; struct servent *sp;
if (!protocol || protocol[0]=='\0') if (!protocol || protocol[0]==0)
protocol = "tcp"; protocol = "tcp";
memset(&socketaddr, 0, sizeof(struct sockaddr_in)); memset(&socketaddr, 0, sizeof(struct sockaddr_in));
@@ -310,7 +312,7 @@ getSocketAddress(char* name, char* service, char* protocol) /*FOLD00*/
* Otherwise we expect the given name to be an address unless it is * Otherwise we expect the given name to be an address unless it is
* NULL (any address). * NULL (any address).
*/ */
if (name && name[0]!='\0') { if (name && name[0]!=0) {
WMHost *host = WMGetHostWithName(name); WMHost *host = WMGetHostWithName(name);
if (!host) if (!host)
@@ -330,7 +332,7 @@ getSocketAddress(char* name, char* service, char* protocol) /*FOLD00*/
socketaddr.sin_addr.s_addr = htonl(INADDR_ANY); socketaddr.sin_addr.s_addr = htonl(INADDR_ANY);
} }
if (!service || service[0]=='\0') { if (!service || service[0]==0) {
socketaddr.sin_port = 0; socketaddr.sin_port = 0;
} else if ((sp = getservbyname(service, protocol))==0) { } else if ((sp = getservbyname(service, protocol))==0) {
char *endptr; char *endptr;
@@ -338,7 +340,7 @@ getSocketAddress(char* name, char* service, char* protocol) /*FOLD00*/
portNumber = strtoul(service, &endptr, 10); portNumber = strtoul(service, &endptr, 10);
if (service[0]!='\0' && *endptr=='\0' && portNumber<65536) { if (service[0]!=0 && *endptr==0 && portNumber<65536) {
socketaddr.sin_port = htons(portNumber); socketaddr.sin_port = htons(portNumber);
} else { } else {
return NULL; return NULL;
@@ -442,8 +444,9 @@ WMCreateConnectionAsServerAtAddress(char *host, char *service, char *protocol) /
struct sockaddr_in *socketaddr; struct sockaddr_in *socketaddr;
int sock, size, on; int sock, size, on;
WCErrorCode = 0;
if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) { if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) {
WCErrorCode = 0;
wwarning("Bad address-service-protocol combination"); wwarning("Bad address-service-protocol combination");
return NULL; return NULL;
} }
@@ -452,7 +455,6 @@ WMCreateConnectionAsServerAtAddress(char *host, char *service, char *protocol) /
sock = socket(PF_INET, SOCK_STREAM, 0); sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock<0) { if (sock<0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to create socket");
return NULL; return NULL;
} }
@@ -466,17 +468,12 @@ WMCreateConnectionAsServerAtAddress(char *host, char *service, char *protocol) /
if (bind(sock, (struct sockaddr *)socketaddr, sizeof(*socketaddr)) < 0) { if (bind(sock, (struct sockaddr *)socketaddr, sizeof(*socketaddr)) < 0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to bind to address '%s:%hu'",
inet_ntoa(socketaddr->sin_addr),
ntohs(socketaddr->sin_port));
close(sock); close(sock);
return NULL; return NULL;
} }
if (listen(sock, 10) < 0) { if (listen(sock, 10) < 0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to listen on port '%hu'",
ntohs(socketaddr->sin_port));
close(sock); close(sock);
return NULL; return NULL;
} }
@@ -486,7 +483,6 @@ WMCreateConnectionAsServerAtAddress(char *host, char *service, char *protocol) /
size = sizeof(*socketaddr); size = sizeof(*socketaddr);
if (getsockname(sock, (struct sockaddr*)socketaddr, &size) < 0) { if (getsockname(sock, (struct sockaddr*)socketaddr, &size) < 0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to get socket address");
close(sock); close(sock);
return NULL; return NULL;
} }
@@ -508,17 +504,14 @@ WMCreateConnectionToAddress(char *host, char *service, char *protocol) /*FOLD00*
struct sockaddr_in *socketaddr; struct sockaddr_in *socketaddr;
int sock; int sock;
if (service==NULL || service[0]=='\0') { WCErrorCode = 0;
WCErrorCode = 0;
wwarning("Bad argument - service is not specified");
return NULL;
}
if (host==NULL || host[0]=='\0') wassertrv(service!=NULL && service[0]!=0, NULL);
if (host==NULL || host[0]==0)
host = "localhost"; host = "localhost";
if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) { if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) {
WCErrorCode = 0;
wwarning("Bad address-service-protocol combination"); wwarning("Bad address-service-protocol combination");
return NULL; return NULL;
} }
@@ -527,16 +520,12 @@ WMCreateConnectionToAddress(char *host, char *service, char *protocol) /*FOLD00*
sock = socket(PF_INET, SOCK_STREAM, 0); sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock<0) { if (sock<0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to create socket");
return NULL; return NULL;
} }
/* make socket blocking while we connect. */ /* make socket blocking while we connect. */
setSocketNonBlocking(sock, False); setSocketNonBlocking(sock, False);
if (connect(sock, (struct sockaddr*)socketaddr, sizeof(*socketaddr)) < 0) { if (connect(sock, (struct sockaddr*)socketaddr, sizeof(*socketaddr)) < 0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to make connection to address '%s:%hu'",
inet_ntoa(socketaddr->sin_addr),
ntohs(socketaddr->sin_port));
close(sock); close(sock);
return NULL; return NULL;
} }
@@ -558,17 +547,14 @@ WMCreateConnectionToAddressAndNotify(char *host, char *service, char *protocol)
int sock; int sock;
Bool isNonBlocking; Bool isNonBlocking;
if (service==NULL || service[0]=='\0') { WCErrorCode = 0;
WCErrorCode = 0;
wwarning("Bad argument - service is not specified");
return NULL;
}
if (host==NULL || host[0]=='\0') wassertrv(service!=NULL && service[0]!=0, NULL);
if (host==NULL || host[0]==0)
host = "localhost"; host = "localhost";
if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) { if ((socketaddr = getSocketAddress(host, service, protocol)) == NULL) {
WCErrorCode = 0;
wwarning("Bad address-service-protocol combination"); wwarning("Bad address-service-protocol combination");
return NULL; return NULL;
} }
@@ -577,16 +563,12 @@ WMCreateConnectionToAddressAndNotify(char *host, char *service, char *protocol)
sock = socket(PF_INET, SOCK_STREAM, 0); sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock<0) { if (sock<0) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to create socket");
return NULL; return NULL;
} }
isNonBlocking = setSocketNonBlocking(sock, True); isNonBlocking = setSocketNonBlocking(sock, True);
if (connect(sock, (struct sockaddr*)socketaddr, sizeof(*socketaddr)) < 0) { if (connect(sock, (struct sockaddr*)socketaddr, sizeof(*socketaddr)) < 0) {
if (errno!=EINPROGRESS) { if (errno!=EINPROGRESS) {
WCErrorCode = errno; WCErrorCode = errno;
wsyserror("Unable to make connection to address '%s:%hu'",
inet_ntoa(socketaddr->sin_addr),
ntohs(socketaddr->sin_port));
close(sock); close(sock);
return NULL; return NULL;
} }
@@ -681,12 +663,7 @@ WMAcceptConnection(WMConnection *listener) /*FOLD00*/
size = sizeof(clientname); size = sizeof(clientname);
newSock = accept(listener->sock, (struct sockaddr*) &clientname, &size); newSock = accept(listener->sock, (struct sockaddr*) &clientname, &size);
if (newSock<0) { if (newSock<0) {
if (errno!=EAGAIN && errno!=EWOULDBLOCK) { WCErrorCode = ((errno!=EAGAIN && errno!=EWOULDBLOCK) ? errno : 0);
WCErrorCode = errno;
wsyserror("Could not accept connection");
} else {
WCErrorCode = 0;
}
return NULL; return NULL;
} }
@@ -918,7 +895,6 @@ WMIsConnectionNonBlocking(WMConnection *cPtr) /*FOLD00*/
state = fcntl(cPtr->sock, F_GETFL, 0); state = fcntl(cPtr->sock, F_GETFL, 0);
if (state < 0) { if (state < 0) {
/*wsyserror("Failed to get socket flags with fcntl.");*/
/* If we can't use fcntl on socket, this probably also means we could /* If we can't use fcntl on socket, this probably also means we could
* not use fcntl to set non-blocking mode, and since a socket defaults * not use fcntl to set non-blocking mode, and since a socket defaults
* to blocking when created, return False as the best assumption */ * to blocking when created, return False as the best assumption */

View File

@@ -115,7 +115,7 @@ WMGetCurrentHost()
return NULL; return NULL;
} }
name[W_MAXHOSTNAMELEN] = '\0'; name[W_MAXHOSTNAMELEN] = 0;
return WMGetHostWithName(name); return WMGetHostWithName(name);
} }
@@ -127,10 +127,7 @@ WMGetHostWithName(char *name)
struct hostent *host; struct hostent *host;
WMHost *hPtr; WMHost *hPtr;
if (name == NULL) { wassertrv(name!=NULL, NULL);
wwarning("NULL host name in 'WMGetHostWithName()'");
return NULL;
}
if (hostCacheEnabled) { if (hostCacheEnabled) {
if ((hPtr = getHostFromCache(name)) != NULL) { if ((hPtr = getHostFromCache(name)) != NULL) {
@@ -157,10 +154,7 @@ WMGetHostWithAddress(char *address)
struct in_addr in; struct in_addr in;
WMHost *hPtr; WMHost *hPtr;
if (address == NULL) { wassertrv(address!=NULL, NULL);
wwarning("NULL address in 'WMGetHostWithAddress()'");
return NULL;
}
if (hostCacheEnabled) { if (hostCacheEnabled) {
if ((hPtr = getHostFromCache(address)) != NULL) { if ((hPtr = getHostFromCache(address)) != NULL) {