blob: 84469469a1156d621a8d61a27b3eccf4ade0dc72 [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * include/types/filteers.h
3 * This file defines everything related to stream filters.
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 _TYPES_FILTERS_H
22#define _TYPES_FILTERS_H
23
24#include <common/config.h>
25#include <common/mini-clist.h>
26
27struct http_msg;
28struct proxy;
29struct stream;
30struct channel;
Christopher Faulet443ea1a2016-02-04 13:40:26 +010031struct flt_conf;
Christopher Fauletd7c91962015-04-30 11:48:27 +020032struct filter;
33
34/* Descriptor for a "filter" keyword. The ->parse() function returns 0 in case
35 * of success, or a combination of ERR_* flags if an error is encountered. The
36 * function pointer can be NULL if not implemented. The function also has an
37 * access to the current "server" config line. The ->skip value tells the parser
38 * how many words have to be skipped after the keyword. If the function needs to
39 * parse more keywords, it needs to update cur_arg.
40 */
41struct flt_kw {
42 const char *kw;
43 int (*parse)(char **args, int *cur_arg, struct proxy *px,
Christopher Faulet443ea1a2016-02-04 13:40:26 +010044 struct flt_conf *fconf, char **err);
Christopher Fauletd7c91962015-04-30 11:48:27 +020045};
46
47/*
48 * A keyword list. It is a NULL-terminated array of keywords. It embeds a struct
49 * list in order to be linked to other lists, allowing it to easily be declared
50 * where it is needed, and linked without duplicating data nor allocating
51 * memory. It is also possible to indicate a scope for the keywords.
52 */
53struct flt_kw_list {
54 const char *scope;
55 struct list list;
56 struct flt_kw kw[VAR_ARRAY];
57};
58
59/*
Christopher Fauletd7c91962015-04-30 11:48:27 +020060 * Callbacks available on a filter:
61 *
62 * - init : Initializes the filter for a proxy. Returns a
63 * negative value if an error occurs.
64 * - deinit : Cleans up what the init function has done.
65 * - check : Check the filter config for a proxy. Returns the
66 * number of errors encountered.
67 *
68 *
69 * - stream_start : Called when a stream is started. This callback will
70 * only be called for filters defined on a proxy with
71 * the frontend capability.
72 * Returns a negative value if an error occurs, any
73 * other value otherwise.
74 * - stream_stop : Called when a stream is stopped. This callback will
75 * only be called for filters defined on a proxy with
76 * the frontend capability.
77 *
78 *
79 * - channel_start_analyze: Called when a filter starts to analyze a channel.
80 * Returns a negative value if an error occurs, 0 if
81 * it needs to wait, any other value otherwise.
82 * - channel_analyze : Called before each analyzer attached to a channel,
83 * expects analyzers responsible for data sending.
84 * Returns a negative value if an error occurs, 0 if
85 * it needs to wait, any other value otherwise.
86 * - channel_end_analyze : Called when all other analyzers have finished their
87 * processing.
88 * Returns a negative value if an error occurs, 0 if
89 * it needs to wait, any other value otherwise.
90 *
91 *
Christopher Fauletd7c91962015-04-30 11:48:27 +020092 * - http_data : Called when unparsed body data are available.
93 * Returns a negative value if an error occurs, else
94 * the number of consumed bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +020095 * - http_chunk_trailers : Called when part of trailer headers of a
96 * chunk-encoded request/response are ready to be
97 * processed.
98 * Returns a negative value if an error occurs, any
99 * other value otherwise.
100 * - http_end : Called when all the request/response has been
101 * processed and all body data has been forwarded.
102 * Returns a negative value if an error occurs, 0 if
103 * it needs to wait for some reason, any other value
104 * otherwise.
105 * - http_reset : Called when the HTTP message is reseted. It happens
106 * when a 100-continue response is received.
107 * Returns nothing.
108 * - http_reply : Called when, at any time, HA proxy decides to stop
109 * the HTTP message's processing and to send a message
110 * to the client (mainly, when an error or a redirect
111 * occur).
112 * Returns nothing.
113 * - http_forward_data : Called when some data can be consumed.
114 * Returns a negative value if an error occurs, else
115 * the number of forwarded bytes.
116 * - tcp_data : Called when unparsed data are available.
117 * Returns a negative value if an error occurs, else
118 * the number of consumed bytes.
119 * - tcp_forward_data : Called when some data can be consumed.
120 * Returns a negative value if an error occurs, else
121 * or the number of forwarded bytes.
122 */
123struct flt_ops {
124 /*
125 * Callbacks to manage the filter lifecycle
126 */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100127 int (*init) (struct proxy *p, struct flt_conf *fconf);
128 void (*deinit)(struct proxy *p, struct flt_conf *fconf);
129 int (*check) (struct proxy *p, struct flt_conf *fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200130
131 /*
132 * Stream callbacks
133 */
134 int (*stream_start) (struct stream *s, struct filter *f);
135 void (*stream_stop) (struct stream *s, struct filter *f);
136
137 /*
138 * Channel callbacks
139 */
140 int (*channel_start_analyze)(struct stream *s, struct filter *f, struct channel *chn);
141 int (*channel_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit);
142 int (*channel_end_analyze) (struct stream *s, struct filter *f, struct channel *chn);
143
144 /*
145 * HTTP callbacks
146 */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200147 int (*http_data) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200148 int (*http_chunk_trailers)(struct stream *s, struct filter *f, struct http_msg *msg);
149 int (*http_end) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100150 int (*http_forward_data) (struct stream *s, struct filter *f, struct http_msg *msg,
151 unsigned int len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200152
Christopher Faulet309c6412015-12-02 09:57:32 +0100153 void (*http_reset) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200154 void (*http_reply) (struct stream *s, struct filter *f, short status,
155 const struct chunk *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200156
157 /*
158 * TCP callbacks
159 */
160 int (*tcp_data) (struct stream *s, struct filter *f, struct channel *chn);
161 int (*tcp_forward_data)(struct stream *s, struct filter *f, struct channel *chn,
162 unsigned int len);
163};
164
Christopher Fauletda02e172015-12-04 09:25:05 +0100165/* Flags set on a filter instance */
166#define FLT_FL_IS_BACKEND_FILTER 0x0001 /* The filter is a backend filter */
167#define FLT_FL_IS_REQ_DATA_FILTER 0x0002 /* The filter will parse data on the request channel */
168#define FLT_FL_IS_RSP_DATA_FILTER 0x0004 /* The filter will parse data on the response channel */
169
170
171/* Flags set on the stream, common to all filters attached to its stream */
172#define STRM_FLT_FL_HAS_FILTERS 0x0001 /* The stream has at least one filter */
173
Christopher Fauletd7c91962015-04-30 11:48:27 +0200174/*
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100175 * Structure representing the filter configuration, attached to a proxy and
176 * accessible from a filter when instantiated in a stream
177 */
178struct flt_conf {
179 const char *id; /* The filter id */
180 struct flt_ops *ops; /* The filter callbacks */
181 void *conf; /* The filter configuration */
182 struct list list; /* Next filter for the same proxy */
183};
184
185/*
186 * Structure reprensenting a filter instance attached to a stream
Christopher Fauletd7c91962015-04-30 11:48:27 +0200187 *
188 * 2D-Array fields are used to store info per channel. The first index stands
189 * for the request channel, and the second one for the response channel.
190 * Especially, <next> and <fwd> are offets representing amount of data that the
191 * filter are, respectively, parsed and forwarded on a channel. Filters can
192 * access these values using FLT_NXT and FLT_FWD macros.
193 */
194struct filter {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100195 struct flt_conf *config; /* the filter's configuration */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200196 void *ctx; /* The filter context (opaque) */
Christopher Fauletda02e172015-12-04 09:25:05 +0100197 unsigned short flags; /* FLT_FL_* */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200198 unsigned int next[2]; /* Offset, relative to buf->p, to the next byte to parse for a specific channel
199 * 0: request channel, 1: response channel */
200 unsigned int fwd[2]; /* Offset, relative to buf->p, to the next byte to forward for a specific channel
201 * 0: request channel, 1: response channel */
202 struct list list; /* Next filter for the same proxy/stream */
203};
204
Christopher Fauletda02e172015-12-04 09:25:05 +0100205/*
206 * Structure reprensenting the "global" state of filters attached to a stream.
207 */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100208struct strm_flt {
Christopher Fauletda02e172015-12-04 09:25:05 +0100209 struct list filters; /* List of filters attached to a stream */
210 struct filter *current[2]; /* From which filter resume processing, for a specific channel.
211 * This is used for resumable callbacks only,
212 * If NULL, we start from the first filter.
213 * 0: request channel, 1: response channel */
214 unsigned short flags; /* STRM_FL_* */
215 unsigned char nb_req_data_filters; /* Number of data filters registerd on the request channel */
216 unsigned char nb_rsp_data_filters; /* Number of data filters registerd on the response channel */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100217};
218
Christopher Fauletd7c91962015-04-30 11:48:27 +0200219#endif /* _TYPES_FILTERS_H */
220
221/*
222 * Local variables:
223 * c-indent-level: 8
224 * c-basic-offset: 8
225 * End:
226 */