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