/*
 * 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:  realtime.c
 *
 * Purpose:  This source file defines the function realtime().  This function
 * is used by pcap_loop, it recieves packets, parses them, filters them, and
 * puts them into the network object for buffering.
 *
 */


#include <pcap.h>
#include <stdlib.h>


#include "db.h"
#include "engine.h"
#include "globals.h"
#include "headers.h"
#include "network.h"
#include "parser.h"


void realtime(unsigned char *user, const struct pcap_pkthdr *header,
              const unsigned char *packet) 
{
    Packet_Headers *		p;
    Packet_Headers *            db_p;
    struct timeval *            db_ts;
    Net_Obj * 			netobj;

    netobj = NULL;
    if ((p = malloc(sizeof(Packet_Headers))) == NULL)
        die("malloc");

    if (parse(packet, p) == -1) { /* We have an unknown packet type. */
        free(p);
        return;
    }
    filter(p, &netobj);

    if (netobj != NULL) {
        /* We assign each element individually to handle the "bpf_timeval"
         * present in some RPM versions of pcap */
        netobj->ts.tv_sec = header->ts.tv_sec;
        netobj->ts.tv_usec = header->ts.tv_usec;

        netobj->len = header->len;

        if (opts->gui) {
            into_net(net, netobj);
        }

        if (opts->db) {
            if ((db_p = malloc(sizeof(Packet_Headers))) == NULL)
                die("malloc");

            db_p->type = p->type;

            if ((db_p->ll.eth = malloc(sizeof(Eth_Header))) == NULL)
                die("malloc");
            memcpy(db_p->ll.eth, p->ll.eth, sizeof(Eth_Header));

            switch (db_p->type) {
            case arp_t:
                if ((db_p->nl.arp = malloc(sizeof(Arp_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->nl.arp, p->nl.arp, sizeof(Arp_Header));
                break;
            case tcp_t:
                if ((db_p->nl.ip = malloc(sizeof(Ip_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->nl.ip, p->nl.ip, sizeof(Ip_Header));
                if ((db_p->tl.tcp = malloc(sizeof(Tcp_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->tl.tcp, p->tl.tcp, sizeof(Tcp_Header));
                break;
            case udp_t:
                if ((db_p->nl.ip = malloc(sizeof(Ip_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->nl.ip, p->nl.ip, sizeof(Ip_Header));
                if ((db_p->tl.udp = malloc(sizeof(Udp_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->tl.udp, p->tl.udp, sizeof(Udp_Header));
                break;
            case icmp_t:
                if ((db_p->nl.ip = malloc(sizeof(Ip_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->nl.ip, p->nl.ip, sizeof(Ip_Header));
                if ((db_p->tl.icmp = malloc(sizeof(Icmp_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->tl.icmp, p->tl.icmp, sizeof(Icmp_Header));
                break;
            case igmp_t:
                if ((db_p->nl.ip = malloc(sizeof(Ip_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->nl.ip, p->nl.ip, sizeof(Ip_Header));
                if ((db_p->tl.igmp = malloc(sizeof(Igmp_Header))) == NULL)
                    die("malloc");
                memcpy(db_p->tl.igmp, p->tl.igmp, sizeof(Igmp_Header));
                break;
            case unknown_t:
                /* FALLTHROUGH */
            default:
                free(db_p);
                return;
            }

            if ((db_ts = malloc(sizeof(struct timeval))) == NULL)
                die("malloc");
            memcpy(db_ts, &header->ts, sizeof(struct timeval));

            into_db(db_p, db_ts);
        }
    } else {
        free(p);
    }

    return;
}


syntax highlighted by Code2HTML, v. 0.9.1