blob: a95e5a1f811387dec882a6e7fe4f529e45f4cac7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau7c3c5412009-12-13 15:53:05 +01002 * include/proto/buffers.h
3 * Buffer management definitions, macros and inline functions.
4 *
Willy Tarreaub97f1992010-02-25 23:54:31 +01005 * Copyright (C) 2000-2010 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
22#ifndef _PROTO_BUFFERS_H
23#define _PROTO_BUFFERS_H
24
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 Tarreau7341d942007-05-13 19:56:02 +020030#include <common/memory.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020031#include <common/ticks.h>
Willy Tarreaufa645582007-06-03 15:59:52 +020032#include <common/time.h>
33
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/buffers.h>
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} */
43int buffer_write(struct buffer *buf, const char *msg, int len);
44int buffer_put_block(struct buffer *buf, const char *str, int len);
45int buffer_put_char(struct buffer *buf, char c);
46int buffer_get_line(struct buffer *buf, char *str, int len);
47int buffer_get_block(struct buffer *buf, char *blk, int len, int offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +020048int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
49int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
50void buffer_dump(FILE *o, struct buffer *b, int from, int to);
51void buffer_bounce_realign(struct buffer *buf);
Willy Tarreau0bc34932011-03-28 16:25:58 +020052unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes);
Willy Tarreau74b08c92010-09-08 17:04:31 +020053
Willy Tarreau7c3c5412009-12-13 15:53:05 +010054/* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */
Willy Tarreau54469402006-07-29 16:59:06 +020055static inline void buffer_init(struct buffer *buf)
56{
Willy Tarreau2e046c62012-03-01 16:08:30 +010057 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010058 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010059 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010060 buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010061 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020062 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020063 buf->cons = NULL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +020064 buf->flags = BF_OUT_EMPTY;
Willy Tarreaua458b672012-03-05 11:17:50 +010065 buf->p = buf->data;
Willy Tarreau54469402006-07-29 16:59:06 +020066}
67
Willy Tarreau4b517ca2011-11-25 20:33:58 +010068/*****************************************************************/
69/* These functions are used to compute various buffer area sizes */
70/*****************************************************************/
71
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010072/* Return the buffer's length in bytes by summing the input and the output */
73static inline int buffer_len(const struct buffer *buf)
74{
75 return buf->i + buf->o;
76}
77
78/* Return non-zero only if the buffer is not empty */
79static inline int buffer_not_empty(const struct buffer *buf)
80{
81 return buf->i | buf->o;
82}
83
84/* Return non-zero only if the buffer is empty */
85static inline int buffer_empty(const struct buffer *buf)
86{
87 return !buffer_not_empty(buf);
88}
89
Willy Tarreau7fd758b2012-03-02 10:38:01 +010090/* Normalizes a pointer after a subtract */
91static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
92{
93 if (ptr < buf->data)
94 ptr += buf->size;
95 return ptr;
96}
97
98/* Normalizes a pointer after an addition */
99static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
100{
101 if (ptr - buf->size >= buf->data)
102 ptr -= buf->size;
103 return ptr;
104}
105
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100106/* Return the number of reserved bytes in the buffer, which ensures that once
107 * all pending data are forwarded, the buffer still has global.tune.maxrewrite
108 * bytes free. The result is between 0 and global.maxrewrite, which is itself
109 * smaller than any buf->size.
110 */
111static inline int buffer_reserved(const struct buffer *buf)
112{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100113 int ret = global.tune.maxrewrite - buf->to_forward - buf->o;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100114
115 if (buf->to_forward == BUF_INFINITE_FORWARD)
116 return 0;
117 if (ret <= 0)
118 return 0;
119 return ret;
120}
121
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100122/* Return the max number of bytes the buffer can contain so that once all the
123 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
124 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
125 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100126static inline int buffer_max_len(const struct buffer *buf)
127{
128 return buf->size - buffer_reserved(buf);
129}
130
131/* Return the maximum amount of bytes that can be written into the buffer,
132 * including reserved space which may be overwritten.
133 */
134static inline int buffer_total_space(const struct buffer *buf)
135{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100136 return buf->size - buffer_len(buf);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100137}
138
139/* Return the maximum amount of bytes that can be written into the buffer,
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100140 * excluding the reserved space, which is preserved. 0 may be returned if
141 * the reserved space was already reached or used.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100142 */
143static inline int buffer_total_space_res(const struct buffer *buf)
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100144{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100145 int len = buffer_max_len(buf) - buffer_len(buf);
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100146 return len < 0 ? 0 : len;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100147}
148
149/* Returns the number of contiguous bytes between <start> and <start>+<count>,
150 * and enforces a limit on buf->data + buf->size. <start> must be within the
151 * buffer.
152 */
153static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
154{
155 if (count > buf->data - start + buf->size)
156 count = buf->data - start + buf->size;
157 return count;
158}
159
160/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100161 * including reserved space which may be overwritten.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100162 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100163static inline int buffer_contig_space(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100164{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100165 const char *left, *right;
166
167 if (buf->data + buf->o <= buf->p)
168 right = buf->data + buf->size;
169 else
170 right = buf->p + buf->size - buf->o;
171
172 left = buffer_wrap_add(buf, buf->p + buf->i);
173 return right - left;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100174}
175
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100176
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100177/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100178 * excluding the amount of reserved space passed in <res>, which is
179 * preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100180 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100181static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100182{
183 /* Proceed differently if the buffer is full, partially used or empty.
184 * The hard situation is when it's partially used and either data or
185 * reserved space wraps at the end.
186 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100187 int spare = buf->size - res;
188
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100189 if (buffer_len(buf) >= spare)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100190 spare = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100191 else if (buffer_len(buf)) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100192 spare = buffer_contig_space(buf) - res;
193 if (spare < 0)
194 spare = 0;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100195 }
196 return spare;
197}
198
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100199
200/* Return the amount of bytes that can be written into the buffer at once,
201 * excluding reserved space, which is preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100202 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100203static inline int buffer_contig_space_res(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100204{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100205 return buffer_contig_space_with_res(buf, buffer_reserved(buf));
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100206}
207
208/* Normalizes a pointer which is supposed to be relative to the beginning of a
209 * buffer, so that wrapping is correctly handled. The intent is to use this
210 * when increasing a pointer. Note that the wrapping test is only performed
Willy Tarreau71730252011-11-28 16:04:29 +0100211 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100212 * otherwise an invalid pointer might be returned.
213 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100214static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100215{
Willy Tarreau71730252011-11-28 16:04:29 +0100216 if (ptr < buf->data)
217 ptr += buf->size;
218 else if (ptr - buf->size >= buf->data)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100219 ptr -= buf->size;
220 return ptr;
221}
222
223/* Returns the distance between two pointers, taking into account the ability
224 * to wrap around the buffer's end.
225 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100226static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100227{
228 int count = to - from;
229 if (count < 0)
230 count += buf->size;
231 return count;
232}
233
234/* returns the amount of pending bytes in the buffer. It is the amount of bytes
235 * that is not scheduled to be sent.
236 */
237static inline int buffer_pending(const struct buffer *buf)
238{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100239 return buf->i;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100240}
241
242/* Returns the size of the working area which the caller knows ends at <end>.
243 * If <end> equals buf->r (modulo size), then it means that the free area which
244 * follows is part of the working area. Otherwise, the working area stops at
Willy Tarreau89fa7062012-03-02 16:13:16 +0100245 * <end>. It always starts at buf->p. The work area includes the
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100246 * reserved area.
247 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100248static inline int buffer_work_area(const struct buffer *buf, const char *end)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100249{
250 end = buffer_pointer(buf, end);
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100251 if (end == buffer_wrap_add(buf, buf->p + buf->i))
252 /* pointer exactly at end, lets push forwards */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100253 end = buffer_wrap_sub(buf, buf->p - buf->o);
254 return buffer_count(buf, buf->p, end);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100255}
256
257/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
258static inline int buffer_almost_full(const struct buffer *buf)
259{
260 if (buffer_total_space(buf) < buf->size / 4)
261 return 1;
262 return 0;
263}
264
Willy Tarreau74b08c92010-09-08 17:04:31 +0200265/* Returns true if the buffer's input is already closed */
266static inline int buffer_input_closed(struct buffer *buf)
267{
268 return ((buf->flags & BF_SHUTR) != 0);
269}
270
271/* Returns true if the buffer's output is already closed */
272static inline int buffer_output_closed(struct buffer *buf)
273{
274 return ((buf->flags & BF_SHUTW) != 0);
275}
276
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200277/* Check buffer timeouts, and set the corresponding flags. The
278 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100279 * The read/write timeouts are not set if there was activity on the buffer.
280 * That way, we don't have to update the timeout on every I/O. Note that the
281 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200282 */
283static inline void buffer_check_timeouts(struct buffer *b)
284{
Willy Tarreau86491c32008-12-14 09:04:47 +0100285 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200286 unlikely(tick_is_expired(b->rex, now_ms)))
287 b->flags |= BF_READ_TIMEOUT;
288
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100289 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200290 unlikely(tick_is_expired(b->wex, now_ms)))
291 b->flags |= BF_WRITE_TIMEOUT;
292
293 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
294 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
295 b->flags |= BF_ANA_TIMEOUT;
296}
297
Willy Tarreau2e046c62012-03-01 16:08:30 +0100298/* Schedule all remaining buffer data to be sent. ->o is not touched if it
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100299 * already covers those data. That permits doing a flush even after a forward,
300 * although not recommended.
301 */
302static inline void buffer_flush(struct buffer *buf)
303{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100304 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100305 buf->o += buf->i;
306 buf->i = 0;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100307 if (buf->o)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200308 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100309}
310
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100311/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100312 * that any spliced data is not affected since we may not have any access to
313 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200314 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100315static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100317 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100318 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100319 buf->to_forward = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +0100320 buf->p = buf->data;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200321 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
322 if (!buf->pipe)
323 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324}
325
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200326/* Cut the "tail" of the buffer, which means strip it to the length of unsent
327 * data only, and kill any remaining unsent data. Any scheduled forwarding is
328 * stopped. This is mainly to be used to send error messages after existing
329 * data.
330 */
331static inline void buffer_cut_tail(struct buffer *buf)
332{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100333 if (!buf->o)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200334 return buffer_erase(buf);
335
336 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100337 if (!buf->i)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200338 return;
339
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100340 buf->i = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200341 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100342 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200343 buf->flags |= BF_FULL;
344}
345
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100346/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
347 * is smaller than the actual buffer's length. This is mainly used to remove
348 * empty lines at the beginning of a request or a response.
349 */
350static inline void buffer_ignore(struct buffer *buf, int n)
351{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100352 buf->i -= n;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100353 buf->p = buffer_wrap_add(buf, buf->p + n);
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100354 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100355 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100356 buf->flags |= BF_FULL;
357}
358
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200359/* marks the buffer as "shutdown" ASAP for reads */
360static inline void buffer_shutr_now(struct buffer *buf)
361{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100362 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200363}
364
365/* marks the buffer as "shutdown" ASAP for writes */
366static inline void buffer_shutw_now(struct buffer *buf)
367{
368 buf->flags |= BF_SHUTW_NOW;
369}
370
371/* marks the buffer as "shutdown" ASAP in both directions */
372static inline void buffer_abort(struct buffer *buf)
373{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100374 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100375 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200376}
377
Willy Tarreau01bf8672008-12-07 18:03:29 +0100378/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
379 * flag is set, and the function called once. The function is responsible for
380 * clearing the hijack bit. It is possible that the function clears the flag
381 * during this first call.
382 */
383static inline void buffer_install_hijacker(struct session *s,
384 struct buffer *b,
385 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200386{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100387 b->hijacker = func;
388 b->flags |= BF_HIJACK;
389 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200390}
391
Willy Tarreau01bf8672008-12-07 18:03:29 +0100392/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200393static inline void buffer_stop_hijack(struct buffer *buf)
394{
395 buf->flags &= ~BF_HIJACK;
396}
397
Willy Tarreau520d95e2009-09-19 21:04:57 +0200398/* allow the consumer to try to establish a new connection. */
399static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200400{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200401 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200402}
403
Willy Tarreau520d95e2009-09-19 21:04:57 +0200404/* prevent the consumer from trying to establish a new connection, and also
405 * disable auto shutdown forwarding.
406 */
407static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200408{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200409 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200410}
411
Willy Tarreau520d95e2009-09-19 21:04:57 +0200412/* allow the producer to forward shutdown requests */
413static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100414{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200415 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100416}
417
Willy Tarreau520d95e2009-09-19 21:04:57 +0200418/* prevent the producer from forwarding shutdown requests */
419static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100420{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200421 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100422}
423
Willy Tarreau90deb182010-01-07 00:20:41 +0100424/* allow the producer to read / poll the input */
425static inline void buffer_auto_read(struct buffer *buf)
426{
427 buf->flags &= ~BF_DONT_READ;
428}
429
430/* prevent the producer from read / poll the input */
431static inline void buffer_dont_read(struct buffer *buf)
432{
433 buf->flags |= BF_DONT_READ;
434}
435
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436/*
437 * Tries to realign the given buffer, and returns how many bytes can be written
438 * there at once without overwriting anything.
439 */
440static inline int buffer_realign(struct buffer *buf)
441{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100442 if (!(buf->i | buf->o)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100444 buf->p = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100446 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200447}
448
449/*
450 * Advance the buffer's read pointer by <len> bytes. This is useful when data
451 * have been read directly from the buffer. It is illegal to call this function
452 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e046c62012-03-01 16:08:30 +0100453 * responsibility to ensure that <len> is never larger than buf->o.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200454 */
455static inline void buffer_skip(struct buffer *buf, int len)
456{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100457 buf->o -= len;
458 if (buffer_len(buf) == 0)
Willy Tarreaua458b672012-03-05 11:17:50 +0100459 buf->p = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200460
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100461 if (buffer_len(buf) < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200462 buf->flags &= ~BF_FULL;
463
Willy Tarreau2e046c62012-03-01 16:08:30 +0100464 if (!buf->o && !buf->pipe)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200465 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200466
467 /* notify that some data was written to the SI from the buffer */
468 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200469}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470
Willy Tarreauaeac3192009-08-31 08:09:57 +0200471/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
472 * -2 if it is larger than the buffer size, or the number of bytes available
473 * otherwise. If the chunk has been written, its size is automatically reset
474 * to zero. The send limit is automatically adjusted with the amount of data
475 * written.
476 */
477static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
478{
479 int ret;
480
481 ret = buffer_write(buf, chunk->str, chunk->len);
482 if (ret == -1)
483 chunk->len = 0;
484 return ret;
485}
486
Willy Tarreau74b08c92010-09-08 17:04:31 +0200487/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100488 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200489 * closed, -2 is returned. If the block is too large for this buffer, -3 is
490 * returned. If there is not enough room left in the buffer, -1 is returned.
491 * Otherwise the number of bytes copied is returned (0 being a valid number).
492 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
493 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200494 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200495static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200496{
497 int ret;
498
Willy Tarreau74b08c92010-09-08 17:04:31 +0200499 ret = buffer_put_block(buf, chunk->str, chunk->len);
500 if (ret > 0)
501 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200502 return ret;
503}
504
Willy Tarreau74b08c92010-09-08 17:04:31 +0200505/* Tries to copy string <str> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100506 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200507 * closed, -2 is returned. If the block is too large for this buffer, -3 is
508 * returned. If there is not enough room left in the buffer, -1 is returned.
509 * Otherwise the number of bytes copied is returned (0 being a valid number).
510 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
511 * transferred.
512 */
513static inline int buffer_put_string(struct buffer *buf, const char *str)
514{
515 return buffer_put_block(buf, str, strlen(str));
516}
517
518/*
519 * Return one char from the buffer. If the buffer is empty and closed, return -2.
520 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
521 * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100522 * Also note that this function respects the ->o limit.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200523 */
524static inline int buffer_get_char(struct buffer *buf)
525{
526 /* closed or empty + imminent close = -2; empty = -1 */
527 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
528 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
529 return -2;
530 return -1;
531 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100532 return *buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200533}
534
535
536/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
537 * returns >= 0 if the buffer is too small to hold the message, -1 if the
538 * transfer was OK, -2 in case of failure.
539 */
540static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
541{
542 int ret = buffer_put_chunk(buf, msg);
543 if (ret >= 0) /* transfer OK */
544 return -1;
545 if (ret == -1) /* missing room */
546 return 1;
547 /* failure */
548 return -2;
549}
550
551/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
552 * returns >= 0 if the buffer is too small to hold the message, -1 if the
553 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200554 */
555static inline int buffer_feed(struct buffer *buf, const char *str)
556{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200557 int ret = buffer_put_string(buf, str);
558 if (ret >= 0) /* transfer OK */
559 return -1;
560 if (ret == -1) /* missing room */
561 return 1;
562 /* failure */
563 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200564}
565
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100566
567/* This function writes the string <str> at position <pos> which must be in
568 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
569 * (l, r, lr) are updated to be valid after the shift. the shift value
570 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100571 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100572 * it does not make sense to use it on data scheduled to be sent.
573 */
574static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
575{
576 return buffer_replace2(b, pos, end, str, strlen(str));
577}
578
Willy Tarreau74b08c92010-09-08 17:04:31 +0200579/*
580 *
581 * Functions below are used to manage chunks
582 *
583 */
584
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200585static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
586 chk->str = str;
587 chk->len = 0;
588 chk->size = size;
589}
590
591/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200592static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200593
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200594 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200595 return 0;
596
597 chk->str = str;
598 chk->len = len;
599 chk->size = size;
600
601 return 1;
602}
603
604static inline void chunk_initstr(struct chunk *chk, char *str) {
605 chk->str = str;
606 chk->len = strlen(str);
607 chk->size = 0; /* mark it read-only */
608}
609
610static inline int chunk_strcpy(struct chunk *chk, const char *str) {
611 size_t len;
612
613 len = strlen(str);
614
615 if (unlikely(len > chk->size))
616 return 0;
617
618 chk->len = len;
619 memcpy(chk->str, str, len);
620
621 return 1;
622}
623
624int chunk_printf(struct chunk *chk, const char *fmt, ...)
625 __attribute__ ((format(printf, 2, 3)));
626
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200627int chunk_htmlencode(struct chunk *dst, struct chunk *src);
628int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
629
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200630static inline void chunk_reset(struct chunk *chk) {
631 chk->str = NULL;
632 chk->len = -1;
633 chk->size = 0;
634}
635
636static inline void chunk_destroy(struct chunk *chk) {
637
638 if (!chk->size)
639 return;
640
641 if (chk->str)
642 free(chk->str);
643
644 chunk_reset(chk);
645}
646
Willy Tarreau0f772532006-12-23 20:51:41 +0100647/*
648 * frees the destination chunk if already allocated, allocates a new string,
649 * and copies the source into it. The pointer to the destination string is
650 * returned, or NULL if the allocation fails or if any pointer is NULL..
651 */
652static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
653 if (!dst || !src || !src->str)
654 return NULL;
655 if (dst->str)
656 free(dst->str);
657 dst->len = src->len;
658 dst->str = (char *)malloc(dst->len);
659 memcpy(dst->str, src->str, dst->len);
660 return dst->str;
661}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
663#endif /* _PROTO_BUFFERS_H */
664
665/*
666 * Local variables:
667 * c-indent-level: 8
668 * c-basic-offset: 8
669 * End:
670 */