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