blob: 6d4d3e73967c442ff0a567a83728d293cc551eb5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauc7e42382012-08-24 19:22:53 +02002 * include/proto/channel.h
3 * Channel management definitions, macros and inline functions.
Willy Tarreau7c3c5412009-12-13 15:53:05 +01004 *
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau7c3c5412009-12-13 15:53:05 +01006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreauc7e42382012-08-24 19:22:53 +020022#ifndef _PROTO_CHANNEL_H
23#define _PROTO_CHANNEL_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau7341d942007-05-13 19:56:02 +020025#include <stdio.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010026#include <stdlib.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020027#include <string.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010028
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020029#include <common/config.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020031#include <common/memory.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020032#include <common/ticks.h>
Willy Tarreaufa645582007-06-03 15:59:52 +020033#include <common/time.h>
34
Willy Tarreau7c3c5412009-12-13 15:53:05 +010035#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau7341d942007-05-13 19:56:02 +020037extern struct pool_head *pool2_buffer;
38
39/* perform minimal intializations, report 0 in case of error, 1 if OK. */
40int init_buffer();
41
Willy Tarreau74b08c92010-09-08 17:04:31 +020042/* SI-to-buffer functions : buffer_{get,put}_{char,block,string,chunk} */
Willy Tarreau7421efb2012-07-02 15:11:27 +020043int bo_inject(struct channel *buf, const char *msg, int len);
44int bi_putblk(struct channel *buf, const char *str, int len);
45int bi_putchr(struct channel *buf, char c);
46int bo_getline(struct channel *buf, char *str, int len);
47int bo_getblk(struct channel *buf, char *blk, int len, int offset);
48int buffer_replace2(struct channel *b, char *pos, char *end, const char *str, int len);
49int buffer_insert_line2(struct channel *b, char *pos, const char *str, int len);
Willy Tarreau7421efb2012-07-02 15:11:27 +020050unsigned long long buffer_forward(struct channel *buf, unsigned long long bytes);
Willy Tarreau74b08c92010-09-08 17:04:31 +020051
Willy Tarreau8e21bb92012-08-24 22:40:29 +020052/* Initialize all fields in the buffer. */
Willy Tarreau7421efb2012-07-02 15:11:27 +020053static inline void buffer_init(struct channel *buf)
Willy Tarreau54469402006-07-29 16:59:06 +020054{
Willy Tarreau572bf902012-07-02 17:01:20 +020055 buf->buf.o = 0;
56 buf->buf.i = 0;
57 buf->buf.p = buf->buf.data;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010058 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010059 buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010060 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020061 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020062 buf->cons = NULL;
Willy Tarreau8e21bb92012-08-24 22:40:29 +020063 buf->flags = 0;
Willy Tarreau54469402006-07-29 16:59:06 +020064}
65
Willy Tarreau4b517ca2011-11-25 20:33:58 +010066/*****************************************************************/
67/* These functions are used to compute various buffer area sizes */
68/*****************************************************************/
69
Willy Tarreau8e21bb92012-08-24 22:40:29 +020070/* Reports non-zero if the channel is empty, which means both its
71 * buffer and pipe are empty. The construct looks strange but is
72 * jump-less and much more efficient on both 32 and 64-bit than
73 * the boolean test.
74 */
75static inline unsigned int channel_is_empty(struct channel *c)
76{
77 return !(c->buf.o | (long)c->pipe);
78}
79
Willy Tarreau4b517ca2011-11-25 20:33:58 +010080/* Return the number of reserved bytes in the buffer, which ensures that once
81 * all pending data are forwarded, the buffer still has global.tune.maxrewrite
82 * bytes free. The result is between 0 and global.maxrewrite, which is itself
83 * smaller than any buf->size.
84 */
Willy Tarreau7421efb2012-07-02 15:11:27 +020085static inline int buffer_reserved(const struct channel *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +010086{
Willy Tarreau572bf902012-07-02 17:01:20 +020087 int ret = global.tune.maxrewrite - buf->to_forward - buf->buf.o;
Willy Tarreau4b517ca2011-11-25 20:33:58 +010088
89 if (buf->to_forward == BUF_INFINITE_FORWARD)
90 return 0;
91 if (ret <= 0)
92 return 0;
93 return ret;
94}
95
Willy Tarreau7c3c5412009-12-13 15:53:05 +010096/* Return the max number of bytes the buffer can contain so that once all the
97 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
98 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
99 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200100static inline int buffer_max_len(const struct channel *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100101{
Willy Tarreau572bf902012-07-02 17:01:20 +0200102 return buf->buf.size - buffer_reserved(buf);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100103}
104
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200105/* Returns non-zero if the buffer input is considered full. The reserved space
106 * is taken into account if ->to_forward indicates that an end of transfer is
107 * close to happen. The test is optimized to avoid as many operations as
108 * possible for the fast case and to be used as an "if" condition.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100109 */
Willy Tarreauad1cc3d2012-08-27 18:54:20 +0200110static inline int channel_full(const struct channel *b)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100111{
Willy Tarreau572bf902012-07-02 17:01:20 +0200112 int rem = b->buf.size;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200113
Willy Tarreau572bf902012-07-02 17:01:20 +0200114 rem -= b->buf.o;
115 rem -= b->buf.i;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200116 if (!rem)
117 return 1; /* buffer already full */
118
Willy Tarreau572bf902012-07-02 17:01:20 +0200119 if (b->to_forward >= b->buf.size ||
120 (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->buf.size)) && // just there to ensure gcc
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200121 b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second
122 return 0; // test whenever possible
123
124 rem -= global.tune.maxrewrite;
Willy Tarreau572bf902012-07-02 17:01:20 +0200125 rem += b->buf.o;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200126 rem += b->to_forward;
127 return rem <= 0;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100128}
129
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200130/* Returns the amount of space available at the input of the buffer, taking the
131 * reserved space into account if ->to_forward indicates that an end of transfer
132 * is close to happen. The test is optimized to avoid as many operations as
133 * possible for the fast case.
134 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200135static inline int bi_avail(const struct channel *b)
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200136{
Willy Tarreau572bf902012-07-02 17:01:20 +0200137 int rem = b->buf.size;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200138 int rem2;
139
Willy Tarreau572bf902012-07-02 17:01:20 +0200140 rem -= b->buf.o;
141 rem -= b->buf.i;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200142 if (!rem)
143 return rem; /* buffer already full */
144
Willy Tarreau572bf902012-07-02 17:01:20 +0200145 if (b->to_forward >= b->buf.size ||
146 (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->buf.size)) && // just there to ensure gcc
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200147 b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second
148 return rem; // test whenever possible
149
150 rem2 = rem - global.tune.maxrewrite;
Willy Tarreau572bf902012-07-02 17:01:20 +0200151 rem2 += b->buf.o;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200152 rem2 += b->to_forward;
153
154 if (rem > rem2)
155 rem = rem2;
156 if (rem > 0)
157 return rem;
158 return 0;
159}
160
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100161/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100162 * excluding reserved space, which is preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100163 */
Willy Tarreau572bf902012-07-02 17:01:20 +0200164static inline int buffer_contig_space_res(const struct channel *chn)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100165{
Willy Tarreau572bf902012-07-02 17:01:20 +0200166 return buffer_contig_space_with_res(&chn->buf, buffer_reserved(chn));
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100167}
168
Willy Tarreau74b08c92010-09-08 17:04:31 +0200169/* Returns true if the buffer's input is already closed */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200170static inline int buffer_input_closed(struct channel *buf)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200171{
172 return ((buf->flags & BF_SHUTR) != 0);
173}
174
175/* Returns true if the buffer's output is already closed */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200176static inline int buffer_output_closed(struct channel *buf)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200177{
178 return ((buf->flags & BF_SHUTW) != 0);
179}
180
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200181/* Check buffer timeouts, and set the corresponding flags. The
182 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100183 * The read/write timeouts are not set if there was activity on the buffer.
184 * That way, we don't have to update the timeout on every I/O. Note that the
185 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200186 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200187static inline void buffer_check_timeouts(struct channel *b)
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200188{
Willy Tarreau86491c32008-12-14 09:04:47 +0100189 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200190 unlikely(tick_is_expired(b->rex, now_ms)))
191 b->flags |= BF_READ_TIMEOUT;
192
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100193 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200194 unlikely(tick_is_expired(b->wex, now_ms)))
195 b->flags |= BF_WRITE_TIMEOUT;
196
197 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
198 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
199 b->flags |= BF_ANA_TIMEOUT;
200}
201
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100202/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100203 * that any spliced data is not affected since we may not have any access to
204 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200205 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200206static inline void buffer_erase(struct channel *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207{
Willy Tarreau572bf902012-07-02 17:01:20 +0200208 buf->buf.o = 0;
209 buf->buf.i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100210 buf->to_forward = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +0200211 buf->buf.p = buf->buf.data;
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200212 buf->flags &= ~BF_FULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213}
214
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200215/* Cut the "tail" of the buffer, which means strip it to the length of unsent
216 * data only, and kill any remaining unsent data. Any scheduled forwarding is
217 * stopped. This is mainly to be used to send error messages after existing
218 * data.
219 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200220static inline void bi_erase(struct channel *buf)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200221{
Willy Tarreau572bf902012-07-02 17:01:20 +0200222 if (!buf->buf.o)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200223 return buffer_erase(buf);
224
225 buf->to_forward = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +0200226 if (!buf->buf.i)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200227 return;
228
Willy Tarreau572bf902012-07-02 17:01:20 +0200229 buf->buf.i = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200230 buf->flags &= ~BF_FULL;
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200231}
232
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200233/* marks the buffer as "shutdown" ASAP for reads */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200234static inline void buffer_shutr_now(struct channel *buf)
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200235{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100236 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200237}
238
239/* marks the buffer as "shutdown" ASAP for writes */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200240static inline void buffer_shutw_now(struct channel *buf)
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200241{
242 buf->flags |= BF_SHUTW_NOW;
243}
244
245/* marks the buffer as "shutdown" ASAP in both directions */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200246static inline void buffer_abort(struct channel *buf)
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200247{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100248 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100249 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200250}
251
Willy Tarreau01bf8672008-12-07 18:03:29 +0100252/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
253 * flag is set, and the function called once. The function is responsible for
254 * clearing the hijack bit. It is possible that the function clears the flag
255 * during this first call.
256 */
257static inline void buffer_install_hijacker(struct session *s,
Willy Tarreau7421efb2012-07-02 15:11:27 +0200258 struct channel *b,
259 void (*func)(struct session *, struct channel *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200260{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100261 b->hijacker = func;
262 b->flags |= BF_HIJACK;
263 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200264}
265
Willy Tarreau01bf8672008-12-07 18:03:29 +0100266/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200267static inline void buffer_stop_hijack(struct channel *buf)
Willy Tarreau72b179a2008-08-28 16:01:32 +0200268{
269 buf->flags &= ~BF_HIJACK;
270}
271
Willy Tarreau520d95e2009-09-19 21:04:57 +0200272/* allow the consumer to try to establish a new connection. */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200273static inline void buffer_auto_connect(struct channel *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200274{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200275 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200276}
277
Willy Tarreau520d95e2009-09-19 21:04:57 +0200278/* prevent the consumer from trying to establish a new connection, and also
279 * disable auto shutdown forwarding.
280 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200281static inline void buffer_dont_connect(struct channel *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200282{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200283 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200284}
285
Willy Tarreau520d95e2009-09-19 21:04:57 +0200286/* allow the producer to forward shutdown requests */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200287static inline void buffer_auto_close(struct channel *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100288{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200289 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100290}
291
Willy Tarreau520d95e2009-09-19 21:04:57 +0200292/* prevent the producer from forwarding shutdown requests */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200293static inline void buffer_dont_close(struct channel *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100294{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200295 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100296}
297
Willy Tarreau90deb182010-01-07 00:20:41 +0100298/* allow the producer to read / poll the input */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200299static inline void buffer_auto_read(struct channel *buf)
Willy Tarreau90deb182010-01-07 00:20:41 +0100300{
301 buf->flags &= ~BF_DONT_READ;
302}
303
304/* prevent the producer from read / poll the input */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200305static inline void buffer_dont_read(struct channel *buf)
Willy Tarreau90deb182010-01-07 00:20:41 +0100306{
307 buf->flags |= BF_DONT_READ;
308}
309
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310/*
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200311 * Advance the buffer's read pointer by <len> bytes. This is useful when data
312 * have been read directly from the buffer. It is illegal to call this function
313 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e046c62012-03-01 16:08:30 +0100314 * responsibility to ensure that <len> is never larger than buf->o.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200315 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200316static inline void bo_skip(struct channel *buf, int len)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200317{
Willy Tarreau572bf902012-07-02 17:01:20 +0200318 buf->buf.o -= len;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200319
Willy Tarreau572bf902012-07-02 17:01:20 +0200320 if (buffer_len(&buf->buf) == 0)
321 buf->buf.p = buf->buf.data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200322
Willy Tarreauad1cc3d2012-08-27 18:54:20 +0200323 if (!channel_full(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200324 buf->flags &= ~BF_FULL;
325
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200326 /* notify that some data was written to the SI from the buffer */
327 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200328}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329
Willy Tarreau74b08c92010-09-08 17:04:31 +0200330/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100331 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200332 * closed, -2 is returned. If the block is too large for this buffer, -3 is
333 * returned. If there is not enough room left in the buffer, -1 is returned.
334 * Otherwise the number of bytes copied is returned (0 being a valid number).
335 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
336 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200337 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200338static inline int bi_putchk(struct channel *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200339{
340 int ret;
341
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200342 ret = bi_putblk(buf, chunk->str, chunk->len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200343 if (ret > 0)
344 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200345 return ret;
346}
347
Willy Tarreau74b08c92010-09-08 17:04:31 +0200348/* Tries to copy string <str> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100349 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200350 * closed, -2 is returned. If the block is too large for this buffer, -3 is
351 * returned. If there is not enough room left in the buffer, -1 is returned.
352 * Otherwise the number of bytes copied is returned (0 being a valid number).
353 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
354 * transferred.
355 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200356static inline int bi_putstr(struct channel *buf, const char *str)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200357{
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200358 return bi_putblk(buf, str, strlen(str));
Willy Tarreau74b08c92010-09-08 17:04:31 +0200359}
360
361/*
362 * Return one char from the buffer. If the buffer is empty and closed, return -2.
363 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200364 * it's up to the caller to call bo_skip(buf, 1) when it has consumed the char.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100365 * Also note that this function respects the ->o limit.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200366 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200367static inline int bo_getchr(struct channel *buf)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200368{
369 /* closed or empty + imminent close = -2; empty = -1 */
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200370 if (unlikely((buf->flags & BF_SHUTW) || channel_is_empty(buf))) {
Willy Tarreau74b08c92010-09-08 17:04:31 +0200371 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
372 return -2;
373 return -1;
374 }
Willy Tarreau572bf902012-07-02 17:01:20 +0200375 return *buffer_wrap_sub(&buf->buf, buf->buf.p - buf->buf.o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200376}
377
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100378/* This function writes the string <str> at position <pos> which must be in
379 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
380 * (l, r, lr) are updated to be valid after the shift. the shift value
381 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau8e21bb92012-08-24 22:40:29 +0200382 * not done. The function does not adjust ->o because it does not make sense
383 * to use it on data scheduled to be sent.
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100384 */
Willy Tarreau7421efb2012-07-02 15:11:27 +0200385static inline int buffer_replace(struct channel *b, char *pos, char *end, const char *str)
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100386{
387 return buffer_replace2(b, pos, end, str, strlen(str));
388}
389
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390
Willy Tarreauc7e42382012-08-24 19:22:53 +0200391#endif /* _PROTO_CHANNEL_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392
393/*
394 * Local variables:
395 * c-indent-level: 8
396 * c-basic-offset: 8
397 * End:
398 */