Below is the code for the program that runs my firehouses alerting system. It reads packets that come to our printer and gives us an audible alert on what piece is due. The two issuse I have with it are that it crashes anytime is receives a packet it doesnt understand and also when I access the internet from the computer its running on. It also stores captured packets so I have to go back and delete them. Im trying to make this a fireman proof as I can so there is very little maintenance. Anyone have any ideas
Code:
#include "pcap.h" #include <stdio.h> #include <string.h> void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data); void checklinesforaction(char *, char *, int , int *, int *, int *, int *, int *, int *); void checkbackups(char *, char *, int *, int *, int *, int *, int *, int *); void dispatchunits(char *, char *, char *, int *, int *, int *, int *, int *, int *); void checklinesforaction(char *linetocheck, char *checkforcommand, int setto, int *a, int *e, int *eb, int *md, int *hc, int *hsc) { char * search; search = 0; search = strstr(linetocheck, checkforcommand); if(search) { search = 0; search = strstr(linetocheck, "A830"); if(search) *a = setto; search = 0; search = strstr(linetocheck, "E830"); if(search) *e = setto; search = 0; search = strstr(linetocheck, "E830B"); if(search) *eb = setto; search = 0; search = strstr(linetocheck, "MD830"); if(search) *md = setto; search = 0; search = strstr(linetocheck, "HC830"); if(search) *hc = setto; search = 0; search = strstr(linetocheck, "HSC830"); if(search) *hsc = setto; } } void checkbackups(char *linetocheck, char *back, int *a, int *e, int *eb, int *md, int *hc, int *hsc) { char * search; char * search2; char first[16]; char hold[16]; search = 0; search = strstr(linetocheck, back); if(search) { search2 = 0; search2 = strstr(search, "A830"); if(search2) *a = 1; search2 = 0; search2 = strstr(search, "E830"); if(search2) *e = 1; search2 = 0; search2 = strstr(search, "E830B"); if(search2) *eb = 1; search2 = 0; search2 = strstr(search, "MD830"); if(search2) *md = 1; search2 = 0; search2 = strstr(search, "HC830"); if(search2) *hc = 1; search2 = 0; search2 = strstr(search, "HSC830"); if(search2) *hsc = 1; sscanf(search, "%s %s", hold, first); if(strcmp("A830", first) == 0) *a = 0; if(strcmp("E830", first) == 0) *e = 0; if(strcmp("E830B", first) == 0) *eb = 0; if(strcmp("MD830", first) == 0) *md = 0; if(strcmp("HC830", first) == 0) *hc = 0; if(strcmp("HSC830", first) == 0) *hsc = 0; } } void dispatchunits(char *area, char *type, char *location, int *ap, int *ep, int *ebp, int *mdp, int *hcp, int *hscp) { int a = *ap; int e = *ep; int eb = *ebp; int md = *mdp; int hc = *hcp; int hsc = *hscp; int beforeflag = 0; char betastring[256]; char talstring[256]; char soundstring[256]; sprintf(talstring, ":%s:%s %s\"", type, area, location); sprintf(soundstring, "soundtest "); if( !(strcmp(type, "HOUSEF")) || !(strcmp(type, "BUILDF")) || !(strcmp(type, "APTF")) || !(strcmp(type, "2ALRM")) || !(strcmp(type, "FTASK")) || !(strcmp(type, "TOWNHF")) ) { if( ((e==1) || (eb==1)) && !(strcmp(area, "30")) ) { sprintf(betastring, "beta r r \"DUMP THE HOUSE! %s\"", location); system(betastring); system("soundtest firstdue.wav"); printf("dispatching first due fire:\n%s\n", betastring); } else if( ((e==1) || (eb==1)) ) { strcat(soundstring, "engine"); sprintf(betastring, "beta r r \""); if(e==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " ENG "); strcat(soundstring, "enginebox"); beforeflag = 1; } if(eb==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " ENGB "); strcat(soundstring, "enginebox"); } strcat(betastring, talstring); strcat(soundstring, ".wav"); system(betastring); system(soundstring); printf("dispatching fire:\n%s\n", betastring); printf("%s\n", soundstring); } } else if(a==1 || e==1 || eb==1 || md==1 || hc==1 || hsc==1) { sprintf(betastring, "beta g g\""); if(e==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " ENG "); strcat(soundstring, "engine"); beforeflag = 1; } if(eb==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " ENGB "); strcat(soundstring, "engine"); beforeflag = 1; } if(a==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " AMB "); strcat(soundstring, "ambo"); beforeflag = 1; } if(md==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " MED "); strcat(soundstring, "medic"); beforeflag = 1; } if(hc==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " HAZ-MAT "); strcat(soundstring, "Hazmat"); beforeflag = 1; } if(hsc==1) { if(beforeflag) strcat(betastring, ":"); strcat(betastring, " HAZ-MAT "); strcat(soundstring, "Hazmat"); beforeflag = 1; } strcat(betastring, talstring); strcat(soundstring, ".wav"); system(betastring); system(soundstring); printf("dispatching call:\n%s\n", betastring); printf("%s\n", soundstring); } } main() { pcap_if_t *alldevs; pcap_if_t *d; int inum; int i=0; pcap_t *adhandle; char errbuf[PCAP_ERRBUF_SIZE]; /* Retrieve the device list */ if(pcap_findalldevs(&alldevs, errbuf) == -1) { fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); exit(1); } /* Print the list */ for(d=alldevs; d; d=d->next) { printf("%d. %s", ++i, d->name); if (d->description) printf(" (%s)\n", d->description); else printf(" (No description available)\n"); } if(i==0) { printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); return -1; } printf("Enter the interface number (1-%d):",i); scanf("%d", &inum); if(inum < 1 || inum > i) { printf("\nInterface number out of range.\n"); /* Free the device list */ pcap_freealldevs(alldevs); return -1; } /* Jump to the selected adapter */ for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); /* Open the device */ /* Open the adapter */ if ((adhandle= pcap_open_live(d->name, // name of the device 65536, // portion of the packet to capture. // 65536 grants that the whole packet will be captured on all the MACs. 1, // promiscuous mode (nonzero means promiscuous) 1000, // read timeout errbuf // error buffer )) == NULL) { fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name); /* Free the device list */ pcap_freealldevs(alldevs); return -1; } printf("\nlistening on %s...\n", d->description); /* At this point, we don't need any more the device list. Free it */ pcap_freealldevs(alldevs); /* start the capture */ pcap_loop(adhandle, 0, packet_handler, NULL); pcap_close(adhandle); return 0; } /* Callback function invoked by libpcap for every incoming packet */ void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) { u_int i=0; char *searcher = 0; char c; int j; int locflag; static int locationtripped = 0; static char currentline[256]; static int currentlinepos = 0; static char lastdispatched[256]; static char dispatchstring[256]; char betastring[256]; static int a = 0; static int e = 0; static int eb = 0; static int md = 0; static int hc = 0; static int hsc = 0; static int pulse = 0; static char location[128]; static char type[16]; static char area[16]; static FILE *fp; static int printoutscanning = 0; static char printoutID[20]; static char printoutfileID[32]; static FILE *dbg; if(pulse) { if(pulse == 80) { sprintf(betastring, "beta a a a"); printf("betastring: \"%s\"\n", betastring); system(betastring); pulse = 0; } else pulse++; } if(header->len > 96) { for(i=55; (i < header->caplen + 1 ) ; i++) { c = pkt_data[i-1]; if(c == 13 || c == 10) { currentline[currentlinepos] = 0; currentlinepos = 0; j = strlen(currentline); if(j && (j > 1)) { if(strlen(printoutfileID) && printoutscanning) { dbg = fopen(printoutfileID, "a"); fprintf(dbg, "%s\n", currentline); fclose(dbg); } if(!printoutscanning) { searcher = 0; searcher = strstr(currentline, "INCIDENT HISTORY DETAIL:"); if(searcher) { searcher = searcher + 26; strncpy(printoutID, searcher, 9); printoutID[9] = 0; printoutscanning = 1; a = 0; e = 0; eb = 0; md = 0; hc = 0; hsc = 0; for(j = 0; j < 128; j++) location[j] = 0; for(j = 0; j < 16; j++) { type[j] = 0; area[j] = 0; } sprintf(printoutfileID, "%s %.6d.txt", printoutID, header-> ts.tv_usec); dbg = fopen(printoutfileID, "a"); fprintf(dbg, "%s\n", currentline); fclose(dbg); } } else { searcher = 0; searcher = strstr(currentline, "OPERATOR ASSIGN"); if(searcher) { sprintf(dispatchstring, "[%s] %s %s\nA:%d E:%d EB:%d MD:%d HC:%d HSC:%d", area, type, location, a, e, eb, md, hc, hsc); printoutscanning = 0; for(j = 0; j < strlen(printoutID); j++) printoutID[j] = 0; for(j = 0; j < strlen(currentline); j++) currentline[j] = 0; currentlinepos = 0; locationtripped = 0; printf("%s\n", dispatchstring); if(strcmp(lastdispatched, dispatchstring)) { dispatchunits(area, type, location, &a, &e, &eb, &md, &hc, &hsc); if( (a==1) || (e==1) || (eb==1) || (md==1) || (hc==1) || (hsc==1) ) pulse = 1; } strcpy(lastdispatched, dispatchstring); } else { searcher = 0; searcher = strstr(currentline, "LOCATION: "); if(searcher && !locationtripped) { locationtripped = 1; searcher = searcher + 10; strcpy(location, searcher); locflag = 0; for(j = 1; j < strlen(location); j++) { if(locflag) location[j] = 0; else if(location[j] == ' ') if(location[j-1] == ' ') { locflag = 1; location[j-1] = 0; location[j] = 0; } } } searcher = 0; searcher = strstr(currentline, "STATION:"); if(searcher) { sscanf(searcher, "STATION: %s", area); searcher = 0; searcher = strstr(currentline, "OLD TYPE:"); if(searcher) { searcher = strstr(currentline, "FINAL:"); sscanf(searcher, "FINAL: %s", type); } else { searcher = strstr(currentline, "TYPE:"); sscanf(searcher, "TYPE: %s", type); } } checklinesforaction(currentline, "DISPATCH", 1, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "DISP-RSP", 1, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "ENROUTE", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "ONSCENE", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "TRANSPRT", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "TR-CMPT", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "CLOSE", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "ON-RADIO", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "PREEMPT", 0, &a, &e, &eb, &md, &hc, &hsc); checklinesforaction(currentline, "STATION", 0, &a, &e, &eb, &md, &hc, &hsc); checkbackups(currentline, "BACKUP", &a, &e, &eb, &md, &hc, &hsc); checkbackups(currentline, "BACK-ER", &a, &e, &eb, &md, &hc, &hsc); checkbackups(currentline, "BACK-OS", &a, &e, &eb, &md, &hc, &hsc); checkbackups(currentline, "REPLACE", &a, &e, &eb, &md, &hc, &hsc); } } } } else { currentline[currentlinepos] = c; currentlinepos++; } } } }
Comment