3 * $Id: pkbuf.c,v 1.7 2004/04/08 01:36:13 mdw Exp $
5 * Simple packet buffering
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @pkbuf_flush@ --- *
44 * Arguments: @pkbuf *pk@ = pointer to buffer block
45 * @octet *p@ = pointer to where to start searching
46 * @size_t len@ = length of new material added
50 * Use: Flushes any complete packets in a packet buffer. New
51 * material is assumed to have been added starting at @p@. If
52 * @p@ is null, then the scan starts at the beginning of the
53 * buffer, and the size of data already in the buffer is used in
56 * It is assumed that the buffer is initially enabled. You
57 * shouldn't be contributing data to a disabled buffer anyway.
58 * However, the buffer handler may at some point disable itself,
59 * and @pkbuf_flush@ can cope with this eventuality. Any
60 * pending data is left at the start of the buffer and can be
61 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
65 void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
70 if (pk->f & PKBUF_CLOSE) {
71 pk->func(0, 0, pk, 0, pk->p);
75 /* --- Initialize variables as necessary --- */
81 l = p + len - pk->buf;
84 /* --- Now grind through any packets which have accumulated --- */
87 while (l >= pk->want) {
90 /* --- Pass a packet to the user handler --- */
93 pk->func(pk->buf + o, sz, pk, &keep, pk->p);
95 /* --- Adjust all the pointers for the next packet --- */
101 /* --- Abort here if disabled --- */
103 if (!(pk->f & PKBUF_ENABLE))
107 /* --- Shunt data around in the buffer --- */
110 memmove(pk->buf, pk->buf + o, l);
114 /* --- @pkbuf_close@ --- *
116 * Arguments: @pkbuf *pk@ = pointer to buffer block
120 * Use: Informs the client that no more data is likely to arrive. If
121 * there is a partial packet in the buffer, it is discarded.
124 void pkbuf_close(pkbuf *pk)
127 x_free(pk->a, pk->buf);
130 pk->f |= PKBUF_CLOSE;
131 if (pk->f & PKBUF_ENABLE)
132 pk->func(0, 0, pk, 0, pk->p);
135 /* --- @pkbuf_free@ --- *
137 * Arguments: @pkbuf *pk@ = pointer to buffer block
138 * @octet **p@ = output pointer to free space
140 * Returns: Free buffer size.
142 * Use: Returns the free portion of a packet buffer. Data can then
143 * be written to this portion, and split out into packets by
144 * calling @pkbuf_flush@. A buffer is allocated if none
148 size_t pkbuf_free(pkbuf *pk, octet **p)
151 pk->buf = x_alloc(pk->a, pk->sz);
152 *p = pk->buf + pk->len;
153 return (pk->sz - pk->len);
156 /* --- @pkbuf_snarf@ --- *
158 * Arguments: @pkbuf *pk@ = pointer to buffer block
159 * @const void *p@ = pointer to input data buffer
160 * @size_t sz@ = size of data in input buffer
164 * Use: Snarfs the data from the input buffer and spits it out as
165 * packets. This interface ignores the complexities of dealing
166 * with disablement: you should be using @pkbuf_free@ to
167 * contribute data if you want to cope with that.
170 void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
173 while (sz && (pk->f & PKBUF_ENABLE)) {
177 bsz = pkbuf_free(pk, &bp);
181 pkbuf_flush(pk, bp, bsz);
187 /* --- @pkbuf_want@ --- *
189 * Arguments: @pkbuf *pk@ = pointer to buffer block
190 * @size_t want@ = how many octets wanted for next packet
194 * Use: Sets the desired size for the next packet to be read. If
195 * it's larger than the current buffer, the buffer is extended.
198 void pkbuf_want(pkbuf *pk, size_t want)
202 do pk->sz <<= 1; while (want > pk->sz);
205 pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
207 x_free(pk->a, pk->buf);
214 /* --- @pkbuf_init@ --- *
216 * Arguments: @pkbuf *pk@ = pointer to buffer block
217 * @pkbuf *func@ = handler function
218 * @void *p@ = argument pointer for @func@
222 * Use: Initializes a packet buffer block. Any packets are passed to
223 * the provided function for handling.
226 void pkbuf_init(pkbuf *pk, pkbuf_func *func, void *p)
231 pk->f = PKBUF_ENABLE;
235 pk->a = arena_global;
238 /* --- @pkbuf_destroy@ --- *
240 * Arguments: @pkbuf *pk@ = pointer to buffer block
244 * Use: Deallocates a line buffer and frees any resources it owned.
247 void pkbuf_destroy(pkbuf *pk)
250 x_free(pk->a, pk->buf);
255 /*----- That's all, folks -------------------------------------------------*/