blob: 6f8e2c15d430432e68c0eb79dd5875b782970d1d [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 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
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} */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +020043int bo_inject(struct buffer *buf, const char *msg, int len);
44int bi_putblk(struct buffer *buf, const char *str, int len);
45int bi_putchr(struct buffer *buf, char c);
46int bo_getline(struct buffer *buf, char *str, int len);
47int bo_getblk(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);
Willy Tarreaua7fe8e52012-05-08 20:40:09 +020051void buffer_slow_realign(struct buffer *buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +020052void buffer_bounce_realign(struct buffer *buf);
Willy Tarreau0bc34932011-03-28 16:25:58 +020053unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes);
Willy Tarreau74b08c92010-09-08 17:04:31 +020054
Willy Tarreau7c3c5412009-12-13 15:53:05 +010055/* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */
Willy Tarreau54469402006-07-29 16:59:06 +020056static inline void buffer_init(struct buffer *buf)
57{
Willy Tarreau2e046c62012-03-01 16:08:30 +010058 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010059 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +010060 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010061 buf->total = 0;
Willy Tarreau3eba98a2009-01-25 13:56:13 +010062 buf->pipe = NULL;
Willy Tarreau2df28e82008-08-17 15:20:19 +020063 buf->analysers = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +020064 buf->cons = NULL;
Willy Tarreauba0b63d2009-09-20 08:09:44 +020065 buf->flags = BF_OUT_EMPTY;
Willy Tarreaua458b672012-03-05 11:17:50 +010066 buf->p = buf->data;
Willy Tarreau54469402006-07-29 16:59:06 +020067}
68
Willy Tarreau4b517ca2011-11-25 20:33:58 +010069/*****************************************************************/
70/* These functions are used to compute various buffer area sizes */
71/*****************************************************************/
72
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +020073/* Returns an absolute pointer for a position relative to the current buffer's
74 * pointer. It is written so that it is optimal when <ofs> is a const. It is
75 * written as a macro instead of an inline function so that the compiler knows
76 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
77 */
78#define b_ptr(b, ofs) \
79 ({ \
80 char *__ret = (b)->p + (ofs); \
81 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
82 __ret -= (b)->size; \
83 else if ((ofs) < 0 && __ret < (b)->data) \
84 __ret += (b)->size; \
85 __ret; \
86 })
87
88/* Returns the start of the input data in a buffer */
89static inline char *bi_ptr(const struct buffer *b)
90{
91 return b->p;
92}
93
94/* Returns the end of the input data in a buffer (pointer to next
95 * insertion point).
96 */
97static inline char *bi_end(const struct buffer *b)
98{
99 char *ret = b->p + b->i;
100
101 if (ret >= b->data + b->size)
102 ret -= b->size;
103 return ret;
104}
105
106/* Returns the amount of input data that can contiguously be read at once */
107static inline int bi_contig_data(const struct buffer *b)
108{
109 int data = b->data + b->size - b->p;
110
111 if (data > b->i)
112 data = b->i;
113 return data;
114}
115
116/* Returns the start of the output data in a buffer */
117static inline char *bo_ptr(const struct buffer *b)
118{
119 char *ret = b->p - b->o;
120
121 if (ret < b->data)
122 ret += b->size;
123 return ret;
124}
125
126/* Returns the end of the output data in a buffer */
127static inline char *bo_end(const struct buffer *b)
128{
129 return b->p;
130}
131
132/* Returns the amount of output data that can contiguously be read at once */
133static inline int bo_contig_data(const struct buffer *b)
134{
135 char *beg = b->p - b->o;
136
137 if (beg < b->data)
138 return b->data - beg;
139 return b->o;
140}
141
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100142/* Return the buffer's length in bytes by summing the input and the output */
143static inline int buffer_len(const struct buffer *buf)
144{
145 return buf->i + buf->o;
146}
147
148/* Return non-zero only if the buffer is not empty */
149static inline int buffer_not_empty(const struct buffer *buf)
150{
151 return buf->i | buf->o;
152}
153
154/* Return non-zero only if the buffer is empty */
155static inline int buffer_empty(const struct buffer *buf)
156{
157 return !buffer_not_empty(buf);
158}
159
Willy Tarreau7fd758b2012-03-02 10:38:01 +0100160/* Normalizes a pointer after a subtract */
161static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
162{
163 if (ptr < buf->data)
164 ptr += buf->size;
165 return ptr;
166}
167
168/* Normalizes a pointer after an addition */
169static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
170{
171 if (ptr - buf->size >= buf->data)
172 ptr -= buf->size;
173 return ptr;
174}
175
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100176/* Return the number of reserved bytes in the buffer, which ensures that once
177 * all pending data are forwarded, the buffer still has global.tune.maxrewrite
178 * bytes free. The result is between 0 and global.maxrewrite, which is itself
179 * smaller than any buf->size.
180 */
181static inline int buffer_reserved(const struct buffer *buf)
182{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100183 int ret = global.tune.maxrewrite - buf->to_forward - buf->o;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100184
185 if (buf->to_forward == BUF_INFINITE_FORWARD)
186 return 0;
187 if (ret <= 0)
188 return 0;
189 return ret;
190}
191
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100192/* Return the max number of bytes the buffer can contain so that once all the
193 * pending bytes are forwarded, the buffer still has global.tune.maxrewrite
194 * bytes free. The result sits between buf->size - maxrewrite and buf->size.
195 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100196static inline int buffer_max_len(const struct buffer *buf)
197{
198 return buf->size - buffer_reserved(buf);
199}
200
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200201/* Returns non-zero if the buffer input is considered full. The reserved space
202 * is taken into account if ->to_forward indicates that an end of transfer is
203 * close to happen. The test is optimized to avoid as many operations as
204 * possible for the fast case and to be used as an "if" condition.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100205 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200206static inline int bi_full(const struct buffer *b)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100207{
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200208 int rem = b->size;
209
210 rem -= b->o;
211 rem -= b->i;
212 if (!rem)
213 return 1; /* buffer already full */
214
215 if (b->to_forward >= b->size ||
216 (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->size)) && // just there to ensure gcc
217 b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second
218 return 0; // test whenever possible
219
220 rem -= global.tune.maxrewrite;
221 rem += b->o;
222 rem += b->to_forward;
223 return rem <= 0;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100224}
225
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200226/* Returns the amount of space available at the input of the buffer, taking the
227 * reserved space into account if ->to_forward indicates that an end of transfer
228 * is close to happen. The test is optimized to avoid as many operations as
229 * possible for the fast case.
230 */
231static inline int bi_avail(const struct buffer *b)
232{
233 int rem = b->size;
234 int rem2;
235
236 rem -= b->o;
237 rem -= b->i;
238 if (!rem)
239 return rem; /* buffer already full */
240
241 if (b->to_forward >= b->size ||
242 (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->size)) && // just there to ensure gcc
243 b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second
244 return rem; // test whenever possible
245
246 rem2 = rem - global.tune.maxrewrite;
247 rem2 += b->o;
248 rem2 += b->to_forward;
249
250 if (rem > rem2)
251 rem = rem2;
252 if (rem > 0)
253 return rem;
254 return 0;
255}
256
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100257/* Return the maximum amount of bytes that can be written into the buffer,
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200258 * including reserved space which may be overwritten.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100259 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200260static inline int buffer_total_space(const struct buffer *buf)
Willy Tarreau7c3c5412009-12-13 15:53:05 +0100261{
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200262 return buf->size - buffer_len(buf);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100263}
264
265/* Returns the number of contiguous bytes between <start> and <start>+<count>,
266 * and enforces a limit on buf->data + buf->size. <start> must be within the
267 * buffer.
268 */
269static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
270{
271 if (count > buf->data - start + buf->size)
272 count = buf->data - start + buf->size;
273 return count;
274}
275
276/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100277 * including reserved space which may be overwritten.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100278 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100279static inline int buffer_contig_space(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100280{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100281 const char *left, *right;
282
283 if (buf->data + buf->o <= buf->p)
284 right = buf->data + buf->size;
285 else
286 right = buf->p + buf->size - buf->o;
287
288 left = buffer_wrap_add(buf, buf->p + buf->i);
289 return right - left;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100290}
291
Willy Tarreau328582c2012-05-05 23:32:27 +0200292/* Advances the buffer by <adv> bytes, which means that the buffer
293 * pointer advances, and that as many bytes from in are transferred
294 * to out. The caller is responsible for ensuring that adv is always
295 * smaller than or equal to b->i. The BF_OUT_EMPTY flag is updated.
296 */
297static inline void b_adv(struct buffer *b, unsigned int adv)
298{
299 b->i -= adv;
300 b->o += adv;
301 if (b->o)
302 b->flags &= ~BF_OUT_EMPTY;
303 b->p = b_ptr(b, adv);
304}
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100305
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100306/* Return the amount of bytes that can be written into the buffer at once,
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100307 * excluding the amount of reserved space passed in <res>, which is
308 * preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100309 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100310static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100311{
312 /* Proceed differently if the buffer is full, partially used or empty.
313 * The hard situation is when it's partially used and either data or
314 * reserved space wraps at the end.
315 */
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100316 int spare = buf->size - res;
317
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100318 if (buffer_len(buf) >= spare)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100319 spare = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100320 else if (buffer_len(buf)) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100321 spare = buffer_contig_space(buf) - res;
322 if (spare < 0)
323 spare = 0;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100324 }
325 return spare;
326}
327
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100328
329/* Return the amount of bytes that can be written into the buffer at once,
330 * excluding reserved space, which is preserved.
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100331 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100332static inline int buffer_contig_space_res(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100333{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100334 return buffer_contig_space_with_res(buf, buffer_reserved(buf));
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100335}
336
337/* Normalizes a pointer which is supposed to be relative to the beginning of a
338 * buffer, so that wrapping is correctly handled. The intent is to use this
339 * when increasing a pointer. Note that the wrapping test is only performed
Willy Tarreau71730252011-11-28 16:04:29 +0100340 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100341 * otherwise an invalid pointer might be returned.
342 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100343static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100344{
Willy Tarreau71730252011-11-28 16:04:29 +0100345 if (ptr < buf->data)
346 ptr += buf->size;
347 else if (ptr - buf->size >= buf->data)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100348 ptr -= buf->size;
349 return ptr;
350}
351
352/* Returns the distance between two pointers, taking into account the ability
353 * to wrap around the buffer's end.
354 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100355static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100356{
357 int count = to - from;
358 if (count < 0)
359 count += buf->size;
360 return count;
361}
362
363/* returns the amount of pending bytes in the buffer. It is the amount of bytes
364 * that is not scheduled to be sent.
365 */
366static inline int buffer_pending(const struct buffer *buf)
367{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100368 return buf->i;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100369}
370
371/* Returns the size of the working area which the caller knows ends at <end>.
372 * If <end> equals buf->r (modulo size), then it means that the free area which
373 * follows is part of the working area. Otherwise, the working area stops at
Willy Tarreau89fa7062012-03-02 16:13:16 +0100374 * <end>. It always starts at buf->p. The work area includes the
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100375 * reserved area.
376 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100377static inline int buffer_work_area(const struct buffer *buf, const char *end)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100378{
379 end = buffer_pointer(buf, end);
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100380 if (end == buffer_wrap_add(buf, buf->p + buf->i))
381 /* pointer exactly at end, lets push forwards */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100382 end = buffer_wrap_sub(buf, buf->p - buf->o);
383 return buffer_count(buf, buf->p, end);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100384}
385
386/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
387static inline int buffer_almost_full(const struct buffer *buf)
388{
389 if (buffer_total_space(buf) < buf->size / 4)
390 return 1;
391 return 0;
392}
393
Willy Tarreau74b08c92010-09-08 17:04:31 +0200394/* Returns true if the buffer's input is already closed */
395static inline int buffer_input_closed(struct buffer *buf)
396{
397 return ((buf->flags & BF_SHUTR) != 0);
398}
399
400/* Returns true if the buffer's output is already closed */
401static inline int buffer_output_closed(struct buffer *buf)
402{
403 return ((buf->flags & BF_SHUTW) != 0);
404}
405
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200406/* Check buffer timeouts, and set the corresponding flags. The
407 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100408 * The read/write timeouts are not set if there was activity on the buffer.
409 * That way, we don't have to update the timeout on every I/O. Note that the
410 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200411 */
412static inline void buffer_check_timeouts(struct buffer *b)
413{
Willy Tarreau86491c32008-12-14 09:04:47 +0100414 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200415 unlikely(tick_is_expired(b->rex, now_ms)))
416 b->flags |= BF_READ_TIMEOUT;
417
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100418 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200419 unlikely(tick_is_expired(b->wex, now_ms)))
420 b->flags |= BF_WRITE_TIMEOUT;
421
422 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
423 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
424 b->flags |= BF_ANA_TIMEOUT;
425}
426
Willy Tarreau2e046c62012-03-01 16:08:30 +0100427/* Schedule all remaining buffer data to be sent. ->o is not touched if it
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100428 * already covers those data. That permits doing a flush even after a forward,
429 * although not recommended.
430 */
431static inline void buffer_flush(struct buffer *buf)
432{
Willy Tarreau363a5bb2012-03-02 20:14:45 +0100433 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100434 buf->o += buf->i;
435 buf->i = 0;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100436 if (buf->o)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200437 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100438}
439
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100440/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100441 * that any spliced data is not affected since we may not have any access to
442 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200443 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100444static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100446 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100447 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100448 buf->to_forward = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +0100449 buf->p = buf->data;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200450 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
451 if (!buf->pipe)
452 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453}
454
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200455/* Cut the "tail" of the buffer, which means strip it to the length of unsent
456 * data only, and kill any remaining unsent data. Any scheduled forwarding is
457 * stopped. This is mainly to be used to send error messages after existing
458 * data.
459 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200460static inline void bi_erase(struct buffer *buf)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200461{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100462 if (!buf->o)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200463 return buffer_erase(buf);
464
465 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100466 if (!buf->i)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200467 return;
468
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100469 buf->i = 0;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200470 buf->flags &= ~BF_FULL;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200471 if (bi_full(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200472 buf->flags |= BF_FULL;
473}
474
Willy Tarreauec1bc822012-03-09 15:03:30 +0100475/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
476 * call this function with remaining data waiting to be sent (o > 0). The
477 * caller must ensure that <n> is smaller than the actual buffer's length.
478 * This is mainly used to remove empty lines at the beginning of a request
479 * or a response.
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100480 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200481static inline void bi_fast_delete(struct buffer *buf, int n)
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100482{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100483 buf->i -= n;
Willy Tarreauec1bc822012-03-09 15:03:30 +0100484 buf->p += n;
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100485}
486
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200487/* marks the buffer as "shutdown" ASAP for reads */
488static inline void buffer_shutr_now(struct buffer *buf)
489{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100490 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200491}
492
493/* marks the buffer as "shutdown" ASAP for writes */
494static inline void buffer_shutw_now(struct buffer *buf)
495{
496 buf->flags |= BF_SHUTW_NOW;
497}
498
499/* marks the buffer as "shutdown" ASAP in both directions */
500static inline void buffer_abort(struct buffer *buf)
501{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100502 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100503 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200504}
505
Willy Tarreau01bf8672008-12-07 18:03:29 +0100506/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
507 * flag is set, and the function called once. The function is responsible for
508 * clearing the hijack bit. It is possible that the function clears the flag
509 * during this first call.
510 */
511static inline void buffer_install_hijacker(struct session *s,
512 struct buffer *b,
513 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200514{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100515 b->hijacker = func;
516 b->flags |= BF_HIJACK;
517 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200518}
519
Willy Tarreau01bf8672008-12-07 18:03:29 +0100520/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200521static inline void buffer_stop_hijack(struct buffer *buf)
522{
523 buf->flags &= ~BF_HIJACK;
524}
525
Willy Tarreau520d95e2009-09-19 21:04:57 +0200526/* allow the consumer to try to establish a new connection. */
527static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200528{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200529 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200530}
531
Willy Tarreau520d95e2009-09-19 21:04:57 +0200532/* prevent the consumer from trying to establish a new connection, and also
533 * disable auto shutdown forwarding.
534 */
535static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200536{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200537 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200538}
539
Willy Tarreau520d95e2009-09-19 21:04:57 +0200540/* allow the producer to forward shutdown requests */
541static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100542{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200543 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100544}
545
Willy Tarreau520d95e2009-09-19 21:04:57 +0200546/* prevent the producer from forwarding shutdown requests */
547static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100548{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200549 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100550}
551
Willy Tarreau90deb182010-01-07 00:20:41 +0100552/* allow the producer to read / poll the input */
553static inline void buffer_auto_read(struct buffer *buf)
554{
555 buf->flags &= ~BF_DONT_READ;
556}
557
558/* prevent the producer from read / poll the input */
559static inline void buffer_dont_read(struct buffer *buf)
560{
561 buf->flags |= BF_DONT_READ;
562}
563
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564/*
565 * Tries to realign the given buffer, and returns how many bytes can be written
566 * there at once without overwriting anything.
567 */
568static inline int buffer_realign(struct buffer *buf)
569{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100570 if (!(buf->i | buf->o)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 /* let's realign the buffer to optimize I/O */
Willy Tarreaua458b672012-03-05 11:17:50 +0100572 buf->p = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100574 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200575}
576
577/*
578 * Advance the buffer's read pointer by <len> bytes. This is useful when data
579 * have been read directly from the buffer. It is illegal to call this function
580 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e046c62012-03-01 16:08:30 +0100581 * responsibility to ensure that <len> is never larger than buf->o.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200582 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200583static inline void bo_skip(struct buffer *buf, int len)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200584{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100585 buf->o -= len;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200586 if (!buf->o && !buf->pipe)
587 buf->flags |= BF_OUT_EMPTY;
588
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100589 if (buffer_len(buf) == 0)
Willy Tarreaua458b672012-03-05 11:17:50 +0100590 buf->p = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200591
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200592 if (!bi_full(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200593 buf->flags &= ~BF_FULL;
594
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200595 /* notify that some data was written to the SI from the buffer */
596 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200597}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598
Willy Tarreau74b08c92010-09-08 17:04:31 +0200599/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100600 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200601 * closed, -2 is returned. If the block is too large for this buffer, -3 is
602 * returned. If there is not enough room left in the buffer, -1 is returned.
603 * Otherwise the number of bytes copied is returned (0 being a valid number).
604 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
605 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200606 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200607static inline int bi_putchk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200608{
609 int ret;
610
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200611 ret = bi_putblk(buf, chunk->str, chunk->len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200612 if (ret > 0)
613 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200614 return ret;
615}
616
Willy Tarreau74b08c92010-09-08 17:04:31 +0200617/* Tries to copy string <str> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100618 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200619 * closed, -2 is returned. If the block is too large for this buffer, -3 is
620 * returned. If there is not enough room left in the buffer, -1 is returned.
621 * Otherwise the number of bytes copied is returned (0 being a valid number).
622 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
623 * transferred.
624 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200625static inline int bi_putstr(struct buffer *buf, const char *str)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200626{
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200627 return bi_putblk(buf, str, strlen(str));
Willy Tarreau74b08c92010-09-08 17:04:31 +0200628}
629
630/*
631 * Return one char from the buffer. If the buffer is empty and closed, return -2.
632 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200633 * 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 +0100634 * Also note that this function respects the ->o limit.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200635 */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200636static inline int bo_getchr(struct buffer *buf)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200637{
638 /* closed or empty + imminent close = -2; empty = -1 */
639 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
640 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
641 return -2;
642 return -1;
643 }
Willy Tarreau89fa7062012-03-02 16:13:16 +0100644 return *buffer_wrap_sub(buf, buf->p - buf->o);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200645}
646
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100647/* This function writes the string <str> at position <pos> which must be in
648 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
649 * (l, r, lr) are updated to be valid after the shift. the shift value
650 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100651 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100652 * it does not make sense to use it on data scheduled to be sent.
653 */
654static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
655{
656 return buffer_replace2(b, pos, end, str, strlen(str));
657}
658
Willy Tarreau74b08c92010-09-08 17:04:31 +0200659/*
660 *
661 * Functions below are used to manage chunks
662 *
663 */
664
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200665static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
666 chk->str = str;
667 chk->len = 0;
668 chk->size = size;
669}
670
671/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200672static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200673
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200674 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200675 return 0;
676
677 chk->str = str;
678 chk->len = len;
679 chk->size = size;
680
681 return 1;
682}
683
684static inline void chunk_initstr(struct chunk *chk, char *str) {
685 chk->str = str;
686 chk->len = strlen(str);
687 chk->size = 0; /* mark it read-only */
688}
689
690static inline int chunk_strcpy(struct chunk *chk, const char *str) {
691 size_t len;
692
693 len = strlen(str);
694
695 if (unlikely(len > chk->size))
696 return 0;
697
698 chk->len = len;
699 memcpy(chk->str, str, len);
700
701 return 1;
702}
703
704int chunk_printf(struct chunk *chk, const char *fmt, ...)
705 __attribute__ ((format(printf, 2, 3)));
706
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200707int chunk_htmlencode(struct chunk *dst, struct chunk *src);
708int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
709
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200710static inline void chunk_reset(struct chunk *chk) {
711 chk->str = NULL;
712 chk->len = -1;
713 chk->size = 0;
714}
715
716static inline void chunk_destroy(struct chunk *chk) {
717
718 if (!chk->size)
719 return;
720
721 if (chk->str)
722 free(chk->str);
723
724 chunk_reset(chk);
725}
726
Willy Tarreau0f772532006-12-23 20:51:41 +0100727/*
728 * frees the destination chunk if already allocated, allocates a new string,
729 * and copies the source into it. The pointer to the destination string is
730 * returned, or NULL if the allocation fails or if any pointer is NULL..
731 */
732static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
733 if (!dst || !src || !src->str)
734 return NULL;
735 if (dst->str)
736 free(dst->str);
737 dst->len = src->len;
738 dst->str = (char *)malloc(dst->len);
739 memcpy(dst->str, src->str, dst->len);
740 return dst->str;
741}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742
743#endif /* _PROTO_BUFFERS_H */
744
745/*
746 * Local variables:
747 * c-indent-level: 8
748 * c-basic-offset: 8
749 * End:
750 */