/*
 * Copyright (c) 2001 Fenris, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * File:  engine.c
 *
 * Purpose:  This source file initializes the use of libpcap for listening
 * on the specified network interface.  pcap_loop() is used for continual
 * listen and read cycle, pcap_loop() sends each packet to realtime.c
 *
 */


#include <pcap.h>               /* pcap_x() */


#include "engine.h"
#include "filter.h"
#include "globals.h"
#include "network.h"
#include "realtime.h"


void *engine(void *arg)
{
    pcap_t *            pcap;
    char                ebuf[PCAP_ERRBUF_SIZE];
    char *              dev             = NULL;
    int                 snaplen         = SNAPLEN;
    int                 promisc         = 1;
    int                 to_ms           = 1000;
    int                 count           = 0;
    bpf_u_int32         localnet;
    bpf_u_int32         netmask;
    struct bpf_program  fcode;

    /* Find a valid device. */
    if (dev == NULL && (dev = pcap_lookupdev(ebuf)) == NULL) {
        die(ebuf);
    }

    /* Open our pcap object. */
    if ((pcap = pcap_open_live(dev, snaplen, promisc, to_ms, ebuf)) == NULL) {
        die(ebuf);
    }

    if (pcap_lookupnet(dev, &localnet, &netmask, ebuf) < 0) {
        die(ebuf);
    }

    if (pcap_compile(pcap, &fcode, "", 1, netmask) < 0) {
        die(ebuf);
    }

    if (pcap_setfilter(pcap, &fcode) < 0) {
        die(ebuf);
    }

    printf("tvark: listening on %s\n", dev);

    /* Let pcap process our packets as they come in, calling output(). */
    if (pcap_loop(pcap, count, realtime, NULL) < 0) {
        die(pcap_geterr(pcap));
    }
    pcap_close(pcap);

    return 0;
}


void usage(void)
{
    fprintf(stderr, "usage: tvark [-dg] [-f file] [-i interface]\n");
    exit(2);
}


syntax highlighted by Code2HTML, v. 0.9.1