diff --git a/include/common/buffer.h b/include/common/buffer.h index d76bc012b..3827a9b60 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -250,112 +251,6 @@ static inline void offer_buffers(void *from, unsigned int threshold) HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); } -/*************************************************************************/ -/* functions used to manipulate strings and blocks with wrapping buffers */ -/*************************************************************************/ - -/* returns > 0 if the first characters of buffer starting at offset - * relative to the buffer's head match . (empty strings do match). It is - * designed to be use with reasonably small strings (ie matches a single byte - * per iteration). This function is usable both with input and output data. To - * be used like this depending on what to match : - * - input contents : b_isteq(b, b->o, b->i, ist); - * - output contents : b_isteq(b, 0, b->o, ist); - * Return value : - * >0 : the number of matching bytes - * =0 : not enough bytes (or matching of empty string) - * <0 : non-matching byte found - */ -static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) -{ - struct ist r = ist; - const char *p; - const char *end = b_wrap(b); - - if (n < r.len) - return 0; - - p = b_peek(b, o); - while (r.len--) { - if (*p++ != *r.ptr++) - return -1; - if (unlikely(p == end)) - p = b_orig(b); - } - return ist.len; -} - -/* "eats" string from the head of buffer . Wrapping data is explicitly - * supported. It matches a single byte per iteration so strings should remain - * reasonably small. Returns : - * > 0 : number of bytes matched and eaten - * = 0 : not enough bytes (or matching an empty string) - * < 0 : non-matching byte found - */ -static inline int b_eat(struct buffer *b, const struct ist ist) -{ - int ret = b_isteq(b, 0, b_data(b), ist); - if (ret > 0) - b_del(b, ret); - return ret; -} - -/* injects string at the tail of input buffer provided that it - * fits. Wrapping is supported. It's designed for small strings as it only - * writes a single byte per iteration. Returns the number of characters copied - * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It - * will only modify the buffer upon success. In all cases, the contents are - * copied prior to reporting an error, so that the destination at least - * contains a valid but truncated string. - */ -static inline int bi_istput(struct buffer *b, const struct ist ist) -{ - const char *end = b_wrap(b); - struct ist r = ist; - char *p; - - if (r.len > (size_t)b_room(b)) - return r.len < b->size ? 0 : -1; - - p = b_tail(b); - b->len += r.len; - while (r.len--) { - *p++ = *r.ptr++; - if (unlikely(p == end)) - p = b_orig(b); - } - return ist.len; -} - - -/* injects string at the tail of output buffer provided that it - * fits. Input data is assumed not to exist and will silently be overwritten. - * Wrapping is supported. It's designed for small strings as it only writes a - * single byte per iteration. Returns the number of characters copied (ist.len), - * 0 if it temporarily does not fit or -1 if it will never fit. It will only - * modify the buffer upon success. In all cases, the contents are copied prior - * to reporting an error, so that the destination at least contains a valid - * but truncated string. - */ -static inline int bo_istput(struct buffer *b, const struct ist ist) -{ - const char *end = b_wrap(b); - struct ist r = ist; - char *p; - - if (r.len > (size_t)b_room(b)) - return r.len < b->size ? 0 : -1; - - p = b_tail(b); - b->len += r.len; - while (r.len--) { - *p++ = *r.ptr++; - if (unlikely(p == end)) - p = b_orig(b); - } - return ist.len; -} - #endif /* _COMMON_BUFFER_H */ diff --git a/include/common/istbuf.h b/include/common/istbuf.h new file mode 100644 index 000000000..2210d4a66 --- /dev/null +++ b/include/common/istbuf.h @@ -0,0 +1,126 @@ +/* + * include/common/istbuf.h + * Functions used to manipulate indirect strings with wrapping buffers. + * + * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _COMMON_ISTBUF_H +#define _COMMON_ISTBUF_H + +#include +#include +#include + + +/* b_isteq() : returns > 0 if the first characters of buffer starting + * at offset relative to the buffer's head match . (empty strings do + * match). It is designed to be used with reasonably small strings (it matches + * a single byte per loop iteration). It is expected to be used with an offset + * to skip old data. For example : + * - "input" contents : b_isteq(b, old_cnt, new_cnt, ist); + * - "output" contents : b_isteq(b, 0, old_cnt, ist); + * Return value : + * >0 : the number of matching bytes + * =0 : not enough bytes (or matching of empty string) + * <0 : non-matching byte found + */ +static inline ssize_t b_isteq(const struct buffer *b, size_t o, size_t n, const struct ist ist) +{ + struct ist r = ist; + const char *p; + const char *end = b_wrap(b); + + if (n < r.len) + return 0; + + p = b_peek(b, o); + while (r.len--) { + if (*p++ != *r.ptr++) + return -1; + if (unlikely(p == end)) + p = b_orig(b); + } + return ist.len; +} + +/* b_isteat() : "eats" string from the head of buffer . Wrapping data + * is explicitly supported. It matches a single byte per iteration so strings + * should remain reasonably small. Returns : + * > 0 : number of bytes matched and eaten + * = 0 : not enough bytes (or matching an empty string) + * < 0 : non-matching byte found + */ +static inline ssize_t b_isteat(struct buffer *b, const struct ist ist) +{ + ssize_t ret = b_isteq(b, 0, b_data(b), ist); + + if (ret > 0) + b_del(b, ret); + return ret; +} + +/* b_istput() : injects string at the tail of output buffer provided + * that it fits. Wrapping is supported. It's designed for small strings as it + * only writes a single byte per iteration. Returns the number of characters + * copied (ist.len), 0 if it temporarily does not fit, or -1 if it will never + * fit. It will only modify the buffer upon success. In all cases, the contents + * are copied prior to reporting an error, so that the destination at least + * contains a valid but truncated string. + */ +static inline ssize_t b_istput(struct buffer *b, const struct ist ist) +{ + const char *end = b_wrap(b); + struct ist r = ist; + char *p; + + if (r.len > (size_t)b_room(b)) + return r.len < b->size ? 0 : -1; + + p = b_tail(b); + b->len += r.len; + while (r.len--) { + *p++ = *r.ptr++; + if (unlikely(p == end)) + p = b_orig(b); + } + return ist.len; +} + +/* b_putist() : tries to copy as much as possible of string into buffer + * and returns the number of bytes copied (truncation is possible). It uses + * b_putblk() and is suitable for large blocks. + */ +static inline size_t b_putist(struct buffer *b, const struct ist ist) +{ + return b_putblk(b, ist.ptr, ist.len); +} + +#endif /* _COMMON_ISTBUF_H */ + +/* + * Local variables: + * c-indent-level: 8 + * c-basic-offset: 8 + * End: + */ diff --git a/src/mux_h2.c b/src/mux_h2.c index d748eaa18..8bb6c6c3a 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -728,7 +728,7 @@ static int h2c_snd_settings(struct h2c *h2c) } h2_set_frame_size(buf.str, buf.len - 9); - ret = bo_istput(res, ist2(buf.str, buf.len)); + ret = b_istput(res, ist2(buf.str, buf.len)); if (unlikely(ret <= 0)) { if (!ret) { h2c->flags |= H2_CF_MUX_MFULL; @@ -811,7 +811,7 @@ static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) write_n32(str + 9, h2c->last_sid); write_n32(str + 13, h2c->errcode); - ret = bo_istput(res, ist2(str, 17)); + ret = b_istput(res, ist2(str, 17)); if (unlikely(ret <= 0)) { if (!ret) { h2c->flags |= H2_CF_MUX_MFULL; @@ -876,7 +876,7 @@ static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) memcpy(str, "\x00\x00\x04\x03\x00", 5); write_n32(str + 5, h2s->id); write_n32(str + 9, h2s->errcode); - ret = bo_istput(res, ist2(str, 13)); + ret = b_istput(res, ist2(str, 13)); if (unlikely(ret <= 0)) { if (!ret) { @@ -939,7 +939,7 @@ static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) write_n32(str + 5, h2c->dsi); write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ? h2s->errcode : H2_ERR_STREAM_CLOSED); - ret = bo_istput(res, ist2(str, 13)); + ret = b_istput(res, ist2(str, 13)); if (unlikely(ret <= 0)) { if (!ret) { @@ -993,7 +993,7 @@ static int h2_send_empty_data_es(struct h2s *h2s) /* len: 0x000000, type: 0(DATA), flags: ES=1 */ memcpy(str, "\x00\x00\x00\x00\x01", 5); write_n32(str + 5, h2s->id); - ret = bo_istput(res, ist2(str, 9)); + ret = b_istput(res, ist2(str, 9)); if (likely(ret > 0)) { h2s->flags |= H2_SF_ES_SENT; } @@ -1179,7 +1179,7 @@ static int h2c_ack_settings(struct h2c *h2c) "\x04" "\x01" /* type : 4, flags : ACK */ "\x00\x00\x00\x00" /* stream ID */, 9); - ret = bo_istput(res, ist2(str, 9)); + ret = b_istput(res, ist2(str, 9)); if (unlikely(ret <= 0)) { if (!ret) { h2c->flags |= H2_CF_MUX_MFULL; @@ -1239,7 +1239,7 @@ static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) write_n32(str + 5, sid); write_n32(str + 9, increment); - ret = bo_istput(res, ist2(str, 13)); + ret = b_istput(res, ist2(str, 13)); if (unlikely(ret <= 0)) { if (!ret) { @@ -1325,7 +1325,7 @@ static int h2c_ack_ping(struct h2c *h2c) /* copy the original payload */ h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0); - ret = bo_istput(res, ist2(str, 17)); + ret = b_istput(res, ist2(str, 17)); if (unlikely(ret <= 0)) { if (!ret) { h2c->flags |= H2_CF_MUX_MFULL;