1 /*
2  * control2socket.c
3  * (C)1998-2011 by Marc Huber <Marc.Huber@web.de>
4  *
5  * $Id: control2socket.c,v 1.11 2015/03/14 06:11:24 marc Exp marc $
6  *
7  */
8 
9 #include "headers.h"
10 
11 static const char rcsid[] __attribute__ ((used)) = "$Id: control2socket.c,v 1.11 2015/03/14 06:11:24 marc Exp marc $";
12 
control2socket(struct context * ctx,int cur)13 void control2socket(struct context *ctx, int cur)
14 {
15     struct buffer *b = ctx->cbufo;
16     ssize_t i;
17 
18     Debug((DEBUG_BUFFER, "+ %s (%d)\n", __func__, ctx->cfn));
19 
20     if (!b) {
21 	logmsg("FATAL: control2socket: Buffer is NULL!");
22 	cleanup_control(ctx, ctx->cfn);
23     }
24 #ifdef WITH_SSL
25     else if (ctx->ssl_c && !(i = io_SSL_write(ctx->ssl_c, b->buf + b->offset, b->length - b->offset, ctx->io, ctx->cfn, (void *) control2socket)))
26 	cleanup_control(ctx, ctx->cfn);
27 #endif
28     else if (
29 #ifdef WITH_SSL
30 		!ctx->ssl_c &&
31 #endif
32 		!(i = write(ctx->cfn, b->buf + b->offset, b->length - b->offset)))
33 	cleanup_control(ctx, ctx->cfn);
34     else if (i > 0) {
35 	b->offset += i;
36 	ctx->traffic_total += i;
37 
38 	if (b->offset == b->length)
39 	    ctx->cbufo = buffer_free(ctx->cbufo);
40 
41 	if (!ctx->cbufo) {
42 	    io_clr_o(ctx->io, ctx->cfn);
43 	    if (ctx->dfn < 0 && io_is_invalid_i(ctx->io, cur))
44 		cleanup_control(ctx, ctx->cfn);
45 	}
46     } else if (errno != EAGAIN)
47 	cleanup(ctx, ctx->cfn);
48 
49     DebugOut(DEBUG_BUFFER);
50 }
51