More help for a fireman

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgfdbug
    New Member
    • Feb 2009
    • 14

    More help for a fireman

    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++;
                }
            }
            } 
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I only skimmed through all that code (it really is too much code to post here) and I don't know where these variables a, e, eb, md etc. come from, i.e. they're not global variables so they should be declared somewere in that function.

    Normally your compiler will tell you about undefined (undeclared) variables. Read its error diagnostic messages and fix them one by one.

    kind regards,

    Jos

    Comment

    • pgfdbug
      New Member
      • Feb 2009
      • 14

      #3
      My apologies on the length of the code. The variables e,eb, a, m, hc, hsc; are the different units in the station. E=engine, A=ambulance and so forth. Should I put the code as an attachment next time? Again i apologize im still learning.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by pgfdbug
        My apologies on the length of the code. The variables e,eb, a, m, hc, hsc; are the different units in the station. E=engine, A=ambulance and so forth. Should I put the code as an attachment next time? Again i apologize im still learning.
        No need to apologize; as a rule of thumb people should post an SSCCE. Most of the time crafting an SSCCE makes the potential poster solve his/her own problem even before posting it :-)

        About your problem: we both know what the semantics of those variables are, i.e. you know what they mean. A compiler isn't so smart; all it sees is something named 'a' and then it throws up: it hasn't seen a declaration, nor a definition of that thing so the compiler doesn't know what it can do with it, so it starts whining; that's the way compilers are. They are not as forgiving as humans and they don't know anything about 'meaning', or 'semantics'. They're just plain stupid.

        kind regards,

        Jos

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          I notice that function packet_handler (line 293) contains static definitions for a bunch of variables. Are these the same variables that you're trying to access from other function (as per question from JosAH)? If so, then you should move those declarations to the top of the file so they are available to all the functions.

          Comment

          • pgfdbug
            New Member
            • Feb 2009
            • 14

            #6
            Line 293 is a repeat of line 5 if that matters.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by pgfdbug
              Line 293 is a repeat of line 5 if that matters.
              That's not what Donbock meant: in that function starting at line 293 you have a couple of local static variables. Do you know what that means?

              kind regards,

              Jos

              Comment

              • pgfdbug
                New Member
                • Feb 2009
                • 14

                #8
                Im not sure what they are, but Im in the process of learning as I type.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by pgfdbug
                  Im not sure what they are, but Im in the process of learning as I type.
                  A local static variable keeps existing even when that function has finished execution. The next time the function executes that same variable (with its last stored value) will be available again to that function. Outside of that function that variable is invisible (just like any local variable).

                  kind regards,

                  Jos

                  Comment

                  • pgfdbug
                    New Member
                    • Feb 2009
                    • 14

                    #10
                    I think i understand the variables. I dont know if Im going down the right path, but is part of the problem that if a packet comes in that doesnt match any of the variables then the compilier doent know what to do with it, so it crashes?

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by pgfdbug
                      I think i understand the variables. I dont know if Im going down the right path, but is part of the problem that if a packet comes in that doesnt match any of the variables then the compilier doent know what to do with it, so it crashes?
                      I don't understand: your code doesn't compile so how can you run it and make it crash?

                      kind regards,

                      Jos

                      Comment

                      • pgfdbug
                        New Member
                        • Feb 2009
                        • 14

                        #12
                        As I said Im still learning so I might not be using the correct terminology. The program runs and does what it is supposed to. The problem is sometimes packets come that dont have any thing to do with the program then the program shuts down. My best guess right now is that since there is nothing in the strange packets that the program recognizes it crashes. I believe the static variables describe what the program is supposed to look for. So now im trying to figure a way to write something into the program that tells it to kick out things it doesnt recognize.
                        Again thank you for your patience and help.

                        Comment

                        Working...