xref: /unit/src/nxt_listen_socket.c (revision 1451:0a8840921fd0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 static u_char *nxt_listen_socket_log_handler(void *ctx, u_char *pos,
11     u_char *last);
12 
13 
14 nxt_int_t
nxt_listen_socket(nxt_task_t * task,nxt_socket_t s,int backlog)15 nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, int backlog)
16 {
17     nxt_debug(task, "listen(%d, %d)", s, backlog);
18 
19     if (nxt_fast_path(listen(s, backlog) == 0)) {
20         return NXT_OK;
21     }
22 
23     nxt_alert(task, "listen(%d, %d) failed %E", s, backlog, nxt_socket_errno);
24 
25     return NXT_ERROR;
26 }
27 
28 
29 nxt_int_t
nxt_listen_socket_create(nxt_task_t * task,nxt_mp_t * mp,nxt_listen_socket_t * ls)30 nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp,
31     nxt_listen_socket_t *ls)
32 {
33     nxt_log_t          log, *old;
34     nxt_uint_t         family;
35     nxt_socket_t       s;
36     nxt_thread_t       *thr;
37     nxt_sockaddr_t     *sa;
38 #if (NXT_HAVE_UNIX_DOMAIN)
39     int                ret;
40     u_char             *p;
41     nxt_err_t          err;
42     nxt_socket_t       ts;
43     nxt_sockaddr_t     *orig_sa;
44     nxt_file_name_t    *name, *tmp;
45     nxt_file_access_t  access;
46 #endif
47 
48     sa = ls->sockaddr;
49 
50     thr = nxt_thread();
51     old = thr->log;
52     log = *thr->log;
53     log.ctx_handler = nxt_listen_socket_log_handler;
54     log.ctx = sa;
55     thr->log = &log;
56 
57     family = sa->u.sockaddr.sa_family;
58 
59     s = nxt_socket_create(task, family, sa->type, 0, ls->flags);
60     if (s == -1) {
61         goto fail;
62     }
63 
64     if (nxt_socket_setsockopt(task, s, SOL_SOCKET, SO_REUSEADDR, 1) != NXT_OK) {
65         goto fail;
66     }
67 
68 #if (NXT_INET6 && defined IPV6_V6ONLY)
69 
70     if (family == AF_INET6 && ls->ipv6only) {
71         int  ipv6only;
72 
73         ipv6only = (ls->ipv6only == 1);
74 
75         /* Ignore possible error. TODO: why? */
76         (void) nxt_socket_setsockopt(task, s, IPPROTO_IPV6, IPV6_V6ONLY,
77                                      ipv6only);
78     }
79 
80 #endif
81 
82 #if 0
83 
84     /* Ignore possible error. TODO: why? */
85     (void) nxt_socket_setsockopt(task, s, SOL_SOCKET, SO_SNDBUF, 8192);
86 
87 #endif
88 
89     if (ls->read_after_accept) {
90         nxt_socket_defer_accept(task, s, sa);
91     }
92 
93 #if (NXT_HAVE_UNIX_DOMAIN)
94 
95     if (family == AF_UNIX
96         && sa->type == SOCK_STREAM
97         && sa->u.sockaddr_un.sun_path[0] != '\0')
98     {
99         orig_sa = sa;
100 
101         sa = nxt_sockaddr_alloc(mp, sa->socklen + 4, sa->length + 4);
102         if (sa == NULL) {
103             goto fail;
104         }
105 
106         sa->type = SOCK_STREAM;
107         sa->u.sockaddr_un.sun_family = AF_UNIX;
108 
109         p = nxt_cpystr((u_char *) sa->u.sockaddr_un.sun_path,
110                        (u_char *) orig_sa->u.sockaddr_un.sun_path);
111         nxt_memcpy(p, ".tmp", 4);
112 
113         nxt_sockaddr_text(sa);
114 
115         (void) unlink(sa->u.sockaddr_un.sun_path);
116 
117     } else {
118         orig_sa = NULL;
119     }
120 
121 #endif
122 
123     if (nxt_socket_bind(task, s, sa) != NXT_OK) {
124         goto fail;
125     }
126 
127 #if (NXT_HAVE_UNIX_DOMAIN)
128 
129     if (family == AF_UNIX) {
130         name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path;
131 
132         access = (S_IRUSR | S_IWUSR);
133 
134         if (nxt_file_set_access(name, access) != NXT_OK) {
135             goto listen_fail;
136         }
137     }
138 
139 #endif
140 
141     nxt_debug(task, "listen(%d, %d)", s, ls->backlog);
142 
143     if (listen(s, ls->backlog) != 0) {
144         nxt_alert(task, "listen(%d, %d) failed %E",
145                   s, ls->backlog, nxt_socket_errno);
146         goto listen_fail;
147     }
148 
149 #if (NXT_HAVE_UNIX_DOMAIN)
150 
151     if (orig_sa != NULL) {
152         ts = nxt_socket_create(task, AF_UNIX, SOCK_STREAM, 0, 0);
153         if (ts == -1) {
154             goto listen_fail;
155         }
156 
157         ret = connect(ts, &orig_sa->u.sockaddr, orig_sa->socklen);
158 
159         err = nxt_socket_errno;
160 
161         nxt_socket_close(task, ts);
162 
163         if (ret == 0) {
164             nxt_alert(task, "connect(%d, %*s) succeed, address already in use",
165                       ts, (size_t) orig_sa->length,
166                       nxt_sockaddr_start(orig_sa));
167 
168             goto listen_fail;
169         }
170 
171         if (err != NXT_ENOENT && err != NXT_ECONNREFUSED) {
172             nxt_alert(task, "connect(%d, %*s) failed %E",
173                       ts, (size_t) orig_sa->length,
174                       nxt_sockaddr_start(orig_sa), err);
175 
176             goto listen_fail;
177         }
178 
179         tmp = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path;
180         name = (nxt_file_name_t *) orig_sa->u.sockaddr_un.sun_path;
181 
182         if (nxt_file_rename(tmp, name) != NXT_OK) {
183             goto listen_fail;
184         }
185     }
186 
187 #endif
188 
189     ls->socket = s;
190     thr->log = old;
191 
192     return NXT_OK;
193 
194 listen_fail:
195 
196 #if (NXT_HAVE_UNIX_DOMAIN)
197 
198     if (family == AF_UNIX) {
199         name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path;
200 
201         (void) nxt_file_delete(name);
202     }
203 
204 #endif
205 
206 fail:
207 
208     if (s != -1) {
209         nxt_socket_close(task, s);
210     }
211 
212     thr->log = old;
213 
214     return NXT_ERROR;
215 }
216 
217 
218 nxt_int_t
nxt_listen_socket_update(nxt_task_t * task,nxt_listen_socket_t * ls,nxt_listen_socket_t * prev)219 nxt_listen_socket_update(nxt_task_t *task, nxt_listen_socket_t *ls,
220     nxt_listen_socket_t *prev)
221 {
222     nxt_log_t     log, *old;
223     nxt_thread_t  *thr;
224 
225     ls->socket = prev->socket;
226 
227     thr = nxt_thread();
228     old = thr->log;
229     log = *thr->log;
230     log.ctx_handler = nxt_listen_socket_log_handler;
231     log.ctx = ls->sockaddr;
232     thr->log = &log;
233 
234     nxt_debug(task, "listen(%d, %d)", ls->socket, ls->backlog);
235 
236     if (listen(ls->socket, ls->backlog) != 0) {
237         nxt_alert(task, "listen(%d, %d) failed %E",
238                   ls->socket, ls->backlog, nxt_socket_errno);
239         goto fail;
240     }
241 
242     thr->log = old;
243 
244     return NXT_OK;
245 
246 fail:
247 
248     thr->log = old;
249 
250     return NXT_ERROR;
251 }
252 
253 
254 void
nxt_listen_socket_remote_size(nxt_listen_socket_t * ls)255 nxt_listen_socket_remote_size(nxt_listen_socket_t *ls)
256 {
257     switch (ls->sockaddr->u.sockaddr.sa_family) {
258 
259 #if (NXT_INET6)
260 
261     case AF_INET6:
262         ls->socklen = sizeof(struct sockaddr_in6);
263         ls->address_length = NXT_INET6_ADDR_STR_LEN;
264 
265         break;
266 
267 #endif
268 
269 #if (NXT_HAVE_UNIX_DOMAIN)
270 
271     case AF_UNIX:
272         /*
273          * A remote socket is usually unbound and thus has unspecified Unix
274          * domain sockaddr_un which can be shortcut to 3 bytes.  To handle
275          * a bound remote socket correctly ls->socklen should be larger, see
276          * comment in nxt_socket.h.
277          */
278         ls->socklen = offsetof(struct sockaddr_un, sun_path) + 1;
279         ls->address_length = nxt_length("unix:");
280 
281         break;
282 
283 #endif
284 
285     default:
286     case AF_INET:
287         ls->socklen = sizeof(struct sockaddr_in);
288         ls->address_length = NXT_INET_ADDR_STR_LEN;
289 
290         break;
291     }
292 }
293 
294 
295 size_t
nxt_listen_socket_pool_min_size(nxt_listen_socket_t * ls)296 nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls)
297 {
298     size_t  size;
299 
300     /*
301      * The first nxt_sockaddr_t is intended for mandatory remote sockaddr
302      * and textual representaion with port.  The second nxt_sockaddr_t
303      * is intended for local sockaddr without textual representaion which
304      * may be required to get specific address of connection received on
305      * wildcard AF_INET and AF_INET6 addresses.  For AF_UNIX addresses
306      * the local sockaddr is not required.
307      */
308 
309     switch (ls->sockaddr->u.sockaddr.sa_family) {
310 
311 #if (NXT_INET6)
312 
313     case AF_INET6:
314         ls->socklen = sizeof(struct sockaddr_in6);
315         ls->address_length = NXT_INET6_ADDR_STR_LEN;
316 
317         size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in6)
318                + NXT_INET6_ADDR_STR_LEN + nxt_length(":65535");
319 
320         if (IN6_IS_ADDR_UNSPECIFIED(&ls->sockaddr->u.sockaddr_in6.sin6_addr)) {
321             size += offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in6);
322         }
323 
324         break;
325 
326 #endif
327 
328 #if (NXT_HAVE_UNIX_DOMAIN)
329 
330     case AF_UNIX:
331         /*
332          * A remote socket is usually unbound and thus has unspecified Unix
333          * domain sockaddr_un which can be shortcut to 3 bytes.  To handle
334          * a bound remote socket correctly ls->socklen should be at least
335          * sizeof(struct sockaddr_un), see comment in nxt_socket.h.
336          */
337         ls->socklen = 3;
338         size = ls->socklen + nxt_length("unix:");
339         ls->address_length = nxt_length("unix:");
340 
341         break;
342 
343 #endif
344 
345     default:
346         ls->socklen = sizeof(struct sockaddr_in);
347         ls->address_length = NXT_INET_ADDR_STR_LEN;
348 
349         size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in)
350                + NXT_INET_ADDR_STR_LEN + nxt_length(":65535");
351 
352         if (ls->sockaddr->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) {
353             size += offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in);
354         }
355 
356         break;
357     }
358 
359 #if (NXT_TLS)
360 
361     if (ls->tls) {
362         size += 4 * sizeof(void *)       /* SSL/TLS connection */
363                 + sizeof(nxt_buf_mem_t)
364                 + sizeof(nxt_work_t);    /* nxt_mp_cleanup */
365     }
366 
367 #endif
368 
369     return size // + sizeof(nxt_mem_pool_t)
370                 + sizeof(nxt_conn_t)
371                 + sizeof(nxt_log_t);
372 }
373 
374 
375 static u_char *
nxt_listen_socket_log_handler(void * ctx,u_char * pos,u_char * end)376 nxt_listen_socket_log_handler(void *ctx, u_char *pos, u_char *end)
377 {
378     nxt_sockaddr_t  *sa;
379 
380     sa = ctx;
381 
382     return nxt_sprintf(pos, end, " while creating listening socket on %*s",
383                        (size_t) sa->length, nxt_sockaddr_start(sa));
384 }
385