/* -*-c-*-
*
- * $Id: pkbuf.c,v 1.1 2000/06/17 10:39:19 mdw Exp $
+ * $Id: pkbuf.c,v 1.7 2004/04/08 01:36:13 mdw Exp $
*
* Simple packet buffering
*
* (c) 2000 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of the mLib utilities library.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* mLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with mLib; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: pkbuf.c,v $
- * Revision 1.1 2000/06/17 10:39:19 mdw
- * Experimental new support for packet buffering.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <stdio.h>
void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
{
size_t l;
- size_t keep;
+ size_t o, keep;
+
+ if (pk->f & PKBUF_CLOSE) {
+ pk->func(0, 0, pk, 0, pk->p);
+ return;
+ }
/* --- Initialize variables as necessary --- */
len = pk->len;
}
l = p + len - pk->buf;
- p = pk->buf;
+ o = 0;
/* --- Now grind through any packets which have accumulated --- */
- while (l > pk->want) {
+ pk->len = l;
+ while (l >= pk->want) {
size_t sz = pk->want;
/* --- Pass a packet to the user handler --- */
keep = 0;
- pk->func(p, sz, pk, &keep, pk->p);
+ pk->func(pk->buf + o, sz, pk, &keep, pk->p);
/* --- Adjust all the pointers for the next packet --- */
sz -= keep;
- p += sz;
+ o += sz;
l -= sz;
/* --- Abort here if disabled --- */
/* --- Shunt data around in the buffer --- */
- if (p > pk->buf && l != 0)
- memmove(pk->buf, p, l);
+ if (o > 0 && l != 0)
+ memmove(pk->buf, pk->buf + o, l);
pk->len = l;
}
void pkbuf_close(pkbuf *pk)
{
if (pk->buf) {
- fprintf(stderr, "*** destroying buffer, closing down\n");
x_free(pk->a, pk->buf);
pk->buf = 0;
}
+ pk->f |= PKBUF_CLOSE;
if (pk->f & PKBUF_ENABLE)
pk->func(0, 0, pk, 0, pk->p);
}
size_t pkbuf_free(pkbuf *pk, octet **p)
{
- if (!pk->buf) {
- fprintf(stderr, "*** allocating new buffer\n");
+ if (!pk->buf)
pk->buf = x_alloc(pk->a, pk->sz);
- }
*p = pk->buf + pk->len;
return (pk->sz - pk->len);
}
{
pk->want = want;
if (want > pk->sz) {
- do pk->sz <<= 1; while (want < pk->sz);
+ do pk->sz <<= 1; while (want > pk->sz);
if (pk->buf) {
if (pk->len)
- pk->buf = x_realloc(pk->a, pk->buf, pk->sz);
+ pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
else {
x_free(pk->a, pk->buf);
pk->buf = 0;
/* --- @pkbuf_init@ --- *
*
* Arguments: @pkbuf *pk@ = pointer to buffer block
- * @void (*func)(octet *b, size_t sz, pkbuf *pk,@
- * @size_t *keep, void *p)@ =
- * handler function
+ * @pkbuf *func@ = handler function
* @void *p@ = argument pointer for @func@
*
* Returns: ---
* the provided function for handling.
*/
-void pkbuf_init(pkbuf *pk,
- void (*func)(octet */*b*/, size_t /*sz*/,
- pkbuf */*pk*/, size_t */*keep*/, void */*p*/),
- void *p)
+void pkbuf_init(pkbuf *pk, pkbuf_func *func, void *p)
{
pk->func = func;
pk->p = p;