Mercurial > hg > nginx-quic
annotate src/core/ngx_inet.c @ 1434:0f0ce9a8eee9
If-Modified-Since should be unique header line
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 29 Aug 2007 15:32:49 +0000 |
parents | b590a528fd41 |
children | fe11e2a3946d |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
5 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
425
diff
changeset
|
6 |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
100
7ebc8b7fb816
nginx-0.0.1-2003-06-03-19:42:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
57
diff
changeset
|
8 #include <ngx_core.h> |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 |
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
461 | 11 /* |
12 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as | |
581 | 13 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however, |
14 * they had been implemented long before the ngx_sprintf() had appeared | |
461 | 15 * and they are faster by 1.5-2.5 times, so it is worth to keep them. |
16 * | |
17 * By the way, the implementation using ngx_sprintf() is faster by 2.5-3 times | |
479 | 18 * than using FreeBSD libc's snprintf(). |
461 | 19 */ |
20 | |
21 | |
631 | 22 static ngx_inline size_t |
23 ngx_sprint_uchar(u_char *text, u_char c, size_t len) | |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
24 { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
25 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
26 ngx_uint_t c1, c2; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
27 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
28 n = 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
29 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
30 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
31 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
32 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
33 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
34 c1 = c / 100; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
35 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
36 if (c1) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
37 *text++ = (u_char) (c1 + '0'); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
38 n++; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
39 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
40 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
41 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
42 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
43 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
44 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
45 c2 = (c % 100) / 10; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
46 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
47 if (c1 || c2) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
48 *text++ = (u_char) (c2 + '0'); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
49 n++; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
50 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
51 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
52 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
53 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
54 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
55 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
56 c2 = c % 10; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
57 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
58 *text++ = (u_char) (c2 + '0'); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
59 n++; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
60 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
61 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
62 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
63 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
64 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
65 /* AF_INET only */ |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
66 |
509 | 67 size_t |
68 ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len) | |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
69 { |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
100
diff
changeset
|
70 u_char *p; |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
71 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
72 ngx_uint_t i; |
479 | 73 struct sockaddr_in *sin; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
74 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
75 if (len == 0) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
76 return 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
77 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
78 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
79 if (family != AF_INET) { |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
80 return 0; |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
81 } |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
82 |
479 | 83 sin = (struct sockaddr_in *) sa; |
84 p = (u_char *) &sin->sin_addr; | |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
85 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
86 if (len > INET_ADDRSTRLEN) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
87 len = INET_ADDRSTRLEN; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
88 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
89 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
90 n = ngx_sprint_uchar(text, p[0], len); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
91 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
92 i = 1; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
93 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
94 do { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
95 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
96 text[n - 1] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
97 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
98 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
99 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
100 text[n++] = '.'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
101 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
102 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
103 text[n - 1] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
104 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
105 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
106 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
107 n += ngx_sprint_uchar(&text[n], p[i++], len - n); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
108 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
109 } while (i < 4); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
110 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
111 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
112 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
113 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
114 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
115 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
116 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
117 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
118 return n; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
119 } |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
120 |
509 | 121 size_t |
122 ngx_inet_ntop(int family, void *addr, u_char *text, size_t len) | |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
123 { |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
124 u_char *p; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
125 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
126 ngx_uint_t i; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
127 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
128 if (len == 0) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
129 return 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
130 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
131 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
132 if (family != AF_INET) { |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
133 return 0; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
134 } |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
135 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
136 p = (u_char *) addr; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
137 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
138 if (len > INET_ADDRSTRLEN) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
139 len = INET_ADDRSTRLEN; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
140 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
141 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
142 n = ngx_sprint_uchar(text, p[0], len); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
143 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
144 i = 1; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
145 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
146 do { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
147 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
148 text[n - 1] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
149 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
150 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
151 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
152 text[n++] = '.'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
153 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
154 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
155 text[n - 1] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
156 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
157 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
158 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
159 n += ngx_sprint_uchar(&text[n], p[i++], len - n); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
160 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
161 } while (i < 4); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
162 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
163 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
164 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
165 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
166 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
167 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
168 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
169 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
170 return n; |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
171 } |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
172 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
173 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
174 /* AF_INET only */ |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
175 |
509 | 176 ngx_int_t |
177 ngx_ptocidr(ngx_str_t *text, void *cidr) | |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
178 { |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
179 ngx_int_t m; |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
180 ngx_uint_t i; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
181 ngx_inet_cidr_t *in_cidr; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
182 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
183 in_cidr = cidr; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
184 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
185 for (i = 0; i < text->len; i++) { |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
186 if (text->data[i] == '/') { |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
187 break; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
188 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
189 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
190 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
191 if (i == text->len) { |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
192 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
193 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
194 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
195 text->data[i] = '\0'; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
196 in_cidr->addr = inet_addr((char *) text->data); |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
197 text->data[i] = '/'; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
198 if (in_cidr->addr == INADDR_NONE) { |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
199 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
200 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
201 |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
202 m = ngx_atoi(&text->data[i + 1], text->len - (i + 1)); |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
203 if (m == NGX_ERROR) { |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
204 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
205 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
206 |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
207 if (m == 0) { |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
208 |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
209 /* the x86 compilers use the shl instruction that shifts by modulo 32 */ |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
210 |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
211 in_cidr->mask = 0; |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
212 return NGX_OK; |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
213 } |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
214 |
404
37b5381fb6d8
nginx-0.0.9-2004-07-30-21:05:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
368
diff
changeset
|
215 in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m)))); |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
216 |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
217 if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) { |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
218 return NGX_OK; |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
219 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
220 |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
221 in_cidr->addr &= in_cidr->mask; |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
222 |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1107
diff
changeset
|
223 return NGX_DONE; |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
224 } |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
225 |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
226 |
651 | 227 ngx_int_t |
228 ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u) | |
229 { | |
906 | 230 u_char *p, *host, *port_start; |
231 size_t len, port_len; | |
651 | 232 ngx_int_t port; |
233 ngx_uint_t i; | |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
234 struct hostent *h; |
651 | 235 #if (NGX_HAVE_UNIX_DOMAIN) |
236 struct sockaddr_un *saun; | |
237 #endif | |
238 | |
239 len = u->url.len; | |
240 p = u->url.data; | |
241 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
242 if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) { |
651 | 243 |
244 #if (NGX_HAVE_UNIX_DOMAIN) | |
245 | |
246 p += 5; | |
247 len -= 5; | |
248 | |
712
2452d89efea9
fix uri part in "proxy_pass http://unix:...", the bug appeared in nginx-0.3.47
Igor Sysoev <igor@sysoev.ru>
parents:
711
diff
changeset
|
249 u->uri.len = len; |
2452d89efea9
fix uri part in "proxy_pass http://unix:...", the bug appeared in nginx-0.3.47
Igor Sysoev <igor@sysoev.ru>
parents:
711
diff
changeset
|
250 u->uri.data = p; |
2452d89efea9
fix uri part in "proxy_pass http://unix:...", the bug appeared in nginx-0.3.47
Igor Sysoev <igor@sysoev.ru>
parents:
711
diff
changeset
|
251 |
651 | 252 if (u->uri_part) { |
253 for (i = 0; i < len; i++) { | |
254 | |
255 if (p[i] == ':') { | |
256 len = i; | |
257 | |
258 u->uri.len -= len + 1; | |
259 u->uri.data += len + 1; | |
260 | |
261 break; | |
262 } | |
263 } | |
264 } | |
265 | |
266 if (len == 0) { | |
267 u->err = "no path in the unix domain socket"; | |
268 return NGX_ERROR; | |
269 } | |
270 | |
271 if (len + 1 > sizeof(saun->sun_path)) { | |
272 u->err = "too long path in the unix domain socket"; | |
273 return NGX_ERROR; | |
274 } | |
275 | |
884 | 276 u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t)); |
277 if (u->addrs == NULL) { | |
651 | 278 return NGX_ERROR; |
279 } | |
280 | |
281 saun = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_un)); | |
282 if (saun == NULL) { | |
283 return NGX_ERROR; | |
284 } | |
285 | |
884 | 286 u->naddrs = 1; |
651 | 287 |
288 saun->sun_family = AF_UNIX; | |
289 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); | |
290 | |
884 | 291 u->addrs[0].sockaddr = (struct sockaddr *) saun; |
292 u->addrs[0].socklen = sizeof(struct sockaddr_un); | |
293 u->addrs[0].name.len = len + 5; | |
294 u->addrs[0].name.data = u->url.data; | |
651 | 295 |
892
b579676ec6b2
unix socket upstreams always used the first unix upstream
Igor Sysoev <igor@sysoev.ru>
parents:
884
diff
changeset
|
296 u->host.len = len; |
b579676ec6b2
unix socket upstreams always used the first unix upstream
Igor Sysoev <igor@sysoev.ru>
parents:
884
diff
changeset
|
297 u->host.data = p; |
b579676ec6b2
unix socket upstreams always used the first unix upstream
Igor Sysoev <igor@sysoev.ru>
parents:
884
diff
changeset
|
298 |
906 | 299 u->unix_socket = 1; |
651 | 300 |
301 return NGX_OK; | |
302 | |
303 #else | |
304 u->err = "the unix domain sockets are not supported on this platform"; | |
305 | |
306 return NGX_ERROR; | |
307 | |
308 #endif | |
309 } | |
310 | |
311 if ((p[0] == ':' || p[0] == '/') && !u->listen) { | |
312 u->err = "invalid host"; | |
313 return NGX_ERROR; | |
314 } | |
315 | |
316 u->host.data = p; | |
906 | 317 |
318 port_start = NULL; | |
319 port_len = 0; | |
651 | 320 |
321 for (i = 0; i < len; i++) { | |
322 | |
323 if (p[i] == ':') { | |
906 | 324 port_start = &p[i + 1]; |
651 | 325 u->host.len = i; |
326 | |
327 if (!u->uri_part) { | |
906 | 328 port_len = len - (i + 1); |
651 | 329 break; |
330 } | |
331 } | |
332 | |
333 if (p[i] == '/') { | |
334 u->uri.len = len - i; | |
335 u->uri.data = &p[i]; | |
336 | |
337 if (u->host.len == 0) { | |
338 u->host.len = i; | |
339 } | |
340 | |
906 | 341 if (port_start == NULL) { |
884 | 342 u->no_port = 1; |
343 goto no_port; | |
651 | 344 } |
345 | |
906 | 346 port_len = &p[i] - port_start; |
651 | 347 |
906 | 348 if (port_len == 0) { |
651 | 349 u->err = "invalid port"; |
350 return NGX_ERROR; | |
351 } | |
352 | |
353 break; | |
354 } | |
355 } | |
356 | |
906 | 357 if (port_start) { |
651 | 358 |
906 | 359 if (port_len == 0) { |
360 port_len = &p[i] - port_start; | |
651 | 361 |
906 | 362 if (port_len == 0) { |
651 | 363 u->err = "invalid port"; |
364 return NGX_ERROR; | |
365 } | |
366 } | |
367 | |
906 | 368 port = ngx_atoi(port_start, port_len); |
651 | 369 |
370 if (port == NGX_ERROR || port < 1 || port > 65536) { | |
371 u->err = "invalid port"; | |
372 return NGX_ERROR; | |
373 } | |
374 | |
375 } else { | |
376 port = ngx_atoi(p, len); | |
377 | |
378 if (port == NGX_ERROR) { | |
379 u->host.len = len; | |
884 | 380 u->no_port = 1; |
651 | 381 |
884 | 382 goto no_port; |
651 | 383 } |
384 | |
385 u->wildcard = 1; | |
386 } | |
387 | |
906 | 388 u->port = (in_port_t) port; |
651 | 389 |
884 | 390 no_port: |
651 | 391 |
392 if (u->listen) { | |
884 | 393 |
906 | 394 if (u->port == 0) { |
395 if (u->default_port == 0) { | |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
396 u->err = "no port"; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
397 return NGX_ERROR; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
398 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
399 |
906 | 400 u->port = u->default_port; |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
401 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
402 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
403 if (u->host.len == 1 && u->host.data[0] == '*') { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
404 u->host.len = 0; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
405 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
406 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
407 /* AF_INET only */ |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
408 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
409 if (u->host.len) { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
410 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
411 host = ngx_palloc(cf->temp_pool, u->host.len + 1); |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
412 if (host == NULL) { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
413 return NGX_ERROR; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
414 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
415 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
416 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
417 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
418 u->addr.in_addr = inet_addr((const char *) host); |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
419 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
420 if (u->addr.in_addr == INADDR_NONE) { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
421 h = gethostbyname((const char *) host); |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
422 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
423 if (h == NULL || h->h_addr_list[0] == NULL) { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
424 u->err = "host not found"; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
425 return NGX_ERROR; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
426 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
427 |
906 | 428 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]); |
804
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
429 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
430 |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
431 } else { |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
432 u->addr.in_addr = INADDR_ANY; |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
433 } |
472cd9768ac2
now the "listen" directives use ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
712
diff
changeset
|
434 |
651 | 435 return NGX_OK; |
436 } | |
437 | |
438 if (u->host.len == 0) { | |
439 u->err = "no host"; | |
440 return NGX_ERROR; | |
441 } | |
442 | |
884 | 443 if (u->no_resolve) { |
444 return NGX_OK; | |
651 | 445 } |
446 | |
906 | 447 if (u->no_port) { |
448 u->port = u->default_port; | |
449 } | |
450 | |
451 if (u->port == 0) { | |
452 u->err = "no port"; | |
453 return NGX_ERROR; | |
454 } | |
455 | |
884 | 456 if (ngx_inet_resolve_host(cf, u) != NGX_OK) { |
651 | 457 return NGX_ERROR; |
458 } | |
459 | |
460 return NGX_OK; | |
461 } | |
462 | |
463 | |
884 | 464 ngx_int_t |
465 ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u) | |
651 | 466 { |
906 | 467 u_char *p, *host; |
651 | 468 size_t len; |
469 in_addr_t in_addr; | |
470 ngx_uint_t i; | |
471 struct hostent *h; | |
472 struct sockaddr_in *sin; | |
473 | |
884 | 474 host = ngx_palloc(cf->temp_pool, u->host.len + 1); |
651 | 475 if (host == NULL) { |
884 | 476 return NGX_ERROR; |
651 | 477 } |
478 | |
884 | 479 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
651 | 480 |
481 /* AF_INET only */ | |
482 | |
483 in_addr = inet_addr((char *) host); | |
484 | |
485 if (in_addr == INADDR_NONE) { | |
486 h = gethostbyname((char *) host); | |
487 | |
488 if (h == NULL || h->h_addr_list[0] == NULL) { | |
884 | 489 u->err = "host not found"; |
490 return NGX_ERROR; | |
651 | 491 } |
492 | |
884 | 493 if (u->one_addr == 0) { |
494 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } | |
495 | |
496 } else { | |
497 i = 1; | |
498 } | |
651 | 499 |
500 /* MP: ngx_shared_palloc() */ | |
501 | |
884 | 502 u->addrs = ngx_pcalloc(cf->pool, i * sizeof(ngx_peer_addr_t)); |
503 if (u->addrs == NULL) { | |
504 return NGX_ERROR; | |
651 | 505 } |
506 | |
884 | 507 u->naddrs = i; |
651 | 508 |
509 for (i = 0; h->h_addr_list[i] != NULL; i++) { | |
510 | |
511 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); | |
512 if (sin == NULL) { | |
884 | 513 return NGX_ERROR; |
651 | 514 } |
515 | |
516 sin->sin_family = AF_INET; | |
906 | 517 sin->sin_port = htons(u->port); |
651 | 518 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); |
519 | |
884 | 520 u->addrs[i].sockaddr = (struct sockaddr *) sin; |
521 u->addrs[i].socklen = sizeof(struct sockaddr_in); | |
651 | 522 |
523 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; | |
524 | |
906 | 525 p = ngx_palloc(cf->pool, len); |
526 if (p == NULL) { | |
884 | 527 return NGX_ERROR; |
651 | 528 } |
529 | |
906 | 530 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, p, len); |
651 | 531 |
906 | 532 u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p; |
533 u->addrs[i].name.data = p; | |
651 | 534 } |
535 | |
536 } else { | |
537 | |
538 /* MP: ngx_shared_palloc() */ | |
539 | |
884 | 540 u->addrs = ngx_pcalloc(cf->pool, sizeof(ngx_peer_addr_t)); |
541 if (u->addrs == NULL) { | |
542 return NGX_ERROR; | |
651 | 543 } |
544 | |
545 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); | |
546 if (sin == NULL) { | |
884 | 547 return NGX_ERROR; |
651 | 548 } |
549 | |
884 | 550 u->naddrs = 1; |
651 | 551 |
552 sin->sin_family = AF_INET; | |
906 | 553 sin->sin_port = htons(u->port); |
651 | 554 sin->sin_addr.s_addr = in_addr; |
555 | |
884 | 556 u->addrs[0].sockaddr = (struct sockaddr *) sin; |
557 u->addrs[0].socklen = sizeof(struct sockaddr_in); | |
651 | 558 |
906 | 559 p = ngx_palloc(cf->pool, u->host.len + sizeof(":65536") - 1); |
560 if (p == NULL) { | |
884 | 561 return NGX_ERROR; |
651 | 562 } |
563 | |
906 | 564 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p; |
565 u->addrs[0].name.data = p; | |
651 | 566 } |
567 | |
884 | 568 return NGX_OK; |
651 | 569 } |