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