| | varnish-cache/lib/libvarnish/vss.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2010 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Dag-Erling Smørgrav <des@des.no> |
| 6 |
|
* Author: Cecilie Fritzvold <cecilihf@linpro.no> |
| 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 |
|
#include "config.h" |
| 33 |
|
|
| 34 |
|
#include <sys/socket.h> |
| 35 |
|
|
| 36 |
|
#include <netdb.h> |
| 37 |
|
#include <stdlib.h> |
| 38 |
|
#include <string.h> |
| 39 |
|
#include <stdio.h> |
| 40 |
|
|
| 41 |
|
#include "vdef.h" |
| 42 |
|
|
| 43 |
|
#include "vas.h" |
| 44 |
|
#include "vsa.h" |
| 45 |
|
#include "vss.h" |
| 46 |
|
|
| 47 |
|
/*lint -esym(754, _storage) not ref */ |
| 48 |
|
|
| 49 |
|
/* |
| 50 |
|
* Take a string provided by the user and break it up into address and |
| 51 |
|
* port parts. The address and port separator may be either a colon or |
| 52 |
|
* a whitespace. Examples of acceptable input include: |
| 53 |
|
* |
| 54 |
|
* "localhost" - "localhost:80" - "localhost 80" |
| 55 |
|
* "127.0.0.1" - "127.0.0.1:80" - "127.0.0.1 80" |
| 56 |
|
* "0.0.0.0" - "0.0.0.0:80" - "0.0.0.0 80" |
| 57 |
|
* "[::1]" - "[::1]:80" - "[::1] 80" |
| 58 |
|
* "[::]" - "[::]:80" - "[::] 80" |
| 59 |
|
* "::1" - "[::1]:80" - "[::1] 80" |
| 60 |
|
* |
| 61 |
|
* See also RFC5952 |
| 62 |
|
*/ |
| 63 |
|
|
| 64 |
|
static const char * |
| 65 |
12820 |
vss_parse(char *str, char **addr, char **port) |
| 66 |
|
{ |
| 67 |
|
char *p; |
| 68 |
|
|
| 69 |
12820 |
*addr = *port = NULL; |
| 70 |
|
|
| 71 |
12820 |
if (str[0] == '[') { |
| 72 |
|
/* IPv6 address of the form [::1]:80 */ |
| 73 |
47 |
*addr = str + 1; |
| 74 |
47 |
p = strchr(str, ']'); |
| 75 |
47 |
if (p == NULL) |
| 76 |
1 |
return ("IPv6 address lacks ']'"); |
| 77 |
46 |
*p++ = '\0'; |
| 78 |
46 |
if (*p == '\0') |
| 79 |
10 |
return (NULL); |
| 80 |
36 |
if (*p != ' ' && *p != ':') |
| 81 |
1 |
return ("IPv6 address has wrong port separator"); |
| 82 |
35 |
} else { |
| 83 |
|
/* |
| 84 |
|
* IPv4 address of the form 127.0.0.1:80, IPv6 address |
| 85 |
|
* without port or non-numeric. |
| 86 |
|
*/ |
| 87 |
12773 |
*addr = str; |
| 88 |
12773 |
p = strchr(str, ' '); |
| 89 |
12773 |
if (p == NULL) |
| 90 |
10725 |
p = strchr(str, ':'); |
| 91 |
12773 |
if (p == NULL) |
| 92 |
2323 |
return (NULL); |
| 93 |
10450 |
if (p[0] == ':' && strchr(&p[1], ':')) |
| 94 |
42 |
return (NULL); |
| 95 |
10408 |
if (p == str) |
| 96 |
1134 |
*addr = NULL; |
| 97 |
|
} |
| 98 |
10443 |
*p++ = '\0'; |
| 99 |
10443 |
*port = p; |
| 100 |
10443 |
return (NULL); |
| 101 |
12820 |
} |
| 102 |
|
|
| 103 |
|
static const char * |
| 104 |
999 |
vss_parse_range(char *str, char **addr, char **port, unsigned long *lo, |
| 105 |
|
unsigned long *hi) |
| 106 |
|
{ |
| 107 |
|
const char *errp; |
| 108 |
|
char *end; |
| 109 |
|
unsigned long l, h; |
| 110 |
|
|
| 111 |
999 |
errp = vss_parse(str, addr, port); |
| 112 |
999 |
if (errp != NULL) |
| 113 |
0 |
return (errp); |
| 114 |
999 |
if (*port == NULL || **port == '-') |
| 115 |
13 |
return (NULL); |
| 116 |
|
|
| 117 |
986 |
l = strtoul(*port, &end, 10); |
| 118 |
986 |
if (end[0] != '-' || end[1] == '\0') |
| 119 |
963 |
return (NULL); |
| 120 |
23 |
if (strchr(end + 1, '-') != NULL) |
| 121 |
2 |
return (NULL); |
| 122 |
21 |
h = strtoul(end + 1, &end, 10); |
| 123 |
21 |
if (end[0] != '\0') |
| 124 |
3 |
return (NULL); |
| 125 |
|
|
| 126 |
|
/* Port range of the form 80-81 */ |
| 127 |
18 |
if (l == 0) |
| 128 |
1 |
return ("Range start cannot be 0"); |
| 129 |
17 |
if (h < l) |
| 130 |
1 |
return ("Range start higher than range end"); |
| 131 |
16 |
if (h > 65535) |
| 132 |
1 |
return ("Range end higher than 65535"); |
| 133 |
|
|
| 134 |
15 |
*lo = l; |
| 135 |
15 |
*hi = h; |
| 136 |
15 |
return (NULL); |
| 137 |
999 |
} |
| 138 |
|
|
| 139 |
|
static int |
| 140 |
11821 |
vss_resolve(const char *addr, const char *def_port, int family, int socktype, |
| 141 |
|
int flags, struct addrinfo **res, const char **errp) |
| 142 |
|
{ |
| 143 |
|
struct addrinfo hints; |
| 144 |
|
char *p, *hp, *pp; |
| 145 |
|
int ret; |
| 146 |
|
|
| 147 |
11821 |
AN(addr); |
| 148 |
11821 |
AN(res); |
| 149 |
11821 |
AZ(*res); |
| 150 |
11821 |
AN(errp); |
| 151 |
11821 |
*errp = NULL; |
| 152 |
|
|
| 153 |
11821 |
memset(&hints, 0, sizeof hints); |
| 154 |
11821 |
hints.ai_family = family; |
| 155 |
11821 |
hints.ai_socktype = socktype; |
| 156 |
11821 |
hints.ai_flags = flags; |
| 157 |
|
|
| 158 |
11821 |
p = strdup(addr); |
| 159 |
11821 |
AN(p); |
| 160 |
11821 |
*errp = vss_parse(p, &hp, &pp); |
| 161 |
11821 |
if (*errp != NULL) { |
| 162 |
2 |
free(p); |
| 163 |
2 |
return (-1); |
| 164 |
|
} |
| 165 |
11819 |
if (pp != NULL) |
| 166 |
9449 |
def_port = pp; |
| 167 |
11819 |
ret = getaddrinfo(hp, def_port, &hints, res); |
| 168 |
11819 |
free(p); |
| 169 |
|
|
| 170 |
11819 |
if (ret == EAI_SYSTEM) |
| 171 |
0 |
*errp = VAS_errtxt(errno); |
| 172 |
11819 |
else if (ret != 0) |
| 173 |
52 |
*errp = gai_strerror(ret); |
| 174 |
|
|
| 175 |
11819 |
return (ret); |
| 176 |
11821 |
} |
| 177 |
|
|
| 178 |
|
static const struct suckaddr * |
| 179 |
2183 |
vss_alloc_suckaddr(void *dst, const struct addrinfo *ai) |
| 180 |
|
{ |
| 181 |
|
|
| 182 |
2183 |
AN(ai); |
| 183 |
2183 |
if (dst == NULL) |
| 184 |
2068 |
return (VSA_Malloc(ai->ai_addr, ai->ai_addrlen)); |
| 185 |
|
|
| 186 |
115 |
return (VSA_Build(dst, ai->ai_addr, ai->ai_addrlen)); |
| 187 |
2183 |
} |
| 188 |
|
|
| 189 |
|
/* |
| 190 |
|
* Look up an address, using a default port if provided, and call |
| 191 |
|
* the callback function with the suckaddrs we find. |
| 192 |
|
* If the callback function returns anything but zero, we terminate |
| 193 |
|
* and pass that value. |
| 194 |
|
*/ |
| 195 |
|
|
| 196 |
|
int |
| 197 |
9604 |
VSS_resolver_socktype(const char *addr, const char *def_port, |
| 198 |
|
vss_resolved_f *func, void *priv, const char **errp, int socktype) |
| 199 |
|
{ |
| 200 |
9604 |
struct addrinfo *res0 = NULL, *res; |
| 201 |
|
const struct suckaddr *vsa; |
| 202 |
|
int ret; |
| 203 |
|
|
| 204 |
9604 |
AN(addr); |
| 205 |
9604 |
AN(func); |
| 206 |
9604 |
AN(errp); |
| 207 |
|
|
| 208 |
19208 |
ret = vss_resolve(addr, def_port, AF_UNSPEC, socktype, AI_PASSIVE, |
| 209 |
9604 |
&res0, errp); |
| 210 |
9604 |
if (ret != 0) |
| 211 |
23 |
return (-1); |
| 212 |
|
|
| 213 |
14855 |
for (res = res0; res != NULL; res = res->ai_next) { |
| 214 |
10579 |
vsa = VSA_Malloc(res->ai_addr, res->ai_addrlen); |
| 215 |
10579 |
if (vsa != NULL) { |
| 216 |
10579 |
ret = func(priv, vsa); |
| 217 |
10579 |
VSA_free(&vsa); |
| 218 |
10579 |
if (ret) |
| 219 |
5305 |
break; |
| 220 |
5274 |
} |
| 221 |
5274 |
} |
| 222 |
9581 |
freeaddrinfo(res0); |
| 223 |
9581 |
return (ret); |
| 224 |
9604 |
} |
| 225 |
|
|
| 226 |
|
int |
| 227 |
9606 |
VSS_resolver(const char *addr, const char *def_port, vss_resolved_f *func, |
| 228 |
|
void *priv, const char **errp) |
| 229 |
|
{ |
| 230 |
9606 |
return (VSS_resolver_socktype( |
| 231 |
9606 |
addr, def_port, func, priv, errp, SOCK_STREAM)); |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
int |
| 235 |
999 |
VSS_resolver_range(const char *addr, const char *def_port, vss_resolved_f *func, |
| 236 |
|
void *priv, const char **errp) |
| 237 |
|
{ |
| 238 |
|
char *p, *hp, *pp; |
| 239 |
999 |
unsigned long lo = 0, hi = 0, i; |
| 240 |
999 |
int error = 0; |
| 241 |
|
|
| 242 |
999 |
AN(addr); |
| 243 |
999 |
AN(func); |
| 244 |
999 |
AN(errp); |
| 245 |
|
|
| 246 |
999 |
p = strdup(addr); |
| 247 |
999 |
AN(p); |
| 248 |
999 |
*errp = vss_parse_range(p, &hp, &pp, &lo, &hi); |
| 249 |
999 |
if (*errp != NULL) { |
| 250 |
3 |
free(p); |
| 251 |
3 |
return (-1); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
996 |
if (lo == 0) { |
| 255 |
|
/* No port range (0 not allowed in range) */ |
| 256 |
981 |
free(p); |
| 257 |
981 |
return (VSS_resolver(addr, def_port, func, priv, errp)); |
| 258 |
|
} |
| 259 |
|
|
| 260 |
|
/* Undo vss_parse() string modifications */ |
| 261 |
15 |
memcpy(p, addr, pp - p); |
| 262 |
|
|
| 263 |
44 |
for (i = lo; i <= hi && !error; i++) { |
| 264 |
|
/* pp points to the first character of the range definition. |
| 265 |
|
* The range definition includes the biggest port number, so the |
| 266 |
|
* buffer must be big enough to fit each number individually. |
| 267 |
|
*/ |
| 268 |
29 |
sprintf(pp, "%lu", i); |
| 269 |
29 |
error = VSS_resolver(p, def_port, func, priv, errp); |
| 270 |
29 |
} |
| 271 |
15 |
free(p); |
| 272 |
15 |
return (error); |
| 273 |
999 |
} |
| 274 |
|
|
| 275 |
|
const struct suckaddr * |
| 276 |
2126 |
VSS_ResolveOne(void *dst, const char *addr, const char *def_port, |
| 277 |
|
int family, int socktype, int flags) |
| 278 |
|
{ |
| 279 |
2126 |
struct addrinfo *res = NULL; |
| 280 |
2126 |
const struct suckaddr *retval = NULL; |
| 281 |
|
const char *err; |
| 282 |
|
int ret; |
| 283 |
|
|
| 284 |
2126 |
AN(addr); |
| 285 |
2126 |
ret = vss_resolve(addr, def_port, family, socktype, flags, &res, &err); |
| 286 |
2126 |
if (ret == 0 && res != NULL && res->ai_next == NULL) { |
| 287 |
2107 |
AZ(err); |
| 288 |
2107 |
retval = vss_alloc_suckaddr(dst, res); |
| 289 |
2107 |
} |
| 290 |
2126 |
if (res != NULL) |
| 291 |
2107 |
freeaddrinfo(res); |
| 292 |
2126 |
return (retval); |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
const struct suckaddr * |
| 296 |
88 |
VSS_ResolveFirst(void *dst, const char *addr, const char *def_port, |
| 297 |
|
int family, int socktype, int flags) |
| 298 |
|
{ |
| 299 |
88 |
struct addrinfo *res0 = NULL, *res; |
| 300 |
88 |
const struct suckaddr *retval = NULL; |
| 301 |
|
const char *err; |
| 302 |
|
int ret; |
| 303 |
|
|
| 304 |
88 |
AN(addr); |
| 305 |
88 |
ret = vss_resolve(addr, def_port, family, socktype, flags, &res0, &err); |
| 306 |
88 |
if (ret == 0) |
| 307 |
76 |
AZ(err); |
| 308 |
|
|
| 309 |
89 |
for (res = res0; res != NULL; res = res->ai_next) { |
| 310 |
76 |
retval = vss_alloc_suckaddr(dst, res); |
| 311 |
76 |
if (retval != NULL) |
| 312 |
75 |
break; |
| 313 |
1 |
} |
| 314 |
88 |
if (res0 != NULL) |
| 315 |
76 |
freeaddrinfo(res0); |
| 316 |
88 |
return (retval); |
| 317 |
|
} |