Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_core_module.c @ 6682:db422604ceb0
Stream: allow using the session context inside handlers.
Previously, it was not possible to use the stream context
inside ngx_stream_init_connection() handlers. Now, limit_conn,
access handlers, as well as those added later, can create
their own contexts.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 06 Sep 2016 21:28:17 +0300 |
parents | 7357abd1fa8c |
children | 4a16fceea03b |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Roman Arutyunyan | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
13 static ngx_int_t ngx_stream_core_preconfiguration(ngx_conf_t *cf); |
6115 | 14 static void *ngx_stream_core_create_main_conf(ngx_conf_t *cf); |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
15 static char *ngx_stream_core_init_main_conf(ngx_conf_t *cf, void *conf); |
6115 | 16 static void *ngx_stream_core_create_srv_conf(ngx_conf_t *cf); |
17 static char *ngx_stream_core_merge_srv_conf(ngx_conf_t *cf, void *parent, | |
18 void *child); | |
19 static char *ngx_stream_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, | |
20 void *conf); | |
21 static char *ngx_stream_core_server(ngx_conf_t *cf, ngx_command_t *cmd, | |
22 void *conf); | |
23 static char *ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, | |
24 void *conf); | |
6618 | 25 static char *ngx_stream_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, |
26 void *conf); | |
6115 | 27 |
28 | |
29 static ngx_command_t ngx_stream_core_commands[] = { | |
30 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
31 { ngx_string("variables_hash_max_size"), |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
32 NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
33 ngx_conf_set_num_slot, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
34 NGX_STREAM_MAIN_CONF_OFFSET, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
35 offsetof(ngx_stream_core_main_conf_t, variables_hash_max_size), |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
36 NULL }, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
37 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
38 { ngx_string("variables_hash_bucket_size"), |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
39 NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
40 ngx_conf_set_num_slot, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
41 NGX_STREAM_MAIN_CONF_OFFSET, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
42 offsetof(ngx_stream_core_main_conf_t, variables_hash_bucket_size), |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
43 NULL }, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
44 |
6115 | 45 { ngx_string("server"), |
46 NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | |
47 ngx_stream_core_server, | |
48 0, | |
49 0, | |
50 NULL }, | |
51 | |
52 { ngx_string("listen"), | |
53 NGX_STREAM_SRV_CONF|NGX_CONF_1MORE, | |
54 ngx_stream_core_listen, | |
55 NGX_STREAM_SRV_CONF_OFFSET, | |
56 0, | |
57 NULL }, | |
58 | |
59 { ngx_string("error_log"), | |
60 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE, | |
61 ngx_stream_core_error_log, | |
62 NGX_STREAM_SRV_CONF_OFFSET, | |
63 0, | |
64 NULL }, | |
65 | |
6618 | 66 { ngx_string("resolver"), |
67 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE, | |
68 ngx_stream_core_resolver, | |
69 NGX_STREAM_SRV_CONF_OFFSET, | |
70 0, | |
71 NULL }, | |
72 | |
73 { ngx_string("resolver_timeout"), | |
74 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
75 ngx_conf_set_msec_slot, | |
76 NGX_STREAM_SRV_CONF_OFFSET, | |
77 offsetof(ngx_stream_core_srv_conf_t, resolver_timeout), | |
78 NULL }, | |
79 | |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
80 { ngx_string("proxy_protocol_timeout"), |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
81 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
82 ngx_conf_set_msec_slot, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
83 NGX_STREAM_SRV_CONF_OFFSET, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
84 offsetof(ngx_stream_core_srv_conf_t, proxy_protocol_timeout), |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
85 NULL }, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
86 |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
87 { ngx_string("tcp_nodelay"), |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
88 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
89 ngx_conf_set_flag_slot, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
90 NGX_STREAM_SRV_CONF_OFFSET, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
91 offsetof(ngx_stream_core_srv_conf_t, tcp_nodelay), |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
92 NULL }, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
93 |
6115 | 94 ngx_null_command |
95 }; | |
96 | |
97 | |
98 static ngx_stream_module_t ngx_stream_core_module_ctx = { | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
99 ngx_stream_core_preconfiguration, /* preconfiguration */ |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
100 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
101 |
6115 | 102 ngx_stream_core_create_main_conf, /* create main configuration */ |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
103 ngx_stream_core_init_main_conf, /* init main configuration */ |
6115 | 104 |
105 ngx_stream_core_create_srv_conf, /* create server configuration */ | |
106 ngx_stream_core_merge_srv_conf /* merge server configuration */ | |
107 }; | |
108 | |
109 | |
110 ngx_module_t ngx_stream_core_module = { | |
111 NGX_MODULE_V1, | |
112 &ngx_stream_core_module_ctx, /* module context */ | |
113 ngx_stream_core_commands, /* module directives */ | |
114 NGX_STREAM_MODULE, /* module type */ | |
115 NULL, /* init master */ | |
116 NULL, /* init module */ | |
117 NULL, /* init process */ | |
118 NULL, /* init thread */ | |
119 NULL, /* exit thread */ | |
120 NULL, /* exit process */ | |
121 NULL, /* exit master */ | |
122 NGX_MODULE_V1_PADDING | |
123 }; | |
124 | |
125 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
126 static ngx_int_t |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
127 ngx_stream_core_preconfiguration(ngx_conf_t *cf) |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
128 { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
129 return ngx_stream_variables_add_core_vars(cf); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
130 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
131 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
132 |
6115 | 133 static void * |
134 ngx_stream_core_create_main_conf(ngx_conf_t *cf) | |
135 { | |
136 ngx_stream_core_main_conf_t *cmcf; | |
137 | |
138 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_core_main_conf_t)); | |
139 if (cmcf == NULL) { | |
140 return NULL; | |
141 } | |
142 | |
143 if (ngx_array_init(&cmcf->servers, cf->pool, 4, | |
144 sizeof(ngx_stream_core_srv_conf_t *)) | |
145 != NGX_OK) | |
146 { | |
147 return NULL; | |
148 } | |
149 | |
150 if (ngx_array_init(&cmcf->listen, cf->pool, 4, sizeof(ngx_stream_listen_t)) | |
151 != NGX_OK) | |
152 { | |
153 return NULL; | |
154 } | |
155 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
156 cmcf->variables_hash_max_size = NGX_CONF_UNSET_UINT; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
157 cmcf->variables_hash_bucket_size = NGX_CONF_UNSET_UINT; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
158 |
6115 | 159 return cmcf; |
160 } | |
161 | |
162 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
163 static char * |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
164 ngx_stream_core_init_main_conf(ngx_conf_t *cf, void *conf) |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
165 { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
166 ngx_stream_core_main_conf_t *cmcf = conf; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
167 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
168 ngx_conf_init_uint_value(cmcf->variables_hash_max_size, 1024); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
169 ngx_conf_init_uint_value(cmcf->variables_hash_bucket_size, 64); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
170 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
171 cmcf->variables_hash_bucket_size = |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
172 ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
173 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
174 if (cmcf->ncaptures) { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
175 cmcf->ncaptures = (cmcf->ncaptures + 1) * 3; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
176 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
177 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
178 return NGX_CONF_OK; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
179 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
180 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
181 |
6115 | 182 static void * |
183 ngx_stream_core_create_srv_conf(ngx_conf_t *cf) | |
184 { | |
185 ngx_stream_core_srv_conf_t *cscf; | |
186 | |
187 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_core_srv_conf_t)); | |
188 if (cscf == NULL) { | |
189 return NULL; | |
190 } | |
191 | |
192 /* | |
193 * set by ngx_pcalloc(): | |
194 * | |
195 * cscf->handler = NULL; | |
196 * cscf->error_log = NULL; | |
197 */ | |
198 | |
199 cscf->file_name = cf->conf_file->file.name.data; | |
200 cscf->line = cf->conf_file->line; | |
6618 | 201 cscf->resolver_timeout = NGX_CONF_UNSET_MSEC; |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
202 cscf->proxy_protocol_timeout = NGX_CONF_UNSET_MSEC; |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
203 cscf->tcp_nodelay = NGX_CONF_UNSET; |
6115 | 204 |
205 return cscf; | |
206 } | |
207 | |
208 | |
209 static char * | |
210 ngx_stream_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) | |
211 { | |
212 ngx_stream_core_srv_conf_t *prev = parent; | |
213 ngx_stream_core_srv_conf_t *conf = child; | |
214 | |
6618 | 215 ngx_conf_merge_msec_value(conf->resolver_timeout, |
216 prev->resolver_timeout, 30000); | |
217 | |
218 if (conf->resolver == NULL) { | |
219 | |
220 if (prev->resolver == NULL) { | |
221 | |
222 /* | |
223 * create dummy resolver in stream {} context | |
224 * to inherit it in all servers | |
225 */ | |
226 | |
227 prev->resolver = ngx_resolver_create(cf, NULL, 0); | |
228 if (prev->resolver == NULL) { | |
229 return NGX_CONF_ERROR; | |
230 } | |
231 } | |
232 | |
233 conf->resolver = prev->resolver; | |
234 } | |
235 | |
6115 | 236 if (conf->handler == NULL) { |
237 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
238 "no handler for server in %s:%ui", | |
239 conf->file_name, conf->line); | |
240 return NGX_CONF_ERROR; | |
241 } | |
242 | |
243 if (conf->error_log == NULL) { | |
244 if (prev->error_log) { | |
245 conf->error_log = prev->error_log; | |
246 } else { | |
247 conf->error_log = &cf->cycle->new_log; | |
248 } | |
249 } | |
250 | |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
251 ngx_conf_merge_msec_value(conf->proxy_protocol_timeout, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
252 prev->proxy_protocol_timeout, 5000); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
253 |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
254 ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1); |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
255 |
6115 | 256 return NGX_CONF_OK; |
257 } | |
258 | |
259 | |
260 static char * | |
261 ngx_stream_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
262 { | |
263 ngx_stream_core_srv_conf_t *cscf = conf; | |
264 | |
265 return ngx_log_set_log(cf, &cscf->error_log); | |
266 } | |
267 | |
268 | |
269 static char * | |
270 ngx_stream_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
271 { | |
272 char *rv; | |
273 void *mconf; | |
274 ngx_uint_t m; | |
275 ngx_conf_t pcf; | |
276 ngx_stream_module_t *module; | |
277 ngx_stream_conf_ctx_t *ctx, *stream_ctx; | |
278 ngx_stream_core_srv_conf_t *cscf, **cscfp; | |
279 ngx_stream_core_main_conf_t *cmcf; | |
280 | |
281 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t)); | |
282 if (ctx == NULL) { | |
283 return NGX_CONF_ERROR; | |
284 } | |
285 | |
286 stream_ctx = cf->ctx; | |
287 ctx->main_conf = stream_ctx->main_conf; | |
288 | |
289 /* the server{}'s srv_conf */ | |
290 | |
291 ctx->srv_conf = ngx_pcalloc(cf->pool, | |
292 sizeof(void *) * ngx_stream_max_module); | |
293 if (ctx->srv_conf == NULL) { | |
294 return NGX_CONF_ERROR; | |
295 } | |
296 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6230
diff
changeset
|
297 for (m = 0; cf->cycle->modules[m]; m++) { |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6230
diff
changeset
|
298 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
6115 | 299 continue; |
300 } | |
301 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6230
diff
changeset
|
302 module = cf->cycle->modules[m]->ctx; |
6115 | 303 |
304 if (module->create_srv_conf) { | |
305 mconf = module->create_srv_conf(cf); | |
306 if (mconf == NULL) { | |
307 return NGX_CONF_ERROR; | |
308 } | |
309 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6230
diff
changeset
|
310 ctx->srv_conf[cf->cycle->modules[m]->ctx_index] = mconf; |
6115 | 311 } |
312 } | |
313 | |
314 /* the server configuration context */ | |
315 | |
316 cscf = ctx->srv_conf[ngx_stream_core_module.ctx_index]; | |
317 cscf->ctx = ctx; | |
318 | |
319 cmcf = ctx->main_conf[ngx_stream_core_module.ctx_index]; | |
320 | |
321 cscfp = ngx_array_push(&cmcf->servers); | |
322 if (cscfp == NULL) { | |
323 return NGX_CONF_ERROR; | |
324 } | |
325 | |
326 *cscfp = cscf; | |
327 | |
328 | |
329 /* parse inside server{} */ | |
330 | |
331 pcf = *cf; | |
332 cf->ctx = ctx; | |
333 cf->cmd_type = NGX_STREAM_SRV_CONF; | |
334 | |
335 rv = ngx_conf_parse(cf, NULL); | |
336 | |
337 *cf = pcf; | |
338 | |
6657
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
339 if (rv == NGX_CONF_OK && !cscf->listen) { |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
340 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
341 "no \"listen\" is defined for server in %s:%ui", |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
342 cscf->file_name, cscf->line); |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
343 return NGX_CONF_ERROR; |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
344 } |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
345 |
6115 | 346 return rv; |
347 } | |
348 | |
349 | |
350 static char * | |
351 ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
352 { | |
6657
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
353 ngx_stream_core_srv_conf_t *cscf = conf; |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
354 |
6115 | 355 ngx_str_t *value; |
356 ngx_url_t u; | |
6436 | 357 ngx_uint_t i, backlog; |
6558
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
358 ngx_stream_listen_t *ls, *als; |
6115 | 359 ngx_stream_core_main_conf_t *cmcf; |
360 | |
6657
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
361 cscf->listen = 1; |
3d5202c71f94
Ensure "listen" exists in a mail or stream server (ticket #1049).
Roman Arutyunyan <arut@nginx.com>
parents:
6618
diff
changeset
|
362 |
6115 | 363 value = cf->args->elts; |
364 | |
365 ngx_memzero(&u, sizeof(ngx_url_t)); | |
366 | |
367 u.url = value[1]; | |
368 u.listen = 1; | |
369 | |
370 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { | |
371 if (u.err) { | |
372 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
373 "%s in \"%V\" of the \"listen\" directive", | |
374 u.err, &u.url); | |
375 } | |
376 | |
377 return NGX_CONF_ERROR; | |
378 } | |
379 | |
380 cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); | |
381 | |
382 ls = ngx_array_push(&cmcf->listen); | |
383 if (ls == NULL) { | |
384 return NGX_CONF_ERROR; | |
385 } | |
386 | |
387 ngx_memzero(ls, sizeof(ngx_stream_listen_t)); | |
388 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6559
diff
changeset
|
389 ngx_memcpy(&ls->sockaddr.sockaddr, &u.sockaddr, u.socklen); |
6115 | 390 |
391 ls->socklen = u.socklen; | |
6172 | 392 ls->backlog = NGX_LISTEN_BACKLOG; |
6436 | 393 ls->type = SOCK_STREAM; |
6115 | 394 ls->wildcard = u.wildcard; |
395 ls->ctx = cf->ctx; | |
396 | |
397 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) | |
398 ls->ipv6only = 1; | |
399 #endif | |
400 | |
6436 | 401 backlog = 0; |
402 | |
6115 | 403 for (i = 2; i < cf->args->nelts; i++) { |
404 | |
6436 | 405 #if !(NGX_WIN32) |
406 if (ngx_strcmp(value[i].data, "udp") == 0) { | |
407 ls->type = SOCK_DGRAM; | |
408 continue; | |
409 } | |
410 #endif | |
411 | |
6115 | 412 if (ngx_strcmp(value[i].data, "bind") == 0) { |
413 ls->bind = 1; | |
414 continue; | |
415 } | |
416 | |
6172 | 417 if (ngx_strncmp(value[i].data, "backlog=", 8) == 0) { |
418 ls->backlog = ngx_atoi(value[i].data + 8, value[i].len - 8); | |
419 ls->bind = 1; | |
420 | |
421 if (ls->backlog == NGX_ERROR || ls->backlog == 0) { | |
422 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
423 "invalid backlog \"%V\"", &value[i]); | |
424 return NGX_CONF_ERROR; | |
425 } | |
426 | |
6436 | 427 backlog = 1; |
428 | |
6172 | 429 continue; |
430 } | |
431 | |
6115 | 432 if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) { |
433 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) | |
6557
6f8254ae61b8
Use ngx_cmp_sockaddr() where appropriate.
Ruslan Ermilov <ru@nginx.com>
parents:
6436
diff
changeset
|
434 size_t len; |
6230
2a621245f4cf
Win32: MSVC 2015 compatibility.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6221
diff
changeset
|
435 u_char buf[NGX_SOCKADDR_STRLEN]; |
6115 | 436 |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6559
diff
changeset
|
437 if (ls->sockaddr.sockaddr.sa_family == AF_INET6) { |
6115 | 438 |
439 if (ngx_strcmp(&value[i].data[10], "n") == 0) { | |
440 ls->ipv6only = 1; | |
441 | |
442 } else if (ngx_strcmp(&value[i].data[10], "ff") == 0) { | |
443 ls->ipv6only = 0; | |
444 | |
445 } else { | |
446 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
447 "invalid ipv6only flags \"%s\"", | |
448 &value[i].data[9]); | |
449 return NGX_CONF_ERROR; | |
450 } | |
451 | |
452 ls->bind = 1; | |
453 | |
454 } else { | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6559
diff
changeset
|
455 len = ngx_sock_ntop(&ls->sockaddr.sockaddr, ls->socklen, buf, |
6115 | 456 NGX_SOCKADDR_STRLEN, 1); |
457 | |
458 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
459 "ipv6only is not supported " | |
460 "on addr \"%*s\", ignored", len, buf); | |
461 } | |
462 | |
463 continue; | |
464 #else | |
465 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
466 "bind ipv6only is not supported " | |
467 "on this platform"); | |
468 return NGX_CONF_ERROR; | |
469 #endif | |
470 } | |
471 | |
6153
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
472 if (ngx_strcmp(value[i].data, "reuseport") == 0) { |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
473 #if (NGX_HAVE_REUSEPORT) |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
474 ls->reuseport = 1; |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
475 ls->bind = 1; |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
476 #else |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
477 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
478 "reuseport is not supported " |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
479 "on this platform, ignored"); |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
480 #endif |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
481 continue; |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
482 } |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
483 |
6115 | 484 if (ngx_strcmp(value[i].data, "ssl") == 0) { |
485 #if (NGX_STREAM_SSL) | |
486 ls->ssl = 1; | |
487 continue; | |
488 #else | |
489 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
490 "the \"ssl\" parameter requires " | |
491 "ngx_stream_ssl_module"); | |
492 return NGX_CONF_ERROR; | |
493 #endif | |
494 } | |
495 | |
496 if (ngx_strncmp(value[i].data, "so_keepalive=", 13) == 0) { | |
497 | |
498 if (ngx_strcmp(&value[i].data[13], "on") == 0) { | |
499 ls->so_keepalive = 1; | |
500 | |
501 } else if (ngx_strcmp(&value[i].data[13], "off") == 0) { | |
502 ls->so_keepalive = 2; | |
503 | |
504 } else { | |
505 | |
506 #if (NGX_HAVE_KEEPALIVE_TUNABLE) | |
507 u_char *p, *end; | |
508 ngx_str_t s; | |
509 | |
510 end = value[i].data + value[i].len; | |
511 s.data = value[i].data + 13; | |
512 | |
513 p = ngx_strlchr(s.data, end, ':'); | |
514 if (p == NULL) { | |
515 p = end; | |
516 } | |
517 | |
518 if (p > s.data) { | |
519 s.len = p - s.data; | |
520 | |
521 ls->tcp_keepidle = ngx_parse_time(&s, 1); | |
522 if (ls->tcp_keepidle == (time_t) NGX_ERROR) { | |
523 goto invalid_so_keepalive; | |
524 } | |
525 } | |
526 | |
527 s.data = (p < end) ? (p + 1) : end; | |
528 | |
529 p = ngx_strlchr(s.data, end, ':'); | |
530 if (p == NULL) { | |
531 p = end; | |
532 } | |
533 | |
534 if (p > s.data) { | |
535 s.len = p - s.data; | |
536 | |
537 ls->tcp_keepintvl = ngx_parse_time(&s, 1); | |
538 if (ls->tcp_keepintvl == (time_t) NGX_ERROR) { | |
539 goto invalid_so_keepalive; | |
540 } | |
541 } | |
542 | |
543 s.data = (p < end) ? (p + 1) : end; | |
544 | |
545 if (s.data < end) { | |
546 s.len = end - s.data; | |
547 | |
548 ls->tcp_keepcnt = ngx_atoi(s.data, s.len); | |
549 if (ls->tcp_keepcnt == NGX_ERROR) { | |
550 goto invalid_so_keepalive; | |
551 } | |
552 } | |
553 | |
554 if (ls->tcp_keepidle == 0 && ls->tcp_keepintvl == 0 | |
555 && ls->tcp_keepcnt == 0) | |
556 { | |
557 goto invalid_so_keepalive; | |
558 } | |
559 | |
560 ls->so_keepalive = 1; | |
561 | |
562 #else | |
563 | |
564 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
565 "the \"so_keepalive\" parameter accepts " | |
566 "only \"on\" or \"off\" on this platform"); | |
567 return NGX_CONF_ERROR; | |
568 | |
569 #endif | |
570 } | |
571 | |
572 ls->bind = 1; | |
573 | |
574 continue; | |
575 | |
576 #if (NGX_HAVE_KEEPALIVE_TUNABLE) | |
577 invalid_so_keepalive: | |
578 | |
579 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
580 "invalid so_keepalive value: \"%s\"", | |
581 &value[i].data[13]); | |
582 return NGX_CONF_ERROR; | |
583 #endif | |
584 } | |
585 | |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
586 if (ngx_strcmp(value[i].data, "proxy_protocol") == 0) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
587 ls->proxy_protocol = 1; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
588 continue; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
589 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
590 |
6115 | 591 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
592 "the invalid \"%V\" parameter", &value[i]); | |
593 return NGX_CONF_ERROR; | |
594 } | |
595 | |
6436 | 596 if (ls->type == SOCK_DGRAM) { |
597 if (backlog) { | |
598 return "\"backlog\" parameter is incompatible with \"udp\""; | |
599 } | |
600 | |
601 #if (NGX_STREAM_SSL) | |
602 if (ls->ssl) { | |
603 return "\"ssl\" parameter is incompatible with \"udp\""; | |
604 } | |
605 #endif | |
606 | |
607 if (ls->so_keepalive) { | |
608 return "\"so_keepalive\" parameter is incompatible with \"udp\""; | |
609 } | |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
610 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
611 if (ls->proxy_protocol) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
612 return "\"proxy_protocol\" parameter is incompatible with \"udp\""; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6657
diff
changeset
|
613 } |
6436 | 614 } |
615 | |
6558
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
616 als = cmcf->listen.elts; |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
617 |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
618 for (i = 0; i < cmcf->listen.nelts - 1; i++) { |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
619 if (ls->type != als[i].type) { |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
620 continue; |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
621 } |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
622 |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6559
diff
changeset
|
623 if (ngx_cmp_sockaddr(&als[i].sockaddr.sockaddr, als[i].socklen, |
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6559
diff
changeset
|
624 &ls->sockaddr.sockaddr, ls->socklen, 1) |
6558
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
625 != NGX_OK) |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
626 { |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
627 continue; |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
628 } |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
629 |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
630 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
631 "duplicate \"%V\" address and port pair", &u.url); |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
632 return NGX_CONF_ERROR; |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
633 } |
68854ce64ec7
Stream: fixed duplicate listen address detection.
Ruslan Ermilov <ru@nginx.com>
parents:
6557
diff
changeset
|
634 |
6115 | 635 return NGX_CONF_OK; |
636 } | |
6618 | 637 |
638 | |
639 static char * | |
640 ngx_stream_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
641 { | |
642 ngx_stream_core_srv_conf_t *cscf = conf; | |
643 | |
644 ngx_str_t *value; | |
645 | |
646 if (cscf->resolver) { | |
647 return "is duplicate"; | |
648 } | |
649 | |
650 value = cf->args->elts; | |
651 | |
652 cscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1); | |
653 if (cscf->resolver == NULL) { | |
654 return NGX_CONF_ERROR; | |
655 } | |
656 | |
657 return NGX_CONF_OK; | |
658 } |