blob: 18495b58fb568e96a42b3cbd6b6beb3a429fd976 [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 Tarreau2df28e82008-08-17 15:20:19 +020065 buf->r = buf->lr = buf->w = 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,
161 * including reserved space which may be overwritten. This version is optimized
162 * to reduce the amount of operations but it's not easy to understand as it is.
163 * Drawing a buffer with wrapping data on a paper helps a lot.
164 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100165static inline int buffer_contig_space(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100166{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100167 int space_from_end = buffer_len(buf) - (buf->r - buf->data);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100168 if (space_from_end < 0) /* data does not wrap */
169 space_from_end = buf->r - buf->data;
170 return buf->size - space_from_end;
171}
172
173/* Return the amount of bytes that can be written into the buffer at once,
174 * excluding reserved space, which is preserved. Same comment as above for
175 * the optimization leading to hardly understandable code.
176 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100177static inline int buffer_contig_space_res(const struct buffer *buf)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100178{
179 /* Proceed differently if the buffer is full, partially used or empty.
180 * The hard situation is when it's partially used and either data or
181 * reserved space wraps at the end.
182 */
183 int res = buffer_reserved(buf);
184 int spare = buf->size - res;
185
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100186 if (buffer_len(buf) >= spare)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100187 spare = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100188 else if (buffer_len(buf)) {
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100189 spare = buf->w - res - buf->r;
190 if (spare <= 0)
191 spare += buf->size;
192 spare = buffer_contig_area(buf, buf->r, spare);
193 }
194 return spare;
195}
196
197/* Same as above, but lets the caller pass the pre-computed value of
198 * buffer_reserved() in <res> if it already knows it, to save some
199 * computations.
200 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100201static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100202{
203 /* Proceed differently if the buffer is full, partially used or empty.
204 * The hard situation is when it's partially used and either data or
205 * reserved space wraps at the end.
206 */
207 int spare = buf->size - res;
208
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100209 if (buffer_len(buf) >= spare)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100210 spare = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100211 else if (buffer_len(buf)) {
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100212 spare = buf->w - res - buf->r;
213 if (spare <= 0)
214 spare += buf->size;
215 spare = buffer_contig_area(buf, buf->r, spare);
216 }
217 return spare;
218}
219
220/* Normalizes a pointer which is supposed to be relative to the beginning of a
221 * buffer, so that wrapping is correctly handled. The intent is to use this
222 * when increasing a pointer. Note that the wrapping test is only performed
Willy Tarreau71730252011-11-28 16:04:29 +0100223 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100224 * otherwise an invalid pointer might be returned.
225 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100226static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100227{
Willy Tarreau71730252011-11-28 16:04:29 +0100228 if (ptr < buf->data)
229 ptr += buf->size;
230 else if (ptr - buf->size >= buf->data)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100231 ptr -= buf->size;
232 return ptr;
233}
234
235/* Returns the distance between two pointers, taking into account the ability
236 * to wrap around the buffer's end.
237 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100238static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100239{
240 int count = to - from;
241 if (count < 0)
242 count += buf->size;
243 return count;
244}
245
246/* returns the amount of pending bytes in the buffer. It is the amount of bytes
247 * that is not scheduled to be sent.
248 */
249static inline int buffer_pending(const struct buffer *buf)
250{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100251 return buf->i;
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100252}
253
254/* Returns the size of the working area which the caller knows ends at <end>.
255 * If <end> equals buf->r (modulo size), then it means that the free area which
256 * follows is part of the working area. Otherwise, the working area stops at
Willy Tarreau2e046c62012-03-01 16:08:30 +0100257 * <end>. It always starts at buf->w+o. The work area includes the
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100258 * reserved area.
259 */
Willy Tarreau18dd41d2012-03-10 08:55:07 +0100260static inline int buffer_work_area(const struct buffer *buf, const char *end)
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100261{
262 end = buffer_pointer(buf, end);
263 if (end == buf->r) /* pointer exactly at end, lets push forwards */
264 end = buf->w;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100265 return buffer_count(buf, buffer_pointer(buf, buf->w + buf->o), end);
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100266}
267
268/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
269static inline int buffer_almost_full(const struct buffer *buf)
270{
271 if (buffer_total_space(buf) < buf->size / 4)
272 return 1;
273 return 0;
274}
275
Willy Tarreau74b08c92010-09-08 17:04:31 +0200276/* Returns true if the buffer's input is already closed */
277static inline int buffer_input_closed(struct buffer *buf)
278{
279 return ((buf->flags & BF_SHUTR) != 0);
280}
281
282/* Returns true if the buffer's output is already closed */
283static inline int buffer_output_closed(struct buffer *buf)
284{
285 return ((buf->flags & BF_SHUTW) != 0);
286}
287
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200288/* Check buffer timeouts, and set the corresponding flags. The
289 * likely/unlikely have been optimized for fastest normal path.
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100290 * The read/write timeouts are not set if there was activity on the buffer.
291 * That way, we don't have to update the timeout on every I/O. Note that the
292 * analyser timeout is always checked.
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200293 */
294static inline void buffer_check_timeouts(struct buffer *b)
295{
Willy Tarreau86491c32008-12-14 09:04:47 +0100296 if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200297 unlikely(tick_is_expired(b->rex, now_ms)))
298 b->flags |= BF_READ_TIMEOUT;
299
Willy Tarreaudd80c6f2008-12-13 22:25:59 +0100300 if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) &&
Willy Tarreau2eb52f02008-09-04 09:14:08 +0200301 unlikely(tick_is_expired(b->wex, now_ms)))
302 b->flags |= BF_WRITE_TIMEOUT;
303
304 if (likely(!(b->flags & BF_ANA_TIMEOUT)) &&
305 unlikely(tick_is_expired(b->analyse_exp, now_ms)))
306 b->flags |= BF_ANA_TIMEOUT;
307}
308
Willy Tarreau2e046c62012-03-01 16:08:30 +0100309/* Schedule all remaining buffer data to be sent. ->o is not touched if it
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100310 * already covers those data. That permits doing a flush even after a forward,
311 * although not recommended.
312 */
313static inline void buffer_flush(struct buffer *buf)
314{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100315 buf->o += buf->i;
316 buf->i = 0;
Willy Tarreau2e046c62012-03-01 16:08:30 +0100317 if (buf->o)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200318 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreaue8a28bf2009-03-08 21:12:04 +0100319}
320
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100321/* Erase any content from buffer <buf> and adjusts flags accordingly. Note
Willy Tarreau0abebcc2009-01-08 00:09:41 +0100322 * that any spliced data is not affected since we may not have any access to
323 * it.
Willy Tarreaue393fe22008-08-16 22:18:07 +0200324 */
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100325static inline void buffer_erase(struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100327 buf->o = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100328 buf->i = 0;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +0100329 buf->to_forward = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100330 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200331 buf->flags &= ~(BF_FULL | BF_OUT_EMPTY);
332 if (!buf->pipe)
333 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334}
335
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200336/* Cut the "tail" of the buffer, which means strip it to the length of unsent
337 * data only, and kill any remaining unsent data. Any scheduled forwarding is
338 * stopped. This is mainly to be used to send error messages after existing
339 * data.
340 */
341static inline void buffer_cut_tail(struct buffer *buf)
342{
Willy Tarreau2e046c62012-03-01 16:08:30 +0100343 if (!buf->o)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200344 return buffer_erase(buf);
345
346 buf->to_forward = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100347 if (!buf->i)
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200348 return;
349
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100350 buf->i = 0;
351 buf->r = buf->w + buf->o;
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200352 if (buf->r >= buf->data + buf->size)
353 buf->r -= buf->size;
354 buf->lr = buf->r;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200355 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100356 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau9cb8daa2009-09-15 21:22:24 +0200357 buf->flags |= BF_FULL;
358}
359
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100360/* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n>
361 * is smaller than the actual buffer's length. This is mainly used to remove
362 * empty lines at the beginning of a request or a response.
363 */
364static inline void buffer_ignore(struct buffer *buf, int n)
365{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100366 buf->i -= n;
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100367 buf->w += n;
368 if (buf->w >= buf->data + buf->size)
369 buf->w -= buf->size;
370 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100371 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreaud21e01c2009-12-27 15:45:38 +0100372 buf->flags |= BF_FULL;
373}
374
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200375/* marks the buffer as "shutdown" ASAP for reads */
376static inline void buffer_shutr_now(struct buffer *buf)
377{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100378 buf->flags |= BF_SHUTR_NOW;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200379}
380
381/* marks the buffer as "shutdown" ASAP for writes */
382static inline void buffer_shutw_now(struct buffer *buf)
383{
384 buf->flags |= BF_SHUTW_NOW;
385}
386
387/* marks the buffer as "shutdown" ASAP in both directions */
388static inline void buffer_abort(struct buffer *buf)
389{
Willy Tarreaufe3718a2008-11-30 18:14:12 +0100390 buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW;
Willy Tarreaue4599762010-03-21 23:25:09 +0100391 buf->flags &= ~BF_AUTO_CONNECT;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200392}
393
Willy Tarreau01bf8672008-12-07 18:03:29 +0100394/* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack
395 * flag is set, and the function called once. The function is responsible for
396 * clearing the hijack bit. It is possible that the function clears the flag
397 * during this first call.
398 */
399static inline void buffer_install_hijacker(struct session *s,
400 struct buffer *b,
401 void (*func)(struct session *, struct buffer *))
Willy Tarreau72b179a2008-08-28 16:01:32 +0200402{
Willy Tarreau01bf8672008-12-07 18:03:29 +0100403 b->hijacker = func;
404 b->flags |= BF_HIJACK;
405 func(s, b);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200406}
407
Willy Tarreau01bf8672008-12-07 18:03:29 +0100408/* Releases the buffer from hijacking mode. Often used by the hijack function */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200409static inline void buffer_stop_hijack(struct buffer *buf)
410{
411 buf->flags &= ~BF_HIJACK;
412}
413
Willy Tarreau520d95e2009-09-19 21:04:57 +0200414/* allow the consumer to try to establish a new connection. */
415static inline void buffer_auto_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200416{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200417 buf->flags |= BF_AUTO_CONNECT;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200418}
419
Willy Tarreau520d95e2009-09-19 21:04:57 +0200420/* prevent the consumer from trying to establish a new connection, and also
421 * disable auto shutdown forwarding.
422 */
423static inline void buffer_dont_connect(struct buffer *buf)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200424{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200425 buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE);
Willy Tarreau3da77c52008-08-29 09:58:42 +0200426}
427
Willy Tarreau520d95e2009-09-19 21:04:57 +0200428/* allow the producer to forward shutdown requests */
429static inline void buffer_auto_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100430{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200431 buf->flags |= BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100432}
433
Willy Tarreau520d95e2009-09-19 21:04:57 +0200434/* prevent the producer from forwarding shutdown requests */
435static inline void buffer_dont_close(struct buffer *buf)
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100436{
Willy Tarreau520d95e2009-09-19 21:04:57 +0200437 buf->flags &= ~BF_AUTO_CLOSE;
Willy Tarreau0a5d5dd2008-11-23 19:31:35 +0100438}
439
Willy Tarreau90deb182010-01-07 00:20:41 +0100440/* allow the producer to read / poll the input */
441static inline void buffer_auto_read(struct buffer *buf)
442{
443 buf->flags &= ~BF_DONT_READ;
444}
445
446/* prevent the producer from read / poll the input */
447static inline void buffer_dont_read(struct buffer *buf)
448{
449 buf->flags |= BF_DONT_READ;
450}
451
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452/*
453 * Tries to realign the given buffer, and returns how many bytes can be written
454 * there at once without overwriting anything.
455 */
456static inline int buffer_realign(struct buffer *buf)
457{
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100458 if (!(buf->i | buf->o)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100460 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 }
Willy Tarreau4b517ca2011-11-25 20:33:58 +0100462 return buffer_contig_space(buf);
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200463}
464
465/*
466 * Advance the buffer's read pointer by <len> bytes. This is useful when data
467 * have been read directly from the buffer. It is illegal to call this function
468 * with <len> causing a wrapping at the end of the buffer. It's the caller's
Willy Tarreau2e046c62012-03-01 16:08:30 +0100469 * responsibility to ensure that <len> is never larger than buf->o.
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200470 */
471static inline void buffer_skip(struct buffer *buf, int len)
472{
473 buf->w += len;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200474 if (buf->w >= buf->data + buf->size)
475 buf->w -= buf->size; /* wrap around the buffer */
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200476
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100477 buf->o -= len;
478 if (buffer_len(buf) == 0)
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200479 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200480
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100481 if (buffer_len(buf) < buffer_max_len(buf))
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200482 buf->flags &= ~BF_FULL;
483
Willy Tarreau2e046c62012-03-01 16:08:30 +0100484 if (!buf->o && !buf->pipe)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200485 buf->flags |= BF_OUT_EMPTY;
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200486
487 /* notify that some data was written to the SI from the buffer */
488 buf->flags |= BF_WRITE_PARTIAL;
Willy Tarreau2b7addc2009-08-31 07:37:22 +0200489}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200490
Willy Tarreauaeac3192009-08-31 08:09:57 +0200491/* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success,
492 * -2 if it is larger than the buffer size, or the number of bytes available
493 * otherwise. If the chunk has been written, its size is automatically reset
494 * to zero. The send limit is automatically adjusted with the amount of data
495 * written.
496 */
497static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
498{
499 int ret;
500
501 ret = buffer_write(buf, chunk->str, chunk->len);
502 if (ret == -1)
503 chunk->len = 0;
504 return ret;
505}
506
Willy Tarreau74b08c92010-09-08 17:04:31 +0200507/* Tries to copy chunk <chunk> into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100508 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200509 * closed, -2 is returned. If the block is too large for this buffer, -3 is
510 * returned. If there is not enough room left in the buffer, -1 is returned.
511 * Otherwise the number of bytes copied is returned (0 being a valid number).
512 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
513 * transferred. The chunk's length is updated with the number of bytes sent.
Willy Tarreauaeac3192009-08-31 08:09:57 +0200514 */
Willy Tarreau74b08c92010-09-08 17:04:31 +0200515static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk)
Willy Tarreauaeac3192009-08-31 08:09:57 +0200516{
517 int ret;
518
Willy Tarreau74b08c92010-09-08 17:04:31 +0200519 ret = buffer_put_block(buf, chunk->str, chunk->len);
520 if (ret > 0)
521 chunk->len -= ret;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200522 return ret;
523}
524
Willy Tarreau74b08c92010-09-08 17:04:31 +0200525/* Tries to copy string <str> at once into buffer <buf> after length controls.
Willy Tarreau2e046c62012-03-01 16:08:30 +0100526 * The ->o and to_forward pointers are updated. If the buffer's input is
Willy Tarreau74b08c92010-09-08 17:04:31 +0200527 * closed, -2 is returned. If the block is too large for this buffer, -3 is
528 * returned. If there is not enough room left in the buffer, -1 is returned.
529 * Otherwise the number of bytes copied is returned (0 being a valid number).
530 * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be
531 * transferred.
532 */
533static inline int buffer_put_string(struct buffer *buf, const char *str)
534{
535 return buffer_put_block(buf, str, strlen(str));
536}
537
538/*
539 * Return one char from the buffer. If the buffer is empty and closed, return -2.
540 * If the buffer is just empty, return -1. The buffer's pointer is not advanced,
541 * 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 +0100542 * Also note that this function respects the ->o limit.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200543 */
544static inline int buffer_get_char(struct buffer *buf)
545{
546 /* closed or empty + imminent close = -2; empty = -1 */
547 if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) {
548 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
549 return -2;
550 return -1;
551 }
552 return *buf->w;
553}
554
555
556/* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!!
557 * returns >= 0 if the buffer is too small to hold the message, -1 if the
558 * transfer was OK, -2 in case of failure.
559 */
560static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg)
561{
562 int ret = buffer_put_chunk(buf, msg);
563 if (ret >= 0) /* transfer OK */
564 return -1;
565 if (ret == -1) /* missing room */
566 return 1;
567 /* failure */
568 return -2;
569}
570
571/* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!!
572 * returns >= 0 if the buffer is too small to hold the message, -1 if the
573 * transfer was OK, -2 in case of failure.
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200574 */
575static inline int buffer_feed(struct buffer *buf, const char *str)
576{
Willy Tarreau74b08c92010-09-08 17:04:31 +0200577 int ret = buffer_put_string(buf, str);
578 if (ret >= 0) /* transfer OK */
579 return -1;
580 if (ret == -1) /* missing room */
581 return 1;
582 /* failure */
583 return -2;
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200584}
585
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100586
587/* This function writes the string <str> at position <pos> which must be in
588 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
589 * (l, r, lr) are updated to be valid after the shift. the shift value
590 * (positive or negative) is returned. If there's no space left, the move is
Willy Tarreau2e046c62012-03-01 16:08:30 +0100591 * not done. The function does not adjust ->o nor BF_OUT_EMPTY because
Willy Tarreau19ae56b2011-11-28 10:36:13 +0100592 * it does not make sense to use it on data scheduled to be sent.
593 */
594static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
595{
596 return buffer_replace2(b, pos, end, str, strlen(str));
597}
598
Willy Tarreau74b08c92010-09-08 17:04:31 +0200599/*
600 *
601 * Functions below are used to manage chunks
602 *
603 */
604
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200605static inline void chunk_init(struct chunk *chk, char *str, size_t size) {
606 chk->str = str;
607 chk->len = 0;
608 chk->size = size;
609}
610
611/* report 0 in case of error, 1 if OK. */
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200612static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200613
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +0200614 if (size && len > size)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200615 return 0;
616
617 chk->str = str;
618 chk->len = len;
619 chk->size = size;
620
621 return 1;
622}
623
624static inline void chunk_initstr(struct chunk *chk, char *str) {
625 chk->str = str;
626 chk->len = strlen(str);
627 chk->size = 0; /* mark it read-only */
628}
629
630static inline int chunk_strcpy(struct chunk *chk, const char *str) {
631 size_t len;
632
633 len = strlen(str);
634
635 if (unlikely(len > chk->size))
636 return 0;
637
638 chk->len = len;
639 memcpy(chk->str, str, len);
640
641 return 1;
642}
643
644int chunk_printf(struct chunk *chk, const char *fmt, ...)
645 __attribute__ ((format(printf, 2, 3)));
646
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200647int chunk_htmlencode(struct chunk *dst, struct chunk *src);
648int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
649
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200650static inline void chunk_reset(struct chunk *chk) {
651 chk->str = NULL;
652 chk->len = -1;
653 chk->size = 0;
654}
655
656static inline void chunk_destroy(struct chunk *chk) {
657
658 if (!chk->size)
659 return;
660
661 if (chk->str)
662 free(chk->str);
663
664 chunk_reset(chk);
665}
666
Willy Tarreau0f772532006-12-23 20:51:41 +0100667/*
668 * frees the destination chunk if already allocated, allocates a new string,
669 * and copies the source into it. The pointer to the destination string is
670 * returned, or NULL if the allocation fails or if any pointer is NULL..
671 */
672static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
673 if (!dst || !src || !src->str)
674 return NULL;
675 if (dst->str)
676 free(dst->str);
677 dst->len = src->len;
678 dst->str = (char *)malloc(dst->len);
679 memcpy(dst->str, src->str, dst->len);
680 return dst->str;
681}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
683#endif /* _PROTO_BUFFERS_H */
684
685/*
686 * Local variables:
687 * c-indent-level: 8
688 * c-basic-offset: 8
689 * End:
690 */