Mercurial > hg > nginx
annotate src/stream/ngx_stream_handler.c @ 6692:56fc55e32f23
Stream: filters.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Thu, 15 Sep 2016 14:55:46 +0300 |
parents | 9cac11efb205 |
children | 3908156a51fa |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Roman Arutyunyan | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_event.h> | |
11 #include <ngx_stream.h> | |
12 | |
13 | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
14 static void ngx_stream_close_connection(ngx_connection_t *c); |
6115 | 15 static u_char *ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len); |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
16 static void ngx_stream_proxy_protocol_handler(ngx_event_t *rev); |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
17 static void ngx_stream_init_session_handler(ngx_event_t *rev); |
6115 | 18 |
19 #if (NGX_STREAM_SSL) | |
20 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); | |
21 static void ngx_stream_ssl_handshake_handler(ngx_connection_t *c); | |
22 #endif | |
23 | |
24 | |
25 void | |
26 ngx_stream_init_connection(ngx_connection_t *c) | |
27 { | |
6175 | 28 u_char text[NGX_SOCKADDR_STRLEN]; |
29 size_t len; | |
30 ngx_uint_t i; | |
6669
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
31 ngx_time_t *tp; |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
32 ngx_event_t *rev; |
6175 | 33 struct sockaddr *sa; |
34 ngx_stream_port_t *port; | |
35 struct sockaddr_in *sin; | |
36 ngx_stream_in_addr_t *addr; | |
37 ngx_stream_session_t *s; | |
38 ngx_stream_addr_conf_t *addr_conf; | |
6115 | 39 #if (NGX_HAVE_INET6) |
6175 | 40 struct sockaddr_in6 *sin6; |
41 ngx_stream_in6_addr_t *addr6; | |
6115 | 42 #endif |
6175 | 43 ngx_stream_core_srv_conf_t *cscf; |
44 ngx_stream_core_main_conf_t *cmcf; | |
6115 | 45 |
46 /* find the server configuration for the address:port */ | |
47 | |
48 port = c->listening->servers; | |
49 | |
50 if (port->naddrs > 1) { | |
51 | |
52 /* | |
53 * There are several addresses on this port and one of them | |
54 * is the "*:port" wildcard so getsockname() is needed to determine | |
55 * the server address. | |
56 * | |
6436 | 57 * AcceptEx() and recvmsg() already gave this address. |
6115 | 58 */ |
59 | |
60 if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) { | |
61 ngx_stream_close_connection(c); | |
62 return; | |
63 } | |
64 | |
65 sa = c->local_sockaddr; | |
66 | |
67 switch (sa->sa_family) { | |
68 | |
69 #if (NGX_HAVE_INET6) | |
70 case AF_INET6: | |
71 sin6 = (struct sockaddr_in6 *) sa; | |
72 | |
73 addr6 = port->addrs; | |
74 | |
75 /* the last address is "*" */ | |
76 | |
77 for (i = 0; i < port->naddrs - 1; i++) { | |
78 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) { | |
79 break; | |
80 } | |
81 } | |
82 | |
83 addr_conf = &addr6[i].conf; | |
84 | |
85 break; | |
86 #endif | |
87 | |
88 default: /* AF_INET */ | |
89 sin = (struct sockaddr_in *) sa; | |
90 | |
91 addr = port->addrs; | |
92 | |
93 /* the last address is "*" */ | |
94 | |
95 for (i = 0; i < port->naddrs - 1; i++) { | |
96 if (addr[i].addr == sin->sin_addr.s_addr) { | |
97 break; | |
98 } | |
99 } | |
100 | |
101 addr_conf = &addr[i].conf; | |
102 | |
103 break; | |
104 } | |
105 | |
106 } else { | |
107 switch (c->local_sockaddr->sa_family) { | |
108 | |
109 #if (NGX_HAVE_INET6) | |
110 case AF_INET6: | |
111 addr6 = port->addrs; | |
112 addr_conf = &addr6[0].conf; | |
113 break; | |
114 #endif | |
115 | |
116 default: /* AF_INET */ | |
117 addr = port->addrs; | |
118 addr_conf = &addr[0].conf; | |
119 break; | |
120 } | |
121 } | |
122 | |
123 s = ngx_pcalloc(c->pool, sizeof(ngx_stream_session_t)); | |
124 if (s == NULL) { | |
125 ngx_stream_close_connection(c); | |
126 return; | |
127 } | |
128 | |
129 s->signature = NGX_STREAM_MODULE; | |
130 s->main_conf = addr_conf->ctx->main_conf; | |
131 s->srv_conf = addr_conf->ctx->srv_conf; | |
132 | |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
133 #if (NGX_STREAM_SSL) |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
134 s->ssl = addr_conf->ssl; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
135 #endif |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
136 |
6692 | 137 if (c->buffer) { |
138 s->received += c->buffer->last - c->buffer->pos; | |
139 } | |
140 | |
6115 | 141 s->connection = c; |
142 c->data = s; | |
143 | |
144 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); | |
145 | |
6129
187aa751ad62
Core: the ngx_set_connection_log() macro.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
146 ngx_set_connection_log(c, cscf->error_log); |
6115 | 147 |
148 len = ngx_sock_ntop(c->sockaddr, c->socklen, text, NGX_SOCKADDR_STRLEN, 1); | |
149 | |
6461
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
150 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%uA %sclient %*s connected to %V", |
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
151 c->number, c->type == SOCK_DGRAM ? "udp " : "", |
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
152 len, text, &addr_conf->addr_text); |
6115 | 153 |
154 c->log->connection = c->number; | |
155 c->log->handler = ngx_stream_log_error; | |
156 c->log->data = s; | |
157 c->log->action = "initializing connection"; | |
158 c->log_error = NGX_ERROR_INFO; | |
159 | |
6682
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
160 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module); |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
161 if (s->ctx == NULL) { |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
162 ngx_stream_close_connection(c); |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
163 return; |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
164 } |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
165 |
6175 | 166 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); |
167 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
168 s->variables = ngx_pcalloc(s->connection->pool, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
169 cmcf->variables.nelts |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
170 * sizeof(ngx_stream_variable_value_t)); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
171 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
172 if (s->variables == NULL) { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
173 ngx_stream_close_connection(c); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
174 return; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
175 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
176 |
6669
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
177 tp = ngx_timeofday(); |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
178 s->start_sec = tp->sec; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
179 s->start_msec = tp->msec; |
164a0824ce20
Stream: the $session_time variable.
Vladimir Homutov <vl@nginx.com>
parents:
6607
diff
changeset
|
180 |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
181 rev = c->read; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
182 rev->handler = ngx_stream_init_session_handler; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
183 |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
184 if (addr_conf->proxy_protocol) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
185 c->log->action = "reading PROXY protocol"; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
186 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
187 rev->handler = ngx_stream_proxy_protocol_handler; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
188 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
189 if (!rev->ready) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
190 ngx_add_timer(rev, cscf->proxy_protocol_timeout); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
191 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
192 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
193 ngx_stream_finalize_session(s, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
194 NGX_STREAM_INTERNAL_SERVER_ERROR); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
195 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
196 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
197 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
198 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
199 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
200 |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
201 if (ngx_use_accept_mutex) { |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
202 ngx_post_event(rev, &ngx_posted_events); |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
203 return; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
204 } |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
205 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
206 rev->handler(rev); |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
207 } |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
208 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
209 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
210 static void |
6680
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
211 ngx_stream_proxy_protocol_handler(ngx_event_t *rev) |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
212 { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
213 u_char *p, buf[NGX_PROXY_PROTOCOL_MAX_HEADER]; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
214 size_t size; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
215 ssize_t n; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
216 ngx_err_t err; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
217 ngx_connection_t *c; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
218 ngx_stream_session_t *s; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
219 ngx_stream_core_srv_conf_t *cscf; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
220 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
221 c = rev->data; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
222 s = c->data; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
223 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
224 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
225 "stream PROXY protocol handler"); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
226 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
227 if (rev->timedout) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
228 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
229 ngx_stream_finalize_session(s, NGX_STREAM_OK); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
230 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
231 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
232 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
233 n = recv(c->fd, (char *) buf, sizeof(buf), MSG_PEEK); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
234 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
235 err = ngx_socket_errno; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
236 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
237 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "recv(): %z", n); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
238 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
239 if (n == -1) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
240 if (err == NGX_EAGAIN) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
241 rev->ready = 0; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
242 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
243 if (!rev->timer_set) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
244 cscf = ngx_stream_get_module_srv_conf(s, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
245 ngx_stream_core_module); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
246 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
247 ngx_add_timer(rev, cscf->proxy_protocol_timeout); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
248 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
249 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
250 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
251 ngx_stream_finalize_session(s, |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
252 NGX_STREAM_INTERNAL_SERVER_ERROR); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
253 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
254 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
255 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
256 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
257 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
258 ngx_connection_error(c, err, "recv() failed"); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
259 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
260 ngx_stream_finalize_session(s, NGX_STREAM_OK); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
261 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
262 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
263 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
264 if (rev->timer_set) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
265 ngx_del_timer(rev); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
266 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
267 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
268 p = ngx_proxy_protocol_read(c, buf, buf + n); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
269 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
270 if (p == NULL) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
271 ngx_stream_finalize_session(s, NGX_STREAM_BAD_REQUEST); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
272 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
273 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
274 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
275 size = p - buf; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
276 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
277 if (c->recv(c, buf, size) != (ssize_t) size) { |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
278 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
279 return; |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
280 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
281 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
282 ngx_stream_init_session_handler(rev); |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
283 } |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
284 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
285 |
7357abd1fa8c
Stream: the "proxy_protocol" parameter of the "listen" directive.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6679
diff
changeset
|
286 static void |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
287 ngx_stream_init_session_handler(ngx_event_t *rev) |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
288 { |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
289 int tcp_nodelay; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
290 ngx_int_t rc; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
291 ngx_connection_t *c; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
292 ngx_stream_session_t *s; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
293 ngx_stream_core_srv_conf_t *cscf; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
294 ngx_stream_core_main_conf_t *cmcf; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
295 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
296 c = rev->data; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
297 s = c->data; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
298 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
299 c->log->action = "initializing session"; |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
300 |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
301 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
302 |
6684
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
303 if (cmcf->realip_handler) { |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
304 rc = cmcf->realip_handler(s); |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
305 |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
306 if (rc == NGX_ERROR) { |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
307 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
308 return; |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
309 } |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
310 } |
9cac11efb205
Stream: realip module.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6682
diff
changeset
|
311 |
6197
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
312 if (cmcf->limit_conn_handler) { |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
313 rc = cmcf->limit_conn_handler(s); |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
314 |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
315 if (rc == NGX_ERROR) { |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
316 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
317 return; |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
318 } |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
319 |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
320 if (rc == NGX_ABORT) { |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
321 ngx_stream_finalize_session(s, NGX_STREAM_SERVICE_UNAVAILABLE); |
6197
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
322 return; |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
323 } |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
324 } |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
325 |
6175 | 326 if (cmcf->access_handler) { |
327 rc = cmcf->access_handler(s); | |
328 | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
329 if (rc == NGX_ERROR) { |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
330 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
331 return; |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
332 } |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
333 |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
334 if (rc == NGX_ABORT) { |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
335 ngx_stream_finalize_session(s, NGX_STREAM_FORBIDDEN); |
6175 | 336 return; |
337 } | |
338 } | |
339 | |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
340 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); |
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
341 |
6436 | 342 if (c->type == SOCK_STREAM |
343 && cscf->tcp_nodelay | |
344 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) | |
345 { | |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
346 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, "tcp_nodelay"); |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
347 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
348 tcp_nodelay = 1; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
349 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
350 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
351 (const void *) &tcp_nodelay, sizeof(int)) == -1) |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
352 { |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
353 ngx_connection_error(c, ngx_socket_errno, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
354 "setsockopt(TCP_NODELAY) failed"); |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
355 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
356 return; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
357 } |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
358 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
359 c->tcp_nodelay = NGX_TCP_NODELAY_SET; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
360 } |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
361 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
362 |
6115 | 363 #if (NGX_STREAM_SSL) |
364 { | |
365 ngx_stream_ssl_conf_t *sslcf; | |
366 | |
367 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module); | |
368 | |
6679
40e8ce405859
Stream: postpone session initialization under accept mutex.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6678
diff
changeset
|
369 if (s->ssl) { |
6115 | 370 c->log->action = "SSL handshaking"; |
371 | |
372 if (sslcf->ssl.ctx == NULL) { | |
373 ngx_log_error(NGX_LOG_ERR, c->log, 0, | |
374 "no \"ssl_certificate\" is defined " | |
375 "in server listening on SSL port"); | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
376 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
6115 | 377 return; |
378 } | |
379 | |
380 ngx_stream_ssl_init_connection(&sslcf->ssl, c); | |
381 return; | |
382 } | |
383 } | |
384 #endif | |
385 | |
386 c->log->action = "handling client connection"; | |
387 | |
388 cscf->handler(s); | |
389 } | |
390 | |
391 | |
392 #if (NGX_STREAM_SSL) | |
393 | |
394 static void | |
395 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c) | |
396 { | |
397 ngx_stream_session_t *s; | |
398 ngx_stream_ssl_conf_t *sslcf; | |
399 | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
400 s = c->data; |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
401 |
6115 | 402 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) { |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
403 ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR); |
6115 | 404 return; |
405 } | |
406 | |
407 if (ngx_ssl_handshake(c) == NGX_AGAIN) { | |
408 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module); | |
409 | |
410 ngx_add_timer(c->read, sslcf->handshake_timeout); | |
411 | |
412 c->ssl->handler = ngx_stream_ssl_handshake_handler; | |
413 | |
414 return; | |
415 } | |
416 | |
417 ngx_stream_ssl_handshake_handler(c); | |
418 } | |
419 | |
420 | |
421 static void | |
422 ngx_stream_ssl_handshake_handler(ngx_connection_t *c) | |
423 { | |
6682
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
424 ngx_stream_session_t *s; |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
425 ngx_stream_core_srv_conf_t *cscf; |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
426 |
6115 | 427 if (!c->ssl->handshaked) { |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
428 ngx_stream_finalize_session(c->data, NGX_STREAM_INTERNAL_SERVER_ERROR); |
6115 | 429 return; |
430 } | |
431 | |
432 if (c->read->timer_set) { | |
433 ngx_del_timer(c->read); | |
434 } | |
435 | |
6682
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
436 c->log->action = "handling client connection"; |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
437 |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
438 s = c->data; |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
439 |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
440 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
441 |
db422604ceb0
Stream: allow using the session context inside handlers.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6680
diff
changeset
|
442 cscf->handler(s); |
6115 | 443 } |
444 | |
445 #endif | |
446 | |
447 | |
448 void | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
449 ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc) |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
450 { |
6678 | 451 ngx_stream_core_main_conf_t *cmcf; |
452 | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
453 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
454 "finalize stream session: %i", rc); |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
455 |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
456 s->status = rc; |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
457 |
6678 | 458 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); |
459 | |
460 if (cmcf->access_log_handler) { | |
461 (void) cmcf->access_log_handler(s); | |
462 } | |
463 | |
6674
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
464 ngx_stream_close_connection(s->connection); |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
465 } |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
466 |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
467 |
38143d1abdec
Stream: the $status variable.
Roman Arutyunyan <arut@nginx.com>
parents:
6669
diff
changeset
|
468 static void |
6115 | 469 ngx_stream_close_connection(ngx_connection_t *c) |
470 { | |
471 ngx_pool_t *pool; | |
472 | |
473 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
474 "close stream connection: %d", c->fd); | |
475 | |
476 #if (NGX_STREAM_SSL) | |
477 | |
478 if (c->ssl) { | |
479 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
480 c->ssl->handler = ngx_stream_close_connection; | |
481 return; | |
482 } | |
483 } | |
484 | |
485 #endif | |
486 | |
487 #if (NGX_STAT_STUB) | |
488 (void) ngx_atomic_fetch_add(ngx_stat_active, -1); | |
489 #endif | |
490 | |
491 pool = c->pool; | |
492 | |
493 ngx_close_connection(c); | |
494 | |
495 ngx_destroy_pool(pool); | |
496 } | |
497 | |
498 | |
499 static u_char * | |
500 ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
501 { | |
502 u_char *p; | |
503 ngx_stream_session_t *s; | |
504 | |
505 if (log->action) { | |
506 p = ngx_snprintf(buf, len, " while %s", log->action); | |
507 len -= p - buf; | |
508 buf = p; | |
509 } | |
510 | |
511 s = log->data; | |
512 | |
6461
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
513 p = ngx_snprintf(buf, len, ", %sclient: %V, server: %V", |
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
514 s->connection->type == SOCK_DGRAM ? "udp " : "", |
6115 | 515 &s->connection->addr_text, |
516 &s->connection->listening->addr_text); | |
6223
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
517 len -= p - buf; |
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
518 buf = p; |
6115 | 519 |
520 if (s->log_handler) { | |
6223
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
521 p = s->log_handler(log, buf, len); |
6115 | 522 } |
523 | |
524 return p; | |
525 } |