3 * $Id: pkbuf.c,v 1.2 2000/07/16 12:29:16 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/07/16 12:29:16 mdw
34 * Change to arena `realloc' interface, to fix a design bug.
36 * Revision 1.1 2000/06/17 10:39:19 mdw
37 * Experimental new support for packet buffering.
41 /*----- Header files ------------------------------------------------------*/
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @pkbuf_flush@ --- *
55 * Arguments: @pkbuf *pk@ = pointer to buffer block
56 * @octet *p@ = pointer to where to start searching
57 * @size_t len@ = length of new material added
61 * Use: Flushes any complete packets in a packet buffer. New
62 * material is assumed to have been added starting at @p@. If
63 * @p@ is null, then the scan starts at the beginning of the
64 * buffer, and the size of data already in the buffer is used in
67 * It is assumed that the buffer is initially enabled. You
68 * shouldn't be contributing data to a disabled buffer anyway.
69 * However, the buffer handler may at some point disable itself,
70 * and @pkbuf_flush@ can cope with this eventuality. Any
71 * pending data is left at the start of the buffer and can be
72 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
76 void pkbuf_flush(pkbuf
*pk
, octet
*p
, size_t len
)
81 /* --- Initialize variables as necessary --- */
87 l
= p
+ len
- pk
->buf
;
90 /* --- Now grind through any packets which have accumulated --- */
92 while (l
> pk
->want
) {
95 /* --- Pass a packet to the user handler --- */
98 pk
->func(p
, sz
, pk
, &keep
, pk
->p
);
100 /* --- Adjust all the pointers for the next packet --- */
106 /* --- Abort here if disabled --- */
108 if (!(pk
->f
& PKBUF_ENABLE
))
112 /* --- Shunt data around in the buffer --- */
114 if (p
> pk
->buf
&& l
!= 0)
115 memmove(pk
->buf
, p
, l
);
119 /* --- @pkbuf_close@ --- *
121 * Arguments: @pkbuf *pk@ = pointer to buffer block
125 * Use: Informs the client that no more data is likely to arrive. If
126 * there is a partial packet in the buffer, it is discarded.
129 void pkbuf_close(pkbuf
*pk
)
132 fprintf(stderr
, "*** destroying buffer, closing down\n");
133 x_free(pk
->a
, pk
->buf
);
136 if (pk
->f
& PKBUF_ENABLE
)
137 pk
->func(0, 0, pk
, 0, pk
->p
);
140 /* --- @pkbuf_free@ --- *
142 * Arguments: @pkbuf *pk@ = pointer to buffer block
143 * @octet **p@ = output pointer to free space
145 * Returns: Free buffer size.
147 * Use: Returns the free portion of a packet buffer. Data can then
148 * be written to this portion, and split out into packets by
149 * calling @pkbuf_flush@. A buffer is allocated if none
153 size_t pkbuf_free(pkbuf
*pk
, octet
**p
)
156 fprintf(stderr
, "*** allocating new buffer\n");
157 pk
->buf
= x_alloc(pk
->a
, pk
->sz
);
159 *p
= pk
->buf
+ pk
->len
;
160 return (pk
->sz
- pk
->len
);
163 /* --- @pkbuf_snarf@ --- *
165 * Arguments: @pkbuf *pk@ = pointer to buffer block
166 * @const void *p@ = pointer to input data buffer
167 * @size_t sz@ = size of data in input buffer
171 * Use: Snarfs the data from the input buffer and spits it out as
172 * packets. This interface ignores the complexities of dealing
173 * with disablement: you should be using @pkbuf_free@ to
174 * contribute data if you want to cope with that.
177 void pkbuf_snarf(pkbuf
*pk
, const void *p
, size_t sz
)
180 while (sz
&& (pk
->f
& PKBUF_ENABLE
)) {
184 bsz
= pkbuf_free(pk
, &bp
);
188 pkbuf_flush(pk
, bp
, bsz
);
194 /* --- @pkbuf_want@ --- *
196 * Arguments: @pkbuf *pk@ = pointer to buffer block
197 * @size_t want@ = how many octets wanted for next packet
201 * Use: Sets the desired size for the next packet to be read. If
202 * it's larger than the current buffer, the buffer is extended.
205 void pkbuf_want(pkbuf
*pk
, size_t want
)
209 do pk
->sz
<<= 1; while (want
< pk
->sz
);
212 pk
->buf
= x_realloc(pk
->a
, pk
->buf
, pk
->sz
, pk
->len
);
214 x_free(pk
->a
, pk
->buf
);
221 /* --- @pkbuf_init@ --- *
223 * Arguments: @pkbuf *pk@ = pointer to buffer block
224 * @void (*func)(octet *b, size_t sz, pkbuf *pk,@
225 * @size_t *keep, void *p)@ =
227 * @void *p@ = argument pointer for @func@
231 * Use: Initializes a packet buffer block. Any packets are passed to
232 * the provided function for handling.
235 void pkbuf_init(pkbuf
*pk
,
236 void (*func
)(octet */
*b*/
, size_t /*sz*/,
237 pkbuf */
*pk*/
, size_t */
*keep*/
, void */
*p*/
),
243 pk
->f
= PKBUF_ENABLE
;
247 pk
->a
= arena_global
;
250 /* --- @pkbuf_destroy@ --- *
252 * Arguments: @pkbuf *pk@ = pointer to buffer block
256 * Use: Deallocates a line buffer and frees any resources it owned.
259 void pkbuf_destroy(pkbuf
*pk
)
262 x_free(pk
->a
, pk
->buf
);
267 /*----- That's all, folks -------------------------------------------------*/