/* * FTP/HTTP daemon * log.cc Copyright (C) 1998 Alex Belits * * This source/code is public free; you can distribute it and/or modify * it under terms of the GNU General Public License (published by the * Free Software Foundation) either version two of this License, or any * later version. * */ #include "fhttpd.h" #include "process.h" #include "log.h" #ifdef IRIX #include "irix-fix.h" #endif void ProcessLog(void){ int l,totalsize; Application *currapplication; ApplicationRequest *currequest,*nextrequest; ApplicationInstance *currinstance; if(glob_wheel->current_process) return; currapplication=(Application*)logapplications.start; while(currapplication){ totalsize=0; currinstance=(ApplicationInstance*)currapplication->instances.start; while(currinstance){ l=0; currequest=(ApplicationRequest*)currinstance->requests.start; while(currequest){ totalsize+=currequest->totalsize; if(totalsize>max_totallogmessagessize){ currequest=NULL; }else{ nextrequest=(ApplicationRequest*)currequest->next; l=currequest->Send(); if(l<0){ currinstance->queuesize--; delete currequest; currequest=NULL; //request deleted because of some error }else{ if(!l){ currinstance->queuesize--; delete currequest; currequest=NULL; }else nextrequest=NULL; } currequest=nextrequest; } } if(currinstance) currinstance=(ApplicationInstance*)currinstance->next; } if(totalsize>max_totallogmessagessize){ LogMessage(NULL,NULL,NULL,NULL,0,"KILLING_PROCESSES", currapplication->appname); while((currinstance=(ApplicationInstance*) currapplication->instances.start)){ delete currinstance; } } currapplication=(Application*)currapplication->next; } } void LogMessage(ControlFTPServerApp *clientapp,ApplicationRequest *request, ApplicationInstance *ainst,struct timeval *tv, int level,char *msgtype,char *message,__s32 xclientid,__s32 xreqid){ int l; Application *currapplication; ApplicationRequest *currequest,*nextrequest; ApplicationInstance *currinstance; struct timeval tv1; if(!tv){ gettimeofday(&tv1,NULL); tv=&tv1; } //if(glob_wheel->current_process) return; currapplication=(Application*)logapplications.start; while(currapplication){ currinstance=(ApplicationInstance*)currapplication->instances.start; while(currinstance){ if(glob_wheel->current_process){ currinstance->SendLogMessage(clientapp,request,ainst,tv,level,msgtype,message,xclientid,xreqid); return; } l=0; currequest=(ApplicationRequest*)currinstance->requests.start; while(currequest){ nextrequest=(ApplicationRequest*)currequest->next; l=currequest->Send(); if(l<0){ currinstance->queuesize--; delete currequest; currequest=NULL; //request deleted because of some error }else{ if(!l){ currinstance->queuesize--; delete currequest; currequest=NULL; }else nextrequest=NULL; } currequest=nextrequest; } currinstance->SendLogMessage(clientapp,request,ainst,tv,level,msgtype,message,xclientid,xreqid); currinstance=(ApplicationInstance*)currinstance->next; } currapplication=(Application*)currapplication->next; } } void LogMessage(ControlFTPServerApp *clientapp,ApplicationRequest *request, ApplicationInstance *ainst,struct timeval *tv, int level,List *messages,__s32 xclientid,__s32 xreqid){ int l; Application *currapplication; ApplicationRequest *currequest,*nextrequest; ApplicationInstance *currinstance; struct timeval tv1; if(!tv){ gettimeofday(&tv1,NULL); tv=&tv1; } if(glob_wheel->current_process) return; currapplication=(Application*)logapplications.start; while(currapplication){ currinstance=(ApplicationInstance*)currapplication->instances.start; while(currinstance){ if(glob_wheel->current_process){ currinstance->SendLogMessage(clientapp,request,ainst,tv,level,messages,xclientid,xreqid); return; } l=0; currequest=(ApplicationRequest*)currinstance->requests.start; while(currequest){ nextrequest=(ApplicationRequest*)currequest->next; l=currequest->Send(); if(l<0){ currinstance->queuesize--; delete currequest; currequest=NULL; //request deleted because of some error }else{ if(!l){ currinstance->queuesize--; delete currequest; currequest=NULL; }else nextrequest=NULL; } currequest=nextrequest; } currinstance->SendLogMessage(clientapp,request,ainst,tv,level,messages,xclientid,xreqid); currinstance=(ApplicationInstance*)currinstance->next; } currapplication=(Application*)currapplication->next; } } void RelayLogMessage(char *relaybuffer,int len){ int l; __s32 *lp; Application *currapplication; ApplicationRequest *currequest,*nextrequest; ApplicationInstance *currinstance; if(glob_wheel->current_process) return; currapplication=(Application*)logapplications.start; while(currapplication){ currinstance=(ApplicationInstance*)currapplication->instances.start; while(currinstance){ l=0; currequest=(ApplicationRequest*)currinstance->requests.start; while(currequest){ nextrequest=(ApplicationRequest*)currequest->next; l=currequest->Send(); if(l<0){ currinstance->queuesize--; delete currequest; currequest=NULL; //request deleted because of some error }else{ if(!l){ currinstance->queuesize--; delete currequest; currequest=NULL; }else nextrequest=NULL; } currequest=nextrequest; } ApplicationRequest *logreq=new ApplicationRequest(currapplication,NULL,NULL,0,0,-1); if(logreq){ logreq->buffsize=len; logreq->buffer=(char*)malloc(logreq->buffsize); if(logreq->buffer){ memcpy(logreq->buffer,relaybuffer,logreq->buffsize); sequencenumber++; if(!sequencenumber) sequencenumber++; logreq->id=sequencenumber; lp=((__s32*)logreq->buffer)+1; *lp=htonl(sequencenumber); currinstance->requests.Add(logreq); currinstance->queuesize++; currinstance->lastrequesttime=global_time; requestcounter++; currinstance->lastrequestcounter=requestcounter; logreq->specialmessage=1; logreq->instance=currinstance; logreq->GetSize(); }else{ logreq->buffsize=0; delete logreq; } } currinstance=(ApplicationInstance*)currinstance->next; } currapplication=(Application*)currapplication->next; } } /* ------------ A buffsize | sequencenumber | FHTTPD_LOG_REQUEST buffsize | rl requestline[rl] | nlines (paramc (rln line[rln])) | databufsize=sizeof(_s32)*6 | client | request | instance | tv_sec | tv_usec V level ------------ */ int ApplicationInstance::SendLogMessage(ControlFTPServerApp *clientapp,ApplicationRequest *request,ApplicationInstance *ainst,struct timeval *tv,int level,char *msgtype,char *message,__s32 xclientid,__s32 xreqid){ ApplicationRequest *logreq=new ApplicationRequest(application,NULL,NULL,0,0,-1); __s32 rln0,rln1,paramc,databuffsize,*lp; char *cp; struct timeval tv1; #ifdef MUST_ALIGN char *cp0; __s32 q; #else __s32 *lp0; #endif if(!tv){ gettimeofday(&tv1,NULL); tv=&tv1; } //if(glob_wheel->current_process) return -1; if(logreq){ logreq->buffsize=sizeof(__s32)*12+1; databuffsize=sizeof(__s32)*6; if(msgtype||message){ logreq->nlines=1; paramc=0; if(msgtype){ paramc++; rln0=strlen(msgtype)+1; logreq->buffsize+=rln0; } if(message){ paramc++; rln1=strlen(message)+1; logreq->buffsize+=rln1; } logreq->buffsize+=(paramc+1)*sizeof(__s32); }else{ logreq->nlines=0; } if(glob_wheel->current_process){ logreq->buffer=(char*)malloc(logreq->buffsize+4); }else{ logreq->buffer=(char*)malloc(logreq->buffsize); } if(logreq->buffer){ if(glob_wheel->current_process){ memcpy(logreq->buffer,"LOG ",4); lp=(__s32*)(logreq->buffer+4); }else{ lp=(__s32*)logreq->buffer; } *lp=htonl(logreq->buffsize); lp++; sequencenumber++; if(!sequencenumber) sequencenumber++; *lp=htonl(sequencenumber); logreq->id=sequencenumber; lp++; *lp=htonl(FHTTPD_LOG_REQUEST); lp++; *lp=htonl(1); lp++; cp=(char*)lp; *cp=0; cp++; #ifndef MUST_ALIGN lp=(__s32*)cp; #endif #ifdef MUST_ALIGN q=htonl(logreq->nlines); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(logreq->nlines); lp++; cp=(char*)lp; #endif if(logreq->nlines){ #ifdef MUST_ALIGN q=htonl(paramc); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(paramc); lp++; #endif if(msgtype){ #ifdef MUST_ALIGN q=htonl(rln0); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(rln0); lp++; cp=(char*)lp; #endif memcpy(cp,msgtype,rln0); cp+=rln0; #ifndef MUST_ALIGN lp=(__s32*)cp; #endif } if(message){ #ifdef MUST_ALIGN q=htonl(rln1); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(rln1); lp++; cp=(char*)lp; #endif memcpy(cp,message,rln1); cp+=rln1; #ifndef MUST_ALIGN lp=(__s32*)cp; #endif } } #ifdef MUST_ALIGN q=htonl(databuffsize); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(databuffsize); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(clientapp?clientapp->preassigned_id:xclientid); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(clientapp?clientapp->preassigned_id:xclientid); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(request?request->preassigned_id:xreqid); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(request?request->preassigned_id:xreqid); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(ainst?ainst->preassigned_id:0); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(ainst?ainst->preassigned_id:0); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(tv->tv_sec); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(tv->tv_sec); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(tv->tv_usec); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(tv->tv_usec); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(level); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(level); lp++; cp=(char*)lp; #endif if(glob_wheel->current_process){ write(glob_wheel->current_process->writehandle, logreq->buffer,logreq->buffsize+4); read(glob_wheel->current_process->readhandle,logreq->buffer,1); delete logreq; }else{ requests.Add(logreq); queuesize++; lastrequesttime=global_time; requestcounter++; lastrequestcounter=requestcounter; logreq->specialmessage=1; logreq->instance=this; } logreq->GetSize(); return 0; }else{ logreq->buffsize=0; delete logreq; return -1; } }else return -1; } int ApplicationInstance::SendLogMessage(ControlFTPServerApp *clientapp,ApplicationRequest *request,ApplicationInstance *ainst,struct timeval *tv,int level,List *messages,__s32 xclientid,__s32 xreqid){ ApplicationRequest *logreq=new ApplicationRequest(application,NULL,NULL,0,0,-1); __s32 databuffsize,*lp,rl,ll; char *cp; #ifdef MUST_ALIGN char *cp0; __s32 q; #else __s32 *lp0; #endif int i; ConfigArgs *currline; struct timeval tv1; if(!tv){ gettimeofday(&tv1,NULL); tv=&tv1; } //if(glob_wheel->current_process) return -1; if(logreq){ logreq->buffsize=sizeof(__s32)*12+1; databuffsize=sizeof(__s32)*6; logreq->nlines=0; if(messages){ currline=(ConfigArgs*)messages->start; while(currline){ logreq->buffsize+=(currline->paramc+1)*(sizeof(__s32)); for(i=0;iparamc;i++) logreq->buffsize+=strlen(currline->params[i])+1; logreq->nlines++; currline=(ConfigArgs*)currline->next; } } if(glob_wheel->current_process){ logreq->buffer=(char*)malloc(logreq->buffsize+4); }else{ logreq->buffer=(char*)malloc(logreq->buffsize); } if(logreq->buffer){ if(glob_wheel->current_process){ memcpy(logreq->buffer,"LOG ",4); lp=(__s32*)(logreq->buffer+4); }else{ lp=(__s32*)logreq->buffer; } *lp=htonl(logreq->buffsize); lp++; sequencenumber++; if(!sequencenumber) sequencenumber++; *lp=htonl(sequencenumber); logreq->id=sequencenumber; lp++; *lp=htonl(FHTTPD_LOG_REQUEST); lp++; *lp=htonl(1); lp++; cp=(char*)lp; *cp=0; cp++; #ifdef MUST_ALIGN cp0=cp; cp+=sizeof(__s32); #else lp=(__s32*)cp; lp0=lp; lp++; #endif ll=0; currline=(ConfigArgs*)messages->start; while(currline){ ll++; #ifdef MUST_ALIGN q=htonl(currline->paramc); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(currline->paramc); lp++; #endif for(i=0;iparamc;i++){ rl=strlen(currline->params[i])+1; #ifdef MUST_ALIGN q=htonl(rl); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(rl); lp++; cp=(char*)lp; #endif memcpy(cp,currline->params[i],rl); cp+=rl; #ifndef MUST_ALIGN lp=(__s32*)cp; #endif } currline=(ConfigArgs*)currline->next; } #ifdef MUST_ALIGN q=htonl(ll); memcpy(cp0,&q,sizeof(__s32)); q=htonl(databuffsize); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp0=htonl(ll); *lp=htonl(databuffsize); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(clientapp?clientapp->preassigned_id:xclientid); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(clientapp?clientapp->preassigned_id:xclientid); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(request?request->preassigned_id:xreqid); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(request?request->preassigned_id:xreqid); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(ainst?ainst->preassigned_id:0); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(ainst?ainst->preassigned_id:0); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(tv->tv_sec); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(tv->tv_sec); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(tv->tv_usec); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(tv->tv_usec); lp++; cp=(char*)lp; #endif #ifdef MUST_ALIGN q=htonl(level); memcpy(cp,&q,sizeof(__s32)); cp+=sizeof(__s32); #else *lp=htonl(level); lp++; cp=(char*)lp; #endif if(glob_wheel->current_process){ write(glob_wheel->current_process->writehandle, logreq->buffer,logreq->buffsize+4); read(glob_wheel->current_process->readhandle,logreq->buffer,1); delete logreq; }else{ requests.Add(logreq); queuesize++; lastrequesttime=global_time; requestcounter++; lastrequestcounter=requestcounter; logreq->specialmessage=1; logreq->instance=this; } logreq->GetSize(); return 0; }else{ logreq->buffsize=0; delete logreq; return -1; } }else return -1; } /* EXISTS_CLIENT client EXISTS_REQUEST request EXISTS_INSTANCE instance */ void ApplicationInstance::LogReportStatus(void){ Application *currapp; ApplicationInstance *currappinstance; ApplicationRequest *currequest; int i; char tmpstr[11]; tmpstr[0]=0; struct sockaddr_in currsock; sockaddr_size_type currsockaddrlen=sizeof(currsock); ServerConnection *currentconnection; ControlFTPServerApp *client; if(glob_wheel->current_process) return; SendLogMessage(NULL,NULL,NULL,NULL,0,"STATUS_REPORT_BEGIN",NULL); if(glob_wheel){ for(i=0;ifdarraysize;i++){ currentconnection=glob_wheel->getclientbynumber(i); if(currentconnection&¤tconnection->socket){ if(currentconnection->socket->port==http_server_port ||currentconnection->socket->port==ftp_server_port){ client=(ControlFTPServerApp*) currentconnection->app; if(client){ sprintf(tmpstr,"%d",currentconnection->socket->port); SendLogMessage(client,NULL,NULL,NULL,0,"CLIENT_EXISTS_LOCAL_PORT",tmpstr); if(!getsockname(currentconnection->gethandle(),(sockaddr*)&currsock,(socklen_t*)&currsockaddrlen)){ SendLogMessage(client,NULL,NULL,NULL,0,"CLIENT_EXISTS_LOCAL_IP",inet_ntoa(currsock.sin_addr)); } SendLogMessage(client,NULL,NULL,NULL,0,"CLIENT_EXISTS_IP",inet_ntoa(((sockaddr_in*)currentconnection->getaddress())->sin_addr)); if(client->resolvedname[0]) SendLogMessage(client,NULL,NULL,NULL,0,"HOSTNAME",client->resolvedname); } } } } } currapp=(Application*)applist.start; while(currapp){ currequest=(ApplicationRequest*)currapp->requests.start; while(currequest){ SendLogMessage(currequest->ctrlapp,currequest,NULL,NULL,0,"REQUEST_EXISTS",NULL); currequest=(ApplicationRequest*) currequest->next; } currappinstance=(ApplicationInstance *)currapp->instances.start; while(currappinstance){ SendLogMessage(NULL,NULL,currappinstance,NULL,0,"INSTANCE_EXISTS", currapp->appname); currequest=(ApplicationRequest*)currappinstance->requests.start; while(currequest){ SendLogMessage(currequest->ctrlapp,currequest,currappinstance,NULL,0,"REQUEST_EXISTS",NULL); currequest=(ApplicationRequest*) currequest->next; } currappinstance=(ApplicationInstance*)currappinstance->next; } currapp=(Application*)currapp->next; } currapp=(Application*)logapplications.start; while(currapp){ currappinstance=(ApplicationInstance*)currapp->instances.start; while(currappinstance){ SendLogMessage(NULL,NULL,currappinstance,NULL,0,"INSTANCE_EXISTS", currapp->appname); currappinstance=(ApplicationInstance*)currappinstance->next; } currapp=(Application*)currapp->next; } SendLogMessage(NULL,NULL,NULL,NULL,0,"STATUS_REPORT_END",NULL); }