0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
28
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_event_connect.h>
|
0
|
11
|
|
12
|
10
|
13 /*
|
|
14 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as
|
|
15 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])",
|
|
16 * however, they were implemented long before the ngx_sprintf() appeared
|
|
17 * and they are faster by 1.5-2.5 times, so it is worth to keep them.
|
|
18 *
|
|
19 * By the way, the implementation using ngx_sprintf() is faster by 2.5-3 times
|
28
|
20 * than using FreeBSD libc's snprintf().
|
10
|
21 */
|
|
22
|
|
23
|
16
|
24 static ngx_inline size_t ngx_sprint_uchar(u_char *text, u_char c, size_t len)
|
0
|
25 {
|
|
26 size_t n;
|
|
27 ngx_uint_t c1, c2;
|
|
28
|
|
29 n = 0;
|
|
30
|
|
31 if (len == n) {
|
|
32 return n;
|
|
33 }
|
|
34
|
|
35 c1 = c / 100;
|
|
36
|
|
37 if (c1) {
|
|
38 *text++ = (u_char) (c1 + '0');
|
|
39 n++;
|
|
40
|
|
41 if (len == n) {
|
|
42 return n;
|
|
43 }
|
|
44 }
|
|
45
|
|
46 c2 = (c % 100) / 10;
|
|
47
|
|
48 if (c1 || c2) {
|
|
49 *text++ = (u_char) (c2 + '0');
|
|
50 n++;
|
|
51
|
|
52 if (len == n) {
|
|
53 return n;
|
|
54 }
|
|
55 }
|
|
56
|
|
57 c2 = c % 10;
|
|
58
|
|
59 *text++ = (u_char) (c2 + '0');
|
|
60 n++;
|
|
61
|
|
62 return n;
|
|
63 }
|
|
64
|
|
65
|
|
66 /* AF_INET only */
|
|
67
|
28
|
68 size_t ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text,
|
0
|
69 size_t len)
|
|
70 {
|
|
71 u_char *p;
|
|
72 size_t n;
|
|
73 ngx_uint_t i;
|
28
|
74 struct sockaddr_in *sin;
|
0
|
75
|
|
76 if (len == 0) {
|
|
77 return 0;
|
|
78 }
|
|
79
|
|
80 if (family != AF_INET) {
|
|
81 return 0;
|
|
82 }
|
|
83
|
28
|
84 sin = (struct sockaddr_in *) sa;
|
|
85 p = (u_char *) &sin->sin_addr;
|
0
|
86
|
|
87 if (len > INET_ADDRSTRLEN) {
|
|
88 len = INET_ADDRSTRLEN;
|
|
89 }
|
|
90
|
|
91 n = ngx_sprint_uchar(text, p[0], len);
|
|
92
|
|
93 i = 1;
|
|
94
|
|
95 do {
|
|
96 if (len == n) {
|
|
97 text[n - 1] = '\0';
|
|
98 return n;
|
|
99 }
|
|
100
|
|
101 text[n++] = '.';
|
|
102
|
|
103 if (len == n) {
|
|
104 text[n - 1] = '\0';
|
|
105 return n;
|
|
106 }
|
|
107
|
|
108 n += ngx_sprint_uchar(&text[n], p[i++], len - n);
|
|
109
|
|
110 } while (i < 4);
|
|
111
|
|
112 if (len == n) {
|
|
113 text[n] = '\0';
|
|
114 return n;
|
|
115 }
|
|
116
|
|
117 text[n] = '\0';
|
|
118
|
|
119 return n;
|
|
120 }
|
|
121
|
|
122 size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
|
|
123 {
|
|
124 u_char *p;
|
|
125 size_t n;
|
|
126 ngx_uint_t i;
|
|
127
|
|
128 if (len == 0) {
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132 if (family != AF_INET) {
|
|
133 return 0;
|
|
134 }
|
|
135
|
|
136 p = (u_char *) addr;
|
|
137
|
|
138 if (len > INET_ADDRSTRLEN) {
|
|
139 len = INET_ADDRSTRLEN;
|
|
140 }
|
|
141
|
|
142 n = ngx_sprint_uchar(text, p[0], len);
|
|
143
|
|
144 i = 1;
|
|
145
|
|
146 do {
|
|
147 if (len == n) {
|
|
148 text[n - 1] = '\0';
|
|
149 return n;
|
|
150 }
|
|
151
|
|
152 text[n++] = '.';
|
|
153
|
|
154 if (len == n) {
|
|
155 text[n - 1] = '\0';
|
|
156 return n;
|
|
157 }
|
|
158
|
|
159 n += ngx_sprint_uchar(&text[n], p[i++], len - n);
|
|
160
|
|
161 } while (i < 4);
|
|
162
|
|
163 if (len == n) {
|
|
164 text[n] = '\0';
|
|
165 return n;
|
|
166 }
|
|
167
|
|
168 text[n] = '\0';
|
|
169
|
|
170 return n;
|
|
171 }
|
|
172
|
|
173
|
|
174 /* AF_INET only */
|
|
175
|
|
176 ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr)
|
|
177 {
|
|
178 ngx_int_t m;
|
|
179 ngx_uint_t i;
|
|
180 ngx_inet_cidr_t *in_cidr;
|
|
181
|
|
182 in_cidr = cidr;
|
|
183
|
|
184 for (i = 0; i < text->len; i++) {
|
|
185 if (text->data[i] == '/') {
|
|
186 break;
|
|
187 }
|
|
188 }
|
|
189
|
|
190 if (i == text->len) {
|
|
191 return NGX_ERROR;
|
|
192 }
|
|
193
|
|
194 text->data[i] = '\0';
|
|
195 in_cidr->addr = inet_addr((char *) text->data);
|
|
196 text->data[i] = '/';
|
|
197 if (in_cidr->addr == INADDR_NONE) {
|
|
198 return NGX_ERROR;
|
|
199 }
|
|
200
|
|
201 m = ngx_atoi(&text->data[i + 1], text->len - (i + 1));
|
|
202 if (m == NGX_ERROR) {
|
|
203 return NGX_ERROR;
|
|
204 }
|
|
205
|
|
206 if (m == 0) {
|
|
207
|
|
208 /* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
|
209
|
|
210 in_cidr->mask = 0;
|
|
211 return NGX_OK;
|
|
212 }
|
|
213
|
|
214 in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m))));
|
|
215
|
|
216 return NGX_OK;
|
|
217 }
|
|
218
|
|
219
|
28
|
220 ngx_peers_t *ngx_inet_upstream_parse(ngx_conf_t *cf, ngx_inet_upstream_t *u)
|
0
|
221 {
|
28
|
222 char *err;
|
|
223 u_char *host;
|
|
224 in_addr_t in_addr;
|
|
225 ngx_uint_t i, len;
|
|
226 ngx_peers_t *peers;
|
|
227 struct hostent *h;
|
|
228 struct sockaddr_in *sin;
|
|
229
|
|
230 err = ngx_inet_parse_host_port(u);
|
|
231
|
|
232 if (err) {
|
|
233 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
234 "%s in upstream \"%V\"", err, &u->name);
|
|
235 return NULL;
|
|
236 }
|
|
237
|
|
238 if (u->default_port) {
|
|
239 if (u->default_port_value == 0) {
|
|
240 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
241 "no port in upstream \"%V\"", &u->name);
|
|
242 return NULL;
|
|
243 }
|
|
244
|
|
245 u->port = u->default_port_value;
|
|
246
|
50
|
247 u->port_text.data = ngx_palloc(cf->pool, sizeof("65536") - 1);
|
|
248 if (u->port_text.data == NULL) {
|
28
|
249 return NULL;
|
|
250 }
|
|
251
|
|
252 u->port_text.len = ngx_sprintf(u->port_text.data, "%d",
|
|
253 u->default_port_value)
|
|
254 - u->port_text.data;
|
|
255
|
|
256 } else if (u->port) {
|
|
257 if (u->port == u->default_port_value) {
|
|
258 u->default_port = 1;
|
|
259 }
|
|
260
|
|
261 } else {
|
|
262 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
263 "no port in upstream \"%V\"", &u->name);
|
|
264 return NULL;
|
|
265 }
|
|
266
|
|
267 if (u->host.len == 0) {
|
|
268 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
269 "no host in upstream \"%V\"", &u->name);
|
|
270 return NULL;
|
|
271 }
|
|
272
|
|
273 u->port = htons(u->port);
|
|
274
|
50
|
275 host = ngx_palloc(cf->pool, u->host.len + 1);
|
|
276 if (host == NULL) {
|
28
|
277 return NULL;
|
|
278 }
|
|
279
|
|
280 ngx_cpystrn(host, u->host.data, u->host.len + 1);
|
|
281
|
|
282 /* AF_INET only */
|
|
283
|
|
284 in_addr = inet_addr((char *) host);
|
|
285
|
|
286 if (in_addr == INADDR_NONE) {
|
|
287 h = gethostbyname((char *) host);
|
|
288
|
|
289 if (h == NULL || h->h_addr_list[0] == NULL) {
|
|
290 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
291 "host %s is not found in upstream \"%V\"",
|
|
292 host, &u->name);
|
|
293 return NULL;
|
|
294 }
|
|
295
|
|
296 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
|
|
297
|
|
298 /* MP: ngx_shared_palloc() */
|
|
299
|
|
300 peers = ngx_pcalloc(cf->pool,
|
|
301 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (i - 1));
|
|
302 if (peers == NULL) {
|
|
303 return NULL;
|
|
304 }
|
|
305
|
|
306 peers->number = i;
|
|
307 peers->weight = 1;
|
|
308
|
|
309 for (i = 0; h->h_addr_list[i] != NULL; i++) {
|
|
310
|
50
|
311 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
|
|
312 if (sin == NULL) {
|
28
|
313 return NULL;
|
|
314 }
|
|
315
|
|
316 sin->sin_family = AF_INET;
|
|
317 sin->sin_port = u->port;
|
36
|
318 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
|
28
|
319
|
|
320 peers->peer[i].sockaddr = (struct sockaddr *) sin;
|
|
321 peers->peer[i].socklen = sizeof(struct sockaddr_in);
|
|
322
|
|
323 len = INET_ADDRSTRLEN - 1 + 1 + u->port_text.len;
|
|
324
|
50
|
325 peers->peer[i].name.data = ngx_palloc(cf->pool, len);
|
|
326 if (peers->peer[i].name.data == NULL) {
|
28
|
327 return NULL;
|
|
328 }
|
|
329
|
|
330 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin,
|
|
331 peers->peer[i].name.data, len);
|
|
332
|
|
333 peers->peer[i].name.data[len++] = ':';
|
0
|
334
|
28
|
335 ngx_memcpy(peers->peer[i].name.data + len,
|
|
336 u->port_text.data, u->port_text.len);
|
|
337
|
|
338 peers->peer[i].name.len = len + u->port_text.len;
|
|
339
|
|
340 peers->peer[i].uri_separator = "";
|
|
341
|
|
342 peers->peer[i].weight = 1;
|
|
343 peers->peer[i].max_fails = 1;
|
|
344 peers->peer[i].fail_timeout = 60;
|
|
345 }
|
|
346
|
|
347 } else {
|
|
348
|
|
349 /* MP: ngx_shared_palloc() */
|
|
350
|
50
|
351 peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t));
|
|
352 if (peers == NULL) {
|
28
|
353 return NULL;
|
|
354 }
|
|
355
|
50
|
356 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
|
|
357 if (sin == NULL) {
|
28
|
358 return NULL;
|
|
359 }
|
|
360
|
|
361 peers->number = 1;
|
|
362
|
|
363 sin->sin_family = AF_INET;
|
|
364 sin->sin_port = u->port;
|
|
365 sin->sin_addr.s_addr = in_addr;
|
|
366
|
|
367 peers->peer[0].sockaddr = (struct sockaddr *) sin;
|
|
368 peers->peer[0].socklen = sizeof(struct sockaddr_in);
|
|
369
|
|
370 len = u->host.len + 1 + u->port_text.len;
|
|
371
|
|
372 peers->peer[0].name.len = len;
|
|
373
|
50
|
374 peers->peer[0].name.data = ngx_palloc(cf->pool, len);
|
|
375 if (peers->peer[0].name.data == NULL) {
|
28
|
376 return NULL;
|
|
377 }
|
|
378
|
|
379 len = u->host.len;
|
|
380
|
|
381 ngx_memcpy(peers->peer[0].name.data, u->host.data, len);
|
|
382
|
|
383 peers->peer[0].name.data[len++] = ':';
|
|
384
|
|
385 ngx_memcpy(peers->peer[0].name.data + len,
|
|
386 u->port_text.data, u->port_text.len);
|
|
387
|
|
388 peers->peer[0].uri_separator = "";
|
|
389 }
|
|
390
|
|
391 return peers;
|
|
392 }
|
|
393
|
|
394
|
|
395 char *ngx_inet_parse_host_port(ngx_inet_upstream_t *u)
|
|
396 {
|
|
397 size_t i;
|
|
398 ngx_int_t port;
|
|
399 ngx_str_t *url;
|
|
400
|
|
401 url = &u->url;
|
|
402
|
|
403 if (u->port_only) {
|
|
404 i = 0;
|
|
405
|
|
406 } else {
|
|
407 if (url->data[0] == ':' || url->data[0] == '/') {
|
|
408 return "invalid host";
|
|
409 }
|
|
410
|
|
411 i = 1;
|
|
412 }
|
|
413
|
|
414 u->host.data = url->data;
|
|
415 u->host_header = *url;
|
|
416
|
|
417 for (/* void */; i < url->len; i++) {
|
|
418
|
|
419 if (url->data[i] == ':') {
|
|
420 u->port_text.data = &url->data[i] + 1;
|
|
421 u->host.len = i;
|
|
422
|
|
423 if (!u->uri_part) {
|
|
424 u->port_text.len = &url->data[url->len] - u->port_text.data;
|
|
425 break;
|
|
426 }
|
|
427 }
|
|
428
|
|
429 if (url->data[i] == '/') {
|
|
430 u->uri.data = &url->data[i];
|
|
431 u->uri.len = url->len - i;
|
|
432 u->host_header.len = i;
|
|
433
|
|
434 if (u->host.len == 0) {
|
|
435 u->host.len = i;
|
|
436 }
|
|
437
|
|
438 if (u->port_text.data == NULL) {
|
|
439 u->default_port = 1;
|
|
440 return NULL;
|
|
441 }
|
|
442
|
|
443 u->port_text.len = &url->data[i] - u->port_text.data;
|
|
444
|
|
445 if (u->port_text.len == 0) {
|
|
446 return "invalid port";
|
|
447 }
|
|
448
|
0
|
449 break;
|
|
450 }
|
|
451 }
|
|
452
|
28
|
453 if (u->port_text.data == NULL) {
|
|
454 port = ngx_atoi(url->data, url->len);
|
|
455
|
|
456 if (port == NGX_ERROR) {
|
|
457 u->default_port = 1;
|
|
458 u->host.len = url->len;
|
|
459
|
|
460 return NULL;
|
|
461 }
|
0
|
462
|
28
|
463 u->port_text = *url;
|
|
464 u->wildcard = 1;
|
0
|
465
|
28
|
466 } else {
|
|
467 if (u->port_text.len == 0) {
|
|
468 return "no URI";
|
|
469 }
|
|
470
|
|
471 port = ngx_atoi(u->port_text.data, u->port_text.len);
|
|
472
|
|
473 if (port == NGX_ERROR || port < 1 || port > 65536) {
|
|
474 return "invalid port";
|
|
475 }
|
0
|
476 }
|
|
477
|
28
|
478 u->port = (in_port_t) port;
|
0
|
479
|
28
|
480 return NULL;
|
0
|
481 }
|