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 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 25 | struct pool_head *pool2_channel; |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 26 | |
| 27 | |
| 28 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 29 | int init_channel() |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 30 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 31 | pool2_channel = create_pool("channel", sizeof(struct channel) + global.tune.bufsize, MEM_F_SHARED); |
| 32 | return pool2_channel != NULL; |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 35 | /* Schedule up to <bytes> more bytes to be forwarded via the channel without |
| 36 | * notifying the owner task. Any data pending in the buffer are scheduled to be |
| 37 | * sent as well, in the limit of the number of bytes to forward. This must be |
| 38 | * the only method to use to schedule bytes to be forwarded. If the requested |
| 39 | * number is too large, it is automatically adjusted. The number of bytes taken |
| 40 | * into account is returned. Directly touching ->to_forward will cause lockups |
| 41 | * when buf->o goes down to zero if nobody is ready to push the remaining data. |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 43 | unsigned long long channel_forward(struct channel *buf, unsigned long long bytes) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 44 | { |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 45 | unsigned int new_forward; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 46 | unsigned int forwarded; |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 47 | unsigned int bytes32; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 49 | bytes32 = bytes; |
| 50 | |
| 51 | /* hint: avoid comparisons on long long for the fast case, since if the |
| 52 | * length does not fit in an unsigned it, it will never be forwarded at |
| 53 | * once anyway. |
| 54 | */ |
| 55 | if (bytes <= ~0U) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 56 | if (bytes32 <= buf->buf.i) { |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 57 | /* OK this amount of bytes might be forwarded at once */ |
| 58 | if (!bytes32) |
| 59 | return 0; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 60 | b_adv(&buf->buf, bytes32); |
Willy Tarreau | 328582c | 2012-05-05 23:32:27 +0200 | [diff] [blame] | 61 | return bytes; |
| 62 | } |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 65 | forwarded = buf->buf.i; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 66 | b_adv(&buf->buf, buf->buf.i); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 68 | /* Note: the case below is the only case where we may return |
| 69 | * a byte count that does not fit into a 32-bit number. |
| 70 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 71 | if (likely(buf->to_forward == CHN_INFINITE_FORWARD)) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 72 | return bytes; |
| 73 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 74 | if (likely(bytes == CHN_INFINITE_FORWARD)) { |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 75 | buf->to_forward = bytes; |
| 76 | return bytes; |
| 77 | } |
| 78 | |
| 79 | new_forward = buf->to_forward + bytes - forwarded; |
| 80 | bytes = forwarded; /* at least those bytes were scheduled */ |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 81 | |
| 82 | if (new_forward <= buf->to_forward) { |
| 83 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 84 | new_forward = MID_RANGE(new_forward); |
| 85 | } |
| 86 | |
| 87 | if (new_forward > buf->to_forward) { |
| 88 | bytes += new_forward - buf->to_forward; |
| 89 | buf->to_forward = new_forward; |
| 90 | } |
| 91 | return bytes; |
| 92 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 94 | /* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in |
| 95 | * case of success, -2 if the message is larger than the buffer size, or the |
| 96 | * number of bytes available otherwise. The send limit is automatically |
| 97 | * adjusted to the amount of data written. FIXME-20060521: handle unaligned |
| 98 | * data. Note: this function appends data to the buffer's output and possibly |
| 99 | * overwrites any pending input data which are assumed not to exist. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 101 | int bo_inject(struct channel *buf, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | { |
| 103 | int max; |
| 104 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 105 | if (len == 0) |
| 106 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 108 | if (len > buf->buf.size) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 109 | /* we can't write this chunk and will never be able to, because |
| 110 | * it is larger than the buffer. This must be reported as an |
| 111 | * error. Then we return -2 so that writers that don't care can |
| 112 | * ignore it and go on, and others can check for this value. |
| 113 | */ |
| 114 | return -2; |
| 115 | } |
| 116 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 117 | max = buffer_realign(&buf->buf); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 118 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | if (len > max) |
| 120 | return max; |
| 121 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 122 | memcpy(buf->buf.p, msg, len); |
| 123 | buf->buf.o += len; |
| 124 | buf->buf.p = b_ptr(&buf->buf, len); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 125 | buf->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 126 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 129 | /* Tries to copy character <c> into the channel's buffer after some length |
| 130 | * controls. The buf->o and to_forward pointers are updated. If the channel |
| 131 | * input is closed, -2 is returned. If there is not enough room left in the |
| 132 | * buffer, -1 is returned. Otherwise the number of bytes copied is returned |
| 133 | * (1). Channel flag READ_PARTIAL is updated if some data can be transferred. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 134 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 135 | int bi_putchr(struct channel *buf, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 136 | { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 137 | if (unlikely(channel_input_closed(buf))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 138 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 139 | |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 140 | if (channel_full(buf)) |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 141 | return -1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 142 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 143 | *bi_end(&buf->buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 144 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 145 | buf->buf.i++; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 146 | buf->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 148 | if (buf->to_forward >= 1) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 149 | if (buf->to_forward != CHN_INFINITE_FORWARD) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 150 | buf->to_forward--; |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 151 | b_adv(&buf->buf, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | buf->total++; |
| 155 | return 1; |
| 156 | } |
| 157 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 158 | /* Tries to copy block <blk> at once into the channel's buffer after length |
| 159 | * controls. The buf->o and to_forward pointers are updated. If the channel |
| 160 | * input is closed, -2 is returned. If the block is too large for this buffer, |
| 161 | * -3 is returned. If there is not enough room left in the buffer, -1 is |
| 162 | * returned. Otherwise the number of bytes copied is returned (0 being a valid |
| 163 | * number). Channel flag READ_PARTIAL is updated if some data can be |
| 164 | * transferred. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 165 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 166 | int bi_putblk(struct channel *buf, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 167 | { |
| 168 | int max; |
| 169 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 170 | if (unlikely(channel_input_closed(buf))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 171 | return -2; |
| 172 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 173 | max = buffer_max_len(buf); |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 174 | if (unlikely(len > max - buffer_len(&buf->buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 175 | /* we can't write this chunk right now because the buffer is |
| 176 | * almost full or because the block is too large. Return the |
| 177 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 178 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 179 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 180 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 181 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 182 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 183 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 184 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 185 | if (unlikely(len == 0)) |
| 186 | return 0; |
| 187 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 188 | /* 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] | 189 | max = buffer_contig_space_with_res(&buf->buf, buf->buf.size - max); |
| 190 | memcpy(bi_end(&buf->buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 191 | if (len > max) |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 192 | memcpy(buf->buf.data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 193 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 194 | buf->buf.i += len; |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 195 | buf->total += len; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 196 | if (buf->to_forward) { |
| 197 | unsigned long fwd = len; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 198 | if (buf->to_forward != CHN_INFINITE_FORWARD) { |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 199 | if (fwd > buf->to_forward) |
| 200 | fwd = buf->to_forward; |
| 201 | buf->to_forward -= fwd; |
| 202 | } |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 203 | b_adv(&buf->buf, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 206 | /* notify that some data was read from the SI into the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 207 | buf->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 208 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 211 | /* Gets one text line out of a channel's buffer from a stream interface. |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 212 | * Return values : |
| 213 | * >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] | 214 | * =0 : no '\n' before end found. <str> is left undefined. |
| 215 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 216 | * The channel status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 217 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 218 | * output are full. If either of them is full, the string may be returned |
| 219 | * as is, without the '\n'. |
| 220 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 221 | int bo_getline(struct channel *buf, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 222 | { |
| 223 | int ret, max; |
| 224 | char *p; |
| 225 | |
| 226 | ret = 0; |
| 227 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 228 | |
| 229 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 230 | if (unlikely((buf->flags & CF_SHUTW) || channel_is_empty(buf))) { |
| 231 | if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 232 | ret = -1; |
| 233 | goto out; |
| 234 | } |
| 235 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 236 | p = bo_ptr(&buf->buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 237 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 238 | if (max > buf->buf.o) { |
| 239 | max = buf->buf.o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 240 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 241 | } |
| 242 | while (max) { |
| 243 | *str++ = *p; |
| 244 | ret++; |
| 245 | max--; |
| 246 | |
| 247 | if (*p == '\n') |
| 248 | break; |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 249 | p = buffer_wrap_add(&buf->buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 250 | } |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 251 | if (ret > 0 && ret < len && ret < buf->buf.o && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 252 | *(str-1) != '\n' && |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 253 | !(buf->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 254 | ret = 0; |
| 255 | out: |
| 256 | if (max) |
| 257 | *str = 0; |
| 258 | return ret; |
| 259 | } |
| 260 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 261 | /* Gets one full block of data at once from a channel's buffer, optionally from |
| 262 | * a specific offset. Return values : |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 263 | * >0 : number of bytes read, equal to requested size. |
| 264 | * =0 : not enough data available. <blk> is left undefined. |
| 265 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 266 | * The channel status is not changed. The caller must call bo_skip() to |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 267 | * update it. |
| 268 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 269 | int bo_getblk(struct channel *buf, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 270 | { |
| 271 | int firstblock; |
| 272 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 273 | if (buf->flags & CF_SHUTW) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 274 | return -1; |
| 275 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 276 | if (len + offset > buf->buf.o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 277 | if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 278 | return -1; |
| 279 | return 0; |
| 280 | } |
| 281 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 282 | firstblock = buf->buf.data + buf->buf.size - bo_ptr(&buf->buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 283 | if (firstblock > offset) { |
| 284 | if (firstblock >= len + offset) { |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 285 | memcpy(blk, bo_ptr(&buf->buf) + offset, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 286 | return len; |
| 287 | } |
| 288 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 289 | memcpy(blk, bo_ptr(&buf->buf) + offset, firstblock - offset); |
| 290 | memcpy(blk + firstblock - offset, buf->buf.data, len - firstblock + offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 291 | return len; |
| 292 | } |
| 293 | |
Willy Tarreau | 572bf90 | 2012-07-02 17:01:20 +0200 | [diff] [blame] | 294 | memcpy(blk, buf->buf.data + offset - firstblock, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 295 | return len; |
| 296 | } |
| 297 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 298 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 299 | * Local variables: |
| 300 | * c-indent-level: 8 |
| 301 | * c-basic-offset: 8 |
| 302 | * End: |
| 303 | */ |