2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2012-08-24 17:22:53 +00:00
|
|
|
* Channel management functions.
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
2012-08-24 17:22:53 +00:00
|
|
|
* Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-10 19:06:03 +00:00
|
|
|
#include <ctype.h>
|
2007-01-01 20:38:07 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <string.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
|
|
|
|
#include <common/config.h>
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <common/memory.h>
|
2012-08-24 17:22:53 +00:00
|
|
|
#include <common/buffer.h>
|
|
|
|
#include <proto/channel.h>
|
2009-08-17 05:23:33 +00:00
|
|
|
#include <types/global.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-08-24 17:22:53 +00:00
|
|
|
|
|
|
|
/* Note: this code has not yet been completely cleaned up and still refers to
|
|
|
|
* the word "buffer" when "channel" is meant instead.
|
|
|
|
*/
|
2007-05-13 17:56:02 +00:00
|
|
|
struct pool_head *pool2_buffer;
|
|
|
|
|
|
|
|
|
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
|
|
|
int init_buffer()
|
|
|
|
{
|
2012-07-02 13:11:27 +00:00
|
|
|
pool2_buffer = create_pool("buffer", sizeof(struct channel) + global.tune.bufsize, MEM_F_SHARED);
|
2007-05-13 17:56:02 +00:00
|
|
|
return pool2_buffer != NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-28 14:25:58 +00:00
|
|
|
/* Schedule up to <bytes> more bytes to be forwarded by the buffer without notifying
|
|
|
|
* the task. Any pending data in the buffer is scheduled to be sent as well,
|
|
|
|
* in the limit of the number of bytes to forward. This must be the only method
|
|
|
|
* to use to schedule bytes to be sent. If the requested number is too large, it
|
|
|
|
* is automatically adjusted. The number of bytes taken into account is returned.
|
2012-03-01 15:08:30 +00:00
|
|
|
* Directly touching ->to_forward will cause lockups when ->o goes down to
|
2011-03-28 14:25:58 +00:00
|
|
|
* zero if nobody is ready to push the remaining data.
|
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
unsigned long long buffer_forward(struct channel *buf, unsigned long long bytes)
|
2011-03-28 14:25:58 +00:00
|
|
|
{
|
|
|
|
unsigned int new_forward;
|
2012-03-01 17:19:58 +00:00
|
|
|
unsigned int forwarded;
|
2012-05-05 21:32:27 +00:00
|
|
|
unsigned int bytes32;
|
2011-03-28 14:25:58 +00:00
|
|
|
|
2012-05-05 21:32:27 +00:00
|
|
|
bytes32 = bytes;
|
|
|
|
|
|
|
|
/* hint: avoid comparisons on long long for the fast case, since if the
|
|
|
|
* length does not fit in an unsigned it, it will never be forwarded at
|
|
|
|
* once anyway.
|
|
|
|
*/
|
|
|
|
if (bytes <= ~0U) {
|
2012-07-02 15:01:20 +00:00
|
|
|
if (bytes32 <= buf->buf.i) {
|
2012-05-05 21:32:27 +00:00
|
|
|
/* OK this amount of bytes might be forwarded at once */
|
|
|
|
if (!bytes32)
|
|
|
|
return 0;
|
2012-08-24 20:56:11 +00:00
|
|
|
b_adv(&buf->buf, bytes32);
|
2012-05-05 21:32:27 +00:00
|
|
|
return bytes;
|
|
|
|
}
|
2011-03-28 14:25:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
forwarded = buf->buf.i;
|
2012-08-24 20:56:11 +00:00
|
|
|
b_adv(&buf->buf, buf->buf.i);
|
2011-03-28 14:25:58 +00:00
|
|
|
|
|
|
|
/* Note: the case below is the only case where we may return
|
|
|
|
* a byte count that does not fit into a 32-bit number.
|
|
|
|
*/
|
2012-08-27 21:14:58 +00:00
|
|
|
if (likely(buf->to_forward == CHN_INFINITE_FORWARD))
|
2011-03-28 14:25:58 +00:00
|
|
|
return bytes;
|
|
|
|
|
2012-08-27 21:14:58 +00:00
|
|
|
if (likely(bytes == CHN_INFINITE_FORWARD)) {
|
2012-03-01 17:19:58 +00:00
|
|
|
buf->to_forward = bytes;
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_forward = buf->to_forward + bytes - forwarded;
|
|
|
|
bytes = forwarded; /* at least those bytes were scheduled */
|
2011-03-28 14:25:58 +00:00
|
|
|
|
|
|
|
if (new_forward <= buf->to_forward) {
|
|
|
|
/* integer overflow detected, let's assume no more than 2G at once */
|
|
|
|
new_forward = MID_RANGE(new_forward);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_forward > buf->to_forward) {
|
|
|
|
bytes += new_forward - buf->to_forward;
|
|
|
|
buf->to_forward = new_forward;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2008-04-20 19:34:47 +00:00
|
|
|
/* writes <len> bytes from message <msg> to buffer <buf>. Returns -1 in case of
|
2009-08-18 05:19:39 +00:00
|
|
|
* success, -2 if the message is larger than the buffer size, or the number of
|
|
|
|
* bytes available otherwise. The send limit is automatically adjusted with the
|
|
|
|
* amount of data written. FIXME-20060521: handle unaligned data.
|
2012-03-02 19:14:45 +00:00
|
|
|
* Note: this function appends data to the buffer's output and possibly overwrites
|
|
|
|
* any pending input data which are assumed not to exist.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
int bo_inject(struct channel *buf, const char *msg, int len)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
int max;
|
|
|
|
|
2009-08-31 06:09:57 +00:00
|
|
|
if (len == 0)
|
|
|
|
return -1;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
if (len > buf->buf.size) {
|
2009-08-18 05:19:39 +00:00
|
|
|
/* we can't write this chunk and will never be able to, because
|
|
|
|
* it is larger than the buffer. This must be reported as an
|
|
|
|
* error. Then we return -2 so that writers that don't care can
|
|
|
|
* ignore it and go on, and others can check for this value.
|
|
|
|
*/
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
max = buffer_realign(&buf->buf);
|
2009-08-31 06:09:57 +00:00
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
if (len > max)
|
|
|
|
return max;
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
memcpy(buf->buf.p, msg, len);
|
|
|
|
buf->buf.o += len;
|
|
|
|
buf->buf.p = b_ptr(&buf->buf, len);
|
2007-01-01 23:28:21 +00:00
|
|
|
buf->total += len;
|
2008-04-20 19:34:47 +00:00
|
|
|
return -1;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2010-09-08 15:04:31 +00:00
|
|
|
/* Tries to copy character <c> into buffer <buf> after length controls. The
|
2012-03-01 15:08:30 +00:00
|
|
|
* ->o and to_forward pointers are updated. If the buffer's input is
|
2010-09-08 15:04:31 +00:00
|
|
|
* closed, -2 is returned. If there is not enough room left in the buffer, -1
|
|
|
|
* is returned. Otherwise the number of bytes copied is returned (1). Buffer
|
2012-08-27 18:53:34 +00:00
|
|
|
* flag READ_PARTIAL is updated if some data can be transferred.
|
2007-01-01 20:38:07 +00:00
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
int bi_putchr(struct channel *buf, char c)
|
2007-01-01 20:38:07 +00:00
|
|
|
{
|
2010-09-08 15:04:31 +00:00
|
|
|
if (unlikely(buffer_input_closed(buf)))
|
|
|
|
return -2;
|
2007-01-01 20:38:07 +00:00
|
|
|
|
2012-08-27 18:46:07 +00:00
|
|
|
if (channel_full(buf))
|
2008-04-20 19:34:47 +00:00
|
|
|
return -1;
|
2007-01-01 20:38:07 +00:00
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
*bi_end(&buf->buf) = c;
|
2010-09-08 15:04:31 +00:00
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
buf->buf.i++;
|
2012-08-27 21:14:58 +00:00
|
|
|
buf->flags |= CF_READ_PARTIAL;
|
2010-09-08 15:04:31 +00:00
|
|
|
|
|
|
|
if (buf->to_forward >= 1) {
|
2012-08-27 21:14:58 +00:00
|
|
|
if (buf->to_forward != CHN_INFINITE_FORWARD)
|
2010-09-08 15:04:31 +00:00
|
|
|
buf->to_forward--;
|
2012-08-24 20:56:11 +00:00
|
|
|
b_adv(&buf->buf, 1);
|
2010-09-08 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf->total++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tries to copy block <blk> at once into buffer <buf> after length controls.
|
2012-03-01 15:08:30 +00:00
|
|
|
* The ->o and to_forward pointers are updated. If the buffer's input is
|
2010-09-08 15:04:31 +00:00
|
|
|
* closed, -2 is returned. If the block is too large for this buffer, -3 is
|
|
|
|
* returned. If there is not enough room left in the buffer, -1 is returned.
|
|
|
|
* Otherwise the number of bytes copied is returned (0 being a valid number).
|
2012-08-27 18:53:34 +00:00
|
|
|
* Buffer flag READ_PARTIAL is updated if some data can be transferred.
|
2010-09-08 15:04:31 +00:00
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
int bi_putblk(struct channel *buf, const char *blk, int len)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
|
|
|
int max;
|
|
|
|
|
|
|
|
if (unlikely(buffer_input_closed(buf)))
|
|
|
|
return -2;
|
|
|
|
|
2010-08-10 13:28:21 +00:00
|
|
|
max = buffer_max_len(buf);
|
2012-07-02 15:01:20 +00:00
|
|
|
if (unlikely(len > max - buffer_len(&buf->buf))) {
|
2010-08-10 13:28:21 +00:00
|
|
|
/* we can't write this chunk right now because the buffer is
|
|
|
|
* almost full or because the block is too large. Return the
|
|
|
|
* available space or -2 if impossible.
|
2009-08-18 05:19:39 +00:00
|
|
|
*/
|
2010-08-10 13:28:21 +00:00
|
|
|
if (len > max)
|
2010-09-08 15:04:31 +00:00
|
|
|
return -3;
|
2009-08-18 05:19:39 +00:00
|
|
|
|
2010-09-08 15:04:31 +00:00
|
|
|
return -1;
|
2010-08-10 13:28:21 +00:00
|
|
|
}
|
2007-01-01 20:38:07 +00:00
|
|
|
|
2010-09-08 15:04:31 +00:00
|
|
|
if (unlikely(len == 0))
|
|
|
|
return 0;
|
|
|
|
|
2010-08-10 13:28:21 +00:00
|
|
|
/* OK so the data fits in the buffer in one or two blocks */
|
2012-07-02 15:01:20 +00:00
|
|
|
max = buffer_contig_space_with_res(&buf->buf, buf->buf.size - max);
|
|
|
|
memcpy(bi_end(&buf->buf), blk, MIN(len, max));
|
2009-08-31 06:09:57 +00:00
|
|
|
if (len > max)
|
2012-07-02 15:01:20 +00:00
|
|
|
memcpy(buf->buf.data, blk + max, len - max);
|
2007-01-01 20:38:07 +00:00
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
buf->buf.i += len;
|
2009-08-31 06:09:57 +00:00
|
|
|
buf->total += len;
|
2009-09-20 10:07:52 +00:00
|
|
|
if (buf->to_forward) {
|
|
|
|
unsigned long fwd = len;
|
2012-08-27 21:14:58 +00:00
|
|
|
if (buf->to_forward != CHN_INFINITE_FORWARD) {
|
2009-09-20 10:07:52 +00:00
|
|
|
if (fwd > buf->to_forward)
|
|
|
|
fwd = buf->to_forward;
|
|
|
|
buf->to_forward -= fwd;
|
|
|
|
}
|
2012-08-24 20:56:11 +00:00
|
|
|
b_adv(&buf->buf, fwd);
|
2009-08-31 06:09:57 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 21:47:55 +00:00
|
|
|
/* notify that some data was read from the SI into the buffer */
|
2012-08-27 21:14:58 +00:00
|
|
|
buf->flags |= CF_READ_PARTIAL;
|
2010-09-08 15:04:31 +00:00
|
|
|
return len;
|
2007-01-01 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
2010-09-08 15:04:31 +00:00
|
|
|
/* Gets one text line out of a buffer from a stream interface.
|
2009-09-01 04:41:32 +00:00
|
|
|
* Return values :
|
|
|
|
* >0 : number of bytes read. Includes the \n if present before len or end.
|
2010-09-08 15:04:31 +00:00
|
|
|
* =0 : no '\n' before end found. <str> is left undefined.
|
|
|
|
* <0 : no more bytes readable because output is shut.
|
2012-05-07 09:56:55 +00:00
|
|
|
* The buffer status is not changed. The caller must call bo_skip() to
|
2009-09-01 04:41:32 +00:00
|
|
|
* update it. The '\n' is waited for as long as neither the buffer nor the
|
|
|
|
* output are full. If either of them is full, the string may be returned
|
|
|
|
* as is, without the '\n'.
|
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
int bo_getline(struct channel *buf, char *str, int len)
|
2009-09-01 04:41:32 +00:00
|
|
|
{
|
|
|
|
int ret, max;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
max = len;
|
2010-09-08 15:04:31 +00:00
|
|
|
|
|
|
|
/* closed or empty + imminent close = -1; empty = 0 */
|
2012-08-27 21:14:58 +00:00
|
|
|
if (unlikely((buf->flags & CF_SHUTW) || channel_is_empty(buf))) {
|
|
|
|
if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW))
|
2009-09-01 04:41:32 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
p = bo_ptr(&buf->buf);
|
2009-09-01 04:41:32 +00:00
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
if (max > buf->buf.o) {
|
|
|
|
max = buf->buf.o;
|
2009-09-23 20:56:07 +00:00
|
|
|
str[max-1] = 0;
|
2009-09-01 04:41:32 +00:00
|
|
|
}
|
|
|
|
while (max) {
|
|
|
|
*str++ = *p;
|
|
|
|
ret++;
|
|
|
|
max--;
|
|
|
|
|
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
2012-07-02 15:01:20 +00:00
|
|
|
p = buffer_wrap_add(&buf->buf, p + 1);
|
2009-09-01 04:41:32 +00:00
|
|
|
}
|
2012-07-02 15:01:20 +00:00
|
|
|
if (ret > 0 && ret < len && ret < buf->buf.o &&
|
2009-09-23 20:56:07 +00:00
|
|
|
*(str-1) != '\n' &&
|
2012-08-27 21:14:58 +00:00
|
|
|
!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW)))
|
2009-09-01 04:41:32 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
if (max)
|
|
|
|
*str = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-08 15:04:31 +00:00
|
|
|
/* Gets one full block of data at once from a buffer, optionally from a
|
|
|
|
* specific offset. Return values :
|
|
|
|
* >0 : number of bytes read, equal to requested size.
|
|
|
|
* =0 : not enough data available. <blk> is left undefined.
|
|
|
|
* <0 : no more bytes readable because output is shut.
|
2012-05-07 09:56:55 +00:00
|
|
|
* The buffer status is not changed. The caller must call bo_skip() to
|
2010-09-08 15:04:31 +00:00
|
|
|
* update it.
|
|
|
|
*/
|
2012-07-02 13:11:27 +00:00
|
|
|
int bo_getblk(struct channel *buf, char *blk, int len, int offset)
|
2010-09-08 15:04:31 +00:00
|
|
|
{
|
|
|
|
int firstblock;
|
|
|
|
|
2012-08-27 21:14:58 +00:00
|
|
|
if (buf->flags & CF_SHUTW)
|
2010-09-08 15:04:31 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
if (len + offset > buf->buf.o) {
|
2012-08-27 21:14:58 +00:00
|
|
|
if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW))
|
2010-09-08 15:04:31 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
firstblock = buf->buf.data + buf->buf.size - bo_ptr(&buf->buf);
|
2010-09-08 15:04:31 +00:00
|
|
|
if (firstblock > offset) {
|
|
|
|
if (firstblock >= len + offset) {
|
2012-07-02 15:01:20 +00:00
|
|
|
memcpy(blk, bo_ptr(&buf->buf) + offset, len);
|
2010-09-08 15:04:31 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
memcpy(blk, bo_ptr(&buf->buf) + offset, firstblock - offset);
|
|
|
|
memcpy(blk + firstblock - offset, buf->buf.data, len - firstblock + offset);
|
2010-09-08 15:04:31 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:01:20 +00:00
|
|
|
memcpy(blk, buf->buf.data + offset - firstblock, len);
|
2010-09-08 15:04:31 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|