aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2008-08-21 18:47:23 +0000
committerIgor Sysoev <igor@sysoev.ru>2008-08-21 18:47:23 +0000
commit6a75019ad6847a5bf8c0dcd1f0ad87ed715add7e (patch)
treef7feb9bb2be60866bf079e19c0736dc0d3f160f3
parentbbe42c41e8f883185745706f92426aa9e73b3eab (diff)
downloadnginx-6a75019ad6847a5bf8c0dcd1f0ad87ed715add7e.tar.gz
nginx-6a75019ad6847a5bf8c0dcd1f0ad87ed715add7e.zip
*) remove zero termination in ngx_inet_ntop() and ngx_sock_ntop()
*) use ngx_snprintf() in ngx_inet_ntop() and ngx_sock_ntop() as they are called just once per connection *) NGX_INET_ADDRSTRLEN
-rw-r--r--src/core/ngx_config.h4
-rw-r--r--src/core/ngx_connection.c13
-rw-r--r--src/core/ngx_inet.c156
-rw-r--r--src/core/ngx_inet.h3
-rw-r--r--src/http/modules/ngx_http_status_module.c4
-rw-r--r--src/http/ngx_http_core_module.c4
-rw-r--r--src/http/ngx_http_core_module.h2
-rw-r--r--src/http/ngx_http_header_filter_module.c2
-rw-r--r--src/http/ngx_http_upstream_round_robin.c4
-rw-r--r--src/http/ngx_http_variables.c2
-rw-r--r--src/mail/ngx_mail.c4
11 files changed, 36 insertions, 162 deletions
diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h
index d2d4e043a..5861bfd09 100644
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -116,10 +116,6 @@ typedef intptr_t ngx_flag_t;
#define INADDR_NONE ((unsigned int) -1)
#endif
-#ifndef INET_ADDRSTRLEN /* Win32 */
-#define INET_ADDRSTRLEN 16
-#endif
-
#ifdef MAXHOSTNAMELEN
#define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN
#else
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
index 03662b398..827477501 100644
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -37,12 +37,13 @@ ngx_listening_inet_stream_socket(ngx_conf_t *cf, in_addr_t addr, in_port_t port)
ls->addr_text.data = ngx_pnalloc(cf->pool,
- INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
+ NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
if (ls->addr_text.data == NULL) {
return NULL;
}
- len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data, INET_ADDRSTRLEN);
+ len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data,
+ NGX_INET_ADDRSTRLEN);
ls->addr_text.len = ngx_sprintf(ls->addr_text.data + len, ":%d", port)
- ls->addr_text.data;
@@ -53,7 +54,7 @@ ngx_listening_inet_stream_socket(ngx_conf_t *cf, in_addr_t addr, in_port_t port)
ls->sockaddr = (struct sockaddr *) sin;
ls->socklen = sizeof(struct sockaddr_in);
ls->addr = offsetof(struct sockaddr_in, sin_addr);
- ls->addr_text_max_len = INET_ADDRSTRLEN;
+ ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
return ls;
}
@@ -104,17 +105,17 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
continue;
}
- ls[i].addr_text_max_len = INET_ADDRSTRLEN;
+ ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN;
ls[i].addr_text.data = ngx_pnalloc(cycle->pool,
- INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
+ NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
if (ls[i].addr_text.data == NULL) {
return NGX_ERROR;
}
ls[i].family = sin->sin_family;
len = ngx_sock_ntop(ls[i].family, ls[i].sockaddr,
- ls[i].addr_text.data, INET_ADDRSTRLEN);
+ ls[i].addr_text.data, NGX_INET_ADDRSTRLEN);
if (len == 0) {
return NGX_ERROR;
}
diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index aefdfcdad..dc0a550c6 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -8,9 +8,6 @@
#include <ngx_core.h>
-static size_t ngx_sprint_uchar(u_char *text, u_char c, size_t len);
-
-
/* AF_INET only */
in_addr_t
@@ -56,166 +53,43 @@ ngx_inet_addr(u_char *text, size_t len)
}
-/*
- * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as
- * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however,
- * they had been implemented long before the ngx_sprintf() had appeared
- * and they are faster by 1.5-2.5 times, so it is worth to keep them.
- *
- * By the way, the implementation using ngx_sprintf() is faster by 2.5-3 times
- * than using FreeBSD libc's snprintf().
- */
-
/* AF_INET only */
size_t
ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len)
{
u_char *p;
- size_t n;
- ngx_uint_t i;
struct sockaddr_in *sin;
- if (len == 0) {
- return 0;
- }
+ if (family == AF_INET) {
- if (family != AF_INET) {
- return 0;
- }
+ sin = (struct sockaddr_in *) sa;
+ p = (u_char *) &sin->sin_addr;
- sin = (struct sockaddr_in *) sa;
- p = (u_char *) &sin->sin_addr;
-
- if (len > INET_ADDRSTRLEN) {
- len = INET_ADDRSTRLEN;
+ return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
+ p[0], p[1], p[2], p[3])
+ - text;
}
- n = ngx_sprint_uchar(text, p[0], len);
-
- i = 1;
-
- do {
- if (len == n) {
- text[n - 1] = '\0';
- return n;
- }
-
- text[n++] = '.';
-
- if (len == n) {
- text[n - 1] = '\0';
- return n;
- }
-
- n += ngx_sprint_uchar(&text[n], p[i++], len - n);
-
- } while (i < 4);
-
- if (len == n) {
- text[n] = '\0';
- return n;
- }
-
- text[n] = '\0';
-
- return n;
+ return 0;
}
size_t
ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
{
- u_char *p;
- size_t n;
- ngx_uint_t i;
-
- if (len == 0) {
- return 0;
- }
+ u_char *p;
- if (family != AF_INET) {
- return 0;
- }
+ if (family == AF_INET) {
- p = (u_char *) addr;
+ p = (u_char *) addr;
- if (len > INET_ADDRSTRLEN) {
- len = INET_ADDRSTRLEN;
+ return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
+ p[0], p[1], p[2], p[3])
+ - text;
}
- n = ngx_sprint_uchar(text, p[0], len);
-
- i = 1;
-
- do {
- if (len == n) {
- text[n - 1] = '\0';
- return n;
- }
-
- text[n++] = '.';
-
- if (len == n) {
- text[n - 1] = '\0';
- return n;
- }
-
- n += ngx_sprint_uchar(&text[n], p[i++], len - n);
-
- } while (i < 4);
-
- if (len == n) {
- text[n] = '\0';
- return n;
- }
-
- text[n] = '\0';
-
- return n;
-}
-
-
-static size_t
-ngx_sprint_uchar(u_char *text, u_char c, size_t len)
-{
- size_t n;
- ngx_uint_t c1, c2;
-
- n = 0;
-
- if (len == n) {
- return n;
- }
-
- c1 = c / 100;
-
- if (c1) {
- *text++ = (u_char) (c1 + '0');
- n++;
-
- if (len == n) {
- return n;
- }
- }
-
- c2 = (c % 100) / 10;
-
- if (c1 || c2) {
- *text++ = (u_char) (c2 + '0');
- n++;
-
- if (len == n) {
- return n;
- }
- }
-
- c2 = c % 10;
-
- *text = (u_char) (c2 + '0');
- n++;
-
- return n;
+ return 0;
}
@@ -576,7 +450,7 @@ ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u)
u->addrs[i].sockaddr = (struct sockaddr *) sin;
u->addrs[i].socklen = sizeof(struct sockaddr_in);
- len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
+ len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
p = ngx_pnalloc(pool, len);
if (p == NULL) {
diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h
index 509f80ab6..95ba30ba7 100644
--- a/src/core/ngx_inet.h
+++ b/src/core/ngx_inet.h
@@ -12,6 +12,9 @@
#include <ngx_core.h>
+#define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
+
+
typedef struct {
in_addr_t addr;
in_addr_t mask;
diff --git a/src/http/modules/ngx_http_status_module.c b/src/http/modules/ngx_http_status_module.c
index a2e2dbc07..000d9a8e4 100644
--- a/src/http/modules/ngx_http_status_module.c
+++ b/src/http/modules/ngx_http_status_module.c
@@ -155,7 +155,7 @@ static ngx_int_t ngx_http_status(ngx_http_status_ctx_t *ctx)
len = NGX_INT64_LEN /* pid */
+ 1 + NGX_INT32_LEN /* connection */
+ 1 + 1 /* state */
- + 1 + INET_ADDRSTRLEN
+ + 1 + NGX_INET_ADDRSTRLEN
+ 1 + (r->server_name ? cmcf->max_server_name_len : 1)
+ 2; /* "\r\n" */
@@ -204,7 +204,7 @@ static ngx_int_t ngx_http_status(ngx_http_status_ctx_t *ctx)
*(b->last++) = ' ';
b->last = ngx_cpymem(b->last, c[i].addr_text.data,
c[i].addr_text.len);
- for (n = c[i].addr_text.len; n < INET_ADDRSTRLEN; n++) {
+ for (n = c[i].addr_text.len; n < NGX_INET_ADDRSTRLEN; n++) {
*(b->last++) = ' ';
}
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 8a5e9d261..30b2c0c50 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1626,7 +1626,7 @@ ngx_http_server_addr(ngx_http_request_t *r, ngx_str_t *s)
}
s->len = ngx_inet_ntop(c->listening->family, &r->in_addr,
- s->data, INET_ADDRSTRLEN);
+ s->data, NGX_INET_ADDRSTRLEN);
return NGX_OK;
}
@@ -2971,7 +2971,7 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ls->conf.rcvbuf = -1;
ls->conf.sndbuf = -1;
- n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, INET_ADDRSTRLEN + 6);
+ n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, NGX_INET_ADDRSTRLEN);
ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port);
if (cf->args->nelts == 2) {
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index 3ecc7c33a..a42b0dac8 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -47,7 +47,7 @@ typedef struct {
ngx_uint_t deferred_accept;
#endif
- u_char addr[INET_ADDRSTRLEN + 6];
+ u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")];
} ngx_http_listen_conf_t;
diff --git a/src/http/ngx_http_header_filter_module.c b/src/http/ngx_http_header_filter_module.c
index 7714873e0..de9533391 100644
--- a/src/http/ngx_http_header_filter_module.c
+++ b/src/http/ngx_http_header_filter_module.c
@@ -162,7 +162,7 @@ ngx_http_header_filter(ngx_http_request_t *r)
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf;
/* AF_INET only */
- u_char addr[INET_ADDRSTRLEN];
+ u_char addr[NGX_INET_ADDRSTRLEN];
r->header_sent = 1;
diff --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c
index e36e68529..d9450c95c 100644
--- a/src/http/ngx_http_upstream_round_robin.c
+++ b/src/http/ngx_http_upstream_round_robin.c
@@ -281,14 +281,14 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
for (i = 0; i < ur->naddrs; i++) {
- len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
+ len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
p = ngx_pnalloc(r->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
- len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, INET_ADDRSTRLEN);
+ len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in));
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
index cd706a2a9..15d1c6d0c 100644
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -872,7 +872,7 @@ ngx_http_variable_server_addr(ngx_http_request_t *r,
{
ngx_str_t s;
- s.data = ngx_pnalloc(r->pool, INET_ADDRSTRLEN);
+ s.data = ngx_pnalloc(r->pool, NGX_INET_ADDRSTRLEN);
if (s.data == NULL) {
return NGX_ERROR;
}
diff --git a/src/mail/ngx_mail.c b/src/mail/ngx_mail.c
index edfa245ee..73668a538 100644
--- a/src/mail/ngx_mail.c
+++ b/src/mail/ngx_mail.c
@@ -358,13 +358,13 @@ ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
imip->addrs[i].ctx = in_addr[i].ctx;
text = ngx_pnalloc(cf->pool,
- INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
+ NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
if (text == NULL) {
return NGX_CONF_ERROR;
}
len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text,
- INET_ADDRSTRLEN);
+ NGX_INET_ADDRSTRLEN);
len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text;