vinyl-cache/lib/libvinyl/vtcp.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include <sys/types.h>
34
#include <sys/socket.h>
35
#include <sys/time.h>           // for NetBSD
36
#include <sys/ioctl.h>
37
#ifdef HAVE_SYS_FILIO_H
38
#  include <sys/filio.h>
39
#endif
40
41
#include <netinet/in.h>
42
#include <netinet/tcp.h>
43
44
#include <math.h>
45
#include <netdb.h>
46
#include <poll.h>
47
#include <stdio.h>
48
#include <string.h>
49
#include <unistd.h>
50
#include <stdlib.h>
51
52
#include "vdef.h"
53
#include "miniobj.h"
54
#include "vas.h"
55
#include "vsa.h"
56
#include "vss.h"
57
#include "vtcp.h"
58
#include "vtim.h"
59
60
/*--------------------------------------------------------------------*/
61
static void
62 89857
vtcp_sa_to_ascii(const void *sa, socklen_t l, char *abuf, unsigned alen,
63
    char *pbuf, unsigned plen)
64
{
65
        int i;
66
67 89857
        assert(abuf == NULL || alen > 0);
68 89857
        assert(pbuf == NULL || plen > 0);
69 89857
        i = getnameinfo(sa, l, abuf, alen, pbuf, plen,
70
           NI_NUMERICHOST | NI_NUMERICSERV);
71 89857
        if (i) {
72
                /*
73
                 * XXX this printf is shitty, but we may not have space
74
                 * for the gai_strerror in the buffer :-(
75
                 */
76 0
                fprintf(stderr, "getnameinfo = %d %s\n", i, gai_strerror(i));
77 0
                if (i == EAI_SYSTEM)
78 0
                        fprintf(stderr, "errno = %d %s\n", errno, VAS_errtxt(errno));
79 0
                if (abuf != NULL)
80 0
                        (void)snprintf(abuf, alen, "Conversion");
81 0
                if (pbuf != NULL)
82 0
                        (void)snprintf(pbuf, plen, "Failed");
83 0
                return;
84
        }
85 89857
}
86
87
/*--------------------------------------------------------------------*/
88
89
void
90 89857
VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen,
91
    char *pbuf, unsigned plen)
92
{
93
        const struct sockaddr *sa;
94
        socklen_t sl;
95
96 89857
        sa = VSA_Get_Sockaddr(addr, &sl);
97 89857
        AN(sa);
98 89857
        vtcp_sa_to_ascii(sa, sl, abuf, alen, pbuf, plen);
99 89857
}
100
101
/*--------------------------------------------------------------------*/
102
103
struct suckaddr *
104 3036
VTCP_my_suckaddr(int sock)
105
{
106
        struct suckaddr *r;
107
108 3036
        r = malloc(vsa_suckaddr_len);
109 3036
        AN(VSA_getsockname(sock, r, vsa_suckaddr_len));
110 3036
        return (r);
111
}
112
113
/*--------------------------------------------------------------------*/
114
115
void
116 39728
VTCP_myname(int sock, char *abuf, unsigned alen, char *pbuf, unsigned plen)
117
{
118 39728
        char buf[vsa_suckaddr_len];
119
120 79456
        VTCP_name(VSA_getsockname(sock, buf, sizeof buf),
121 39728
                  abuf, alen, pbuf, plen);
122 39728
}
123
124
/*--------------------------------------------------------------------*/
125
126
void
127 24945
VTCP_hisname(int sock, char *abuf, unsigned alen, char *pbuf, unsigned plen)
128
{
129 24945
        char buf[vsa_suckaddr_len];
130
        const struct suckaddr *sua;
131
132 24945
        sua = VSA_getpeername(sock, buf, sizeof buf);
133 24945
        if (sua != NULL)
134 24945
                VTCP_name(sua, abuf, alen, pbuf, plen);
135
        else {
136 0
                (void)snprintf(abuf, alen, "<none>");
137 0
                (void)snprintf(pbuf, plen, "<none>");
138
        }
139 24945
}
140
141
/*--------------------------------------------------------------------*/
142
143
#ifdef HAVE_ACCEPT_FILTERS
144
145
int
146 2907
VTCP_filter_http(int sock)
147
{
148
        int retval;
149
        struct accept_filter_arg afa;
150
151 2907
        memset(&afa, 0, sizeof afa);
152 2907
        bprintf(afa.af_name, "%s", "httpready");
153 2907
        retval = setsockopt(sock, SOL_SOCKET, SO_ACCEPTFILTER,
154
            &afa, sizeof afa);
155 2907
        return (retval);
156
}
157
158
#elif defined(__linux)
159
160
int
161
VTCP_filter_http(int sock)
162
{
163
        int retval;
164
        int defer = 1;
165
166
        retval = setsockopt(sock, SOL_TCP, TCP_DEFER_ACCEPT,
167
            &defer, sizeof defer);
168
        return (retval);
169
}
170
171
#else
172
173
int
174
VTCP_filter_http(int sock)
175
{
176
        errno = EOPNOTSUPP;
177
        (void)sock;
178
        return (-1);
179
}
180
181
#endif
182
183
/*--------------------------------------------------------------------*/
184
185
186
int
187 0
VTCP_fastopen(int sock, int depth)
188
{
189
#ifdef HAVE_TCP_FASTOPEN
190
#  ifndef SOL_TCP
191
#    define SOL_TCP IPPROTO_TCP
192
#  endif
193 0
        return (setsockopt(sock, SOL_TCP, TCP_FASTOPEN, &depth, sizeof depth));
194
#else
195
        errno = EOPNOTSUPP;
196
        (void)sock;
197
        (void)depth;
198
        return (-1);
199
#endif
200
}
201
202
203
/*--------------------------------------------------------------------
204
 * Functions for controlling NONBLOCK mode.
205
 *
206
 * We use FIONBIO because it is cheaper than fcntl(2), which requires
207
 * us to do two syscalls, one to get and one to set, the latter of
208
 * which mucks about a bit before it ends up calling ioctl(FIONBIO),
209
 * at least on FreeBSD.
210
 * On Solaris ioctl(FIONBIO) can fail with connection related errnos,
211
 * but as long as that is how they fail, we're fine.
212
 */
213
214
void
215 45208
VTCP_blocking(int sock)
216
{
217
        int i, j;
218
219 45208
        i = 0;
220 45208
        j = ioctl(sock, FIONBIO, &i);
221 45208
        VTCP_Assert(j);
222 45208
}
223
224
void
225 15933
VTCP_nonblocking(int sock)
226
{
227
        int i, j;
228
229 15933
        i = 1;
230 15933
        j = ioctl(sock, FIONBIO, &i);
231 15933
        VTCP_Assert(j);
232 15933
}
233
234
/*--------------------------------------------------------------------
235
 * On TCP a connect(2) can block for a looong time, and we don't want that.
236
 * Unfortunately, the SocketWizards back in those days were happy to wait
237
 * any amount of time for a connection, so the connect(2) syscall does not
238
 * take an argument for patience.
239
 *
240
 * There is a little used work-around, and we employ it at our peril.
241
 *
242
 */
243
244
int
245 13767
VTCP_connected(int s)
246
{
247
        int k;
248
        socklen_t l;
249
250
        /* Find out if we got a connection */
251 13767
        l = sizeof k;
252 13767
        AZ(getsockopt(s, SOL_SOCKET, SO_ERROR, &k, &l));
253
254
        /* An error means no connection established */
255 13767
        errno = k;
256 13767
        if (k) {
257 76
                closefd(&s);
258 76
                return (-1);
259
        }
260
261 13691
        VTCP_blocking(s);
262 13691
        return (s);
263 13767
}
264
265
int
266 14465
VTCP_connect(const struct suckaddr *name, int msec)
267
{
268
        int s, i;
269
        struct pollfd fds[1];
270
        const struct sockaddr *sa;
271
        socklen_t sl;
272
        int val;
273
274 14465
        if (name == NULL)
275 85
                return (-1);
276
        /* Attempt the connect */
277 14380
        AN(VSA_Sane(name));
278 14380
        sa = VSA_Get_Sockaddr(name, &sl);
279 14380
        AN(sa);
280 14380
        AN(sl);
281
282 14380
        s = socket(sa->sa_family, SOCK_STREAM, 0);
283 14380
        if (s < 0)
284 0
                return (s);
285
286
        /* Set the socket non-blocking */
287 14380
        if (msec != 0)
288 13772
                VTCP_nonblocking(s);
289
290 14380
        val = 1;
291 14380
        AZ(setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val));
292
293 14380
        i = connect(s, sa, sl);
294 14380
        if (i == 0 && msec > 0)
295 0
                VTCP_blocking(s);
296 14380
        if (i == 0)
297 609
                return (s);
298 13771
        if (errno != EINPROGRESS) {
299 0
                closefd(&s);
300 0
                return (-1);
301
        }
302
303 13771
        if (msec < 0) {
304
                /*
305
                 * Caller is responsible for waiting and
306
                 * calling VTCP_connected
307
                 */
308 2982
                return (s);
309
        }
310
311 10789
        assert(msec > 0);
312
        /* Exercise our patience, polling for write */
313 10789
        fds[0].fd = s;
314 10789
        fds[0].events = POLLWRNORM;
315 10789
        fds[0].revents = 0;
316 10789
        i = poll(fds, 1, msec);
317
318 10789
        if (i == 0) {
319
                /* Timeout, close and give up */
320 3
                closefd(&s);
321 3
                errno = ETIMEDOUT;
322 3
                return (-1);
323
        }
324
325 10786
        return (VTCP_connected(s));
326 14465
}
327
328
/*--------------------------------------------------------------------
329
 * When closing a TCP connection, a couple of errno's are legit, we
330
 * can't be held responsible for the other end wanting to talk to us.
331
 */
332
333
void
334 20471
VTCP_close(int *s)
335
{
336
        int i;
337
338 20471
        i = close(*s);
339
340 20471
        VTCP_Assert(i);
341 20471
        *s = -1;
342 20471
}
343
344
void
345 6435
VTCP_set_read_timeout(int s, vtim_dur seconds)
346
{
347 6435
        struct timeval timeout = VTIM_timeval_sock(seconds);
348
349
        /*
350
         * Solaris bug (present at least in snv_151 and older): If this fails
351
         * with EINVAL, the socket is half-closed (SS_CANTSENDMORE) and the
352
         * timeout does not get set. Needs to be fixed in Solaris, there is
353
         * nothing we can do about this.
354
         */
355 6435
        VTCP_Assert(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
356
            &timeout, sizeof timeout));
357 6435
}
358
359
/*--------------------------------------------------------------------
360
 */
361
362
struct vto_priv {
363
        unsigned                magic;
364
#define VTO_PRIV_MAGIC          0xca70b0e7
365
        int                     latest_errno;
366
        int                     fd;
367
        double                  timeout;
368
};
369
370
static int v_matchproto_(vss_resolved_f)
371 6867
vtcp_open_callback(void *priv, const struct suckaddr *sa)
372
{
373
        struct vto_priv *vto;
374
        int fd;
375
376 6867
        CAST_OBJ_NOTNULL(vto, priv, VTO_PRIV_MAGIC);
377
378 6867
        errno = 0;
379 6867
        fd = VTCP_connect(sa, (int)floor(vto->timeout * 1e3));
380 6867
        if (fd >= 0) {
381 6864
                vto->fd = fd;
382 6864
                vto->latest_errno = 0;
383 6864
                return (1);
384
        }
385 3
        vto->latest_errno = errno;
386 3
        return (0);
387 6867
}
388
389
int
390 6865
VTCP_open(const char *addr, const char *def_port, vtim_dur timeout,
391
    const char **errp)
392
{
393
        struct vto_priv vto[1];
394
395 6865
        AN(errp);
396 6865
        assert(timeout >= 0);
397 6865
        INIT_OBJ(vto, VTO_PRIV_MAGIC);
398 6865
        vto->fd = -1;
399 6865
        vto->timeout = timeout;
400
401 6865
        if (VSS_resolver(addr, def_port, vtcp_open_callback, vto, errp) < 0)
402 0
                return (-1);
403 6865
        if (vto->fd < 0)
404 3
                *errp = strerror(vto->latest_errno);
405 6865
        return (vto->fd);
406 6865
}
407
408
/*--------------------------------------------------------------------
409
 * Given a struct suckaddr, open a socket of the appropriate type, and bind
410
 * it to the requested address.
411
 *
412
 * If the address is an IPv6 address, the IPV6_V6ONLY option is set to
413
 * avoid conflicts between INADDR_ANY and IN6ADDR_ANY.
414
 */
415
416
int
417 28344
VTCP_bind(const struct suckaddr *sa, const char **errp)
418
{
419
        int sd, val, e;
420
        socklen_t sl;
421
        const struct sockaddr *so;
422
        int proto;
423
424 28344
        if (errp != NULL)
425 15927
                *errp = NULL;
426
427 28344
        proto = VSA_Get_Proto(sa);
428 28344
        sd = socket(proto, SOCK_STREAM, 0);
429 28344
        if (sd < 0) {
430 0
                if (errp != NULL)
431 0
                        *errp = "socket(2)";
432 0
                return (-1);
433
        }
434 28344
        val = 1;
435 28344
        if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) != 0) {
436 0
                if (errp != NULL)
437 0
                        *errp = "setsockopt(SO_REUSEADDR, 1)";
438 0
                e = errno;
439 0
                closefd(&sd);
440 0
                errno = e;
441 0
                return (-1);
442
        }
443
#ifdef IPV6_V6ONLY
444
        /* forcibly use separate sockets for IPv4 and IPv6 */
445 28344
        val = 1;
446 28344
        if (proto == AF_INET6 &&
447 6429
            setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val) != 0) {
448 0
                if (errp != NULL)
449 0
                        *errp = "setsockopt(IPV6_V6ONLY, 1)";
450 0
                e = errno;
451 0
                closefd(&sd);
452 0
                errno = e;
453 0
                return (-1);
454
        }
455
#endif
456 28344
        so = VSA_Get_Sockaddr(sa, &sl);
457 28344
        if (bind(sd, so, sl) != 0) {
458 3
                if (errp != NULL)
459 0
                        *errp = "bind(2)";
460 3
                e = errno;
461 3
                closefd(&sd);
462 3
                errno = e;
463 3
                return (-1);
464
        }
465 28341
        return (sd);
466 28344
}
467
468
/*--------------------------------------------------------------------
469
 * Given a struct suckaddr, open a socket of the appropriate type, bind it
470
 * to the requested address, and start listening.
471
 */
472
473
int
474 15927
VTCP_listen(const struct suckaddr *sa, int depth, const char **errp)
475
{
476
        int sd;
477
        int e;
478
479 15927
        if (errp != NULL)
480 15927
                *errp = NULL;
481 15927
        sd = VTCP_bind(sa, errp);
482 15927
        if (sd >= 0)  {
483 15927
                if (listen(sd, depth) != 0) {
484 0
                        e = errno;
485 0
                        closefd(&sd);
486 0
                        errno = e;
487 0
                        if (errp != NULL)
488 0
                                *errp = "listen(2)";
489 0
                        return (-1);
490
                }
491 15927
        }
492 15927
        return (sd);
493 15927
}
494
495
/*--------------------------------------------------------------------*/
496
497
struct helper {
498
        int             depth;
499
        const char      **errp;
500
};
501
502
static int v_matchproto_(vss_resolved_f)
503 9867
vtcp_lo_cb(void *priv, const struct suckaddr *sa)
504
{
505
        int sock;
506 9867
        struct helper *hp = priv;
507
508 9867
        sock = VTCP_listen(sa, hp->depth, hp->errp);
509 9867
        if (sock >= 0) {
510 9867
                *hp->errp = NULL;
511 9867
                return (sock);
512
        }
513 0
        AN(*hp->errp);
514 0
        return (0);
515 9867
}
516
517
int
518 9867
VTCP_listen_on(const char *addr, const char *def_port, int depth,
519
    const char **errp)
520
{
521
        struct helper h;
522
        int sock;
523
524 9867
        AN(errp);
525 9867
        h.depth = depth;
526 9867
        h.errp = errp;
527
528 9867
        sock = VSS_resolver(addr, def_port, vtcp_lo_cb, &h, errp);
529 9867
        if (*errp != NULL)
530 0
                return (-1);
531 9867
        return (sock);
532 9867
}
533
534
/*--------------------------------------------------------------------
535
 * Set or reset SO_LINGER flag
536
 */
537
538
int
539 0
VTCP_linger(int sock, int linger)
540
{
541
        struct linger lin;
542
        int i;
543
544 0
        memset(&lin, 0, sizeof lin);
545 0
        lin.l_onoff = linger;
546 0
        i = setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof lin);
547 0
        VTCP_Assert(i);
548 0
        return (i);
549
}
550
551
/*--------------------------------------------------------------------
552
 * Do a poll to check for remote HUP
553
 */
554
555
int
556 0
VTCP_check_hup(int sock)
557
{
558
        struct pollfd pfd;
559
560 0
        assert(sock > 0);
561 0
        pfd.fd = sock;
562 0
        pfd.events = POLLOUT;
563 0
        pfd.revents = 0;
564
565 0
        if (poll(&pfd, 1, 0) == 1 && pfd.revents & POLLHUP)
566 0
                return (1);
567 0
        return (0);
568 0
}
569
570
/*--------------------------------------------------------------------
571
 * Check if a TCP syscall return value is fatal
572
 */
573
574
int
575 156914
VTCP_Check(ssize_t a)
576
{
577 156914
        if (a == 0)
578 134610
                return (1);
579 22304
        if (a > 0)
580 22220
                return (1);
581 84
        if (errno == ECONNRESET || errno == ENOTCONN || errno == EPIPE)
582 57
                return (1);
583
        /* Accept EAGAIN (and EWOULDBLOCK in case they are not the same)
584
         * as errno values. Even though our sockets are all non-blocking,
585
         * when a SO_{SND|RCV}TIMEO expires, read() or write() on the
586
         * socket will return (-1) and errno set to EAGAIN. (This is not
587
         * documented in the read(2) and write(2) manpages, but is
588
         * described in the socket(7) manpage.) */
589 27
        if (errno == EAGAIN || errno == EWOULDBLOCK)
590 27
                return (1);
591
        /* tcp(7): The other end didn't acknowledge retransmitted data after
592
         * some time. */
593 0
        if (errno == ETIMEDOUT)
594 0
                return (1);
595
        /* #3539 various errnos documented on linux as POSIX.1 */
596 0
        if (errno == ENETDOWN || errno == ENETUNREACH || errno == ENETRESET ||
597 0
            errno == ECONNABORTED || /* ECONNRESET see above */
598 0
            errno == EHOSTUNREACH || errno == EHOSTDOWN) {
599 0
                return (1);
600
        }
601
602
#if (defined (__SVR4) && defined (__sun))
603
        if (errno == ECONNREFUSED)      // in r02702.vtc
604
                return (1);
605
        if (errno == EPROTO)
606
                return (1);
607
#endif
608
#if (defined (__SVR4) && defined (__sun)) ||            \
609
    defined (__NetBSD__) ||                             \
610
    defined (__APPLE__)
611
        /*
612
         * Solaris and macOS returns EINVAL if the other end unexpectedly reset
613
         * the connection.
614
         *
615
         * On NetBSD it is documented behaviour.
616
         */
617
        if (errno == EINVAL)
618
                return (1);
619
#endif
620
#if defined(ENABLE_SANITIZER)
621
        if (errno == EINTR)
622
                return (1);
623
#endif
624 0
        return (0);
625 156914
}
626
627
/*--------------------------------------------------------------------
628
 *
629
 */
630
631
int
632 26575
VTCP_read(int fd, void *ptr, size_t len, vtim_dur tmo)
633
{
634
        struct pollfd pfd[1];
635
        int i, j;
636
637 26575
        if (tmo > 0.0) {
638 26338
                pfd[0].fd = fd;
639 26338
                pfd[0].events = POLLIN;
640 26338
                pfd[0].revents = 0;
641 26338
                j = poll(pfd, 1, VTIM_poll_tmo(tmo));
642 26338
                if (j == 0)
643 827
                        return (-2);
644 25511
        }
645 25748
        i = read(fd, ptr, len);
646 25748
        VTCP_Assert(i);
647 25748
        return (i < 0 ? -1 : i);
648 26575
}