Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/buffers.h |
| 3 | Buffer management definitions, macros and inline functions. |
| 4 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [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 | */ |
| 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> |
| 35 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 36 | extern struct pool_head *pool2_buffer; |
| 37 | |
| 38 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 39 | int init_buffer(); |
| 40 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 41 | /* Initializes all fields in the buffer. The ->rlim field is initialized last |
| 42 | * so that the compiler can optimize it away if changed immediately after the |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 43 | * call to this function. By default, it is set to the full size of the buffer. |
| 44 | * The BF_EMPTY flags is set. |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 45 | */ |
| 46 | static inline void buffer_init(struct buffer *buf) |
| 47 | { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 48 | buf->l = buf->total = 0; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 49 | buf->analysers = 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 50 | buf->cons = NULL; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 51 | buf->flags = BF_EMPTY; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 52 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 53 | buf->rlim = buf->data + BUFSIZE; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | /* returns 1 if the buffer is empty, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 57 | static inline int buffer_isempty(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | { |
| 59 | return buf->l == 0; |
| 60 | } |
| 61 | |
| 62 | /* returns 1 if the buffer is full, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 63 | static inline int buffer_isfull(const struct buffer *buf) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | return buf->l == BUFSIZE; |
| 65 | } |
| 66 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 67 | /* flushes any content from buffer <buf> and adjusts flags |
| 68 | * accordingly. |
| 69 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | static inline void buffer_flush(struct buffer *buf) |
| 71 | { |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 72 | buf->r = buf->lr = buf->w = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | buf->l = 0; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 74 | buf->flags |= BF_EMPTY | BF_FULL; |
| 75 | if (buf->rlim) |
| 76 | buf->flags &= ~BF_FULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 79 | /* marks the buffer as "shutdown" for reads and cancels the timeout */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 80 | static inline void buffer_shutr(struct buffer *buf) |
| 81 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 82 | buf->rex = TICK_ETERNITY; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 83 | buf->flags |= BF_SHUTR; |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 86 | /* marks the buffer as "shutdown" for writes and cancels the timeout */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 87 | static inline void buffer_shutw(struct buffer *buf) |
| 88 | { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 89 | buf->wex = TICK_ETERNITY; |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 90 | buf->flags |= BF_SHUTW; |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 93 | /* marks the buffer as "shutdown" ASAP for reads */ |
| 94 | static inline void buffer_shutr_now(struct buffer *buf) |
| 95 | { |
| 96 | buf->flags |= BF_SHUTR_NOW; |
| 97 | } |
| 98 | |
| 99 | /* marks the buffer as "shutdown" ASAP for writes */ |
| 100 | static inline void buffer_shutw_now(struct buffer *buf) |
| 101 | { |
| 102 | buf->flags |= BF_SHUTW_NOW; |
| 103 | } |
| 104 | |
| 105 | /* marks the buffer as "shutdown" ASAP in both directions */ |
| 106 | static inline void buffer_abort(struct buffer *buf) |
| 107 | { |
| 108 | buf->flags |= BF_SHUTR_NOW | BF_SHUTW_NOW; |
| 109 | } |
| 110 | |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 111 | /* set the buffer to hijacking mode */ |
| 112 | static inline void buffer_start_hijack(struct buffer *buf) |
| 113 | { |
| 114 | buf->flags |= BF_HIJACK; |
| 115 | } |
| 116 | |
| 117 | /* releases the buffer from hijacking mode */ |
| 118 | static inline void buffer_stop_hijack(struct buffer *buf) |
| 119 | { |
| 120 | buf->flags &= ~BF_HIJACK; |
| 121 | } |
| 122 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 123 | /* allows the consumer to send the buffer contents */ |
| 124 | static inline void buffer_write_ena(struct buffer *buf) |
| 125 | { |
| 126 | buf->flags |= BF_WRITE_ENA; |
| 127 | } |
| 128 | |
| 129 | /* prevents the consumer from sending the buffer contents */ |
| 130 | static inline void buffer_write_dis(struct buffer *buf) |
| 131 | { |
| 132 | buf->flags &= ~BF_WRITE_ENA; |
| 133 | } |
| 134 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | /* returns the maximum number of bytes writable at once in this buffer */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 136 | static inline int buffer_max(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 137 | { |
| 138 | if (buf->l == BUFSIZE) |
| 139 | return 0; |
| 140 | else if (buf->r >= buf->w) |
| 141 | return buf->data + BUFSIZE - buf->r; |
| 142 | else |
| 143 | return buf->w - buf->r; |
| 144 | } |
| 145 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 146 | /* sets the buffer read limit to <size> bytes, and adjusts the FULL |
| 147 | * flag accordingly. |
| 148 | */ |
| 149 | static inline void buffer_set_rlim(struct buffer *buf, int size) |
| 150 | { |
| 151 | buf->rlim = buf->data + size; |
| 152 | if (buf->l < size) |
| 153 | buf->flags &= ~BF_FULL; |
| 154 | else |
| 155 | buf->flags |= BF_FULL; |
| 156 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 160 | * there at once without overwriting anything. |
| 161 | */ |
| 162 | static inline int buffer_realign(struct buffer *buf) |
| 163 | { |
| 164 | if (buf->l == 0) { |
| 165 | /* let's realign the buffer to optimize I/O */ |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 166 | buf->r = buf->w = buf->lr = buf->data; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | } |
| 168 | return buffer_max(buf); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | int buffer_write(struct buffer *buf, const char *msg, int len); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 173 | int buffer_write_chunk(struct buffer *buf, struct chunk *chunk); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 174 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str); |
| 175 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 176 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 177 | int chunk_printf(struct chunk *chk, int size, const char *fmt, ...); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 178 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 180 | /* |
| 181 | * frees the destination chunk if already allocated, allocates a new string, |
| 182 | * and copies the source into it. The pointer to the destination string is |
| 183 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 184 | */ |
| 185 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 186 | if (!dst || !src || !src->str) |
| 187 | return NULL; |
| 188 | if (dst->str) |
| 189 | free(dst->str); |
| 190 | dst->len = src->len; |
| 191 | dst->str = (char *)malloc(dst->len); |
| 192 | memcpy(dst->str, src->str, dst->len); |
| 193 | return dst->str; |
| 194 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 195 | |
| 196 | #endif /* _PROTO_BUFFERS_H */ |
| 197 | |
| 198 | /* |
| 199 | * Local variables: |
| 200 | * c-indent-level: 8 |
| 201 | * c-basic-offset: 8 |
| 202 | * End: |
| 203 | */ |