1
1
/* assuan-buffer.c - read and send data
2
* Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
2
* Copyright (C) 2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
4
4
* This file is part of Assuan.
16
16
* You should have received a copy of the GNU Lesser General Public
17
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21
22
#include <config.h>
49
53
return 0; /* okay */
52
/* Read an entire line. */
56
/* Read an entire line. Returns 0 on success or -1 and ERRNo on
57
failure. EOF is indictated by setting the integer at address
54
readline (ASSUAN_CONTEXT ctx, char *buf, size_t buflen,
60
readline (assuan_context_t ctx, char *buf, size_t buflen,
55
61
int *r_nread, int *r_eof)
57
63
size_t nleft = buflen;
131
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Error: %s]\n",
138
fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [Error: %s (%d)]\n",
132
139
assuan_get_assuan_log_prefix (),
133
(unsigned int)getpid (), ctx, strerror (errno));
134
return ASSUAN_Read_Error;
140
(unsigned int)getpid (), ctx->inbound.fd,
141
strerror (errno), errno);
142
return _assuan_error (ASSUAN_Read_Error);
138
146
assert (ctx->inbound.eof);
140
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [EOF]\n",
148
fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [EOF]\n",
141
149
assuan_get_assuan_log_prefix (),
142
(unsigned int)getpid (), ctx);
150
(unsigned int)getpid (), ctx->inbound.fd);
151
return _assuan_error (-1);
146
154
ctx->inbound.attic.pending = 0;
170
180
ctx->inbound.linelen = endp - line;
182
monitor_result = (ctx->io_monitor
183
? ctx->io_monitor (ctx, 0,
185
ctx->inbound.linelen)
187
if ( (monitor_result & 2) )
188
ctx->inbound.linelen = 0;
190
if (ctx->log_fp && !(monitor_result & 1))
173
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- ",
192
fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- ",
174
193
assuan_get_assuan_log_prefix (),
175
(unsigned int)getpid (), ctx);
194
(unsigned int)getpid (), ctx->inbound.fd);
176
195
if (ctx->confidential)
177
196
fputs ("[Confidential data not shown]", ctx->log_fp);
189
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Invalid line]\n",
208
fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [Invalid line]\n",
190
209
assuan_get_assuan_log_prefix (),
191
(unsigned int)getpid (), ctx);
210
(unsigned int)getpid (), ctx->inbound.fd);
193
212
ctx->inbound.linelen = 0;
194
return ctx->inbound.eof ? ASSUAN_Line_Not_Terminated
195
: ASSUAN_Line_Too_Long;
213
return _assuan_error (ctx->inbound.eof
214
? ASSUAN_Line_Not_Terminated
215
: ASSUAN_Line_Too_Long);
207
227
See also: assuan_pending_line().
210
assuan_read_line (ASSUAN_CONTEXT ctx, char **line, size_t *linelen)
230
assuan_read_line (assuan_context_t ctx, char **line, size_t *linelen)
212
232
assuan_error_t err;
215
return ASSUAN_Invalid_Value;
235
return _assuan_error (ASSUAN_Invalid_Value);
217
237
err = _assuan_read_line (ctx);
218
238
*line = ctx->inbound.line;
234
254
_assuan_write_line (assuan_context_t ctx, const char *prefix,
235
255
const char *line, size_t len)
257
assuan_error_t rc = 0;
238
258
size_t prefixlen = prefix? strlen (prefix):0;
259
unsigned int monitor_result;
240
261
/* Make sure that the line is short enough. */
241
262
if (len + prefixlen + 2 > ASSUAN_LINELENGTH)
244
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
265
fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> "
245
266
"[supplied line too long -truncated]\n",
246
267
assuan_get_assuan_log_prefix (),
247
(unsigned int)getpid (), ctx);
268
(unsigned int)getpid (), ctx->inbound.fd);
248
269
if (prefixlen > 5)
250
271
if (len > ASSUAN_LINELENGTH - prefixlen - 2)
251
272
len = ASSUAN_LINELENGTH - prefixlen - 2 - 1;
275
monitor_result = (ctx->io_monitor
276
? ctx->io_monitor (ctx, 1, line, len)
254
279
/* Fixme: we should do some kind of line buffering. */
280
if (ctx->log_fp && !(monitor_result & 1))
257
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
282
fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ",
258
283
assuan_get_assuan_log_prefix (),
259
(unsigned int)getpid (), ctx);
284
(unsigned int)getpid (), ctx->inbound.fd);
260
285
if (ctx->confidential)
261
286
fputs ("[Confidential data not shown]", ctx->log_fp);
263
_assuan_log_print_buffer (ctx->log_fp, line, len);
290
_assuan_log_print_buffer (ctx->log_fp, prefix, prefixlen);
291
_assuan_log_print_buffer (ctx->log_fp, line, len);
264
293
putc ('\n', ctx->log_fp);
296
if (prefixlen && !(monitor_result & 2))
269
298
rc = writen (ctx, prefix, prefixlen);
271
rc = ASSUAN_Write_Error;
300
rc = _assuan_error (ASSUAN_Write_Error);
302
if (!rc && !(monitor_result & 2))
275
304
rc = writen (ctx, line, len);
277
rc = ASSUAN_Write_Error;
306
rc = _assuan_error (ASSUAN_Write_Error);
280
309
rc = writen (ctx, "\n", 1);
282
rc = ASSUAN_Write_Error;
311
rc = _assuan_error (ASSUAN_Write_Error);
301
330
len = s? (s-line) : strlen (line);
303
332
if (ctx->log_fp && s)
304
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
305
"[supplied line contained a LF -truncated]\n",
333
fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> "
334
"[supplied line contained a LF - truncated]\n",
306
335
assuan_get_assuan_log_prefix (),
307
(unsigned int)getpid (), ctx);
336
(unsigned int)getpid (), ctx->inbound.fd);
309
338
return _assuan_write_line (ctx, NULL, line, len);
314
343
/* Write out the data in buffer as datalines with line wrapping and
315
percent escaping. This function is used for GNU's custom streams */
344
percent escaping. This function is used for GNU's custom streams. */
317
346
_assuan_cookie_write_data (void *cookie, const char *buffer, size_t orig_size)
319
ASSUAN_CONTEXT ctx = cookie;
348
assuan_context_t ctx = cookie;
320
349
size_t size = orig_size;
390
monitor_result = (ctx->io_monitor
391
? ctx->io_monitor (ctx, 1,
392
ctx->outbound.data.line, linelen)
358
395
if (linelen >= LINELENGTH-2-2)
397
if (ctx->log_fp && !(monitor_result & 1))
362
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
399
fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ",
363
400
assuan_get_assuan_log_prefix (),
364
(unsigned int)getpid (), ctx);
401
(unsigned int)getpid (), ctx->inbound.fd);
366
403
if (ctx->confidential)
367
404
fputs ("[Confidential data not shown]", ctx->log_fp);
376
if (writen (ctx, ctx->outbound.data.line, linelen))
413
if ( !(monitor_result & 2)
414
&& writen (ctx, ctx->outbound.data.line, linelen))
378
ctx->outbound.data.error = ASSUAN_Write_Error;
416
ctx->outbound.data.error = _assuan_error (ASSUAN_Write_Error);
381
419
line = ctx->outbound.data.line;
403
442
line = ctx->outbound.data.line;
404
443
linelen = ctx->outbound.data.linelen;
446
monitor_result = (ctx->io_monitor
447
? ctx->io_monitor (ctx, 1,
448
ctx->outbound.data.line, linelen)
453
if (ctx->log_fp && !(monitor_result & 1))
410
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
455
fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ",
411
456
assuan_get_assuan_log_prefix (),
412
(unsigned int)getpid (), ctx);
457
(unsigned int)getpid (), ctx->inbound.fd);
413
458
if (ctx->confidential)
414
459
fputs ("[Confidential data not shown]", ctx->log_fp);
422
if (writen (ctx, ctx->outbound.data.line, linelen))
467
if ( !(monitor_result & 2)
468
&& writen (ctx, ctx->outbound.data.line, linelen))
424
ctx->outbound.data.error = ASSUAN_Write_Error;
470
ctx->outbound.data.error = _assuan_error (ASSUAN_Write_Error);
427
473
ctx->outbound.data.linelen = 0;
452
assuan_send_data (ASSUAN_CONTEXT ctx, const void *buffer, size_t length)
498
assuan_send_data (assuan_context_t ctx, const void *buffer, size_t length)
455
return ASSUAN_Invalid_Value;
501
return _assuan_error (ASSUAN_Invalid_Value);
456
502
if (!buffer && length)
457
return ASSUAN_Invalid_Value;
503
return _assuan_error (ASSUAN_Invalid_Value);
460
506
{ /* flush what we have */
478
assuan_sendfd (ASSUAN_CONTEXT ctx, int fd)
524
assuan_sendfd (assuan_context_t ctx, int fd)
526
/* It is explicitly allowed to use (NULL, -1) as a runtime test to
527
check whether descriptor passing is available. */
528
if (!ctx && fd == -1)
529
#ifdef USE_DESCRIPTOR_PASSING
532
return _assuan_error (ASSUAN_Not_Implemented);
480
535
if (! ctx->io->sendfd)
481
536
return set_error (ctx, Not_Implemented,
482
537
"server does not support sending and receiving "