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
51
 * .. INT64_MAX.
52
 *
53
 * We redistrict to VRT_INTEGER_MIN .. VRT_INTEGER_MAX
54
 */
55
56
/* limited by using double for conversions */
57
#define VCL_BYTES_MAX ((INT64_C(1)<<53)-1)
58
59
static
60 7150
int onearg(VRT_CTX, const char *f, int nargs)
61
{
62 7150
        if (nargs == 1)
63 7130
                return (1);
64 40
        VRT_fail(ctx, "std.%s: %s arguments", f,
65 20
            nargs > 1 ? "too many" : "not enough");
66 20
        return (0);
67 7150
}
68
69
/*
70
 * not handling real arg isfinite() / nan() : caller error
71
 * always trunc, never round
72
 */
73
74
VCL_DURATION v_matchproto_(td_std_duration)
75 420
vmod_duration(VRT_CTX, struct VARGS(duration) *a)
76
{
77
        double r;
78
        int nargs;
79
80 420
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
81
82 420
        nargs = a->valid_s + a->valid_real + a->valid_integer;
83
84 420
        if (!onearg(ctx, "duration", nargs))
85 0
                return (0);
86
87 420
        if (a->valid_real)
88 60
                return ((VCL_DURATION)a->real);
89
90 360
        if (a->valid_integer)
91 40
                return ((VCL_DURATION)a->integer);
92
93 320
        if (a->valid_s) {
94 320
                r = VNUM_duration(a->s);
95 320
                if (!isnan(r))
96 170
                        return (r);
97 150
        }
98
99 150
        if (a->valid_fallback)
100 90
                return (a->fallback);
101
102 60
        VRT_fail(ctx, "std.duration: conversion failed");
103 60
        return (0);
104 420
}
105
106
VCL_BYTES v_matchproto_(td_std_bytes)
107 300
vmod_bytes(VRT_CTX, struct VARGS(bytes) *a)
108
{
109
        uintmax_t r;
110
        VCL_REAL rr;
111
        int nargs;
112
        const char *errtxt;
113
114 300
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
115
116 300
        nargs = a->valid_s + a->valid_real + a->valid_integer;
117
118 300
        if (!onearg(ctx, "bytes", nargs))
119 0
                return (0);
120
121 300
        if (a->valid_s) {
122 220
                errtxt = VNUM_2bytes(a->s, &r, 0);
123 220
                if (errtxt == NULL && r <= VCL_BYTES_MAX)
124 90
                        return ((VCL_BYTES)r);
125 130
        }
126
127 210
        if (a->valid_real && !isnan(a->real) && a->real >= 0) {
128 50
                rr = trunc(a->real);
129 50
                if (rr <= (VCL_REAL)VCL_BYTES_MAX)
130 50
                        return ((VCL_BYTES)rr);
131 0
        }
132
133 160
        if (a->valid_integer && a->integer >= 0)
134 30
                return ((VCL_BYTES)a->integer);
135
136 130
        if (a->valid_fallback)
137 120
                return (a->fallback);
138
139 10
        VRT_fail(ctx, "std.bytes: conversion failed");
140 10
        return (0);
141 300
}
142
143
VCL_INT v_matchproto_(td_std_integer)
144 4830
vmod_integer(VRT_CTX, struct VARGS(integer) *a)
145
{
146 4830
        const char *p, *errtxt = NULL;
147
        double r, tmp;
148
        int nargs;
149
150 4830
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
151
152 14490
        nargs = a->valid_s + a->valid_bool + a->valid_bytes +
153 9660
            a->valid_duration + a->valid_real + a->valid_time;
154
155 4830
        if (!onearg(ctx, "integer", nargs))
156 20
                return (0);
157
158 4810
        r = NAN;
159 4810
        if (a->valid_bool)
160 10
                return (a->bool ? 1 : 0);
161
162 4800
        if (a->valid_bytes)
163 20
                return (a->bytes);
164
165 4780
        if (a->valid_s && a->s != NULL) {
166 4450
                p = a->s;
167 4450
                r = SF_Parse_Number(&p, 0, &errtxt);
168 4450
                if (!errno && *p == '\0' && modf(r, &tmp) == 0.0)
169 4290
                        return ((VCL_INT)r);
170 160
                r = NAN;
171 160
        }
172
173 490
        if (a->valid_duration)
174 30
                r = a->duration;
175
176 490
        if (a->valid_real)
177 50
                r = a->real;
178
179 490
        if (a->valid_time)
180 20
                r = a->time;
181
182 490
        if (!isnan(r)) {
183 100
                r = trunc(r);
184 100
                if (r >= VRT_INTEGER_MIN && r <= VRT_INTEGER_MAX)
185 100
                        return ((VCL_INT)r);
186 0
        }
187
188 390
        if (a->valid_fallback)
189 340
                return (a->fallback);
190
191 50
        if (errtxt != NULL)
192 20
                VRT_fail(ctx, "std.integer: conversion failed: %s", errtxt);
193
        else
194 30
                VRT_fail(ctx, "std.integer: conversion failed");
195 50
        return (0);
196 4830
}
197
198
VCL_IP
199 560
vmod_ip(VRT_CTX, struct VARGS(ip) *a)
200
{
201
        uintptr_t sn;
202
        void *p;
203 560
        VCL_IP retval = NULL, fb = bogo_ip;
204
205 560
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
206
207 560
        if (a->valid_fallback) {
208 240
                if (a->fallback == NULL || !VSA_Sane(a->fallback)) {
209 0
                        VRT_fail(ctx, "std.ip: invalid fallback");
210 0
                        return (fb);
211
                }
212 240
                fb = a->fallback;
213 240
        }
214
215 560
        sn = WS_Snapshot(ctx->ws);
216 560
        p = WS_Alloc(ctx->ws, vsa_suckaddr_len);
217 560
        if (p == NULL) {
218 0
                VRT_fail(ctx, "std.ip: insufficient workspace");
219 0
                return (fb);
220
        }
221
222 560
        if (a->s != NULL)
223 550
                retval = VSS_ResolveFirst(
224 550
                    p, a->s, a->valid_p ? a->p : "80",
225
                    AF_UNSPEC, SOCK_STREAM,
226 550
                    a->resolve ? 0 : AI_NUMERICHOST|AI_NUMERICSERV);
227
228 560
        if (retval != NULL)
229 420
                return (retval);
230
231 140
        WS_Reset(ctx->ws, sn);
232
233 140
        if (!a->valid_fallback)
234 0
                VRT_fail(ctx, "std.ip: conversion failed");
235
236 140
        return (fb);
237 560
}
238
239
VCL_REAL v_matchproto_(td_std_real)
240 600
vmod_real(VRT_CTX, struct VARGS(real) *a)
241
{
242
        VCL_REAL r;
243 600
        const char *p, *errtxt = NULL;
244
        int nargs;
245
246 600
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
247
248 1800
        nargs = a->valid_s + a->valid_integer + a->valid_bool + a->valid_bytes +
249 1200
            a->valid_duration + a->valid_time;
250
251 600
        if (!onearg(ctx, "real", nargs))
252 0
                return (0);
253
254 600
        if (a->valid_integer)
255 30
                return ((VCL_REAL)a->integer);
256
257 570
        if (a->valid_bool)
258 10
                return ((VCL_REAL)(a->bool ? 1 : 0));
259
260 560
        if (a->valid_bytes)
261 20
                return ((VCL_REAL)a->bytes);
262
263 540
        if (a->valid_duration)
264 30
                return ((VCL_REAL)a->duration);
265
266 510
        if (a->valid_time)
267 20
                return ((VCL_REAL)a->time);
268
269 490
        if (a->valid_s && a->s != NULL) {
270 420
                p = a->s;
271 420
                r = SF_Parse_Decimal(&p, 0, &errtxt);
272 420
                if (!errno && *p == '\0')
273 350
                        return (r);
274 70
        }
275
276 140
        if (a->valid_fallback)
277 90
                return (a->fallback);
278
279 50
        if (errtxt != NULL)
280 20
                VRT_fail(ctx, "std.real: conversion failed: %s", errtxt);
281
        else
282 30
                VRT_fail(ctx, "std.real: conversion failed");
283 50
        return (0);
284 600
}
285
286
VCL_REAL v_matchproto_(td_std_round)
287 20
vmod_round(VRT_CTX, VCL_REAL r)
288
{
289 20
        (void) ctx;
290 20
        return (round(r));
291
}
292
293
VCL_TIME v_matchproto_(td_std_time)
294 1000
vmod_time(VRT_CTX, struct VARGS(time)* a)
295
{
296
        double r;
297
        int nargs;
298
299 1000
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
300
301 1000
        nargs = a->valid_s + a->valid_real + a->valid_integer;
302
303 1000
        if (!onearg(ctx, "time", nargs))
304 0
                return (0);
305
306 1000
        if (a->valid_integer)
307 20
                return ((VCL_REAL)a->integer);
308
309 980
        if (a->valid_real)
310 30
                return ((VCL_REAL)a->real);
311
312 950
        if (a->valid_s && a->s != NULL) {
313 920
                r = VTIM_parse(a->s);
314 920
                if (r)
315 300
                        return (r);
316
317 620
                r = VNUM(a->s);
318
319 620
                if (!isnan(r) && r > 0)
320 140
                        return (r);
321 480
        }
322
323 510
        if (a->valid_fallback)
324 510
                return (a->fallback);
325
326 0
        VRT_fail(ctx, "std.time: conversion failed");
327 0
        return (0);
328 1000
}
329
330
VCL_STRING v_matchproto_(td_std_strftime)
331 220
vmod_strftime(VRT_CTX, VCL_TIME t, VCL_STRING fmt)
332
{
333
        struct tm tm;
334
        time_t tt;
335
        size_t r;
336
        unsigned spc;
337
        char *s;
338
339 220
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
340
341 220
        tt = (time_t)(intmax_t)t;
342 220
        if (gmtime_r(&tt, &tm) == NULL)
343 0
                return ("");
344
345 220
        spc = WS_ReserveAll(ctx->ws);
346 220
        s = WS_Reservation(ctx->ws);
347 220
        r = strftime(s, spc, fmt, &tm);
348 220
        if (r == 0) {
349 10
                WS_Release(ctx->ws, 0);
350 10
                return ("");
351
        }
352 210
        r++;
353 210
        WS_Release(ctx->ws, r);
354 210
        return (s);
355 220
}
356
357
/* These functions are deprecated as of 2019-03-15 release */
358
359
VCL_INT v_matchproto_(td_std_real2integer)
360 40
vmod_real2integer(VRT_CTX, VCL_REAL r, VCL_INT i)
361
{
362
        VCL_INT retval;
363
364 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
365
366 40
        if (!VRT_REAL_is_valid(r))
367 20
                return (i);
368 20
        retval = (VCL_INT)round(r);
369 20
        if (!VRT_INT_is_valid(retval))
370 0
                return (i);
371 20
        return (retval);
372 40
}
373
374
VCL_TIME v_matchproto_(td_std_real2time)
375 60
vmod_real2time(VRT_CTX, VCL_REAL r, VCL_TIME t)
376
{
377 60
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
378
379 60
        if (!isfinite(r))
380 0
                return (t);
381
382 60
        return (r);
383 60
}
384
385
VCL_INT v_matchproto_(td_std_time2integer)
386 20
vmod_time2integer(VRT_CTX, VCL_TIME t, VCL_INT i)
387
{
388 20
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
389
390 20
        if (!isfinite(t))
391 0
                return (i);
392 20
        t = round(t);
393 20
        if (t > VRT_INTEGER_MAX || t < VRT_INTEGER_MIN)
394 0
                return (i);
395 20
        return ((VCL_INT)t);
396 20
}
397
398
VCL_REAL v_matchproto_(td_std_time2real)
399 20
vmod_time2real(VRT_CTX, VCL_TIME t, VCL_REAL r)
400
{
401 20
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
402
403 20
        if (!isfinite(t))
404 0
                return (r);
405
406 20
        return (t);
407 20
}