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 | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2010 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} */ |
| 43 | int buffer_write(struct buffer *buf, const char *msg, int len); |
| 44 | int buffer_put_block(struct buffer *buf, const char *str, int len); |
| 45 | int buffer_put_char(struct buffer *buf, char c); |
| 46 | int buffer_get_line(struct buffer *buf, char *str, int len); |
| 47 | int buffer_get_block(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 | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 65 | buf->r = buf->lr = buf->w = 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 | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 72 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 73 | static 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 */ |
| 79 | static 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 */ |
| 85 | static inline int buffer_empty(const struct buffer *buf) |
| 86 | { |
| 87 | return !buffer_not_empty(buf); |
| 88 | } |
| 89 | |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 90 | /* Return the number of reserved bytes in the buffer, which ensures that once |
| 91 | * all pending data are forwarded, the buffer still has global.tune.maxrewrite |
| 92 | * bytes free. The result is between 0 and global.maxrewrite, which is itself |
| 93 | * smaller than any buf->size. |
| 94 | */ |
| 95 | static inline int buffer_reserved(const struct buffer *buf) |
| 96 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 97 | int ret = global.tune.maxrewrite - buf->to_forward - buf->o; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 98 | |
| 99 | if (buf->to_forward == BUF_INFINITE_FORWARD) |
| 100 | return 0; |
| 101 | if (ret <= 0) |
| 102 | return 0; |
| 103 | return ret; |
| 104 | } |
| 105 | |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 106 | /* Return the max number of bytes the buffer can contain so that once all the |
| 107 | * pending bytes are forwarded, the buffer still has global.tune.maxrewrite |
| 108 | * bytes free. The result sits between buf->size - maxrewrite and buf->size. |
| 109 | */ |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 110 | static inline int buffer_max_len(const struct buffer *buf) |
| 111 | { |
| 112 | return buf->size - buffer_reserved(buf); |
| 113 | } |
| 114 | |
| 115 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 116 | * including reserved space which may be overwritten. |
| 117 | */ |
| 118 | static inline int buffer_total_space(const struct buffer *buf) |
| 119 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 120 | return buf->size - buffer_len(buf); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Return the maximum amount of bytes that can be written into the buffer, |
Willy Tarreau | fe4b1f9 | 2011-11-28 13:40:49 +0100 | [diff] [blame] | 124 | * excluding the reserved space, which is preserved. 0 may be returned if |
| 125 | * the reserved space was already reached or used. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 126 | */ |
| 127 | static inline int buffer_total_space_res(const struct buffer *buf) |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 128 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 129 | int len = buffer_max_len(buf) - buffer_len(buf); |
Willy Tarreau | fe4b1f9 | 2011-11-28 13:40:49 +0100 | [diff] [blame] | 130 | return len < 0 ? 0 : len; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* Returns the number of contiguous bytes between <start> and <start>+<count>, |
| 134 | * and enforces a limit on buf->data + buf->size. <start> must be within the |
| 135 | * buffer. |
| 136 | */ |
| 137 | static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count) |
| 138 | { |
| 139 | if (count > buf->data - start + buf->size) |
| 140 | count = buf->data - start + buf->size; |
| 141 | return count; |
| 142 | } |
| 143 | |
| 144 | /* Return the amount of bytes that can be written into the buffer at once, |
| 145 | * including reserved space which may be overwritten. This version is optimized |
| 146 | * to reduce the amount of operations but it's not easy to understand as it is. |
| 147 | * Drawing a buffer with wrapping data on a paper helps a lot. |
| 148 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 149 | static inline int buffer_contig_space(const struct buffer *buf) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 150 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 151 | int space_from_end = buffer_len(buf) - (buf->r - buf->data); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 152 | if (space_from_end < 0) /* data does not wrap */ |
| 153 | space_from_end = buf->r - buf->data; |
| 154 | return buf->size - space_from_end; |
| 155 | } |
| 156 | |
| 157 | /* Return the amount of bytes that can be written into the buffer at once, |
| 158 | * excluding reserved space, which is preserved. Same comment as above for |
| 159 | * the optimization leading to hardly understandable code. |
| 160 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 161 | static inline int buffer_contig_space_res(const struct buffer *buf) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 162 | { |
| 163 | /* Proceed differently if the buffer is full, partially used or empty. |
| 164 | * The hard situation is when it's partially used and either data or |
| 165 | * reserved space wraps at the end. |
| 166 | */ |
| 167 | int res = buffer_reserved(buf); |
| 168 | int spare = buf->size - res; |
| 169 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 170 | if (buffer_len(buf) >= spare) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 171 | spare = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 172 | else if (buffer_len(buf)) { |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 173 | spare = buf->w - res - buf->r; |
| 174 | if (spare <= 0) |
| 175 | spare += buf->size; |
| 176 | spare = buffer_contig_area(buf, buf->r, spare); |
| 177 | } |
| 178 | return spare; |
| 179 | } |
| 180 | |
| 181 | /* Same as above, but lets the caller pass the pre-computed value of |
| 182 | * buffer_reserved() in <res> if it already knows it, to save some |
| 183 | * computations. |
| 184 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 185 | 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] | 186 | { |
| 187 | /* Proceed differently if the buffer is full, partially used or empty. |
| 188 | * The hard situation is when it's partially used and either data or |
| 189 | * reserved space wraps at the end. |
| 190 | */ |
| 191 | int spare = buf->size - res; |
| 192 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 193 | if (buffer_len(buf) >= spare) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 194 | spare = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 195 | else if (buffer_len(buf)) { |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 196 | spare = buf->w - res - buf->r; |
| 197 | if (spare <= 0) |
| 198 | spare += buf->size; |
| 199 | spare = buffer_contig_area(buf, buf->r, spare); |
| 200 | } |
| 201 | return spare; |
| 202 | } |
| 203 | |
| 204 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 205 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 206 | * when increasing a pointer. Note that the wrapping test is only performed |
Willy Tarreau | 7173025 | 2011-11-28 16:04:29 +0100 | [diff] [blame] | 207 | * 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] | 208 | * otherwise an invalid pointer might be returned. |
| 209 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 210 | 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] | 211 | { |
Willy Tarreau | 7173025 | 2011-11-28 16:04:29 +0100 | [diff] [blame] | 212 | if (ptr < buf->data) |
| 213 | ptr += buf->size; |
| 214 | else if (ptr - buf->size >= buf->data) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 215 | ptr -= buf->size; |
| 216 | return ptr; |
| 217 | } |
| 218 | |
| 219 | /* Returns the distance between two pointers, taking into account the ability |
| 220 | * to wrap around the buffer's end. |
| 221 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 222 | 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] | 223 | { |
| 224 | int count = to - from; |
| 225 | if (count < 0) |
| 226 | count += buf->size; |
| 227 | return count; |
| 228 | } |
| 229 | |
| 230 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 231 | * that is not scheduled to be sent. |
| 232 | */ |
| 233 | static inline int buffer_pending(const struct buffer *buf) |
| 234 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 235 | return buf->i; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* Returns the size of the working area which the caller knows ends at <end>. |
| 239 | * If <end> equals buf->r (modulo size), then it means that the free area which |
| 240 | * follows is part of the working area. Otherwise, the working area stops at |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 241 | * <end>. It always starts at buf->w+o. The work area includes the |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 242 | * reserved area. |
| 243 | */ |
Willy Tarreau | 18dd41d | 2012-03-10 08:55:07 +0100 | [diff] [blame] | 244 | 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] | 245 | { |
| 246 | end = buffer_pointer(buf, end); |
| 247 | if (end == buf->r) /* pointer exactly at end, lets push forwards */ |
| 248 | end = buf->w; |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 249 | return buffer_count(buf, buffer_pointer(buf, buf->w + buf->o), end); |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 253 | static inline int buffer_almost_full(const struct buffer *buf) |
| 254 | { |
| 255 | if (buffer_total_space(buf) < buf->size / 4) |
| 256 | return 1; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Return the max amount of bytes that can be read from the buffer at once. |
| 262 | * Note that this may be lower than the actual buffer length when the data |
| 263 | * wrap after the end, so it's preferable to call this function again after |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 264 | * reading. Also note that this function respects the ->o limit. |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 265 | */ |
| 266 | static inline int buffer_contig_data(struct buffer *buf) |
| 267 | { |
| 268 | int ret; |
| 269 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 270 | if (!buf->o) |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 271 | return 0; |
| 272 | |
| 273 | if (buf->r > buf->w) |
| 274 | ret = buf->r - buf->w; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 275 | else |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 276 | ret = buf->data + buf->size - buf->w; |
| 277 | |
| 278 | /* limit the amount of outgoing data if required */ |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 279 | if (ret > buf->o) |
| 280 | ret = buf->o; |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 281 | |
| 282 | return ret; |
Willy Tarreau | 7c3c541 | 2009-12-13 15:53:05 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 285 | /* Returns true if the buffer's input is already closed */ |
| 286 | static inline int buffer_input_closed(struct buffer *buf) |
| 287 | { |
| 288 | return ((buf->flags & BF_SHUTR) != 0); |
| 289 | } |
| 290 | |
| 291 | /* Returns true if the buffer's output is already closed */ |
| 292 | static inline int buffer_output_closed(struct buffer *buf) |
| 293 | { |
| 294 | return ((buf->flags & BF_SHUTW) != 0); |
| 295 | } |
| 296 | |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 297 | /* Check buffer timeouts, and set the corresponding flags. The |
| 298 | * likely/unlikely have been optimized for fastest normal path. |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 299 | * The read/write timeouts are not set if there was activity on the buffer. |
| 300 | * That way, we don't have to update the timeout on every I/O. Note that the |
| 301 | * analyser timeout is always checked. |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 302 | */ |
| 303 | static inline void buffer_check_timeouts(struct buffer *b) |
| 304 | { |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 305 | 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] | 306 | unlikely(tick_is_expired(b->rex, now_ms))) |
| 307 | b->flags |= BF_READ_TIMEOUT; |
| 308 | |
Willy Tarreau | dd80c6f | 2008-12-13 22:25:59 +0100 | [diff] [blame] | 309 | if (likely(!(b->flags & (BF_SHUTW|BF_WRITE_TIMEOUT|BF_WRITE_ACTIVITY))) && |
Willy Tarreau | 2eb52f0 | 2008-09-04 09:14:08 +0200 | [diff] [blame] | 310 | unlikely(tick_is_expired(b->wex, now_ms))) |
| 311 | b->flags |= BF_WRITE_TIMEOUT; |
| 312 | |
| 313 | if (likely(!(b->flags & BF_ANA_TIMEOUT)) && |
| 314 | unlikely(tick_is_expired(b->analyse_exp, now_ms))) |
| 315 | b->flags |= BF_ANA_TIMEOUT; |
| 316 | } |
| 317 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 318 | /* 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] | 319 | * already covers those data. That permits doing a flush even after a forward, |
| 320 | * although not recommended. |
| 321 | */ |
| 322 | static inline void buffer_flush(struct buffer *buf) |
| 323 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 324 | buf->o += buf->i; |
| 325 | buf->i = 0; |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 326 | if (buf->o) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 327 | buf->flags &= ~BF_OUT_EMPTY; |
Willy Tarreau | e8a28bf | 2009-03-08 21:12:04 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 330 | /* Erase any content from buffer <buf> and adjusts flags accordingly. Note |
Willy Tarreau | 0abebcc | 2009-01-08 00:09:41 +0100 | [diff] [blame] | 331 | * that any spliced data is not affected since we may not have any access to |
| 332 | * it. |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 333 | */ |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 334 | static inline void buffer_erase(struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 335 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 336 | buf->o = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 337 | buf->i = 0; |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 338 | buf->to_forward = 0; |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 339 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 340 | buf->flags &= ~(BF_FULL | BF_OUT_EMPTY); |
| 341 | if (!buf->pipe) |
| 342 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 345 | /* Cut the "tail" of the buffer, which means strip it to the length of unsent |
| 346 | * data only, and kill any remaining unsent data. Any scheduled forwarding is |
| 347 | * stopped. This is mainly to be used to send error messages after existing |
| 348 | * data. |
| 349 | */ |
| 350 | static inline void buffer_cut_tail(struct buffer *buf) |
| 351 | { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 352 | if (!buf->o) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 353 | return buffer_erase(buf); |
| 354 | |
| 355 | buf->to_forward = 0; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 356 | if (!buf->i) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 357 | return; |
| 358 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 359 | buf->i = 0; |
| 360 | buf->r = buf->w + buf->o; |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 361 | if (buf->r >= buf->data + buf->size) |
| 362 | buf->r -= buf->size; |
| 363 | buf->lr = buf->r; |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 364 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 365 | if (buffer_len(buf) >= buffer_max_len(buf)) |
Willy Tarreau | 9cb8daa | 2009-09-15 21:22:24 +0200 | [diff] [blame] | 366 | buf->flags |= BF_FULL; |
| 367 | } |
| 368 | |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 369 | /* Cut the <n> next unsent bytes of the buffer. The caller must ensure that <n> |
| 370 | * is smaller than the actual buffer's length. This is mainly used to remove |
| 371 | * empty lines at the beginning of a request or a response. |
| 372 | */ |
| 373 | static inline void buffer_ignore(struct buffer *buf, int n) |
| 374 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 375 | buf->i -= n; |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 376 | buf->w += n; |
| 377 | if (buf->w >= buf->data + buf->size) |
| 378 | buf->w -= buf->size; |
| 379 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 380 | if (buffer_len(buf) >= buffer_max_len(buf)) |
Willy Tarreau | d21e01c | 2009-12-27 15:45:38 +0100 | [diff] [blame] | 381 | buf->flags |= BF_FULL; |
| 382 | } |
| 383 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 384 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 385 | static inline void buffer_shutr_now(struct buffer *buf) |
| 386 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 387 | buf->flags |= BF_SHUTR_NOW; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 391 | static inline void buffer_shutw_now(struct buffer *buf) |
| 392 | { |
| 393 | buf->flags |= BF_SHUTW_NOW; |
| 394 | } |
| 395 | |
| 396 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 397 | static inline void buffer_abort(struct buffer *buf) |
| 398 | { |
Willy Tarreau | fe3718a | 2008-11-30 18:14:12 +0100 | [diff] [blame] | 399 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
Willy Tarreau | e459976 | 2010-03-21 23:25:09 +0100 | [diff] [blame] | 400 | buf->flags &= ~BF_AUTO_CONNECT; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 403 | /* Installs <func> as a hijacker on the buffer <b> for session <s>. The hijack |
| 404 | * flag is set, and the function called once. The function is responsible for |
| 405 | * clearing the hijack bit. It is possible that the function clears the flag |
| 406 | * during this first call. |
| 407 | */ |
| 408 | static inline void buffer_install_hijacker(struct session *s, |
| 409 | struct buffer *b, |
| 410 | void (*func)(struct session *, struct buffer *)) |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 411 | { |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 412 | b->hijacker = func; |
| 413 | b->flags |= BF_HIJACK; |
| 414 | func(s, b); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Willy Tarreau | 01bf867 | 2008-12-07 18:03:29 +0100 | [diff] [blame] | 417 | /* Releases the buffer from hijacking mode. Often used by the hijack function */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 418 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 419 | { |
| 420 | buf->flags &= ~BF_HIJACK; |
| 421 | } |
| 422 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 423 | /* allow the consumer to try to establish a new connection. */ |
| 424 | static inline void buffer_auto_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 425 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 426 | buf->flags |= BF_AUTO_CONNECT; |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 429 | /* prevent the consumer from trying to establish a new connection, and also |
| 430 | * disable auto shutdown forwarding. |
| 431 | */ |
| 432 | static inline void buffer_dont_connect(struct buffer *buf) |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 433 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 434 | buf->flags &= ~(BF_AUTO_CONNECT|BF_AUTO_CLOSE); |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 437 | /* allow the producer to forward shutdown requests */ |
| 438 | static inline void buffer_auto_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 439 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 440 | buf->flags |= BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 443 | /* prevent the producer from forwarding shutdown requests */ |
| 444 | static inline void buffer_dont_close(struct buffer *buf) |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 445 | { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 446 | buf->flags &= ~BF_AUTO_CLOSE; |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 449 | /* allow the producer to read / poll the input */ |
| 450 | static inline void buffer_auto_read(struct buffer *buf) |
| 451 | { |
| 452 | buf->flags &= ~BF_DONT_READ; |
| 453 | } |
| 454 | |
| 455 | /* prevent the producer from read / poll the input */ |
| 456 | static inline void buffer_dont_read(struct buffer *buf) |
| 457 | { |
| 458 | buf->flags |= BF_DONT_READ; |
| 459 | } |
| 460 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 461 | /* |
| 462 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 463 | * there at once without overwriting anything. |
| 464 | */ |
| 465 | static inline int buffer_realign(struct buffer *buf) |
| 466 | { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 467 | if (!(buf->i | buf->o)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 468 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 469 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 470 | } |
Willy Tarreau | 4b517ca | 2011-11-25 20:33:58 +0100 | [diff] [blame] | 471 | return buffer_contig_space(buf); |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Advance the buffer's read pointer by <len> bytes. This is useful when data |
| 476 | * have been read directly from the buffer. It is illegal to call this function |
| 477 | * 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] | 478 | * responsibility to ensure that <len> is never larger than buf->o. |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 479 | */ |
| 480 | static inline void buffer_skip(struct buffer *buf, int len) |
| 481 | { |
| 482 | buf->w += len; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 483 | if (buf->w >= buf->data + buf->size) |
| 484 | buf->w -= buf->size; /* wrap around the buffer */ |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 486 | buf->o -= len; |
| 487 | if (buffer_len(buf) == 0) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 488 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 489 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame^] | 490 | if (buffer_len(buf) < buffer_max_len(buf)) |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 491 | buf->flags &= ~BF_FULL; |
| 492 | |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 493 | if (!buf->o && !buf->pipe) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 494 | buf->flags |= BF_OUT_EMPTY; |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 495 | |
| 496 | /* notify that some data was written to the SI from the buffer */ |
| 497 | buf->flags |= BF_WRITE_PARTIAL; |
Willy Tarreau | 2b7addc | 2009-08-31 07:37:22 +0200 | [diff] [blame] | 498 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 499 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 500 | /* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of success, |
| 501 | * -2 if it is larger than the buffer size, or the number of bytes available |
| 502 | * otherwise. If the chunk has been written, its size is automatically reset |
| 503 | * to zero. The send limit is automatically adjusted with the amount of data |
| 504 | * written. |
| 505 | */ |
| 506 | static inline int buffer_write_chunk(struct buffer *buf, struct chunk *chunk) |
| 507 | { |
| 508 | int ret; |
| 509 | |
| 510 | ret = buffer_write(buf, chunk->str, chunk->len); |
| 511 | if (ret == -1) |
| 512 | chunk->len = 0; |
| 513 | return ret; |
| 514 | } |
| 515 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 516 | /* Tries to copy chunk <chunk> into buffer <buf> after length controls. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 517 | * 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] | 518 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 519 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 520 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 521 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 522 | * 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] | 523 | */ |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 524 | static inline int buffer_put_chunk(struct buffer *buf, struct chunk *chunk) |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 525 | { |
| 526 | int ret; |
| 527 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 528 | ret = buffer_put_block(buf, chunk->str, chunk->len); |
| 529 | if (ret > 0) |
| 530 | chunk->len -= ret; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 531 | return ret; |
| 532 | } |
| 533 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 534 | /* 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] | 535 | * 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] | 536 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 537 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 538 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 539 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 540 | * transferred. |
| 541 | */ |
| 542 | static inline int buffer_put_string(struct buffer *buf, const char *str) |
| 543 | { |
| 544 | return buffer_put_block(buf, str, strlen(str)); |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Return one char from the buffer. If the buffer is empty and closed, return -2. |
| 549 | * If the buffer is just empty, return -1. The buffer's pointer is not advanced, |
| 550 | * it's up to the caller to call buffer_skip(buf, 1) when it has consumed the char. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 551 | * Also note that this function respects the ->o limit. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 552 | */ |
| 553 | static inline int buffer_get_char(struct buffer *buf) |
| 554 | { |
| 555 | /* closed or empty + imminent close = -2; empty = -1 */ |
| 556 | if (unlikely(buf->flags & (BF_OUT_EMPTY|BF_SHUTW))) { |
| 557 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 558 | return -2; |
| 559 | return -1; |
| 560 | } |
| 561 | return *buf->w; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* DEPRECATED, just provided for compatibility, use buffer_put_chunk() instead !!! |
| 566 | * returns >= 0 if the buffer is too small to hold the message, -1 if the |
| 567 | * transfer was OK, -2 in case of failure. |
| 568 | */ |
| 569 | static inline int buffer_feed_chunk(struct buffer *buf, struct chunk *msg) |
| 570 | { |
| 571 | int ret = buffer_put_chunk(buf, msg); |
| 572 | if (ret >= 0) /* transfer OK */ |
| 573 | return -1; |
| 574 | if (ret == -1) /* missing room */ |
| 575 | return 1; |
| 576 | /* failure */ |
| 577 | return -2; |
| 578 | } |
| 579 | |
| 580 | /* DEPRECATED, just provided for compatibility, use buffer_put_string() instead !!! |
| 581 | * returns >= 0 if the buffer is too small to hold the message, -1 if the |
| 582 | * transfer was OK, -2 in case of failure. |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 583 | */ |
| 584 | static inline int buffer_feed(struct buffer *buf, const char *str) |
| 585 | { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 586 | int ret = buffer_put_string(buf, str); |
| 587 | if (ret >= 0) /* transfer OK */ |
| 588 | return -1; |
| 589 | if (ret == -1) /* missing room */ |
| 590 | return 1; |
| 591 | /* failure */ |
| 592 | return -2; |
Willy Tarreau | 9bcc91e | 2009-10-10 18:01:44 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 595 | |
| 596 | /* This function writes the string <str> at position <pos> which must be in |
| 597 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 598 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 599 | * (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] | 600 | * 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] | 601 | * it does not make sense to use it on data scheduled to be sent. |
| 602 | */ |
| 603 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 604 | { |
| 605 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 606 | } |
| 607 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 608 | /* |
| 609 | * |
| 610 | * Functions below are used to manage chunks |
| 611 | * |
| 612 | */ |
| 613 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 614 | static inline void chunk_init(struct chunk *chk, char *str, size_t size) { |
| 615 | chk->str = str; |
| 616 | chk->len = 0; |
| 617 | chk->size = size; |
| 618 | } |
| 619 | |
| 620 | /* report 0 in case of error, 1 if OK. */ |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 621 | 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] | 622 | |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 623 | if (size && len > size) |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 624 | return 0; |
| 625 | |
| 626 | chk->str = str; |
| 627 | chk->len = len; |
| 628 | chk->size = size; |
| 629 | |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | static inline void chunk_initstr(struct chunk *chk, char *str) { |
| 634 | chk->str = str; |
| 635 | chk->len = strlen(str); |
| 636 | chk->size = 0; /* mark it read-only */ |
| 637 | } |
| 638 | |
| 639 | static inline int chunk_strcpy(struct chunk *chk, const char *str) { |
| 640 | size_t len; |
| 641 | |
| 642 | len = strlen(str); |
| 643 | |
| 644 | if (unlikely(len > chk->size)) |
| 645 | return 0; |
| 646 | |
| 647 | chk->len = len; |
| 648 | memcpy(chk->str, str, len); |
| 649 | |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | int chunk_printf(struct chunk *chk, const char *fmt, ...) |
| 654 | __attribute__ ((format(printf, 2, 3))); |
| 655 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 656 | int chunk_htmlencode(struct chunk *dst, struct chunk *src); |
| 657 | int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc); |
| 658 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 659 | static inline void chunk_reset(struct chunk *chk) { |
| 660 | chk->str = NULL; |
| 661 | chk->len = -1; |
| 662 | chk->size = 0; |
| 663 | } |
| 664 | |
| 665 | static inline void chunk_destroy(struct chunk *chk) { |
| 666 | |
| 667 | if (!chk->size) |
| 668 | return; |
| 669 | |
| 670 | if (chk->str) |
| 671 | free(chk->str); |
| 672 | |
| 673 | chunk_reset(chk); |
| 674 | } |
| 675 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 676 | /* |
| 677 | * frees the destination chunk if already allocated, allocates a new string, |
| 678 | * and copies the source into it. The pointer to the destination string is |
| 679 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 680 | */ |
| 681 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 682 | if (!dst || !src || !src->str) |
| 683 | return NULL; |
| 684 | if (dst->str) |
| 685 | free(dst->str); |
| 686 | dst->len = src->len; |
| 687 | dst->str = (char *)malloc(dst->len); |
| 688 | memcpy(dst->str, src->str, dst->len); |
| 689 | return dst->str; |
| 690 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 691 | |
| 692 | #endif /* _PROTO_BUFFERS_H */ |
| 693 | |
| 694 | /* |
| 695 | * Local variables: |
| 696 | * c-indent-level: 8 |
| 697 | * c-basic-offset: 8 |
| 698 | * End: |
| 699 | */ |