3 .\" Manual for packet splitting
5 .\" (c) 1999--2003, 2005, 2007, 2009, 2023, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH pkbuf 3mLib "16 July 2000" "Straylight/Edgeware" "mLib utilities library"
40 .\"--------------------------------------------------------------------------
42 pkbuf \- split packets out of asynchronously received blocks
44 .\"--------------------------------------------------------------------------
49 .B "#include <mLib/pkbuf.h>"
52 .B " PKBUF_ENABLE = ..."
60 .ta \w'\fBtypedef void pkbuf_func('u
61 .B "typedef void pkbuf_func(octet *" b ", size_t " sz ", pkbuf *" p ,
62 .BI " size_t *" keep ", void *" p );
64 .BI "void pkbuf_flush(pkbuf *" pk ", octet *" p ", size_t " len );
65 .BI "void pkbuf_close(pkbuf *" pk );
66 .BI "size_t pkbuf_free(pkbuf *" pk ", octet **" p );
67 .BI "void pkbuf_snarf(pkbuf *" pk ", const void *" p ", size_t " sz );
68 .BI "void pkbuf_want(pkbuf *" pk ", size_t " want );
69 .BI "void pkbuf_init(pkbuf *" pk ", pkbuf_func *" func ", void *" p );
70 .BI "void pkbuf_destroy(pkbuf *" pk );
73 .\"--------------------------------------------------------------------------
80 Given unpredictably-sized chunks of data, the packet buffer extracts
81 completed packets of data, with sizes ascertained by a handler
84 The state of a packet buffer is stored in an object of type
86 This is a structure which must be allocated by the caller. The
87 structure should normally be considered opaque (see the section on
89 for an exception to this).
91 .SS "Initialization and finalization"
94 initializes a packet buffer ready for use. It is given three arguments:
97 A pointer to the block of memory to use for the packet buffer. The packet
98 buffer will allocate memory to store incoming data automatically: this
99 structure just contains bookkeeping information.
101 .BI "pkbuf_func *" func
104 function to which the packet buffer should pass packets of data when
105 they're received. See
106 .B "Packet breaking and the handler function"
110 A pointer argument to be passed to the function when a packet arrives.
112 By default, the buffer is
113 allocated from the current arena,
114 .BR arena_global (3);
115 this may be changed by altering the buffer's
117 member to refer to a different arena at any time when the buffer is
120 A packet buffer must be destroyed after use by calling
122 passing it the address of the buffer block.
124 .SS "Inserting data into the buffer"
125 There are two interfaces for inserting data into the buffer. One's much
126 simpler than the other, although it's less expressive.
128 The simple interface is
130 This function is given three arguments: a pointer
132 to a packet buffer structure; a pointer
134 to a chunk of data to read; and the size
136 of the chunk of data. The data is pushed through the packet buffer and
137 any complete packets are passed on to the packet handler.
139 The complex interface is the pair of functions
146 function returns the address and size of a free portion of the packet
147 buffer's memory into which data may be written. The function is passed
150 of the packet buffer. Its result is the size of the free area, and it
151 writes the base address of this free space to the location pointed to by
154 The caller's data must be written to ascending memory locations starting
157 and no data may be written beyond the end of the free space. However,
158 it isn't necessary to completely fill the buffer.
160 Once the free area has had some data written to it,
162 is called to examine the new data and break it into packets. This is
163 given three arguments:
166 The address of the packet buffer.
169 The address at which the new data has been written. This must be the
170 base address returned from
174 The number of bytes which have been written to the buffer.
178 function breaks the new data into packets as described below, and passes
179 each one in turn to the packet-handler function.
183 function is trivially implemented in terms of the more complex
184 .BR pkbuf_free / pkbuf_flush
187 .SS "Packet breaking and the handler function"
190 is used to inform the packet buffer of the expected length of the next
191 packet. It is given the pointer
193 to the packet buffer and a size
197 When enough data has arrived, the packet-handler function is called. It
201 A pointer to the packet data in the buffer, or zero to signify
205 The size of the packet, as previously configured via
209 A pointer to the packet buffer.
212 A location in which to store the number of bytes of the current packet
213 to be retained. The bytes kept are from the end of the current packet:
214 if you want to keep bytes from the beginning of the packet, you should
215 move them into the right place.
218 The pointer which was set up in the call to
222 .SS "Flushing the remaining data"
223 When the client program knows that there's no more data arriving (for
224 example, an end-of-file condition exists on its data source) it should
227 This will call the handler one final time with a null pointer to inform
228 it of the end-of-file.
231 The packet buffer is intended to be used in higher-level program
232 objects, such as the packet selector described in
234 Unfortunately, a concept from this high level needs to exist at the
235 packet buffer level, which complicates the description somewhat. The
236 idea is that, when a packet-handler attached to some higher-level object
237 decides that it's read enough, it can
239 the object so that it doesn't see any more data.
243 call can emit more than one packet, it must be aware that the packet
244 handler isn't interested in any more packet. However, this fact must
245 also be signalled to the higher-level object so that it can detach
246 itself from its data source.
248 Rather than invent some complex interface for this, the packet buffer
249 exports one of its structure members, a flags words called
251 A higher-level object wishing to disable the packet buffer simply clears
256 Disabling a buffer causes an immediate return from
258 However, it is not permitted for the functions
262 to be called on a disabled buffer. (This condition isn't checked for;
263 it'll just do the wrong thing.) Furthermore, the
265 function does not handle disablement at all, because it would complicate
266 the interface so much that it wouldn't have any advantage over the more
268 .BR pkbuf_free / pkbuf_flush .
270 .\"--------------------------------------------------------------------------
277 .\"--------------------------------------------------------------------------
280 Mark Wooding, <mdw@distorted.org.uk>
282 .\"----- That's all, folks --------------------------------------------------