Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_handler.c @ 6624:e3faa5fb7772
Configure: fix build with -Werror=old-style-definition.
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
author | Piotr Sikora <piotrsikora@google.com> |
---|---|
date | Mon, 27 Jun 2016 15:00:05 -0700 |
parents | c70b7f4537e1 |
children | 164a0824ce20 |
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 | |
14 static u_char *ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len); | |
15 static void ngx_stream_init_session(ngx_connection_t *c); | |
16 | |
17 #if (NGX_STREAM_SSL) | |
18 static void ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); | |
19 static void ngx_stream_ssl_handshake_handler(ngx_connection_t *c); | |
20 #endif | |
21 | |
22 | |
23 void | |
24 ngx_stream_init_connection(ngx_connection_t *c) | |
25 { | |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
26 int tcp_nodelay; |
6175 | 27 u_char text[NGX_SOCKADDR_STRLEN]; |
28 size_t len; | |
29 ngx_int_t rc; | |
30 ngx_uint_t i; | |
31 struct sockaddr *sa; | |
32 ngx_stream_port_t *port; | |
33 struct sockaddr_in *sin; | |
34 ngx_stream_in_addr_t *addr; | |
35 ngx_stream_session_t *s; | |
36 ngx_stream_addr_conf_t *addr_conf; | |
6115 | 37 #if (NGX_HAVE_INET6) |
6175 | 38 struct sockaddr_in6 *sin6; |
39 ngx_stream_in6_addr_t *addr6; | |
6115 | 40 #endif |
6175 | 41 ngx_stream_core_srv_conf_t *cscf; |
42 ngx_stream_core_main_conf_t *cmcf; | |
6115 | 43 |
44 /* find the server configuration for the address:port */ | |
45 | |
46 port = c->listening->servers; | |
47 | |
48 if (port->naddrs > 1) { | |
49 | |
50 /* | |
51 * There are several addresses on this port and one of them | |
52 * is the "*:port" wildcard so getsockname() is needed to determine | |
53 * the server address. | |
54 * | |
6436 | 55 * AcceptEx() and recvmsg() already gave this address. |
6115 | 56 */ |
57 | |
58 if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) { | |
59 ngx_stream_close_connection(c); | |
60 return; | |
61 } | |
62 | |
63 sa = c->local_sockaddr; | |
64 | |
65 switch (sa->sa_family) { | |
66 | |
67 #if (NGX_HAVE_INET6) | |
68 case AF_INET6: | |
69 sin6 = (struct sockaddr_in6 *) sa; | |
70 | |
71 addr6 = port->addrs; | |
72 | |
73 /* the last address is "*" */ | |
74 | |
75 for (i = 0; i < port->naddrs - 1; i++) { | |
76 if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) { | |
77 break; | |
78 } | |
79 } | |
80 | |
81 addr_conf = &addr6[i].conf; | |
82 | |
83 break; | |
84 #endif | |
85 | |
86 default: /* AF_INET */ | |
87 sin = (struct sockaddr_in *) sa; | |
88 | |
89 addr = port->addrs; | |
90 | |
91 /* the last address is "*" */ | |
92 | |
93 for (i = 0; i < port->naddrs - 1; i++) { | |
94 if (addr[i].addr == sin->sin_addr.s_addr) { | |
95 break; | |
96 } | |
97 } | |
98 | |
99 addr_conf = &addr[i].conf; | |
100 | |
101 break; | |
102 } | |
103 | |
104 } else { | |
105 switch (c->local_sockaddr->sa_family) { | |
106 | |
107 #if (NGX_HAVE_INET6) | |
108 case AF_INET6: | |
109 addr6 = port->addrs; | |
110 addr_conf = &addr6[0].conf; | |
111 break; | |
112 #endif | |
113 | |
114 default: /* AF_INET */ | |
115 addr = port->addrs; | |
116 addr_conf = &addr[0].conf; | |
117 break; | |
118 } | |
119 } | |
120 | |
121 s = ngx_pcalloc(c->pool, sizeof(ngx_stream_session_t)); | |
122 if (s == NULL) { | |
123 ngx_stream_close_connection(c); | |
124 return; | |
125 } | |
126 | |
127 s->signature = NGX_STREAM_MODULE; | |
128 s->main_conf = addr_conf->ctx->main_conf; | |
129 s->srv_conf = addr_conf->ctx->srv_conf; | |
130 | |
131 s->connection = c; | |
132 c->data = s; | |
133 | |
134 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); | |
135 | |
6129
187aa751ad62
Core: the ngx_set_connection_log() macro.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
136 ngx_set_connection_log(c, cscf->error_log); |
6115 | 137 |
138 len = ngx_sock_ntop(c->sockaddr, c->socklen, text, NGX_SOCKADDR_STRLEN, 1); | |
139 | |
6461
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
140 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
|
141 c->number, c->type == SOCK_DGRAM ? "udp " : "", |
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
142 len, text, &addr_conf->addr_text); |
6115 | 143 |
144 c->log->connection = c->number; | |
145 c->log->handler = ngx_stream_log_error; | |
146 c->log->data = s; | |
147 c->log->action = "initializing connection"; | |
148 c->log_error = NGX_ERROR_INFO; | |
149 | |
6175 | 150 cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module); |
151 | |
6607
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
152 s->variables = ngx_pcalloc(s->connection->pool, |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
153 cmcf->variables.nelts |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
154 * sizeof(ngx_stream_variable_value_t)); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
155 |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
156 if (s->variables == NULL) { |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
157 ngx_stream_close_connection(c); |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
158 return; |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
159 } |
c70b7f4537e1
Stream: variables and script.
Vladimir Homutov <vl@nginx.com>
parents:
6461
diff
changeset
|
160 |
6197
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
161 if (cmcf->limit_conn_handler) { |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
162 rc = cmcf->limit_conn_handler(s); |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
163 |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
164 if (rc != NGX_DECLINED) { |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
165 ngx_stream_close_connection(c); |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
166 return; |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
167 } |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
168 } |
0dcef374b8bb
Stream: connection limiting module.
Vladimir Homutov <vl@nginx.com>
parents:
6175
diff
changeset
|
169 |
6175 | 170 if (cmcf->access_handler) { |
171 rc = cmcf->access_handler(s); | |
172 | |
173 if (rc != NGX_OK && rc != NGX_DECLINED) { | |
174 ngx_stream_close_connection(c); | |
175 return; | |
176 } | |
177 } | |
178 | |
6436 | 179 if (c->type == SOCK_STREAM |
180 && cscf->tcp_nodelay | |
181 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) | |
182 { | |
6221
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
183 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
|
184 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
185 tcp_nodelay = 1; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
186 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
187 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
188 (const void *) &tcp_nodelay, sizeof(int)) == -1) |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
189 { |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
190 ngx_connection_error(c, ngx_socket_errno, |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
191 "setsockopt(TCP_NODELAY) failed"); |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
192 ngx_stream_close_connection(c); |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
193 return; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
194 } |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
195 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
196 c->tcp_nodelay = NGX_TCP_NODELAY_SET; |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
197 } |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
198 |
7565e056fad6
Stream: the "tcp_nodelay" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6197
diff
changeset
|
199 |
6115 | 200 #if (NGX_STREAM_SSL) |
201 { | |
202 ngx_stream_ssl_conf_t *sslcf; | |
203 | |
204 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module); | |
205 | |
206 if (addr_conf->ssl) { | |
207 c->log->action = "SSL handshaking"; | |
208 | |
209 if (sslcf->ssl.ctx == NULL) { | |
210 ngx_log_error(NGX_LOG_ERR, c->log, 0, | |
211 "no \"ssl_certificate\" is defined " | |
212 "in server listening on SSL port"); | |
213 ngx_stream_close_connection(c); | |
214 return; | |
215 } | |
216 | |
217 ngx_stream_ssl_init_connection(&sslcf->ssl, c); | |
218 return; | |
219 } | |
220 } | |
221 #endif | |
222 | |
223 ngx_stream_init_session(c); | |
224 } | |
225 | |
226 | |
227 static void | |
228 ngx_stream_init_session(ngx_connection_t *c) | |
229 { | |
230 ngx_stream_session_t *s; | |
231 ngx_stream_core_srv_conf_t *cscf; | |
232 | |
233 s = c->data; | |
234 c->log->action = "handling client connection"; | |
235 | |
236 cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module); | |
237 | |
238 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module); | |
239 if (s->ctx == NULL) { | |
240 ngx_stream_close_connection(c); | |
241 return; | |
242 } | |
243 | |
244 cscf->handler(s); | |
245 } | |
246 | |
247 | |
248 #if (NGX_STREAM_SSL) | |
249 | |
250 static void | |
251 ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c) | |
252 { | |
253 ngx_stream_session_t *s; | |
254 ngx_stream_ssl_conf_t *sslcf; | |
255 | |
256 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) { | |
257 ngx_stream_close_connection(c); | |
258 return; | |
259 } | |
260 | |
261 if (ngx_ssl_handshake(c) == NGX_AGAIN) { | |
262 | |
263 s = c->data; | |
264 | |
265 sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module); | |
266 | |
267 ngx_add_timer(c->read, sslcf->handshake_timeout); | |
268 | |
269 c->ssl->handler = ngx_stream_ssl_handshake_handler; | |
270 | |
271 return; | |
272 } | |
273 | |
274 ngx_stream_ssl_handshake_handler(c); | |
275 } | |
276 | |
277 | |
278 static void | |
279 ngx_stream_ssl_handshake_handler(ngx_connection_t *c) | |
280 { | |
281 if (!c->ssl->handshaked) { | |
282 ngx_stream_close_connection(c); | |
283 return; | |
284 } | |
285 | |
286 if (c->read->timer_set) { | |
287 ngx_del_timer(c->read); | |
288 } | |
289 | |
290 ngx_stream_init_session(c); | |
291 } | |
292 | |
293 #endif | |
294 | |
295 | |
296 void | |
297 ngx_stream_close_connection(ngx_connection_t *c) | |
298 { | |
299 ngx_pool_t *pool; | |
300 | |
301 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
302 "close stream connection: %d", c->fd); | |
303 | |
304 #if (NGX_STREAM_SSL) | |
305 | |
306 if (c->ssl) { | |
307 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
308 c->ssl->handler = ngx_stream_close_connection; | |
309 return; | |
310 } | |
311 } | |
312 | |
313 #endif | |
314 | |
315 #if (NGX_STAT_STUB) | |
316 (void) ngx_atomic_fetch_add(ngx_stat_active, -1); | |
317 #endif | |
318 | |
319 pool = c->pool; | |
320 | |
321 ngx_close_connection(c); | |
322 | |
323 ngx_destroy_pool(pool); | |
324 } | |
325 | |
326 | |
327 static u_char * | |
328 ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
329 { | |
330 u_char *p; | |
331 ngx_stream_session_t *s; | |
332 | |
333 if (log->action) { | |
334 p = ngx_snprintf(buf, len, " while %s", log->action); | |
335 len -= p - buf; | |
336 buf = p; | |
337 } | |
338 | |
339 s = log->data; | |
340 | |
6461
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
341 p = ngx_snprintf(buf, len, ", %sclient: %V, server: %V", |
a01e315b3a78
Stream: additional logging for UDP.
Vladimir Homutov <vl@nginx.com>
parents:
6436
diff
changeset
|
342 s->connection->type == SOCK_DGRAM ? "udp " : "", |
6115 | 343 &s->connection->addr_text, |
344 &s->connection->listening->addr_text); | |
6223
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
345 len -= p - buf; |
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
346 buf = p; |
6115 | 347 |
348 if (s->log_handler) { | |
6223
d1f94042c29c
Stream: fixed potential error log buffer overrun.
Vladimir Homutov <vl@nginx.com>
parents:
6221
diff
changeset
|
349 p = s->log_handler(log, buf, len); |
6115 | 350 } |
351 | |
352 return p; | |
353 } |