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 ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @pkbuf_flush@ --- *
40 * Arguments: @pkbuf *pk@ = pointer to buffer block
41 * @octet *p@ = pointer to where to start searching
42 * @size_t len@ = length of new material added
46 * Use: Flushes any complete packets in a packet buffer. New
47 * material is assumed to have been added starting at @p@. If
48 * @p@ is null, then the scan starts at the beginning of the
49 * buffer, and the size of data already in the buffer is used in
52 * It is assumed that the buffer is initially enabled. You
53 * shouldn't be contributing data to a disabled buffer anyway.
54 * However, the buffer handler may at some point disable itself,
55 * and @pkbuf_flush@ can cope with this eventuality. Any
56 * pending data is left at the start of the buffer and can be
57 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
61 void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
66 if (pk->f & PKBUF_CLOSE) {
67 pk->func(0, 0, pk, 0, pk->p);
71 /* --- Initialize variables as necessary --- */
77 l = p + len - pk->buf;
80 /* --- Now grind through any packets which have accumulated --- */
83 while (l >= pk->want) {
86 /* --- Pass a packet to the user handler --- */
89 pk->func(pk->buf + o, sz, pk, &keep, pk->p);
91 /* --- Adjust all the pointers for the next packet --- */
97 /* --- Abort here if disabled --- */
99 if (!(pk->f & PKBUF_ENABLE))
103 /* --- Shunt data around in the buffer --- */
106 memmove(pk->buf, pk->buf + o, l);
110 /* --- @pkbuf_close@ --- *
112 * Arguments: @pkbuf *pk@ = pointer to buffer block
116 * Use: Informs the client that no more data is likely to arrive. If
117 * there is a partial packet in the buffer, it is discarded.
120 void pkbuf_close(pkbuf *pk)
123 x_free(pk->a, pk->buf);
126 pk->f |= PKBUF_CLOSE;
127 if (pk->f & PKBUF_ENABLE)
128 pk->func(0, 0, pk, 0, pk->p);
131 /* --- @pkbuf_free@ --- *
133 * Arguments: @pkbuf *pk@ = pointer to buffer block
134 * @octet **p@ = output pointer to free space
136 * Returns: Free buffer size.
138 * Use: Returns the free portion of a packet buffer. Data can then
139 * be written to this portion, and split out into packets by
140 * calling @pkbuf_flush@. A buffer is allocated if none
144 size_t pkbuf_free(pkbuf *pk, octet **p)
147 pk->buf = x_alloc(pk->a, pk->sz);
148 *p = pk->buf + pk->len;
149 return (pk->sz - pk->len);
152 /* --- @pkbuf_snarf@ --- *
154 * Arguments: @pkbuf *pk@ = pointer to buffer block
155 * @const void *p@ = pointer to input data buffer
156 * @size_t sz@ = size of data in input buffer
160 * Use: Snarfs the data from the input buffer and spits it out as
161 * packets. This interface ignores the complexities of dealing
162 * with disablement: you should be using @pkbuf_free@ to
163 * contribute data if you want to cope with that.
166 void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
169 while (sz && (pk->f & PKBUF_ENABLE)) {
173 bsz = pkbuf_free(pk, &bp);
177 pkbuf_flush(pk, bp, bsz);
183 /* --- @pkbuf_want@ --- *
185 * Arguments: @pkbuf *pk@ = pointer to buffer block
186 * @size_t want@ = how many octets wanted for next packet
190 * Use: Sets the desired size for the next packet to be read. If
191 * it's larger than the current buffer, the buffer is extended.
194 void pkbuf_want(pkbuf *pk, size_t want)
198 do pk->sz <<= 1; while (want > pk->sz);
201 pk->buf = x_realloc(pk->a, pk->buf, pk->sz, pk->len);
203 x_free(pk->a, pk->buf);
210 /* --- @pkbuf_init@ --- *
212 * Arguments: @pkbuf *pk@ = pointer to buffer block
213 * @pkbuf *func@ = handler function
214 * @void *p@ = argument pointer for @func@
218 * Use: Initializes a packet buffer block. Any packets are passed to
219 * the provided function for handling.
222 void pkbuf_init(pkbuf *pk, pkbuf_func *func, void *p)
227 pk->f = PKBUF_ENABLE;
231 pk->a = arena_global;
234 /* --- @pkbuf_destroy@ --- *
236 * Arguments: @pkbuf *pk@ = pointer to buffer block
240 * Use: Deallocates a line buffer and frees any resources it owned.
243 void pkbuf_destroy(pkbuf *pk)
246 x_free(pk->a, pk->buf);
251 /*----- That's all, folks -------------------------------------------------*/