436fddaa |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: pkbuf.h,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.h,v $ |
33 | * Revision 1.1 2000/06/17 10:39:19 mdw |
34 | * Experimental new support for packet buffering. |
35 | * |
36 | */ |
37 | |
38 | #ifndef MLIB_PKBUF_H |
39 | #define MLIB_PKBUF_H |
40 | |
41 | #ifdef __cplusplus |
42 | extern "C" { |
43 | #endif |
44 | |
45 | /*----- Header files ------------------------------------------------------*/ |
46 | |
47 | #include <stddef.h> |
48 | |
49 | #ifndef MLIB_ARENA_H |
50 | # include "arena.h" |
51 | #endif |
52 | |
53 | #ifndef MLIB_BITS_H |
54 | # include "bits.h" |
55 | #endif |
56 | |
57 | /*----- Data structures ---------------------------------------------------*/ |
58 | |
59 | typedef struct pkbuf { |
60 | size_t sz; /* Size of current buffer */ |
61 | size_t len; /* Length of data in the buffer */ |
62 | size_t want; /* Want this many bytes for packet */ |
63 | unsigned f; /* Various state flags */ |
64 | void (*func)(octet */*b*/, size_t /*sz*/, |
65 | struct pkbuf */*pk*/, size_t */*keep*/, |
66 | void */*p*/); /* Handler function */ |
67 | void *p; /* Argument for handler */ |
68 | arena *a; /* Memory allocation arena */ |
69 | octet *buf; /* Actual buffer space */ |
70 | } pkbuf; |
71 | |
72 | enum { |
73 | PKBUF_ENABLE = 1 /* Buffer is currently enabled */ |
74 | }; |
75 | |
76 | /*----- Functions provided ------------------------------------------------*/ |
77 | |
78 | /* --- @pkbuf_flush@ --- * |
79 | * |
80 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
81 | * @octet *p@ = pointer to where to start searching |
82 | * @size_t len@ = length of new material added |
83 | * |
84 | * Returns: --- |
85 | * |
86 | * Use: Flushes any complete packets in a packet buffer. New |
87 | * material is assumed to have been added starting at @p@. If |
88 | * @p@ is null, then the scan starts at the beginning of the |
89 | * buffer, and the size of data already in the buffer is used in |
90 | * place of @len@. |
91 | * |
92 | * It is assumed that the buffer is initially enabled. You |
93 | * shouldn't be contributing data to a disabled buffer anyway. |
94 | * However, the buffer handler may at some point disable itself, |
95 | * and @pkbuf_flush@ can cope with this eventuality. Any |
96 | * pending data is left at the start of the buffer and can be |
97 | * flushed out by calling @pkbuf_flush(b, 0, 0)@ if the buffer |
98 | * is ever re-enabled. |
99 | */ |
100 | |
101 | extern void pkbuf_flush(pkbuf */*pk*/, octet */*p*/, size_t /*len*/); |
102 | |
103 | /* --- @pkbuf_close@ --- * |
104 | * |
105 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
106 | * |
107 | * Returns: --- |
108 | * |
109 | * Use: Informs the client that no more data is likely to arrive. If |
110 | * there is a partial packet in the buffer, it is discarded. |
111 | */ |
112 | |
113 | extern void pkbuf_close(pkbuf */*pk*/); |
114 | |
115 | /* --- @pkbuf_free@ --- * |
116 | * |
117 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
118 | * @octet **p@ = output pointer to free space |
119 | * |
120 | * Returns: Free buffer size. |
121 | * |
122 | * Use: Returns the free portion of a packet buffer. Data can then |
123 | * be written to this portion, and split out into packets by |
124 | * calling @pkbuf_flush@. |
125 | */ |
126 | |
127 | extern size_t pkbuf_free(pkbuf */*pk*/, octet **/*p*/); |
128 | |
129 | /* --- @pkbuf_snarf@ --- * |
130 | * |
131 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
132 | * @const void *p@ = pointer to input data buffer |
133 | * @size_t sz@ = size of data in input buffer |
134 | * |
135 | * Returns: --- |
136 | * |
137 | * Use: Snarfs the data from the input buffer and spits it out as |
138 | * packets. This interface ignores the complexities of dealing |
139 | * with disablement: you should be using @pkbuf_free@ to |
140 | * contribute data if you want to cope with that. |
141 | */ |
142 | |
143 | extern void pkbuf_snarf(pkbuf */*pk*/, const void */*p*/, size_t /*sz*/); |
144 | |
145 | /* --- @pkbuf_want@ --- * |
146 | * |
147 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
148 | * @size_t want@ = how many octets wanted for next packet |
149 | * |
150 | * Returns: --- |
151 | * |
152 | * Use: Sets the desired size for the next packet to be read. If |
153 | * it's larger than the current buffer, the buffer is extended. |
154 | */ |
155 | |
156 | extern void pkbuf_want(pkbuf */*pk*/, size_t /*want*/); |
157 | |
158 | /* --- @pkbuf_init@ --- * |
159 | * |
160 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
161 | * @void (*func)(octet *b, size_t sz, pkbuf *pk,@ |
162 | * @size_t *keep, void *p)@ = |
163 | * handler function |
164 | * @void *p@ = argument pointer for @func@ |
165 | * |
166 | * Returns: --- |
167 | * |
168 | * Use: Initializes a packet buffer block. Any packets are passed to |
169 | * the provided function for handling. |
170 | */ |
171 | |
172 | extern void pkbuf_init(pkbuf */*pk*/, |
173 | void (*/*func*/)(octet */*b*/, size_t /*sz*/, |
174 | pkbuf */*pk*/, size_t */*keep*/, |
175 | void */*p*/), |
176 | void */*p*/); |
177 | |
178 | /* --- @pkbuf_destroy@ --- * |
179 | * |
180 | * Arguments: @pkbuf *pk@ = pointer to buffer block |
181 | * |
182 | * Returns: --- |
183 | * |
184 | * Use: Deallocates a packet buffer and frees any resources it owned. |
185 | */ |
186 | |
187 | extern void pkbuf_destroy(pkbuf */*pk*/); |
188 | |
189 | /*----- That's all, folks -------------------------------------------------*/ |
190 | |
191 | #ifdef __cplusplus |
192 | } |
193 | #endif |
194 | |
195 | #endif |