houxiao
2016-12-08 ba44a2cd99d617986a8bbb0d4ab96bc0a27e6322
ProxyServer/proxyserver.c
File was renamed from ProxyServer/proxyserver18.c
@@ -5,8 +5,8 @@
 *      Author: JKM
 */
#include<dirent.h>
#include"proxyserver.h"
#include <dirent.h>
#include "proxyserver.h"
int RequestChannel;
char* gAnalyzerIP;
char* gUser;
@@ -28,10 +28,9 @@
int ZipSerFileNum=0;
int ZipCountidx;
char NetInfo[128];
char EventString[5000 * 1024];
EVENT_LOG myEventLogs[1000 * 1024];
char EventString[100000];
EVENT_LOG myEventLogs[200];
int EventCount=0;
int EventCountInSearchRange=0;
int FindIdx(char* IP);
int FindEmpty();
@@ -44,15 +43,12 @@
#define fileread_buf 128
AlarmInfo RealAlarmEvent[16][1000];
//#define DBG_LOG
#define ZIP_HOME "/tmp"
/////////////////////////////////////////////////
char* strnncat(char*s1,char*s2,int n)
{
   int len = strlen(s1);
int i;
   for(i=0;i<n;i++)
   for(int i=0;i<n;i++)
   {
      s1[len+i]=s2[i];
   }
@@ -124,53 +120,8 @@
   }
   return EventString;
}
int InsertAnalyser(char *AnalyserName,char* AnalyserIP,char* AnalyserPort,char* ServerIP,char* ServerPort,char* user,char* pass,char* resol,char* commType)
{
   int ret=0;
   ret=Check_MysqlData(AnalyserName,AnalyserIP,AnalyserPort,ServerIP,ServerPort,user, pass, resol, commType);
   if(ret !=9)
      return InsertMysqlData(AnalyserName,AnalyserIP,AnalyserPort,ServerIP,ServerPort,user, pass, resol, commType);
   else
     return 0;
}
int DeleteAnalyser(char* AnalyserIP)                                                                           //8.删除设备   参数:设备ip   返回:成功或失败的提示。
{
   int ret=0;
   MYSQL *conn;
   char *server="localhost";
   char *user="root";
   char *password="123";
   char *database="opentutorials";
   char query[255];
   if(!(conn=mysql_init((MYSQL*)NULL))){
      printf("init fail\n");
      return -1;
   }
   if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
      printf("connect error.\n");
      return -1;
   }
   if(mysql_select_db(conn,database)!=0){
      mysql_close(conn);
      printf("select_db_fail.\n");
      return -1;
   }
   sprintf(query, "delete from tbl_deviceinfo where equipmentip='%s';",AnalyserIP);
   if(mysql_query(conn,query)){
      printf("query fail\n");
      return -1;
   }
   mysql_close(conn);
   return ret;
}
char* GetAnalyserAll()                                                                                    //9.查询设备   参数:无      返回:分析器的名称和ip
{
   memset(EventString,0,100000);
   int ret = Read_MysqlData();
   if(ret==0)   return    EventString;
   else  return "fail";
}
char* GetAnalyserNet(void)                                                                                    //5.获取当前转发器的ip      参数:无。返回:当前的ip地址子网掩码和网关
{
   memset(NetInfo,0,128);
@@ -418,8 +369,7 @@
   int Idx=FindIdx(AnalyserIP);
   memset(EventString,0,100000);
   if(Idx>=0 && Idx<16 && RealAlarmEventNum[Idx]>0){      
      int i;
      for ( i=0;i<RealAlarmEventNum[Idx];i++)
      for (int i=0;i<RealAlarmEventNum[Idx];i++)
      {
         strcat(EventString,RealAlarmEvent[Idx][i].channelId);
         strcat(EventString,"\n");
@@ -477,6 +427,141 @@
   return result;
   
}
char* GetMp4Info(char* AnalyserIP,char* user,char* pass,int Channel,char* path)
{
   char Pathbuffer[100] = {0};
   RequestChannel = Channel;
   gAnalyzerIP = AnalyserIP;
   gUser = user;
   gPass = pass;
   bMetadataSaving = 0;
   if(AnalyserIP==NULL || AnalyserIP[0]==0) return "-1";
   int length;
   int bMp4FileSaving=0;
   int Mp4FileAppendingSiz=0;
   int Mp4FileSiz;
   int file;
   char* result;
   char* httpUri = path;
   int iidx =0,llen=0;
   llen = sprintf(byte_array,"GET %s %s",httpUri,"HTTP/1.1\r\n"); iidx+=llen;
   llen = sprintf(&byte_array[iidx],"Pragma: no-cache\r\n"); iidx+=llen;
   //   llen = sprintf(&byte_array[iidx],"Host: %s\r\n",Equipments[EquipIdx].ip); iidx+=llen;
   llen = sprintf(&byte_array[iidx],"Host: %s:80\r\n",gAnalyzerIP); iidx+=llen;
   llen = sprintf(&byte_array[iidx],"%s","Connection: Keep_Alive\r\n\r\n"); iidx+=llen;
   struct sockaddr_in servaddr;
   int strlen1=sizeof(servaddr);
   if((GSocket=socket(PF_INET,SOCK_STREAM,0))<0){
      perror("socket fail2");
      return "-3";
   }
   memset(&servaddr,0,strlen1);
   servaddr.sin_family=AF_INET;
   inet_pton(AF_INET,gAnalyzerIP,&servaddr.sin_addr);
   servaddr.sin_port=htons(80);
   if(connect(GSocket,(struct sockaddr*)&servaddr,strlen1)<0){
      printf("Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
      close(GSocket);
      return "-4";
   }
   //printf("byte_array==%s",byte_array);
   write(GSocket,byte_array,iidx);
   while(1){
      memset(buff,0,sizeof(buff));
      length=read(GSocket,&buff,sizeof(buff));
      //printf("buff==%s",buff);
      if(length<=0)
      {
         close(GSocket);
         break;
      }
      if(memorycmp((unsigned char*)buff,length, "WWW-Authenticate:",17)>0)
      {
         // analyse reception
         //printf("===================================");
         MakeAuthorizedPacketSimp(buff,length,"GET",httpUri);
         write(GSocket,byte_array,strlen(byte_array));
         continue;
      }
      if(httpUri[0]!=0){
         printf("**************************************flag======%d\n",bMp4FileSaving);
         if(bMp4FileSaving==1){
            if((file = open(Pathbuffer, O_RDWR, S_IREAD|S_IWRITE)) <0){
               printf("open file fail===2!\n");
               result="File open error(2)!";
               close(GSocket);
               return result;
            }
            lseek(file,0,SEEK_END);
            write(file,&buff[0], length);//CString m_data
            Mp4FileAppendingSiz += length;
            if (Mp4FileAppendingSiz>=Mp4FileSiz+8)
            {
               int sidx = memorycmp((unsigned char*)buff,length, "moov",4);
               Mp4FileSiz+=buff[sidx-4]*256*256*256+buff[sidx-3]*256*256+buff[sidx-2]*256+buff[sidx-1]; //movie box size
               printf("**************************************Mp4FileSiz======%d\n",Mp4FileSiz);
            }
            if (Mp4FileAppendingSiz>=Mp4FileSiz)
            {
               close(file);
               bMp4FileSaving = false;
               break;
            }
         }
         else
         {
            int sidx = memorycmp((unsigned char*)buff,length, "ftypmp42",8);
            if(sidx>=0){
               bMp4FileSaving= 1;
               Mp4FileAppendingSiz = length-sidx+4;
         printf("**************************************Mp4FileAppendingSiz======%d\n",Mp4FileAppendingSiz);
               if(length>36){
                  //printf("****************************************");
                  Mp4FileSiz = 24 + buff[sidx+20]*256*256*256+buff[sidx+21]*256*256+buff[sidx+22]*256+buff[sidx+23];// file_type box size + Media databox size
         printf("**************************************Mp4FileSiz======%d\n",Mp4FileSiz);
                  int count=0,i;
                  for(i=0;i<strlen(httpUri);i++){
                     if(httpUri[i]=='/') {
                        count++;
                        if(count>=9) break;
                     }
                  }
                  sprintf(Pathbuffer,"/tmp/%s",/*RequestChannel*/&httpUri[i+1]); iidx+=llen;
                  if((file = open(Pathbuffer,O_CREAT|O_RDWR,S_IREAD|S_IWRITE)) <0){
                     printf("open file fail===1!\n");
                     result="File open error(1)!";
                     close(GSocket);
                     return result;
                  }
                  write(file,&buff[sidx-4], length-sidx+4);
               }
            }
         }
      }
   }
   close(GSocket);
   return "ok";
}
//// 查询报警记录
//// 输入:分析器IP,开始时间,结束时间,信道号,用户,密码
//// 输出:查询结果。(包括信道号,报警时间,播放地址,等。)
@@ -487,48 +572,40 @@
   gUser = user;
   gPass = pass;
   bMetadataSaving = 0;
   int i;
   if(AnalyserIP==NULL || AnalyserIP[0]==0) return "-1";
//   if(gGettingStep>0) return -2;
chmod(ZIP_HOME,0777);
//delete_file(ZIP_HOME "/tmp/tmp.zip");
delete_file(ZIP_HOME "/tmpZip/*");
    
   struct tm ttm;
   time_t StepTm;
   int length;
   char* httpUri;
   char sss[24],eee[24];
   chmod("/home",0777);
   const char* folderr;
   const char* filename1;
   folderr="/home/tmpZip";
   filename1="/home/tmp.zip";
   struct stat sb;
   if(stat(folderr,&sb)==0 && S_ISDIR(sb.st_mode))
      {
         delete_folder("/home/tmpZip");
      }
   if (doesFileExist(filename1))
      {
         delete_file("/home/tmp.zip");
      }
   if(RequestChannel==0) httpUri= "/PSIA/VIQ/Event/metadata/0/files";
   if(RequestChannel==1) httpUri= "/PSIA/VIQ/Event/metadata/1/files";
   if(RequestChannel==2) httpUri= "/PSIA/VIQ/Event/metadata/2/files";
   if(RequestChannel==3) httpUri= "/PSIA/VIQ/Event/metadata/3/files";
   ttm.tm_year=StartTm->Year - 1900;
   ttm.tm_mon=StartTm->Month - 1;
   ttm.tm_mday=StartTm->Day;
   ttm.tm_hour=StartTm->Hour;
   ttm.tm_min=StartTm->Min;
   ttm.tm_sec=StartTm->Sec;
   ttm.tm_year=StartTm->Year;ttm.tm_mon=StartTm->Month;ttm.tm_mday=StartTm->Day;ttm.tm_hour=StartTm->Hour;ttm.tm_min=StartTm->Min;ttm.tm_sec=StartTm->Sec;
   time_t SearchStartTm = mktime(&ttm);
   SearchStartTm -= 8 * 60 * 60;
   ttm.tm_year=EndTm->Year - 1900;
   ttm.tm_mon=EndTm->Month - 1;
   ttm.tm_mday=EndTm->Day;
   ttm.tm_hour=EndTm->Hour;
   ttm.tm_min=EndTm->Min;
   ttm.tm_sec=EndTm->Sec;
   ttm.tm_year=EndTm->Year;ttm.tm_mon=EndTm->Month;ttm.tm_mday=EndTm->Day;ttm.tm_hour=EndTm->Hour;ttm.tm_min=EndTm->Min;ttm.tm_sec=EndTm->Sec;
   time_t SearchEndTm = mktime(&ttm);
   SearchEndTm -= 8 * 60 * 60;
   
#ifdef DBG_LOG
   printf("stm=%d,etm=%d\n",(int)SearchStartTm,(int)SearchEndTm);
#endif
   //for (StepTm=SearchEndTm; StepTm>SearchStartTm; StepTm-=1*3600)
   for (StepTm=SearchEndTm;StepTm>SearchStartTm;StepTm-=2*3600)
   {
      zclock_sleep(200);
//      printf(" ==== Search loop function execution  =================%d,%d \n",(int)StepTm,(int)SearchStartTm);
@@ -548,21 +625,14 @@
         close(GSocket);
         return "-4";
      }
      time_t ttt=SearchStartTm;
      time_t ttt=StepTm-2*3600;
      struct tm *t;
      //ttt-=4*60*60;
      t = localtime(&ttt);
      sprintf(sss,"%d-%02d-%02dT%02d:%02d:%02d.000",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);// = "2016-08-26T07:31:00.000";
      sprintf(sss,"%d-%02d-%02dT%02d:%02d:%02d.000",t->tm_year+2000-100,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);// = "2016-08-26T07:31:00.000";
      sss[23]=0;
      ttt=SearchEndTm;
      //ttt+=4*60*60;
      t = localtime(&ttt);
      sprintf(eee,"%d-%02d-%02dT%02d:%02d:%02d.000",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);// = "2016-08-26T07:31:00.000";
      t = localtime(&StepTm);
      sprintf(eee,"%d-%02d-%02dT%02d:%02d:%02d.000",t->tm_year+2000-100,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);// = "2016-08-26T07:31:00.000";
      eee[23]=0;
      printf("sss=%s, eee=%s\n", sss, eee);
      RequestMetaFiles(gAnalyzerIP,httpUri,sss,eee);
      write(GSocket,byte_array,strlen(byte_array));
      
@@ -593,10 +663,10 @@
            if(eidx >=0){
               bMetadataSaving = 0;
               int SSize=0;
               for (i=0;i<bandCount;i++) SSize+=bandsize[i];
               for (int i=0;i<bandCount;i++) SSize+=bandsize[i];
               char *tmpp = (char*)malloc(SSize);
               int xx=0;
               for (i=0;i<bandCount;i++)
               for (int i=0;i<bandCount;i++)
               {
                  memcpy(&tmpp[xx],memoryband[i],bandsize[i]);
                  xx=bandsize[i];
@@ -664,11 +734,6 @@
         }
       }// while end
   }//for 
   //////// get
   for(ZipCountidx = 0; ZipCountidx<ZipSerFileNum; ZipCountidx++)
   {
@@ -697,7 +762,8 @@
      write(GSocket,byte_array,strlen(byte_array));
//      printf("==========================================================================================================================>>>>>>>>\n");
//      printf("%s",byte_array);
      chmod(ZIP_HOME,0777);
      while(1){
         length=read(GSocket,&buff,sizeof(buff)-1);      
         if(length<=0)
@@ -721,7 +787,7 @@
         if(bZipFileSaving==1){
            //下载Zip filesave
            int file;
            if((file = open(ZIP_HOME "/tmp.zip", O_RDWR, S_IREAD|S_IWRITE)) <0)
            if((file = open("/home/tmp.zip", O_RDWR, S_IREAD|S_IWRITE)) <0)
               printf("open file fail!\n");
            lseek(file,0,SEEK_END);
            write(file,&buff[0], length);//CString m_data
@@ -732,18 +798,17 @@
               bZipFileSaving = 0;
               close(GSocket);
               //Convert Zip file to Ser file
               system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
               system("unzip -o /home/tmp.zip -d /home/tmpZip");
            }
         }
         else{
            int sidx = stringcmp((char*)buff,"PK",2);
            if(sidx >=0){//下载Zip filesave
               delete_file(ZIP_HOME "/tmp.zip");
               //delete_file(ZIP_HOME "/tmpZip/*");
               delete_file("/home/tmp.zip");
               //下载Zip filesave
               bZipFileSaving = 1;
               int file;
               if((file = open(ZIP_HOME "/tmp.zip",O_CREAT|O_RDWR,S_IREAD|S_IWRITE)) <0)
               if((file = open("/home/tmp.zip",O_CREAT|O_RDWR,S_IREAD|S_IWRITE)) <0)
                  printf("open file fail!\n");
               write(file,&buff[sidx], length-sidx);//CString m_data
               close(file);
@@ -754,15 +819,15 @@
                  bZipFileSaving = 0;
                  close(GSocket);
                  //Convert Zip file to Ser file
                  system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
                  system("unzip -o /home/tmp.zip -d /home/tmpZip");
               }
            }
         }
      }//while
   }//for
   int res=0,num=0; //得长度
   int   i,res=0,num=0; //得长度
   filenames filename[100];
   num = GetFileNameInFolder(ZIP_HOME "/tmpZip",filename);
   num = GetFileNameInFolder("/home/tmpZip",filename);
   char stringpath[1024];
   unsigned char p_save_buf[6144000];
   
@@ -770,68 +835,37 @@
   {   
      if(filename[i].name[0]=='.') continue;
      memset(stringpath,0,1024);
      memcpy(stringpath, ZIP_HOME "/tmpZip/",13);
      memcpy(stringpath,"/home/tmpZip/",13);
      strcat(stringpath,filename[i].name);
      int handle;
      if((handle = open(stringpath, O_RDWR, S_IREAD|S_IWRITE)) <0)
         printf("open file fail!\n");
      res = read(handle,p_save_buf,6144000);//CString m_data
      close(handle);
      AnalySerFile((char*)p_save_buf,res,SearchStartTm,SearchEndTm);//#todo
      AnalySerFile((char*)p_save_buf,res);
   }   
   memset(EventString,0,sizeof(EventString));
   memset(EventString,0,100000);
   //for(i=0;i<1000;i++)
#ifdef DBG_LOG
   printf("EventCount===========================%d\n",EventCount);
   printf("EventCountInSearchRange==============%d\n",EventCountInSearchRange);
#endif
   strcat(EventString,"[");
   for (i=0;i<EventCount;i++)
   printf("EventCount===========================%d\n",EventCount);
   for (int i=0;i<EventCount;i++)
   {
#ifndef DBG_LOG
      if (! myEventLogs[i].InSearchRange)
         continue;
#endif
      strcat(EventString,"{");
      strcat(EventString,"\"datetime\":\"");
      strcat(EventString,myEventLogs[i].Date);
      strcat(EventString," ");
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].Time);
      strcat(EventString,"\",\"path\":\"");
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].Duration);
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].clipUrl);
      strcat(EventString,"\"}");
      if (i < EventCount - 1)
         strcat(EventString,",");
      //strcat(EventString,myEventLogs[i].Date);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].Time);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].Duration);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].clipUrl);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].Region);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].Rule);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].DebugMessageS);
      //strcat(EventString,"\n");
      //strcat(EventString,myEventLogs[i].DebugMessageE);
      //strcat(EventString,"\n");
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].Region);
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].Rule);
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].DebugMessageS);
      strcat(EventString,"\n");
      strcat(EventString,myEventLogs[i].DebugMessageE);
      strcat(EventString,"\n");
   }
   strcat(EventString,"]");
   //reset
   EventCount = 0;//#todo
   EventCountInSearchRange = 0;
   RequestChannel = -1;
   //memset(&myEventLogs, 0, sizeof(myEventLogs));
   return EventString;
   //return "333333333333333333333333333333333333333333333333333333333333333333789";
}
@@ -1235,8 +1269,7 @@
}
int FindIdx(char* IP)
{
   int i;
   for (i=0;i<16;i++)
   for (int i=0;i<16;i++)
   {
      if(strcmp(sessions[i].IP,IP)==0) return i;
   }
@@ -1244,8 +1277,7 @@
}
int FindEmpty()
{
int i;
   for (i=0;i<16;i++)
   for (int i=0;i<16;i++)
   {
      if(sessions[i].IP[0]==0) return i;
   }
@@ -1325,12 +1357,11 @@
   }
   return n;
}
void AnalySerFile(char* p_save_buf,int res, int SearchStartTm, int SearchEndTm)
void AnalySerFile(char* p_save_buf,int res)
{
   int ParagraphIdx;
   bool somewhat;
first:
   somewhat=false;
   memset(myEventLogs[EventCount].DebugMessageS,0,200);
   memset(myEventLogs[EventCount].DebugMessageE,0,200);
@@ -1370,37 +1401,6 @@
      if(sidx>=0 && p_save_buf[sidx+7]==0x08){
         int flen = (unsigned char)p_save_buf[sidx+7+1]*256+(unsigned char)p_save_buf[sidx+7+2];
         memcpy(myEventLogs[EventCount].clipUrl,&p_save_buf[sidx+7+3],flen);
         //#todo
         {
            const char* dtInUrlPos = strrchr(myEventLogs[EventCount].clipUrl, '/');
            //printf("%s\n", dtInUrlPos);
            struct tm dtInUrl;
            memset(&dtInUrl, 0, sizeof(dtInUrl));
            sscanf(dtInUrlPos, "/%d_%d_%d-%d_%d_%d.",
               &dtInUrl.tm_year, &dtInUrl.tm_mon, &dtInUrl.tm_mday, &dtInUrl.tm_hour, &dtInUrl.tm_min, &dtInUrl.tm_sec);
            dtInUrl.tm_year-=1900;
            dtInUrl.tm_mon--;
            time_t tt = mktime(&dtInUrl);
            if(tt<SearchStartTm||tt>SearchEndTm)
            {
               myEventLogs[EventCount].InSearchRange = 0;
#ifdef DBG_LOG
               sprintf(myEventLogs[EventCount].clipUrl,"%s ER %d",myEventLogs[EventCount].clipUrl, tt);
#endif
            }
            else
            {
               myEventLogs[EventCount].InSearchRange = 1;
#ifdef DBG_LOG
               sprintf(myEventLogs[EventCount].clipUrl,"%s OK %d",myEventLogs[EventCount].clipUrl, tt);
#endif
               EventCountInSearchRange++;
            }
         }
      }
      sidx = memorycmp((unsigned char*)p_save_buf,ParagraphIdx, "debugMessage",12);
      if(sidx>=0 && p_save_buf[sidx+12]==0x08){
@@ -1501,31 +1501,14 @@
                     sscanf(tmpTime,"%d:%d:%d",&hh2,&Mm2,&ss2);
                     struct tm ttm;
                     ttm.tm_year = yy-1900;
                     ttm.tm_mon=mm - 1;
                     ttm.tm_mday = dd;
                     ttm.tm_hour=hh;
                     ttm.tm_min=Mm;
                     ttm.tm_sec=ss;
                     ttm.tm_year = yy-2000+100;ttm.tm_mon=mm;ttm.tm_mday = dd;ttm.tm_hour=hh;ttm.tm_min=Mm;ttm.tm_sec=ss;
                     time_t StartTm = mktime(&ttm);
                     ttm.tm_year = yy2-1900;
                     ttm.tm_mon=mm2 - 1;
                     ttm.tm_mday = dd2;
                     ttm.tm_hour=hh2;
                     ttm.tm_min=Mm2;
                     ttm.tm_sec=ss2;
                     ttm.tm_year = yy2-2000+100;ttm.tm_mon=mm2;ttm.tm_mday = dd2;ttm.tm_hour=hh2;ttm.tm_min=Mm2;ttm.tm_sec=ss2;
                     time_t EndTm = mktime(&ttm);
                     int dur = EndTm-StartTm;
                     sprintf(myEventLogs[i].Duration,"%d 秒",dur);
                     memcpy(myEventLogs[i].DebugMessageE,tmpdeg,msglen);
                     //calc offset datetime
                     StartTm+=8*60*60;
                     struct tm* toffset = localtime(&StartTm);
                     sprintf(myEventLogs[i].Date, "%d/%d/%d", toffset->tm_year+1900, toffset->tm_mon+1, toffset->tm_mday);
                     sprintf(myEventLogs[i].Time, "%d:%d:%d", toffset->tm_hour, toffset->tm_min, toffset->tm_sec);
                  }
               }
            }
@@ -1537,7 +1520,7 @@
   if((myEventLogs[EventCount].DebugMessageE[0]!=0|| myEventLogs[EventCount].DebugMessageS[0]!=0) && myEventLogs[EventCount].Rule[0]==0 && myEventLogs[EventCount].Region[0]==0){
      memcpy(ttmp,(unsigned char*)p_save_buf,5000);
   }
   if(somewhat && EventCount<1000-1){
   if(somewhat && EventCount<200-1){///repair
      sprintf(myEventLogs[EventCount].Camera,"%d",RequestChannel+1);
      int i;
      for (i=0;i<EventCount;i++)
@@ -1842,7 +1825,21 @@
   sprintf(cmd,"rm -f %s",path);
   system(cmd);
}
void delete_folder(const char *path)
{
   char cmd[30];
   memset(cmd,0,30);
   sprintf(cmd,"rm -r %s",path);
   system(cmd);
}
int doesFileExist(const char *filename)
{
   struct stat st;
   int result=stat(filename,&st);
   if (result==0)
    return 1;
    else return 0;
 }
int GetFileNameInFolder(char* dirname,lpfilenames lpfilename)
{  
   int num=0;
@@ -1970,166 +1967,8 @@
  outb(DATA,PortNum);
}
int Read_MysqlData(){
   int ret=0;
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server="localhost";
   char *user="root";
   char *password="123";
   char *database="opentutorials";
   puts("Step.0 ========>MySQL is getting......");
   if(!(conn=mysql_init((MYSQL*)NULL))){
      printf("init fail\n");
      return -1;
   }
   if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
      printf("connect error.\n");
      return -1;
   }
   if(mysql_select_db(conn,database)!=0){
      mysql_close(conn);
      printf("select_db_fail.\n");
      return -1;
   }
   if(mysql_query(conn,"select * from tbl_deviceinfo")){
      printf("query fail\n");
      return -1;
   }
   res=mysql_store_result(conn);
   while((row=mysql_fetch_row(res))!=NULL){
      strcat(EventString,row[1]);   strcat(EventString,":");
      strcat(EventString,row[2]); strcat(EventString,":");
      strcat(EventString,row[3]);   strcat(EventString,":");
      strcat(EventString,row[4]);   strcat(EventString,":");
      strcat(EventString,row[5]);   strcat(EventString,":");
      strcat(EventString,row[6]);   strcat(EventString,":");
      strcat(EventString,row[7]);   strcat(EventString,":");
      strcat(EventString,row[8]);   strcat(EventString,":");
      strcat(EventString,row[9]);   strcat(EventString,":");
      strcat(EventString,row[10]);strcat(EventString,"\n");
   }
   mysql_close(conn);
   return ret;
}
int UpdateMysqData(int key)
{
   int ret=0;
   MYSQL *conn;
   char *server="localhost";
   char *user="root";
   char *password="123";
   char *database="opentutorials";
   char query[255];
   puts("Step.0 ========>MySQL is getting......");
   if(!(conn=mysql_init((MYSQL*)NULL))){
      printf("init fail\n");
      exit(1);
   }
   if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
      printf("connect error.\n");
      exit(1);
   }
   if(mysql_select_db(conn,database)!=0){
      mysql_close(conn);
      printf("select_db_fail.\n");
      exit(1);
   }
   sprintf(query,"UPDATE tbl_deviceinfo SET reboot=0 where id=%d",key);
   if(mysql_query(conn,query)){
      printf("query fail\n");
      exit(1);
   }
   mysql_close(conn);
   return ret;
}
int Check_MysqlData(char *AnalyserName,char* AnalyserIP,char* AnalyserPort,char* ServerIP,char* ServerPort,char* user,char* pass,char* resol,char* commType){
   int ret=0;
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server="localhost";
   char *user1="root";
   char *password="123";
   char *database="opentutorials";
    puts("Step.0 ========>MySQL is getting......");
   if(!(conn=mysql_init((MYSQL*)NULL))){
      printf("init fail\n");
      exit(1);
   }
   if(!mysql_real_connect(conn,server,user1,password,NULL,3306,NULL,0)){
      printf("connect error.\n");
      exit(1);
   }
   if(mysql_select_db(conn,database)!=0){
      mysql_close(conn);
      printf("select_db_fail.\n");
      exit(1);
   }
   if(mysql_query(conn,"select * from tbl_deviceinfo")){
      printf("query fail\n");
      exit(1);
   }
   res=mysql_store_result(conn);
   while((row=mysql_fetch_row(res))!=NULL){
      ret=0;
//      printf("====%s:%s",row[0],row[11]);      //sprintf(equipments[NUM].m_id,"%d",row[0]);
      if(strcmp(AnalyserName,row[10]) ==0) ret = 1;
      if(strcmp(AnalyserIP,row[1]) ==0) ret =ret+1;
      if(strcmp(AnalyserPort,row[2]) ==0) ret =ret+1;
      if(strcmp(ServerIP,row[3]) ==0) ret =ret+1;
      if(strcmp(ServerPort,row[4]) ==0) ret =ret+1;
      if(strcmp(user,row[6]) ==0) ret =ret+1;
      if(strcmp(pass,row[5]) ==0) ret =ret+1;
      if(strcmp(resol,row[7]) ==0) ret =ret+1;
      if(strcmp(commType,row[8]) ==0) ret =ret+1;
      if(ret==9) break;
   }
   mysql_close(conn);
   return ret;
}
int InsertMysqlData(char *AnalyserName,char* AnalyserIP,char* AnalyserPort,char* ServerIP,char* ServerPort,char* username,char* pass,char* resol,char* commType)
{
   int ret=0;
   MYSQL *conn;
   char *server="localhost";
   char *user="root";
   char *password="123";
   char *database="opentutorials";
   char query[255];
   struct tm *t;
   char time_info[20]={0,};
   time_t timer;
   timer=time(NULL);
   t=localtime(&timer);
   sprintf(time_info, "%d-%d-%d %d:%d:%d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
   if(!(conn=mysql_init((MYSQL*)NULL))){
      printf("init fail\n");
      return -1;
   }
   if(!mysql_real_connect(conn,server,user,password,NULL,3306,NULL,0)){
      printf("connect error.\n");
      return -1;
   }
   if(mysql_select_db(conn,database)!=0){
      mysql_close(conn);
      printf("select_db_fail.\n");
      return -1;
   }
   sprintf(query, "INSERT INTO tbl_deviceinfo(equipmentip,equipmentport,serverip,serverport,password,username,analysis,comtype,redate,comment,reboot)VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s',0);",AnalyserIP, AnalyserPort, ServerIP, ServerPort,pass,username, resol, commType,time_info,AnalyserName);//?????????????
   printf("query=%s",query);
   if(mysql_query(conn,query)){
      printf("query fail\n");
      return -1;
   }
   mysql_close(conn);
   return ret;
}
//=====================================MD5 Function Definition============================================
void MD5Init (MD5_CTX *context)    /* context */
{