blob: 5caff85fb1cef25b920b9f1328c471d9bc1f699f [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 Tarreaucc5cfcb2012-05-04 21:35:27 +020072/* Returns an absolute pointer for a position relative to the current buffer's
73 * pointer. It is written so that it is optimal when <ofs> is a const. It is
74 * written as a macro instead of an inline function so that the compiler knows
75 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
76 */
77#define b_ptr(b, ofs) \
78 ({ \
79 char *__ret = (b)->p + (ofs); \
80 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
81 __ret -= (b)->size; \
82 else if ((ofs) < 0 && __ret < (b)->data) \
83 __ret += (b)->size; \
84 __ret; \
85 })
86
87/* Returns the start of the input data in a buffer */
88static inline char *bi_ptr(const struct buffer *b)
89{
90 return b->p;
91}
92
93/* Returns the end of the input data in a buffer (pointer to next
94 * insertion point).
95 */
96static inline char *bi_end(const struct buffer *b)
97{
98 char *ret = b->p + b->i;
99
100 if (ret >= b->data + b->size)
101 ret -= b->size;
102 return ret;
103}
104
105/* Returns the amount of input data that can contiguously be read at once */
106static inline int bi_contig_data(const struct buffer *b)
107{
108 int data = b->data + b->size - b->p;
109
110 if (data > b->i)
111 data = b->i;
112 return data;
113}
114
115/* Returns the start of the output data in a buffer */
116static inline char *bo_ptr(const struct buffer *b)
117{
118 char *ret = b->p - b->o;
119
120 if (ret < b->data)
121 ret += b->size;
122 return ret;
123}
124
125/* Returns the end of the output data in a buffer */
126static inline char *bo_end(const struct buffer *b)
127{
128 return b->p;
129}
130
131/* Returns the amount of output data that can contiguously be read at once */
132static inline int bo_contig_data(const struct buffer *b)
133{
134 char *beg = b->p - b->o;
135
136 if (beg < b->data)
137 return b->data - beg;
138 return b->o;
139}
140
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100141/* Return the buffer's length in bytes by summing the input and the output */
142static inline int buffer_len(const struct buffer *buf)
143{
144 return buf->i + buf->o;
145}
146
147/* Return non-zero only if the buffer is not empty */
148static inline int buffer_not_empty(const struct buffer *buf)
149{
150 return buf->i | buf->o;
151}
152
153/* Return non-zero only if the buffer is empty */
154static inline int buffer_empty(const struct buffer *buf)
155{
156 return !buffer_not_empty(buf);
157}
158
Willy Tarreau7fd758b2012-03-02 10:38:01 +0100159/* Normalizes a pointer after a subtract */
160static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
161{
162 if (ptr < buf->data)
163 ptr += buf->size;
164 return ptr;
165}
166
167/* Normalizes a pointer after an addition */
168static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
169{
170 if (ptr - buf->size >= buf->data)
171 ptr -= buf->size;
172 return ptr;
173}
174
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100175/* Return the number of reserved bytes in the buffer, which ensures that once
176 * all pending data are forwarded, the buffer still has global.tune.maxrewrite
177 * bytes free. The result is between 0 and global.maxrewrite, which is itself
178 * smaller than any buf->size.
179 */
180static inline int buffer_reserved(const struct buffer *buf)
181{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100182 int ret = global.tune.maxrewrite - buf->to_forward - buf->o;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100183
184 if (buf->to_forward == BUF_INFINITE_FORWARD)
185 return 0;
186 if (ret <= 0)
187 return 0;
188 return ret;
189}
190
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100191/* Return the max number of bytes the buffer can contain so that once all the
192 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
193 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
194 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100195static inline int buffer_max_len(const struct buffer *buf)
196{
197 return buf->size - buffer_reserved(buf);
198}
199
200/* Return the maximum amount of bytes that can be written into the buffer,
201 * including reserved space which may be overwritten.
202 */
203static inline int buffer_total_space(const struct buffer *buf)
204{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100205 return buf->size - buffer_len(buf);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100206}
207
208/* Return the maximum amount of bytes that can be written into the buffer,
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100209 * excluding the reserved space, which is preserved. 0 may be returned if
210 * the reserved space was already reached or used.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100211 */
212static inline int buffer_total_space_res(const struct buffer *buf)
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100213{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100214 int len = buffer_max_len(buf) - buffer_len(buf);
Willy Tarreaufe4b1f92011-11-28 13:40:49 +0100215 return len < 0 ? 0 : len;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100216}
217
218/* Returns the number of contiguous bytes between <start> and <start>+<count>,
219 * and enforces a limit on buf->data + buf->size. <start> must be within the
220 * buffer.
221 */
222static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
223{
224 if (count > buf->data - start + buf->size)
225 count = buf->data - start + buf->size;
226 return count;
227}
228
229/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100230 * including reserved space which may be overwritten.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100231 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100232static inline int buffer_contig_space(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100233{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100234 const char *left, *right;
235
236 if (buf->data + buf->o <= buf->p)
237 right = buf->data + buf->size;
238 else
239 right = buf->p + buf->size - buf->o;
240
241 left = buffer_wrap_add(buf, buf->p + buf->i);
242 return right - left;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100243}
244
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100245
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100246/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100247 * excluding the amount of reserved space passed in <res>, which is
248 * preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100249 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100250static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100251{
252 /* Proceed differently if the buffer is full, partially used or empty.
253 * The hard situation is when it's partially used and either data or
254 * reserved space wraps at the end.
255 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100256 int spare = buf->size - res;
257
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100258 if (buffer_len(buf) >= spare)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100259 spare = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100260 else if (buffer_len(buf)) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100261 spare = buffer_contig_space(buf) - res;
262 if (spare < 0)
263 spare = 0;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100264 }
265 return spare;
266}
267
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100268
269/* Return the amount of bytes that can be written into the buffer at once,
270 * excluding reserved space, which is preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100271 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100272static inline int buffer_contig_space_res(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100273{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100274 return buffer_contig_space_with_res(buf, buffer_reserved(buf));
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100275}
276
277/* Normalizes a pointer which is supposed to be relative to the beginning of a
278 * buffer, so that wrapping is correctly handled. The intent is to use this
279 * when increasing a pointer. Note that the wrapping test is only performed
Willy Tarreau71730252011-11-28 16:04:29 +0100280 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100281 * otherwise an invalid pointer might be returned.
282 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100283static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100284{
Willy Tarreau71730252011-11-28 16:04:29 +0100285 if (ptr < buf->data)
286 ptr += buf->size;
287 else if (ptr - buf->size >= buf->data)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100288 ptr -= buf->size;
289 return ptr;
290}
291
292/* Returns the distance between two pointers, taking into account the ability
293 * to wrap around the buffer's end.
294 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100295static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100296{
297 int count = to - from;
298 if (count < 0)
299 count += buf->size;
300 return count;
301}
302
303/* returns the amount of pending bytes in the buffer. It is the amount of bytes
304 * that is not scheduled to be sent.
305 */
306static inline int buffer_pending(const struct buffer *buf)
307{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100308 return buf->i;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100309}
310
311/* Returns the size of the working area which the caller knows ends at <end>.
312 * If <end> equals buf->r (modulo size), then it means that the free area which
313 * follows is part of the working area. Otherwise, the working area stops at
Willy Tarreau89fa7062012-03-02 16:13:16 +0100314 * <end>. It always starts at buf->p. The work area includes the
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100315 * reserved area.
316 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100317static inline int buffer_work_area(const struct buffer *buf, const char *end)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100318{
319 end = buffer_pointer(buf, end);
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100320 if (end == buffer_wrap_add(buf, buf->p + buf->i))
321 /* pointer exactly at end, lets push forwards */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100322 end = buffer_wrap_sub(buf, buf->p - buf->o);
323 return buffer_count(buf, buf->p, end);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100324}
325
326/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
327static inline int buffer_almost_full(const struct buffer *buf)
328{
329 if (buffer_total_space(buf) < buf->size / 4)
330 return 1;
331 return 0;
332}
333
Willy Tarreau74b08c92010-09-08 17:04:31 +0200334/* Returns true if the buffer's input is already closed */
335static inline int buffer_input_closed(struct buffer *buf)
336{
337 return ((buf->flags & BF_SHUTR) != 0);
338}
339
340/* Returns true if the buffer's output is already closed */
341static inline int buffer_output_closed(struct buffer *buf)
342{
343 return ((buf->flags & BF_SHUTW) != 0);
344}
345
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200346/* Check buffer timeouts, and set the corresponding flags. The
347 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100348 * The read/write timeouts are not set if there was activity on the buffer.
349 * That way, we don't have to update the timeout on every I/O. Note that the
350 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200351 */
352static inline void buffer_check_timeouts(struct buffer *b)
353{
Willy Tarreau86491c32008-12-14 09:04:47 +0100354 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200355 unlikely(tick_is_expired(b->rex, now_ms)))
356 b->flags |= BF_READ_TIMEOUT;
357
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100358 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200359 unlikely(tick_is_expired(b->wex, now_ms)))
360 b->flags |= BF_WRITE_TIMEOUT;
361
362 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
363 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
364 b->flags |= BF_ANA_TIMEOUT;
365}
366
Willy Tarreau2e046c62012-03-01 16:08:30 +0100367/* Schedule all remaining buffer data to be sent. ->o is not touched if it
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100368 * already covers those data. That permits doing a flush even after a forward,
369 * although not recommended.
370 */
371static inline void buffer_flush(struct buffer *buf)
372{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100373 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100374 buf->o += buf->i;
375 buf->i = 0;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100376 if (buf->o)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200377 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100378}
379
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100380/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100381 * that any spliced data is not affected since we may not have any access to
382 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200383 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100384static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100386 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100387 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100388 buf->to_forward = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +0100389 buf->p = buf->data;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200390 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
391 if (!buf->pipe)
392 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393}
394
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200395/* Cut the "tail" of the buffer, which means strip it to the length of unsent
396 * data only, and kill any remaining unsent data. Any scheduled forwarding is
397 * stopped. This is mainly to be used to send error messages after existing
398 * data.
399 */
400static inline void buffer_cut_tail(struct buffer *buf)
401{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100402 if (!buf->o)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200403 return buffer_erase(buf);
404
405 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100406 if (!buf->i)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200407 return;
408
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100409 buf->i = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200410 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100411 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200412 buf->flags |= BF_FULL;
413}
414
Willy Tarreauec1bc822012-03-09 15:03:30 +0100415/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
416 * call this function with remaining data waiting to be sent (o > 0). The
417 * caller must ensure that <n> is smaller than the actual buffer's length.
418 * This is mainly used to remove empty lines at the beginning of a request
419 * or a response.
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100420 */
421static inline void buffer_ignore(struct buffer *buf, int n)
422{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100423 buf->i -= n;
Willy Tarreauec1bc822012-03-09 15:03:30 +0100424 buf->p += n;
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100425}
426
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200427/* marks the buffer as "shutdown" ASAP for reads */
428static inline void buffer_shutr_now(struct buffer *buf)
429{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100430 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200431}
432
433/* marks the buffer as "shutdown" ASAP for writes */
434static inline void buffer_shutw_now(struct buffer *buf)
435{
436 buf->flags |= BF_SHUTW_NOW;
437}
438
439/* marks the buffer as "shutdown" ASAP in both directions */
440static inline void buffer_abort(struct buffer *buf)
441{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100442 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100443 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200444}
445
Willy Tarreau01bf8672008-12-07 18:03:29 +0100446/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
447 * flag is set, and the function called once. The function is responsible for
448 * clearing the hijack bit. It is possible that the function clears the flag
449 * during this first call.
450 */
451static inline void buffer_install_hijacker(struct session *s,
452 struct buffer *b,
453 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200454{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100455 b->hijacker = func;
456 b->flags |= BF_HIJACK;
457 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200458}
459
Willy Tarreau01bf8672008-12-07 18:03:29 +0100460/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200461static inline void buffer_stop_hijack(struct buffer *buf)
462{
463 buf->flags &= ~BF_HIJACK;
464}
465
Willy Tarreau520d95e2009-09-19 21:04:57 +0200466/* allow the consumer to try to establish a new connection. */
467static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200468{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200469 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200470}
471
Willy Tarreau520d95e2009-09-19 21:04:57 +0200472/* prevent the consumer from trying to establish a new connection, and also
473 * disable auto shutdown forwarding.
474 */
475static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200476{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200477 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200478}
479
Willy Tarreau520d95e2009-09-19 21:04:57 +0200480/* allow the producer to forward shutdown requests */
481static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100482{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200483 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100484}
485
Willy Tarreau520d95e2009-09-19 21:04:57 +0200486/* prevent the producer from forwarding shutdown requests */
487static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100488{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200489 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100490}
491
Willy Tarreau90deb182010-01-07 00:20:41 +0100492/* allow the producer to read / poll the input */
493static inline void buffer_auto_read(struct buffer *buf)
494{
495 buf->flags &= ~BF_DONT_READ;
496}
497
498/* prevent the producer from read / poll the input */
499static inline void buffer_dont_read(struct buffer *buf)
500{
501 buf->flags |= BF_DONT_READ;
502}
503
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504/*
505 * Tries to realign the given buffer, and returns how many bytes can be written
506 * there at once without overwriting anything.
507 */
508static inline int buffer_realign(struct buffer *buf)
509{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100510 if (!(buf->i | buf->o)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200511 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100512 buf->p = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100514 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200515}
516
517/*
518 * Advance the buffer's read pointer by <len> bytes. This is useful when data
519 * have been read directly from the buffer. It is illegal to call this function
520 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e046c62012-03-01 16:08:30 +0100521 * responsibility to ensure that <len> is never larger than buf->o.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200522 */
523static inline void buffer_skip(struct buffer *buf, int len)
524{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100525 buf->o -= len;
526 if (buffer_len(buf) == 0)
Willy Tarreaua458b672012-03-05 11:17:50 +0100527 buf->p = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200528
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100529 if (buffer_len(buf) < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200530 buf->flags &= ~BF_FULL;
531
Willy Tarreau2e046c62012-03-01 16:08:30 +0100532 if (!buf->o && !buf->pipe)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200533 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200534
535 /* notify that some data was written to the SI from the buffer */
536 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200537}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538
Willy Tarreauaeac3192009-08-31 08:09:57 +0200539/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
540 * -2 if it is larger than the buffer size, or the number of bytes available
541 * otherwise. If the chunk has been written, its size is automatically reset
542 * to zero. The send limit is automatically adjusted with the amount of data
543 * written.
544 */
545static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
546{
547 int ret;
548
549 ret = buffer_write(buf, chunk->str, chunk->len);
550 if (ret == -1)
551 chunk->len = 0;
552 return ret;
553}
554
Willy Tarreau74b08c92010-09-08 17:04:31 +0200555/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100556 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200557 * closed, -2 is returned. If the block is too large for this buffer, -3 is
558 * returned. If there is not enough room left in the buffer, -1 is returned.
559 * Otherwise the number of bytes copied is returned (0 being a valid number).
560 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
561 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200562 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200563static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200564{
565 int ret;
566
Willy Tarreau74b08c92010-09-08 17:04:31 +0200567 ret = buffer_put_block(buf, chunk->str, chunk->len);
568 if (ret > 0)
569 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200570 return ret;
571}
572
Willy Tarreau74b08c92010-09-08 17:04:31 +0200573/* Tries to copy string <str> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100574 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200575 * closed, -2 is returned. If the block is too large for this buffer, -3 is
576 * returned. If there is not enough room left in the buffer, -1 is returned.
577 * Otherwise the number of bytes copied is returned (0 being a valid number).
578 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
579 * transferred.
580 */
581static inline int buffer_put_string(struct buffer *buf, const char *str)
582{
583 return buffer_put_block(buf, str, strlen(str));
584}
585
586/*
587 * Return one char from the buffer. If the buffer is empty and closed, return -2.
588 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
589 * 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 +0100590 * Also note that this function respects the ->o limit.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200591 */
592static inline int buffer_get_char(struct buffer *buf)
593{
594 /* closed or empty + imminent close = -2; empty = -1 */
595 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
596 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
597 return -2;
598 return -1;
599 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100600 return *buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200601}
602
603
604/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
605 * returns >= 0 if the buffer is too small to hold the message, -1 if the
606 * transfer was OK, -2 in case of failure.
607 */
608static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
609{
610 int ret = buffer_put_chunk(buf, msg);
611 if (ret >= 0) /* transfer OK */
612 return -1;
613 if (ret == -1) /* missing room */
614 return 1;
615 /* failure */
616 return -2;
617}
618
619/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
620 * returns >= 0 if the buffer is too small to hold the message, -1 if the
621 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200622 */
623static inline int buffer_feed(struct buffer *buf, const char *str)
624{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200625 int ret = buffer_put_string(buf, str);
626 if (ret >= 0) /* transfer OK */
627 return -1;
628 if (ret == -1) /* missing room */
629 return 1;
630 /* failure */
631 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200632}
633
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100634
635/* This function writes the string <str> at position <pos> which must be in
636 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
637 * (l, r, lr) are updated to be valid after the shift. the shift value
638 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100639 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100640 * it does not make sense to use it on data scheduled to be sent.
641 */
642static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
643{
644 return buffer_replace2(b, pos, end, str, strlen(str));
645}
646
Willy Tarreau74b08c92010-09-08 17:04:31 +0200647/*
648 *
649 * Functions below are used to manage chunks
650 *
651 */
652
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200653static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
654 chk->str = str;
655 chk->len = 0;
656 chk->size = size;
657}
658
659/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200660static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200661
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200662 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200663 return 0;
664
665 chk->str = str;
666 chk->len = len;
667 chk->size = size;
668
669 return 1;
670}
671
672static inline void chunk_initstr(struct chunk *chk, char *str) {
673 chk->str = str;
674 chk->len = strlen(str);
675 chk->size = 0; /* mark it read-only */
676}
677
678static inline int chunk_strcpy(struct chunk *chk, const char *str) {
679 size_t len;
680
681 len = strlen(str);
682
683 if (unlikely(len > chk->size))
684 return 0;
685
686 chk->len = len;
687 memcpy(chk->str, str, len);
688
689 return 1;
690}
691
692int chunk_printf(struct chunk *chk, const char *fmt, ...)
693 __attribute__ ((format(printf, 2, 3)));
694
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200695int chunk_htmlencode(struct chunk *dst, struct chunk *src);
696int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
697
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200698static inline void chunk_reset(struct chunk *chk) {
699 chk->str = NULL;
700 chk->len = -1;
701 chk->size = 0;
702}
703
704static inline void chunk_destroy(struct chunk *chk) {
705
706 if (!chk->size)
707 return;
708
709 if (chk->str)
710 free(chk->str);
711
712 chunk_reset(chk);
713}
714
Willy Tarreau0f772532006-12-23 20:51:41 +0100715/*
716 * frees the destination chunk if already allocated, allocates a new string,
717 * and copies the source into it. The pointer to the destination string is
718 * returned, or NULL if the allocation fails or if any pointer is NULL..
719 */
720static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
721 if (!dst || !src || !src->str)
722 return NULL;
723 if (dst->str)
724 free(dst->str);
725 dst->len = src->len;
726 dst->str = (char *)malloc(dst->len);
727 memcpy(dst->str, src->str, dst->len);
728 return dst->str;
729}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730
731#endif /* _PROTO_BUFFERS_H */
732
733/*
734 * Local variables:
735 * c-indent-level: 8
736 * c-basic-offset: 8
737 * End:
738 */