📄 scan.cpp
字号:
#include <sys/types.h>#include <stdlib.h>#include <fcntl.h>#include <sys/time.h>#include <unistd.h>#include <stdio.h>#include <sys/types.h>#include <stdlib.h>#include <sys/wait.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#include <time.h>#include <errno.h>extern int errno;#define FATAL_ERROR 0x00000001#define COMMON_ERROR 0x00000002#define COMMON_LOG 0x00000010#define DEBUG_LOG 0x00000020int debug_level= FATAL_ERROR | COMMON_ERROR | COMMON_LOG;#define dbg_print0(level,x); if ( level&debug_level) {\ struct tm *ptm;\ time_t t;\ time(&t);\ ptm = localtime(&t);\ FILE* trace_warn;\ trace_warn = fopen(fname_log, "a"); \ if (trace_warn!=NULL) {\ fprintf(trace_warn, "%.2d:%.2d:%.2d>>>", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);\ fprintf(trace_warn, x); \ fclose(trace_warn);\ }\ }#define dbg_print1(level,x,y); if ( level&debug_level) {\ struct tm *ptm;\ time_t t;\ time(&t);\ ptm = localtime(&t);\ FILE* trace_warn;\ trace_warn = fopen(fname_log, "a"); \ if (trace_warn!=NULL) {\ fprintf(trace_warn, "%.2d:%.2d:%.2d>>>", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);\ fprintf(trace_warn, x,y); \ fclose(trace_warn);\ }\ }#define dbg_print2(level,x,y,z); if ( level&debug_level) {\ struct tm *ptm;\ time_t t;\ time(&t);\ ptm = localtime(&t);\ FILE* trace_warn;\ trace_warn = fopen(fname_log, "a"); \ if (trace_warn!=NULL) {\ fprintf(trace_warn, "%.2d:%.2d:%.2d>>>", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);\ fprintf(trace_warn, x,y,z); \ fclose(trace_warn);\ }\ }#define dbg_print3(level,x,y,z,q); if ( level&debug_level) {\ struct tm *ptm;\ time_t t;\ time(&t);\ ptm = localtime(&t);\ FILE* trace_warn;\ trace_warn = fopen(fname_log, "a"); \ if (trace_warn!=NULL) {\ fprintf(trace_warn, "%.2d:%.2d:%.2d>>>", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);\ fprintf(trace_warn, x,y,z,q); \ fclose(trace_warn);\ }\ }#define SCAN_INTERVAL 120#define MAXCHANNELNUM 200#define TRUE 1#define FALSE 0#define MAX_IP_NAME_LEN 32#define MAX_FILE_NAME_LEN 256#define NULL_CH 0#define REALTIME_CH 0x801#define FILE_CH 0x802struct ACTIVE_CHANNEL{ int channel_id; int pid; int end_flag; //0:continue running;1:end of running int type_flag; // REALTIME_CH , FILE_CH}active_channel[MAXCHANNELNUM];static void child_quit( int signo ); void initChnTable();int createChn(char * channel_id, char * program_ip, char * program_port, char * channel_ip, char * channel_port,char * band_width);int killChn(char * channel_id);int updateStatusFile(char *filename);int get(char *name,char *s);double CountSecond(char *s);void recvSign(int signo);char fname_log[MAX_FILE_NAME_LEN]="chn_log.txt";FILE * fp;char lip[MAX_IP_NAME_LEN]="0.0.0.0";char fname_vodfile[MAX_FILE_NAME_LEN]="vodfile";int main(int argv, char **argc){ struct timeval tm; int ret; char currentDir[MAX_FILE_NAME_LEN]="./"; char fname_Ch[MAX_FILE_NAME_LEN]="Ch.txt"; char fname_scanPid[MAX_FILE_NAME_LEN]="scanPid.txt"; char fname_status[MAX_FILE_NAME_LEN]="status"; char str[128]; char source_ip[MAX_IP_NAME_LEN],source_port[16], target_ip[MAX_IP_NAME_LEN],target_port[16], start_time[64],end_time[64], channel_no[16],bandwidth[128],describe[256], mode[16],v_pid[16],a_pid[16],program_type[6]; time_t currentTime; char timeStr[128]; struct tm * localTime; FILE *scanFP; int index; // get program's directory char * tempstr=NULL; strcpy(currentDir,argc[0]); tempstr = rindex(currentDir,'/'); if ( tempstr==NULL ) strcpy(currentDir, "./"); else *(tempstr+1)='\0'; strcpy(fname_Ch, currentDir); strcat(fname_Ch,"Ch.txt"); strcpy(fname_scanPid, currentDir); strcat(fname_scanPid,"scanPid.txt"); strcpy(fname_status, currentDir); strcat(fname_status,"status"); strcpy(fname_log, currentDir); strcat(fname_log,"chn_log.txt"); strcpy(fname_vodfile, currentDir); strcat(fname_vodfile,"vodfile"); for(index=1;index<argv;index++){ if ( (tempstr=strstr(argc[index], "lip="))!=NULL ) { tempstr+=4; strcpy(lip, tempstr); } else if((strstr(argc[index],"--update"))!=NULL){ scanFP=fopen(fname_scanPid,"r"); if(scanFP==NULL){ printf("failure in opening the file scanPid.txt\n"); } int pid; if(fscanf(scanFP,"%d",&pid)==1){ char commstr[30]; sprintf(commstr,"kill -14 %d",pid); // printf("the command string is: %s\n",commstr); system(commstr); } else{ printf("there is a error when reading the file scanPid.txt\n"); } fclose(scanFP); exit(0); } } signal(SIGALRM,recvSign); signal(SIGCHLD,child_quit); // avoid child to be dead process// signal(SIGCHLD,SIG_IGN); // avoid child to be dead process scanFP=fopen(fname_scanPid,"w+"); if(scanFP==NULL){ printf("failure in opening the file scanPid.txt\n"); } fprintf(scanFP,"%d",(int)getpid()); fclose(scanFP); initChnTable(); while(1){ while((fp=fopen(fname_Ch,"r"))==NULL){ printf("Maybe program sheet is being edit,wait 5 seconds.\n"); sleep(5); } while (!feof(fp)){ fscanf(fp,"%s",str); while(strcmp(str,"[channel]")&&(!feof(fp))) fscanf(fp,"%s",str); if(feof(fp)) break; if((get("source_ip=",source_ip)) && (get("source_port=",source_port)) && (get("target_ip=",target_ip)) && (get("target_port=",target_port)) &&(get("start_time=",start_time)) &&(get("end_time=",end_time)) &&(get("channel_no=",channel_no)) &&(get("bandwidth=",bandwidth)) &&(get("describe=",describe)) &&(get("mode=",mode)) &&(get("v_pid=",v_pid)) &&(get("a_pid=",a_pid)) &&(get("type=",program_type))){ if(atoi(mode)==0){//自动模式 currentTime = time(NULL); localTime = localtime(¤tTime); sprintf(timeStr,"%d:%d:%d",localTime->tm_hour,localTime->tm_min,localTime->tm_sec); if(CountSecond(start_time)==CountSecond(end_time)){ if( createChn(channel_no, source_ip,source_port,target_ip, target_port,bandwidth)==1 ) { printf("createChn return 1\n"); dbg_print2(COMMON_LOG,"BEGIN CYCLE program on channel %s at %s.\n",channel_no,timeStr); } continue; } else if(CountSecond(timeStr)>CountSecond(start_time) && CountSecond(timeStr)<CountSecond(end_time)){ if( createChn(channel_no, source_ip,source_port,target_ip, target_port,bandwidth)==1 ) { dbg_print2(COMMON_LOG,"BEGIN AUTO program on channel %s at %s.\n",channel_no,timeStr); } continue; } else if(CountSecond(timeStr)>CountSecond(end_time) || CountSecond(timeStr)<CountSecond(start_time)){ if(killChn(channel_no)==1) dbg_print2(COMMON_LOG,"STOP AUTO program on channel %s at %s.\n",channel_no,timeStr); continue; } }//自动模式处理结束 else if(atoi(mode)==1){ //手动启动 if(createChn(channel_no, source_ip,source_port,target_ip, target_port,bandwidth)==1) dbg_print2(COMMON_LOG,"BEGIN MANUAL program on channel %s at %s.\n",channel_no,timeStr); continue; } else if(atoi(mode)==2){ //手动停止 if(killChn(channel_no)==1) dbg_print2(COMMON_LOG,"STOP MANUAL program on channel %s at %s.\n",channel_no,timeStr); continue; } }//一条记录处理结束 }//结束一次扫描 fclose(fp); // kill not active channel for(index=0;index<MAXCHANNELNUM;index++) { if(active_channel[index].channel_id>0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -