📄 util.cpp
字号:
#if defined(WIN32) && defined(ENABLE_WIN32_SERVICE)void CreateApplicationEventLogEntry(const char *msg){ HANDLE hEventLog; char* pEventSourceName = "testpcapService"; /* prepare to write to Application log on local host * with Event Source of testpcapService */ AddEventSource(pEventSourceName); hEventLog = RegisterEventSource(NULL, pEventSourceName); if (hEventLog == NULL) { /* Could not register the event source. */ return; } if (!ReportEvent(hEventLog, /* event log handle */ EVENTLOG_ERROR_TYPE, /* event type */ 0, /* category zero */ EVMSG_SIMPLE, /* event identifier */ NULL, /* no user security identifier */ 1, /* one substitution string */ 0, /* no data */ &msg, /* pointer to array of strings */ NULL)) /* pointer to data */ { /* Could not report the event. */ } DeregisterEventSource(hEventLog); } #endif /* WIN32 && ENABLE_WIN32_SERVICE *//* * Function: FatalError(const char *, ...) * * Purpose: When a fatal error occurs, this function prints the error message * and cleanly shuts down the program * * Arguments: format => the formatted error string to print out * ... => format commands/fillers * * Returns: void function */void FatalError(const char *format,...){ char buf[STD_BUF+1]; va_list ap; va_start(ap, format); vsnprintf(buf, STD_BUF, format, ap); if(pv.daemon_flag) { syslog(LOG_CONS | LOG_DAEMON | LOG_ERR, "FATAL ERROR: %s", buf); } else { fprintf(stderr, "ERROR: %s", buf); fprintf(stderr,"Fatal Error, Quitting..\n");#if defined(WIN32) && defined(ENABLE_WIN32_SERVICE) CreateApplicationEventLogEntry(buf);#endif } return;}void FatalPrintError(char *msg){ PrintError(msg); return ;}/**************************************************************************** * * Function: CreatePidFile(char *) * * Purpose: Creates a PID file * * Arguments: Interface opened. * * Returns: void function * ****************************************************************************/void CreatePidFile(char *intf){ }/**************************************************************************** * * Function: SetUidGid(char *) * * Purpose: Sets safe UserID and GroupID if needed * * Arguments: none * * Returns: void function * ****************************************************************************/void SetUidGid(void){}/* need int parameter here because of function declaration of signal(2) */void DropStats(int iParamIgnored){ return;}/**************************************************************************** * * Function: InitProtoNames() * * Purpose: Initializes the protocol names * * Arguments: None. * * Returns: void function * ****************************************************************************/void InitProtoNames(){}/**************************************************************************** * * Function: CleanupProtoNames() * * Purpose: Frees the protocol names * * Arguments: None. * * Returns: void function * ****************************************************************************/void CleanupProtoNames(){}/**************************************************************************** * * Function: read_infile(char *) * * Purpose: Reads the BPF filters in from a file. Ripped from tcpdump. * * Arguments: fname => the name of the file containing the BPF filters * * Returns: the processed BPF string * ****************************************************************************/char *read_infile(char *fname){ register int fd, cc; register char *cp, *cmt; struct stat buf; return(cp);} /**************************************************************************** * * Function: SanityChecks() * * Purpose: CyberPsychotic sez: basically we only check if logdir exist and * writable, since it might screw the whole thing in the middle. Any * other checks could be performed here as well. * * Arguments: None. * * Returns: void function * ****************************************************************************//* TODO rename this function to reflect what it really does */void SanityChecks(void){ struct stat st; char log_dir[STD_BUF]; snprintf(log_dir, STD_BUF, "%s", pv.log_dir); stat(log_dir, &st); if(!S_ISDIR(st.st_mode) || access(log_dir, W_OK) == -1) { FatalError("\n[!] ERROR: " "Can not get write access to logging directory \"%s\".\n" "(directory doesn't exist or permissions are set incorrectly\n" /* * let us add this comment. Too many people seem to * confuse it otherwise :-) */ "or it is not a directory at all)\n\n", log_dir); }}/**************************************************************************** * * Function: GoDaemon() * * Purpose: Puts the program into daemon mode, nice and quiet like.... * * Arguments: None. * * Returns: void function * ****************************************************************************/void GoDaemon(void){}/* This function has been moved into mstring.c, since that* is where the allocation actually occurs. It has been* renamed to mSplitFree().*void FreeToks(char **toks, int num_toks){ if (toks) { if (num_toks > 0) { do { num_toks--; free(toks[num_toks]); } while(num_toks); } free(toks); }}*//* Self preserving memory allocator */void *SPAlloc(unsigned long size, struct _SPMemControl *spmc){}void *testpcapAlloc(unsigned long size){ void *tmp; return tmp;}/** * Chroot and adjust the pv.log_dir reference * * @param directory directory to chroot to * @param logdir ptr to pv.log_dir */void SetChroot(char *directory, char **logstore){ char *absdir; int abslen; char *logdir; if(!directory || !logstore) { FatalError("Null parameter passed\n"); } logdir = *logstore; if(logdir == NULL || *logdir == '\0') { FatalError("Null log directory\n"); } DEBUG_WRAP(DebugMessage(DEBUG_INIT,"SetChroot: %s\n", CurrentWorkingDir());); logdir = GetAbsolutePath(logdir); DEBUG_WRAP(DebugMessage(DEBUG_INIT, "SetChroot: %s\n", CurrentWorkingDir())); logdir = strdup(logdir); if(logdir == NULL) { FatalError("SetChroot: Out of memory"); } /* change to the directory */ if(chdir(directory) != 0) { FatalError("SetChroot: Can not chdir to \"%s\": %s\n", directory, strerror(errno)); } /* always returns an absolute pathname */ absdir = CurrentWorkingDir(); if(absdir == NULL) { FatalError("NULL Chroot found\n"); } abslen = strlen(absdir); DEBUG_WRAP(DebugMessage(DEBUG_INIT, "ABS: %s %d\n", absdir, abslen);); /* make the chroot call */ if(chroot(absdir) < 0) { FatalError("Can not chroot to \"%s\": absolute: %s: %s\n", directory, absdir, strerror(errno)); } DEBUG_WRAP(DebugMessage(DEBUG_INIT,"chroot success (%s ->", absdir);); DEBUG_WRAP(DebugMessage(DEBUG_INIT,"%s)\n ", CurrentWorkingDir());); /* change to "/" in the new directory */ if(chdir("/") < 0) { FatalError("Can not chdir to \"/\" after chroot: %s\n", strerror(errno)); } DEBUG_WRAP(DebugMessage(DEBUG_INIT,"chdir success (%s)\n", CurrentWorkingDir());); if(strncmp(absdir, logdir, strlen(absdir))) { FatalError("Absdir is not a subset of the logdir"); } if(abslen >= strlen(logdir)) { *logstore = "/"; } else { *logstore = logdir + abslen; } DEBUG_WRAP(DebugMessage(DEBUG_INIT,"new logdir from %s to %s\n", logdir, *logstore));}/** * Return a ptr to the absolute pathname of testpcap. This memory must * be copied to another region if you wish to save it for later use. */char *CurrentWorkingDir(void){ static char buf[PATH_MAX_UTIL + 1]; if(getcwd((char *) buf, PATH_MAX_UTIL) == NULL) { return NULL; } buf[PATH_MAX_UTIL] = '\0'; return (char *) buf;}/** * Given a directory name, return a ptr to a static */char *GetAbsolutePath(char *dir){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -