Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 2 | * Channel management functions. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 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 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <string.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | |
| 18 | #include <common/config.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 20 | #include <common/buffer.h> |
| 21 | #include <proto/channel.h> |
Willy Tarreau | 27a674e | 2009-08-17 07:23:33 +0200 | [diff] [blame] | 22 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 24 | |
| 25 | /* Note: this code has not yet been completely cleaned up and still refers to |
| 26 | * the word "buffer" when "channel" is meant instead. |
| 27 | */ |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 28 | struct pool_head *pool2_buffer; |
| 29 | |
| 30 | |
| 31 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 32 | int init_buffer() |
| 33 | { |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 34 | pool2_buffer = create_pool("buffer", sizeof(struct channel) + global.tune.bufsize, MEM_F_SHARED); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 35 | return pool2_buffer != NULL; |
| 36 | } |
| 37 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 38 | /* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying |
| 39 | * the task. Any pending data in the buffer is scheduled to be sent as well, |
| 40 | * in the limit of the number of bytes to forward. This must be the only method |
| 41 | * to use to schedule bytes to be sent. If the requested number is too large, it |
| 42 | * is automatically adjusted. The number of bytes taken into account is returned. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 43 | * Directly touching ->to_forward will cause lockups when ->o goes down to |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 44 | * zero if nobody is ready to push the remaining data. |
| 45 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 46 | unsigned long long buffer_forward(struct channel *buf, unsigned long long bytes) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 47 | { |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 48 | unsigned int new_forward; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 49 | unsigned int forwarded; |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 50 | unsigned int bytes32; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 51 | |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 52 | bytes32 = bytes; |
| 53 | |
| 54 | /* hint: avoid comparisons on long long for the fast case, since if the |
| 55 | * length does not fit in an unsigned it, it will never be forwarded at |
| 56 | * once anyway. |
| 57 | */ |
| 58 | if (bytes <= ~0U) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 59 | if (bytes32 <= buf->buf.i) { |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 60 | /* OK this amount of bytes might be forwarded at once */ |
| 61 | if (!bytes32) |
| 62 | return 0; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame^] | 63 | b_adv(&buf->buf, bytes32); |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 64 | return bytes; |
| 65 | } |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 68 | forwarded = buf->buf.i; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame^] | 69 | b_adv(&buf->buf, buf->buf.i); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 70 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 71 | /* Note: the case below is the only case where we may return |
| 72 | * a byte count that does not fit into a 32-bit number. |
| 73 | */ |
| 74 | if (likely(buf->to_forward == BUF_INFINITE_FORWARD)) |
| 75 | return bytes; |
| 76 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 77 | if (likely(bytes == BUF_INFINITE_FORWARD)) { |
| 78 | buf->to_forward = bytes; |
| 79 | return bytes; |
| 80 | } |
| 81 | |
| 82 | new_forward = buf->to_forward + bytes - forwarded; |
| 83 | bytes = forwarded; /* at least those bytes were scheduled */ |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 84 | |
| 85 | if (new_forward <= buf->to_forward) { |
| 86 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 87 | new_forward = MID_RANGE(new_forward); |
| 88 | } |
| 89 | |
| 90 | if (new_forward > buf->to_forward) { |
| 91 | bytes += new_forward - buf->to_forward; |
| 92 | buf->to_forward = new_forward; |
| 93 | } |
| 94 | return bytes; |
| 95 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 97 | /* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 98 | * success, -2 if the message is larger than the buffer size, or the number of |
| 99 | * bytes available otherwise. The send limit is automatically adjusted with the |
| 100 | * amount of data written. FIXME-20060521: handle unaligned data. |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 101 | * Note: this function appends data to the buffer's output and possibly overwrites |
| 102 | * any pending input data which are assumed not to exist. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 104 | int bo_inject(struct channel *buf, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | { |
| 106 | int max; |
| 107 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 108 | if (len == 0) |
| 109 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 111 | if (len > buf->buf.size) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 112 | /* we can't write this chunk and will never be able to, because |
| 113 | * it is larger than the buffer. This must be reported as an |
| 114 | * error. Then we return -2 so that writers that don't care can |
| 115 | * ignore it and go on, and others can check for this value. |
| 116 | */ |
| 117 | return -2; |
| 118 | } |
| 119 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 120 | max = buffer_realign(&buf->buf); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | if (len > max) |
| 123 | return max; |
| 124 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 125 | memcpy(buf->buf.p, msg, len); |
| 126 | buf->buf.o += len; |
| 127 | buf->buf.p = b_ptr(&buf->buf, len); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 128 | buf->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 8e21bb9 | 2012-08-24 22:40:29 +0200 | [diff] [blame] | 130 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 131 | if (bi_full(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 132 | buf->flags |= BF_FULL; |
| 133 | |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 134 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 137 | /* Tries to copy character <c> into buffer <buf> after length controls. The |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 138 | * ->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] | 139 | * closed, -2 is returned. If there is not enough room left in the buffer, -1 |
| 140 | * is returned. Otherwise the number of bytes copied is returned (1). Buffer |
| 141 | * flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 142 | * transferred. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 143 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 144 | int bi_putchr(struct channel *buf, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 145 | { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 146 | if (unlikely(buffer_input_closed(buf))) |
| 147 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 148 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 149 | if (buf->flags & BF_FULL) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 150 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 151 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 152 | *bi_end(&buf->buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 153 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 154 | buf->buf.i++; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 155 | if (bi_full(buf)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 156 | buf->flags |= BF_FULL; |
| 157 | buf->flags |= BF_READ_PARTIAL; |
| 158 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 159 | if (buf->to_forward >= 1) { |
| 160 | if (buf->to_forward != BUF_INFINITE_FORWARD) |
| 161 | buf->to_forward--; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame^] | 162 | b_adv(&buf->buf, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | buf->total++; |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | /* Tries to copy block <blk> at once into buffer <buf> after length controls. |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 170 | * 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] | 171 | * closed, -2 is returned. If the block is too large for this buffer, -3 is |
| 172 | * returned. If there is not enough room left in the buffer, -1 is returned. |
| 173 | * Otherwise the number of bytes copied is returned (0 being a valid number). |
| 174 | * Buffer flags FULL, EMPTY and READ_PARTIAL are updated if some data can be |
| 175 | * transferred. |
| 176 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 177 | int bi_putblk(struct channel *buf, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 178 | { |
| 179 | int max; |
| 180 | |
| 181 | if (unlikely(buffer_input_closed(buf))) |
| 182 | return -2; |
| 183 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 184 | max = buffer_max_len(buf); |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 185 | if (unlikely(len > max - buffer_len(&buf->buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 186 | /* we can't write this chunk right now because the buffer is |
| 187 | * almost full or because the block is too large. Return the |
| 188 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 189 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 190 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 191 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 192 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 193 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 194 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 195 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 196 | if (unlikely(len == 0)) |
| 197 | return 0; |
| 198 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 199 | /* OK so the data fits in the buffer in one or two blocks */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 200 | max = buffer_contig_space_with_res(&buf->buf, buf->buf.size - max); |
| 201 | memcpy(bi_end(&buf->buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 202 | if (len > max) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 203 | memcpy(buf->buf.data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 204 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 205 | buf->buf.i += len; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 206 | buf->total += len; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 207 | if (buf->to_forward) { |
| 208 | unsigned long fwd = len; |
| 209 | if (buf->to_forward != BUF_INFINITE_FORWARD) { |
| 210 | if (fwd > buf->to_forward) |
| 211 | fwd = buf->to_forward; |
| 212 | buf->to_forward -= fwd; |
| 213 | } |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame^] | 214 | b_adv(&buf->buf, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 217 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 218 | if (bi_full(buf)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 219 | buf->flags |= BF_FULL; |
| 220 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 221 | /* notify that some data was read from the SI into the buffer */ |
| 222 | buf->flags |= BF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 223 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 226 | /* Gets one text line out of a buffer from a stream interface. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 227 | * Return values : |
| 228 | * >0 : number of bytes read. Includes the \n if present before len or end. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 229 | * =0 : no '\n' before end found. <str> is left undefined. |
| 230 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 231 | * The buffer status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 232 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 233 | * output are full. If either of them is full, the string may be returned |
| 234 | * as is, without the '\n'. |
| 235 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 236 | int bo_getline(struct channel *buf, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 237 | { |
| 238 | int ret, max; |
| 239 | char *p; |
| 240 | |
| 241 | ret = 0; |
| 242 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 243 | |
| 244 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 8e21bb9 | 2012-08-24 22:40:29 +0200 | [diff] [blame] | 245 | if (unlikely((buf->flags & BF_SHUTW) || channel_is_empty(buf))) { |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 246 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 247 | ret = -1; |
| 248 | goto out; |
| 249 | } |
| 250 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 251 | p = bo_ptr(&buf->buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 252 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 253 | if (max > buf->buf.o) { |
| 254 | max = buf->buf.o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 255 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 256 | } |
| 257 | while (max) { |
| 258 | *str++ = *p; |
| 259 | ret++; |
| 260 | max--; |
| 261 | |
| 262 | if (*p == '\n') |
| 263 | break; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 264 | p = buffer_wrap_add(&buf->buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 265 | } |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 266 | if (ret > 0 && ret < len && ret < buf->buf.o && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 267 | *(str-1) != '\n' && |
| 268 | !(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 269 | ret = 0; |
| 270 | out: |
| 271 | if (max) |
| 272 | *str = 0; |
| 273 | return ret; |
| 274 | } |
| 275 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 276 | /* Gets one full block of data at once from a buffer, optionally from a |
| 277 | * specific offset. Return values : |
| 278 | * >0 : number of bytes read, equal to requested size. |
| 279 | * =0 : not enough data available. <blk> is left undefined. |
| 280 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 281 | * The buffer status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 282 | * update it. |
| 283 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 284 | int bo_getblk(struct channel *buf, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 285 | { |
| 286 | int firstblock; |
| 287 | |
| 288 | if (buf->flags & BF_SHUTW) |
| 289 | return -1; |
| 290 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 291 | if (len + offset > buf->buf.o) { |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 292 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) |
| 293 | return -1; |
| 294 | return 0; |
| 295 | } |
| 296 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 297 | firstblock = buf->buf.data + buf->buf.size - bo_ptr(&buf->buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 298 | if (firstblock > offset) { |
| 299 | if (firstblock >= len + offset) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 300 | memcpy(blk, bo_ptr(&buf->buf) + offset, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 301 | return len; |
| 302 | } |
| 303 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 304 | memcpy(blk, bo_ptr(&buf->buf) + offset, firstblock - offset); |
| 305 | memcpy(blk + firstblock - offset, buf->buf.data, len - firstblock + offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 306 | return len; |
| 307 | } |
| 308 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 309 | memcpy(blk, buf->buf.data + offset - firstblock, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 310 | return len; |
| 311 | } |
| 312 | |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 313 | /* This function writes the string <str> at position <pos> which must be in |
| 314 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 315 | * <l> and <r> are updated to be valid after the shift. The shift value |
Willy Tarreau | 19ae56b | 2011-11-28 10:36:13 +0100 | [diff] [blame] | 316 | * (positive or negative) is returned. If there's no space left, the move is |
Willy Tarreau | 8e21bb9 | 2012-08-24 22:40:29 +0200 | [diff] [blame] | 317 | * not done. The function does not adjust ->o because it does not make sense to |
| 318 | * use it on data scheduled to be sent. For the same reason, it does not make |
| 319 | * sense to call this function on unparsed data, so <orig> is not updated. The |
| 320 | * string length is taken from parameter <len>. If <len> is null, the <str> |
| 321 | * pointer is allowed to be null. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 323 | int buffer_replace2(struct channel *b, char *pos, char *end, const char *str, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 324 | { |
| 325 | int delta; |
| 326 | |
| 327 | delta = len - (end - pos); |
| 328 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 329 | if (bi_end(&b->buf) + delta >= b->buf.data + b->buf.size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | return 0; /* no space left */ |
| 331 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 332 | if (buffer_not_empty(&b->buf) && |
| 333 | bi_end(&b->buf) + delta > bo_ptr(&b->buf) && |
| 334 | bo_ptr(&b->buf) >= bi_end(&b->buf)) |
Willy Tarreau | bbfa793 | 2010-01-25 01:49:57 +0100 | [diff] [blame] | 335 | return 0; /* no space left before wrapping data */ |
| 336 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | /* first, protect the end of the buffer */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 338 | memmove(end + delta, end, bi_end(&b->buf) - end); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 339 | |
| 340 | /* now, copy str over pos */ |
| 341 | if (len) |
| 342 | memcpy(pos, str, len); |
| 343 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 344 | b->buf.i += delta; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 345 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 346 | b->flags &= ~BF_FULL; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 347 | if (buffer_len(&b->buf) == 0) |
| 348 | b->buf.p = b->buf.data; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 349 | if (bi_full(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 350 | b->flags |= BF_FULL; |
| 351 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | return delta; |
| 353 | } |
| 354 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 355 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 356 | * Inserts <str> followed by "\r\n" at position <pos> in buffer <b>. The <len> |
| 357 | * argument informs about the length of string <str> so that we don't have to |
| 358 | * measure it. It does not include the "\r\n". If <str> is NULL, then the buffer |
| 359 | * is only opened for len+2 bytes but nothing is copied in. It may be useful in |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 360 | * some circumstances. The send limit is *not* adjusted. Same comments as above |
| 361 | * for the valid use cases. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 362 | * |
| 363 | * The number of bytes added is returned on success. 0 is returned on failure. |
| 364 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 365 | int buffer_insert_line2(struct channel *b, char *pos, const char *str, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 366 | { |
| 367 | int delta; |
| 368 | |
| 369 | delta = len + 2; |
| 370 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 371 | if (bi_end(&b->buf) + delta >= b->buf.data + b->buf.size) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | return 0; /* no space left */ |
| 373 | |
| 374 | /* first, protect the end of the buffer */ |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 375 | memmove(pos + delta, pos, bi_end(&b->buf) - pos); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 376 | |
| 377 | /* now, copy str over pos */ |
| 378 | if (len && str) { |
| 379 | memcpy(pos, str, len); |
| 380 | pos[len] = '\r'; |
| 381 | pos[len + 1] = '\n'; |
| 382 | } |
| 383 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 384 | b->buf.i += delta; |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 385 | |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 386 | b->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 387 | if (bi_full(b)) |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 388 | b->flags |= BF_FULL; |
| 389 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 390 | return delta; |
| 391 | } |
| 392 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 393 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 394 | * Local variables: |
| 395 | * c-indent-level: 8 |
| 396 | * c-basic-offset: 8 |
| 397 | * End: |
| 398 | */ |