xref: /unit/src/nxt_listen_socket.c (revision 1450:d431a2d91caa)
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
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
30 nxt_listen_socket_create(nxt_task_t *task, nxt_listen_socket_t *ls)
31 {
32     nxt_log_t       log, *old;
33     nxt_uint_t      family;
34     nxt_socket_t    s;
35     nxt_thread_t    *thr;
36     nxt_sockaddr_t  *sa;
37 
38     sa = ls->sockaddr;
39 
40     thr = nxt_thread();
41     old = thr->log;
42     log = *thr->log;
43     log.ctx_handler = nxt_listen_socket_log_handler;
44     log.ctx = sa;
45     thr->log = &log;
46 
47     family = sa->u.sockaddr.sa_family;
48 
49     s = nxt_socket_create(task, family, sa->type, 0, ls->flags);
50     if (s == -1) {
51         goto fail;
52     }
53 
54     if (nxt_socket_setsockopt(task, s, SOL_SOCKET, SO_REUSEADDR, 1) != NXT_OK) {
55         goto fail;
56     }
57 
58 #if (NXT_INET6 && defined IPV6_V6ONLY)
59 
60     if (family == AF_INET6 && ls->ipv6only) {
61         int  ipv6only;
62 
63         ipv6only = (ls->ipv6only == 1);
64 
65         /* Ignore possible error. TODO: why? */
66         (void) nxt_socket_setsockopt(task, s, IPPROTO_IPV6, IPV6_V6ONLY,
67                                      ipv6only);
68     }
69 
70 #endif
71 
72 #if 0
73 
74     /* Ignore possible error. TODO: why? */
75     (void) nxt_socket_setsockopt(task, s, SOL_SOCKET, SO_SNDBUF, 8192);
76 
77 #endif
78 
79     if (ls->read_after_accept) {
80         nxt_socket_defer_accept(task, s, sa);
81     }
82 
83     if (nxt_socket_bind(task, s, sa) != NXT_OK) {
84         goto fail;
85     }
86 
87 #if (NXT_HAVE_UNIX_DOMAIN)
88 
89     if (family == AF_UNIX) {
90         nxt_file_name_t     *name;
91         nxt_file_access_t   access;
92 
93         name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path;
94 
95         access = (S_IRUSR | S_IWUSR);
96 
97         if (nxt_file_set_access(name, access) != NXT_OK) {
98             goto listen_fail;
99         }
100     }
101 
102 #endif
103 
104     nxt_debug(task, "listen(%d, %d)", s, ls->backlog);
105 
106     if (listen(s, ls->backlog) != 0) {
107         nxt_alert(task, "listen(%d, %d) failed %E",
108                   s, ls->backlog, nxt_socket_errno);
109         goto listen_fail;
110     }
111 
112     ls->socket = s;
113     thr->log = old;
114 
115     return NXT_OK;
116 
117 listen_fail:
118 
119 #if (NXT_HAVE_UNIX_DOMAIN)
120 
121     if (family == AF_UNIX) {
122         nxt_file_name_t  *name;
123 
124         name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path;
125 
126         (void) nxt_file_delete(name);
127     }
128 
129 #endif
130 
131 fail:
132 
133     if (s != -1) {
134         nxt_socket_close(task, s);
135     }
136 
137     thr->log = old;
138 
139     return NXT_ERROR;
140 }
141 
142 
143 nxt_int_t
144 nxt_listen_socket_update(nxt_task_t *task, nxt_listen_socket_t *ls,
145     nxt_listen_socket_t *prev)
146 {
147     nxt_log_t     log, *old;
148     nxt_thread_t  *thr;
149 
150     ls->socket = prev->socket;
151 
152     thr = nxt_thread();
153     old = thr->log;
154     log = *thr->log;
155     log.ctx_handler = nxt_listen_socket_log_handler;
156     log.ctx = ls->sockaddr;
157     thr->log = &log;
158 
159     nxt_debug(task, "listen(%d, %d)", ls->socket, ls->backlog);
160 
161     if (listen(ls->socket, ls->backlog) != 0) {
162         nxt_alert(task, "listen(%d, %d) failed %E",
163                   ls->socket, ls->backlog, nxt_socket_errno);
164         goto fail;
165     }
166 
167     thr->log = old;
168 
169     return NXT_OK;
170 
171 fail:
172 
173     thr->log = old;
174 
175     return NXT_ERROR;
176 }
177 
178 
179 void
180 nxt_listen_socket_remote_size(nxt_listen_socket_t *ls)
181 {
182     switch (ls->sockaddr->u.sockaddr.sa_family) {
183 
184 #if (NXT_INET6)
185 
186     case AF_INET6:
187         ls->socklen = sizeof(struct sockaddr_in6);
188         ls->address_length = NXT_INET6_ADDR_STR_LEN;
189 
190         break;
191 
192 #endif
193 
194 #if (NXT_HAVE_UNIX_DOMAIN)
195 
196     case AF_UNIX:
197         /*
198          * A remote socket is usually unbound and thus has unspecified Unix
199          * domain sockaddr_un which can be shortcut to 3 bytes.  To handle
200          * a bound remote socket correctly ls->socklen should be larger, see
201          * comment in nxt_socket.h.
202          */
203         ls->socklen = offsetof(struct sockaddr_un, sun_path) + 1;
204         ls->address_length = nxt_length("unix:");
205 
206         break;
207 
208 #endif
209 
210     default:
211     case AF_INET:
212         ls->socklen = sizeof(struct sockaddr_in);
213         ls->address_length = NXT_INET_ADDR_STR_LEN;
214 
215         break;
216     }
217 }
218 
219 
220 size_t
221 nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls)
222 {
223     size_t  size;
224 
225     /*
226      * The first nxt_sockaddr_t is intended for mandatory remote sockaddr
227      * and textual representaion with port.  The second nxt_sockaddr_t
228      * is intended for local sockaddr without textual representaion which
229      * may be required to get specific address of connection received on
230      * wildcard AF_INET and AF_INET6 addresses.  For AF_UNIX addresses
231      * the local sockaddr is not required.
232      */
233 
234     switch (ls->sockaddr->u.sockaddr.sa_family) {
235 
236 #if (NXT_INET6)
237 
238     case AF_INET6:
239         ls->socklen = sizeof(struct sockaddr_in6);
240         ls->address_length = NXT_INET6_ADDR_STR_LEN;
241 
242         size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in6)
243                + NXT_INET6_ADDR_STR_LEN + nxt_length(":65535");
244 
245         if (IN6_IS_ADDR_UNSPECIFIED(&ls->sockaddr->u.sockaddr_in6.sin6_addr)) {
246             size += offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in6);
247         }
248 
249         break;
250 
251 #endif
252 
253 #if (NXT_HAVE_UNIX_DOMAIN)
254 
255     case AF_UNIX:
256         /*
257          * A remote socket is usually unbound and thus has unspecified Unix
258          * domain sockaddr_un which can be shortcut to 3 bytes.  To handle
259          * a bound remote socket correctly ls->socklen should be at least
260          * sizeof(struct sockaddr_un), see comment in nxt_socket.h.
261          */
262         ls->socklen = 3;
263         size = ls->socklen + nxt_length("unix:");
264         ls->address_length = nxt_length("unix:");
265 
266         break;
267 
268 #endif
269 
270     default:
271         ls->socklen = sizeof(struct sockaddr_in);
272         ls->address_length = NXT_INET_ADDR_STR_LEN;
273 
274         size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in)
275                + NXT_INET_ADDR_STR_LEN + nxt_length(":65535");
276 
277         if (ls->sockaddr->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) {
278             size += offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in);
279         }
280 
281         break;
282     }
283 
284 #if (NXT_TLS)
285 
286     if (ls->tls) {
287         size += 4 * sizeof(void *)       /* SSL/TLS connection */
288                 + sizeof(nxt_buf_mem_t)
289                 + sizeof(nxt_work_t);    /* nxt_mp_cleanup */
290     }
291 
292 #endif
293 
294     return size // + sizeof(nxt_mem_pool_t)
295                 + sizeof(nxt_conn_t)
296                 + sizeof(nxt_log_t);
297 }
298 
299 
300 static u_char *
301 nxt_listen_socket_log_handler(void *ctx, u_char *pos, u_char *end)
302 {
303     nxt_sockaddr_t  *sa;
304 
305     sa = ctx;
306 
307     return nxt_sprintf(pos, end, " while creating listening socket on %*s",
308                        (size_t) sa->length, nxt_sockaddr_start(sa));
309 }
310