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