Code Coverage: vinyl-cache/bin/vinyld/acceptor/mgt_acceptor_tcp.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2023 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 * Author: Asad Sajjad Ahmed <asadsa@varnish-software.com>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 *
31
 */
32
33
#include "config.h"
34
35
#include <sys/types.h>
36
#include <sys/socket.h>
37
#include <sys/stat.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
#include "mgt/mgt.h"
44
#include "common/heritage.h"
45
46
#include "acceptor/cache_acceptor.h"
47
#include "acceptor/acceptor_tcp.h"
48
#include "acceptor/mgt_acceptor.h"
49
50
#include "vsa.h"
51
#include "vss.h"
52
#include "vtcp.h"
53
54
int
55
vca_tcp_config(void)
56 1099
{
57
58 1099
        return (0);
59 1099
}
60
61
static int
62
vca_tcp_opensocket(struct listen_sock *ls)
63 2058
{
64 2058
        int fail;
65
66 2058
        CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
67
68 2058
        if (ls->sock > 0) {
69 1014
                MCH_Fd_Inherit(ls->sock, NULL);
70 1014
                closefd(&ls->sock);
71 1014
        }
72
73 2058
        ls->sock = VTCP_bind(ls->addr, NULL);
74 2058
        fail = errno;
75
76 2058
        if (ls->sock < 0) {
77 1
                AN(fail);
78 1
                return (fail);
79 1
        }
80
81 2057
        AZ(ls->perms);
82 2057
        MCH_Fd_Inherit(ls->sock, "sock");
83
84 2057
        return (0);
85 2057
}
86
87
static int v_matchproto_(vss_resolved_f)
88
vca_tcp_open_cb(void *priv, const struct suckaddr *sa)
89 1045
{
90 1045
        struct listen_arg *la;
91 1045
        struct listen_sock *ls;
92 1045
        char abuf[VTCP_ADDRBUFSIZE], pbuf[VTCP_PORTBUFSIZE];
93 1045
        char nbuf[VTCP_ADDRBUFSIZE+VTCP_PORTBUFSIZE+2];
94 1045
        int fail;
95
96 1045
        CAST_OBJ_NOTNULL(la, priv, LISTEN_ARG_MAGIC);
97
98 1045
        VTAILQ_FOREACH(ls, &TCP_acceptor.socks, vcalist) {
99 25
                CHECK_OBJ(ls, LISTEN_SOCK_MAGIC);
100
101 25
                if (!VSA_Compare(sa, ls->addr))
102 1
                        ARGV_ERR("-a arguments %s and %s have same address\n",
103 25
                            ls->endpoint, la->endpoint);
104 25
        }
105
106 1044
        ALLOC_OBJ(ls, LISTEN_SOCK_MAGIC);
107 1044
        AN(ls);
108
109 1044
        ls->sock = -1;
110 1044
        ls->vca = &TCP_acceptor;
111
112 1044
        ls->addr = VSA_Clone(sa);
113 1044
        AN(ls->addr);
114
115 1044
        REPLACE(ls->endpoint, la->endpoint);
116 1044
        ls->name = la->name;
117 1044
        ls->transport = la->transport;
118 1044
        ls->perms = la->perms;
119
120 1044
        VJ_master(JAIL_MASTER_PRIVPORT);
121 1044
        fail = vca_tcp_opensocket(ls);
122 1044
        VJ_master(JAIL_MASTER_LOW);
123
124 1044
        if (fail) {
125 1
                VSA_free(&ls->addr);
126 1
                free(ls->endpoint);
127 1
                FREE_OBJ(ls);
128 1
                if (fail != EAFNOSUPPORT)
129 1
                        ARGV_ERR("Could not get socket %s: %s\n",
130 1
                            la->endpoint, VAS_errtxt(fail));
131 0
                return (0);
132 1
        }
133
134 1043
        AZ(ls->uds);
135
136 1043
        if (VSA_Port(ls->addr) == 0) {
137
                /*
138
                 * If the argv port number is zero, we adopt whatever
139
                 * port number this VTCP_bind() found us, as if
140
                 * it was specified by the argv.
141
                 */
142 1042
                VSA_free(&ls->addr);
143 1042
                ls->addr = VTCP_my_suckaddr(ls->sock);
144 1042
                VTCP_myname(ls->sock, abuf, sizeof abuf,
145 1042
                    pbuf, sizeof pbuf);
146 1042
                if (VSA_Get_Proto(sa) == AF_INET6)
147 24
                        bprintf(nbuf, "[%s]:%s", abuf, pbuf);
148 1018
                else
149 1018
                        bprintf(nbuf, "%s:%s", abuf, pbuf);
150 1042
                REPLACE(ls->endpoint, nbuf);
151 1042
        }
152
153 1043
        VTAILQ_INSERT_TAIL(&la->socks, ls, arglist);
154 1043
        VTAILQ_INSERT_TAIL(&heritage.socks, ls, list);
155 1043
        VTAILQ_INSERT_TAIL(&TCP_acceptor.socks, ls, vcalist);
156
157 1043
        return (0);
158 1043
}
159
160
161
int
162
vca_tcp_open(char **av, struct listen_arg *la, const char **err)
163 1028
{
164 1028
        const struct transport *xp = NULL;
165
166 1028
        CHECK_OBJ_NOTNULL(la, LISTEN_ARG_MAGIC);
167 1028
        AN(av);
168 1028
        AN(err);
169 1028
        AZ(la->perms);
170
171 1028
        if (strchr(la->endpoint, '/') != NULL)
172 1
                ARGV_ERR("Unix domain socket addresses must be"
173 1028
                    " absolute paths in -a (%s)\n", la->endpoint);
174
175 1046
        for (int i = 0; av[i] != NULL; i++) {
176 25
                if (strchr(av[i], '=') == NULL) {
177 22
                        if (xp != NULL)
178 2
                                ARGV_ERR("Too many protocol sub-args"
179 22
                                    " in -a (%s)\n", av[i]);
180 20
                        xp = XPORT_Find(av[i]);
181 20
                        if (xp == NULL)
182 1
                                ARGV_ERR("Unknown protocol '%s'\n", av[i]);
183 19
                        continue;
184 20
                }
185
186 3
                ARGV_ERR("Invalid sub-arg %s in -a\n", av[i]);
187 3
        }
188
189 1021
        if (xp == NULL)
190 1004
                xp = XPORT_Find("http");
191
192 1021
        AN(xp);
193 1021
        la->transport = xp;
194
195 1021
        return (VSS_resolver_range(la->endpoint, "80", vca_tcp_open_cb, la, err));
196 1021
}
197
198
int
199
vca_tcp_reopen(void)
200 1032
{
201 1032
        struct listen_sock *ls;
202 1032
        int err, fail = 0;
203
204 1032
        VTAILQ_FOREACH(ls, &TCP_acceptor.socks, vcalist) {
205 1014
                CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC);
206
207 1014
                VJ_master(JAIL_MASTER_PRIVPORT);
208 1014
                err = vca_tcp_opensocket(ls);
209 1014
                VJ_master(JAIL_MASTER_LOW);
210
211 1014
                if (err == 0)
212 1014
                        continue;
213
214 0
                fail = vmax(fail, err);
215 0
                MGT_Complain(C_ERR, "Could not reopen listen socket %s: %s",
216 0
                    ls->endpoint, VAS_errtxt(err));
217 0
        }
218
219 1032
        return (fail);
220 1032
}