Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream.c @ 6632:787dcc15b802
Stream: split_clients module.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Tue, 12 Jul 2016 17:34:52 +0300 |
parents | c70b7f4537e1 |
children | 7357abd1fa8c |
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_event.h> | |
11 #include <ngx_stream.h> | |
12 | |
13 | |
14 static char *ngx_stream_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
15 static ngx_int_t ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports, | |
16 ngx_stream_listen_t *listen); | |
17 static char *ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports); | |
18 static ngx_int_t ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport, | |
19 ngx_stream_conf_addr_t *addr); | |
20 #if (NGX_HAVE_INET6) | |
21 static ngx_int_t ngx_stream_add_addrs6(ngx_conf_t *cf, | |
22 ngx_stream_port_t *stport, ngx_stream_conf_addr_t *addr); | |
23 #endif | |
24 static ngx_int_t ngx_stream_cmp_conf_addrs(const void *one, const void *two); | |
25 | |
26 | |
27 ngx_uint_t ngx_stream_max_module; | |
28 | |
29 | |
30 static ngx_command_t ngx_stream_commands[] = { | |
31 | |
32 { ngx_string("stream"), | |
33 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | |
34 ngx_stream_block, | |
35 0, | |
36 0, | |
37 NULL }, | |
38 | |
39 ngx_null_command | |
40 }; | |
41 | |
42 | |
43 static ngx_core_module_t ngx_stream_module_ctx = { | |
44 ngx_string("stream"), | |
45 NULL, | |
46 NULL | |
47 }; | |
48 | |
49 | |
50 ngx_module_t ngx_stream_module = { | |
51 NGX_MODULE_V1, | |
52 &ngx_stream_module_ctx, /* module context */ | |
53 ngx_stream_commands, /* module directives */ | |
54 NGX_CORE_MODULE, /* module type */ | |
55 NULL, /* init master */ | |
56 NULL, /* init module */ | |
57 NULL, /* init process */ | |
58 NULL, /* init thread */ | |
59 NULL, /* exit thread */ | |
60 NULL, /* exit process */ | |
61 NULL, /* exit master */ | |
62 NGX_MODULE_V1_PADDING | |
63 }; | |
64 | |
65 | |
66 static char * | |
67 ngx_stream_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
68 { | |
69 char *rv; | |
70 ngx_uint_t i, m, mi, s; | |
71 ngx_conf_t pcf; | |
72 ngx_array_t ports; | |
73 ngx_stream_listen_t *listen; | |
74 ngx_stream_module_t *module; | |
75 ngx_stream_conf_ctx_t *ctx; | |
76 ngx_stream_core_srv_conf_t **cscfp; | |
77 ngx_stream_core_main_conf_t *cmcf; | |
78 | |
6193
78c06e5e1d76
Disabled duplicate http, mail, and stream blocks.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
79 if (*(ngx_stream_conf_ctx_t **) conf) { |
78c06e5e1d76
Disabled duplicate http, mail, and stream blocks.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
80 return "is duplicate"; |
78c06e5e1d76
Disabled duplicate http, mail, and stream blocks.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
81 } |
78c06e5e1d76
Disabled duplicate http, mail, and stream blocks.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
82 |
6115 | 83 /* the main stream context */ |
84 | |
85 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t)); | |
86 if (ctx == NULL) { | |
87 return NGX_CONF_ERROR; | |
88 } | |
89 | |
90 *(ngx_stream_conf_ctx_t **) conf = ctx; | |
91 | |
92 /* count the number of the stream modules and set up their indices */ | |
93 | |
6378
0f203a2af17c
Dynamic modules: moved module-related stuff to separate files.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6193
diff
changeset
|
94 ngx_stream_max_module = ngx_count_modules(cf->cycle, NGX_STREAM_MODULE); |
6115 | 95 |
96 | |
97 /* the stream main_conf context, it's the same in the all stream contexts */ | |
98 | |
99 ctx->main_conf = ngx_pcalloc(cf->pool, | |
100 sizeof(void *) * ngx_stream_max_module); | |
101 if (ctx->main_conf == NULL) { | |
102 return NGX_CONF_ERROR; | |
103 } | |
104 | |
105 | |
106 /* | |
107 * the stream null srv_conf context, it is used to merge | |
108 * the server{}s' srv_conf's | |
109 */ | |
110 | |
111 ctx->srv_conf = ngx_pcalloc(cf->pool, | |
112 sizeof(void *) * ngx_stream_max_module); | |
113 if (ctx->srv_conf == NULL) { | |
114 return NGX_CONF_ERROR; | |
115 } | |
116 | |
117 | |
118 /* | |
119 * create the main_conf's and the null srv_conf's of the all stream modules | |
120 */ | |
121 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
122 for (m = 0; cf->cycle->modules[m]; m++) { |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
123 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
6115 | 124 continue; |
125 } | |
126 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
127 module = cf->cycle->modules[m]->ctx; |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
128 mi = cf->cycle->modules[m]->ctx_index; |
6115 | 129 |
130 if (module->create_main_conf) { | |
131 ctx->main_conf[mi] = module->create_main_conf(cf); | |
132 if (ctx->main_conf[mi] == NULL) { | |
133 return NGX_CONF_ERROR; | |
134 } | |
135 } | |
136 | |
137 if (module->create_srv_conf) { | |
138 ctx->srv_conf[mi] = module->create_srv_conf(cf); | |
139 if (ctx->srv_conf[mi] == NULL) { | |
140 return NGX_CONF_ERROR; | |
141 } | |
142 } | |
143 } | |
144 | |
145 | |
146 pcf = *cf; | |
147 cf->ctx = ctx; | |
148 | |
6606
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
149 for (m = 0; cf->cycle->modules[m]; m++) { |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
150 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
151 continue; |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
152 } |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
153 |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
154 module = cf->cycle->modules[m]->ctx; |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
155 |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
156 if (module->preconfiguration) { |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
157 if (module->preconfiguration(cf) != NGX_OK) { |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
158 return NGX_CONF_ERROR; |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
159 } |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
160 } |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
161 } |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
162 |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
163 |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
164 /* parse inside the stream{} block */ |
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6593
diff
changeset
|
165 |
6115 | 166 cf->module_type = NGX_STREAM_MODULE; |
167 cf->cmd_type = NGX_STREAM_MAIN_CONF; | |
168 rv = ngx_conf_parse(cf, NULL); | |
169 | |
170 if (rv != NGX_CONF_OK) { | |
171 *cf = pcf; | |
172 return rv; | |
173 } | |
174 | |
175 | |
176 /* init stream{} main_conf's, merge the server{}s' srv_conf's */ | |
177 | |
178 cmcf = ctx->main_conf[ngx_stream_core_module.ctx_index]; | |
179 cscfp = cmcf->servers.elts; | |
180 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
181 for (m = 0; cf->cycle->modules[m]; m++) { |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
182 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
6115 | 183 continue; |
184 } | |
185 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
186 module = cf->cycle->modules[m]->ctx; |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
187 mi = cf->cycle->modules[m]->ctx_index; |
6115 | 188 |
189 /* init stream{} main_conf's */ | |
190 | |
191 cf->ctx = ctx; | |
192 | |
193 if (module->init_main_conf) { | |
194 rv = module->init_main_conf(cf, ctx->main_conf[mi]); | |
195 if (rv != NGX_CONF_OK) { | |
196 *cf = pcf; | |
197 return rv; | |
198 } | |
199 } | |
200 | |
201 for (s = 0; s < cmcf->servers.nelts; s++) { | |
202 | |
203 /* merge the server{}s' srv_conf's */ | |
204 | |
205 cf->ctx = cscfp[s]->ctx; | |
206 | |
207 if (module->merge_srv_conf) { | |
208 rv = module->merge_srv_conf(cf, | |
209 ctx->srv_conf[mi], | |
210 cscfp[s]->ctx->srv_conf[mi]); | |
211 if (rv != NGX_CONF_OK) { | |
212 *cf = pcf; | |
213 return rv; | |
214 } | |
215 } | |
216 } | |
217 } | |
218 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
219 for (m = 0; cf->cycle->modules[m]; m++) { |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
220 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
221 continue; |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
222 } |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
223 |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6378
diff
changeset
|
224 module = cf->cycle->modules[m]->ctx; |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
225 |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
226 if (module->postconfiguration) { |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
227 if (module->postconfiguration(cf) != NGX_OK) { |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
228 return NGX_CONF_ERROR; |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
229 } |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
230 } |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
231 } |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6172
diff
changeset
|
232 |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
233 if (ngx_stream_variables_init_vars(cf) != NGX_OK) { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
234 return NGX_CONF_ERROR; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
235 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6606
diff
changeset
|
236 |
6115 | 237 *cf = pcf; |
238 | |
239 | |
240 if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_stream_conf_port_t)) | |
241 != NGX_OK) | |
242 { | |
243 return NGX_CONF_ERROR; | |
244 } | |
245 | |
246 listen = cmcf->listen.elts; | |
247 | |
248 for (i = 0; i < cmcf->listen.nelts; i++) { | |
249 if (ngx_stream_add_ports(cf, &ports, &listen[i]) != NGX_OK) { | |
250 return NGX_CONF_ERROR; | |
251 } | |
252 } | |
253 | |
254 return ngx_stream_optimize_servers(cf, &ports); | |
255 } | |
256 | |
257 | |
258 static ngx_int_t | |
259 ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports, | |
260 ngx_stream_listen_t *listen) | |
261 { | |
262 in_port_t p; | |
263 ngx_uint_t i; | |
264 struct sockaddr *sa; | |
265 ngx_stream_conf_port_t *port; | |
266 ngx_stream_conf_addr_t *addr; | |
267 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
268 sa = &listen->sockaddr.sockaddr; |
6593
b3b7e33083ac
Introduced ngx_inet_get_port() and ngx_inet_set_port() functions.
Roman Arutyunyan <arut@nginx.com>
parents:
6560
diff
changeset
|
269 p = ngx_inet_get_port(sa); |
6115 | 270 |
271 port = ports->elts; | |
272 for (i = 0; i < ports->nelts; i++) { | |
273 | |
6436 | 274 if (p == port[i].port |
275 && listen->type == port[i].type | |
276 && sa->sa_family == port[i].family) | |
277 { | |
6115 | 278 /* a port is already in the port list */ |
279 | |
280 port = &port[i]; | |
281 goto found; | |
282 } | |
283 } | |
284 | |
285 /* add a port to the port list */ | |
286 | |
287 port = ngx_array_push(ports); | |
288 if (port == NULL) { | |
289 return NGX_ERROR; | |
290 } | |
291 | |
292 port->family = sa->sa_family; | |
6436 | 293 port->type = listen->type; |
6115 | 294 port->port = p; |
295 | |
296 if (ngx_array_init(&port->addrs, cf->temp_pool, 2, | |
297 sizeof(ngx_stream_conf_addr_t)) | |
298 != NGX_OK) | |
299 { | |
300 return NGX_ERROR; | |
301 } | |
302 | |
303 found: | |
304 | |
305 addr = ngx_array_push(&port->addrs); | |
306 if (addr == NULL) { | |
307 return NGX_ERROR; | |
308 } | |
309 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
310 addr->opt = *listen; |
6115 | 311 |
312 return NGX_OK; | |
313 } | |
314 | |
315 | |
316 static char * | |
317 ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports) | |
318 { | |
319 ngx_uint_t i, p, last, bind_wildcard; | |
320 ngx_listening_t *ls; | |
321 ngx_stream_port_t *stport; | |
322 ngx_stream_conf_port_t *port; | |
323 ngx_stream_conf_addr_t *addr; | |
324 ngx_stream_core_srv_conf_t *cscf; | |
325 | |
326 port = ports->elts; | |
327 for (p = 0; p < ports->nelts; p++) { | |
328 | |
329 ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts, | |
330 sizeof(ngx_stream_conf_addr_t), ngx_stream_cmp_conf_addrs); | |
331 | |
332 addr = port[p].addrs.elts; | |
333 last = port[p].addrs.nelts; | |
334 | |
335 /* | |
336 * if there is the binding to the "*:port" then we need to bind() | |
337 * to the "*:port" only and ignore the other bindings | |
338 */ | |
339 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
340 if (addr[last - 1].opt.wildcard) { |
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
341 addr[last - 1].opt.bind = 1; |
6115 | 342 bind_wildcard = 1; |
343 | |
344 } else { | |
345 bind_wildcard = 0; | |
346 } | |
347 | |
348 i = 0; | |
349 | |
350 while (i < last) { | |
351 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
352 if (bind_wildcard && !addr[i].opt.bind) { |
6115 | 353 i++; |
354 continue; | |
355 } | |
356 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
357 ls = ngx_create_listening(cf, &addr[i].opt.sockaddr.sockaddr, |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
358 addr[i].opt.socklen); |
6115 | 359 if (ls == NULL) { |
360 return NGX_CONF_ERROR; | |
361 } | |
362 | |
363 ls->addr_ntop = 1; | |
364 ls->handler = ngx_stream_init_connection; | |
365 ls->pool_size = 256; | |
6436 | 366 ls->type = addr[i].opt.type; |
6115 | 367 |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
368 cscf = addr->opt.ctx->srv_conf[ngx_stream_core_module.ctx_index]; |
6129
187aa751ad62
Core: the ngx_set_connection_log() macro.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
369 |
6115 | 370 ls->logp = cscf->error_log; |
371 ls->log.data = &ls->addr_text; | |
372 ls->log.handler = ngx_accept_log_error; | |
373 | |
6172 | 374 ls->backlog = addr[i].opt.backlog; |
375 | |
6436 | 376 ls->wildcard = addr[i].opt.wildcard; |
377 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
378 ls->keepalive = addr[i].opt.so_keepalive; |
6115 | 379 #if (NGX_HAVE_KEEPALIVE_TUNABLE) |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
380 ls->keepidle = addr[i].opt.tcp_keepidle; |
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
381 ls->keepintvl = addr[i].opt.tcp_keepintvl; |
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
382 ls->keepcnt = addr[i].opt.tcp_keepcnt; |
6115 | 383 #endif |
384 | |
385 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
386 ls->ipv6only = addr[i].opt.ipv6only; |
6115 | 387 #endif |
388 | |
6169
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
389 #if (NGX_HAVE_REUSEPORT) |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
390 ls->reuseport = addr[i].opt.reuseport; |
6169
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
391 #endif |
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
392 |
6115 | 393 stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t)); |
394 if (stport == NULL) { | |
395 return NGX_CONF_ERROR; | |
396 } | |
397 | |
398 ls->servers = stport; | |
399 | |
6152
3c344ea7d88b
Simplified ngx_http_init_listening().
Maxim Dounin <mdounin@mdounin.ru>
parents:
6129
diff
changeset
|
400 stport->naddrs = i + 1; |
6115 | 401 |
402 switch (ls->sockaddr->sa_family) { | |
403 #if (NGX_HAVE_INET6) | |
404 case AF_INET6: | |
405 if (ngx_stream_add_addrs6(cf, stport, addr) != NGX_OK) { | |
406 return NGX_CONF_ERROR; | |
407 } | |
408 break; | |
409 #endif | |
410 default: /* AF_INET */ | |
411 if (ngx_stream_add_addrs(cf, stport, addr) != NGX_OK) { | |
412 return NGX_CONF_ERROR; | |
413 } | |
414 break; | |
415 } | |
416 | |
6153
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
417 if (ngx_clone_listening(cf, ls) != NGX_OK) { |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
418 return NGX_CONF_ERROR; |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
419 } |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
420 |
6115 | 421 addr++; |
422 last--; | |
423 } | |
424 } | |
425 | |
426 return NGX_CONF_OK; | |
427 } | |
428 | |
429 | |
430 static ngx_int_t | |
431 ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport, | |
432 ngx_stream_conf_addr_t *addr) | |
433 { | |
434 u_char *p; | |
435 size_t len; | |
436 ngx_uint_t i; | |
437 struct sockaddr_in *sin; | |
438 ngx_stream_in_addr_t *addrs; | |
439 u_char buf[NGX_SOCKADDR_STRLEN]; | |
440 | |
441 stport->addrs = ngx_pcalloc(cf->pool, | |
442 stport->naddrs * sizeof(ngx_stream_in_addr_t)); | |
443 if (stport->addrs == NULL) { | |
444 return NGX_ERROR; | |
445 } | |
446 | |
447 addrs = stport->addrs; | |
448 | |
449 for (i = 0; i < stport->naddrs; i++) { | |
450 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
451 sin = &addr[i].opt.sockaddr.sockaddr_in; |
6115 | 452 addrs[i].addr = sin->sin_addr.s_addr; |
453 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
454 addrs[i].conf.ctx = addr[i].opt.ctx; |
6115 | 455 #if (NGX_STREAM_SSL) |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
456 addrs[i].conf.ssl = addr[i].opt.ssl; |
6115 | 457 #endif |
458 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
459 len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen, |
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
460 buf, NGX_SOCKADDR_STRLEN, 1); |
6115 | 461 |
462 p = ngx_pnalloc(cf->pool, len); | |
463 if (p == NULL) { | |
464 return NGX_ERROR; | |
465 } | |
466 | |
467 ngx_memcpy(p, buf, len); | |
468 | |
469 addrs[i].conf.addr_text.len = len; | |
470 addrs[i].conf.addr_text.data = p; | |
471 } | |
472 | |
473 return NGX_OK; | |
474 } | |
475 | |
476 | |
477 #if (NGX_HAVE_INET6) | |
478 | |
479 static ngx_int_t | |
480 ngx_stream_add_addrs6(ngx_conf_t *cf, ngx_stream_port_t *stport, | |
481 ngx_stream_conf_addr_t *addr) | |
482 { | |
483 u_char *p; | |
484 size_t len; | |
485 ngx_uint_t i; | |
486 struct sockaddr_in6 *sin6; | |
487 ngx_stream_in6_addr_t *addrs6; | |
488 u_char buf[NGX_SOCKADDR_STRLEN]; | |
489 | |
490 stport->addrs = ngx_pcalloc(cf->pool, | |
491 stport->naddrs * sizeof(ngx_stream_in6_addr_t)); | |
492 if (stport->addrs == NULL) { | |
493 return NGX_ERROR; | |
494 } | |
495 | |
496 addrs6 = stport->addrs; | |
497 | |
498 for (i = 0; i < stport->naddrs; i++) { | |
499 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
500 sin6 = &addr[i].opt.sockaddr.sockaddr_in6; |
6115 | 501 addrs6[i].addr6 = sin6->sin6_addr; |
502 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
503 addrs6[i].conf.ctx = addr[i].opt.ctx; |
6115 | 504 #if (NGX_STREAM_SSL) |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
505 addrs6[i].conf.ssl = addr[i].opt.ssl; |
6115 | 506 #endif |
507 | |
6560
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
508 len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen, |
c90cf79d0e1d
Renamed "u" to "sockaddr" in listen options types.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6436
diff
changeset
|
509 buf, NGX_SOCKADDR_STRLEN, 1); |
6115 | 510 |
511 p = ngx_pnalloc(cf->pool, len); | |
512 if (p == NULL) { | |
513 return NGX_ERROR; | |
514 } | |
515 | |
516 ngx_memcpy(p, buf, len); | |
517 | |
518 addrs6[i].conf.addr_text.len = len; | |
519 addrs6[i].conf.addr_text.data = p; | |
520 } | |
521 | |
522 return NGX_OK; | |
523 } | |
524 | |
525 #endif | |
526 | |
527 | |
528 static ngx_int_t | |
529 ngx_stream_cmp_conf_addrs(const void *one, const void *two) | |
530 { | |
531 ngx_stream_conf_addr_t *first, *second; | |
532 | |
533 first = (ngx_stream_conf_addr_t *) one; | |
534 second = (ngx_stream_conf_addr_t *) two; | |
535 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
536 if (first->opt.wildcard) { |
6115 | 537 /* a wildcard must be the last resort, shift it to the end */ |
538 return 1; | |
539 } | |
540 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
541 if (second->opt.wildcard) { |
6115 | 542 /* a wildcard must be the last resort, shift it to the end */ |
543 return -1; | |
544 } | |
545 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
546 if (first->opt.bind && !second->opt.bind) { |
6115 | 547 /* shift explicit bind()ed addresses to the start */ |
548 return -1; | |
549 } | |
550 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
551 if (!first->opt.bind && second->opt.bind) { |
6115 | 552 /* shift explicit bind()ed addresses to the start */ |
553 return 1; | |
554 } | |
555 | |
556 /* do not sort by default */ | |
557 | |
558 return 0; | |
559 } |