📄 util.cpp
字号:
#include "forenet.h"#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <string.h>#include <sys/types.h>#ifndef WIN32#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#endif /* !WIN32 */#include <stdarg.h>#include <syslog.h>#include <errno.h>#include <sys/stat.h>#include <time.h>#include <signal.h>#include <unistd.h>#ifndef WIN32#include <grp.h>#include <pwd.h>#include <netdb.h>#include <limits.h>#endif /* !WIN32 */#include <fcntl.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#include "debug.h"#include "util.h"#ifdef WIN32#include "win32/WIN32-Code/name.h"#endif#ifdef PATH_MAX#define PATH_MAX_UTIL PATH_MAX#else#define PATH_MAX_UTIL 1024#endif /* PATH_MAX */#define VERSION "0.10"extern char *username;extern char *groupname;extern unsigned long userid;extern unsigned long groupid;/* * Function: GenHomenet(char *) * * Purpose: Translate the command line character string into its equivalent * 32-bit network byte ordered value (with netmask) * * Arguments: netdata => The address/CIDR block * * Returns: void function */void GenHomenet(char *netdata){}void GenObfuscationMask(char *netdata){ }/**************************************************************************** * * Function : DefineIfaceVar() * Purpose : Assign network address and network mast to IFACE_ADDR_VARNAME * variable. * Arguments : interface name (string) netaddress and netmask (4 octets each) * Returns : void function * ****************************************************************************/void DefineIfaceVar(char *iname, u_char * network, u_char * netmask){ char valbuf[32]; char varbuf[BUFSIZ]; snprintf(varbuf, BUFSIZ, "%s_ADDRESS", iname); snprintf(valbuf, 32, "%d.%d.%d.%d/%d.%d.%d.%d", network[0] & 0xff, network[1] & 0xff, network[2] & 0xff, network[3] & 0xff, netmask[0] & 0xff, netmask[1] & 0xff, netmask[2] & 0xff, netmask[3] & 0xff); }/**************************************************************************** * * Function: CalcPct(float, float) * * Purpose: Calculate the percentage of a value compared to a total * * Arguments: cnt => the numerator in the equation * total => the denominator in the calculation * * Returns: pct -> the percentage of cnt to value * ****************************************************************************/float CalcPct(float cnt, float total){ float pct; if(cnt > 0.0) pct = cnt / total; else return 0.0; pct *= 100.0; return pct;}/**************************************************************************** * * Function: DisplayBanner() * * Purpose: Show valuable proggie info * * Arguments: None. * * Returns: 0 all the time * ****************************************************************************/int DisplayBanner(){ fprintf(stderr, "\n-*> receive packet on link layer! <*-\nVersion %s (Build %s)\n" "By qianyuwen (admon1999@mail.njust.edu.cn )\n" , VERSION, BUILD); return 0;}/**************************************************************************** * * Function: ts_print(register const struct, char *) * * Purpose: Generate a time stamp and stuff it in a buffer. This one has * millisecond precision. Oh yeah, I ripped this code off from * TCPdump, props to those guys. * * Arguments: timeval => clock struct coming out of libpcap * timebuf => buffer to stuff timestamp into * * Returns: void function * ****************************************************************************/void ts_print(register const struct timeval *tvp, char *timebuf){}/**************************************************************************** * * Function: gmt2local(time_t) * * Purpose: Figures out how to adjust the current clock reading based on the * timezone you're in. Ripped off from TCPdump. * * Arguments: time_t => offset from GMT * * Returns: offset seconds from GMT * ****************************************************************************/int gmt2local(time_t t){ register int dt, dir; register struct tm *gmt, *loc; struct tm sgmt; if(t == 0) t = time(NULL); gmt = &sgmt; *gmt = *gmtime(&t); loc = localtime(&t); dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + (loc->tm_min - gmt->tm_min) * 60; dir = loc->tm_year - gmt->tm_year; if(dir == 0) dir = loc->tm_yday - gmt->tm_yday; dt += dir * 24 * 60 * 60; return(dt);}/**************************************************************************** * * Function: copy_argv(u_char **) * * Purpose: Copies a 2D array (like argv) into a flat string. Stolen from * TCPDump. * * Arguments: argv => 2D array to flatten * * Returns: Pointer to the flat string * ****************************************************************************/char *copy_argv(char **argv){ return NULL;}/**************************************************************************** * * Function: strip(char *) * * Purpose: Strips a data buffer of CR/LF/TABs. Replaces CR/LF's with * NULL and TABs with spaces. * * Arguments: data => ptr to the data buf to be stripped * * Returns: size of the newly stripped string * ****************************************************************************/int strip(char *data){ int size; char *end; char *idx; idx = data; end = data + strlen(data); size = end - idx; while(idx != end) { if((*idx == '\n') || (*idx == '\r')) { *idx = 0; size--; } if(*idx == '\t') { *idx = ' '; } idx++; } return size;}/**************************************************************************** * * Function: InitNetMasks() * * Purpose: Loads the netmask struct in network order. Yes, I know I could * just load the array when I define it, but this is what occurred * to me when I wrote this at 3:00 AM. * * Arguments: None. * * Returns: void function * ****************************************************************************/extern u_long netmasks[33]; /* defined in lubo_main.c */void InitNetmasks(){}/* * error message printing routines. in daemon mode these would go into * syslog. * * first would allow to print formatted error messages (similar to printf) and * the second is similar to perror. * */void PrintError(char *str){ if(pv.daemon_flag) syslog(LOG_CONS | LOG_DAEMON | LOG_ERR, "%s:%m", str); else perror(str);}/* * Function: ErrorMessage(const char *, ...) * * Purpose: Print a message to stderr. * * Arguments: format => the formatted error string to print out * ... => format commands/fillers * * Returns: void function */void ErrorMessage(const char *format,...){ char buf[STD_BUF+1]; va_list ap; va_start(ap, format); if(pv.daemon_flag) { vsnprintf(buf, STD_BUF, format, ap); syslog(LOG_CONS | LOG_DAEMON | LOG_ERR, "%s", buf); } else { vfprintf(stderr, format, ap); } va_end(ap);}/* * Function: LogMessage(const char *, ...) * * Purpose: Print a message to stdout or with logfacility. * * Arguments: format => the formatted error string to print out * ... => format commands/fillers * * Returns: void function */void LogMessage(const char *format,...){ char buf[STD_BUF+1]; va_list ap; if(pv.quiet_flag && !pv.daemon_flag) return; va_start(ap, format); if(pv.daemon_flag) { vsnprintf(buf, STD_BUF, format, ap); syslog(LOG_DAEMON | LOG_NOTICE, "%s", buf); } else { vfprintf(stderr, format, ap); } va_end(ap);}/* * Function: CreateApplicationEventLogEntry(const char *) * * Purpose: Add an entry to the Win32 "Application" EventLog * * Arguments: szMessage => the formatted error string to print out * * Returns: void function */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -