blob: c0c80b6e135c3341302a7ef32f63243ca9c7d0ec [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
4 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <common/compat.h>
23#include <common/config.h>
24#include <common/debug.h>
25#include <common/standard.h>
26#include <common/ticks.h>
27#include <common/time.h>
28
29#include <proto/buffers.h>
30#include <proto/client.h>
31#include <proto/fd.h>
32#include <proto/stream_sock.h>
33#include <proto/task.h>
34
35/*
36 * This function only has to be called once after a wakeup event in case of
37 * suspected timeout. It controls the stream interface timeouts and sets
38 * si->flags accordingly. It does NOT close anything, as this timeout may
39 * be used for any purpose. It returns 1 if the timeout fired, otherwise
40 * zero.
41 */
42int stream_int_check_timeouts(struct stream_interface *si)
43{
44 if (tick_is_expired(si->exp, now_ms)) {
45 si->flags |= SI_FL_EXP;
46 return 1;
47 }
48 return 0;
49}
50
Willy Tarreaufe3718a2008-11-30 18:14:12 +010051/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010052void stream_int_report_error(struct stream_interface *si)
53{
54 if (!si->err_type)
55 si->err_type = SI_ET_DATA_ERR;
56
Willy Tarreaucff64112008-11-03 06:26:53 +010057 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010058 si->ib->flags |= BF_READ_ERROR;
59}
60
61/*
Willy Tarreau6f0aa472009-03-08 20:33:29 +010062 * Erase any content from input and output buffers, and return a message into
63 * the output buffer. The message is provided as a "chunk". If it is null,
64 * then an empty message is used.
Willy Tarreau81acfab2008-11-30 19:22:53 +010065 */
66void stream_int_return(struct stream_interface *si, const struct chunk *msg)
67{
Willy Tarreau6f0aa472009-03-08 20:33:29 +010068 buffer_erase(si->ib);
69 buffer_erase(si->ob);
Willy Tarreau81acfab2008-11-30 19:22:53 +010070 if (msg && msg->len)
71 buffer_write(si->ob, msg->str, msg->len);
72}
73
74/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010075 * Returns a message to the client ; the connection is shut down for read,
76 * and the request is cleared so that no server connection can be initiated.
77 * The buffer is marked for read shutdown on the other side to protect the
78 * message, and the buffer write is enabled. The message is contained in a
79 * "chunk". If it is null, then an empty message is used. The reply buffer
80 * doesn't need to be empty before this. The goal of this function is to
81 * return error messages to a client.
82 */
83void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
84{
85 buffer_abort(si->ib);
Willy Tarreau6f0aa472009-03-08 20:33:29 +010086 buffer_erase(si->ob);
Willy Tarreaudded32d2008-11-30 19:48:07 +010087 buffer_shutr_now(si->ob);
88 if (msg && msg->len)
89 buffer_write(si->ob, msg->str, msg->len);
90
91 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
92 buffer_write_ena(si->ob);
93}
94
95/*
Willy Tarreaucff64112008-11-03 06:26:53 +010096 * Local variables:
97 * c-indent-level: 8
98 * c-basic-offset: 8
99 * End:
100 */