comparison src/stream/ngx_stream.c @ 6170:c13091e6292c

Stream: embed ngx_stream_listen_t into ngx_stream_conf_addr_t.
author Ruslan Ermilov <ru@nginx.com>
date Mon, 08 Jun 2015 23:11:42 +0300
parents f654addf0eea
children 3e8cddcff381
comparison
equal deleted inserted replaced
6169:f654addf0eea 6170:c13091e6292c
237 ngx_stream_conf_addr_t *addr; 237 ngx_stream_conf_addr_t *addr;
238 #if (NGX_HAVE_INET6) 238 #if (NGX_HAVE_INET6)
239 struct sockaddr_in6 *sin6; 239 struct sockaddr_in6 *sin6;
240 #endif 240 #endif
241 241
242 sa = (struct sockaddr *) &listen->sockaddr; 242 sa = &listen->u.sockaddr;
243 243
244 switch (sa->sa_family) { 244 switch (sa->sa_family) {
245 245
246 #if (NGX_HAVE_INET6) 246 #if (NGX_HAVE_INET6)
247 case AF_INET6: 247 case AF_INET6:
248 sin6 = (struct sockaddr_in6 *) sa; 248 sin6 = &listen->u.sockaddr_in6;
249 p = sin6->sin6_port; 249 p = sin6->sin6_port;
250 break; 250 break;
251 #endif 251 #endif
252 252
253 #if (NGX_HAVE_UNIX_DOMAIN) 253 #if (NGX_HAVE_UNIX_DOMAIN)
255 p = 0; 255 p = 0;
256 break; 256 break;
257 #endif 257 #endif
258 258
259 default: /* AF_INET */ 259 default: /* AF_INET */
260 sin = (struct sockaddr_in *) sa; 260 sin = &listen->u.sockaddr_in;
261 p = sin->sin_port; 261 p = sin->sin_port;
262 break; 262 break;
263 } 263 }
264 264
265 port = ports->elts; 265 port = ports->elts;
295 addr = ngx_array_push(&port->addrs); 295 addr = ngx_array_push(&port->addrs);
296 if (addr == NULL) { 296 if (addr == NULL) {
297 return NGX_ERROR; 297 return NGX_ERROR;
298 } 298 }
299 299
300 addr->sockaddr = (struct sockaddr *) &listen->sockaddr; 300 addr->opt = *listen;
301 addr->socklen = listen->socklen;
302 addr->ctx = listen->ctx;
303 addr->bind = listen->bind;
304 addr->wildcard = listen->wildcard;
305 addr->so_keepalive = listen->so_keepalive;
306 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
307 addr->tcp_keepidle = listen->tcp_keepidle;
308 addr->tcp_keepintvl = listen->tcp_keepintvl;
309 addr->tcp_keepcnt = listen->tcp_keepcnt;
310 #endif
311 #if (NGX_STREAM_SSL)
312 addr->ssl = listen->ssl;
313 #endif
314 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
315 addr->ipv6only = listen->ipv6only;
316 #endif
317 #if (NGX_HAVE_REUSEPORT)
318 addr->reuseport = listen->reuseport;
319 #endif
320 301
321 return NGX_OK; 302 return NGX_OK;
322 } 303 }
323 304
324 305
344 /* 325 /*
345 * if there is the binding to the "*:port" then we need to bind() 326 * if there is the binding to the "*:port" then we need to bind()
346 * to the "*:port" only and ignore the other bindings 327 * to the "*:port" only and ignore the other bindings
347 */ 328 */
348 329
349 if (addr[last - 1].wildcard) { 330 if (addr[last - 1].opt.wildcard) {
350 addr[last - 1].bind = 1; 331 addr[last - 1].opt.bind = 1;
351 bind_wildcard = 1; 332 bind_wildcard = 1;
352 333
353 } else { 334 } else {
354 bind_wildcard = 0; 335 bind_wildcard = 0;
355 } 336 }
356 337
357 i = 0; 338 i = 0;
358 339
359 while (i < last) { 340 while (i < last) {
360 341
361 if (bind_wildcard && !addr[i].bind) { 342 if (bind_wildcard && !addr[i].opt.bind) {
362 i++; 343 i++;
363 continue; 344 continue;
364 } 345 }
365 346
366 ls = ngx_create_listening(cf, addr[i].sockaddr, addr[i].socklen); 347 ls = ngx_create_listening(cf, &addr[i].opt.u.sockaddr,
348 addr[i].opt.socklen);
367 if (ls == NULL) { 349 if (ls == NULL) {
368 return NGX_CONF_ERROR; 350 return NGX_CONF_ERROR;
369 } 351 }
370 352
371 ls->addr_ntop = 1; 353 ls->addr_ntop = 1;
372 ls->handler = ngx_stream_init_connection; 354 ls->handler = ngx_stream_init_connection;
373 ls->pool_size = 256; 355 ls->pool_size = 256;
374 356
375 cscf = addr->ctx->srv_conf[ngx_stream_core_module.ctx_index]; 357 cscf = addr->opt.ctx->srv_conf[ngx_stream_core_module.ctx_index];
376 358
377 ls->logp = cscf->error_log; 359 ls->logp = cscf->error_log;
378 ls->log.data = &ls->addr_text; 360 ls->log.data = &ls->addr_text;
379 ls->log.handler = ngx_accept_log_error; 361 ls->log.handler = ngx_accept_log_error;
380 362
381 ls->keepalive = addr[i].so_keepalive; 363 ls->keepalive = addr[i].opt.so_keepalive;
382 #if (NGX_HAVE_KEEPALIVE_TUNABLE) 364 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
383 ls->keepidle = addr[i].tcp_keepidle; 365 ls->keepidle = addr[i].opt.tcp_keepidle;
384 ls->keepintvl = addr[i].tcp_keepintvl; 366 ls->keepintvl = addr[i].opt.tcp_keepintvl;
385 ls->keepcnt = addr[i].tcp_keepcnt; 367 ls->keepcnt = addr[i].opt.tcp_keepcnt;
386 #endif 368 #endif
387 369
388 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) 370 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
389 ls->ipv6only = addr[i].ipv6only; 371 ls->ipv6only = addr[i].opt.ipv6only;
390 #endif 372 #endif
391 373
392 #if (NGX_HAVE_REUSEPORT) 374 #if (NGX_HAVE_REUSEPORT)
393 ls->reuseport = addr[i].reuseport; 375 ls->reuseport = addr[i].opt.reuseport;
394 #endif 376 #endif
395 377
396 stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t)); 378 stport = ngx_palloc(cf->pool, sizeof(ngx_stream_port_t));
397 if (stport == NULL) { 379 if (stport == NULL) {
398 return NGX_CONF_ERROR; 380 return NGX_CONF_ERROR;
449 431
450 addrs = stport->addrs; 432 addrs = stport->addrs;
451 433
452 for (i = 0; i < stport->naddrs; i++) { 434 for (i = 0; i < stport->naddrs; i++) {
453 435
454 sin = (struct sockaddr_in *) addr[i].sockaddr; 436 sin = &addr[i].opt.u.sockaddr_in;
455 addrs[i].addr = sin->sin_addr.s_addr; 437 addrs[i].addr = sin->sin_addr.s_addr;
456 438
457 addrs[i].conf.ctx = addr[i].ctx; 439 addrs[i].conf.ctx = addr[i].opt.ctx;
458 #if (NGX_STREAM_SSL) 440 #if (NGX_STREAM_SSL)
459 addrs[i].conf.ssl = addr[i].ssl; 441 addrs[i].conf.ssl = addr[i].opt.ssl;
460 #endif 442 #endif
461 443
462 len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf, 444 len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf,
463 NGX_SOCKADDR_STRLEN, 1); 445 NGX_SOCKADDR_STRLEN, 1);
464 446
465 p = ngx_pnalloc(cf->pool, len); 447 p = ngx_pnalloc(cf->pool, len);
466 if (p == NULL) { 448 if (p == NULL) {
467 return NGX_ERROR; 449 return NGX_ERROR;
498 480
499 addrs6 = stport->addrs; 481 addrs6 = stport->addrs;
500 482
501 for (i = 0; i < stport->naddrs; i++) { 483 for (i = 0; i < stport->naddrs; i++) {
502 484
503 sin6 = (struct sockaddr_in6 *) addr[i].sockaddr; 485 sin6 = &addr[i].opt.u.sockaddr_in6;
504 addrs6[i].addr6 = sin6->sin6_addr; 486 addrs6[i].addr6 = sin6->sin6_addr;
505 487
506 addrs6[i].conf.ctx = addr[i].ctx; 488 addrs6[i].conf.ctx = addr[i].opt.ctx;
507 #if (NGX_STREAM_SSL) 489 #if (NGX_STREAM_SSL)
508 addrs6[i].conf.ssl = addr[i].ssl; 490 addrs6[i].conf.ssl = addr[i].opt.ssl;
509 #endif 491 #endif
510 492
511 len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf, 493 len = ngx_sock_ntop(&addr[i].opt.u.sockaddr, addr[i].opt.socklen, buf,
512 NGX_SOCKADDR_STRLEN, 1); 494 NGX_SOCKADDR_STRLEN, 1);
513 495
514 p = ngx_pnalloc(cf->pool, len); 496 p = ngx_pnalloc(cf->pool, len);
515 if (p == NULL) { 497 if (p == NULL) {
516 return NGX_ERROR; 498 return NGX_ERROR;
534 ngx_stream_conf_addr_t *first, *second; 516 ngx_stream_conf_addr_t *first, *second;
535 517
536 first = (ngx_stream_conf_addr_t *) one; 518 first = (ngx_stream_conf_addr_t *) one;
537 second = (ngx_stream_conf_addr_t *) two; 519 second = (ngx_stream_conf_addr_t *) two;
538 520
539 if (first->wildcard) { 521 if (first->opt.wildcard) {
540 /* a wildcard must be the last resort, shift it to the end */ 522 /* a wildcard must be the last resort, shift it to the end */
541 return 1; 523 return 1;
542 } 524 }
543 525
544 if (second->wildcard) { 526 if (second->opt.wildcard) {
545 /* a wildcard must be the last resort, shift it to the end */ 527 /* a wildcard must be the last resort, shift it to the end */
546 return -1; 528 return -1;
547 } 529 }
548 530
549 if (first->bind && !second->bind) { 531 if (first->opt.bind && !second->opt.bind) {
550 /* shift explicit bind()ed addresses to the start */ 532 /* shift explicit bind()ed addresses to the start */
551 return -1; 533 return -1;
552 } 534 }
553 535
554 if (!first->bind && second->bind) { 536 if (!first->opt.bind && second->opt.bind) {
555 /* shift explicit bind()ed addresses to the start */ 537 /* shift explicit bind()ed addresses to the start */
556 return 1; 538 return 1;
557 } 539 }
558 540
559 /* do not sort by default */ 541 /* do not sort by default */