Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/filters.h |
| 3 | * This file defines function prototypes for stream filters management. |
| 4 | * |
| 5 | * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | #ifndef _PROTO_FILTERS_H |
| 22 | #define _PROTO_FILTERS_H |
| 23 | |
| 24 | #include <types/channel.h> |
| 25 | #include <types/filters.h> |
| 26 | #include <types/proto_http.h> |
| 27 | #include <types/proxy.h> |
| 28 | #include <types/stream.h> |
| 29 | |
| 30 | #include <proto/channel.h> |
| 31 | |
| 32 | /* Useful macros to access per-channel values. It can be safely used inside |
| 33 | * filters. */ |
| 34 | #define CHN_IDX(chn) (((chn)->flags & CF_ISRESP) == CF_ISRESP) |
| 35 | #define FLT_NXT(flt, chn) ((flt)->next[CHN_IDX(chn)]) |
| 36 | #define FLT_FWD(flt, chn) ((flt)->fwd[CHN_IDX(chn)]) |
| 37 | |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 38 | #define HAS_FILTERS(strm) ((strm)->strm_flt.has_filters) |
| 39 | |
| 40 | #define FLT_STRM_CB_IMPL_0(strm, call) \ |
| 41 | do { \ |
| 42 | if (HAS_FILTERS(strm)) { call; } \ |
| 43 | } while (0) |
| 44 | #define FLT_STRM_CB_IMPL_1(strm, call, default_ret, ...) \ |
| 45 | (HAS_FILTERS(strm) ? call : default_ret) |
| 46 | #define FLT_STRM_CB_IMPL_2(strm, call, default_ret, on_error) \ |
| 47 | ({ \ |
| 48 | int _ret; \ |
| 49 | if (HAS_FILTERS(strm)) { \ |
| 50 | _ret = call; \ |
| 51 | if (_ret < 0) { on_error; } \ |
| 52 | } \ |
| 53 | else \ |
| 54 | _ret = default_ret; \ |
| 55 | _ret; \ |
| 56 | }) |
| 57 | #define FLT_STRM_CB_IMPL_3(strm, call, default_ret, on_error, on_wait) \ |
| 58 | ({ \ |
| 59 | int _ret; \ |
| 60 | if (HAS_FILTERS(strm)) { \ |
| 61 | _ret = call; \ |
| 62 | if (_ret < 0) { on_error; } \ |
| 63 | if (!_ret) { on_wait; } \ |
| 64 | } \ |
| 65 | else \ |
| 66 | _ret = default_ret; \ |
| 67 | _ret; \ |
| 68 | }) |
| 69 | |
| 70 | #define FLT_STRM_CB_IMPL_X(strm, call, A, B, C, CB_IMPL, ...) CB_IMPL |
| 71 | |
| 72 | #define FLT_STRM_CB(strm, call, ...) \ |
| 73 | FLT_STRM_CB_IMPL_X(strm, call, ##__VA_ARGS__, \ |
| 74 | FLT_STRM_CB_IMPL_3(strm, call, ##__VA_ARGS__), \ |
| 75 | FLT_STRM_CB_IMPL_2(strm, call, ##__VA_ARGS__), \ |
| 76 | FLT_STRM_CB_IMPL_1(strm, call, ##__VA_ARGS__), \ |
| 77 | FLT_STRM_CB_IMPL_0(strm, call)) |
| 78 | |
| 79 | #define CALL_FILTER_ANALYZER(analyzer, strm, chn, bit) \ |
| 80 | if (!HAS_FILTERS(strm) || analyzer((strm), (chn), bit)) ; else break |
| 81 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 82 | extern struct pool_head *pool2_filter; |
| 83 | |
| 84 | int flt_init(struct proxy *p); |
| 85 | void flt_deinit(struct proxy *p); |
| 86 | int flt_check(struct proxy *p); |
| 87 | |
| 88 | int flt_stream_start(struct stream *s); |
| 89 | void flt_stream_stop(struct stream *s); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 90 | int flt_set_stream_backend(struct stream *s, struct proxy *be); |
| 91 | int flt_stream_init(struct stream *s); |
| 92 | void flt_stream_release(struct stream *s, int only_backend); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 93 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 94 | int flt_http_data(struct stream *s, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 95 | int flt_http_chunk_trailers(struct stream *s, struct http_msg *msg); |
| 96 | int flt_http_end(struct stream *s, struct http_msg *msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 97 | int flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 98 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 99 | void flt_http_reset(struct stream *s, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 100 | void flt_http_reply(struct stream *s, short status, const struct chunk *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 101 | |
| 102 | int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 103 | int flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 104 | int flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 105 | int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 106 | |
| 107 | int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 108 | |
| 109 | void flt_register_keywords(struct flt_kw_list *kwl); |
| 110 | struct flt_kw *flt_find_kw(const char *kw); |
| 111 | void flt_dump_kws(char **out); |
| 112 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame^] | 113 | /* Helper function that returns the "global" state of filters attached to a |
| 114 | * stream. */ |
| 115 | static inline struct strm_flt * |
| 116 | strm_flt(struct stream *s) |
| 117 | { |
| 118 | return &s->strm_flt; |
| 119 | } |
| 120 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 121 | static inline void |
| 122 | flt_set_forward_data(struct filter *filter, struct channel *chn) |
| 123 | { |
| 124 | filter->flags[CHN_IDX(chn)] |= FILTER_FL_FORWARD_DATA; |
| 125 | } |
| 126 | |
| 127 | static inline void |
| 128 | flt_reset_forward_data(struct filter *filter, struct channel *chn) |
| 129 | { |
| 130 | filter->flags[CHN_IDX(chn)] &= ~FILTER_FL_FORWARD_DATA; |
| 131 | } |
| 132 | |
| 133 | static inline int |
| 134 | flt_want_forward_data(struct filter *filter, const struct channel *chn) |
| 135 | { |
| 136 | return filter->flags[CHN_IDX(chn)] & FILTER_FL_FORWARD_DATA; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /* This function must be called when a filter alter incoming data. It updates |
| 141 | * next offset value of all filter's predecessors. Do not call this function |
| 142 | * when a filter change the size of incomding data leads to an undefined |
| 143 | * behavior. |
| 144 | * |
| 145 | * This is the filter's responsiblitiy to update data itself. For now, it is |
| 146 | * unclear to know how to handle data updates, so we do the minimum here. For |
| 147 | * example, if you filter an HTTP message, we must update msg->next and |
| 148 | * msg->chunk_len values. |
| 149 | */ |
| 150 | static inline void |
| 151 | flt_change_next_size(struct filter *filter, struct channel *chn, int len) |
| 152 | { |
| 153 | struct stream *s = chn_strm(chn); |
| 154 | struct filter *f; |
| 155 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame^] | 156 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 157 | if (f == filter) |
| 158 | break; |
| 159 | FLT_NXT(f, chn) += len; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* This function must be called when a filter alter forwarded data. It updates |
| 164 | * offset values (next and forward) of all filters. Do not call this function |
| 165 | * when a filter change the size of forwarded data leads to an undefined |
| 166 | * behavior. |
| 167 | * |
| 168 | * This is the filter's responsiblitiy to update data itself. For now, it is |
| 169 | * unclear to know how to handle data updates, so we do the minimum here. For |
| 170 | * example, if you filter an HTTP message, we must update msg->next and |
| 171 | * msg->chunk_len values. |
| 172 | */ |
| 173 | static inline void |
| 174 | flt_change_forward_size(struct filter *filter, struct channel *chn, int len) |
| 175 | { |
| 176 | struct stream *s = chn_strm(chn); |
| 177 | struct filter *f; |
| 178 | int before = 1; |
| 179 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame^] | 180 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 181 | if (f == filter) |
| 182 | before = 0; |
| 183 | if (before) |
| 184 | FLT_FWD(f, chn) += len; |
| 185 | FLT_NXT(f, chn) += len; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | #endif /* _PROTO_FILTERS_H */ |