Klaus Demo nginx / 6a75019
*) 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 Igor Sysoev 12 years ago
11 changed file(s) with 45 addition(s) and 171 deletion(s). Raw diff Collapse all Expand all
115115 #define INADDR_NONE ((unsigned int) -1)
116116 #endif
117117
118 #ifndef INET_ADDRSTRLEN /* Win32 */
119 #define INET_ADDRSTRLEN 16
120 #endif
121
122118 #ifdef MAXHOSTNAMELEN
123119 #define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN
124120 #else
3636
3737
3838 ls->addr_text.data = ngx_pnalloc(cf->pool,
39 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
39 NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
4040 if (ls->addr_text.data == NULL) {
4141 return NULL;
4242 }
4343
44 len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data, INET_ADDRSTRLEN);
44 len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data,
45 NGX_INET_ADDRSTRLEN);
4546
4647 ls->addr_text.len = ngx_sprintf(ls->addr_text.data + len, ":%d", port)
4748 - ls->addr_text.data;
5253 ls->sockaddr = (struct sockaddr *) sin;
5354 ls->socklen = sizeof(struct sockaddr_in);
5455 ls->addr = offsetof(struct sockaddr_in, sin_addr);
55 ls->addr_text_max_len = INET_ADDRSTRLEN;
56 ls->addr_text_max_len = NGX_INET_ADDRSTRLEN;
5657
5758 return ls;
5859 }
103104 continue;
104105 }
105106
106 ls[i].addr_text_max_len = INET_ADDRSTRLEN;
107 ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN;
107108
108109 ls[i].addr_text.data = ngx_pnalloc(cycle->pool,
109 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
110 NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
110111 if (ls[i].addr_text.data == NULL) {
111112 return NGX_ERROR;
112113 }
113114
114115 ls[i].family = sin->sin_family;
115116 len = ngx_sock_ntop(ls[i].family, ls[i].sockaddr,
116 ls[i].addr_text.data, INET_ADDRSTRLEN);
117 ls[i].addr_text.data, NGX_INET_ADDRSTRLEN);
117118 if (len == 0) {
118119 return NGX_ERROR;
119120 }
55
66 #include <ngx_config.h>
77 #include <ngx_core.h>
8
9
10 static size_t ngx_sprint_uchar(u_char *text, u_char c, size_t len);
118
129
1310 /* AF_INET only */
5552 }
5653
5754
58 /*
59 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as
60 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however,
61 * they had been implemented long before the ngx_sprintf() had appeared
62 * and they are faster by 1.5-2.5 times, so it is worth to keep them.
63 *
64 * By the way, the implementation using ngx_sprintf() is faster by 2.5-3 times
65 * than using FreeBSD libc's snprintf().
66 */
67
6855 /* AF_INET only */
6956
7057 size_t
7158 ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len)
7259 {
7360 u_char *p;
74 size_t n;
75 ngx_uint_t i;
7661 struct sockaddr_in *sin;
7762
78 if (len == 0) {
79 return 0;
80 }
81
82 if (family != AF_INET) {
83 return 0;
84 }
85
86 sin = (struct sockaddr_in *) sa;
87 p = (u_char *) &sin->sin_addr;
88
89 if (len > INET_ADDRSTRLEN) {
90 len = INET_ADDRSTRLEN;
91 }
92
93 n = ngx_sprint_uchar(text, p[0], len);
94
95 i = 1;
96
97 do {
98 if (len == n) {
99 text[n - 1] = '\0';
100 return n;
101 }
102
103 text[n++] = '.';
104
105 if (len == n) {
106 text[n - 1] = '\0';
107 return n;
108 }
109
110 n += ngx_sprint_uchar(&text[n], p[i++], len - n);
111
112 } while (i < 4);
113
114 if (len == n) {
115 text[n] = '\0';
116 return n;
117 }
118
119 text[n] = '\0';
120
121 return n;
63 if (family == AF_INET) {
64
65 sin = (struct sockaddr_in *) sa;
66 p = (u_char *) &sin->sin_addr;
67
68 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
69 p[0], p[1], p[2], p[3])
70 - text;
71 }
72
73 return 0;
12274 }
12375
12476
12577 size_t
12678 ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
12779 {
128 u_char *p;
129 size_t n;
130 ngx_uint_t i;
131
132 if (len == 0) {
133 return 0;
134 }
135
136 if (family != AF_INET) {
137 return 0;
138 }
139
140 p = (u_char *) addr;
141
142 if (len > INET_ADDRSTRLEN) {
143 len = INET_ADDRSTRLEN;
144 }
145
146 n = ngx_sprint_uchar(text, p[0], len);
147
148 i = 1;
149
150 do {
151 if (len == n) {
152 text[n - 1] = '\0';
153 return n;
154 }
155
156 text[n++] = '.';
157
158 if (len == n) {
159 text[n - 1] = '\0';
160 return n;
161 }
162
163 n += ngx_sprint_uchar(&text[n], p[i++], len - n);
164
165 } while (i < 4);
166
167 if (len == n) {
168 text[n] = '\0';
169 return n;
170 }
171
172 text[n] = '\0';
173
174 return n;
175 }
176
177
178 static size_t
179 ngx_sprint_uchar(u_char *text, u_char c, size_t len)
180 {
181 size_t n;
182 ngx_uint_t c1, c2;
183
184 n = 0;
185
186 if (len == n) {
187 return n;
188 }
189
190 c1 = c / 100;
191
192 if (c1) {
193 *text++ = (u_char) (c1 + '0');
194 n++;
195
196 if (len == n) {
197 return n;
198 }
199 }
200
201 c2 = (c % 100) / 10;
202
203 if (c1 || c2) {
204 *text++ = (u_char) (c2 + '0');
205 n++;
206
207 if (len == n) {
208 return n;
209 }
210 }
211
212 c2 = c % 10;
213
214 *text = (u_char) (c2 + '0');
215 n++;
216
217 return n;
80 u_char *p;
81
82 if (family == AF_INET) {
83
84 p = (u_char *) addr;
85
86 return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",
87 p[0], p[1], p[2], p[3])
88 - text;
89 }
90
91 return 0;
21892 }
21993
22094
575449 u->addrs[i].sockaddr = (struct sockaddr *) sin;
576450 u->addrs[i].socklen = sizeof(struct sockaddr_in);
577451
578 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
452 len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
579453
580454 p = ngx_pnalloc(pool, len);
581455 if (p == NULL) {
99
1010 #include <ngx_config.h>
1111 #include <ngx_core.h>
12
13
14 #define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1)
1215
1316
1417 typedef struct {
154154 len = NGX_INT64_LEN /* pid */
155155 + 1 + NGX_INT32_LEN /* connection */
156156 + 1 + 1 /* state */
157 + 1 + INET_ADDRSTRLEN
157 + 1 + NGX_INET_ADDRSTRLEN
158158 + 1 + (r->server_name ? cmcf->max_server_name_len : 1)
159159 + 2; /* "\r\n" */
160160
203203 *(b->last++) = ' ';
204204 b->last = ngx_cpymem(b->last, c[i].addr_text.data,
205205 c[i].addr_text.len);
206 for (n = c[i].addr_text.len; n < INET_ADDRSTRLEN; n++) {
206 for (n = c[i].addr_text.len; n < NGX_INET_ADDRSTRLEN; n++) {
207207 *(b->last++) = ' ';
208208 }
209209
16251625 }
16261626
16271627 s->len = ngx_inet_ntop(c->listening->family, &r->in_addr,
1628 s->data, INET_ADDRSTRLEN);
1628 s->data, NGX_INET_ADDRSTRLEN);
16291629
16301630 return NGX_OK;
16311631 }
29702970 ls->conf.rcvbuf = -1;
29712971 ls->conf.sndbuf = -1;
29722972
2973 n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, INET_ADDRSTRLEN + 6);
2973 n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, NGX_INET_ADDRSTRLEN);
29742974 ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port);
29752975
29762976 if (cf->args->nelts == 2) {
4646 ngx_uint_t deferred_accept;
4747 #endif
4848
49 u_char addr[INET_ADDRSTRLEN + 6];
49 u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")];
5050
5151 } ngx_http_listen_conf_t;
5252
161161 ngx_http_core_loc_conf_t *clcf;
162162 ngx_http_core_srv_conf_t *cscf;
163163 /* AF_INET only */
164 u_char addr[INET_ADDRSTRLEN];
164 u_char addr[NGX_INET_ADDRSTRLEN];
165165
166166 r->header_sent = 1;
167167
280280
281281 for (i = 0; i < ur->naddrs; i++) {
282282
283 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
283 len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
284284
285285 p = ngx_pnalloc(r->pool, len);
286286 if (p == NULL) {
287287 return NGX_ERROR;
288288 }
289289
290 len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, INET_ADDRSTRLEN);
290 len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
291291 len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
292292
293293 sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in));
871871 {
872872 ngx_str_t s;
873873
874 s.data = ngx_pnalloc(r->pool, INET_ADDRSTRLEN);
874 s.data = ngx_pnalloc(r->pool, NGX_INET_ADDRSTRLEN);
875875 if (s.data == NULL) {
876876 return NGX_ERROR;
877877 }
357357 imip->addrs[i].ctx = in_addr[i].ctx;
358358
359359 text = ngx_pnalloc(cf->pool,
360 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
360 NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
361361 if (text == NULL) {
362362 return NGX_CONF_ERROR;
363363 }
364364
365365 len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text,
366 INET_ADDRSTRLEN);
366 NGX_INET_ADDRSTRLEN);
367367
368368 len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text;
369369