upstream nginx-0.7.31
[nginx.git] / nginx / src / event / ngx_event_connect.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_event_connect.h>
11
12
13 ngx_int_t
14 ngx_event_connect_peer(ngx_peer_connection_t *pc)
15 {
16     int                rc;
17     ngx_int_t          event;
18     ngx_err_t          err;
19     ngx_uint_t         level;
20     ngx_socket_t       s;
21     ngx_event_t       *rev, *wev;
22     ngx_connection_t  *c;
23
24     rc = pc->get(pc, pc->data);
25     if (rc != NGX_OK) {
26         return rc;
27     }
28
29     s = ngx_socket(pc->sockaddr->sa_family, SOCK_STREAM, 0);
30
31     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0, "socket %d", s);
32
33     if (s == -1) {
34         ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
35                       ngx_socket_n " failed");
36         return NGX_ERROR;
37     }
38
39
40     c = ngx_get_connection(s, pc->log);
41
42     if (c == NULL) {
43         if (ngx_close_socket(s) == -1) {
44             ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
45                           ngx_close_socket_n "failed");
46         }
47
48         return NGX_ERROR;
49     }
50
51     if (pc->rcvbuf) {
52         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
53                        (const void *) &pc->rcvbuf, sizeof(int)) == -1)
54         {
55             ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
56                           "setsockopt(SO_RCVBUF) failed");
57
58             ngx_free_connection(c);
59
60             if (ngx_close_socket(s) == -1) {
61                 ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
62                               ngx_close_socket_n " failed");
63             }
64
65             return NGX_ERROR;
66         }
67     }
68
69     if (ngx_nonblocking(s) == -1) {
70         ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
71                       ngx_nonblocking_n " failed");
72
73         ngx_free_connection(c);
74
75         if (ngx_close_socket(s) == -1) {
76             ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
77                           ngx_close_socket_n " failed");
78         }
79
80         return NGX_ERROR;
81     }
82
83     c->recv = ngx_recv;
84     c->send = ngx_send;
85     c->recv_chain = ngx_recv_chain;
86     c->send_chain = ngx_send_chain;
87
88     c->sendfile = 1;
89
90     c->log_error = pc->log_error;
91
92     if (pc->sockaddr->sa_family != AF_INET) {
93         c->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
94         c->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;
95
96 #if (NGX_SOLARIS)
97         /* Solaris's sendfilev() supports AF_NCA, AF_INET, and AF_INET6 */
98         c->sendfile = 0;
99 #endif
100     }
101
102     rev = c->read;
103     wev = c->write;
104
105     rev->log = pc->log;
106     wev->log = pc->log;
107
108     pc->connection = c;
109
110     /*
111      * TODO: MT: - ngx_atomic_fetch_add()
112      *             or protection by critical section or mutex
113      *
114      * TODO: MP: - allocated in a shared memory
115      *           - ngx_atomic_fetch_add()
116      *             or protection by critical section or mutex
117      */
118
119     c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
120
121 #if (NGX_THREADS)
122     rev->lock = pc->lock;
123     wev->lock = pc->lock;
124     rev->own_lock = &c->lock;
125     wev->own_lock = &c->lock;
126 #endif
127
128     if (ngx_add_conn) {
129         if (ngx_add_conn(c) == NGX_ERROR) {
130             return NGX_ERROR;
131         }
132     }
133
134     ngx_log_debug3(NGX_LOG_DEBUG_EVENT, pc->log, 0,
135                    "connect to %V, fd:%d #%d", pc->name, s, c->number);
136
137     rc = connect(s, pc->sockaddr, pc->socklen);
138
139     if (rc == -1) {
140         err = ngx_socket_errno;
141
142
143         if (err != NGX_EINPROGRESS
144 #if (NGX_WIN32)
145             /* Winsock returns WSAEWOULDBLOCK (NGX_EAGAIN) */
146             && err != NGX_EAGAIN
147 #endif
148             )
149         {
150             if (err == NGX_ECONNREFUSED
151 #if (NGX_LINUX)
152                 /*
153                  * Linux returns EAGAIN instead of ECONNREFUSED
154                  * for unix sockets if listen queue is full
155                  */
156                 || err == NGX_EAGAIN
157 #endif
158                 || err == NGX_ENETDOWN
159                 || err == NGX_ENETUNREACH
160                 || err == NGX_EHOSTDOWN
161                 || err == NGX_EHOSTUNREACH)
162             {
163                 level = NGX_LOG_ERR;
164
165             } else {
166                 level = NGX_LOG_CRIT;
167             }
168
169             ngx_log_error(level, c->log, err, "connect() to %V failed",
170                           pc->name);
171
172             return NGX_DECLINED;
173         }
174     }
175
176     if (ngx_add_conn) {
177         if (rc == -1) {
178
179             /* NGX_EINPROGRESS */
180
181             return NGX_AGAIN;
182         }
183
184         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "connected");
185
186         wev->ready = 1;
187
188         return NGX_OK;
189     }
190
191     if (ngx_event_flags & NGX_USE_AIO_EVENT) {
192
193         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, ngx_socket_errno,
194                        "connect(): %d", rc);
195
196         /* aio, iocp */
197
198         if (ngx_blocking(s) == -1) {
199             ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
200                           ngx_blocking_n " failed");
201             return NGX_ERROR;
202         }
203
204         /*
205          * FreeBSD's aio allows to post an operation on non-connected socket.
206          * NT does not support it.
207          *
208          * TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT
209          */
210
211         rev->ready = 1;
212         wev->ready = 1;
213
214         return NGX_OK;
215     }
216
217     if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
218
219         /* kqueue */
220
221         event = NGX_CLEAR_EVENT;
222
223     } else {
224
225         /* select, poll, /dev/poll */
226
227         event = NGX_LEVEL_EVENT;
228     }
229
230     if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
231         return NGX_ERROR;
232     }
233
234     if (rc == -1) {
235
236         /* NGX_EINPROGRESS */
237
238         if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) {
239             return NGX_ERROR;
240         }
241
242         return NGX_AGAIN;
243     }
244
245     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "connected");
246
247     wev->ready = 1;
248
249     return NGX_OK;
250 }
251
252
253 ngx_int_t
254 ngx_event_get_peer(ngx_peer_connection_t *pc, void *data)
255 {
256     return NGX_OK;
257 }