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 2
vmod_hash_destroy(VCL_BACKEND dir)
51
{
52
        struct vmod_directors_hash *rr;
53
54 2
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
55 2
        vdir_delete(&rr->vd);
56 2
        FREE_OBJ(rr);
57 2
}
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 4
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
68
    const char *vcl_name)
69
{
70
        struct vmod_directors_hash *rr;
71
72 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
73 4
        AN(rrp);
74 4
        AZ(*rrp);
75 4
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
76 4
        AN(rr);
77 4
        *rrp = rr;
78 4
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
79 4
}
80
81
VCL_VOID v_matchproto_()
82 2
vmod_hash__fini(struct vmod_directors_hash **rrp)
83
{
84
        struct vmod_directors_hash *rr;
85
86
        // XXX 2297
87 2
        if (*rrp == NULL)
88 0
                return;
89
90 2
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
91 2
        VRT_DelDirector(&rr->vd->dir);
92
}
93
94
VCL_VOID v_matchproto_()
95 6
vmod_hash_add_backend(VRT_CTX,
96
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
97
{
98
99 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
100 6
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
101 6
        vdir_add_backend(ctx, rr->vd, be, w);
102 6
}
103
104
VCL_VOID v_matchproto_()
105 2
vmod_hash_remove_backend(VRT_CTX,
106
    struct vmod_directors_hash *rr, VCL_BACKEND be)
107
{
108
109 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
110 2
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
111 2
        vdir_remove_backend(ctx, rr->vd, be, NULL);
112 2
}
113
114
VCL_BACKEND v_matchproto_()
115 24
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr,
116
    const char *arg, ...)
117
{
118
        struct VSHA256Context sha_ctx;
119
        va_list ap;
120
        const char *p;
121
        unsigned char sha256[VSHA256_LEN];
122
        VCL_BACKEND be;
123
        double r;
124
125 24
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
126 24
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
127
128 24
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
129 24
        VSHA256_Init(&sha_ctx);
130 24
        va_start(ap, arg);
131 24
        p = arg;
132 76
        while (p != vrt_magic_string_end) {
133 28
                if (p != NULL && *p != '\0')
134 20
                        VSHA256_Update(&sha_ctx, p, strlen(p));
135 28
                p = va_arg(ap, const char *);
136
        }
137 24
        va_end(ap);
138 24
        VSHA256_Final(sha256, &sha_ctx);
139
140 24
        r = vbe32dec(sha256);
141 24
        r = scalbn(r, -32);
142 24
        assert(r >= 0 && r <= 1.0);
143 24
        be = vdir_pick_be(ctx, rr->vd, r);
144 24
        return (be);
145
}