Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 2 | * include/proto/buffers.h |
| 3 | * Buffer management definitions, macros and inline functions. |
| 4 | * |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 6 | * |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _PROTO_BUFFERS_H |
| 23 | #define _PROTO_BUFFERS_H |
| 24 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 27 | #include <string.h> |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 29 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 31 | #include <common/ticks.h> |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 32 | #include <common/time.h> |
| 33 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <types/buffers.h> |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 35 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 37 | extern struct pool_head *pool2_buffer; |
| 38 | |
| 39 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 40 | int init_buffer(); |
| 41 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 42 | /* SI-to-buffer functions : buffer_{get,put}_{char,block,string,chunk} */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 43 | int bo_inject(struct buffer *buf, const char *msg, int len); |
| 44 | int bi_putblk(struct buffer *buf, const char *str, int len); |
| 45 | int bi_putchr(struct buffer *buf, char c); |
| 46 | int bo_getline(struct buffer *buf, char *str, int len); |
| 47 | int bo_getblk(struct buffer *buf, char *blk, int len, int offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 48 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 49 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
| 50 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 51 | void buffer_bounce_realign(struct buffer *buf); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 52 | unsigned long long buffer_forward(struct buffer *buf, unsigned long long bytes); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 54 | /* Initialize all fields in the buffer. The BF_OUT_EMPTY flags is set. */ |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 55 | static inline void buffer_init(struct buffer *buf) |
| 56 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 57 | buf->o = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 58 | buf->i = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 59 | buf->to_forward = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 60 | buf->total = 0; |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 61 | buf->pipe = NULL; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 62 | buf->analysers = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 63 | buf->cons = NULL; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 64 | buf->flags = BF_OUT_EMPTY; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 65 | buf->p = buf->data; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 68 | /*****************************************************************/ |
| 69 | /* These functions are used to compute various buffer area sizes */ |
| 70 | /*****************************************************************/ |
| 71 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 72 | /* 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 */ |
| 88 | static 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 | */ |
| 96 | static 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 */ |
| 106 | static 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 */ |
| 116 | static 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 */ |
| 126 | static 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 */ |
| 132 | static 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 Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 141 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 142 | static 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 */ |
| 148 | static 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 */ |
| 154 | static inline int buffer_empty(const struct buffer *buf) |
| 155 | { |
| 156 | return !buffer_not_empty(buf); |
| 157 | } |
| 158 | |
Willy Tarreau | 7fd758b | 2012-03-02 10:38:01 +0100 | [diff] [blame] | 159 | /* Normalizes a pointer after a subtract */ |
| 160 | static 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 */ |
| 168 | static 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 Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 175 | /* 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 | */ |
| 180 | static inline int buffer_reserved(const struct buffer *buf) |
| 181 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 182 | int ret = global.tune.maxrewrite - buf->to_forward - buf->o; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 183 | |
| 184 | if (buf->to_forward == BUF_INFINITE_FORWARD) |
| 185 | return 0; |
| 186 | if (ret <= 0) |
| 187 | return 0; |
| 188 | return ret; |
| 189 | } |
| 190 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 191 | /* 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 Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 195 | static inline int buffer_max_len(const struct buffer *buf) |
| 196 | { |
| 197 | return buf->size - buffer_reserved(buf); |
| 198 | } |
| 199 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 200 | /* Returns non-zero if the buffer input is considered full. The reserved space |
| 201 | * is taken into account if ->to_forward indicates that an end of transfer is |
| 202 | * close to happen. The test is optimized to avoid as many operations as |
| 203 | * possible for the fast case and to be used as an "if" condition. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 204 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 205 | static inline int bi_full(const struct buffer *b) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 206 | { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 207 | int rem = b->size; |
| 208 | |
| 209 | rem -= b->o; |
| 210 | rem -= b->i; |
| 211 | if (!rem) |
| 212 | return 1; /* buffer already full */ |
| 213 | |
| 214 | if (b->to_forward >= b->size || |
| 215 | (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->size)) && // just there to ensure gcc |
| 216 | b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second |
| 217 | return 0; // test whenever possible |
| 218 | |
| 219 | rem -= global.tune.maxrewrite; |
| 220 | rem += b->o; |
| 221 | rem += b->to_forward; |
| 222 | return rem <= 0; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 225 | /* Returns the amount of space available at the input of the buffer, taking the |
| 226 | * reserved space into account if ->to_forward indicates that an end of transfer |
| 227 | * is close to happen. The test is optimized to avoid as many operations as |
| 228 | * possible for the fast case. |
| 229 | */ |
| 230 | static inline int bi_avail(const struct buffer *b) |
| 231 | { |
| 232 | int rem = b->size; |
| 233 | int rem2; |
| 234 | |
| 235 | rem -= b->o; |
| 236 | rem -= b->i; |
| 237 | if (!rem) |
| 238 | return rem; /* buffer already full */ |
| 239 | |
| 240 | if (b->to_forward >= b->size || |
| 241 | (BUF_INFINITE_FORWARD < MAX_RANGE(typeof(b->size)) && // just there to ensure gcc |
| 242 | b->to_forward == BUF_INFINITE_FORWARD)) // avoids the useless second |
| 243 | return rem; // test whenever possible |
| 244 | |
| 245 | rem2 = rem - global.tune.maxrewrite; |
| 246 | rem2 += b->o; |
| 247 | rem2 += b->to_forward; |
| 248 | |
| 249 | if (rem > rem2) |
| 250 | rem = rem2; |
| 251 | if (rem > 0) |
| 252 | return rem; |
| 253 | return 0; |
| 254 | } |
| 255 | |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 256 | /* Return the maximum amount of bytes that can be written into the buffer, |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 257 | * including reserved space which may be overwritten. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 258 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 259 | static inline int buffer_total_space(const struct buffer *buf) |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 260 | { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 261 | return buf->size - buffer_len(buf); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* Returns the number of contiguous bytes between <start> and <start>+<count>, |
| 265 | * and enforces a limit on buf->data + buf->size. <start> must be within the |
| 266 | * buffer. |
| 267 | */ |
| 268 | static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count) |
| 269 | { |
| 270 | if (count > buf->data - start + buf->size) |
| 271 | count = buf->data - start + buf->size; |
| 272 | return count; |
| 273 | } |
| 274 | |
| 275 | /* Return the amount of bytes that can be written into the buffer at once, |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 276 | * including reserved space which may be overwritten. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 277 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 278 | static inline int buffer_contig_space(const struct buffer *buf) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 279 | { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 280 | const char *left, *right; |
| 281 | |
| 282 | if (buf->data + buf->o <= buf->p) |
| 283 | right = buf->data + buf->size; |
| 284 | else |
| 285 | right = buf->p + buf->size - buf->o; |
| 286 | |
| 287 | left = buffer_wrap_add(buf, buf->p + buf->i); |
| 288 | return right - left; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 291 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 292 | * pointer advances, and that as many bytes from in are transferred |
| 293 | * to out. The caller is responsible for ensuring that adv is always |
| 294 | * smaller than or equal to b->i. The BF_OUT_EMPTY flag is updated. |
| 295 | */ |
| 296 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 297 | { |
| 298 | b->i -= adv; |
| 299 | b->o += adv; |
| 300 | if (b->o) |
| 301 | b->flags &= ~BF_OUT_EMPTY; |
| 302 | b->p = b_ptr(b, adv); |
| 303 | } |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 304 | |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 305 | /* Return the amount of bytes that can be written into the buffer at once, |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 306 | * excluding the amount of reserved space passed in <res>, which is |
| 307 | * preserved. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 308 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 309 | static inline int buffer_contig_space_with_res(const struct buffer *buf, int res) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 310 | { |
| 311 | /* Proceed differently if the buffer is full, partially used or empty. |
| 312 | * The hard situation is when it's partially used and either data or |
| 313 | * reserved space wraps at the end. |
| 314 | */ |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 315 | int spare = buf->size - res; |
| 316 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 317 | if (buffer_len(buf) >= spare) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 318 | spare = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 319 | else if (buffer_len(buf)) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 320 | spare = buffer_contig_space(buf) - res; |
| 321 | if (spare < 0) |
| 322 | spare = 0; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 323 | } |
| 324 | return spare; |
| 325 | } |
| 326 | |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 327 | |
| 328 | /* Return the amount of bytes that can be written into the buffer at once, |
| 329 | * excluding reserved space, which is preserved. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 330 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 331 | static inline int buffer_contig_space_res(const struct buffer *buf) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 332 | { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 333 | return buffer_contig_space_with_res(buf, buffer_reserved(buf)); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 337 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 338 | * when increasing a pointer. Note that the wrapping test is only performed |
Willy Tarreau | 7173025 | 2011-11-28 16:04:29 +0100 | [diff] [blame] | 339 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 340 | * otherwise an invalid pointer might be returned. |
| 341 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 342 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 343 | { |
Willy Tarreau | 7173025 | 2011-11-28 16:04:29 +0100 | [diff] [blame] | 344 | if (ptr < buf->data) |
| 345 | ptr += buf->size; |
| 346 | else if (ptr - buf->size >= buf->data) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 347 | ptr -= buf->size; |
| 348 | return ptr; |
| 349 | } |
| 350 | |
| 351 | /* Returns the distance between two pointers, taking into account the ability |
| 352 | * to wrap around the buffer's end. |
| 353 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 354 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 355 | { |
| 356 | int count = to - from; |
| 357 | if (count < 0) |
| 358 | count += buf->size; |
| 359 | return count; |
| 360 | } |
| 361 | |
| 362 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 363 | * that is not scheduled to be sent. |
| 364 | */ |
| 365 | static inline int buffer_pending(const struct buffer *buf) |
| 366 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 367 | return buf->i; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Returns the size of the working area which the caller knows ends at <end>. |
| 371 | * If <end> equals buf->r (modulo size), then it means that the free area which |
| 372 | * follows is part of the working area. Otherwise, the working area stops at |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 373 | * <end>. It always starts at buf->p. The work area includes the |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 374 | * reserved area. |
| 375 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 376 | static inline int buffer_work_area(const struct buffer *buf, const char *end) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 377 | { |
| 378 | end = buffer_pointer(buf, end); |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 379 | if (end == buffer_wrap_add(buf, buf->p + buf->i)) |
| 380 | /* pointer exactly at end, lets push forwards */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 381 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 382 | return buffer_count(buf, buf->p, end); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 386 | static inline int buffer_almost_full(const struct buffer *buf) |
| 387 | { |
| 388 | if (buffer_total_space(buf) < buf->size / 4) |
| 389 | return 1; |
| 390 | return 0; |
| 391 | } |
| 392 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 393 | /* Returns true if the buffer's input is already closed */ |
| 394 | static inline int buffer_input_closed(struct buffer *buf) |
| 395 | { |
| 396 | return ((buf->flags & BF_SHUTR) != 0); |
| 397 | } |
| 398 | |
| 399 | /* Returns true if the buffer's output is already closed */ |
| 400 | static inline int buffer_output_closed(struct buffer *buf) |
| 401 | { |
| 402 | return ((buf->flags & BF_SHUTW) != 0); |
| 403 | } |
| 404 | |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 405 | /* Check buffer timeouts, and set the corresponding flags. The |
| 406 | * likely/unlikely have been optimized for fastest normal path. |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 407 | * The read/write timeouts are not set if there was activity on the buffer. |
| 408 | * That way, we don't have to update the timeout on every I/O. Note that the |
| 409 | * analyser timeout is always checked. |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 410 | */ |
| 411 | static inline void buffer_check_timeouts(struct buffer *b) |
| 412 | { |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 413 | if (likely(!(b->flags & (BF_SHUTR|BF_READ_TIMEOUT|BF_READ_ACTIVITY|BF_READ_NOEXP))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 414 | unlikely(tick_is_expired(b->rex, now_ms))) |
| 415 | b->flags |= BF_READ_TIMEOUT; |
| 416 | |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 417 | if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 418 | unlikely(tick_is_expired(b->wex, now_ms))) |
| 419 | b->flags |= BF_WRITE_TIMEOUT; |
| 420 | |
| 421 | if (likely(!(b->flags & BF_ANA_TIMEOUT)) && |
| 422 | unlikely(tick_is_expired(b->analyse_exp, now_ms))) |
| 423 | b->flags |= BF_ANA_TIMEOUT; |
| 424 | } |
| 425 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 426 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 427 | * already covers those data. That permits doing a flush even after a forward, |
| 428 | * although not recommended. |
| 429 | */ |
| 430 | static inline void buffer_flush(struct buffer *buf) |
| 431 | { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 432 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 433 | buf->o += buf->i; |
| 434 | buf->i = 0; |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 435 | if (buf->o) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 436 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 439 | /* Erase any content from buffer <buf> and adjusts flags accordingly. Note |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 440 | * that any spliced data is not affected since we may not have any access to |
| 441 | * it. |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 442 | */ |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 443 | static inline void buffer_erase(struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 444 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 445 | buf->o = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 446 | buf->i = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 447 | buf->to_forward = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 448 | buf->p = buf->data; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 449 | buf->flags &= ~(BF_FULL | BF_OUT_EMPTY); |
| 450 | if (!buf->pipe) |
| 451 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 454 | /* Cut the "tail" of the buffer, which means strip it to the length of unsent |
| 455 | * data only, and kill any remaining unsent data. Any scheduled forwarding is |
| 456 | * stopped. This is mainly to be used to send error messages after existing |
| 457 | * data. |
| 458 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 459 | static inline void bi_erase(struct buffer *buf) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 460 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 461 | if (!buf->o) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 462 | return buffer_erase(buf); |
| 463 | |
| 464 | buf->to_forward = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 465 | if (!buf->i) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 466 | return; |
| 467 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 468 | buf->i = 0; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 469 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 470 | if (bi_full(buf)) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 471 | buf->flags |= BF_FULL; |
| 472 | } |
| 473 | |
Willy Tarreau | ec1bc82 | 2012-03-09 15:03:30 +0100 | [diff] [blame] | 474 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 475 | * call this function with remaining data waiting to be sent (o > 0). The |
| 476 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 477 | * This is mainly used to remove empty lines at the beginning of a request |
| 478 | * or a response. |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 479 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 480 | static inline void bi_fast_delete(struct buffer *buf, int n) |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 481 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 482 | buf->i -= n; |
Willy Tarreau | ec1bc82 | 2012-03-09 15:03:30 +0100 | [diff] [blame] | 483 | buf->p += n; |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 486 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 487 | static inline void buffer_shutr_now(struct buffer *buf) |
| 488 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 489 | buf->flags |= BF_SHUTR_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 493 | static inline void buffer_shutw_now(struct buffer *buf) |
| 494 | { |
| 495 | buf->flags |= BF_SHUTW_NOW; |
| 496 | } |
| 497 | |
| 498 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 499 | static inline void buffer_abort(struct buffer *buf) |
| 500 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 501 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 502 | buf->flags &= ~BF_AUTO_CONNECT; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 505 | /* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack |
| 506 | * flag is set, and the function called once. The function is responsible for |
| 507 | * clearing the hijack bit. It is possible that the function clears the flag |
| 508 | * during this first call. |
| 509 | */ |
| 510 | static inline void buffer_install_hijacker(struct session *s, |
| 511 | struct buffer *b, |
| 512 | void (*func)(struct session *, struct buffer *)) |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 513 | { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 514 | b->hijacker = func; |
| 515 | b->flags |= BF_HIJACK; |
| 516 | func(s, b); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 519 | /* Releases the buffer from hijacking mode. Often used by the hijack function */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 520 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 521 | { |
| 522 | buf->flags &= ~BF_HIJACK; |
| 523 | } |
| 524 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 525 | /* allow the consumer to try to establish a new connection. */ |
| 526 | static inline void buffer_auto_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 527 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 528 | buf->flags |= BF_AUTO_CONNECT; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 531 | /* prevent the consumer from trying to establish a new connection, and also |
| 532 | * disable auto shutdown forwarding. |
| 533 | */ |
| 534 | static inline void buffer_dont_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 535 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 536 | buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 539 | /* allow the producer to forward shutdown requests */ |
| 540 | static inline void buffer_auto_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 541 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 542 | buf->flags |= BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 543 | } |
| 544 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 545 | /* prevent the producer from forwarding shutdown requests */ |
| 546 | static inline void buffer_dont_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 547 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 548 | buf->flags &= ~BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 549 | } |
| 550 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 551 | /* allow the producer to read / poll the input */ |
| 552 | static inline void buffer_auto_read(struct buffer *buf) |
| 553 | { |
| 554 | buf->flags &= ~BF_DONT_READ; |
| 555 | } |
| 556 | |
| 557 | /* prevent the producer from read / poll the input */ |
| 558 | static inline void buffer_dont_read(struct buffer *buf) |
| 559 | { |
| 560 | buf->flags |= BF_DONT_READ; |
| 561 | } |
| 562 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 563 | /* |
| 564 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 565 | * there at once without overwriting anything. |
| 566 | */ |
| 567 | static inline int buffer_realign(struct buffer *buf) |
| 568 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 569 | if (!(buf->i | buf->o)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 571 | buf->p = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 572 | } |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 573 | return buffer_contig_space(buf); |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Advance the buffer's read pointer by <len> bytes. This is useful when data |
| 578 | * have been read directly from the buffer. It is illegal to call this function |
| 579 | * with <len> causing a wrapping at the end of the buffer. It's the caller's |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 580 | * responsibility to ensure that <len> is never larger than buf->o. |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 581 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 582 | static inline void bo_skip(struct buffer *buf, int len) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 583 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 584 | buf->o -= len; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 585 | if (!buf->o && !buf->pipe) |
| 586 | buf->flags |= BF_OUT_EMPTY; |
| 587 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 588 | if (buffer_len(buf) == 0) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 589 | buf->p = buf->data; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 591 | if (!bi_full(buf)) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 592 | buf->flags &= ~BF_FULL; |
| 593 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 594 | /* notify that some data was written to the SI from the buffer */ |
| 595 | buf->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 596 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 597 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 598 | /* Tries to copy chunk <chunk> into buffer <buf> after length controls. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 599 | * The ->o and to_forward pointers are updated. If the buffer's input is |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 600 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 601 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 602 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 603 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 604 | * transferred. The chunk's length is updated with the number of bytes sent. |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 605 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 606 | static inline int bi_putchk(struct buffer *buf, struct chunk *chunk) |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 607 | { |
| 608 | int ret; |
| 609 | |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 610 | ret = bi_putblk(buf, chunk->str, chunk->len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 611 | if (ret > 0) |
| 612 | chunk->len -= ret; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 613 | return ret; |
| 614 | } |
| 615 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 616 | /* Tries to copy string <str> at once into buffer <buf> after length controls. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 617 | * The ->o and to_forward pointers are updated. If the buffer's input is |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 618 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 619 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 620 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 621 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 622 | * transferred. |
| 623 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 624 | static inline int bi_putstr(struct buffer *buf, const char *str) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 625 | { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 626 | return bi_putblk(buf, str, strlen(str)); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Return one char from the buffer. If the buffer is empty and closed, return -2. |
| 631 | * If the buffer is just empty, return -1. The buffer's pointer is not advanced, |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 632 | * it's up to the caller to call bo_skip(buf, 1) when it has consumed the char. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 633 | * Also note that this function respects the ->o limit. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 634 | */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 635 | static inline int bo_getchr(struct buffer *buf) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 636 | { |
| 637 | /* closed or empty + imminent close = -2; empty = -1 */ |
| 638 | if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) { |
| 639 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 640 | return -2; |
| 641 | return -1; |
| 642 | } |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 643 | return *buffer_wrap_sub(buf, buf->p - buf->o); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 646 | /* This function writes the string <str> at position <pos> which must be in |
| 647 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 648 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 649 | * (positive or negative) is returned. If there's no space left, the move is |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 650 | * not done. The function does not adjust ->o nor BF_OUT_EMPTY because |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 651 | * it does not make sense to use it on data scheduled to be sent. |
| 652 | */ |
| 653 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 654 | { |
| 655 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 656 | } |
| 657 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 658 | /* |
| 659 | * |
| 660 | * Functions below are used to manage chunks |
| 661 | * |
| 662 | */ |
| 663 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 664 | static inline void chunk_init(struct chunk *chk, char *str, size_t size) { |
| 665 | chk->str = str; |
| 666 | chk->len = 0; |
| 667 | chk->size = size; |
| 668 | } |
| 669 | |
| 670 | /* report 0 in case of error, 1 if OK. */ |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 671 | static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len) { |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 672 | |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 673 | if (size && len > size) |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 674 | return 0; |
| 675 | |
| 676 | chk->str = str; |
| 677 | chk->len = len; |
| 678 | chk->size = size; |
| 679 | |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | static inline void chunk_initstr(struct chunk *chk, char *str) { |
| 684 | chk->str = str; |
| 685 | chk->len = strlen(str); |
| 686 | chk->size = 0; /* mark it read-only */ |
| 687 | } |
| 688 | |
| 689 | static inline int chunk_strcpy(struct chunk *chk, const char *str) { |
| 690 | size_t len; |
| 691 | |
| 692 | len = strlen(str); |
| 693 | |
| 694 | if (unlikely(len > chk->size)) |
| 695 | return 0; |
| 696 | |
| 697 | chk->len = len; |
| 698 | memcpy(chk->str, str, len); |
| 699 | |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 704 | __attribute__ ((format(printf, 2, 3))); |
| 705 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 706 | int chunk_htmlencode(struct chunk *dst, struct chunk *src); |
| 707 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc); |
| 708 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 709 | static inline void chunk_reset(struct chunk *chk) { |
| 710 | chk->str = NULL; |
| 711 | chk->len = -1; |
| 712 | chk->size = 0; |
| 713 | } |
| 714 | |
| 715 | static inline void chunk_destroy(struct chunk *chk) { |
| 716 | |
| 717 | if (!chk->size) |
| 718 | return; |
| 719 | |
| 720 | if (chk->str) |
| 721 | free(chk->str); |
| 722 | |
| 723 | chunk_reset(chk); |
| 724 | } |
| 725 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 726 | /* |
| 727 | * frees the destination chunk if already allocated, allocates a new string, |
| 728 | * and copies the source into it. The pointer to the destination string is |
| 729 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 730 | */ |
| 731 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 732 | if (!dst || !src || !src->str) |
| 733 | return NULL; |
| 734 | if (dst->str) |
| 735 | free(dst->str); |
| 736 | dst->len = src->len; |
| 737 | dst->str = (char *)malloc(dst->len); |
| 738 | memcpy(dst->str, src->str, dst->len); |
| 739 | return dst->str; |
| 740 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 741 | |
| 742 | #endif /* _PROTO_BUFFERS_H */ |
| 743 | |
| 744 | /* |
| 745 | * Local variables: |
| 746 | * c-indent-level: 8 |
| 747 | * c-basic-offset: 8 |
| 748 | * End: |
| 749 | */ |