int STREAMAddDataProcessor(STREAM *S, TProcessingModule *Mod, const char *Args) { ListNode *Curr; char *Tempstr=NULL; int len; STREAMFlush(S); if (! S->ProcessingModules) S->ProcessingModules=ListCreate(); Tempstr=MCopyStr(Tempstr,Mod->Name,NULL); ListAddNamedItem(S->ProcessingModules,Tempstr,Mod); len=S->InEnd - S->InStart; Tempstr=SetStrLen(Tempstr,len); memcpy(Tempstr,S->InputBuff + S->InStart,len); STREAMResetInputBuffers(S); Curr=ListGetNext(Mod->Values); while (Curr) { STREAMSetValue(S,Curr->Tag,(char *) Curr->Item); Curr=ListGetNext(Curr); } STREAMReadThroughProcessors(S, Tempstr, len); DestroyString(Tempstr); return(TRUE); }
STREAM *STREAMClose(STREAM *S) { ListNode *Curr; int len; if (! S) return(NULL); len=S->OutEnd; STREAMReadThroughProcessors(S, NULL, 0); STREAMFlush(S); if ( (StrLen(S->Path)==0) || (strcmp(S->Path,"-") !=0) ) { if ((S->out_fd != -1) && (S->out_fd != S->in_fd)) close(S->out_fd); if (S->in_fd != -1) close(S->in_fd); } Curr=ListGetNext(S->Values); while (Curr) { if (strncmp(Curr->Tag,"HelperPID",9)==0) kill(atoi(Curr->Item),SIGKILL); Curr=ListGetNext(Curr); } ListDestroy(S->Values,(LIST_ITEM_DESTROY_FUNC)DestroyString); ListDestroy(S->ProcessingModules,DataProcessorDestroy); DestroyString(S->InputBuff); DestroyString(S->OutputBuff); DestroyString(S->Path); free(S); return(NULL); }
int STREAMReadCharsToBuffer(STREAM *S) { fd_set selectset; int result=0, diff, read_result=0, WaitForBytes=TRUE; struct timeval tv; char *tmpBuff=NULL; int v1, v2,v3; void *SSL_CTX=NULL; if (! S) return(0); if (S->State & SS_EMBARGOED) return(0); SSL_CTX=STREAMGetItem(S,"LIBUSEFUL-SSL-CTX"); if (S->InStart >= S->InEnd) { S->InEnd=0; S->InStart=0; } diff=S->InEnd-S->InStart; if (S->InStart > (S->BuffSize / 2)) { memmove(S->InputBuff,S->InputBuff + S->InStart,diff); S->InStart=0; S->InEnd=diff; } v1=S->InStart; v2=S->InEnd; v3=S->BuffSize; //if no room in buffer, we can't read in more bytes if (S->InEnd >= S->BuffSize) return(1); //if there are bytes available in the internal OpenSSL buffers, when we don't have to //wait on a select, we can just go straight through to SSL_read #ifdef HAVE_LIBSSL if (S->Flags & SF_SSL) { if (SSL_pending((SSL *) SSL_CTX) > 0) WaitForBytes=FALSE; } //else #endif //if ((S->Timeout > 0) && (! (S->Flags & SF_NONBLOCK)) && WaitForBytes) if ((S->Timeout > 0) && WaitForBytes) { FD_ZERO(&selectset); FD_SET(S->in_fd, &selectset); tv.tv_sec=S->Timeout; tv.tv_usec=0; result=select(S->in_fd+1,&selectset,NULL,NULL,&tv); switch (result) { //we are only checking one FD, so should be 1 case 1: read_result=0; break; case 0: errno=ETIMEDOUT; read_result=STREAM_TIMEOUT; break; default: if (errno==EINTR) read_result=STREAM_TIMEOUT; else read_result=STREAM_CLOSED; break; } } //must do this, as we need it to be 0 if we don't do the reads result=0; if (read_result==0) { tmpBuff=SetStrLen(tmpBuff,S->BuffSize-S->InEnd); #ifdef HAVE_LIBSSL if (S->Flags & SF_SSL) { read_result=SSL_read((SSL *) SSL_CTX, tmpBuff, S->BuffSize-S->InEnd); } else #endif { if (S->Flags & SF_RDLOCK) flock(S->in_fd,LOCK_SH); read_result=read(S->in_fd, tmpBuff, S->BuffSize-S->InEnd); if (S->Flags & SF_RDLOCK) flock(S->in_fd,LOCK_UN); } if (read_result > 0) { result=read_result; S->BytesRead+=read_result; } else { if ((read_result == -1) && (errno==EAGAIN)) read_result=STREAM_NODATA; else read_result=STREAM_CLOSED; result=0; } } if (result !=0) read_result=result; if (result < 0) result=0; read_result=STREAMReadThroughProcessors(S, tmpBuff, result); //if (result==STREAM_DATA_ERROR) read_result=STREAM_DATA_ERROR; //We are not returning number of bytes read. We only return something if //there is a condition (like socket close) where the thing we are waiting for //may not appear DestroyString(tmpBuff); return(read_result); }