/*
 * Copyright (c) 1994, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/* @(#) $Header: /afs/sipb/project/tcpdump/repository/pcapture/pbuf.c,v 1.1.1.2 1997/08/17 23:19:50 jhawk Exp $ (LBL) */

#include <sys/types.h>

#include <errno.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "interface.h"

/* Round "x" up to the next multiple of "y" */
#define roundup(x, y)	((((x)+((y)-1))/(y))*(y))

/* Packet buffer */
struct pbuf {
	struct pcap_pkthdr h;
	u_char b[sizeof(u_int32_t)];	/* actual size depend on snaplen */
};

/* Locals */
static	int pbuf_cnt;			/* number of pbufs */
static	int pbuf_size;			/* size of one pbuf */
static	int pbuf_pnext_invalid;		/* pnext is being updated */

static	struct pbuf *pbuf;		/* start of circular buffer */
static	struct pbuf *pnext;		/* next pbuf to use */
static	struct pbuf *pend;		/* end of circular buffer */

/* Initialize the circular buffer */
void
pbuf_init(register int cnt, register int snaplen)
{
	u_int size;

	/* Allocate the memory */
	pbuf_cnt = cnt;
	pbuf_size = sizeof(struct pbuf) - sizeof(((struct pbuf *)0)->b);
	pbuf_size += roundup(snaplen, sizeof(u_int32_t));
	size = cnt * pbuf_size;
	pbuf = (struct pbuf *)malloc(size);
	if (pbuf == NULL)
		error("pbuf_init: malloc: %s", pcap_strerror(errno));
	memset(pbuf, 0, size);

	/* Setup pointers */
	pnext = pbuf;
	pend = (struct pbuf *)((u_char *)pbuf + size);
}

/* Sock the next packet away */
void
pbuf_process(u_char *user, const struct pcap_pkthdr *hp, const u_char *bp)
{
	register struct pbuf *p;

	p = pnext;

	/* pnext now points to an inconsistent packet */
	++pbuf_pnext_invalid;
	memcpy(&p->h, hp, sizeof(*hp));
	memcpy(p->b, bp, hp->caplen);

	/* Update pnext */
	(u_char *)p += pbuf_size;
	if (p >= pend)
		p = pbuf;
	pnext = p;

	/* pnext ok again */
	pbuf_pnext_invalid = 0;
}

/* Save the packets we've buffered to the dump file */
u_int
pbuf_dump(register pcap_dumper_t *d)
{
	register int n;
	register u_int cnt;
	register struct pbuf *p;

	/* Ignore the packet pnext points to if we're processing a new packet */
	p = pnext;
	if (pbuf_pnext_invalid) {
		(u_char *)p += pbuf_size;
		--pbuf_cnt;
	}

	cnt = 0;
	for (n = pbuf_cnt; n > 0; --n) {
		if (p->h.caplen != 0) {
			pcap_dump((u_char *)d, &p->h, p->b);
			++cnt;
		}
		(u_char *)p += pbuf_size;
		if (p >= pend)
			p = pbuf;
	}
	pcap_dump_close(d);
	return (cnt);
}
