1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
.TH VENTI-PACKET 3
.SH NAME
Packet,
packetalloc,
packetappend,
packetasize,
packetcmp,
packetconcat,
packetconsume,
packetcopy,
packetdup,
packetforeign,
packetfragments,
packetfree,
packetheader,
packetpeek,
packetprefix,
packetsha1,
packetsize,
packetsplit,
packetstats,
packettrailer,
packettrim \- zero-copy network buffers
.SH SYNOPSIS
.ft L
#include <u.h>
.br
#include <libc.h>
.br
#include <venti.h>
.ta +\w'\fLPacket* 'u +\w'\fLxxxx'u
.PP
.B
.PP
.B
Packet* packetalloc(void);
.PP
.B
void packetappend(Packet *p, uchar *buf, int n)
.PP
.B
uint packetasize(Packet *p)
.PP
.B
int packetcmp(Packet *p, Packet *q)
.PP
.B
void packetconcat(Packet *p, Packet *q)
.PP
.B
int packetconsume(Packet *p, uchar *buf, int n)
.PP
.B
int packetcopy(Packet *p, uchar *buf, int offset, int n)
.PP
.B
Packet* packetdup(Packet *p, int offset, int n)
.PP
.B
Packet* packetforeign(uchar *buf, int n,
.br
.B
void (*free)(void *a), void *a)
.PP
.B
int packetfragments(Packet *p, IOchunk *io, int nio,
.br
.B
int offset)
.PP
.B
void packetfree(Packet *p)
.PP
.B
uchar* packetheader(Packet *p, int n)
.PP
.B
uchar* packetpeek(Packet *p, uchar *buf, int offset, int n)
.PP
.B
void packetprefix(Packet *p, uchar *buf, int n)
.PP
.B
void packetsha1(Packet *p, uchar sha1[20])
.PP
.B
uint packetsize(Packet *p)
.PP
.B
Packet* packetsplit(Packet *p, int n)
.PP
.B
void packetstats(void)
.PP
.B
uchar* packettrailer(Packet *p, int n)
.PP
.B
int packettrim(Packet *p, int offset, int n)
.SH DESCRIPTION
A
.B Packet
is a chain of blocks of data.
Each block, called a fragment,
is contiguous in memory, but the entire packet
may not be.
This representation helps avoid unnecessary memory copies.
.PP
.I Packetalloc
allocates an empty packet.
.PP
.I Packetappend
appends the
.I n
bytes at
.I buf
to the end of
.IR p .
.PP
.I Packetasize
returns the number of data bytes allocated to
.IR p .
This may be larger than the number of bytes stored
in
.IR p
because fragments may not be filled completely.
.PP
.I Packetcmp
compares the data sections of two packets as
.I memcmp
(see
.IR memory (3))
would.
.PP
.I Packetconcat
removes all data from
.IR q ,
appending it to
.IR p .
.PP
.I Packetconsume
removes
.I n
bytes from the beginning of
.IR p ,
storing them into
.IR buf .
.PP
.I Packetcopy
copies
.I n
bytes at
.I offset
in
.I p
to
.IR buf .
.PP
.I Packetdup
creates a new packet initialized with
.I n
bytes from
.I offset
in
.IR p .
.PP
.I Packetforeign
allocates a packet containing `foreign' data: the
.I n
bytes pointed to by
.IR buf .
Once the bytes are no longer needed, they are freed by calling
.IB free ( a )\fR.
.PP
.I Packetfragments
initializes up to
.I nio
of the
.I io
structures with pointers to the data in
.IR p ,
starting at
.IR offset .
It returns the total number of bytes represented
by the returned structures.
.I Packetfragments
initializes any unused
.I io
structures with nil pointer and zero length.
.PP
.I Packetfree
frees the packet
.IR p .
.PP
.I Packetheader
returns a pointer to the first
.I n
bytes of
.IR p ,
making them contiguous in memory
if necessary.
.PP
.I Packetpeek
returns a pointer to the
.I n
bytes at
.I offset
in
.IR p .
If the requested bytes are already stored contiguously in memory,
the returned pointer points at the internal data storage for
.IR p .
Otherwise, the bytes are copied into
.IR buf ,
and
.I packetpeek
returns
.IR buf .
.PP
.I Packetprefix
inserts a copy of the
.I n
bytes at
.I buf
at the beginning of
.IR p .
.PP
.I Packetsha1
computes the SHA1 hash of the data contained in
.IR p .
.PP
.I Packetsize
returns the length, in bytes, of the data contained in
.IR p .
.PP
.I Packetsplit
returns a new packet initialized with
.I n
bytes removed from the beginning of
.IR p .
.PP
.I Packetstats
prints run-time statistics to standard output.
.PP
.I Packettrailer
returns a pointer to the last
.I n
bytes of
.IR p ,
making them contiguous in memory
if necessary.
.PP
.I Packettrim
removes
.I n
bytes at offset
.I offset
from packet
.IR p .
.SH SOURCE
.B \*9/src/libventi
.SH SEE ALSO
.IR venti (3)
.SH DIAGNOSTICS
These functions return errors only when passed
invalid inputs,
.IR e.g. ,
requests for data at negative offsets or beyond the end of a packet.
.PP
Functions returning pointers return nil on error;
functions returning integers return \-1 on error.
Most functions returning integers return 0 on success.
The exceptions are
.I packetfragments
and
.IR packetcmp ,
whose return values are described above.
.PP
When these functions run out of memory, they
print error messages and call
.IR sysfatal .
|