blob: 59eca1bd3d341a8ac595553163cf5d4f820daaa3 [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
Willy Tarreauf873d752012-05-11 17:47:17 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaucff64112008-11-03 06:26:53 +01005 *
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>
Willy Tarreaucff64112008-11-03 06:26:53 +010030#include <proto/fd.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020031#include <proto/frontend.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020032#include <proto/sock_raw.h>
Willy Tarreau269358d2009-09-20 20:14:49 +020033#include <proto/stream_interface.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010034#include <proto/task.h>
35
Willy Tarreauf873d752012-05-11 17:47:17 +020036/* socket functions used when running a stream interface as a task */
37static void stream_int_update(struct stream_interface *si);
38static void stream_int_update_embedded(struct stream_interface *si);
39static void stream_int_shutr(struct stream_interface *si);
40static void stream_int_shutw(struct stream_interface *si);
41static void stream_int_chk_rcv(struct stream_interface *si);
42static void stream_int_chk_snd(struct stream_interface *si);
43
Willy Tarreau5c979a92012-05-07 17:15:39 +020044/* socket operations for embedded tasks */
45struct sock_ops stream_int_embedded = {
46 .update = stream_int_update_embedded,
47 .shutr = stream_int_shutr,
48 .shutw = stream_int_shutw,
49 .chk_rcv = stream_int_chk_rcv,
50 .chk_snd = stream_int_chk_snd,
51 .read = NULL,
52 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020053 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020054};
55
56/* socket operations for external tasks */
57struct sock_ops stream_int_task = {
58 .update = stream_int_update,
59 .shutr = stream_int_shutr,
60 .shutw = stream_int_shutw,
61 .chk_rcv = stream_int_chk_rcv,
62 .chk_snd = stream_int_chk_snd,
63 .read = NULL,
64 .write = NULL,
Willy Tarreau24208272012-05-21 17:28:50 +020065 .close = NULL,
Willy Tarreau5c979a92012-05-07 17:15:39 +020066};
67
Willy Tarreaucff64112008-11-03 06:26:53 +010068/*
69 * This function only has to be called once after a wakeup event in case of
70 * suspected timeout. It controls the stream interface timeouts and sets
71 * si->flags accordingly. It does NOT close anything, as this timeout may
72 * be used for any purpose. It returns 1 if the timeout fired, otherwise
73 * zero.
74 */
75int stream_int_check_timeouts(struct stream_interface *si)
76{
77 if (tick_is_expired(si->exp, now_ms)) {
78 si->flags |= SI_FL_EXP;
79 return 1;
80 }
81 return 0;
82}
83
Willy Tarreaufe3718a2008-11-30 18:14:12 +010084/* to be called only when in SI_ST_DIS with SI_FL_ERR */
Willy Tarreaucff64112008-11-03 06:26:53 +010085void stream_int_report_error(struct stream_interface *si)
86{
87 if (!si->err_type)
88 si->err_type = SI_ET_DATA_ERR;
89
Willy Tarreaucff64112008-11-03 06:26:53 +010090 si->ob->flags |= BF_WRITE_ERROR;
Willy Tarreaucff64112008-11-03 06:26:53 +010091 si->ib->flags |= BF_READ_ERROR;
92}
93
94/*
Willy Tarreaudded32d2008-11-30 19:48:07 +010095 * Returns a message to the client ; the connection is shut down for read,
96 * and the request is cleared so that no server connection can be initiated.
97 * The buffer is marked for read shutdown on the other side to protect the
98 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +010099 * "chunk". If it is null, then an empty message is used. The reply buffer does
100 * not need to be empty before this, and its contents will not be overwritten.
101 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100102 */
103void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg)
104{
Willy Tarreau148d0992010-01-10 10:21:21 +0100105 buffer_auto_read(si->ib);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100106 buffer_abort(si->ib);
Willy Tarreau148d0992010-01-10 10:21:21 +0100107 buffer_auto_close(si->ib);
108 buffer_erase(si->ib);
Willy Tarreau798e1282010-12-12 13:06:00 +0100109
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200110 bi_erase(si->ob);
Willy Tarreau148d0992010-01-10 10:21:21 +0100111 if (likely(msg && msg->len))
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200112 bo_inject(si->ob, msg->str, msg->len);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100113
114 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
Willy Tarreau148d0992010-01-10 10:21:21 +0100115 buffer_auto_read(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200116 buffer_auto_close(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100117 buffer_shutr_now(si->ob);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100118}
119
Willy Tarreaufb90d942009-09-05 20:57:35 +0200120/* default update function for scheduled tasks, not used for embedded tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200121static void stream_int_update(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200122{
123 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
124 __FUNCTION__,
125 si, si->state, si->ib->flags, si->ob->flags);
126
127 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
128 task_wakeup(si->owner, TASK_WOKEN_IO);
129}
130
131/* default update function for embedded tasks, to be used at the end of the i/o handler */
Willy Tarreauf873d752012-05-11 17:47:17 +0200132static void stream_int_update_embedded(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200133{
Willy Tarreau3488e252010-08-09 16:24:56 +0200134 int old_flags = si->flags;
135
Willy Tarreaufb90d942009-09-05 20:57:35 +0200136 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
137 __FUNCTION__,
138 si, si->state, si->ib->flags, si->ob->flags);
139
140 if (si->state != SI_ST_EST)
141 return;
142
143 if ((si->ob->flags & (BF_OUT_EMPTY|BF_SHUTW|BF_HIJACK|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200144 si_shutw(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200145
146 if ((si->ob->flags & (BF_FULL|BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK)) == 0)
147 si->flags |= SI_FL_WAIT_DATA;
148
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200149 /* we're almost sure that we need some space if the buffer is not
150 * empty, even if it's not full, because the applets can't fill it.
151 */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200152 if ((si->ib->flags & (BF_SHUTR|BF_OUT_EMPTY|BF_DONT_READ)) == 0)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200153 si->flags |= SI_FL_WAIT_ROOM;
154
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200155 if (si->ob->flags & BF_WRITE_ACTIVITY) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200156 if (tick_isset(si->ob->wex))
157 si->ob->wex = tick_add_ifset(now_ms, si->ob->wto);
158 }
159
Willy Tarreauf27b5ea2009-10-03 22:01:18 +0200160 if (si->ib->flags & BF_READ_ACTIVITY ||
161 (si->ob->flags & BF_WRITE_ACTIVITY && !(si->flags & SI_FL_INDEP_STR))) {
162 if (tick_isset(si->ib->rex))
163 si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
164 }
165
Willy Tarreau3488e252010-08-09 16:24:56 +0200166 /* save flags to detect changes */
167 old_flags = si->flags;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200168 if (likely((si->ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL|BF_DONT_READ)) == BF_WRITE_PARTIAL &&
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200169 (si->ob->prod->flags & SI_FL_WAIT_ROOM)))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200170 si_chk_rcv(si->ob->prod);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200171
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200172 if (((si->ib->flags & (BF_READ_PARTIAL|BF_OUT_EMPTY)) == BF_READ_PARTIAL) &&
Willy Tarreau3488e252010-08-09 16:24:56 +0200173 (si->ib->cons->flags & SI_FL_WAIT_DATA)) {
Willy Tarreau73b013b2012-05-21 16:31:45 +0200174 si_chk_snd(si->ib->cons);
Willy Tarreau3488e252010-08-09 16:24:56 +0200175 /* check if the consumer has freed some space */
176 if (!(si->ib->flags & BF_FULL))
177 si->flags &= ~SI_FL_WAIT_ROOM;
178 }
Willy Tarreaufb90d942009-09-05 20:57:35 +0200179
180 /* Note that we're trying to wake up in two conditions here :
181 * - special event, which needs the holder task attention
182 * - status indicating that the applet can go on working. This
183 * is rather hard because we might be blocking on output and
184 * don't want to wake up on input and vice-versa. The idea is
Willy Tarreau3488e252010-08-09 16:24:56 +0200185 * to only rely on the changes the chk_* might have performed.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200186 */
187 if (/* check stream interface changes */
Willy Tarreau3488e252010-08-09 16:24:56 +0200188 ((old_flags & ~si->flags) & (SI_FL_WAIT_ROOM|SI_FL_WAIT_DATA)) ||
189
190 /* changes on the production side */
191 (si->ib->flags & (BF_READ_NULL|BF_READ_ERROR)) ||
192 si->state != SI_ST_EST ||
193 (si->flags & SI_FL_ERR) ||
194 ((si->ib->flags & BF_READ_PARTIAL) &&
195 (!si->ib->to_forward || si->ib->cons->state != SI_ST_EST)) ||
196
197 /* changes on the consumption side */
198 (si->ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR)) ||
199 ((si->ob->flags & BF_WRITE_ACTIVITY) &&
200 ((si->ob->flags & BF_SHUTW) ||
201 si->ob->prod->state != SI_ST_EST ||
202 ((si->ob->flags & BF_OUT_EMPTY) && !si->ob->to_forward)))) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200203 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
204 task_wakeup(si->owner, TASK_WOKEN_IO);
205 }
Willy Tarreau3488e252010-08-09 16:24:56 +0200206 if (si->ib->flags & BF_READ_ACTIVITY)
207 si->ib->flags &= ~BF_READ_DONTWAIT;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200208}
209
210/* default shutr function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200211static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200212{
213 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
214 __FUNCTION__,
215 si, si->state, si->ib->flags, si->ob->flags);
216
217 si->ib->flags &= ~BF_SHUTR_NOW;
218 if (si->ib->flags & BF_SHUTR)
219 return;
220 si->ib->flags |= BF_SHUTR;
221 si->ib->rex = TICK_ETERNITY;
222 si->flags &= ~SI_FL_WAIT_ROOM;
223
224 if (si->state != SI_ST_EST && si->state != SI_ST_CON)
225 return;
226
227 if (si->ob->flags & BF_SHUTW) {
228 si->state = SI_ST_DIS;
229 si->exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200230
Willy Tarreau4da69a92012-05-21 18:05:40 +0200231 si_data_close(si);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200232 if (si->release)
233 si->release(si);
234 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200235
Willy Tarreaufb90d942009-09-05 20:57:35 +0200236 /* note that if the task exist, it must unregister itself once it runs */
237 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
238 task_wakeup(si->owner, TASK_WOKEN_IO);
239}
240
241/* default shutw function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200242static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200243{
244 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
245 __FUNCTION__,
246 si, si->state, si->ib->flags, si->ob->flags);
247
248 si->ob->flags &= ~BF_SHUTW_NOW;
249 if (si->ob->flags & BF_SHUTW)
250 return;
251 si->ob->flags |= BF_SHUTW;
252 si->ob->wex = TICK_ETERNITY;
253 si->flags &= ~SI_FL_WAIT_DATA;
254
255 switch (si->state) {
256 case SI_ST_EST:
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200257 if (!(si->ib->flags & (BF_SHUTR|BF_DONT_READ)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200258 break;
259
260 /* fall through */
261 case SI_ST_CON:
262 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100263 case SI_ST_QUE:
264 case SI_ST_TAR:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200265 si->state = SI_ST_DIS;
266 /* fall through */
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200267
Willy Tarreau4da69a92012-05-21 18:05:40 +0200268 si_data_close(si);
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200269 if (si->release)
270 si->release(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200271 default:
272 si->flags &= ~SI_FL_WAIT_ROOM;
273 si->ib->flags |= BF_SHUTR;
274 si->ib->rex = TICK_ETERNITY;
275 si->exp = TICK_ETERNITY;
276 }
277
278 /* note that if the task exist, it must unregister itself once it runs */
279 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
280 task_wakeup(si->owner, TASK_WOKEN_IO);
281}
282
283/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200284static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200285{
286 struct buffer *ib = si->ib;
287
288 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
289 __FUNCTION__,
290 si, si->state, si->ib->flags, si->ob->flags);
291
292 if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
293 return;
294
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200295 if (ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200296 /* stop reading */
Willy Tarreauf1ba4b32009-10-17 14:37:52 +0200297 if ((ib->flags & (BF_FULL|BF_HIJACK|BF_DONT_READ)) == BF_FULL)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200298 si->flags |= SI_FL_WAIT_ROOM;
299 }
300 else {
301 /* (re)start reading */
302 si->flags &= ~SI_FL_WAIT_ROOM;
303 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
304 task_wakeup(si->owner, TASK_WOKEN_IO);
305 }
306}
307
308/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200309static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200310{
311 struct buffer *ob = si->ob;
312
313 DPRINTF(stderr, "%s: si=%p, si->state=%d ib->flags=%08x ob->flags=%08x\n",
314 __FUNCTION__,
315 si, si->state, si->ib->flags, si->ob->flags);
316
317 if (unlikely(si->state != SI_ST_EST || (si->ob->flags & BF_SHUTW)))
318 return;
319
320 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
321 (ob->flags & BF_OUT_EMPTY)) /* called with nothing to send ! */
322 return;
323
324 /* Otherwise there are remaining data to be sent in the buffer,
325 * so we tell the handler.
326 */
327 si->flags &= ~SI_FL_WAIT_DATA;
328 if (!tick_isset(ob->wex))
329 ob->wex = tick_add_ifset(now_ms, ob->wto);
330
331 if (!(si->flags & SI_FL_DONT_WAKE) && si->owner)
332 task_wakeup(si->owner, TASK_WOKEN_IO);
333}
334
Willy Tarreaub24281b2011-02-13 13:16:36 +0100335/* Register an applet to handle a stream_interface as part of the stream
Willy Tarreaufb90d942009-09-05 20:57:35 +0200336 * interface's owner task, which is returned. The SI will wake it up everytime
Willy Tarreaub24281b2011-02-13 13:16:36 +0100337 * it is solicited. The task's processing function must call the applet's
Willy Tarreaufb90d942009-09-05 20:57:35 +0200338 * function before returning. It must be deleted by the task handler using
Willy Tarreaub24281b2011-02-13 13:16:36 +0100339 * stream_int_unregister_handler(), possibly from within the function itself.
Willy Tarreaufa6bac62012-05-31 14:16:59 +0200340 * It also pre-initializes applet.state to zero and the connection context
341 * to NULL.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200342 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100343struct task *stream_int_register_handler(struct stream_interface *si, struct si_applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200344{
Simon Horman7abd00d2011-08-13 08:03:51 +0900345 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si->owner);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200346
Willy Tarreau5c979a92012-05-07 17:15:39 +0200347 stream_interface_prepare(si, &stream_int_embedded);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200348 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100349 set_target_applet(&si->target, app);
Aman Gupta9a13e842012-04-02 18:57:53 -0700350 si->release = app->release;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200351 si->flags |= SI_FL_WAIT_DATA;
352 return si->owner;
353}
354
355/* Register a function to handle a stream_interface as a standalone task. The
356 * new task itself is returned and is assigned as si->owner. The stream_interface
357 * pointer will be pointed to by the task's context. The handler can be detached
358 * by using stream_int_unregister_handler().
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100359 * FIXME: the code should be updated to ensure that we don't change si->owner
360 * anymore as this is not needed. However, process_session still relies on it.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200361 */
362struct task *stream_int_register_handler_task(struct stream_interface *si,
363 struct task *(*fct)(struct task *))
364{
365 struct task *t;
366
367 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", fct, si, si->owner);
368
Willy Tarreau5c979a92012-05-07 17:15:39 +0200369 stream_interface_prepare(si, &stream_int_task);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200370 si->conn.ctrl = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100371 clear_target(&si->target);
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200372 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200373 si->flags |= SI_FL_WAIT_DATA;
374
375 t = task_new();
376 si->owner = t;
377 if (!t)
378 return t;
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100379
Willy Tarreau9e000c62011-03-10 14:03:36 +0100380 set_target_task(&si->target, t);
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100381
Willy Tarreaufb90d942009-09-05 20:57:35 +0200382 t->process = fct;
383 t->context = si;
384 task_wakeup(si->owner, TASK_WOKEN_INIT);
385
386 return t;
387}
388
389/* Unregister a stream interface handler. This must be called by the handler task
390 * itself when it detects that it is in the SI_ST_DIS state. This function can
391 * both detach standalone handlers and embedded handlers.
392 */
393void stream_int_unregister_handler(struct stream_interface *si)
394{
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100395 if (si->target.type == TARG_TYPE_TASK) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200396 /* external handler : kill the task */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100397 task_delete(si->target.ptr.t);
398 task_free(si->target.ptr.t);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200399 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200400 si->release = NULL;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200401 si->owner = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100402 clear_target(&si->target);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200403}
404
Willy Tarreau2c6be842012-07-06 17:12:34 +0200405/* This callback is used to send a valid PROXY protocol line to a socket being
406 * established. It returns a combination of FD_WAIT_* if it wants some polling
407 * before being called again, otherwise it returns zero and removes itself from
408 * the connection's flags (the bit is provided in <flag> by the caller).
409 */
410int conn_si_send_proxy(struct connection *conn, unsigned int flag)
411{
412 int fd = conn->t.sock.fd;
413 struct stream_interface *si = container_of(conn, struct stream_interface, conn);
414 struct buffer *b = si->ob;
415
416 /* we might have been called just after an asynchronous shutw */
417 if (b->flags & BF_SHUTW)
418 goto out_error;
419
420 /* If we have a PROXY line to send, we'll use this to validate the
421 * connection, in which case the connection is validated only once
422 * we've sent the whole proxy line. Otherwise we use connect().
423 */
424 if (si->send_proxy_ofs) {
425 int ret;
426
427 /* The target server expects a PROXY line to be sent first.
428 * If the send_proxy_ofs is negative, it corresponds to the
429 * offset to start sending from then end of the proxy string
430 * (which is recomputed every time since it's constant). If
431 * it is positive, it means we have to send from the start.
432 */
433 ret = make_proxy_line(trash, trashlen, &b->prod->addr.from, &b->prod->addr.to);
434 if (!ret)
435 goto out_error;
436
437 if (si->send_proxy_ofs > 0)
438 si->send_proxy_ofs = -ret; /* first call */
439
440 /* we have to send trash from (ret+sp for -sp bytes) */
441 ret = send(fd, trash + ret + si->send_proxy_ofs, -si->send_proxy_ofs,
442 (b->flags & BF_OUT_EMPTY) ? 0 : MSG_MORE);
443
444 if (ret == 0)
445 goto out_wait;
446
447 if (ret < 0) {
448 if (errno == EAGAIN)
449 goto out_wait;
450 goto out_error;
451 }
452
453 si->send_proxy_ofs += ret; /* becomes zero once complete */
454 if (si->send_proxy_ofs != 0)
455 goto out_wait;
456
457 /* OK we've sent the whole line, we're connected */
458 }
459
460 /* The FD is ready now, simply return and let the connection handler
461 * notify upper layers if needed.
462 */
463 if (conn->flags & CO_FL_WAIT_L4_CONN)
464 conn->flags &= ~CO_FL_WAIT_L4_CONN;
465 b->flags |= BF_WRITE_NULL;
466 si->exp = TICK_ETERNITY;
467
468 out_leave:
469 conn->flags &= ~flag;
470 return 0;
471
472 out_error:
473 /* Write error on the file descriptor. We mark the FD as STERROR so
474 * that we don't use it anymore. The error is reported to the stream
475 * interface which will take proper action. We must not perturbate the
476 * buffer because the stream interface wants to ensure transparent
477 * connection retries.
478 */
479
480 conn->flags |= CO_FL_ERROR;
481 fdtab[fd].ev &= ~FD_POLL_STICKY;
482 EV_FD_REM(fd);
483 goto out_leave;
484
485 out_wait:
486 return FD_WAIT_WRITE;
487}
488
489
Willy Tarreaudded32d2008-11-30 19:48:07 +0100490/*
Willy Tarreaucff64112008-11-03 06:26:53 +0100491 * Local variables:
492 * c-indent-level: 8
493 * c-basic-offset: 8
494 * End:
495 */