Core: externalized ngx_cmp_sockaddr().

It's also extended with the "cmp_port" argument to indicate
whether ports should be compared as well, or only addresses.
This commit is contained in:
Ruslan Ermilov 2013-12-06 14:30:27 +04:00
parent 159486239f
commit efd0e0e177
3 changed files with 78 additions and 70 deletions

View File

@ -11,7 +11,6 @@
static void ngx_destroy_cycle_pools(ngx_conf_t *conf); static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2);
static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle, static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
ngx_shm_zone_t *shm_zone); ngx_shm_zone_t *shm_zone);
static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log); static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
@ -494,7 +493,9 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
continue; continue;
} }
if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK) if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen,
ls[i].sockaddr, ls[n].socklen, 1)
== NGX_OK)
{ {
nls[n].fd = ls[i].fd; nls[n].fd = ls[i].fd;
nls[n].previous = &ls[i]; nls[n].previous = &ls[i];
@ -848,74 +849,6 @@ ngx_destroy_cycle_pools(ngx_conf_t *conf)
} }
static ngx_int_t
ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
{
struct sockaddr_in *sin1, *sin2;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin61, *sin62;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
struct sockaddr_un *saun1, *saun2;
#endif
if (sa1->sa_family != sa2->sa_family) {
return NGX_DECLINED;
}
switch (sa1->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin61 = (struct sockaddr_in6 *) sa1;
sin62 = (struct sockaddr_in6 *) sa2;
if (sin61->sin6_port != sin62->sin6_port) {
return NGX_DECLINED;
}
if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
return NGX_DECLINED;
}
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
saun1 = (struct sockaddr_un *) sa1;
saun2 = (struct sockaddr_un *) sa2;
if (ngx_memcmp(&saun1->sun_path, &saun2->sun_path,
sizeof(saun1->sun_path))
!= 0)
{
return NGX_DECLINED;
}
break;
#endif
default: /* AF_INET */
sin1 = (struct sockaddr_in *) sa1;
sin2 = (struct sockaddr_in *) sa2;
if (sin1->sin_port != sin2->sin_port) {
return NGX_DECLINED;
}
if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
return NGX_DECLINED;
}
break;
}
return NGX_OK;
}
static ngx_int_t static ngx_int_t
ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn) ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
{ {

View File

@ -1196,3 +1196,76 @@ ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
} }
#endif /* NGX_HAVE_GETADDRINFO && NGX_HAVE_INET6 */ #endif /* NGX_HAVE_GETADDRINFO && NGX_HAVE_INET6 */
ngx_int_t
ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port)
{
struct sockaddr_in *sin1, *sin2;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin61, *sin62;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
struct sockaddr_un *saun1, *saun2;
#endif
if (sa1->sa_family != sa2->sa_family) {
return NGX_DECLINED;
}
switch (sa1->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin61 = (struct sockaddr_in6 *) sa1;
sin62 = (struct sockaddr_in6 *) sa2;
if (cmp_port && sin61->sin6_port != sin62->sin6_port) {
return NGX_DECLINED;
}
if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
return NGX_DECLINED;
}
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
/* TODO length */
saun1 = (struct sockaddr_un *) sa1;
saun2 = (struct sockaddr_un *) sa2;
if (ngx_memcmp(&saun1->sun_path, &saun2->sun_path,
sizeof(saun1->sun_path))
!= 0)
{
return NGX_DECLINED;
}
break;
#endif
default: /* AF_INET */
sin1 = (struct sockaddr_in *) sa1;
sin2 = (struct sockaddr_in *) sa2;
if (cmp_port && sin1->sin_port != sin2->sin_port) {
return NGX_DECLINED;
}
if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
return NGX_DECLINED;
}
break;
}
return NGX_OK;
}

View File

@ -115,6 +115,8 @@ ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len); size_t len);
ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u); ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u); ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,
struct sockaddr *sa2, socklen_t slen2, ngx_uint_t cmp_port);
#endif /* _NGX_INET_H_INCLUDED_ */ #endif /* _NGX_INET_H_INCLUDED_ */