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 |
| 36 | * sent as well, in the limit of the number of bytes to forward. This must be |
| 37 | * the only method to use to schedule bytes to be forwarded. If the requested |
| 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 | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 44 | unsigned int new_forward; |
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 | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 47 | forwarded = chn->buf->i; |
| 48 | b_adv(chn->buf, chn->buf->i); |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 50 | /* Note: the case below is the only case where we may return |
| 51 | * a byte count that does not fit into a 32-bit number. |
| 52 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 53 | if (likely(chn->to_forward == CHN_INFINITE_FORWARD)) |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 54 | return bytes; |
| 55 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 56 | if (likely(bytes == CHN_INFINITE_FORWARD)) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 57 | chn->to_forward = bytes; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 58 | return bytes; |
| 59 | } |
| 60 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 61 | new_forward = chn->to_forward + bytes - forwarded; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 62 | bytes = forwarded; /* at least those bytes were scheduled */ |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 63 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 64 | if (new_forward <= chn->to_forward) { |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 65 | /* integer overflow detected, let's assume no more than 2G at once */ |
| 66 | new_forward = MID_RANGE(new_forward); |
| 67 | } |
| 68 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 69 | if (new_forward > chn->to_forward) { |
| 70 | bytes += new_forward - chn->to_forward; |
| 71 | chn->to_forward = new_forward; |
Willy Tarreau | 0bc3493 | 2011-03-28 16:25:58 +0200 | [diff] [blame] | 72 | } |
| 73 | return bytes; |
| 74 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 76 | /* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in |
| 77 | * case of success, -2 if the message is larger than the buffer size, or the |
| 78 | * number of bytes available otherwise. The send limit is automatically |
| 79 | * adjusted to the amount of data written. FIXME-20060521: handle unaligned |
| 80 | * data. Note: this function appends data to the buffer's output and possibly |
| 81 | * overwrites any pending input data which are assumed not to exist. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 83 | int bo_inject(struct channel *chn, const char *msg, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | { |
| 85 | int max; |
| 86 | |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 87 | if (len == 0) |
| 88 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 90 | if (len > chn->buf->size) { |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 91 | /* we can't write this chunk and will never be able to, because |
| 92 | * it is larger than the buffer. This must be reported as an |
| 93 | * error. Then we return -2 so that writers that don't care can |
| 94 | * ignore it and go on, and others can check for this value. |
| 95 | */ |
| 96 | return -2; |
| 97 | } |
| 98 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 99 | max = buffer_realign(chn->buf); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | if (len > max) |
| 102 | return max; |
| 103 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 104 | memcpy(chn->buf->p, msg, len); |
| 105 | chn->buf->o += len; |
| 106 | chn->buf->p = b_ptr(chn->buf, len); |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 107 | chn->total += len; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 108 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 111 | /* 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] | 112 | * 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] | 113 | * input is closed, -2 is returned. If there is not enough room left in the |
| 114 | * buffer, -1 is returned. Otherwise the number of bytes copied is returned |
| 115 | * (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] | 116 | * Channel flag CF_WAKE_WRITE is set if the write fails because the buffer is |
| 117 | * full. |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 118 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 119 | int bi_putchr(struct channel *chn, char c) |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 120 | { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 121 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 122 | return -2; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 123 | |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 124 | if (channel_full(chn)) { |
| 125 | chn->flags |= CF_WAKE_WRITE; |
Krzysztof Piotr Oledzki | 8e4b21d | 2008-04-20 21:34:47 +0200 | [diff] [blame] | 126 | return -1; |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 127 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 128 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 129 | *bi_end(chn->buf) = c; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 130 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 131 | chn->buf->i++; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 132 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 133 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 134 | if (chn->to_forward >= 1) { |
| 135 | if (chn->to_forward != CHN_INFINITE_FORWARD) |
| 136 | chn->to_forward--; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 137 | b_adv(chn->buf, 1); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 140 | chn->total++; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 141 | return 1; |
| 142 | } |
| 143 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 144 | /* 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] | 145 | * 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] | 146 | * input is closed, -2 is returned. If the block is too large for this buffer, |
| 147 | * -3 is returned. If there is not enough room left in the buffer, -1 is |
| 148 | * returned. Otherwise the number of bytes copied is returned (0 being a valid |
| 149 | * number). Channel flag READ_PARTIAL is updated if some data can be |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 150 | * transferred. Channel flag CF_WAKE_WRITE is set if the write fails because |
| 151 | * the buffer is full. |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 152 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 153 | int bi_putblk(struct channel *chn, const char *blk, int len) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 154 | { |
| 155 | int max; |
| 156 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 157 | if (unlikely(channel_input_closed(chn))) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 158 | return -2; |
| 159 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 160 | max = buffer_max_len(chn); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 161 | if (unlikely(len > max - buffer_len(chn->buf))) { |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 162 | /* we can't write this chunk right now because the buffer is |
| 163 | * almost full or because the block is too large. Return the |
| 164 | * available space or -2 if impossible. |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 165 | */ |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 166 | if (len > max) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 167 | return -3; |
Willy Tarreau | 078e294 | 2009-08-18 07:19:39 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 169 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 170 | return -1; |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 171 | } |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 172 | |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 173 | if (unlikely(len == 0)) |
| 174 | return 0; |
| 175 | |
Willy Tarreau | 591fedc | 2010-08-10 15:28:21 +0200 | [diff] [blame] | 176 | /* 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] | 177 | max = buffer_contig_space(chn->buf); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 178 | memcpy(bi_end(chn->buf), blk, MIN(len, max)); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 179 | if (len > max) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 180 | memcpy(chn->buf->data, blk + max, len - max); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 181 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 182 | chn->buf->i += len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 183 | chn->total += len; |
| 184 | if (chn->to_forward) { |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 185 | unsigned long fwd = len; |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 186 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 187 | if (fwd > chn->to_forward) |
| 188 | fwd = chn->to_forward; |
| 189 | chn->to_forward -= fwd; |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 190 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 191 | b_adv(chn->buf, fwd); |
Willy Tarreau | aeac319 | 2009-08-31 08:09:57 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Willy Tarreau | fb0e920 | 2009-09-23 23:47:55 +0200 | [diff] [blame] | 194 | /* notify that some data was read from the SI into the buffer */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 195 | chn->flags |= CF_READ_PARTIAL; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 196 | return len; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Willy Tarreau | b034b25 | 2014-12-08 18:14:53 +0100 | [diff] [blame] | 199 | /* Tries to copy the whole buffer <buf> into the channel's buffer after length |
| 200 | * controls. It will only succeed if the target buffer is empty, in which case |
| 201 | * it will simply swap the buffers. The buffer not attached to the channel is |
| 202 | * returned so that the caller can store it locally. The chn->buf->o and |
| 203 | * to_forward pointers are updated. If the output buffer is a dummy buffer or |
| 204 | * if it still contains data <buf> is returned, indicating that nothing could |
| 205 | * be done. Channel flag READ_PARTIAL is updated if some data can be transferred. |
| 206 | * The chunk's length is updated with the number of bytes sent. On errors, NULL |
| 207 | * is returned. Note that only buf->i is considered. |
| 208 | */ |
| 209 | struct buffer *bi_swpbuf(struct channel *chn, struct buffer *buf) |
| 210 | { |
| 211 | struct buffer *old; |
| 212 | |
| 213 | if (unlikely(channel_input_closed(chn))) |
| 214 | return NULL; |
| 215 | |
| 216 | if (!chn->buf->size || !buffer_empty(chn->buf)) { |
| 217 | chn->flags |= CF_WAKE_WRITE; |
| 218 | return buf; |
| 219 | } |
| 220 | |
| 221 | old = chn->buf; |
| 222 | chn->buf = buf; |
| 223 | |
| 224 | if (!buf->i) |
| 225 | return old; |
| 226 | |
| 227 | chn->total += buf->i; |
| 228 | |
| 229 | if (chn->to_forward) { |
| 230 | unsigned long fwd = buf->i; |
| 231 | if (chn->to_forward != CHN_INFINITE_FORWARD) { |
| 232 | if (fwd > chn->to_forward) |
| 233 | fwd = chn->to_forward; |
| 234 | chn->to_forward -= fwd; |
| 235 | } |
| 236 | b_adv(chn->buf, fwd); |
| 237 | } |
| 238 | |
| 239 | /* notify that some data was read from the SI into the buffer */ |
| 240 | chn->flags |= CF_READ_PARTIAL; |
| 241 | return old; |
| 242 | } |
| 243 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 244 | /* 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] | 245 | * Return values : |
| 246 | * >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] | 247 | * =0 : no '\n' before end found. <str> is left undefined. |
| 248 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 249 | * 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] | 250 | * update it. The '\n' is waited for as long as neither the buffer nor the |
| 251 | * output are full. If either of them is full, the string may be returned |
| 252 | * as is, without the '\n'. |
| 253 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 254 | int bo_getline(struct channel *chn, char *str, int len) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 255 | { |
| 256 | int ret, max; |
| 257 | char *p; |
| 258 | |
| 259 | ret = 0; |
| 260 | max = len; |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 261 | |
| 262 | /* closed or empty + imminent close = -1; empty = 0 */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 263 | if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) { |
| 264 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 265 | ret = -1; |
| 266 | goto out; |
| 267 | } |
| 268 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 269 | p = bo_ptr(chn->buf); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 270 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 271 | if (max > chn->buf->o) { |
| 272 | max = chn->buf->o; |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 273 | str[max-1] = 0; |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 274 | } |
| 275 | while (max) { |
| 276 | *str++ = *p; |
| 277 | ret++; |
| 278 | max--; |
| 279 | |
| 280 | if (*p == '\n') |
| 281 | break; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 282 | p = buffer_wrap_add(chn->buf, p + 1); |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 283 | } |
Willy Tarreau | 82de2b6 | 2013-12-10 18:58:23 +0100 | [diff] [blame] | 284 | if (ret > 0 && ret < len && |
| 285 | (ret < chn->buf->o || !channel_full(chn)) && |
Willy Tarreau | 2e1dd3d | 2009-09-23 22:56:07 +0200 | [diff] [blame] | 286 | *(str-1) != '\n' && |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 287 | !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 4fe7a2e | 2009-09-01 06:41:32 +0200 | [diff] [blame] | 288 | ret = 0; |
| 289 | out: |
| 290 | if (max) |
| 291 | *str = 0; |
| 292 | return ret; |
| 293 | } |
| 294 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 295 | /* Gets one full block of data at once from a channel's buffer, optionally from |
| 296 | * a specific offset. Return values : |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 297 | * >0 : number of bytes read, equal to requested size. |
| 298 | * =0 : not enough data available. <blk> is left undefined. |
| 299 | * <0 : no more bytes readable because output is shut. |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 300 | * 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] | 301 | * update it. |
| 302 | */ |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 303 | int bo_getblk(struct channel *chn, char *blk, int len, int offset) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 304 | { |
| 305 | int firstblock; |
| 306 | |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 307 | if (chn->flags & CF_SHUTW) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 308 | return -1; |
| 309 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 310 | if (len + offset > chn->buf->o) { |
Willy Tarreau | 974ced6 | 2012-10-12 23:11:02 +0200 | [diff] [blame] | 311 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 312 | return -1; |
| 313 | return 0; |
| 314 | } |
| 315 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 316 | firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 317 | if (firstblock > offset) { |
| 318 | if (firstblock >= len + offset) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 319 | memcpy(blk, bo_ptr(chn->buf) + offset, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 320 | return len; |
| 321 | } |
| 322 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 323 | memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset); |
| 324 | memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 325 | return len; |
| 326 | } |
| 327 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 328 | memcpy(blk, chn->buf->data + offset - firstblock, len); |
Willy Tarreau | 74b08c9 | 2010-09-08 17:04:31 +0200 | [diff] [blame] | 329 | return len; |
| 330 | } |
| 331 | |
Krzysztof Piotr Oledzki | ba8d7d3 | 2009-10-10 21:06:03 +0200 | [diff] [blame] | 332 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 333 | * Local variables: |
| 334 | * c-indent-level: 8 |
| 335 | * c-basic-offset: 8 |
| 336 | * End: |
| 337 | */ |