blob: 6b551ab2c2683d5fb2e3766280f2f55cfbdb0643 [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 Tarreauba0b63d2009-09-20 08:09:44 +020067 buf->flags &= ~(BF_OUT_EMPTY|BF_FULL);
Willy Tarreau03d60bb2009-01-09 11:13:00 +010068 if (buf->l >= buf->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +020069 buf->flags |= BF_FULL;
70
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020071 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072}
73
Willy Tarreauaeac3192009-08-31 08:09:57 +020074/* Try to write string <str> into buffer <buf> after length controls. This
75 * is the equivalent of buffer_write() except that to_forward and send_max
76 * are updated and that max_len is respected. Returns -1 in case of success,
Willy Tarreau078e2942009-08-18 07:19:39 +020077 * -2 if it is larger than the buffer size, or the number of bytes available
Willy Tarreauaeac3192009-08-31 08:09:57 +020078 * otherwise. The send limit is automatically adjusted with the amount of data
Willy Tarreau078e2942009-08-18 07:19:39 +020079 * written.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010080 */
Willy Tarreauaeac3192009-08-31 08:09:57 +020081int buffer_feed(struct buffer *buf, const char *str, int len)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010082{
83 int max;
84
Willy Tarreauaeac3192009-08-31 08:09:57 +020085 if (len == 0)
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020086 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010087
Willy Tarreauaeac3192009-08-31 08:09:57 +020088 if (len > buf->max_len) {
Willy Tarreau078e2942009-08-18 07:19:39 +020089 /* we can't write this chunk and will never be able to, because
Willy Tarreauaeac3192009-08-31 08:09:57 +020090 * it is larger than the buffer's current max size.
Willy Tarreau078e2942009-08-18 07:19:39 +020091 */
92 return -2;
93 }
94
Willy Tarreauaeac3192009-08-31 08:09:57 +020095 max = buffer_contig_space(buf);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010096
Willy Tarreauaeac3192009-08-31 08:09:57 +020097 if (len > max)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010098 return max;
99
Willy Tarreauaeac3192009-08-31 08:09:57 +0200100 memcpy(buf->r, str, len);
101 buf->l += len;
102 buf->r += len;
103 buf->total += len;
104 if (buf->to_forward > 0) {
105 int fwd = MIN(buf->to_forward, len);
106 buf->send_max += fwd;
107 buf->to_forward -= fwd;
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200108 buf->flags &= ~BF_OUT_EMPTY;
Willy Tarreauaeac3192009-08-31 08:09:57 +0200109 }
110
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200111 if (buf->r == buf->data + buf->size)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100112 buf->r = buf->data;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200113
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200114 buf->flags &= ~BF_FULL;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100115 if (buf->l >= buf->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200116 buf->flags |= BF_FULL;
117
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200118 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100119}
120
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200121/* Get one text line out of a buffer from a stream interface.
122 * Return values :
123 * >0 : number of bytes read. Includes the \n if present before len or end.
124 * =0 : no '\n' before end found. <buf> is undefined.
125 * <0 : no more bytes readable + shutdown set.
126 * The buffer status is not changed. The caller must call buffer_skip() to
127 * update it. The '\n' is waited for as long as neither the buffer nor the
128 * output are full. If either of them is full, the string may be returned
129 * as is, without the '\n'.
130 */
131int buffer_si_peekline(struct buffer *buf, char *str, int len)
132{
133 int ret, max;
134 char *p;
135
136 ret = 0;
137 max = len;
138 if (!buf->send_max) {
139 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW))
140 ret = -1;
141 goto out;
142 }
143
144 p = buf->w;
145
146 if (max > buf->send_max) {
147 max = buf->send_max;
148 str[max] = 0;
149 }
150 while (max) {
151 *str++ = *p;
152 ret++;
153 max--;
154
155 if (*p == '\n')
156 break;
157 p++;
158 if (p == buf->data + buf->size)
159 p = buf->data;
160 }
161 if (*p != '\n' && ret < len && ret < buf->max_len &&
162 !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
163 ret = 0;
164 out:
165 if (max)
166 *str = 0;
167 return ret;
168}
169
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170/*
171 * this function writes the string <str> at position <pos> which must be in buffer <b>,
172 * and moves <end> just after the end of <str>.
173 * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift.
174 * the shift value (positive or negative) is returned.
175 * If there's no space left, the move is not done.
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200176 * The function does not adjust ->send_max nor BF_OUT_EMPTY because it does not
177 * make sense to use it on data scheduled to be sent.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178 *
179 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100180int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181{
182 int delta;
183 int len;
184
185 len = strlen(str);
186 delta = len - (end - pos);
187
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200188 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 return 0; /* no space left */
190
191 /* first, protect the end of the buffer */
192 memmove(end + delta, end, b->data + b->l - end);
193
194 /* now, copy str over pos */
195 memcpy(pos, str,len);
196
197 /* we only move data after the displaced zone */
198 if (b->r > pos) b->r += delta;
199 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200 if (b->lr > pos) b->lr += delta;
201 b->l += delta;
202
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200203 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200204 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200205 b->r = b->w = b->lr = b->data;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100206 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200207 b->flags |= BF_FULL;
208
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 return delta;
210}
211
212/*
213 * same except that the string length is given, which allows str to be NULL if
Willy Tarreauf890dc92008-12-13 21:12:26 +0100214 * len is 0. The send limit is *not* adjusted.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100216int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217{
218 int delta;
219
220 delta = len - (end - pos);
221
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200222 if (delta + b->r >= b->data + b->size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223 return 0; /* no space left */
224
225 if (b->data + b->l < end) {
226 /* The data has been stolen, we could have crashed.
227 * Maybe we should abort() ? */
228 return 0;
229 }
230
231 /* first, protect the end of the buffer */
232 memmove(end + delta, end, b->data + b->l - end);
233
234 /* now, copy str over pos */
235 if (len)
236 memcpy(pos, str, len);
237
238 /* we only move data after the displaced zone */
239 if (b->r > pos) b->r += delta;
240 if (b->w > pos) b->w += delta;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 if (b->lr > pos) b->lr += delta;
242 b->l += delta;
243
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200244 b->flags &= ~BF_FULL;
Willy Tarreaue393fe22008-08-16 22:18:07 +0200245 if (b->l == 0)
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200246 b->r = b->w = b->lr = b->data;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100247 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200248 b->flags |= BF_FULL;
249
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250 return delta;
251}
252
253
254/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100255 * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len>
256 * argument informs about the length of string <str> so that we don't have to
257 * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer
258 * is only opened for len+2 bytes but nothing is copied in. It may be useful in
Willy Tarreauf890dc92008-12-13 21:12:26 +0100259 * some circumstances. The send limit is *not* adjusted.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100260 *
261 * The number of bytes added is returned on success. 0 is returned on failure.
262 */
263int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
264{
265 int delta;
266
267 delta = len + 2;
268
Willy Tarreaua07a34e2009-08-16 23:27:46 +0200269 if (delta + b->r >= b->data + b->size)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100270 return 0; /* no space left */
271
272 /* first, protect the end of the buffer */
273 memmove(pos + delta, pos, b->data + b->l - pos);
274
275 /* now, copy str over pos */
276 if (len && str) {
277 memcpy(pos, str, len);
278 pos[len] = '\r';
279 pos[len + 1] = '\n';
280 }
281
282 /* we only move data after the displaced zone */
283 if (b->r > pos) b->r += delta;
284 if (b->w > pos) b->w += delta;
285 if (b->lr > pos) b->lr += delta;
286 b->l += delta;
287
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200288 b->flags &= ~BF_FULL;
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100289 if (b->l >= b->max_len)
Willy Tarreaue393fe22008-08-16 22:18:07 +0200290 b->flags |= BF_FULL;
291
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100292 return delta;
293}
294
295
296/*
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100297 * Does an snprintf() at the end of chunk <chk>, respecting the limit of
298 * at most <size> chars. If the size is over, nothing is added. Returns
299 * the new chunk size.
300 */
301int chunk_printf(struct chunk *chk, int size, const char *fmt, ...)
302{
303 va_list argp;
Willy Tarreaudceaa082007-07-25 14:38:45 +0200304 int ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100305
306 va_start(argp, fmt);
Willy Tarreaudceaa082007-07-25 14:38:45 +0200307 ret = vsnprintf(chk->str + chk->len, size - chk->len, fmt, argp);
308 if (ret >= size - chk->len)
309 /* do not copy anything in case of truncation */
310 chk->str[chk->len] = 0;
311 else
312 chk->len += ret;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100313 va_end(argp);
314 return chk->len;
315}
316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100317/*
318 * Dumps part or all of a buffer.
319 */
320void buffer_dump(FILE *o, struct buffer *b, int from, int to)
321{
322 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100323 fprintf(o, " data=%p l=%d r=%p w=%p lr=%p\n",
324 b->data, b->l, b->r, b->w, b->lr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100325
326 if (!to || to > b->l)
327 to = b->l;
328
329 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
330 for (; from < to; from++) {
331 if ((from & 15) == 0)
332 fprintf(o, " %04x: ", from);
333 fprintf(o, "%02x ", b->data[from]);
334 if ((from & 15) == 7)
335 fprintf(o, "- ");
336 else if (((from & 15) == 15) && (from != to-1))
337 fprintf(o, "\n");
338 }
339 fprintf(o, "\n--\n");
340}
341
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100342
343/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 * Local variables:
345 * c-indent-level: 8
346 * c-basic-offset: 8
347 * End:
348 */