blob: edda45954fa0227e2001fac357b8359632d01267 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Buffer management functions.
3 *
Willy Tarreaue09e0ce2007-03-18 16:31:29 +01004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreauc0dde7a2007-01-01 21:38:07 +010013#include <stdarg.h>
14#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020018#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <proto/buffers.h>
Willy Tarreau27a674e2009-08-17 07:23:33 +020020#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau7341d942007-05-13 19:56:02 +020022struct pool_head *pool2_buffer;
23
24
25/* perform minimal intializations, report 0 in case of error, 1 if OK. */
26int init_buffer()
27{
Willy Tarreau27a674e2009-08-17 07:23:33 +020028 pool2_buffer = create_pool("buffer", sizeof(struct buffer) + global.tune.bufsize, MEM_F_SHARED);
Willy Tarreau7341d942007-05-13 19:56:02 +020029 return pool2_buffer != NULL;
30}
31
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020033/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
Willy Tarreau078e2942009-08-18 07:19:39 +020034 * success, -2 if the message is larger than the buffer size, or the number of
35 * bytes available otherwise. The send limit is automatically adjusted with the
36 * amount of data written. FIXME-20060521: handle unaligned data.
Willy Tarreaubaaee002006-06-26 02:48:02 +020037 */
38int buffer_write(struct buffer *buf, const char *msg, int len)
39{
40 int max;
41
Willy Tarreauaeac3192009-08-31 08:09:57 +020042 if (len == 0)
43 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau078e2942009-08-18 07:19:39 +020045 if (len > buf->size) {
46 /* we can't write this chunk and will never be able to, because
47 * it is larger than the buffer. This must be reported as an
48 * error. Then we return -2 so that writers that don't care can
49 * ignore it and go on, and others can check for this value.
50 */
51 return -2;
52 }
53
Willy Tarreauaeac3192009-08-31 08:09:57 +020054 max = buffer_realign(buf);
55
Willy Tarreaubaaee002006-06-26 02:48:02 +020056 if (len > max)
57 return max;
58
59 memcpy(buf->r, msg, len);
60 buf->l += len;
Willy Tarreauf890dc92008-12-13 21:12:26 +010061 buf->send_max += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 buf->r += len;
Willy Tarreau35d66b02007-01-02 00:28:21 +010063 buf->total += len;
Willy Tarreaua07a34e2009-08-16 23:27:46 +020064 if (buf->r == buf->data + buf->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020066
Willy Tarreaue393fe22008-08-16 22:18:07 +020067 buf->flags &= ~(BF_EMPTY|BF_FULL);
68 if (buf->l == 0)
69 buf->flags |= BF_EMPTY;
Willy Tarreau03d60bb2009-01-09 11:13:00 +010070 if (buf->l >= buf->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +020071 buf->flags |= BF_FULL;
72
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020073 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020074}
75
Willy Tarreauaeac3192009-08-31 08:09:57 +020076/* Try to write string <str> into buffer <buf> after length controls. This
77 * is the equivalent of buffer_write() except that to_forward and send_max
78 * are updated and that max_len is respected. Returns -1 in case of success,
Willy Tarreau078e2942009-08-18 07:19:39 +020079 * -2 if it is larger than the buffer size, or the number of bytes available
Willy Tarreauaeac3192009-08-31 08:09:57 +020080 * otherwise. The send limit is automatically adjusted with the amount of data
Willy Tarreau078e2942009-08-18 07:19:39 +020081 * written.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010082 */
Willy Tarreauaeac3192009-08-31 08:09:57 +020083int buffer_feed(struct buffer *buf, const char *str, int len)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010084{
85 int max;
86
Willy Tarreauaeac3192009-08-31 08:09:57 +020087 if (len == 0)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020088 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010089
Willy Tarreauaeac3192009-08-31 08:09:57 +020090 if (len > buf->max_len) {
Willy Tarreau078e2942009-08-18 07:19:39 +020091 /* we can't write this chunk and will never be able to, because
Willy Tarreauaeac3192009-08-31 08:09:57 +020092 * it is larger than the buffer's current max size.
Willy Tarreau078e2942009-08-18 07:19:39 +020093 */
94 return -2;
95 }
96
Willy Tarreauaeac3192009-08-31 08:09:57 +020097 max = buffer_contig_space(buf);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010098
Willy Tarreauaeac3192009-08-31 08:09:57 +020099 if (len > max)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100100 return max;
101
Willy Tarreauaeac3192009-08-31 08:09:57 +0200102 memcpy(buf->r, str, len);
103 buf->l += len;
104 buf->r += len;
105 buf->total += len;
106 if (buf->to_forward > 0) {
107 int fwd = MIN(buf->to_forward, len);
108 buf->send_max += fwd;
109 buf->to_forward -= fwd;
110 }
111
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200112 if (buf->r == buf->data + buf->size)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100113 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200114
Willy Tarreaue393fe22008-08-16 22:18:07 +0200115 buf->flags &= ~(BF_EMPTY|BF_FULL);
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100116 if (buf->l >= buf->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200117 buf->flags |= BF_FULL;
118
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200119 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100120}
121
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200122/* Get one text line out of a buffer from a stream interface.
123 * Return values :
124 * >0 : number of bytes read. Includes the \n if present before len or end.
125 * =0 : no '\n' before end found. <buf> is undefined.
126 * <0 : no more bytes readable + shutdown set.
127 * The buffer status is not changed. The caller must call buffer_skip() to
128 * update it. The '\n' is waited for as long as neither the buffer nor the
129 * output are full. If either of them is full, the string may be returned
130 * as is, without the '\n'.
131 */
132int buffer_si_peekline(struct buffer *buf, char *str, int len)
133{
134 int ret, max;
135 char *p;
136
137 ret = 0;
138 max = len;
139 if (!buf->send_max) {
140 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
141 ret = -1;
142 goto out;
143 }
144
145 p = buf->w;
146
147 if (max > buf->send_max) {
148 max = buf->send_max;
149 str[max] = 0;
150 }
151 while (max) {
152 *str++ = *p;
153 ret++;
154 max--;
155
156 if (*p == '\n')
157 break;
158 p++;
159 if (p == buf->data + buf->size)
160 p = buf->data;
161 }
162 if (*p != '\n' && ret < len && ret < buf->max_len &&
163 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
164 ret = 0;
165 out:
166 if (max)
167 *str = 0;
168 return ret;
169}
170
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171/*
172 * this function writes the string <str> at position <pos> which must be in buffer <b>,
173 * and moves <end> just after the end of <str>.
174 * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift.
175 * the shift value (positive or negative) is returned.
176 * If there's no space left, the move is not done.
177 *
178 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100179int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180{
181 int delta;
182 int len;
183
184 len = strlen(str);
185 delta = len - (end - pos);
186
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200187 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188 return 0; /* no space left */
189
190 /* first, protect the end of the buffer */
191 memmove(end + delta, end, b->data + b->l - end);
192
193 /* now, copy str over pos */
194 memcpy(pos, str,len);
195
196 /* we only move data after the displaced zone */
197 if (b->r > pos) b->r += delta;
198 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199 if (b->lr > pos) b->lr += delta;
200 b->l += delta;
201
Willy Tarreaue393fe22008-08-16 22:18:07 +0200202 b->flags &= ~(BF_EMPTY|BF_FULL);
203 if (b->l == 0)
204 b->flags |= BF_EMPTY;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100205 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200206 b->flags |= BF_FULL;
207
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 return delta;
209}
210
211/*
212 * same except that the string length is given, which allows str to be NULL if
Willy Tarreauf890dc92008-12-13 21:12:26 +0100213 * len is 0. The send limit is *not* adjusted.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100215int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216{
217 int delta;
218
219 delta = len - (end - pos);
220
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200221 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222 return 0; /* no space left */
223
224 if (b->data + b->l < end) {
225 /* The data has been stolen, we could have crashed.
226 * Maybe we should abort() ? */
227 return 0;
228 }
229
230 /* first, protect the end of the buffer */
231 memmove(end + delta, end, b->data + b->l - end);
232
233 /* now, copy str over pos */
234 if (len)
235 memcpy(pos, str, len);
236
237 /* we only move data after the displaced zone */
238 if (b->r > pos) b->r += delta;
239 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 if (b->lr > pos) b->lr += delta;
241 b->l += delta;
242
Willy Tarreaue393fe22008-08-16 22:18:07 +0200243 b->flags &= ~(BF_EMPTY|BF_FULL);
244 if (b->l == 0)
245 b->flags |= BF_EMPTY;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100246 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200247 b->flags |= BF_FULL;
248
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249 return delta;
250}
251
252
253/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100254 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
255 * argument informs about the length of string <str> so that we don't have to
256 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
257 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100258 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100259 *
260 * The number of bytes added is returned on success. 0 is returned on failure.
261 */
262int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
263{
264 int delta;
265
266 delta = len + 2;
267
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200268 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100269 return 0; /* no space left */
270
271 /* first, protect the end of the buffer */
272 memmove(pos + delta, pos, b->data + b->l - pos);
273
274 /* now, copy str over pos */
275 if (len && str) {
276 memcpy(pos, str, len);
277 pos[len] = '\r';
278 pos[len + 1] = '\n';
279 }
280
281 /* we only move data after the displaced zone */
282 if (b->r > pos) b->r += delta;
283 if (b->w > pos) b->w += delta;
284 if (b->lr > pos) b->lr += delta;
285 b->l += delta;
286
Willy Tarreaue393fe22008-08-16 22:18:07 +0200287 b->flags &= ~(BF_EMPTY|BF_FULL);
288 if (b->l == 0)
289 b->flags |= BF_EMPTY;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100290 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200291 b->flags |= BF_FULL;
292
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100293 return delta;
294}
295
296
297/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100298 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
299 * at most <size> chars. If the size is over, nothing is added. Returns
300 * the new chunk size.
301 */
302int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
303{
304 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200305 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100306
307 va_start(argp, fmt);
Willy Tarreaudceaa082007-07-25 14:38:45 +0200308 ret = vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp);
309 if (ret >= size - chk->len)
310 /* do not copy anything in case of truncation */
311 chk->str[chk->len] = 0;
312 else
313 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100314 va_end(argp);
315 return chk->len;
316}
317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100318/*
319 * Dumps part or all of a buffer.
320 */
321void buffer_dump(FILE *o, struct buffer *b, int from, int to)
322{
323 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100324 fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n",
325 b->data, b->l, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100326
327 if (!to || to > b->l)
328 to = b->l;
329
330 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
331 for (; from < to; from++) {
332 if ((from & 15) == 0)
333 fprintf(o, " %04x: ", from);
334 fprintf(o, "%02x ", b->data[from]);
335 if ((from & 15) == 7)
336 fprintf(o, "- ");
337 else if (((from & 15) == 15) && (from != to-1))
338 fprintf(o, "\n");
339 }
340 fprintf(o, "\n--\n");
341}
342
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100343
344/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345 * Local variables:
346 * c-indent-level: 8
347 * c-basic-offset: 8
348 * End:
349 */