*) 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
13 years ago
115 | 115 | #define INADDR_NONE ((unsigned int) -1) |
116 | 116 | #endif |
117 | 117 | |
118 | #ifndef INET_ADDRSTRLEN /* Win32 */ | |
119 | #define INET_ADDRSTRLEN 16 | |
120 | #endif | |
121 | ||
122 | 118 | #ifdef MAXHOSTNAMELEN |
123 | 119 | #define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN |
124 | 120 | #else |
36 | 36 | |
37 | 37 | |
38 | 38 | ls->addr_text.data = ngx_pnalloc(cf->pool, |
39 | INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1); | |
39 | NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1); | |
40 | 40 | if (ls->addr_text.data == NULL) { |
41 | 41 | return NULL; |
42 | 42 | } |
43 | 43 | |
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); | |
45 | 46 | |
46 | 47 | ls->addr_text.len = ngx_sprintf(ls->addr_text.data + len, ":%d", port) |
47 | 48 | - ls->addr_text.data; |
52 | 53 | ls->sockaddr = (struct sockaddr *) sin; |
53 | 54 | ls->socklen = sizeof(struct sockaddr_in); |
54 | 55 | 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; | |
56 | 57 | |
57 | 58 | return ls; |
58 | 59 | } |
103 | 104 | continue; |
104 | 105 | } |
105 | 106 | |
106 | ls[i].addr_text_max_len = INET_ADDRSTRLEN; | |
107 | ls[i].addr_text_max_len = NGX_INET_ADDRSTRLEN; | |
107 | 108 | |
108 | 109 | ls[i].addr_text.data = ngx_pnalloc(cycle->pool, |
109 | INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1); | |
110 | NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1); | |
110 | 111 | if (ls[i].addr_text.data == NULL) { |
111 | 112 | return NGX_ERROR; |
112 | 113 | } |
113 | 114 | |
114 | 115 | ls[i].family = sin->sin_family; |
115 | 116 | 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); | |
117 | 118 | if (len == 0) { |
118 | 119 | return NGX_ERROR; |
119 | 120 | } |
5 | 5 | |
6 | 6 | #include <ngx_config.h> |
7 | 7 | #include <ngx_core.h> |
8 | ||
9 | ||
10 | static size_t ngx_sprint_uchar(u_char *text, u_char c, size_t len); | |
11 | 8 | |
12 | 9 | |
13 | 10 | /* AF_INET only */ |
55 | 52 | } |
56 | 53 | |
57 | 54 | |
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 | ||
68 | 55 | /* AF_INET only */ |
69 | 56 | |
70 | 57 | size_t |
71 | 58 | ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len) |
72 | 59 | { |
73 | 60 | u_char *p; |
74 | size_t n; | |
75 | ngx_uint_t i; | |
76 | 61 | struct sockaddr_in *sin; |
77 | 62 | |
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; | |
122 | 74 | } |
123 | 75 | |
124 | 76 | |
125 | 77 | size_t |
126 | 78 | ngx_inet_ntop(int family, void *addr, u_char *text, size_t len) |
127 | 79 | { |
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; | |
218 | 92 | } |
219 | 93 | |
220 | 94 | |
575 | 449 | u->addrs[i].sockaddr = (struct sockaddr *) sin; |
576 | 450 | u->addrs[i].socklen = sizeof(struct sockaddr_in); |
577 | 451 | |
578 | len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; | |
452 | len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1; | |
579 | 453 | |
580 | 454 | p = ngx_pnalloc(pool, len); |
581 | 455 | if (p == NULL) { |
9 | 9 | |
10 | 10 | #include <ngx_config.h> |
11 | 11 | #include <ngx_core.h> |
12 | ||
13 | ||
14 | #define NGX_INET_ADDRSTRLEN (sizeof("255.255.255.255") - 1) | |
12 | 15 | |
13 | 16 | |
14 | 17 | typedef struct { |
154 | 154 | len = NGX_INT64_LEN /* pid */ |
155 | 155 | + 1 + NGX_INT32_LEN /* connection */ |
156 | 156 | + 1 + 1 /* state */ |
157 | + 1 + INET_ADDRSTRLEN | |
157 | + 1 + NGX_INET_ADDRSTRLEN | |
158 | 158 | + 1 + (r->server_name ? cmcf->max_server_name_len : 1) |
159 | 159 | + 2; /* "\r\n" */ |
160 | 160 | |
203 | 203 | *(b->last++) = ' '; |
204 | 204 | b->last = ngx_cpymem(b->last, c[i].addr_text.data, |
205 | 205 | 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++) { | |
207 | 207 | *(b->last++) = ' '; |
208 | 208 | } |
209 | 209 |
1625 | 1625 | } |
1626 | 1626 | |
1627 | 1627 | s->len = ngx_inet_ntop(c->listening->family, &r->in_addr, |
1628 | s->data, INET_ADDRSTRLEN); | |
1628 | s->data, NGX_INET_ADDRSTRLEN); | |
1629 | 1629 | |
1630 | 1630 | return NGX_OK; |
1631 | 1631 | } |
2970 | 2970 | ls->conf.rcvbuf = -1; |
2971 | 2971 | ls->conf.sndbuf = -1; |
2972 | 2972 | |
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); | |
2974 | 2974 | ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port); |
2975 | 2975 | |
2976 | 2976 | if (cf->args->nelts == 2) { |
46 | 46 | ngx_uint_t deferred_accept; |
47 | 47 | #endif |
48 | 48 | |
49 | u_char addr[INET_ADDRSTRLEN + 6]; | |
49 | u_char addr[NGX_INET_ADDRSTRLEN + sizeof(":65535")]; | |
50 | 50 | |
51 | 51 | } ngx_http_listen_conf_t; |
52 | 52 |
161 | 161 | ngx_http_core_loc_conf_t *clcf; |
162 | 162 | ngx_http_core_srv_conf_t *cscf; |
163 | 163 | /* AF_INET only */ |
164 | u_char addr[INET_ADDRSTRLEN]; | |
164 | u_char addr[NGX_INET_ADDRSTRLEN]; | |
165 | 165 | |
166 | 166 | r->header_sent = 1; |
167 | 167 |
280 | 280 | |
281 | 281 | for (i = 0; i < ur->naddrs; i++) { |
282 | 282 | |
283 | len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; | |
283 | len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1; | |
284 | 284 | |
285 | 285 | p = ngx_pnalloc(r->pool, len); |
286 | 286 | if (p == NULL) { |
287 | 287 | return NGX_ERROR; |
288 | 288 | } |
289 | 289 | |
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); | |
291 | 291 | len = ngx_sprintf(&p[len], ":%d", ur->port) - p; |
292 | 292 | |
293 | 293 | sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in)); |
871 | 871 | { |
872 | 872 | ngx_str_t s; |
873 | 873 | |
874 | s.data = ngx_pnalloc(r->pool, INET_ADDRSTRLEN); | |
874 | s.data = ngx_pnalloc(r->pool, NGX_INET_ADDRSTRLEN); | |
875 | 875 | if (s.data == NULL) { |
876 | 876 | return NGX_ERROR; |
877 | 877 | } |
357 | 357 | imip->addrs[i].ctx = in_addr[i].ctx; |
358 | 358 | |
359 | 359 | text = ngx_pnalloc(cf->pool, |
360 | INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1); | |
360 | NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1); | |
361 | 361 | if (text == NULL) { |
362 | 362 | return NGX_CONF_ERROR; |
363 | 363 | } |
364 | 364 | |
365 | 365 | len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text, |
366 | INET_ADDRSTRLEN); | |
366 | NGX_INET_ADDRSTRLEN); | |
367 | 367 | |
368 | 368 | len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text; |
369 | 369 |