Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream.c @ 6173:1729d8d3eb3a
Mail: listen backlog=.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Mon, 08 Jun 2015 23:13:56 +0300 |
parents | 3e8cddcff381 |
children | 68c106e6fa0a |
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 | |
79 /* the main stream context */ | |
80 | |
81 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t)); | |
82 if (ctx == NULL) { | |
83 return NGX_CONF_ERROR; | |
84 } | |
85 | |
86 *(ngx_stream_conf_ctx_t **) conf = ctx; | |
87 | |
88 /* count the number of the stream modules and set up their indices */ | |
89 | |
90 ngx_stream_max_module = 0; | |
91 for (m = 0; ngx_modules[m]; m++) { | |
92 if (ngx_modules[m]->type != NGX_STREAM_MODULE) { | |
93 continue; | |
94 } | |
95 | |
96 ngx_modules[m]->ctx_index = ngx_stream_max_module++; | |
97 } | |
98 | |
99 | |
100 /* the stream main_conf context, it's the same in the all stream contexts */ | |
101 | |
102 ctx->main_conf = ngx_pcalloc(cf->pool, | |
103 sizeof(void *) * ngx_stream_max_module); | |
104 if (ctx->main_conf == NULL) { | |
105 return NGX_CONF_ERROR; | |
106 } | |
107 | |
108 | |
109 /* | |
110 * the stream null srv_conf context, it is used to merge | |
111 * the server{}s' srv_conf's | |
112 */ | |
113 | |
114 ctx->srv_conf = ngx_pcalloc(cf->pool, | |
115 sizeof(void *) * ngx_stream_max_module); | |
116 if (ctx->srv_conf == NULL) { | |
117 return NGX_CONF_ERROR; | |
118 } | |
119 | |
120 | |
121 /* | |
122 * create the main_conf's and the null srv_conf's of the all stream modules | |
123 */ | |
124 | |
125 for (m = 0; ngx_modules[m]; m++) { | |
126 if (ngx_modules[m]->type != NGX_STREAM_MODULE) { | |
127 continue; | |
128 } | |
129 | |
130 module = ngx_modules[m]->ctx; | |
131 mi = ngx_modules[m]->ctx_index; | |
132 | |
133 if (module->create_main_conf) { | |
134 ctx->main_conf[mi] = module->create_main_conf(cf); | |
135 if (ctx->main_conf[mi] == NULL) { | |
136 return NGX_CONF_ERROR; | |
137 } | |
138 } | |
139 | |
140 if (module->create_srv_conf) { | |
141 ctx->srv_conf[mi] = module->create_srv_conf(cf); | |
142 if (ctx->srv_conf[mi] == NULL) { | |
143 return NGX_CONF_ERROR; | |
144 } | |
145 } | |
146 } | |
147 | |
148 | |
149 /* parse inside the stream{} block */ | |
150 | |
151 pcf = *cf; | |
152 cf->ctx = ctx; | |
153 | |
154 cf->module_type = NGX_STREAM_MODULE; | |
155 cf->cmd_type = NGX_STREAM_MAIN_CONF; | |
156 rv = ngx_conf_parse(cf, NULL); | |
157 | |
158 if (rv != NGX_CONF_OK) { | |
159 *cf = pcf; | |
160 return rv; | |
161 } | |
162 | |
163 | |
164 /* init stream{} main_conf's, merge the server{}s' srv_conf's */ | |
165 | |
166 cmcf = ctx->main_conf[ngx_stream_core_module.ctx_index]; | |
167 cscfp = cmcf->servers.elts; | |
168 | |
169 for (m = 0; ngx_modules[m]; m++) { | |
170 if (ngx_modules[m]->type != NGX_STREAM_MODULE) { | |
171 continue; | |
172 } | |
173 | |
174 module = ngx_modules[m]->ctx; | |
175 mi = ngx_modules[m]->ctx_index; | |
176 | |
177 /* init stream{} main_conf's */ | |
178 | |
179 cf->ctx = ctx; | |
180 | |
181 if (module->init_main_conf) { | |
182 rv = module->init_main_conf(cf, ctx->main_conf[mi]); | |
183 if (rv != NGX_CONF_OK) { | |
184 *cf = pcf; | |
185 return rv; | |
186 } | |
187 } | |
188 | |
189 for (s = 0; s < cmcf->servers.nelts; s++) { | |
190 | |
191 /* merge the server{}s' srv_conf's */ | |
192 | |
193 cf->ctx = cscfp[s]->ctx; | |
194 | |
195 if (module->merge_srv_conf) { | |
196 rv = module->merge_srv_conf(cf, | |
197 ctx->srv_conf[mi], | |
198 cscfp[s]->ctx->srv_conf[mi]); | |
199 if (rv != NGX_CONF_OK) { | |
200 *cf = pcf; | |
201 return rv; | |
202 } | |
203 } | |
204 } | |
205 } | |
206 | |
207 *cf = pcf; | |
208 | |
209 | |
210 if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_stream_conf_port_t)) | |
211 != NGX_OK) | |
212 { | |
213 return NGX_CONF_ERROR; | |
214 } | |
215 | |
216 listen = cmcf->listen.elts; | |
217 | |
218 for (i = 0; i < cmcf->listen.nelts; i++) { | |
219 if (ngx_stream_add_ports(cf, &ports, &listen[i]) != NGX_OK) { | |
220 return NGX_CONF_ERROR; | |
221 } | |
222 } | |
223 | |
224 return ngx_stream_optimize_servers(cf, &ports); | |
225 } | |
226 | |
227 | |
228 static ngx_int_t | |
229 ngx_stream_add_ports(ngx_conf_t *cf, ngx_array_t *ports, | |
230 ngx_stream_listen_t *listen) | |
231 { | |
232 in_port_t p; | |
233 ngx_uint_t i; | |
234 struct sockaddr *sa; | |
235 struct sockaddr_in *sin; | |
236 ngx_stream_conf_port_t *port; | |
237 ngx_stream_conf_addr_t *addr; | |
238 #if (NGX_HAVE_INET6) | |
239 struct sockaddr_in6 *sin6; | |
240 #endif | |
241 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
242 sa = &listen->u.sockaddr; |
6115 | 243 |
244 switch (sa->sa_family) { | |
245 | |
246 #if (NGX_HAVE_INET6) | |
247 case AF_INET6: | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
248 sin6 = &listen->u.sockaddr_in6; |
6115 | 249 p = sin6->sin6_port; |
250 break; | |
251 #endif | |
252 | |
253 #if (NGX_HAVE_UNIX_DOMAIN) | |
254 case AF_UNIX: | |
255 p = 0; | |
256 break; | |
257 #endif | |
258 | |
259 default: /* AF_INET */ | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
260 sin = &listen->u.sockaddr_in; |
6115 | 261 p = sin->sin_port; |
262 break; | |
263 } | |
264 | |
265 port = ports->elts; | |
266 for (i = 0; i < ports->nelts; i++) { | |
267 if (p == port[i].port && sa->sa_family == port[i].family) { | |
268 | |
269 /* a port is already in the port list */ | |
270 | |
271 port = &port[i]; | |
272 goto found; | |
273 } | |
274 } | |
275 | |
276 /* add a port to the port list */ | |
277 | |
278 port = ngx_array_push(ports); | |
279 if (port == NULL) { | |
280 return NGX_ERROR; | |
281 } | |
282 | |
283 port->family = sa->sa_family; | |
284 port->port = p; | |
285 | |
286 if (ngx_array_init(&port->addrs, cf->temp_pool, 2, | |
287 sizeof(ngx_stream_conf_addr_t)) | |
288 != NGX_OK) | |
289 { | |
290 return NGX_ERROR; | |
291 } | |
292 | |
293 found: | |
294 | |
295 addr = ngx_array_push(&port->addrs); | |
296 if (addr == NULL) { | |
297 return NGX_ERROR; | |
298 } | |
299 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
300 addr->opt = *listen; |
6115 | 301 |
302 return NGX_OK; | |
303 } | |
304 | |
305 | |
306 static char * | |
307 ngx_stream_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports) | |
308 { | |
309 ngx_uint_t i, p, last, bind_wildcard; | |
310 ngx_listening_t *ls; | |
311 ngx_stream_port_t *stport; | |
312 ngx_stream_conf_port_t *port; | |
313 ngx_stream_conf_addr_t *addr; | |
314 ngx_stream_core_srv_conf_t *cscf; | |
315 | |
316 port = ports->elts; | |
317 for (p = 0; p < ports->nelts; p++) { | |
318 | |
319 ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts, | |
320 sizeof(ngx_stream_conf_addr_t), ngx_stream_cmp_conf_addrs); | |
321 | |
322 addr = port[p].addrs.elts; | |
323 last = port[p].addrs.nelts; | |
324 | |
325 /* | |
326 * if there is the binding to the "*:port" then we need to bind() | |
327 * to the "*:port" only and ignore the other bindings | |
328 */ | |
329 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
330 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
|
331 addr[last - 1].opt.bind = 1; |
6115 | 332 bind_wildcard = 1; |
333 | |
334 } else { | |
335 bind_wildcard = 0; | |
336 } | |
337 | |
338 i = 0; | |
339 | |
340 while (i < last) { | |
341 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
342 if (bind_wildcard && !addr[i].opt.bind) { |
6115 | 343 i++; |
344 continue; | |
345 } | |
346 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
347 ls = ngx_create_listening(cf, &addr[i].opt.u.sockaddr, |
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
348 addr[i].opt.socklen); |
6115 | 349 if (ls == NULL) { |
350 return NGX_CONF_ERROR; | |
351 } | |
352 | |
353 ls->addr_ntop = 1; | |
354 ls->handler = ngx_stream_init_connection; | |
355 ls->pool_size = 256; | |
356 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
357 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
|
358 |
6115 | 359 ls->logp = cscf->error_log; |
360 ls->log.data = &ls->addr_text; | |
361 ls->log.handler = ngx_accept_log_error; | |
362 | |
6172 | 363 ls->backlog = addr[i].opt.backlog; |
364 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
365 ls->keepalive = addr[i].opt.so_keepalive; |
6115 | 366 #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
|
367 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
|
368 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
|
369 ls->keepcnt = addr[i].opt.tcp_keepcnt; |
6115 | 370 #endif |
371 | |
372 #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
|
373 ls->ipv6only = addr[i].opt.ipv6only; |
6115 | 374 #endif |
375 | |
6169
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
376 #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
|
377 ls->reuseport = addr[i].opt.reuseport; |
6169
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
378 #endif |
f654addf0eea
Stream: fixed "reuseport" to actually work.
Ruslan Ermilov <ru@nginx.com>
parents:
6153
diff
changeset
|
379 |
6115 | 380 stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t)); |
381 if (stport == NULL) { | |
382 return NGX_CONF_ERROR; | |
383 } | |
384 | |
385 ls->servers = stport; | |
386 | |
6152
3c344ea7d88b
Simplified ngx_http_init_listening().
Maxim Dounin <mdounin@mdounin.ru>
parents:
6129
diff
changeset
|
387 stport->naddrs = i + 1; |
6115 | 388 |
389 switch (ls->sockaddr->sa_family) { | |
390 #if (NGX_HAVE_INET6) | |
391 case AF_INET6: | |
392 if (ngx_stream_add_addrs6(cf, stport, addr) != NGX_OK) { | |
393 return NGX_CONF_ERROR; | |
394 } | |
395 break; | |
396 #endif | |
397 default: /* AF_INET */ | |
398 if (ngx_stream_add_addrs(cf, stport, addr) != NGX_OK) { | |
399 return NGX_CONF_ERROR; | |
400 } | |
401 break; | |
402 } | |
403 | |
6153
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
404 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
|
405 return NGX_CONF_ERROR; |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
406 } |
4f6efabcb09b
The "reuseport" option of the "listen" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6152
diff
changeset
|
407 |
6115 | 408 addr++; |
409 last--; | |
410 } | |
411 } | |
412 | |
413 return NGX_CONF_OK; | |
414 } | |
415 | |
416 | |
417 static ngx_int_t | |
418 ngx_stream_add_addrs(ngx_conf_t *cf, ngx_stream_port_t *stport, | |
419 ngx_stream_conf_addr_t *addr) | |
420 { | |
421 u_char *p; | |
422 size_t len; | |
423 ngx_uint_t i; | |
424 struct sockaddr_in *sin; | |
425 ngx_stream_in_addr_t *addrs; | |
426 u_char buf[NGX_SOCKADDR_STRLEN]; | |
427 | |
428 stport->addrs = ngx_pcalloc(cf->pool, | |
429 stport->naddrs * sizeof(ngx_stream_in_addr_t)); | |
430 if (stport->addrs == NULL) { | |
431 return NGX_ERROR; | |
432 } | |
433 | |
434 addrs = stport->addrs; | |
435 | |
436 for (i = 0; i < stport->naddrs; i++) { | |
437 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
438 sin = &addr[i].opt.u.sockaddr_in; |
6115 | 439 addrs[i].addr = sin->sin_addr.s_addr; |
440 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
441 addrs[i].conf.ctx = addr[i].opt.ctx; |
6115 | 442 #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
|
443 addrs[i].conf.ssl = addr[i].opt.ssl; |
6115 | 444 #endif |
445 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
446 len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf, |
6115 | 447 NGX_SOCKADDR_STRLEN, 1); |
448 | |
449 p = ngx_pnalloc(cf->pool, len); | |
450 if (p == NULL) { | |
451 return NGX_ERROR; | |
452 } | |
453 | |
454 ngx_memcpy(p, buf, len); | |
455 | |
456 addrs[i].conf.addr_text.len = len; | |
457 addrs[i].conf.addr_text.data = p; | |
458 } | |
459 | |
460 return NGX_OK; | |
461 } | |
462 | |
463 | |
464 #if (NGX_HAVE_INET6) | |
465 | |
466 static ngx_int_t | |
467 ngx_stream_add_addrs6(ngx_conf_t *cf, ngx_stream_port_t *stport, | |
468 ngx_stream_conf_addr_t *addr) | |
469 { | |
470 u_char *p; | |
471 size_t len; | |
472 ngx_uint_t i; | |
473 struct sockaddr_in6 *sin6; | |
474 ngx_stream_in6_addr_t *addrs6; | |
475 u_char buf[NGX_SOCKADDR_STRLEN]; | |
476 | |
477 stport->addrs = ngx_pcalloc(cf->pool, | |
478 stport->naddrs * sizeof(ngx_stream_in6_addr_t)); | |
479 if (stport->addrs == NULL) { | |
480 return NGX_ERROR; | |
481 } | |
482 | |
483 addrs6 = stport->addrs; | |
484 | |
485 for (i = 0; i < stport->naddrs; i++) { | |
486 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
487 sin6 = &addr[i].opt.u.sockaddr_in6; |
6115 | 488 addrs6[i].addr6 = sin6->sin6_addr; |
489 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
490 addrs6[i].conf.ctx = addr[i].opt.ctx; |
6115 | 491 #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
|
492 addrs6[i].conf.ssl = addr[i].opt.ssl; |
6115 | 493 #endif |
494 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
495 len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf, |
6115 | 496 NGX_SOCKADDR_STRLEN, 1); |
497 | |
498 p = ngx_pnalloc(cf->pool, len); | |
499 if (p == NULL) { | |
500 return NGX_ERROR; | |
501 } | |
502 | |
503 ngx_memcpy(p, buf, len); | |
504 | |
505 addrs6[i].conf.addr_text.len = len; | |
506 addrs6[i].conf.addr_text.data = p; | |
507 } | |
508 | |
509 return NGX_OK; | |
510 } | |
511 | |
512 #endif | |
513 | |
514 | |
515 static ngx_int_t | |
516 ngx_stream_cmp_conf_addrs(const void *one, const void *two) | |
517 { | |
518 ngx_stream_conf_addr_t *first, *second; | |
519 | |
520 first = (ngx_stream_conf_addr_t *) one; | |
521 second = (ngx_stream_conf_addr_t *) two; | |
522 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
523 if (first->opt.wildcard) { |
6115 | 524 /* a wildcard must be the last resort, shift it to the end */ |
525 return 1; | |
526 } | |
527 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
528 if (second->opt.wildcard) { |
6115 | 529 /* a wildcard must be the last resort, shift it to the end */ |
530 return -1; | |
531 } | |
532 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
533 if (first->opt.bind && !second->opt.bind) { |
6115 | 534 /* shift explicit bind()ed addresses to the start */ |
535 return -1; | |
536 } | |
537 | |
6170
c13091e6292c
Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
Ruslan Ermilov <ru@nginx.com>
parents:
6169
diff
changeset
|
538 if (!first->opt.bind && second->opt.bind) { |
6115 | 539 /* shift explicit bind()ed addresses to the start */ |
540 return 1; | |
541 } | |
542 | |
543 /* do not sort by default */ | |
544 | |
545 return 0; | |
546 } |