vinyl-cache/bin/vinyltest/vtest2/lib/vudp.c
0
/*-
1
 * Copyright (c) 2008-2025 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Frédéric Lécaille <flecaille@haproxy.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <sys/types.h>
33
#include <sys/socket.h>
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
40
#include "vdef.h"
41
#include "vas.h"
42
#include "vsa.h"
43
#include "vss.h"
44
#include "vudp.h"
45
46
/*--------------------------------------------------------------------
47
 * Check if a UDP syscall return value is fatal
48
 * XXX: Largely copied from VTCP, not sure if really applicable
49
 */
50
51
int
52 5
VUDP_Check(int a)
53
{
54 5
        if (a == 0)
55 5
                return (1);
56 0
        if (errno == ECONNRESET)
57 0
                return (1);
58
#if (defined (__SVR4) && defined (__sun)) || defined (__NetBSD__)
59
        /*
60
         * Solaris returns EINVAL if the other end unexpectedly reset the
61
         * connection.
62
         * This is a bug in Solaris and documented behaviour on NetBSD.
63
         */
64
        if (errno == EINVAL || errno == ETIMEDOUT || errno == EPIPE)
65
                return (1);
66
#elif defined (__APPLE__)
67
        /*
68
         * macOS returns EINVAL if the other end unexpectedly reset
69
         * the connection.
70
         */
71
        if (errno == EINVAL)
72
                return (1);
73
#endif
74 0
        return (0);
75 5
}
76
77
/*--------------------------------------------------------------------
78
 * When closing a UDP connection, a couple of errno's are legit, we
79
 * can't be held responsible for the other end wanting to talk to us.
80
 */
81
82
void
83 5
VUDP_close(int *s)
84
{
85
        int i;
86
87 5
        i = close(*s);
88
89 5
        assert(VUDP_Check(i));
90 5
        *s = -1;
91 5
}
92
93
/*--------------------------------------------------------------------
94
 * Given a struct suckaddr, open a socket of the appropriate type, and bind
95
 * it to the requested address.
96
 *
97
 * If the address is an IPv6 address, the IPV6_V6ONLY option is set to
98
 * avoid conflicts between INADDR_ANY and IN6ADDR_ANY.
99
 */
100
101
int
102 5
VUDP_bind(const struct suckaddr *sa, const char **errp)
103
{
104
#ifdef IPV6_V6ONLY
105
        int val;
106
#endif
107
        int sd, e;
108
        socklen_t sl;
109
        const struct sockaddr *so;
110
        int proto;
111
112 5
        if (errp != NULL)
113 5
                *errp = NULL;
114
115 5
        proto = VSA_Get_Proto(sa);
116 5
        sd = socket(proto, SOCK_DGRAM, 0);
117 5
        if (sd < 0) {
118 0
                if (errp != NULL)
119 0
                        *errp = "socket(2)";
120 0
                return (-1);
121
        }
122
123
#ifdef IPV6_V6ONLY
124
        /* forcibly use separate sockets for IPv4 and IPv6 */
125
        val = 1;
126
        if (proto == AF_INET6 &&
127
            setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val) != 0) {
128
                if (errp != NULL)
129
                        *errp = "setsockopt(IPV6_V6ONLY, 1)";
130
                e = errno;
131
                closefd(&sd);
132
                errno = e;
133
                return (-1);
134
        }
135
#endif
136 5
        so = VSA_Get_Sockaddr(sa, &sl);
137 5
        if (bind(sd, so, sl) != 0) {
138 0
                if (errp != NULL)
139 0
                        *errp = "bind(2)";
140 0
                e = errno;
141 0
                closefd(&sd);
142 0
                errno = e;
143 0
                return (-1);
144
        }
145 5
        return (sd);
146 5
}
147
148
static int v_matchproto_(vss_resolved_f)
149 5
vudp_lo_cb(void *priv, const struct suckaddr *sa)
150
{
151
        int sock;
152 5
        struct udp_helper *hp = priv;
153
154 5
        sock = VUDP_bind(sa, hp->errp);
155 5
        if (sock > 0) {
156 5
                *hp->errp = NULL;
157 5
                return (sock);
158
        }
159 0
        AN(*hp->errp);
160 0
        return (0);
161 5
}
162
163
int
164 5
VUDP_bind_on(const char *addr, const char *def_port, const char **errp)
165
{
166
        struct udp_helper h;
167
        int sock;
168
169 5
        h.errp = errp;
170
171 5
        sock = VSS_resolver_socktype(
172 5
            addr, def_port, vudp_lo_cb, &h, errp, SOCK_DGRAM);
173 5
        if (*errp != NULL)
174 0
                return (-1);
175 5
        return (sock);
176 5
}