📄 iptraf.c
字号:
if (!aborted) { if (strcmp(ifname, "") != 0) ifptr = ifname; else ifptr = NULL; hostmon(options, 0, ifptr, &ofilter); } break; case 7: config_filters(&ofilter); savefilters(&ofilter); break; case 9: setoptions(options, &ports); saveoptions(options); break; case 11: endloop = 1; break; } } while (!endloop); tx_destroymenu(&menu); } else { switch (opt) { case 'i': if ((strcmp(optarg, ALLSPEC) == 0) || (strcmp(optarg, "") == 0)) ifptr = NULL; else ifptr = optarg; ipmon(options, &ofilter, facilitytime, ifptr); break; case 'g': ifstats(options, &ofilter, facilitytime); break; case 'd': detstats(optarg, options, facilitytime, &ofilter); break; case 's': servmon(optarg, ports, options, facilitytime, &ofilter); break; case 'z': packet_size_breakdown(options, optarg, facilitytime, &ofilter); break; case 'l': if ((strcmp(optarg, ALLSPEC) == 0) || (strcmp(optarg, "") == 0)) ifptr = NULL; else ifptr = optarg; hostmon(options, facilitytime, ifptr, &ofilter); break; } } destroyporttab(ports); erase(); update_panels(); doupdate();}/* * Command-line help facility. */void commandhelp(){ printf("\nSyntax:\n"); printf (" iptraf [ -f ] [ { -i iface | -g | -d iface | -s iface | -z iface |\n"); printf (" -l iface } [ -t timeout ] [ -B ] [ -L logfile ] [-I interval] ] \n\n"); printf ("Issue the iptraf command with no parameters for menu-driven operation.\n"); printf("These options can also be supplied to the command:\n\n"); printf ("-i iface - start the IP traffic monitor (use \"-i all\" for all interfaces)\n"); printf("-g - start the general interface statistics\n"); printf ("-d iface - start the detailed statistics facility on an interface\n"); printf ("-s iface - start the TCP and UDP monitor on an interface\n"); printf("-z iface - shows the packet size counts on an interface\n"); printf ("-l iface - start the LAN station monitor (\"-l all\" for all LAN interfaces)\n"); printf ("-B - run in background (use only with one of the above parameters)\n"); printf ("-t timeout - when used with one of the above parameters, tells\n"); printf (" the facility to run only for the specified number of\n"); printf(" minutes (timeout)\n"); printf ("-L logfile - specifies an alternate log file for any direct invocation\n"); printf (" of a facility from the command line. The log is placed in\n"); printf(" %s if path is not specified.\n", LOGDIR); printf ("-I interval - specifies the log interval for all facilities except the IP\n"); printf(" traffic monitor. Value is in minutes.\n"); printf ("-f - clear all locks and counters. Use with great caution.\n"); printf (" Normally used to recover from an abnormal termination.\n\n"); printf("IPTraf %s Copyright (c) Gerard Paul Java 1997-2004\n", VERSION);}int first_instance(){ int fd; fd = open(IPTIDFILE, O_RDONLY); if (fd < 0) return !0; else { close(fd); return 0; }}void mark_first_instance(){ int fd; fd = open(IPTIDFILE, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR); if (fd < 0) { fprintf(stderr, "\nWarning: unable to tag this process\r\n"); press_enter_to_continue(); return; } close(fd);}/* * Main routine */int main(int argc, char **argv){ struct OPTIONS options; int opt = 0; int command = 0; char keyparm[12]; int facilitytime = 0; int current_log_interval;#ifndef ALLOWUSERS if (getuid() != 0) { fprintf(stderr, "\nIPTraf Version %s\n", VERSION); fprintf(stderr, "Copyright (c) Gerard Paul Java 1997-2004l\n\n"); fprintf(stderr, "This program can be run only by the system administrator\n\n"); exit(1); }#endif strcpy(current_logfile, ""); strcpy(graphing_logfile, ""); strcpy(graphing_filter, ""); /* * Parse command line */ if (argc > 1) { do { opterr = 0; opt = getopt(argc, argv, "i:gd:s:z:l:hfqt:BL:uI:G:F:"); if (opt == 'h') { commandhelp(); exit(0); } else if (opt == 'f') { removetags(); remove_sockets(); } else if (opt == 't') { facilitytime = atoi(optarg); if (facilitytime == 0) { fprintf(stderr, "\nInvalid time value\n\n"); exit(1); } } else if (opt == 'B') { daemonized = 1; setenv("TERM", "linux", 1); } else if (opt == 'L') { if (strchr(optarg, '/') != NULL) strncpy(current_logfile, optarg, 80); else strncpy(current_logfile, get_path(T_LOGDIR, optarg), 80); } else if (opt == 'q') { /* -q parameter now ignored, maintained for compatibility */ } else if (opt == 'u') { accept_unsupported_interfaces = 1; } else if (opt == 'I') { current_log_interval = atoi(optarg); if (current_log_interval == 0) fprintf(stderr, "Invalid log interval value\n"); exit(1); } else if (opt == 'G') { if (strchr(optarg, '/') != NULL) strncpy(graphing_logfile, optarg, 80); else strncpy(graphing_logfile, get_path(T_LOGDIR, optarg), 80); daemonized = 1; } else if (opt == 'F') { strncpy(graphing_filter, optarg, 80); } else if (opt == '?') { fprintf(stderr, "\nInvalid option or missing parameter, use iptraf -h for help\n\n"); exit(1); } else if (opt != -1) { if (optarg != 0) { bzero(keyparm, 12); strncpy(keyparm, optarg, 11); } else strcpy(keyparm, ""); command = opt; } } while ((opt != '?') && (opt != -1)); } is_first_instance = first_instance(); if ((getenv("TERM") == NULL) && (!daemonized)) { fprintf(stderr, "Your TERM variable is not set.\n"); fprintf(stderr, "Please set it to an appropriate value.\n"); exit(1); } if (graphing_logfile[0] != '\0' && graphing_filter[0] == '\0') { fprintf(stderr, "Specify an IP filter name with -F\n"); exit(1); } loadoptions(&options); /* * If a facility is directly invoked from the command line, check for * a daemonization request */ if ((daemonized) && (command != 0)) { switch (fork()) { case 0: /* child */ setsid(); freopen("/dev/null", "w", stdout); /* redirect std output */ freopen("/dev/null", "r", stdin); /* redirect std input */ freopen("/dev/null", "w", stderr); /* redirect std error */ signal(SIGUSR2, (void *) term_usr2_handler); if (graphing_logfile[0] != '\0') options.logging = 0; /* if raw logging is specified */ else /* then standard logging is disabled */ options.logging = 1; break; case -1: /* error */ fprintf(stderr, "\nFork error, IPTraf cannot run in background\n\n"); exit(1); default: /* parent */ exit(0); } }#ifdef SIMDAEMON daemonized = 1; freopen("/dev/null", "w", stdout); /* redirect std output */ freopen("/dev/null", "r", stdin); freopen("/dev/null", "w", stderr);#endif initscr(); if ((LINES < 24) || (COLS < 80)) { endwin(); fprintf(stderr, "\nThis program requires a screen size of at least 80 columns by 24 lines\n"); fprintf(stderr, "Please resize your window\n\n"); exit(1); } mark_first_instance(); signal(SIGTERM, (void *) term_signal_handler); signal(SIGHUP, (void *) term_signal_handler); signal(SIGSEGV, (void *) segvhandler); signal(SIGTSTP, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGUSR1, SIG_IGN); start_color(); standardcolors(options.color); noecho(); nonl(); cbreak();#ifndef DEBUG curs_set(0);#endif /* * Set logfilename variable to NULL if -L was specified without an * appropriate facility on the command line. */ if (command == 0) strcpy(current_logfile, ""); /* * If by this time the logfile is still acceptable, obtain the * logspan from the command line if so specified. */ if (current_logfile[0] != '\0') { options.logging = 1; if (current_log_interval != 0) { options.logspan = current_log_interval; } } program_interface(&options, command, keyparm, facilitytime); endwin(); if (is_first_instance) unlink(IPTIDFILE); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -