chiark / gitweb /
Version bump.
[mLib] / pkbuf.c
CommitLineData
436fddaa 1/* -*-c-*-
2 *
3 * $Id: pkbuf.c,v 1.1 2000/06/17 10:39:19 mdw Exp $
4 *
5 * Simple packet buffering
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: pkbuf.c,v $
33 * Revision 1.1 2000/06/17 10:39:19 mdw
34 * Experimental new support for packet buffering.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#include "alloc.h"
45#include "arena.h"
46#include "pkbuf.h"
47
48/*----- Main code ---------------------------------------------------------*/
49
50/* --- @pkbuf_flush@ --- *
51 *
52 * Arguments: @pkbuf *pk@ = pointer to buffer block
53 * @octet *p@ = pointer to where to start searching
54 * @size_t len@ = length of new material added
55 *
56 * Returns: ---
57 *
58 * Use: Flushes any complete packets in a packet buffer. New
59 * material is assumed to have been added starting at @p@. If
60 * @p@ is null, then the scan starts at the beginning of the
61 * buffer, and the size of data already in the buffer is used in
62 * place of @len@.
63 *
64 * It is assumed that the buffer is initially enabled. You
65 * shouldn't be contributing data to a disabled buffer anyway.
66 * However, the buffer handler may at some point disable itself,
67 * and @pkbuf_flush@ can cope with this eventuality. Any
68 * pending data is left at the start of the buffer and can be
69 * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer
70 * is ever re-enabled.
71 */
72
73void pkbuf_flush(pkbuf *pk, octet *p, size_t len)
74{
75 size_t l;
76 size_t keep;
77
78 /* --- Initialize variables as necessary --- */
79
80 if (!p) {
81 p = pk->buf;
82 len = pk->len;
83 }
84 l = p + len - pk->buf;
85 p = pk->buf;
86
87 /* --- Now grind through any packets which have accumulated --- */
88
89 while (l > pk->want) {
90 size_t sz = pk->want;
91
92 /* --- Pass a packet to the user handler --- */
93
94 keep = 0;
95 pk->func(p, sz, pk, &keep, pk->p);
96
97 /* --- Adjust all the pointers for the next packet --- */
98
99 sz -= keep;
100 p += sz;
101 l -= sz;
102
103 /* --- Abort here if disabled --- */
104
105 if (!(pk->f & PKBUF_ENABLE))
106 break;
107 }
108
109 /* --- Shunt data around in the buffer --- */
110
111 if (p > pk->buf && l != 0)
112 memmove(pk->buf, p, l);
113 pk->len = l;
114}
115
116/* --- @pkbuf_close@ --- *
117 *
118 * Arguments: @pkbuf *pk@ = pointer to buffer block
119 *
120 * Returns: ---
121 *
122 * Use: Informs the client that no more data is likely to arrive. If
123 * there is a partial packet in the buffer, it is discarded.
124 */
125
126void pkbuf_close(pkbuf *pk)
127{
128 if (pk->buf) {
129 fprintf(stderr, "*** destroying buffer, closing down\n");
130 x_free(pk->a, pk->buf);
131 pk->buf = 0;
132 }
133 if (pk->f & PKBUF_ENABLE)
134 pk->func(0, 0, pk, 0, pk->p);
135}
136
137/* --- @pkbuf_free@ --- *
138 *
139 * Arguments: @pkbuf *pk@ = pointer to buffer block
140 * @octet **p@ = output pointer to free space
141 *
142 * Returns: Free buffer size.
143 *
144 * Use: Returns the free portion of a packet buffer. Data can then
145 * be written to this portion, and split out into packets by
146 * calling @pkbuf_flush@. A buffer is allocated if none
147 * currently exists.
148 */
149
150size_t pkbuf_free(pkbuf *pk, octet **p)
151{
152 if (!pk->buf) {
153 fprintf(stderr, "*** allocating new buffer\n");
154 pk->buf = x_alloc(pk->a, pk->sz);
155 }
156 *p = pk->buf + pk->len;
157 return (pk->sz - pk->len);
158}
159
160/* --- @pkbuf_snarf@ --- *
161 *
162 * Arguments: @pkbuf *pk@ = pointer to buffer block
163 * @const void *p@ = pointer to input data buffer
164 * @size_t sz@ = size of data in input buffer
165 *
166 * Returns: ---
167 *
168 * Use: Snarfs the data from the input buffer and spits it out as
169 * packets. This interface ignores the complexities of dealing
170 * with disablement: you should be using @pkbuf_free@ to
171 * contribute data if you want to cope with that.
172 */
173
174void pkbuf_snarf(pkbuf *pk, const void *p, size_t sz)
175{
176 const octet *pp = p;
177 while (sz && (pk->f & PKBUF_ENABLE)) {
178 size_t bsz;
179 octet *bp;
180
181 bsz = pkbuf_free(pk, &bp);
182 if (bsz > sz)
183 bsz = sz;
184 memcpy(bp, pp, bsz);
185 pkbuf_flush(pk, bp, bsz);
186 pp += bsz;
187 sz -= bsz;
188 }
189}
190
191/* --- @pkbuf_want@ --- *
192 *
193 * Arguments: @pkbuf *pk@ = pointer to buffer block
194 * @size_t want@ = how many octets wanted for next packet
195 *
196 * Returns: ---
197 *
198 * Use: Sets the desired size for the next packet to be read. If
199 * it's larger than the current buffer, the buffer is extended.
200 */
201
202void pkbuf_want(pkbuf *pk, size_t want)
203{
204 pk->want = want;
205 if (want > pk->sz) {
206 do pk->sz <<= 1; while (want < pk->sz);
207 if (pk->buf) {
208 if (pk->len)
209 pk->buf = x_realloc(pk->a, pk->buf, pk->sz);
210 else {
211 x_free(pk->a, pk->buf);
212 pk->buf = 0;
213 }
214 }
215 }
216}
217
218/* --- @pkbuf_init@ --- *
219 *
220 * Arguments: @pkbuf *pk@ = pointer to buffer block
221 * @void (*func)(octet *b, size_t sz, pkbuf *pk,@
222 * @size_t *keep, void *p)@ =
223 * handler function
224 * @void *p@ = argument pointer for @func@
225 *
226 * Returns: ---
227 *
228 * Use: Initializes a packet buffer block. Any packets are passed to
229 * the provided function for handling.
230 */
231
232void pkbuf_init(pkbuf *pk,
233 void (*func)(octet */*b*/, size_t /*sz*/,
234 pkbuf */*pk*/, size_t */*keep*/, void */*p*/),
235 void *p)
236{
237 pk->func = func;
238 pk->p = p;
239 pk->len = 0;
240 pk->f = PKBUF_ENABLE;
241 pk->buf = 0;
242 pk->sz = 256;
243 pk->want = 1;
244 pk->a = arena_global;
245}
246
247/* --- @pkbuf_destroy@ --- *
248 *
249 * Arguments: @pkbuf *pk@ = pointer to buffer block
250 *
251 * Returns: ---
252 *
253 * Use: Deallocates a line buffer and frees any resources it owned.
254 */
255
256void pkbuf_destroy(pkbuf *pk)
257{
258 if (pk->buf) {
259 x_free(pk->a, pk->buf);
260 pk->buf = 0;
261 }
262}
263
264/*----- That's all, folks -------------------------------------------------*/