Klaus Demo nginx / af18946
Core: extended ngx_sock_ntop() with socklen parameter. On Linux, sockaddr length is required to process unix socket addresses properly due to unnamed sockets (which don't have sun_path set at all) and abstract namespace sockets. Vladimir Homutov 9 years ago
10 changed file(s) with 39 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
4040 ls->sockaddr = sa;
4141 ls->socklen = socklen;
4242
43 len = ngx_sock_ntop(sa, text, NGX_SOCKADDR_STRLEN, 1);
43 len = ngx_sock_ntop(sa, socklen, text, NGX_SOCKADDR_STRLEN, 1);
4444 ls->addr_text.len = len;
4545
4646 switch (ls->sockaddr->sa_family) {
151151 return NGX_ERROR;
152152 }
153153
154 len = ngx_sock_ntop(ls[i].sockaddr, ls[i].addr_text.data, len, 1);
154 len = ngx_sock_ntop(ls[i].sockaddr, ls[i].socklen,
155 ls[i].addr_text.data, len, 1);
155156 if (len == 0) {
156157 return NGX_ERROR;
157158 }
10671068 return NGX_OK;
10681069 }
10691070
1070 s->len = ngx_sock_ntop(c->local_sockaddr, s->data, s->len, port);
1071 s->len = ngx_sock_ntop(c->local_sockaddr, len, s->data, s->len, port);
10711072
10721073 return NGX_OK;
10731074 }
173173
174174
175175 size_t
176 ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port)
176 ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text, size_t len,
177 ngx_uint_t port)
177178 {
178179 u_char *p;
179180 struct sockaddr_in *sin;
229230 case AF_UNIX:
230231 saun = (struct sockaddr_un *) sa;
231232
233 /* on Linux sockaddr might not include sun_path at all */
234
235 if (socklen <= offsetof(struct sockaddr_un, sun_path)) {
236 p = ngx_snprintf(text, len, "unix:%Z");
237
238 } else {
239 p = ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path);
240 }
241
232242 /* we do not include trailing zero in address length */
233243
234 return ngx_snprintf(text, len, "unix:%s%Z", saun->sun_path) - text - 1;
244 return (p - text - 1);
235245
236246 #endif
237247
10191029 goto failed;
10201030 }
10211031
1022 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
1032 len = ngx_sock_ntop((struct sockaddr *) sin, rp->ai_addrlen, p, len, 1);
10231033
10241034 u->addrs[i].name.len = len;
10251035 u->addrs[i].name.data = p;
10521062 goto failed;
10531063 }
10541064
1055 len = ngx_sock_ntop((struct sockaddr *) sin6, p, len, 1);
1065 len = ngx_sock_ntop((struct sockaddr *) sin6, rp->ai_addrlen, p,
1066 len, 1);
10561067
10571068 u->addrs[i].name.len = len;
10581069 u->addrs[i].name.data = p;
11371148 return NGX_ERROR;
11381149 }
11391150
1140 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
1151 len = ngx_sock_ntop((struct sockaddr *) sin,
1152 sizeof(struct sockaddr_in), p, len, 1);
11411153
11421154 u->addrs[i].name.len = len;
11431155 u->addrs[i].name.data = p;
106106 ngx_int_t ngx_inet6_addr(u_char *p, size_t len, u_char *addr);
107107 size_t ngx_inet6_ntop(u_char *p, u_char *text, size_t len);
108108 #endif
109 size_t ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len,
110 ngx_uint_t port);
109 size_t ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text,
110 size_t len, ngx_uint_t port);
111111 size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
112112 ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr);
113113 ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
274274 return;
275275 }
276276
277 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
277 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
278 c->addr_text.data,
278279 ls->addr_text_max_len, 0);
279280 if (c->addr_text.len == 0) {
280281 ngx_close_accepted_connection(c);
6767 return;
6868 }
6969
70 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->addr_text.data,
70 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen,
71 c->addr_text.data,
7172 ls->addr_text_max_len, 0);
7273 if (c->addr_text.len == 0) {
7374 /* TODO: close socket */
877877 goto failed;
878878 }
879879
880 len = ngx_sock_ntop((struct sockaddr *) sin, p, len, 1);
880 len = ngx_sock_ntop((struct sockaddr *) sin, sizeof(struct sockaddr_in),
881 p, len, 1);
881882
882883 ctx->addrs[i].name.len = len;
883884 ctx->addrs[i].name.data = p;
229229
230230 c = r->connection;
231231
232 len = ngx_sock_ntop(addr->sockaddr, text, NGX_SOCKADDR_STRLEN, 0);
232 len = ngx_sock_ntop(addr->sockaddr, addr->socklen, text,
233 NGX_SOCKADDR_STRLEN, 0);
233234 if (len == 0) {
234235 return NGX_HTTP_INTERNAL_SERVER_ERROR;
235236 }
30323032 #endif
30333033 lsopt.wildcard = 1;
30343034
3035 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
3035 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
30363036 NGX_SOCKADDR_STRLEN, 1);
30373037
30383038 if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
39833983 lsopt.ipv6only = 1;
39843984 #endif
39853985
3986 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
3986 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
39873987 NGX_SOCKADDR_STRLEN, 1);
39883988
39893989 for (n = 2; n < cf->args->nelts; n++) {
464464 addrs[i].conf.ssl = addr[i].ssl;
465465 #endif
466466
467 len = ngx_sock_ntop(addr[i].sockaddr, buf, NGX_SOCKADDR_STRLEN, 1);
467 len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen , buf,
468 NGX_SOCKADDR_STRLEN, 1);
468469
469470 p = ngx_pnalloc(cf->pool, len);
470471 if (p == NULL) {
512513 addrs6[i].conf.ssl = addr[i].ssl;
513514 #endif
514515
515 len = ngx_sock_ntop(addr[i].sockaddr, buf, NGX_SOCKADDR_STRLEN, 1);
516 len = ngx_sock_ntop(addr[i].sockaddr, addr[i].socklen, buf,
517 NGX_SOCKADDR_STRLEN, 1);
516518
517519 p = ngx_pnalloc(cf->pool, len);
518520 if (p == NULL) {
438438 ls->bind = 1;
439439
440440 } else {
441 len = ngx_sock_ntop(sa, buf, NGX_SOCKADDR_STRLEN, 1);
441 len = ngx_sock_ntop(sa, ls->socklen, buf,
442 NGX_SOCKADDR_STRLEN, 1);
442443
443444 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
444445 "ipv6only is not supported "