blob: c51021a6bcefbc57130f743bcae36f7169930128 [file] [log] [blame]
Igor Sysoev73009772003-02-06 17:21:13 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev73009772003-02-06 17:21:13 +00007#include <ngx_config.h>
Igor Sysoev73009772003-02-06 17:21:13 +00008#include <ngx_core.h>
Igor Sysoev73009772003-02-06 17:21:13 +00009#include <ngx_event.h>
Igor Sysoev73009772003-02-06 17:21:13 +000010
11
Igor Sysoev31eb8c02005-09-23 11:02:22 +000012static void ngx_close_posted_connection(ngx_connection_t *c);
13
14
15void
16ngx_event_acceptex(ngx_event_t *rev)
Igor Sysoev73009772003-02-06 17:21:13 +000017{
18 ngx_connection_t *c;
19
Igor Sysoev1b735832004-11-11 14:07:14 +000020 c = rev->data;
21
Igor Sysoev31eb8c02005-09-23 11:02:22 +000022 c->log->handler = ngx_accept_log_error;
23
Igor Sysoev1b735832004-11-11 14:07:14 +000024 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "AcceptEx: %d", c->fd);
Igor Sysoev73009772003-02-06 17:21:13 +000025
Igor Sysoeve4a25262003-06-06 14:59:20 +000026 if (rev->ovlp.error) {
Igor Sysoev239baac2003-06-11 15:28:34 +000027 ngx_log_error(NGX_LOG_CRIT, c->log, rev->ovlp.error,
Igor Sysoev1b735832004-11-11 14:07:14 +000028 "AcceptEx() %V failed", &c->listening->addr_text);
Igor Sysoev1c104622003-06-03 15:42:58 +000029 return;
Igor Sysoev73009772003-02-06 17:21:13 +000030 }
31
Igor Sysoeve4a25262003-06-06 14:59:20 +000032 /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
Igor Sysoevfa73aac2003-05-21 13:28:21 +000033
Igor Sysoev1c104622003-06-03 15:42:58 +000034 if (setsockopt(c->fd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
Igor Sysoev1b735832004-11-11 14:07:14 +000035 (char *) &c->listening->fd, sizeof(ngx_socket_t)) == -1)
Igor Sysoev1c104622003-06-03 15:42:58 +000036 {
Igor Sysoev239baac2003-06-11 15:28:34 +000037 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
Igor Sysoev1b735832004-11-11 14:07:14 +000038 "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed for %V",
39 &c->addr_text);
Igor Sysoeve4a25262003-06-06 14:59:20 +000040 } else {
Igor Sysoev239baac2003-06-11 15:28:34 +000041 c->accept_context_updated = 1;
Igor Sysoev1c104622003-06-03 15:42:58 +000042 }
Igor Sysoevfa73aac2003-05-21 13:28:21 +000043
Igor Sysoev239baac2003-06-11 15:28:34 +000044 getacceptexsockaddrs(c->buffer->pos, c->listening->post_accept_buffer_size,
45 c->listening->socklen + 16,
46 c->listening->socklen + 16,
Igor Sysoev73009772003-02-06 17:21:13 +000047 &c->local_sockaddr, &c->local_socklen,
48 &c->sockaddr, &c->socklen);
49
Igor Sysoev239baac2003-06-11 15:28:34 +000050 if (c->listening->post_accept_buffer_size) {
51 c->buffer->last += rev->available;
52 c->buffer->end = c->buffer->start
53 + c->listening->post_accept_buffer_size;
54
55 } else {
56 c->buffer = NULL;
57 }
58
Igor Sysoev1b735832004-11-11 14:07:14 +000059 if (c->listening->addr_ntop) {
60 c->addr_text.data = ngx_palloc(c->pool,
61 c->listening->addr_text_max_len);
62 if (c->addr_text.data == NULL) {
63 /* TODO: close socket */
64 return;
65 }
66
67 c->addr_text.len = ngx_sock_ntop(c->listening->family, c->sockaddr,
68 c->addr_text.data,
69 c->listening->addr_text_max_len);
70 if (c->addr_text.len == 0) {
71 /* TODO: close socket */
72 return;
73 }
74 }
75
Igor Sysoev73009772003-02-06 17:21:13 +000076 ngx_event_post_acceptex(c->listening, 1);
77
Igor Sysoev78452232005-10-12 13:50:36 +000078 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
Igor Sysoev73009772003-02-06 17:21:13 +000079
Igor Sysoev239baac2003-06-11 15:28:34 +000080 c->listening->handler(c);
Igor Sysoev73009772003-02-06 17:21:13 +000081
Igor Sysoev1c104622003-06-03 15:42:58 +000082 return;
Igor Sysoev73009772003-02-06 17:21:13 +000083
84}
85
86
Igor Sysoev31eb8c02005-09-23 11:02:22 +000087ngx_int_t
88ngx_event_post_acceptex(ngx_listening_t *ls, ngx_uint_t n)
Igor Sysoev73009772003-02-06 17:21:13 +000089{
Igor Sysoevab9d5fd2004-03-05 08:34:24 +000090 u_long rcvd;
Igor Sysoev73009772003-02-06 17:21:13 +000091 ngx_err_t err;
Igor Sysoev31eb8c02005-09-23 11:02:22 +000092 ngx_log_t *log;
93 ngx_uint_t i;
Igor Sysoev73009772003-02-06 17:21:13 +000094 ngx_event_t *rev, *wev;
95 ngx_socket_t s;
96 ngx_connection_t *c;
97
98 for (i = 0; i < n; i++) {
99
100 /* TODO: look up reused sockets */
101
Igor Sysoev02025fd2005-01-18 13:03:58 +0000102 s = ngx_socket(ls->family, ls->type, 0);
Igor Sysoeva5362982004-03-04 07:04:55 +0000103
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000104 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &ls->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000105 ngx_socket_n " s:%d", s);
Igor Sysoev73009772003-02-06 17:21:13 +0000106
107 if (s == -1) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000108 ngx_log_error(NGX_LOG_ALERT, &ls->log, ngx_socket_errno,
109 ngx_socket_n " failed");
Igor Sysoev73009772003-02-06 17:21:13 +0000110
111 return NGX_ERROR;
112 }
113
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000114 c = ngx_get_connection(s, &ls->log);
Igor Sysoev73009772003-02-06 17:21:13 +0000115
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000116 if (c == NULL) {
117 return NGX_ERROR;
Igor Sysoev239baac2003-06-11 15:28:34 +0000118 }
119
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000120 c->pool = ngx_create_pool(ls->pool_size, &ls->log);
121 if (c->pool == NULL) {
122 ngx_close_posted_connection(c);
123 return NGX_ERROR;
124 }
125
126 log = ngx_palloc(c->pool, sizeof(ngx_log_t));
127 if (log == NULL) {
128 ngx_close_posted_connection(c);
129 return NGX_ERROR;
130 }
131
132 c->buffer = ngx_create_temp_buf(c->pool, ls->post_accept_buffer_size
133 + 2 * (ls->socklen + 16));
134 if (c->buffer == NULL) {
135 ngx_close_posted_connection(c);
136 return NGX_ERROR;
137 }
138
139 c->local_sockaddr = ngx_palloc(c->pool, ls->socklen);
140 if (c->local_sockaddr == NULL) {
141 ngx_close_posted_connection(c);
142 return NGX_ERROR;
143 }
144
145 c->sockaddr = ngx_palloc(c->pool, ls->socklen);
146 if (c->sockaddr == NULL) {
147 ngx_close_posted_connection(c);
148 return NGX_ERROR;
149 }
150
151 *log = ls->log;
152 c->log = log;
153
154 c->recv = ngx_recv;
155 c->send = ngx_send;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000156 c->recv_chain = ngx_recv_chain;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000157 c->send_chain = ngx_send_chain;
158
159 c->unexpected_eof = 1;
160
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000161 c->listening = ls;
162
Igor Sysoev78452232005-10-12 13:50:36 +0000163 rev = c->read;
164 wev = c->write;
Igor Sysoev73009772003-02-06 17:21:13 +0000165
166 rev->ovlp.event = rev;
167 wev->ovlp.event = wev;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000168 rev->handler = ngx_event_acceptex;
Igor Sysoev73009772003-02-06 17:21:13 +0000169
Igor Sysoev1b735832004-11-11 14:07:14 +0000170 rev->ready = 1;
Igor Sysoev1b735832004-11-11 14:07:14 +0000171 wev->ready = 1;
Igor Sysoev73009772003-02-06 17:21:13 +0000172
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000173 rev->log = c->log;
174 wev->log = c->log;
Igor Sysoev73009772003-02-06 17:21:13 +0000175
Igor Sysoeve4a25262003-06-06 14:59:20 +0000176 if (ngx_add_event(rev, 0, NGX_IOCP_IO) == NGX_ERROR) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000177 ngx_close_posted_connection(c);
Igor Sysoev73009772003-02-06 17:21:13 +0000178 return NGX_ERROR;
179 }
180
Igor Sysoev239baac2003-06-11 15:28:34 +0000181 if (acceptex(ls->fd, s, c->buffer->pos, ls->post_accept_buffer_size,
182 ls->socklen + 16, ls->socklen + 16,
183 &rcvd, (LPOVERLAPPED) &rev->ovlp) == 0)
184 {
Igor Sysoev73009772003-02-06 17:21:13 +0000185
186 err = ngx_socket_errno;
Igor Sysoev239baac2003-06-11 15:28:34 +0000187 if (err != WSA_IO_PENDING) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000188 ngx_log_error(NGX_LOG_ALERT, &ls->log, err,
Igor Sysoevc2068d02005-10-19 12:33:58 +0000189 "AcceptEx() %V falied", &ls->addr_text);
Igor Sysoev239baac2003-06-11 15:28:34 +0000190
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000191 ngx_close_posted_connection(c);
Igor Sysoev239baac2003-06-11 15:28:34 +0000192 return NGX_ERROR;
Igor Sysoev73009772003-02-06 17:21:13 +0000193 }
Igor Sysoev73009772003-02-06 17:21:13 +0000194 }
195 }
196
197 return NGX_OK;
198}
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000199
200
201static void
202ngx_close_posted_connection(ngx_connection_t *c)
203{
204 ngx_socket_t fd;
205
206 ngx_free_connection(c);
207
208 fd = c->fd;
209 c->fd = (ngx_socket_t) -1;
210
211 if (ngx_close_socket(fd) == -1) {
212 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
213 ngx_close_socket_n " failed");
214 }
215
216 if (c->pool) {
217 ngx_destroy_pool(c->pool);
218 }
219}
220
221
222u_char *
223ngx_acceptex_log_error(ngx_log_t *log, u_char *buf, size_t len)
224{
225 return ngx_snprintf(buf, len, " while posting AcceptEx() on %V", log->data);
226}