initiate disconnect on timeout in NETWrite
This commit is contained in:
114
network.c
114
network.c
@ -39,6 +39,7 @@
|
|||||||
----------------------------------------------------------------------------*/
|
----------------------------------------------------------------------------*/
|
||||||
#include "fortify.h"
|
#include "fortify.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -65,9 +66,14 @@ struct timeval lastclose={-1,0};
|
|||||||
#include "Scommon.h"
|
#include "Scommon.h"
|
||||||
extern void SICSLogWrite(char *pText, OutCode eCode); /* servlog.c */
|
extern void SICSLogWrite(char *pText, OutCode eCode); /* servlog.c */
|
||||||
|
|
||||||
|
void WriteToCommandLog(char *p, char *t);
|
||||||
|
|
||||||
static void NetError(char *pText)
|
static void NetError(char *pText)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
SICSLogWrite(pText,eError);
|
SICSLogWrite(pText,eError);
|
||||||
|
*/
|
||||||
|
WriteToCommandLog("NET>", pText);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------- Local ------------------------------------
|
/* ---------------------------- Local ------------------------------------
|
||||||
@ -404,6 +410,7 @@ CreateSocketAdress(
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
strncpy(pCompost,host->h_name,iBufLen);
|
strncpy(pCompost,host->h_name,iBufLen);
|
||||||
|
pCompost[iBufLen-1]='\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -411,10 +418,14 @@ CreateSocketAdress(
|
|||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
int NETWrite(mkChannel *self, char *buffer, long lLen)
|
int NETWrite(mkChannel *self, char *buffer, long lLen)
|
||||||
{
|
{
|
||||||
int iRet;
|
long iRet;
|
||||||
fd_set lMask;
|
fd_set lMask;
|
||||||
struct timeval tmo ={0,1};
|
struct timeval tmo;
|
||||||
|
char buf[256];
|
||||||
|
time_t expire, delta;
|
||||||
|
char *pos;
|
||||||
|
long l;
|
||||||
|
int disconnected;
|
||||||
|
|
||||||
if(!VerifyChannel(self))
|
if(!VerifyChannel(self))
|
||||||
{
|
{
|
||||||
@ -431,46 +442,81 @@ CreateSocketAdress(
|
|||||||
* there is a problem with Java clients not reliably receiving data when
|
* there is a problem with Java clients not reliably receiving data when
|
||||||
* this is active.
|
* this is active.
|
||||||
*/
|
*/
|
||||||
#ifndef CYGNUS
|
|
||||||
tmo.tv_usec = 10;
|
|
||||||
FD_ZERO(&lMask);
|
|
||||||
FD_SET(self->sockid,&lMask);
|
|
||||||
if((self->sockid >= FD_SETSIZE) || (self->sockid < 0) )
|
if((self->sockid >= FD_SETSIZE) || (self->sockid < 0) )
|
||||||
/* invalid descriptor */
|
|
||||||
{
|
{
|
||||||
return -1; /* eof */
|
/* invalid descriptor */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
iRet = select( (self->sockid + 1),NULL, &lMask, NULL,&tmo);
|
#ifdef DO_NOT_SELECT_BEFORE_SEND
|
||||||
if( iRet <= 0)
|
iRet = send(self->sockid,buffer,lLen,0);
|
||||||
{
|
if (iRet != lLen) {
|
||||||
/* failure, or no data */
|
self->iType = 0;
|
||||||
return -1;
|
if (iRet < 0) {
|
||||||
}
|
if (errno == EPIPE) { /* do not write "broken pipe" error */
|
||||||
/* blocking on write */
|
return 0;
|
||||||
if(!FD_ISSET(self->sockid,&lMask))
|
}
|
||||||
{
|
snprintf(buf, sizeof buf, "NETWrite: send system error: %s (socket %d)",
|
||||||
return -2;
|
strerror(errno), self->sockid);
|
||||||
|
} else {
|
||||||
|
snprintf(buf, sizeof buf, "NETWrite: only %ld of %ld bytes sent (socket %d)",
|
||||||
|
iRet, lLen, self->sockid);
|
||||||
|
}
|
||||||
|
NetError(buf);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
pos = buffer;
|
||||||
|
l = lLen;
|
||||||
|
FD_ZERO(&lMask);
|
||||||
|
disconnected = 0;
|
||||||
|
|
||||||
iRet = send(self->sockid,buffer,lLen,0);
|
#define TIMEOUT 10
|
||||||
if(iRet != lLen)
|
expire = time(NULL) + TIMEOUT;
|
||||||
{
|
while (l > 0) {
|
||||||
if(iRet < 0)
|
delta = expire - time(NULL);
|
||||||
{
|
if (delta <= 0) break;
|
||||||
if (errno != EPIPE) { /* do not write "broken pipe" error */
|
FD_SET(self->sockid,&lMask);
|
||||||
printf("System error: %s\n",strerror(errno));
|
tmo.tv_usec = 0;
|
||||||
}
|
tmo.tv_sec = delta;
|
||||||
} else {
|
iRet = select( (self->sockid + 1),NULL, &lMask, NULL,&tmo);
|
||||||
printf("Incomplete send: %d to %ld\n",iRet,lLen);
|
if (iRet < 0) {
|
||||||
}
|
/* failure, or no data */
|
||||||
|
self->iType = 0;
|
||||||
|
snprintf(buf, sizeof buf,
|
||||||
|
"NETWrite: failure on select before send: %s (socket %d)",
|
||||||
|
strerror(errno), self->sockid);
|
||||||
|
NetError(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!FD_ISSET(self->sockid,&lMask)) break;
|
||||||
|
|
||||||
|
iRet = send(self->sockid,pos,l,0);
|
||||||
|
disconnected = (iRet == 0);
|
||||||
|
if (iRet < 0) {
|
||||||
|
self->iType = 0;
|
||||||
|
if (errno == EPIPE || errno == ECONNRESET) { /* do not write these errors */
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
snprintf(buf, sizeof buf, "NETWrite: send system error: %s (socket %d)",
|
||||||
|
strerror(errno), self->sockid);
|
||||||
|
NetError(buf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
l -= iRet;
|
||||||
|
pos += iRet;
|
||||||
}
|
}
|
||||||
else
|
if (l > 0) {
|
||||||
{
|
self->iType = 0;
|
||||||
return 1;
|
if (!disconnected) { /* do not write an error message on disconnect */
|
||||||
|
snprintf(buf, sizeof buf, "NETWrite: timeout, only %ld of %ld bytes sent (socket %d)",
|
||||||
|
lLen - l, lLen, self->sockid);
|
||||||
|
NetError(buf);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
/* -------------------------------------------------------------------------*/
|
/* -------------------------------------------------------------------------*/
|
||||||
long NETRead(mkChannel *self, char *buffer, long lLen, long timeout)
|
long NETRead(mkChannel *self, char *buffer, long lLen, long timeout)
|
||||||
|
Reference in New Issue
Block a user