tcpdump mailing list archives

Help with script using pcap.h


From: "J.R. Sellers" <jrsellers09 () gmail com>
Date: Mon, 7 Sep 2009 15:37:00 -0400

Hello everyone!  First off - I am beginner at this kind of stuff so please
bare with me on this.  I am a firefighter at a fire station.  We currently
have a program that runs "cygwin" on Windows and runs the following script.
 I am trying to move this script over to a Ubuntu Server that runs faster
and more efficiently than the computer that is running Windows 2000.
The goal of this program is to capture a printout that is sent from the
dispatchers to a printer at our station.  The program then reads the
printout line by line and determines if it needs to send the call to the
signboard displays and play a message.  This program is hopefully able to
run continuously with no down time.

I finally have figured out how to compile the script and make it run,
however I get a segment fragmentation error when it runs.  I have also been
told by someone who is smart in this kind of stuff that the reason the
program currently crashes so much in Windows is because it is not "releasing
memory" back to the system and is getting larger and larger until the
computer runs out of memory.   A copy of the script is attached.  Any help
in improving the script would be greatly appreciated!

Thanks again!  J.R. Sellers

prealert.c File:
-------------------------------------------------------------------------------------------------------------------

#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 *);
void checkbackups(char *, char *, int *, int *, int *, int *);
void dispatchunits(char *, char *, char *, int *, int *, int *, int *);

void checklinesforaction(char *linetocheck, char *checkforcommand, int
setto, int *a, int *e, int *eb, int *t)
{

    char * search;

search = 0;
search = strstr(linetocheck, checkforcommand);
if(search)
{
search = 0;
search = strstr(linetocheck, "A834");
if(search)
*a = setto;

search = 0;
search = strstr(linetocheck, "E834");
if(search)
*e = setto;
search = 0;
search = strstr(linetocheck, "E834B");
if(search)
*eb = setto;

search = 0;
search = strstr(linetocheck, "TK834");
if(search)
*t = setto;
}
}


void checkbackups(char *linetocheck, char *back, int *a, int *e, int *eb,
int *t)
{

    char * search;
    char * search2;
    char first[16];
    char hold[16];

search = 0;
search = strstr(linetocheck, back);
if(search)
{
search2 = 0;
search2 = strstr(search, "A834");
if(search2)
*a = 1;
search2 = 0;
search2 = strstr(search, "E834");
if(search2)
*e = 1;
search2 = 0;
search2 = strstr(search, "E834B");
if(search2)
*eb = 1;
search2 = 0;
search2 = strstr(search, "TK834");
if(search2)
*t = 1;
sscanf(search, "%s %s", hold, first);
if(strcmp("A834", first) == 0)
*a = 0;
if(strcmp("E834", first) == 0)
*e = 0;
if(strcmp("E834B", first) == 0)
*eb = 0;
if(strcmp("TK834", first) == 0)
*t = 0;
}
}

void dispatchunits(char *area, char *type, char *location, int *ap, int *ep,
int *ebp, int *tp)
{

    int a = *ap;
    int e = *ep;
    int eb = *ebp;
    int t = *tp;

int beforeflag = 0;
char betastring[256];
char talstring[256];
char soundstring[256];
sprintf(talstring, ":%s:%s   %s\"", type, area, location);
sprintf(soundstring, "soundtest ");

if((a==1) && (e!=1) && (eb!=1) && (t!=1))
{
sprintf(betastring, "beta g r \"AMB:%s:%s   %s\"", type, area, location);
system(betastring);
system("soundtest ambulance.wav");
printf("Dispatching AMBO:\n%s\n", betastring);
} else if( !(strcmp(type, "HOUSEF")) || !(strcmp(type, "BUILDF")) ||
!(strcmp(type, "APTF"))
|| !(strcmp(type, "2ALRM")) )
{
if( ((e==1) || (eb==1) || (t==1)) && !(strcmp(area, "34")) )
{
sprintf(betastring, "beta r r \"DUMP THE HOUSE!   %s\"", location);
system(betastring);
system("soundtest house.wav");
printf("Dispatching FIRST DUE FIRE:\n%s\n", betastring);
} else if( ((e==1) || (eb==1) || (t==1)) )
{
strcat(soundstring, "fire");
sprintf(betastring, "beta r r \"");
if(t==1)
{
strcat(betastring, "TRK");
strcat(soundstring, "trk");
beforeflag = 1;
}
if(e==1)
{
if(beforeflag)
strcat(betastring, ":");
strcat(betastring, "ENG");
strcat(soundstring, "eng");
beforeflag = 1;
}
if(eb==1)
{
if(beforeflag)
strcat(betastring, ":");
strcat(betastring, "ENGB");
strcat(soundstring, "eng");
}
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 || t==1)
{
sprintf(betastring, "beta y r \"");
if(t==1)
{
strcat(betastring, "TRK");
strcat(soundstring, "trk");
beforeflag = 1;
}
if(e==1)
{
if(beforeflag)
strcat(betastring, ":");
strcat(betastring, "ENG");
strcat(soundstring, "eng");
beforeflag = 1;
}
if(eb==1)
{
if(beforeflag)
strcat(betastring, ":");
strcat(betastring, "ENGB");
strcat(soundstring, "eng");
beforeflag = 1;
}
if(a==1)
{
if(beforeflag)
strcat(betastring, ":");
strcat(betastring, "AMB");
strcat(soundstring, "amb");
beforeflag = 1;
}
strcat(betastring, talstring);
strcat(soundstring, ".wav");
system(betastring);
system(soundstring);
printf("Dispatching Call:\n%s\n", betastring);
printf("%s\n", soundstring);
}

}


main()
{
//original basic_dump main function

    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)
{
//custom function. catches each character of incoming packets,
//builds printout lines by filtering for alphanumerics and endlines.
//does the pre-dispatch thinking and stores many variables as "statics"
//since this function will run 2-4 times per printout and
//must remember variable settings between runs, like location and type.
//also prints the entire printout to the basic_dump folder just for debug.

        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 t = 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;
                            t = 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 T:%d",
                                area, type, location, a, e, eb, t);
                            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, &t);
                                if( (a==1) || (e==1) || (eb==1) || (t==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, &t);
                            checklinesforaction(currentline, "DISP-RSP", 1,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "ENROUTE", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "ONSCENE", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "TRANSPRT", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "TR-CMPT", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "CLOSE", 0, &a,
&e, &eb, &t);
                            checklinesforaction(currentline, "ON-RADIO", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "PREEMPT", 0,
&a, &e, &eb, &t);
                            checklinesforaction(currentline, "STATION", 0,
&a, &e, &eb, &t);
                            checkbackups(currentline, "BACKUP", &a, &e, &eb,
&t);
                            checkbackups(currentline, "BACK-ER", &a, &e,
&eb, &t);
                            checkbackups(currentline, "BACK-OS", &a, &e,
&eb, &t);
                            checkbackups(currentline, "REPLACE", &a, &e,
&eb, &t);

                        }
                    }
                }
            } else {
                currentline[currentlinepos] = c;
                currentlinepos++;
            }
        }
        }
}
-
This is the tcpdump-workers list.
Visit https://cod.sandelman.ca/ to unsubscribe.


Current thread: