varnish-cache/lib/libvmod_directors/hash.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include "cache/cache.h"
35
36
#include "vend.h"
37
#include "vsha256.h"
38
39
#include "vdir.h"
40
41
#include "vcc_if.h"
42
43
struct vmod_directors_hash {
44
        unsigned                                magic;
45
#define VMOD_DIRECTORS_HASH_MAGIC               0xc08dd611
46
        struct vdir                             *vd;
47
};
48
49
static void v_matchproto_(vdi_destroy_f)
50 12
vmod_hash_destroy(VCL_BACKEND dir)
51
{
52
        struct vmod_directors_hash *rr;
53
54 12
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
55 12
        vdir_delete(&rr->vd);
56 12
        FREE_OBJ(rr);
57 12
}
58
59
static const struct vdi_methods vmod_hash_methods[1] = {{
60
        .magic =                VDI_METHODS_MAGIC,
61
        .type =                 "hash",
62
        .destroy =              vmod_hash_destroy
63
}};
64
65
66
VCL_VOID v_matchproto_()
67 24
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
68
    const char *vcl_name)
69
{
70
        struct vmod_directors_hash *rr;
71
72 24
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
73 24
        AN(rrp);
74 24
        AZ(*rrp);
75 24
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
76 24
        AN(rr);
77 24
        *rrp = rr;
78 24
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
79 24
}
80
81
VCL_VOID v_matchproto_()
82 12
vmod_hash__fini(struct vmod_directors_hash **rrp)
83
{
84
        struct vmod_directors_hash *rr;
85
86 12
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
87 12
        VRT_DelDirector(&rr->vd->dir);
88 12
}
89
90
VCL_VOID v_matchproto_()
91 36
vmod_hash_add_backend(VRT_CTX,
92
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
93
{
94
95 36
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
96 36
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
97 36
        vdir_add_backend(ctx, rr->vd, be, w);
98 36
}
99
100
VCL_VOID v_matchproto_()
101 12
vmod_hash_remove_backend(VRT_CTX,
102
    struct vmod_directors_hash *rr, VCL_BACKEND be)
103
{
104
105 12
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
106 12
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
107 12
        vdir_remove_backend(ctx, rr->vd, be, NULL);
108 12
}
109
110
VCL_BACKEND v_matchproto_()
111 144
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr,
112
    const char *arg, ...)
113
{
114
        struct VSHA256Context sha_ctx;
115
        va_list ap;
116
        const char *p;
117
        unsigned char sha256[VSHA256_LEN];
118
        VCL_BACKEND be;
119
        double r;
120
121 144
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
122 144
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
123
124 144
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
125 144
        VSHA256_Init(&sha_ctx);
126 144
        va_start(ap, arg);
127 144
        p = arg;
128 456
        while (p != vrt_magic_string_end) {
129 168
                if (p != NULL && *p != '\0')
130 120
                        VSHA256_Update(&sha_ctx, p, strlen(p));
131 168
                p = va_arg(ap, const char *);
132
        }
133 144
        va_end(ap);
134 144
        VSHA256_Final(sha256, &sha_ctx);
135
136 144
        r = vbe32dec(sha256);
137 144
        r = scalbn(r, -32);
138 144
        assert(r >= 0 && r <= 1.0);
139 144
        be = vdir_pick_be(ctx, rr->vd, r);
140 144
        return (be);
141
}