Mercurial > hg > nginx-quic
annotate src/core/ngx_inet.c @ 750:e3df50b4a4fd
fix hash building on unknown 64-bit platform
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 06 Oct 2006 15:06:02 +0000 |
parents | 2452d89efea9 |
children | 472cd9768ac2 |
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> |
479 | 9 #include <ngx_event.h> |
10 #include <ngx_event_connect.h> | |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
461 | 13 /* |
14 * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as | |
581 | 15 * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however, |
16 * they had been implemented long before the ngx_sprintf() had appeared | |
461 | 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 | |
479 | 20 * than using FreeBSD libc's snprintf(). |
461 | 21 */ |
22 | |
23 | |
631 | 24 static ngx_inline size_t |
25 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
|
26 { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
27 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
28 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
|
29 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
30 n = 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
31 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
32 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
33 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
34 } |
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 c1 = c / 100; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
37 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
38 if (c1) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
39 *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
|
40 n++; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
41 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
42 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
43 return n; |
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 } |
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 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
|
48 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
49 if (c1 || c2) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
50 *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
|
51 n++; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
52 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
53 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
54 return n; |
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 } |
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 c2 = c % 10; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
59 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
60 *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
|
61 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 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
64 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
65 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
66 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
67 /* AF_INET only */ |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
68 |
509 | 69 size_t |
70 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
|
71 { |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
100
diff
changeset
|
72 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
|
73 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
74 ngx_uint_t i; |
479 | 75 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
|
76 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
77 if (len == 0) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
78 return 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
79 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
80 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
81 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
|
82 return 0; |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
83 } |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
84 |
479 | 85 sin = (struct sockaddr_in *) sa; |
86 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
|
87 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
88 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
|
89 len = INET_ADDRSTRLEN; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
90 } |
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 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
|
93 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
94 i = 1; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
95 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
96 do { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
97 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
98 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
|
99 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
100 } |
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 text[n++] = '.'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
103 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
104 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
105 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
|
106 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
107 } |
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 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
|
110 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
111 } while (i < 4); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
112 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
113 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
114 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
115 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
116 } |
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 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
119 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
120 return n; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
121 } |
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
122 |
509 | 123 size_t |
124 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
|
125 { |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
126 u_char *p; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
127 size_t n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
128 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
|
129 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
130 if (len == 0) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
131 return 0; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
132 } |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
133 |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
134 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
|
135 return 0; |
57
a499e0d1f16e
nginx-0.0.1-2003-01-30-10:28:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
26
diff
changeset
|
136 } |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
137 |
291
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
138 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
|
139 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
140 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
|
141 len = INET_ADDRSTRLEN; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
142 } |
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 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
|
145 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
146 i = 1; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
147 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
148 do { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
149 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
150 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
|
151 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
152 } |
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 text[n++] = '.'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
155 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
156 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
157 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
|
158 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
159 } |
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 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
|
162 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
163 } while (i < 4); |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
164 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
165 if (len == n) { |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
166 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
167 return n; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
168 } |
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 text[n] = '\0'; |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
171 |
117ccc7c4055
nginx-0.0.3-2004-03-16-16:35:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
172 return n; |
26
53cb81681040
nginx-0.0.1-2002-12-15-09:25:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
173 } |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
174 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
175 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
176 /* AF_INET only */ |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
177 |
509 | 178 ngx_int_t |
179 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
|
180 { |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
185 in_cidr = cidr; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
186 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
187 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
|
188 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
|
189 break; |
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 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
192 |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
193 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
|
194 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
195 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
196 |
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] = '\0'; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
198 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
|
199 text->data[i] = '/'; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
200 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
|
201 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
202 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
203 |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
204 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
|
205 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
|
206 return NGX_ERROR; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
207 } |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
208 |
368
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
209 if (m == 0) { |
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 /* 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
|
212 |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
213 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
|
214 return NGX_OK; |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
215 } |
15c84a40e87d
nginx-0.0.7-2004-06-24-20:07:04 import
Igor Sysoev <igor@sysoev.ru>
parents:
340
diff
changeset
|
216 |
404
37b5381fb6d8
nginx-0.0.9-2004-07-30-21:05:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
368
diff
changeset
|
217 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
|
218 |
340
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
219 return NGX_OK; |
0bf903191ceb
nginx-0.0.3-2004-05-25-19:28:46 import
Igor Sysoev <igor@sysoev.ru>
parents:
291
diff
changeset
|
220 } |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
221 |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
222 |
651 | 223 ngx_int_t |
224 ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u) | |
225 { | |
226 u_char *p; | |
227 size_t len; | |
228 ngx_int_t port; | |
229 ngx_uint_t i; | |
230 #if (NGX_HAVE_UNIX_DOMAIN) | |
231 struct sockaddr_un *saun; | |
232 #endif | |
233 | |
234 len = u->url.len; | |
235 p = u->url.data; | |
236 | |
237 if (ngx_strncasecmp(p, "unix:", 5) == 0) { | |
238 | |
239 #if (NGX_HAVE_UNIX_DOMAIN) | |
240 | |
241 u->type = NGX_PARSE_URL_UNIX; | |
242 | |
243 p += 5; | |
244 len -= 5; | |
245 | |
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
|
246 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
|
247 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
|
248 |
651 | 249 if (u->uri_part) { |
250 for (i = 0; i < len; i++) { | |
251 | |
252 if (p[i] == ':') { | |
253 len = i; | |
254 | |
255 u->uri.len -= len + 1; | |
256 u->uri.data += len + 1; | |
257 | |
258 break; | |
259 } | |
260 } | |
261 } | |
262 | |
263 if (len == 0) { | |
264 u->err = "no path in the unix domain socket"; | |
265 return NGX_ERROR; | |
266 } | |
267 | |
268 if (len + 1 > sizeof(saun->sun_path)) { | |
269 u->err = "too long path in the unix domain socket"; | |
270 return NGX_ERROR; | |
271 } | |
272 | |
273 u->peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)); | |
274 if (u->peers == NULL) { | |
275 return NGX_ERROR; | |
276 } | |
277 | |
278 saun = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_un)); | |
279 if (saun == NULL) { | |
280 return NGX_ERROR; | |
281 } | |
282 | |
283 u->peers->number = 1; | |
284 | |
285 saun->sun_family = AF_UNIX; | |
286 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1); | |
287 | |
288 u->peers->peer[0].sockaddr = (struct sockaddr *) saun; | |
289 u->peers->peer[0].socklen = sizeof(struct sockaddr_un); | |
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
|
290 u->peers->peer[0].name.len = len + 5; |
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
|
291 u->peers->peer[0].name.data = u->url.data; |
651 | 292 u->peers->peer[0].uri_separator = ":"; |
293 | |
294 u->host_header.len = sizeof("localhost") - 1; | |
295 u->host_header.data = (u_char *) "localhost"; | |
296 | |
297 return NGX_OK; | |
298 | |
299 #else | |
300 u->err = "the unix domain sockets are not supported on this platform"; | |
301 | |
302 return NGX_ERROR; | |
303 | |
304 #endif | |
305 } | |
306 | |
307 if ((p[0] == ':' || p[0] == '/') && !u->listen) { | |
308 u->err = "invalid host"; | |
309 return NGX_ERROR; | |
310 } | |
311 | |
312 u->type = NGX_PARSE_URL_INET; | |
313 | |
314 u->host.data = p; | |
315 u->host_header.len = len; | |
316 u->host_header.data = p; | |
317 | |
318 for (i = 0; i < len; i++) { | |
319 | |
320 if (p[i] == ':') { | |
321 u->port.data = &p[i + 1]; | |
322 u->host.len = i; | |
323 | |
324 if (!u->uri_part) { | |
325 u->port.len = &p[len] - u->port.data; | |
326 break; | |
327 } | |
328 } | |
329 | |
330 if (p[i] == '/') { | |
331 u->uri.len = len - i; | |
332 u->uri.data = &p[i]; | |
333 u->host_header.len = i; | |
334 | |
335 if (u->host.len == 0) { | |
336 u->host.len = i; | |
337 } | |
338 | |
339 if (u->port.data == NULL) { | |
340 u->default_port = 1; | |
341 goto port; | |
342 } | |
343 | |
344 u->port.len = &p[i] - u->port.data; | |
345 | |
346 if (u->port.len == 0) { | |
347 u->err = "invalid port"; | |
348 return NGX_ERROR; | |
349 } | |
350 | |
351 break; | |
352 } | |
353 } | |
354 | |
355 if (u->port.data) { | |
356 | |
357 if (u->port.len == 0) { | |
358 u->port.len = &p[i] - u->port.data; | |
359 | |
360 if (u->port.len == 0) { | |
361 u->err = "invalid port"; | |
362 return NGX_ERROR; | |
363 } | |
364 } | |
365 | |
366 port = ngx_atoi(u->port.data, u->port.len); | |
367 | |
368 if (port == NGX_ERROR || port < 1 || port > 65536) { | |
369 u->err = "invalid port"; | |
370 return NGX_ERROR; | |
371 } | |
372 | |
373 } else { | |
374 port = ngx_atoi(p, len); | |
375 | |
376 if (port == NGX_ERROR) { | |
377 u->default_port = 1; | |
378 u->host.len = len; | |
379 | |
380 goto port; | |
381 } | |
382 | |
383 u->port.len = len; | |
384 u->port.data = p; | |
385 u->wildcard = 1; | |
386 } | |
387 | |
388 u->portn = (in_port_t) port; | |
389 | |
390 port: | |
391 | |
392 if (u->listen) { | |
393 return NGX_OK; | |
394 } | |
395 | |
396 if (u->default_port) { | |
397 | |
398 if (u->upstream) { | |
399 return NGX_OK; | |
400 } | |
401 | |
402 if (u->default_portn == 0) { | |
403 u->err = "no port"; | |
404 return NGX_ERROR; | |
405 } | |
406 | |
407 u->portn = u->default_portn; | |
408 | |
409 u->port.data = ngx_palloc(cf->pool, sizeof("65536") - 1); | |
410 if (u->port.data == NULL) { | |
411 return NGX_ERROR; | |
412 } | |
413 | |
414 u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data; | |
415 | |
416 } else if (u->portn) { | |
417 if (u->portn == u->default_portn) { | |
418 u->default_port = 1; | |
419 } | |
420 | |
421 } else { | |
422 u->err = "no port"; | |
423 return NGX_ERROR; | |
424 } | |
425 | |
426 if (u->host.len == 0) { | |
427 u->err = "no host"; | |
428 return NGX_ERROR; | |
429 } | |
430 | |
431 u->peers = ngx_inet_resolve_peer(cf, &u->host, u->portn); | |
432 | |
433 if (u->peers == NULL) { | |
434 return NGX_ERROR; | |
435 } | |
436 | |
437 if (u->peers == NGX_CONF_ERROR) { | |
438 u->err = "host not found"; | |
439 return NGX_ERROR; | |
440 } | |
441 | |
442 return NGX_OK; | |
443 } | |
444 | |
445 | |
446 ngx_peers_t * | |
447 ngx_inet_resolve_peer(ngx_conf_t *cf, ngx_str_t *name, in_port_t port) | |
448 { | |
449 u_char *host; | |
450 size_t len; | |
451 in_addr_t in_addr; | |
452 ngx_uint_t i; | |
453 ngx_peers_t *peers; | |
454 struct hostent *h; | |
455 struct sockaddr_in *sin; | |
456 | |
457 host = ngx_palloc(cf->temp_pool, name->len + 1); | |
458 if (host == NULL) { | |
459 return NULL; | |
460 } | |
461 | |
462 (void) ngx_cpystrn(host, name->data, name->len + 1); | |
463 | |
464 /* AF_INET only */ | |
465 | |
466 in_addr = inet_addr((char *) host); | |
467 | |
468 if (in_addr == INADDR_NONE) { | |
469 h = gethostbyname((char *) host); | |
470 | |
471 if (h == NULL || h->h_addr_list[0] == NULL) { | |
472 return NGX_CONF_ERROR; | |
473 } | |
474 | |
475 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } | |
476 | |
477 /* MP: ngx_shared_palloc() */ | |
478 | |
479 peers = ngx_pcalloc(cf->pool, | |
480 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (i - 1)); | |
481 if (peers == NULL) { | |
482 return NULL; | |
483 } | |
484 | |
485 peers->number = i; | |
486 | |
487 for (i = 0; h->h_addr_list[i] != NULL; i++) { | |
488 | |
489 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); | |
490 if (sin == NULL) { | |
491 return NULL; | |
492 } | |
493 | |
494 sin->sin_family = AF_INET; | |
495 sin->sin_port = htons(port); | |
496 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); | |
497 | |
498 peers->peer[i].sockaddr = (struct sockaddr *) sin; | |
499 peers->peer[i].socklen = sizeof(struct sockaddr_in); | |
500 | |
501 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; | |
502 | |
503 peers->peer[i].name.data = ngx_palloc(cf->pool, len); | |
504 if (peers->peer[i].name.data == NULL) { | |
505 return NULL; | |
506 } | |
507 | |
508 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, | |
509 peers->peer[i].name.data, len); | |
510 | |
511 peers->peer[i].name.len = | |
512 ngx_sprintf(&peers->peer[i].name.data[len], | |
513 ":%d", port) | |
514 - peers->peer[i].name.data; | |
515 | |
516 peers->peer[i].uri_separator = ""; | |
663 | 517 |
518 peers->peer[i].weight = NGX_CONF_UNSET_UINT; | |
519 peers->peer[i].max_fails = NGX_CONF_UNSET_UINT; | |
520 peers->peer[i].fail_timeout = NGX_CONF_UNSET; | |
651 | 521 } |
522 | |
523 } else { | |
524 | |
525 /* MP: ngx_shared_palloc() */ | |
526 | |
527 peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)); | |
528 if (peers == NULL) { | |
529 return NULL; | |
530 } | |
531 | |
532 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); | |
533 if (sin == NULL) { | |
534 return NULL; | |
535 } | |
536 | |
537 peers->number = 1; | |
538 | |
539 sin->sin_family = AF_INET; | |
540 sin->sin_port = htons(port); | |
541 sin->sin_addr.s_addr = in_addr; | |
542 | |
543 peers->peer[0].sockaddr = (struct sockaddr *) sin; | |
544 peers->peer[0].socklen = sizeof(struct sockaddr_in); | |
545 | |
546 peers->peer[0].name.data = ngx_palloc(cf->pool, | |
547 name->len + sizeof(":65536") - 1); | |
548 if (peers->peer[0].name.data == NULL) { | |
549 return NULL; | |
550 } | |
551 | |
552 peers->peer[0].name.len = ngx_sprintf(peers->peer[0].name.data, "%V:%d", | |
553 name, port) | |
554 - peers->peer[0].name.data; | |
555 | |
556 peers->peer[0].uri_separator = ""; | |
557 } | |
558 | |
559 return peers; | |
560 } | |
561 | |
562 | |
509 | 563 ngx_peers_t * |
564 ngx_inet_upstream_parse(ngx_conf_t *cf, ngx_inet_upstream_t *u) | |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
565 { |
479 | 566 char *err; |
567 u_char *host; | |
517 | 568 size_t len; |
479 | 569 in_addr_t in_addr; |
517 | 570 ngx_uint_t i; |
479 | 571 ngx_peers_t *peers; |
572 struct hostent *h; | |
573 struct sockaddr_in *sin; | |
574 | |
575 err = ngx_inet_parse_host_port(u); | |
576 | |
577 if (err) { | |
578 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
579 "%s in upstream \"%V\"", err, &u->name); | |
580 return NULL; | |
581 } | |
582 | |
583 if (u->default_port) { | |
651 | 584 |
479 | 585 if (u->default_port_value == 0) { |
586 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
587 "no port in upstream \"%V\"", &u->name); | |
588 return NULL; | |
589 } | |
590 | |
591 u->port = u->default_port_value; | |
592 | |
501 | 593 u->port_text.data = ngx_palloc(cf->pool, sizeof("65536") - 1); |
594 if (u->port_text.data == NULL) { | |
479 | 595 return NULL; |
596 } | |
597 | |
598 u->port_text.len = ngx_sprintf(u->port_text.data, "%d", | |
599 u->default_port_value) | |
600 - u->port_text.data; | |
601 | |
602 } else if (u->port) { | |
603 if (u->port == u->default_port_value) { | |
604 u->default_port = 1; | |
605 } | |
606 | |
607 } else { | |
608 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
609 "no port in upstream \"%V\"", &u->name); | |
610 return NULL; | |
611 } | |
612 | |
613 if (u->host.len == 0) { | |
614 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
615 "no host in upstream \"%V\"", &u->name); | |
616 return NULL; | |
617 } | |
618 | |
619 u->port = htons(u->port); | |
620 | |
651 | 621 host = ngx_palloc(cf->temp_pool, u->host.len + 1); |
501 | 622 if (host == NULL) { |
479 | 623 return NULL; |
624 } | |
625 | |
517 | 626 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1); |
479 | 627 |
628 /* AF_INET only */ | |
629 | |
630 in_addr = inet_addr((char *) host); | |
631 | |
632 if (in_addr == INADDR_NONE) { | |
633 h = gethostbyname((char *) host); | |
634 | |
635 if (h == NULL || h->h_addr_list[0] == NULL) { | |
636 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
637 "host %s is not found in upstream \"%V\"", | |
638 host, &u->name); | |
639 return NULL; | |
640 } | |
641 | |
642 for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ } | |
643 | |
644 /* MP: ngx_shared_palloc() */ | |
645 | |
646 peers = ngx_pcalloc(cf->pool, | |
647 sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (i - 1)); | |
648 if (peers == NULL) { | |
649 return NULL; | |
650 } | |
651 | |
652 peers->number = i; | |
653 | |
654 for (i = 0; h->h_addr_list[i] != NULL; i++) { | |
655 | |
501 | 656 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); |
657 if (sin == NULL) { | |
479 | 658 return NULL; |
659 } | |
660 | |
661 sin->sin_family = AF_INET; | |
662 sin->sin_port = u->port; | |
487 | 663 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]); |
479 | 664 |
665 peers->peer[i].sockaddr = (struct sockaddr *) sin; | |
666 peers->peer[i].socklen = sizeof(struct sockaddr_in); | |
667 | |
668 len = INET_ADDRSTRLEN - 1 + 1 + u->port_text.len; | |
577 | 669 |
501 | 670 peers->peer[i].name.data = ngx_palloc(cf->pool, len); |
671 if (peers->peer[i].name.data == NULL) { | |
479 | 672 return NULL; |
673 } | |
674 | |
675 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, | |
676 peers->peer[i].name.data, len); | |
677 | |
678 peers->peer[i].name.data[len++] = ':'; | |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
679 |
479 | 680 ngx_memcpy(peers->peer[i].name.data + len, |
681 u->port_text.data, u->port_text.len); | |
682 | |
683 peers->peer[i].name.len = len + u->port_text.len; | |
684 | |
685 peers->peer[i].uri_separator = ""; | |
663 | 686 |
687 peers->peer[i].weight = NGX_CONF_UNSET_UINT; | |
688 peers->peer[i].max_fails = NGX_CONF_UNSET_UINT; | |
689 peers->peer[i].fail_timeout = NGX_CONF_UNSET; | |
479 | 690 } |
691 | |
692 } else { | |
693 | |
694 /* MP: ngx_shared_palloc() */ | |
695 | |
501 | 696 peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t)); |
697 if (peers == NULL) { | |
479 | 698 return NULL; |
699 } | |
700 | |
501 | 701 sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)); |
702 if (sin == NULL) { | |
479 | 703 return NULL; |
704 } | |
705 | |
706 peers->number = 1; | |
707 | |
708 sin->sin_family = AF_INET; | |
709 sin->sin_port = u->port; | |
710 sin->sin_addr.s_addr = in_addr; | |
711 | |
712 peers->peer[0].sockaddr = (struct sockaddr *) sin; | |
713 peers->peer[0].socklen = sizeof(struct sockaddr_in); | |
714 | |
715 len = u->host.len + 1 + u->port_text.len; | |
716 | |
717 peers->peer[0].name.len = len; | |
718 | |
501 | 719 peers->peer[0].name.data = ngx_palloc(cf->pool, len); |
720 if (peers->peer[0].name.data == NULL) { | |
479 | 721 return NULL; |
722 } | |
723 | |
724 len = u->host.len; | |
725 | |
726 ngx_memcpy(peers->peer[0].name.data, u->host.data, len); | |
727 | |
728 peers->peer[0].name.data[len++] = ':'; | |
729 | |
730 ngx_memcpy(peers->peer[0].name.data + len, | |
731 u->port_text.data, u->port_text.len); | |
732 | |
733 peers->peer[0].uri_separator = ""; | |
734 } | |
735 | |
736 return peers; | |
737 } | |
738 | |
739 | |
509 | 740 char * |
741 ngx_inet_parse_host_port(ngx_inet_upstream_t *u) | |
479 | 742 { |
743 size_t i; | |
744 ngx_int_t port; | |
745 ngx_str_t *url; | |
746 | |
747 url = &u->url; | |
748 | |
749 if (u->port_only) { | |
750 i = 0; | |
751 | |
752 } else { | |
753 if (url->data[0] == ':' || url->data[0] == '/') { | |
754 return "invalid host"; | |
755 } | |
756 | |
757 i = 1; | |
758 } | |
759 | |
760 u->host.data = url->data; | |
761 u->host_header = *url; | |
762 | |
515 | 763 for ( /* void */ ; i < url->len; i++) { |
479 | 764 |
765 if (url->data[i] == ':') { | |
766 u->port_text.data = &url->data[i] + 1; | |
767 u->host.len = i; | |
768 | |
769 if (!u->uri_part) { | |
770 u->port_text.len = &url->data[url->len] - u->port_text.data; | |
771 break; | |
772 } | |
773 } | |
774 | |
775 if (url->data[i] == '/') { | |
776 u->uri.data = &url->data[i]; | |
777 u->uri.len = url->len - i; | |
778 u->host_header.len = i; | |
779 | |
780 if (u->host.len == 0) { | |
781 u->host.len = i; | |
782 } | |
783 | |
784 if (u->port_text.data == NULL) { | |
785 u->default_port = 1; | |
786 return NULL; | |
787 } | |
788 | |
789 u->port_text.len = &url->data[i] - u->port_text.data; | |
790 | |
791 if (u->port_text.len == 0) { | |
792 return "invalid port"; | |
793 } | |
794 | |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
795 break; |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
796 } |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
797 } |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
798 |
569 | 799 if (u->port_text.data) { |
800 | |
801 if (u->port_text.len == 0) { | |
802 u->port_text.len = &url->data[i] - u->port_text.data; | |
803 | |
804 if (u->port_text.len == 0) { | |
805 return "invalid port"; | |
806 } | |
807 } | |
808 | |
809 port = ngx_atoi(u->port_text.data, u->port_text.len); | |
810 | |
811 if (port == NGX_ERROR || port < 1 || port > 65536) { | |
812 return "invalid port"; | |
813 } | |
814 | |
815 } else { | |
479 | 816 port = ngx_atoi(url->data, url->len); |
817 | |
818 if (port == NGX_ERROR) { | |
819 u->default_port = 1; | |
820 u->host.len = url->len; | |
821 | |
822 return NULL; | |
823 } | |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
824 |
479 | 825 u->port_text = *url; |
826 u->wildcard = 1; | |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
827 } |
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
828 |
479 | 829 u->port = (in_port_t) port; |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
830 |
479 | 831 return NULL; |
425
bd39260a1383
nginx-0.0.10-2004-09-14-19:55:24 import
Igor Sysoev <igor@sysoev.ru>
parents:
404
diff
changeset
|
832 } |