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