diff options
author | Igor Sysoev <igor@sysoev.ru> | 2009-05-18 16:12:06 +0400 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2009-05-18 16:12:06 +0400 |
commit | c985ee7a61ce0beb12890da46971a71babdbb215 (patch) | |
tree | eec760d3a4c6e7b0e49bc2a11ebcc62551c4febf /src | |
parent | 868d805c8b9011aa9b2e1d8182a41b6b8c804ac8 (diff) |
ngx_connection_local_sockaddr()
Diffstat (limited to 'src')
-rw-r--r-- | src/core/ngx_connection.c | 60 | ||||
-rw-r--r-- | src/core/ngx_connection.h | 2 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c index a743dbbed..d4bb0648a 100644 --- a/src/core/ngx_connection.c +++ b/src/core/ngx_connection.c @@ -811,6 +811,66 @@ ngx_close_connection(ngx_connection_t *c) ngx_int_t +ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s, + ngx_uint_t port) +{ + socklen_t len; + ngx_uint_t addr; + u_char sa[NGX_SOCKADDRLEN]; + struct sockaddr_in *sin; +#if (NGX_HAVE_INET6) + ngx_uint_t i; + struct sockaddr_in6 *sin6; +#endif + + switch (c->local_sockaddr->sa_family) { + +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) c->local_sockaddr; + + for (addr = 0, i = 0; addr == 0 && i < 16; i++) { + addr |= sin6->sin6_addr.s6_addr[i]; + } + + break; +#endif + + default: /* AF_INET */ + sin = (struct sockaddr_in *) c->local_sockaddr; + addr = sin->sin_addr.s_addr; + break; + } + + if (addr == 0) { + + len = NGX_SOCKADDRLEN; + + if (getsockname(c->fd, (struct sockaddr *) &sa, &len) == -1) { + ngx_connection_error(c, ngx_socket_errno, "getsockname() failed"); + return NGX_ERROR; + } + + c->local_sockaddr = ngx_palloc(c->pool, len); + if (c->local_sockaddr == NULL) { + return NGX_ERROR; + } + + c->local_socklen = len; + ngx_memcpy(c->local_sockaddr, &sa, len); + } + + if (s == NULL) { + return NGX_OK; + } + + s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, port); + + return NGX_OK; +} + + +ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text) { ngx_uint_t level; diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h index 7a4c65860..5fd7a5ead 100644 --- a/src/core/ngx_connection.h +++ b/src/core/ngx_connection.h @@ -170,6 +170,8 @@ ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle); void ngx_configure_listening_sockets(ngx_cycle_t *cycle); void ngx_close_listening_sockets(ngx_cycle_t *cycle); void ngx_close_connection(ngx_connection_t *c); +ngx_int_t ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s, + ngx_uint_t port); ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text); ngx_connection_t *ngx_get_connection(ngx_socket_t s, ngx_log_t *log); |