ngx_stream.h revision e18a033b
1
2/*
3 * Copyright (C) Roman Arutyunyan
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_STREAM_H_INCLUDED_
9#define _NGX_STREAM_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14
15#if (NGX_STREAM_SSL)
16#include <ngx_stream_ssl_module.h>
17#endif
18
19
20typedef struct ngx_stream_session_s  ngx_stream_session_t;
21
22
23#include <ngx_stream_variables.h>
24#include <ngx_stream_script.h>
25#include <ngx_stream_upstream.h>
26#include <ngx_stream_upstream_round_robin.h>
27
28
29#define NGX_STREAM_OK                        200
30#define NGX_STREAM_BAD_REQUEST               400
31#define NGX_STREAM_FORBIDDEN                 403
32#define NGX_STREAM_INTERNAL_SERVER_ERROR     500
33#define NGX_STREAM_BAD_GATEWAY               502
34#define NGX_STREAM_SERVICE_UNAVAILABLE       503
35
36
37typedef struct {
38    void                         **main_conf;
39    void                         **srv_conf;
40} ngx_stream_conf_ctx_t;
41
42
43typedef struct {
44    ngx_sockaddr_t                 sockaddr;
45    socklen_t                      socklen;
46
47    /* server ctx */
48    ngx_stream_conf_ctx_t         *ctx;
49
50    unsigned                       bind:1;
51    unsigned                       wildcard:1;
52    unsigned                       ssl:1;
53#if (NGX_HAVE_INET6)
54    unsigned                       ipv6only:1;
55#endif
56    unsigned                       reuseport:1;
57    unsigned                       so_keepalive:2;
58    unsigned                       proxy_protocol:1;
59#if (NGX_HAVE_KEEPALIVE_TUNABLE)
60    int                            tcp_keepidle;
61    int                            tcp_keepintvl;
62    int                            tcp_keepcnt;
63#endif
64    int                            backlog;
65    int                            type;
66} ngx_stream_listen_t;
67
68
69typedef struct {
70    ngx_stream_conf_ctx_t         *ctx;
71    ngx_str_t                      addr_text;
72    unsigned                       ssl:1;
73    unsigned                       proxy_protocol:1;
74} ngx_stream_addr_conf_t;
75
76typedef struct {
77    in_addr_t                      addr;
78    ngx_stream_addr_conf_t         conf;
79} ngx_stream_in_addr_t;
80
81
82#if (NGX_HAVE_INET6)
83
84typedef struct {
85    struct in6_addr                addr6;
86    ngx_stream_addr_conf_t         conf;
87} ngx_stream_in6_addr_t;
88
89#endif
90
91
92typedef struct {
93    /* ngx_stream_in_addr_t or ngx_stream_in6_addr_t */
94    void                          *addrs;
95    ngx_uint_t                     naddrs;
96} ngx_stream_port_t;
97
98
99typedef struct {
100    int                            family;
101    int                            type;
102    in_port_t                      port;
103    ngx_array_t                    addrs; /* array of ngx_stream_conf_addr_t */
104} ngx_stream_conf_port_t;
105
106
107typedef struct {
108    ngx_stream_listen_t            opt;
109} ngx_stream_conf_addr_t;
110
111
112typedef enum {
113    NGX_STREAM_POST_ACCEPT_PHASE = 0,
114    NGX_STREAM_PREACCESS_PHASE,
115    NGX_STREAM_ACCESS_PHASE,
116    NGX_STREAM_SSL_PHASE,
117    NGX_STREAM_PREREAD_PHASE,
118    NGX_STREAM_CONTENT_PHASE,
119    NGX_STREAM_LOG_PHASE
120} ngx_stream_phases;
121
122
123typedef struct ngx_stream_phase_handler_s  ngx_stream_phase_handler_t;
124
125typedef ngx_int_t (*ngx_stream_phase_handler_pt)(ngx_stream_session_t *s,
126    ngx_stream_phase_handler_t *ph);
127typedef ngx_int_t (*ngx_stream_handler_pt)(ngx_stream_session_t *s);
128typedef void (*ngx_stream_content_handler_pt)(ngx_stream_session_t *s);
129
130
131struct ngx_stream_phase_handler_s {
132    ngx_stream_phase_handler_pt    checker;
133    ngx_stream_handler_pt          handler;
134    ngx_uint_t                     next;
135};
136
137
138typedef struct {
139    ngx_stream_phase_handler_t    *handlers;
140} ngx_stream_phase_engine_t;
141
142
143typedef struct {
144    ngx_array_t                    handlers;
145} ngx_stream_phase_t;
146
147
148typedef struct {
149    ngx_array_t                    servers;     /* ngx_stream_core_srv_conf_t */
150    ngx_array_t                    listen;      /* ngx_stream_listen_t */
151
152    ngx_stream_phase_engine_t      phase_engine;
153
154    ngx_hash_t                     variables_hash;
155
156    ngx_array_t                    variables;        /* ngx_stream_variable_t */
157    ngx_array_t                    prefix_variables; /* ngx_stream_variable_t */
158    ngx_uint_t                     ncaptures;
159
160    ngx_uint_t                     variables_hash_max_size;
161    ngx_uint_t                     variables_hash_bucket_size;
162
163    ngx_hash_keys_arrays_t        *variables_keys;
164
165    ngx_stream_phase_t             phases[NGX_STREAM_LOG_PHASE + 1];
166} ngx_stream_core_main_conf_t;
167
168
169typedef struct {
170    ngx_stream_content_handler_pt  handler;
171
172    ngx_stream_conf_ctx_t         *ctx;
173
174    u_char                        *file_name;
175    ngx_uint_t                     line;
176
177    ngx_flag_t                     tcp_nodelay;
178    size_t                         preread_buffer_size;
179    ngx_msec_t                     preread_timeout;
180
181    ngx_log_t                     *error_log;
182
183    ngx_msec_t                     resolver_timeout;
184    ngx_resolver_t                *resolver;
185
186    ngx_msec_t                     proxy_protocol_timeout;
187
188    ngx_uint_t                     listen;  /* unsigned  listen:1; */
189} ngx_stream_core_srv_conf_t;
190
191
192struct ngx_stream_session_s {
193    uint32_t                       signature;         /* "STRM" */
194
195    ngx_connection_t              *connection;
196
197    off_t                          received;
198    time_t                         start_sec;
199    ngx_msec_t                     start_msec;
200
201    ngx_log_handler_pt             log_handler;
202
203    void                         **ctx;
204    void                         **main_conf;
205    void                         **srv_conf;
206
207    ngx_stream_upstream_t         *upstream;
208    ngx_array_t                   *upstream_states;
209                                           /* of ngx_stream_upstream_state_t */
210    ngx_stream_variable_value_t   *variables;
211
212#if (NGX_PCRE)
213    ngx_uint_t                     ncaptures;
214    int                           *captures;
215    u_char                        *captures_data;
216#endif
217
218    ngx_int_t                      phase_handler;
219    ngx_uint_t                     status;
220
221    unsigned                       ssl:1;
222
223    unsigned                       stat_processing:1;
224
225    unsigned                       health_check:1;
226};
227
228
229typedef struct {
230    ngx_int_t                    (*preconfiguration)(ngx_conf_t *cf);
231    ngx_int_t                    (*postconfiguration)(ngx_conf_t *cf);
232
233    void                        *(*create_main_conf)(ngx_conf_t *cf);
234    char                        *(*init_main_conf)(ngx_conf_t *cf, void *conf);
235
236    void                        *(*create_srv_conf)(ngx_conf_t *cf);
237    char                        *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
238                                                   void *conf);
239} ngx_stream_module_t;
240
241
242#define NGX_STREAM_MODULE       0x4d525453     /* "STRM" */
243
244#define NGX_STREAM_MAIN_CONF    0x02000000
245#define NGX_STREAM_SRV_CONF     0x04000000
246#define NGX_STREAM_UPS_CONF     0x08000000
247
248
249#define NGX_STREAM_MAIN_CONF_OFFSET  offsetof(ngx_stream_conf_ctx_t, main_conf)
250#define NGX_STREAM_SRV_CONF_OFFSET   offsetof(ngx_stream_conf_ctx_t, srv_conf)
251
252
253#define ngx_stream_get_module_ctx(s, module)   (s)->ctx[module.ctx_index]
254#define ngx_stream_set_ctx(s, c, module)       s->ctx[module.ctx_index] = c;
255#define ngx_stream_delete_ctx(s, module)       s->ctx[module.ctx_index] = NULL;
256
257
258#define ngx_stream_get_module_main_conf(s, module)                             \
259    (s)->main_conf[module.ctx_index]
260#define ngx_stream_get_module_srv_conf(s, module)                              \
261    (s)->srv_conf[module.ctx_index]
262
263#define ngx_stream_conf_get_module_main_conf(cf, module)                       \
264    ((ngx_stream_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
265#define ngx_stream_conf_get_module_srv_conf(cf, module)                        \
266    ((ngx_stream_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
267
268#define ngx_stream_cycle_get_module_main_conf(cycle, module)                   \
269    (cycle->conf_ctx[ngx_stream_module.index] ?                                \
270        ((ngx_stream_conf_ctx_t *) cycle->conf_ctx[ngx_stream_module.index])   \
271            ->main_conf[module.ctx_index]:                                     \
272        NULL)
273
274
275#define NGX_STREAM_WRITE_BUFFERED  0x10
276
277
278void ngx_stream_core_run_phases(ngx_stream_session_t *s);
279ngx_int_t ngx_stream_core_generic_phase(ngx_stream_session_t *s,
280    ngx_stream_phase_handler_t *ph);
281ngx_int_t ngx_stream_core_preread_phase(ngx_stream_session_t *s,
282    ngx_stream_phase_handler_t *ph);
283ngx_int_t ngx_stream_core_content_phase(ngx_stream_session_t *s,
284    ngx_stream_phase_handler_t *ph);
285
286
287void ngx_stream_init_connection(ngx_connection_t *c);
288void ngx_stream_session_handler(ngx_event_t *rev);
289void ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc);
290
291
292extern ngx_module_t  ngx_stream_module;
293extern ngx_uint_t    ngx_stream_max_module;
294extern ngx_module_t  ngx_stream_core_module;
295
296
297typedef ngx_int_t (*ngx_stream_filter_pt)(ngx_stream_session_t *s,
298    ngx_chain_t *chain, ngx_uint_t from_upstream);
299
300
301extern ngx_stream_filter_pt  ngx_stream_top_filter;
302
303
304#endif /* _NGX_STREAM_H_INCLUDED_ */
305