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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*++
/* NAME
/* netstring 3
/* SUMMARY
/* netstring stream I/O support
/* SYNOPSIS
/* #include <netstring.h>
/*
/* void netstring_setup(stream, timeout)
/* VSTREAM *stream;
/* int timeout;
/*
/* void netstring_except(stream, exception)
/* VSTREAM *stream;
/* int exception;
/*
/* VSTRING *netstring_get(stream, buf, limit)
/* VSTREAM *stream;
/* VSTRING *buf;
/* int limit;
/*
/* void netstring_put(stream, data, len)
/* VSTREAM *stream;
/* const char *data;
/* int len;
/*
/* void netstring_put_multi(stream, data, len, data, len, ..., 0)
/* VSTREAM *stream;
/* const char *data;
/* int len;
/*
/* void NETSTRING_PUT_BUF(stream, buf)
/* VSTREAM *stream;
/* VSTRING *buf;
/*
/* void netstring_fflush(stream)
/* VSTREAM *stream;
/*
/* VSTRING *netstring_memcpy(buf, data, len)
/* VSTRING *buf;
/* const char *data;
/* int len;
/*
/* VSTRING *netstring_memcat(buf, data, len)
/* VSTRING *buf;
/* const char *src;
/* int len;
/* AUXILIARY ROUTINES
/* int netstring_get_length(stream)
/* VSTREAM *stream;
/*
/* VSTRING *netstring_get_data(stream, buf, len)
/* VSTREAM *stream;
/* VSTRING *buf;
/* int len;
/*
/* void netstring_get_terminator(stream)
/* VSTREAM *stream;
/* DESCRIPTION
/* This module reads and writes netstrings with error detection:
/* timeouts, unexpected end-of-file, or format errors. Netstring
/* is a data format designed by Daniel Bernstein.
/*
/* netstring_setup() arranges for a time limit on the netstring
/* read and write operations described below.
/* This routine alters the behavior of streams as follows:
/* .IP \(bu
/* The read/write timeout is set to the specified value.
/* .IP \(bu
/* The stream is configured to enable exception handling.
/* .PP
/* netstring_except() raises the specified exception on the
/* named stream. See the DIAGNOSTICS section below.
/*
/* netstring_get() reads a netstring from the specified stream
/* and extracts its content. The limit specifies a maximal size.
/* Specify zero to disable the size limit. The result is not null
/* terminated. The result value is the buf argument.
/*
/* netstring_put() encapsulates the specified string as a netstring
/* and sends the result to the specified stream.
/* The stream output buffer is not flushed.
/*
/* netstring_put_multi() encapsulates the content of multiple strings
/* as one netstring and sends the result to the specified stream. The
/* argument list must be terminated with a null data pointer.
/* The stream output buffer is not flushed.
/*
/* NETSTRING_PUT_BUF() is a macro that provides a VSTRING-based
/* wrapper for the netstring_put() routine.
/*
/* netstring_fflush() flushes the output buffer of the specified
/* stream and handles any errors.
/*
/* netstring_memcpy() encapsulates the specified data as a netstring
/* and copies the result over the specified buffer. The result
/* value is the buffer.
/*
/* netstring_memcat() encapsulates the specified data as a netstring
/* and appends the result to the specified buffer. The result
/* value is the buffer.
/*
/* The following routines provide low-level access to a netstring
/* stream.
/*
/* netstring_get_length() reads a length field from the specified
/* stream, and absorbs the netstring length field terminator.
/*
/* netstring_get_data() reads the specified number of bytes from the
/* specified stream into the specified buffer, and absorbs the
/* netstring terminator. The result value is the buf argument.
/*
/* netstring_get_terminator() reads the netstring terminator from
/* the specified stream.
/* DIAGNOSTICS
/* .fi
/* .ad
/* In case of error, a vstream_longjmp() call is performed to the
/* context specified with vstream_setjmp().
/* Error codes passed along with vstream_longjmp() are:
/* .IP NETSTRING_ERR_EOF
/* An I/O error happened, or the peer has disconnected unexpectedly.
/* .IP NETSTRING_ERR_TIME
/* The time limit specified to netstring_setup() was exceeded.
/* .IP NETSTRING_ERR_FORMAT
/* The input contains an unexpected character value.
/* .IP NETSTRING_ERR_SIZE
/* The input is larger than acceptable.
/* BUGS
/* The timeout deadline affects all I/O on the named stream, not
/* just the I/O done on behalf of this module.
/*
/* The timeout deadline overwrites any previously set up state on
/* the named stream.
/*
/* netstrings are not null terminated, which makes printing them
/* a bit awkward.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* SEE ALSO
/* http://cr.yp.to/proto/netstrings.txt, netstring definition
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System library. */
#include <sys_defs.h>
#include <stdarg.h>
#include <ctype.h>
/* Utility library. */
#include <msg.h>
#include <vstream.h>
#include <vstring.h>
#include <netstring.h>
/* Application-specific. */
#define STR(x) vstring_str(x)
#define LEN(x) VSTRING_LEN(x)
/* netstring_setup - initialize netstring stream */
void netstring_setup(VSTREAM *stream, int timeout)
{
vstream_control(stream,
VSTREAM_CTL_TIMEOUT, timeout,
VSTREAM_CTL_EXCEPT,
VSTREAM_CTL_END);
}
/* netstring_except - process netstring stream exception */
void netstring_except(VSTREAM *stream, int exception)
{
vstream_longjmp(stream, exception);
}
/* netstring_get_length - read netstring length + terminator */
int netstring_get_length(VSTREAM *stream)
{
char *myname = "netstring_get_length";
int len = 0;
int ch;
for (;;) {
switch (ch = VSTREAM_GETC(stream)) {
case VSTREAM_EOF:
netstring_except(stream, vstream_ftimeout(stream) ?
NETSTRING_ERR_TIME : NETSTRING_ERR_EOF);
case ':':
if (msg_verbose > 1)
msg_info("%s: read netstring length %d", myname, len);
return (len);
default:
if (!ISDIGIT(ch))
netstring_except(stream, NETSTRING_ERR_FORMAT);
len = len * 10 + ch - '0';
break;
}
}
}
/* netstring_get_data - read netstring payload + terminator */
VSTRING *netstring_get_data(VSTREAM *stream, VSTRING *buf, int len)
{
char *myname = "netstring_get_data";
/*
* Allocate buffer space.
*/
VSTRING_RESET(buf);
VSTRING_SPACE(buf, len);
/*
* Read the payload and absorb the terminator.
*/
if (vstream_fread(stream, STR(buf), len) != len)
netstring_except(stream, vstream_ftimeout(stream) ?
NETSTRING_ERR_TIME : NETSTRING_ERR_EOF);
if (msg_verbose > 1)
msg_info("%s: read netstring data %.*s",
myname, len < 30 ? len : 30, STR(buf));
netstring_get_terminator(stream);
/*
* Position the buffer.
*/
VSTRING_AT_OFFSET(buf, len);
return (buf);
}
/* netstring_get_terminator - absorb netstring terminator */
void netstring_get_terminator(VSTREAM *stream)
{
if (VSTREAM_GETC(stream) != ',')
netstring_except(stream, NETSTRING_ERR_FORMAT);
}
/* netstring_get - read string from netstring stream */
VSTRING *netstring_get(VSTREAM *stream, VSTRING *buf, int limit)
{
int len;
len = netstring_get_length(stream);
if (limit && len > limit)
netstring_except(stream, NETSTRING_ERR_SIZE);
netstring_get_data(stream, buf, len);
return (buf);
}
/* netstring_put - send string as netstring */
void netstring_put(VSTREAM *stream, const char *data, int len)
{
char *myname = "netstring_put";
if (msg_verbose > 1)
msg_info("%s: write netstring len %d data %.*s",
myname, len, len < 30 ? len : 30, data);
vstream_fprintf(stream, "%d:", len);
vstream_fwrite(stream, data, len);
VSTREAM_PUTC(',', stream);
}
/* netstring_put_multi - send multiple strings as one netstring */
void netstring_put_multi(VSTREAM *stream,...)
{
char *myname = "netstring_put_multi";
int total;
char *data;
int data_len;
va_list ap;
/*
* Figure out the total result size.
*/
va_start(ap, stream);
for (total = 0; (data = va_arg(ap, char *)) != 0; total += data_len)
if ((data_len = va_arg(ap, int)) < 0)
msg_panic("netstring_put_multi: bad data length %d", data_len);
va_end(ap);
/*
* Debugging support.
*/
if (msg_verbose > 1) {
va_start(ap, stream);
data = va_arg(ap, char *);
data_len = va_arg(ap, int);
msg_info("%s: write netstring len %d data %.*s",
myname, total, data_len < 30 ? data_len : 30, data);
va_end(ap);
}
/*
* Send the length, content and terminator.
*/
vstream_fprintf(stream, "%d:", total);
va_start(ap, stream);
while ((data = va_arg(ap, char *)) != 0) {
data_len = va_arg(ap, int);
if (data_len > 0)
if (vstream_fwrite(stream, data, data_len) != data_len)
netstring_except(stream, vstream_ftimeout(stream) ?
NETSTRING_ERR_TIME : NETSTRING_ERR_EOF);
va_end(ap);
}
vstream_fwrite(stream, ",", 1);
}
/* netstring_fflush - flush netstring stream */
void netstring_fflush(VSTREAM *stream)
{
if (vstream_fflush(stream) == VSTREAM_EOF)
netstring_except(stream, vstream_ftimeout(stream) ?
NETSTRING_ERR_TIME : NETSTRING_ERR_EOF);
}
/* netstring_memcpy - copy data as in-memory netstring */
VSTRING *netstring_memcpy(VSTRING *buf, const char *src, int len)
{
vstring_sprintf(buf, "%d:", len);
vstring_memcat(buf, src, len);
VSTRING_ADDCH(buf, ',');
return (buf);
}
/* netstring_memcat - append data as in-memory netstring */
VSTRING *netstring_memcat(VSTRING *buf, const char *src, int len)
{
vstring_sprintf_append(buf, "%d:", len);
vstring_memcat(buf, src, len);
VSTRING_ADDCH(buf, ',');
return (buf);
}
|