3 * Simple packet buffering
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @pkbuf_flush@ --- *
42 * Arguments: @pkbuf *pk@ = pointer to buffer block
43 * @octet *p@ = pointer to where to start searching
44 * @size_t len@ = length of new material added
48 * Use: Flushes any complete packets in a packet buffer. New
49 * material is assumed to have been added starting at @p@. If
50 * @p@ is null, then the scan starts at the beginning of the
51 * buffer, and the size of data already in the buffer is used in
54 * It is assumed that the buffer is initially enabled. You
55 * shouldn't be contributing data to a disabled buffer anyway.
56 * However, the buffer handler may at some point disable itself,
57 * and @pkbuf_flush@ can cope with this eventuality. Any
58 * pending data is left at the start of the buffer and can be
59 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
63 void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
68 if (pk->f & PKBUF_CLOSE) {
69 pk->func(0, 0, pk, 0, pk->p);
73 /* --- Initialize variables as necessary --- */
79 l = p + len - pk->buf;
82 /* --- Now grind through any packets which have accumulated --- */
85 while (l >= pk->want) {
88 /* --- Pass a packet to the user handler --- */
91 pk->func(pk->buf + o, sz, pk, &keep, pk->p);
93 /* --- Adjust all the pointers for the next packet --- */
99 /* --- Abort here if disabled --- */
101 if (!(pk->f & PKBUF_ENABLE))
105 /* --- Shunt data around in the buffer --- */
108 memmove(pk->buf, pk->buf + o, l);
112 /* --- @pkbuf_close@ --- *
114 * Arguments: @pkbuf *pk@ = pointer to buffer block
118 * Use: Informs the client that no more data is likely to arrive. If
119 * there is a partial packet in the buffer, it is discarded.
122 void pkbuf_close(pkbuf *pk)
125 x_free(pk->a, pk->buf);
128 pk->f |= PKBUF_CLOSE;
129 if (pk->f & PKBUF_ENABLE)
130 pk->func(0, 0, pk, 0, pk->p);
133 /* --- @pkbuf_free@ --- *
135 * Arguments: @pkbuf *pk@ = pointer to buffer block
136 * @octet **p@ = output pointer to free space
138 * Returns: Free buffer size.
140 * Use: Returns the free portion of a packet buffer. Data can then
141 * be written to this portion, and split out into packets by
142 * calling @pkbuf_flush@. A buffer is allocated if none
146 size_t pkbuf_free(pkbuf *pk, octet **p)
149 pk->buf = x_alloc(pk->a, pk->sz);
150 *p = pk->buf + pk->len;
151 return (pk->sz - pk->len);
154 /* --- @pkbuf_snarf@ --- *
156 * Arguments: @pkbuf *pk@ = pointer to buffer block
157 * @const void *p@ = pointer to input data buffer
158 * @size_t sz@ = size of data in input buffer
162 * Use: Snarfs the data from the input buffer and spits it out as
163 * packets. This interface ignores the complexities of dealing
164 * with disablement: you should be using @pkbuf_free@ to
165 * contribute data if you want to cope with that.
168 void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
171 while (sz && (pk->f & PKBUF_ENABLE)) {
175 bsz = pkbuf_free(pk, &bp);
179 pkbuf_flush(pk, bp, bsz);
185 /* --- @pkbuf_want@ --- *
187 * Arguments: @pkbuf *pk@ = pointer to buffer block
188 * @size_t want@ = how many octets wanted for next packet
192 * Use: Sets the desired size for the next packet to be read. If
193 * it's larger than the current buffer, the buffer is extended.
196 void pkbuf_want(pkbuf *pk, size_t want)
200 do pk->sz <<= 1; while (want > pk->sz);
203 pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
205 x_free(pk->a, pk->buf);
212 /* --- @pkbuf_init@ --- *
214 * Arguments: @pkbuf *pk@ = pointer to buffer block
215 * @pkbuf *func@ = handler function
216 * @void *p@ = argument pointer for @func@
220 * Use: Initializes a packet buffer block. Any packets are passed to
221 * the provided function for handling.
224 void pkbuf_init(pkbuf *pk, pkbuf_func *func, void *p)
229 pk->f = PKBUF_ENABLE;
233 pk->a = arena_global;
236 /* --- @pkbuf_destroy@ --- *
238 * Arguments: @pkbuf *pk@ = pointer to buffer block
242 * Use: Deallocates a line buffer and frees any resources it owned.
245 void pkbuf_destroy(pkbuf *pk)
248 x_free(pk->a, pk->buf);
253 /*----- That's all, folks -------------------------------------------------*/