Mercurial > hg > nginx-quic
comparison src/core/ngx_connection.c @ 417:0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 07 Sep 2004 15:29:22 +0000 |
parents | 55e496a8ece3 |
children | da8c5707af39 |
comparison
equal
deleted
inserted
replaced
416:b9bd635011de | 417:0526206251f6 |
---|---|
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 | 5 |
6 | 6 |
7 ngx_os_io_t ngx_io; | 7 ngx_os_io_t ngx_io; |
8 | |
9 | |
10 ngx_listening_t *ngx_listening_inet_stream_socket(ngx_conf_t *cf, | |
11 in_addr_t addr, | |
12 in_port_t port) | |
13 { | |
14 size_t len; | |
15 ngx_listening_t *ls; | |
16 struct sockaddr_in *addr_in; | |
17 | |
18 if (!(ls = ngx_array_push(&cf->cycle->listening))) { | |
19 return NULL; | |
20 } | |
21 | |
22 ngx_memzero(ls, sizeof(ngx_listening_t)); | |
23 | |
24 if (!(addr_in = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)))) { | |
25 return NULL; | |
26 } | |
27 | |
28 #if (HAVE_SIN_LEN) | |
29 addr_in->sin_len = sizeof(struct sockaddr_in); | |
30 #endif | |
31 addr_in->sin_family = AF_INET; | |
32 addr_in->sin_addr.s_addr = addr; | |
33 addr_in->sin_port = htons(port); | |
34 | |
35 if (!(ls->addr_text.data = ngx_palloc(cf->pool, INET_ADDRSTRLEN + 6))) { | |
36 return NULL; | |
37 } | |
38 | |
39 len = ngx_inet_ntop(AF_INET, &addr, ls->addr_text.data, INET_ADDRSTRLEN); | |
40 ls->addr_text.len = ngx_snprintf((char *) ls->addr_text.data + len, | |
41 6, ":%d", port); | |
42 | |
43 ls->fd = (ngx_socket_t) -1; | |
44 ls->family = AF_INET; | |
45 ls->type = SOCK_STREAM; | |
46 ls->protocol = IPPROTO_IP; | |
47 #if (WIN32) | |
48 ls->flags = WSA_FLAG_OVERLAPPED; | |
49 #endif | |
50 ls->sockaddr = (struct sockaddr *) addr_in; | |
51 ls->socklen = sizeof(struct sockaddr_in); | |
52 ls->addr = offsetof(struct sockaddr_in, sin_addr); | |
53 ls->addr_text_max_len = INET_ADDRSTRLEN; | |
54 | |
55 return ls; | |
56 } | |
8 | 57 |
9 | 58 |
10 ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle) | 59 ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle) |
11 { | 60 { |
12 ngx_uint_t i; | 61 ngx_uint_t i; |
249 cycle->connections[fd].fd = (ngx_socket_t) -1; | 298 cycle->connections[fd].fd = (ngx_socket_t) -1; |
250 } | 299 } |
251 } | 300 } |
252 | 301 |
253 | 302 |
303 void ngx_close_connection(ngx_connection_t *c) | |
304 { | |
305 ngx_socket_t fd; | |
306 | |
307 if (c->pool == NULL) { | |
308 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed"); | |
309 return; | |
310 } | |
311 | |
312 #if (NGX_OPENSSL) | |
313 | |
314 if (c->ssl) { | |
315 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
316 c->read->event_handler = ngx_ssl_close_handler; | |
317 c->write->event_handler = ngx_ssl_close_handler; | |
318 return; | |
319 } | |
320 } | |
321 | |
322 #endif | |
323 | |
324 if (c->read->timer_set) { | |
325 ngx_del_timer(c->read); | |
326 } | |
327 | |
328 if (c->write->timer_set) { | |
329 ngx_del_timer(c->write); | |
330 } | |
331 | |
332 if (ngx_del_conn) { | |
333 ngx_del_conn(c, NGX_CLOSE_EVENT); | |
334 | |
335 } else { | |
336 if (c->read->active || c->read->disabled) { | |
337 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT); | |
338 } | |
339 | |
340 if (c->write->active || c->write->disabled) { | |
341 ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT); | |
342 } | |
343 } | |
344 | |
345 #if (NGX_THREADS) | |
346 | |
347 /* | |
348 * we have to clean the connection information before the closing | |
349 * because another thread may reopen the same file descriptor | |
350 * before we clean the connection | |
351 */ | |
352 | |
353 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) { | |
354 | |
355 if (c->read->prev) { | |
356 ngx_delete_posted_event(c->read); | |
357 } | |
358 | |
359 if (c->write->prev) { | |
360 ngx_delete_posted_event(c->write); | |
361 } | |
362 | |
363 c->read->closed = 1; | |
364 c->write->closed = 1; | |
365 | |
366 if (c->single_connection) { | |
367 ngx_unlock(&c->lock); | |
368 c->read->locked = 0; | |
369 c->write->locked = 0; | |
370 } | |
371 | |
372 ngx_mutex_unlock(ngx_posted_events_mutex); | |
373 } | |
374 | |
375 #else | |
376 | |
377 if (c->read->prev) { | |
378 ngx_delete_posted_event(c->read); | |
379 } | |
380 | |
381 if (c->write->prev) { | |
382 ngx_delete_posted_event(c->write); | |
383 } | |
384 | |
385 c->read->closed = 1; | |
386 c->write->closed = 1; | |
387 | |
388 #endif | |
389 | |
390 fd = c->fd; | |
391 c->fd = (ngx_socket_t) -1; | |
392 c->data = NULL; | |
393 | |
394 ngx_destroy_pool(c->pool); | |
395 | |
396 if (ngx_close_socket(fd) == -1) { | |
397 | |
398 /* we use ngx_cycle->log because c->log was in c->pool */ | |
399 | |
400 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_socket_errno, | |
401 ngx_close_socket_n " failed"); | |
402 } | |
403 } | |
404 | |
405 | |
406 | |
254 ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text) | 407 ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text) |
255 { | 408 { |
256 ngx_uint_t level; | 409 ngx_uint_t level; |
257 | 410 |
258 if (err == NGX_ECONNRESET | 411 if (err == NGX_ECONNRESET |