Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Buffer management functions. |
| 3 | * |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 18 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <proto/buffers.h> |
| 20 | |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 21 | struct pool_head *pool2_buffer; |
| 22 | |
| 23 | |
| 24 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 25 | int init_buffer() |
| 26 | { |
| 27 | pool2_buffer = create_pool("buffer", sizeof(struct buffer), MEM_F_SHARED); |
| 28 | return pool2_buffer != NULL; |
| 29 | } |
| 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 32 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 33 | * success, or the number of bytes available otherwise. The send limit is |
| 34 | * automatically adjusted with the amount of data written. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | * FIXME-20060521: handle unaligned data. |
| 36 | */ |
| 37 | int buffer_write(struct buffer *buf, const char *msg, int len) |
| 38 | { |
| 39 | int max; |
| 40 | |
| 41 | max = buffer_realign(buf); |
| 42 | |
| 43 | if (len > max) |
| 44 | return max; |
| 45 | |
| 46 | memcpy(buf->r, msg, len); |
| 47 | buf->l += len; |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 48 | buf->send_max += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | buf->r += len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 50 | buf->total += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | if (buf->r == buf->data + BUFSIZE) |
| 52 | buf->r = buf->data; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 54 | buf->flags &= ~(BF_EMPTY|BF_FULL); |
| 55 | if (buf->l == 0) |
| 56 | buf->flags |= BF_EMPTY; |
| 57 | if (buf->l >= buf->rlim - buf->data) |
| 58 | buf->flags |= BF_FULL; |
| 59 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 60 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 63 | /* writes the chunk <chunk> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 64 | * success, or the number of bytes available otherwise. If the chunk |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 65 | * has been written, its size is automatically reset to zero. The send limit is |
| 66 | * automatically adjusted with the amount of data written. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 67 | */ |
| 68 | int buffer_write_chunk(struct buffer *buf, struct chunk *chunk) |
| 69 | { |
| 70 | int max; |
| 71 | |
| 72 | if (chunk->len == 0) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 73 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 74 | |
| 75 | max = buffer_realign(buf); |
| 76 | |
| 77 | if (chunk->len > max) |
| 78 | return max; |
| 79 | |
| 80 | memcpy(buf->r, chunk->str, chunk->len); |
| 81 | buf->l += chunk->len; |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 82 | buf->send_max += chunk->len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 83 | buf->r += chunk->len; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 84 | buf->total += chunk->len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 85 | if (buf->r == buf->data + BUFSIZE) |
| 86 | buf->r = buf->data; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 87 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 88 | buf->flags &= ~(BF_EMPTY|BF_FULL); |
| 89 | if (buf->l == 0) |
| 90 | buf->flags |= BF_EMPTY; |
| 91 | if (buf->l >= buf->rlim - buf->data) |
| 92 | buf->flags |= BF_FULL; |
| 93 | |
| 94 | chunk->len = 0; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 95 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | /* |
| 99 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 100 | * and moves <end> just after the end of <str>. |
| 101 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 102 | * the shift value (positive or negative) is returned. |
| 103 | * If there's no space left, the move is not done. |
| 104 | * |
| 105 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 106 | int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | { |
| 108 | int delta; |
| 109 | int len; |
| 110 | |
| 111 | len = strlen(str); |
| 112 | delta = len - (end - pos); |
| 113 | |
| 114 | if (delta + b->r >= b->data + BUFSIZE) |
| 115 | return 0; /* no space left */ |
| 116 | |
| 117 | /* first, protect the end of the buffer */ |
| 118 | memmove(end + delta, end, b->data + b->l - end); |
| 119 | |
| 120 | /* now, copy str over pos */ |
| 121 | memcpy(pos, str,len); |
| 122 | |
| 123 | /* we only move data after the displaced zone */ |
| 124 | if (b->r > pos) b->r += delta; |
| 125 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | if (b->lr > pos) b->lr += delta; |
| 127 | b->l += delta; |
| 128 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 129 | b->flags &= ~(BF_EMPTY|BF_FULL); |
| 130 | if (b->l == 0) |
| 131 | b->flags |= BF_EMPTY; |
| 132 | if (b->l >= b->rlim - b->data) |
| 133 | b->flags |= BF_FULL; |
| 134 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | return delta; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * same except that the string length is given, which allows str to be NULL if |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 140 | * len is 0. The send limit is *not* adjusted. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 141 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 142 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | { |
| 144 | int delta; |
| 145 | |
| 146 | delta = len - (end - pos); |
| 147 | |
| 148 | if (delta + b->r >= b->data + BUFSIZE) |
| 149 | return 0; /* no space left */ |
| 150 | |
| 151 | if (b->data + b->l < end) { |
| 152 | /* The data has been stolen, we could have crashed. |
| 153 | * Maybe we should abort() ? */ |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* first, protect the end of the buffer */ |
| 158 | memmove(end + delta, end, b->data + b->l - end); |
| 159 | |
| 160 | /* now, copy str over pos */ |
| 161 | if (len) |
| 162 | memcpy(pos, str, len); |
| 163 | |
| 164 | /* we only move data after the displaced zone */ |
| 165 | if (b->r > pos) b->r += delta; |
| 166 | if (b->w > pos) b->w += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | if (b->lr > pos) b->lr += delta; |
| 168 | b->l += delta; |
| 169 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 170 | b->flags &= ~(BF_EMPTY|BF_FULL); |
| 171 | if (b->l == 0) |
| 172 | b->flags |= BF_EMPTY; |
| 173 | if (b->l >= b->rlim - b->data) |
| 174 | b->flags |= BF_FULL; |
| 175 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 176 | return delta; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 181 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 182 | * argument informs about the length of string <str> so that we don't have to |
| 183 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 184 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 185 | * some circumstances. The send limit is *not* adjusted. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 186 | * |
| 187 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 188 | */ |
| 189 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len) |
| 190 | { |
| 191 | int delta; |
| 192 | |
| 193 | delta = len + 2; |
| 194 | |
| 195 | if (delta + b->r >= b->data + BUFSIZE) |
| 196 | return 0; /* no space left */ |
| 197 | |
| 198 | /* first, protect the end of the buffer */ |
| 199 | memmove(pos + delta, pos, b->data + b->l - pos); |
| 200 | |
| 201 | /* now, copy str over pos */ |
| 202 | if (len && str) { |
| 203 | memcpy(pos, str, len); |
| 204 | pos[len] = '\r'; |
| 205 | pos[len + 1] = '\n'; |
| 206 | } |
| 207 | |
| 208 | /* we only move data after the displaced zone */ |
| 209 | if (b->r > pos) b->r += delta; |
| 210 | if (b->w > pos) b->w += delta; |
| 211 | if (b->lr > pos) b->lr += delta; |
| 212 | b->l += delta; |
| 213 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 214 | b->flags &= ~(BF_EMPTY|BF_FULL); |
| 215 | if (b->l == 0) |
| 216 | b->flags |= BF_EMPTY; |
| 217 | if (b->l >= b->rlim - b->data) |
| 218 | b->flags |= BF_FULL; |
| 219 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 220 | return delta; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 225 | * Does an snprintf() at the end of chunk <chk>, respecting the limit of |
| 226 | * at most <size> chars. If the size is over, nothing is added. Returns |
| 227 | * the new chunk size. |
| 228 | */ |
| 229 | int chunk_printf(struct chunk *chk, int size, const char *fmt, ...) |
| 230 | { |
| 231 | va_list argp; |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 232 | int ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 233 | |
| 234 | va_start(argp, fmt); |
Willy Tarreau | dceaa08 | 2007-07-25 14:38:45 +0200 | [diff] [blame] | 235 | ret = vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp); |
| 236 | if (ret >= size - chk->len) |
| 237 | /* do not copy anything in case of truncation */ |
| 238 | chk->str[chk->len] = 0; |
| 239 | else |
| 240 | chk->len += ret; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 241 | va_end(argp); |
| 242 | return chk->len; |
| 243 | } |
| 244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Dumps part or all of a buffer. |
| 247 | */ |
| 248 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 249 | { |
| 250 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 251 | fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n", |
| 252 | b->data, b->l, b->r, b->w, b->lr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 253 | |
| 254 | if (!to || to > b->l) |
| 255 | to = b->l; |
| 256 | |
| 257 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
| 258 | for (; from < to; from++) { |
| 259 | if ((from & 15) == 0) |
| 260 | fprintf(o, " %04x: ", from); |
| 261 | fprintf(o, "%02x ", b->data[from]); |
| 262 | if ((from & 15) == 7) |
| 263 | fprintf(o, "- "); |
| 264 | else if (((from & 15) == 15) && (from != to-1)) |
| 265 | fprintf(o, "\n"); |
| 266 | } |
| 267 | fprintf(o, "\n--\n"); |
| 268 | } |
| 269 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 270 | |
| 271 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | * Local variables: |
| 273 | * c-indent-level: 8 |
| 274 | * c-basic-offset: 8 |
| 275 | * End: |
| 276 | */ |