varnish-cache/vmod/vmod_std_conversions.c
1
/*-
2
 * Copyright (c) 2010-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
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
32
#include "config.h"
33
34
#include <ctype.h>
35
#include <string.h>
36
#include <stdlib.h>
37
#include <sys/socket.h>
38
39
#include <netdb.h>
40
41
#include "cache/cache.h"
42
43
#include "vnum.h"
44
#include "vsa.h"
45
#include "vss.h"
46
#include "vtim.h"
47
#include "vcc_std_if.h"
48
49
/*
50
 * technically, as our VCL_INT is int64_t, its limits are INT64_MIN/INT64_MAX.
51
 *
52
 * Yet, for conversions, we use VNUMpfx with a double intermediate, so above
53
 * 2^53 we see rounding errors. In order to catch a potential floor rounding
54
 * error, we make our limit 2^53-1
55
 *
56
 * Ref: https://stackoverflow.com/a/1848762
57
 */
58
#define VCL_INT_MAX ((INT64_C(1)<<53)-1)
59
#define VCL_INT_MIN (-VCL_INT_MAX)
60
61
#define VCL_BYTES_MAX VCL_INT_MAX
62
63
static
64 14385
int onearg(VRT_CTX, const char *f, int nargs)
65
{
66 14385
        if (nargs == 1)
67 14343
                return (1);
68 84
        VRT_fail(ctx, "std.%s: %s arguments", f,
69 42
            nargs > 1 ? "too many" : "not enough");
70 42
        return (0);
71 14385
}
72
73
/*
74
 * not handling real arg isfinite() / nan() : caller error
75
 * always trunc, never round
76
 */
77
78
VCL_DURATION v_matchproto_(td_std_duration)
79 882
vmod_duration(VRT_CTX, struct VARGS(duration) *a)
80
{
81
        double r;
82
        int nargs;
83
84 882
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
85
86 882
        nargs = a->valid_s + a->valid_real + a->valid_integer;
87
88 882
        if (!onearg(ctx, "duration", nargs))
89 0
                return (0);
90
91 882
        if (a->valid_real)
92 126
                return ((VCL_DURATION)a->real);
93
94 756
        if (a->valid_integer)
95 84
                return ((VCL_DURATION)a->integer);
96
97 672
        if (a->valid_s) {
98 672
                r = VNUM_duration(a->s);
99 672
                if (!isnan(r))
100 357
                        return (r);
101 315
        }
102
103 315
        if (a->valid_fallback)
104 189
                return (a->fallback);
105
106 126
        VRT_fail(ctx, "std.duration: conversion failed");
107 126
        return (0);
108 882
}
109
110
VCL_BYTES v_matchproto_(td_std_bytes)
111 630
vmod_bytes(VRT_CTX, struct VARGS(bytes) *a)
112
{
113
        uintmax_t r;
114
        VCL_REAL rr;
115
        int nargs;
116
        const char *errtxt;
117
118 630
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
119
120 630
        nargs = a->valid_s + a->valid_real + a->valid_integer;
121
122 630
        if (!onearg(ctx, "bytes", nargs))
123 0
                return (0);
124
125 630
        if (a->valid_s) {
126 462
                errtxt = VNUM_2bytes(a->s, &r, 0);
127 462
                if (errtxt == NULL && r <= VCL_BYTES_MAX)
128 189
                        return ((VCL_BYTES)r);
129 273
        }
130
131 441
        if (a->valid_real && !isnan(a->real) && a->real >= 0) {
132 105
                rr = trunc(a->real);
133 105
                if (rr <= (VCL_REAL)VCL_BYTES_MAX)
134 105
                        return ((VCL_BYTES)rr);
135 0
        }
136
137 336
        if (a->valid_integer && a->integer >= 0)
138 63
                return ((VCL_BYTES)a->integer);
139
140 273
        if (a->valid_fallback)
141 252
                return (a->fallback);
142
143 21
        VRT_fail(ctx, "std.bytes: conversion failed");
144 21
        return (0);
145 630
}
146
147
VCL_INT v_matchproto_(td_std_integer)
148 10080
vmod_integer(VRT_CTX, struct VARGS(integer) *a)
149
{
150 10080
        const char *p, *errtxt = NULL;
151
        double r, tmp;
152
        int nargs;
153
154 10080
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
155
156 30240
        nargs = a->valid_s + a->valid_bool + a->valid_bytes +
157 20160
            a->valid_duration + a->valid_real + a->valid_time;
158
159 10080
        if (!onearg(ctx, "integer", nargs))
160 42
                return (0);
161
162 10038
        r = NAN;
163 10038
        if (a->valid_bool)
164 21
                return (a->bool ? 1 : 0);
165
166 10017
        if (a->valid_bytes)
167 42
                return (a->bytes);
168
169 9975
        if (a->valid_s && a->s != NULL) {
170 9282
                p = a->s;
171 9282
                r = SF_Parse_Number(&p, 0, &errtxt);
172 9282
                if (!errno && *p == '\0' && modf(r, &tmp) == 0.0)
173 8946
                        return ((VCL_INT)r);
174 336
                r = NAN;
175 336
        }
176
177 1029
        if (a->valid_duration)
178 63
                r = a->duration;
179
180 1029
        if (a->valid_real)
181 105
                r = a->real;
182
183 1029
        if (a->valid_time)
184 42
                r = a->time;
185
186 1029
        if (!isnan(r)) {
187 210
                r = trunc(r);
188 210
                if (r >= VCL_INT_MIN && r <= VCL_INT_MAX)
189 210
                        return ((VCL_INT)r);
190 0
        }
191
192 819
        if (a->valid_fallback)
193 714
                return (a->fallback);
194
195 105
        if (errtxt != NULL)
196 42
                VRT_fail(ctx, "std.integer: conversion failed: %s", errtxt);
197
        else
198 63
                VRT_fail(ctx, "std.integer: conversion failed");
199 105
        return (0);
200 10080
}
201
202
VCL_IP
203 1176
vmod_ip(VRT_CTX, struct VARGS(ip) *a)
204
{
205
        uintptr_t sn;
206
        void *p;
207 1176
        VCL_IP retval = NULL;
208
209 1176
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
210 1176
        if (a->valid_fallback)
211 504
                assert(VSA_Sane(a->fallback));
212
213 1176
        sn = WS_Snapshot(ctx->ws);
214 1176
        p = WS_Alloc(ctx->ws, vsa_suckaddr_len);
215 1176
        if (p == NULL) {
216 0
                VRT_fail(ctx, "std.ip: insufficient workspace");
217 0
                return (NULL);
218
        }
219
220 1176
        if (a->s != NULL)
221 1155
                retval = VSS_ResolveFirst(
222 1155
                    p, a->s, a->valid_p ? a->p : "80",
223
                    AF_UNSPEC, SOCK_STREAM,
224 1155
                    a->resolve ? 0 : AI_NUMERICHOST|AI_NUMERICSERV);
225
226 1176
        if (retval != NULL)
227 882
                return (retval);
228
229 294
        WS_Reset(ctx->ws, sn);
230
231 294
        if (a->valid_fallback)
232 294
                return (a->fallback);
233
234 0
        VRT_fail(ctx, "std.ip: conversion failed");
235 0
        return (NULL);
236 1176
}
237
238
VCL_REAL v_matchproto_(td_std_real)
239 1260
vmod_real(VRT_CTX, struct VARGS(real) *a)
240
{
241
        VCL_REAL r;
242 1260
        const char *p, *errtxt = NULL;
243
        int nargs;
244
245 1260
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
246
247 3780
        nargs = a->valid_s + a->valid_integer + a->valid_bool + a->valid_bytes +
248 2520
            a->valid_duration + a->valid_time;
249
250 1260
        if (!onearg(ctx, "real", nargs))
251 0
                return (0);
252
253 1260
        if (a->valid_integer)
254 63
                return ((VCL_REAL)a->integer);
255
256 1197
        if (a->valid_bool)
257 21
                return ((VCL_REAL)(a->bool ? 1 : 0));
258
259 1176
        if (a->valid_bytes)
260 42
                return ((VCL_REAL)a->bytes);
261
262 1134
        if (a->valid_duration)
263 63
                return ((VCL_REAL)a->duration);
264
265 1071
        if (a->valid_time)
266 42
                return ((VCL_REAL)a->time);
267
268 1029
        if (a->valid_s && a->s != NULL) {
269 882
                p = a->s;
270 882
                r = SF_Parse_Decimal(&p, 0, &errtxt);
271 882
                if (!errno && *p == '\0')
272 735
                        return (r);
273 147
        }
274
275 294
        if (a->valid_fallback)
276 189
                return (a->fallback);
277
278 105
        if (errtxt != NULL)
279 42
                VRT_fail(ctx, "std.real: conversion failed: %s", errtxt);
280
        else
281 63
                VRT_fail(ctx, "std.real: conversion failed");
282 105
        return (0);
283 1260
}
284
285
VCL_REAL v_matchproto_(td_std_round)
286 42
vmod_round(VRT_CTX, VCL_REAL r)
287
{
288 42
        (void) ctx;
289 42
        return (round(r));
290
}
291
292
VCL_TIME v_matchproto_(td_std_time)
293 1533
vmod_time(VRT_CTX, struct VARGS(time)* a)
294
{
295
        double r;
296
        int nargs;
297
298 1533
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
299
300 1533
        nargs = a->valid_s + a->valid_real + a->valid_integer;
301
302 1533
        if (!onearg(ctx, "time", nargs))
303 0
                return (0);
304
305 1533
        if (a->valid_integer)
306 42
                return ((VCL_REAL)a->integer);
307
308 1491
        if (a->valid_real)
309 63
                return ((VCL_REAL)a->real);
310
311 1428
        if (a->valid_s && a->s != NULL) {
312 1386
                r = VTIM_parse(a->s);
313 1386
                if (r)
314 462
                        return (r);
315
316 924
                r = VNUM(a->s);
317
318 924
                if (!isnan(r) && r > 0)
319 168
                        return (r);
320 756
        }
321
322 798
        if (a->valid_fallback)
323 798
                return (a->fallback);
324
325 0
        VRT_fail(ctx, "std.time: conversion failed");
326 0
        return (0);
327 1533
}
328
329
/* These functions are deprecated as of 2019-03-15 release */
330
331
VCL_INT v_matchproto_(td_std_real2integer)
332 84
vmod_real2integer(VRT_CTX, VCL_REAL r, VCL_INT i)
333
{
334
        VCL_INT retval;
335
336 84
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
337
338 84
        if (!VRT_REAL_is_valid(r))
339 42
                return (i);
340 42
        retval = (VCL_INT)round(r);
341 42
        if (!VRT_INT_is_valid(retval))
342 0
                return (i);
343 42
        return (retval);
344 84
}
345
346
VCL_TIME v_matchproto_(td_std_real2time)
347 126
vmod_real2time(VRT_CTX, VCL_REAL r, VCL_TIME t)
348
{
349 126
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
350
351 126
        if (!isfinite(r))
352 0
                return (t);
353
354 126
        return (r);
355 126
}
356
357
VCL_INT v_matchproto_(td_std_time2integer)
358 42
vmod_time2integer(VRT_CTX, VCL_TIME t, VCL_INT i)
359
{
360 42
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
361
362 42
        if (!isfinite(t))
363 0
                return (i);
364 42
        t = round(t);
365 42
        if (t > VCL_INT_MAX || t < VCL_INT_MIN)
366 0
                return (i);
367 42
        return ((VCL_INT)t);
368 42
}
369
370
VCL_REAL v_matchproto_(td_std_time2real)
371 42
vmod_time2real(VRT_CTX, VCL_TIME t, VCL_REAL r)
372
{
373 42
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
374
375 42
        if (!isfinite(t))
376 0
                return (r);
377
378 42
        return (t);
379 42
}