1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*-
* Copyright (c) 2010-2015 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@FreeBSD.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "config.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netdb.h>
#include "cache/cache.h"
#include "vnum.h"
#include "vsa.h"
#include "vss.h"
#include "vtim.h"
#include "vcc_std_if.h"
/*
* technically, as our VCL_INT is int64_t, its limits are INT64_MIN
* .. INT64_MAX.
*
* We redistrict to VRT_INTEGER_MIN .. VRT_INTEGER_MAX
*/
/* limited by using double for conversions */
#define VCL_BYTES_MAX ((INT64_C(1)<<53)-1)
static
int onearg(VRT_CTX, const char *f, int nargs)
{
if (nargs == 1)
return (1);
VRT_fail(ctx, "std.%s: %s arguments", f,
nargs > 1 ? "too many" : "not enough");
return (0);
}
/*
* not handling real arg isfinite() / nan() : caller error
* always trunc, never round
*/
VCL_DURATION v_matchproto_(td_std_duration)
vmod_duration(VRT_CTX, struct VARGS(duration) *a)
{
double r;
int nargs;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
nargs = a->valid_s + a->valid_real + a->valid_integer;
if (!onearg(ctx, "duration", nargs))
return (0);
if (a->valid_real)
return ((VCL_DURATION)a->real);
if (a->valid_integer)
return ((VCL_DURATION)a->integer);
if (a->valid_s) {
r = VNUM_duration(a->s);
if (!isnan(r))
return (r);
}
if (a->valid_fallback)
return (a->fallback);
VRT_fail(ctx, "std.duration: conversion failed");
return (0);
}
VCL_BYTES v_matchproto_(td_std_bytes)
vmod_bytes(VRT_CTX, struct VARGS(bytes) *a)
{
uintmax_t r;
VCL_REAL rr;
int nargs;
const char *errtxt;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
nargs = a->valid_s + a->valid_real + a->valid_integer;
if (!onearg(ctx, "bytes", nargs))
return (0);
if (a->valid_s) {
errtxt = VNUM_2bytes(a->s, &r, 0);
if (errtxt == NULL && r <= VCL_BYTES_MAX)
return ((VCL_BYTES)r);
}
if (a->valid_real && !isnan(a->real) && a->real >= 0) {
rr = trunc(a->real);
if (rr <= (VCL_REAL)VCL_BYTES_MAX)
return ((VCL_BYTES)rr);
}
if (a->valid_integer && a->integer >= 0)
return ((VCL_BYTES)a->integer);
if (a->valid_fallback)
return (a->fallback);
VRT_fail(ctx, "std.bytes: conversion failed");
return (0);
}
VCL_INT v_matchproto_(td_std_integer)
vmod_integer(VRT_CTX, struct VARGS(integer) *a)
{
const char *p, *errtxt = NULL;
double r, tmp;
int nargs;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
nargs = a->valid_s + a->valid_boolean + a->valid_bytes +
a->valid_duration + a->valid_real + a->valid_time;
if (!onearg(ctx, "integer", nargs))
return (0);
r = NAN;
if (a->valid_boolean)
return (a->boolean ? 1 : 0);
if (a->valid_bytes)
return (a->bytes);
if (a->valid_s && a->s != NULL) {
p = a->s;
r = SF_Parse_Number(&p, 0, &errtxt);
if (!errno && *p == '\0' && modf(r, &tmp) == 0.0)
return ((VCL_INT)r);
r = NAN;
}
if (a->valid_duration)
r = a->duration;
if (a->valid_real)
r = a->real;
if (a->valid_time)
r = a->time;
if (!isnan(r)) {
r = trunc(r);
if (r >= VRT_INTEGER_MIN && r <= VRT_INTEGER_MAX)
return ((VCL_INT)r);
}
if (a->valid_fallback)
return (a->fallback);
if (errtxt != NULL)
VRT_fail(ctx, "std.integer: conversion failed: %s", errtxt);
else
VRT_fail(ctx, "std.integer: conversion failed");
return (0);
}
VCL_IP
vmod_ip(VRT_CTX, struct VARGS(ip) *a)
{
uintptr_t sn;
void *p;
VCL_IP retval = NULL, fb = bogo_ip;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (a->valid_fallback) {
if (a->fallback == NULL || !VSA_Sane(a->fallback)) {
VRT_fail(ctx, "std.ip: invalid fallback");
return (fb);
}
fb = a->fallback;
}
sn = WS_Snapshot(ctx->ws);
p = WS_Alloc(ctx->ws, vsa_suckaddr_len);
if (p == NULL) {
VRT_fail(ctx, "std.ip: insufficient workspace");
return (fb);
}
if (a->s != NULL)
retval = VSS_ResolveFirst(
p, a->s, a->valid_p ? a->p : "80",
AF_UNSPEC, SOCK_STREAM,
a->resolve ? 0 : AI_NUMERICHOST|AI_NUMERICSERV);
if (retval != NULL)
return (retval);
WS_Reset(ctx->ws, sn);
if (!a->valid_fallback)
VRT_fail(ctx, "std.ip: conversion failed");
return (fb);
}
VCL_REAL v_matchproto_(td_std_real)
vmod_real(VRT_CTX, struct VARGS(real) *a)
{
VCL_REAL r;
const char *p, *errtxt = NULL;
int nargs;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
nargs = a->valid_s + a->valid_integer + a->valid_boolean + a->valid_bytes +
a->valid_duration + a->valid_time;
if (!onearg(ctx, "real", nargs))
return (0);
if (a->valid_integer)
return ((VCL_REAL)a->integer);
if (a->valid_boolean)
return ((VCL_REAL)(a->boolean ? 1 : 0));
if (a->valid_bytes)
return ((VCL_REAL)a->bytes);
if (a->valid_duration)
return ((VCL_REAL)a->duration);
if (a->valid_time)
return ((VCL_REAL)a->time);
if (a->valid_s && a->s != NULL) {
p = a->s;
r = SF_Parse_Decimal(&p, 0, &errtxt);
if (!errno && *p == '\0')
return (r);
}
if (a->valid_fallback)
return (a->fallback);
if (errtxt != NULL)
VRT_fail(ctx, "std.real: conversion failed: %s", errtxt);
else
VRT_fail(ctx, "std.real: conversion failed");
return (0);
}
VCL_REAL v_matchproto_(td_std_round)
vmod_round(VRT_CTX, VCL_REAL r)
{
(void) ctx;
return (round(r));
}
VCL_TIME v_matchproto_(td_std_time)
vmod_time(VRT_CTX, struct VARGS(time)* a)
{
double r;
int nargs;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
nargs = a->valid_s + a->valid_real + a->valid_integer;
if (!onearg(ctx, "time", nargs))
return (0);
if (a->valid_integer)
return ((VCL_REAL)a->integer);
if (a->valid_real)
return ((VCL_REAL)a->real);
if (a->valid_s && a->s != NULL) {
r = VTIM_parse(a->s);
if (r)
return (r);
r = VNUM(a->s);
if (!isnan(r) && r > 0)
return (r);
}
if (a->valid_fallback)
return (a->fallback);
VRT_fail(ctx, "std.time: conversion failed");
return (0);
}
VCL_STRING v_matchproto_(td_std_strftime)
vmod_strftime(VRT_CTX, VCL_TIME t, VCL_STRING fmt)
{
struct tm tm;
time_t tt;
size_t r;
unsigned spc;
char *s;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
tt = (time_t)(intmax_t)t;
if (gmtime_r(&tt, &tm) == NULL)
return ("");
spc = WS_ReserveAll(ctx->ws);
s = WS_Reservation(ctx->ws);
r = strftime(s, spc, fmt, &tm);
if (r == 0) {
WS_Release(ctx->ws, 0);
return ("");
}
r++;
WS_Release(ctx->ws, r);
return (s);
}
/* These functions are deprecated as of 2019-03-15 release */
VCL_INT v_matchproto_(td_std_real2integer)
vmod_real2integer(VRT_CTX, VCL_REAL r, VCL_INT i)
{
VCL_INT retval;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (!VRT_REAL_is_valid(r))
return (i);
retval = (VCL_INT)round(r);
if (!VRT_INT_is_valid(retval))
return (i);
return (retval);
}
VCL_TIME v_matchproto_(td_std_real2time)
vmod_real2time(VRT_CTX, VCL_REAL r, VCL_TIME t)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (!isfinite(r))
return (t);
return (r);
}
VCL_INT v_matchproto_(td_std_time2integer)
vmod_time2integer(VRT_CTX, VCL_TIME t, VCL_INT i)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (!isfinite(t))
return (i);
t = round(t);
if (t > VRT_INTEGER_MAX || t < VRT_INTEGER_MIN)
return (i);
return ((VCL_INT)t);
}
VCL_REAL v_matchproto_(td_std_time2real)
vmod_time2real(VRT_CTX, VCL_TIME t, VCL_REAL r)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (!isfinite(t))
return (r);
return (t);
}
|