int main(int argc, char**argv) { int sockfd, n, i, rc; struct sockaddr_in servaddr, cliaddr; socklen_t len; char mesg[BUFSIZE]; struct clientrec clients[CLIARRAYSIZE]; fd_set fdset, workset; struct timeval timeout; long lport; if (argc != 2) { printf("Usage %s portnumber\n", argv[0]); return -1; } lport = strtol(argv[1], NULL, 10); if (lport < 1 || lport > 65535) { printf("Invalid port %ld\n", lport); return -1; } // Initialize the client array for (i = 0; i < CLIARRAYSIZE; i++) { cleanrecord(clients + i); } // Open the socket and bind to the port sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("Failed opening socket"); return -1; } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(lport); if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) { perror("Error binding socket"); close(sockfd); return -1; } FD_ZERO(&fdset); FD_SET(sockfd, &fdset); timeout.tv_sec = TIMEOUT; timeout.tv_usec = 0; for (;;) { memcpy(&workset, &fdset, sizeof(fdset)); rc = select(sockfd+1, &workset, NULL, NULL, &timeout); if (rc < 0) { perror("select() failed"); break; } if (rc == 0) { // Timeout occured // logmsg("Cleaning up records", NULL); removeoldrecords(clients, sockfd); } if (FD_ISSET(sockfd, &workset)) { len = sizeof(cliaddr); n = recvfrom(sockfd, mesg, BUFSIZE, 0, (struct sockaddr *) &cliaddr, &len); logmsg("received message from", inet_ntoa(cliaddr.sin_addr)); if (processpacket(sockfd, clients, mesg, cliaddr)) { // log error logmsg("Error processing message", NULL); } } } return 0; }
int main(int argc, char* argv[]) { struct metrics metrics; FILE *metricssourcefile; pcap_t *pcap; struct pcap_pkthdr *header; const u_char *pkt_data; int packetnum; struct timeval firstpacket; struct timeval lastpacket; double capturetime; int res; if(argc != 3) { usage(argv[0]); return 1; } memset(&metrics, 0, sizeof(struct metrics)); #pragma warning(push) #pragma warning(disable: 4996) metricssourcefile = fopen(argv[1], "r"); #pragma warning(pop) if (metricssourcefile == NULL) { printf("Couldn't open %s\n", argv[1]); return 1; } readmetricsspec(&metrics, metricssourcefile); fclose(metricssourcefile); if (openpcapfile(&pcap, argv[2])) { return 1; } /* Compile pcap filters */ if (compileallfilters(pcap, metrics.metrics)) { return -1; } /* Retrieve the packets from the file */ packetnum = 0; while((res = pcap_next_ex(pcap, &header, &pkt_data)) >= 0) { if (packetnum++ == 0) { memcpy(&firstpacket, &header->ts, sizeof(struct timeval)); } processpacket(metrics.metrics, header, pkt_data); } if (res == -1) { printf("Error reading the packets: %s\n", pcap_geterr(pcap)); } if (packetnum > 0) { memcpy(&lastpacket, &header->ts, sizeof(struct timeval)); capturetime = (lastpacket.tv_sec - firstpacket.tv_sec) + 1e-6*(lastpacket.tv_usec - firstpacket.tv_usec); if (capturetime == 0.0) { capturetime = 1.0; } printf("Results of %.1f second capture\n", capturetime); displayresults(metrics.metrics, capturetime); } else { printf("No packets read"); } return 0; }
void packet_size_breakdown(struct OPTIONS *options, char *ifname, int facilitytime, struct filterstate *ofilter) { WINDOW *win; PANEL *panel; WINDOW *borderwin; PANEL *borderpanel; struct ifstat_brackets brackets[20]; unsigned int interval; int ch; int fd; char buf[MAX_PACKET_SIZE]; int br; char *ipacket; char iface[10]; unsigned int mtu; struct sockaddr_ll fromaddr; unsigned short linktype; int pkt_result; struct timeval tv; unsigned long starttime, startlog, timeint; unsigned long now; unsigned long long unow; unsigned long updtime = 0; unsigned long long updtime_usec = 0; int logging = options->logging; FILE *logfile = NULL; struct promisc_states *promisc_list; char msgstring[80]; if (!facility_active(PKTSIZEIDFILE, ifname)) mark_facility(PKTSIZEIDFILE, "Packet size breakdown", ifname); else { snprintf(msgstring, 80, "Packet sizes already being monitored on %s", ifname); write_error(msgstring, daemonized); return; } if (!iface_supported(ifname)) { err_iface_unsupported(); unmark_facility(PKTSIZEIDFILE, ifname); return; } if (!iface_up(ifname)) { err_iface_down(); unmark_facility(PKTSIZEIDFILE, ifname); return; } borderwin = newwin(LINES - 2, COLS, 1, 0); borderpanel = new_panel(borderwin); wattrset(borderwin, BOXATTR); tx_box(borderwin, ACS_VLINE, ACS_HLINE); mvwprintw(borderwin, 0, 1, " Packet Distribution by Size "); win = newwin(LINES - 4, COLS - 2, 2, 1); panel = new_panel(win); tx_stdwinset(win); wtimeout(win, -1); wattrset(win, STDATTR); tx_colorwin(win); move(LINES - 1, 1); stdexitkeyhelp(); initialize_brackets(ifname, brackets, &interval, &mtu, win); mvwprintw(win, 1, 1, "Packet size brackets for interface %s", ifname); wattrset(win, BOXATTR); mvwprintw(win, 4, 1, "Packet Size (bytes)"); mvwprintw(win, 4, 26, "Count"); mvwprintw(win, 4, 36, "Packet Size (bytes)"); mvwprintw(win, 4, 60, "Count"); wattrset(win, HIGHATTR); if (logging) { if (strcmp(current_logfile, "") == 0) { snprintf(current_logfile, 80, "%s-%s.log", PKTSIZELOG, ifname); if (!daemonized) input_logfile(current_logfile, &logging); } } if (logging) { opentlog(&logfile, current_logfile); if (logfile == NULL) logging = 0; } if (logging) signal(SIGUSR1, rotate_size_log); writelog(logging, logfile, "******** Packet size distribution facility started ********"); exitloop = 0; gettimeofday(&tv, NULL); starttime = startlog = timeint = tv.tv_sec; open_socket(&fd); if (fd < 0) { unmark_facility(PKTSIZEIDFILE, ifname); return; } if ((first_active_facility()) && (options->promisc)) { init_promisc_list(&promisc_list); save_promisc_list(promisc_list); srpromisc(1, promisc_list); destroy_promisc_list(&promisc_list); } adjust_instance_count(PROCCOUNTFILE, 1); active_facility_countfile[0] = '\0'; do { gettimeofday(&tv, NULL); now = tv.tv_sec; unow = tv.tv_sec * 1e+6 + tv.tv_usec; if (((options->updrate != 0) && (now - updtime >= options->updrate)) || ((options->updrate == 0) && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) { update_panels(); doupdate(); updtime = now; updtime_usec = unow; } if (now - timeint >= 5) { printelapsedtime(starttime, now, LINES - 3, 1, borderwin); timeint = now; } if ((now - startlog >= options->logspan) && (logging)) { write_size_log(brackets, now - starttime, ifname, mtu, logfile); startlog = now; } check_rotate_flag(&logfile, logging); if ((facilitytime != 0) && (((now - starttime) / 60) >= facilitytime)) exitloop = 1; getpacket(fd, buf, &fromaddr, &ch, &br, iface, win); if (ch != ERR) { switch (ch) { case 12: case 'l': case 'L': tx_refresh_screen(); break; case 'x': case 'X': case 'q': case 'Q': case 27: case 24: exitloop = 1; } } if (br > 0) { pkt_result = processpacket(buf, &ipacket, &br, NULL, NULL, NULL, &fromaddr, &linktype, ofilter, MATCH_OPPOSITE_USECONFIG, iface, ifname); if (pkt_result != PACKET_OK) continue; update_size_distrib(br, brackets, interval, win); } } while (!exitloop); if (logging) { signal(SIGUSR1, SIG_DFL); write_size_log(brackets, now - starttime, ifname, mtu, logfile); writelog(logging, logfile, "******** Packet size distribution facility stopped ********"); fclose(logfile); } close(fd); if ((options->promisc) && (is_last_instance())) { load_promisc_list(&promisc_list); srpromisc(0, promisc_list); destroy_promisc_list(&promisc_list); } adjust_instance_count(PROCCOUNTFILE, -1); del_panel(panel); delwin(win); del_panel(borderpanel); delwin(borderwin); unmark_facility(PKTSIZEIDFILE, ifname); strcpy(current_logfile, ""); }