From 9d4201b6a3bd6fe1f98cdc08eb1a98795937c0ac Mon Sep 17 00:00:00 2001 From: phk Date: Sat, 22 Jul 2006 08:02:47 +0000 Subject: [PATCH] Split IP/ACL compilation into vcc_acl.c git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@555 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/include/vrt_obj.h | 4 +- varnish-cache/lib/libvcl/Makefile.am | 1 + varnish-cache/lib/libvcl/vcc_acl.c | 135 +++++++++++++++++++++ varnish-cache/lib/libvcl/vcc_compile.c | 143 ++--------------------- varnish-cache/lib/libvcl/vcc_compile.h | 18 +++ varnish-cache/lib/libvcl/vcc_gen_obj.tcl | 2 + varnish-cache/lib/libvcl/vcc_obj.c | 6 + 7 files changed, 176 insertions(+), 133 deletions(-) create mode 100644 varnish-cache/lib/libvcl/vcc_acl.c diff --git a/varnish-cache/include/vrt_obj.h b/varnish-cache/include/vrt_obj.h index 98bc72af..9ab2334c 100644 --- a/varnish-cache/include/vrt_obj.h +++ b/varnish-cache/include/vrt_obj.h @@ -1,5 +1,5 @@ /* - * $Id: vcc_gen_obj.tcl 548 2006-07-21 20:51:24Z phk $ + * $Id: vcc_gen_obj.tcl 550 2006-07-21 21:13:43Z phk $ * * NB: This file is machine generated, DO NOT EDIT! * @@ -10,6 +10,8 @@ const char * VRT_r_backend_host(struct backend *); void VRT_l_backend_host(struct backend *, const char *); const char * VRT_r_backend_port(struct backend *); void VRT_l_backend_port(struct backend *, const char *); +const unsigned char * VRT_r_client_ip(struct sess *); +void VRT_l_client_ip(struct sess *, const unsigned char *); const char * VRT_r_req_request(struct sess *); void VRT_l_req_request(struct sess *, const char *); const char * VRT_r_req_url(struct sess *); diff --git a/varnish-cache/lib/libvcl/Makefile.am b/varnish-cache/lib/libvcl/Makefile.am index ce34282b..023b7f60 100644 --- a/varnish-cache/lib/libvcl/Makefile.am +++ b/varnish-cache/lib/libvcl/Makefile.am @@ -9,6 +9,7 @@ libvcl_la_SOURCES = \ vcc_compile.h \ vcc_token_defs.h \ \ + vcc_acl.c \ vcc_compile.c \ vcc_fixed_token.c \ vcc_obj.c \ diff --git a/varnish-cache/lib/libvcl/vcc_acl.c b/varnish-cache/lib/libvcl/vcc_acl.c new file mode 100644 index 00000000..128260b4 --- /dev/null +++ b/varnish-cache/lib/libvcl/vcc_acl.c @@ -0,0 +1,135 @@ +/* + * $Id$ + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vcc_priv.h" +#include "vcc_compile.h" + +#include "libvcl.h" + +unsigned +vcc_IpVal(struct tokenlist *tl) +{ + unsigned u, v; + struct token *t; + + t = tl->t; + u = UintVal(tl); + if (u < 256) { + v = u << 24; + Expect(tl, '.'); + vcc_NextToken(tl); + t = tl->t; + u = UintVal(tl); + if (u < 256) { + v |= u << 16; + Expect(tl, '.'); + vcc_NextToken(tl); + t = tl->t; + u = UintVal(tl); + if (u < 256) { + v |= u << 8; + Expect(tl, '.'); + vcc_NextToken(tl); + t = tl->t; + u = UintVal(tl); + if (u < 256) { + v |= u; + return (v); + } + } + } + } + sbuf_printf(tl->sb, "Illegal octet in IP number\n"); + vcc_ErrWhere(tl, t); + return (0); +} + +void +vcc_Cond_Ip(struct var *vp, struct tokenlist *tl) +{ + unsigned u; + + switch (tl->t->tok) { + case '~': + vcc_NextToken(tl); + ExpectErr(tl, ID); + AddRef(tl, tl->t, R_ACL); + Fc(tl, 1, "ip_match(%s, acl_%T)\n", vp->rname, tl->t); + vcc_NextToken(tl); + break; + case T_EQ: + case T_NEQ: + Fc(tl, 1, "%s %T ", vp->rname, tl->t); + vcc_NextToken(tl); + u = vcc_IpVal(tl); + Fc(tl, 0, "%uU /* %u.%u.%u.%u */\n", u, + (u >> 24) & 0xff, (u >> 16) & 0xff, + (u >> 8) & 0xff, (u) & 0xff); + break; + default: + sbuf_printf(tl->sb, "Illegal condition "); + vcc_ErrToken(tl, tl->t); + sbuf_printf(tl->sb, " on IP number variable\n"); + sbuf_printf(tl->sb, " only '==', '!=' and '~' are legal\n"); + vcc_ErrWhere(tl, tl->t); + break; + } +} + +void +vcc_Acl(struct tokenlist *tl) +{ + unsigned u, m; + + vcc_NextToken(tl); + + ExpectErr(tl, ID); + AddDef(tl, tl->t, R_ACL); + Fh(tl, 0, "static struct vcl_acl acl_%T[];\n", tl->t); + Fc(tl, 1, "static struct vcl_acl acl_%T[] = {\n", tl->t); + vcc_NextToken(tl); + + tl->indent += INDENT; + + ExpectErr(tl, '{'); + vcc_NextToken(tl); + + while (tl->t->tok == CNUM) { + u = vcc_IpVal(tl); + if (tl->t->tok == '/') { + vcc_NextToken(tl); + ExpectErr(tl, CNUM); + m = UintVal(tl); + } else + m = 32; + ExpectErr(tl, ';'); + vcc_NextToken(tl); + Fc(tl, 1, "{ %11uU, %3uU }, /* %u.%u.%u.%u/%u */\n", + u, m, + (u >> 24) & 0xff, (u >> 16) & 0xff, + (u >> 8) & 0xff, (u) & 0xff, m); + } + ExpectErr(tl, '}'); + Fc(tl, 1, "{ %11uU, %3uU }\n", 0, 0); + + tl->indent -= INDENT; + + Fc(tl, 1, "};\n\n"); + vcc_NextToken(tl); +} diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index 909c0ce4..0f5b0262 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -56,8 +56,6 @@ #include "libvcl.h" -#define ERRCHK(tl) do { if ((tl)->err) return; } while (0) - static struct method method_tab[] = { #define VCL_RET_MAC(a,b,c,d) #define VCL_MET_MAC(a,b,c) { "vcl_"#a, "default_vcl_"#a, c }, @@ -77,11 +75,6 @@ const char *vcc_default_vcl_b, *vcc_default_vcl_e; /*--------------------------------------------------------------------*/ -#define ErrInternal(tl) vcc__ErrInternal(tl, __func__, __LINE__) - -#define Expect(a, b) vcc__Expect(a, b, __LINE__) -#define ExpectErr(a, b) do { vcc__Expect(a, b, __LINE__); ERRCHK(a);} while (0) - #define L(tl, foo) do { \ tl->indent += INDENT; \ foo; \ @@ -97,7 +90,7 @@ const char *vcc_default_vcl_b, *vcc_default_vcl_e; * Printf output to the two sbufs, possibly indented */ -static void +void Fh(struct tokenlist *tl, int indent, const char *fmt, ...) { va_list ap; @@ -109,7 +102,7 @@ Fh(struct tokenlist *tl, int indent, const char *fmt, ...) va_end(ap); } -static void +void Fc(struct tokenlist *tl, int indent, const char *fmt, ...) { va_list ap; @@ -210,7 +203,7 @@ FindRefStr(struct tokenlist *tl, const char *s, enum ref_type type) return (0); } -static void +void AddRef(struct tokenlist *tl, struct token *t, enum ref_type type) { @@ -230,7 +223,7 @@ AddRefStr(struct tokenlist *tl, const char *s, enum ref_type type) AddRef(tl, t, type); } -static void +void AddDef(struct tokenlist *tl, struct token *t, enum ref_type type) { struct ref *r; @@ -321,7 +314,7 @@ RateUnit(struct tokenlist *tl) * Recognize and convert { CNUM } to unsigned value */ -static unsigned +unsigned UintVal(struct tokenlist *tl) { unsigned d = 0; @@ -367,46 +360,6 @@ DoubleVal(struct tokenlist *tl) /*--------------------------------------------------------------------*/ -static unsigned -IpVal(struct tokenlist *tl) -{ - unsigned u, v; - struct token *t; - - t = tl->t; - u = UintVal(tl); - if (u < 256) { - v = u << 24; - Expect(tl, '.'); - vcc_NextToken(tl); - t = tl->t; - u = UintVal(tl); - if (u < 256) { - v |= u << 16; - Expect(tl, '.'); - vcc_NextToken(tl); - t = tl->t; - u = UintVal(tl); - if (u < 256) { - v |= u << 8; - Expect(tl, '.'); - vcc_NextToken(tl); - t = tl->t; - u = UintVal(tl); - if (u < 256) { - v |= u; - return (v); - } - } - } - } - sbuf_printf(tl->sb, "Illegal octet in IP number\n"); - vcc_ErrWhere(tl, t); - return (0); -} - -/*--------------------------------------------------------------------*/ - static struct var * HeaderVar(struct tokenlist *tl __unused, struct token *t, struct var *vh) { @@ -493,38 +446,6 @@ RateVal(struct tokenlist *tl) /*--------------------------------------------------------------------*/ -static void -Cond_Ip(struct var *vp, struct tokenlist *tl) -{ - unsigned u; - - switch (tl->t->tok) { - case '~': - vcc_NextToken(tl); - ExpectErr(tl, ID); - AddRef(tl, tl->t, R_ACL); - Fc(tl, 1, "ip_match(%s, acl_%T)\n", vp->rname, tl->t); - vcc_NextToken(tl); - break; - case T_EQ: - case T_NEQ: - Fc(tl, 1, "%s %T ", vp->rname, tl->t); - vcc_NextToken(tl); - u = IpVal(tl); - Fc(tl, 0, "%uU /* %u.%u.%u.%u */\n", u, - (u >> 24) & 0xff, (u >> 16) & 0xff, - (u >> 8) & 0xff, (u) & 0xff); - break; - default: - sbuf_printf(tl->sb, "Illegal condition "); - vcc_ErrToken(tl, tl->t); - sbuf_printf(tl->sb, " on IP number variable\n"); - sbuf_printf(tl->sb, " only '==', '!=' and '~' are legal\n"); - vcc_ErrWhere(tl, tl->t); - break; - } -} - static void Cond_String(struct var *vp, struct tokenlist *tl) { @@ -631,7 +552,7 @@ Cond_2(struct tokenlist *tl) case INT: L(tl, Cond_Int(vp, tl)); break; case SIZE: L(tl, Cond_Int(vp, tl)); break; case BOOL: L(tl, Cond_Bool(vp, tl)); break; - case IP: L(tl, Cond_Ip(vp, tl)); break; + case IP: L(tl, vcc_Cond_Ip(vp, tl)); break; case STRING: L(tl, Cond_String(vp, tl)); break; case TIME: L(tl, Cond_Int(vp, tl)); break; /* XXX backend == */ @@ -742,7 +663,7 @@ IfStmt(struct tokenlist *tl) static void Action(struct tokenlist *tl) { - unsigned a, u; + unsigned a; struct var *vp; struct token *at; @@ -825,10 +746,11 @@ Action(struct tokenlist *tl) Fc(tl, 0, "%g", DoubleVal(tl)); Fc(tl, 0, ");\n"); break; +#if 0 /* XXX: enable if we find a legit use */ case IP: if (tl->t->tok == '=') { vcc_NextToken(tl); - u = IpVal(tl); + u = vcc_IpVal(tl); Fc(tl, 0, "= %uU; /* %u.%u.%u.%u */\n", u, (u >> 24) & 0xff, @@ -843,6 +765,7 @@ Action(struct tokenlist *tl) " only '=' is legal for IP numbers\n"); vcc_ErrWhere(tl, tl->t); return; +#endif case BACKEND: if (tl->t->tok == '=') { vcc_NextToken(tl); @@ -873,50 +796,6 @@ Action(struct tokenlist *tl) /*--------------------------------------------------------------------*/ -static void -Acl(struct tokenlist *tl) -{ - unsigned u, m; - - vcc_NextToken(tl); - - ExpectErr(tl, ID); - AddDef(tl, tl->t, R_ACL); - Fh(tl, 0, "static struct vcl_acl acl_%T[];\n", tl->t); - Fc(tl, 1, "static struct vcl_acl acl_%T[] = {\n", tl->t); - vcc_NextToken(tl); - - tl->indent += INDENT; - - ExpectErr(tl, '{'); - vcc_NextToken(tl); - - while (tl->t->tok == CNUM) { - u = IpVal(tl); - if (tl->t->tok == '/') { - vcc_NextToken(tl); - ExpectErr(tl, CNUM); - m = UintVal(tl); - } else - m = 32; - ExpectErr(tl, ';'); - vcc_NextToken(tl); - Fc(tl, 1, "{ %11uU, %3uU }, /* %u.%u.%u.%u/%u */\n", - u, m, - (u >> 24) & 0xff, (u >> 16) & 0xff, - (u >> 8) & 0xff, (u) & 0xff, m); - } - ExpectErr(tl, '}'); - Fc(tl, 1, "{ %11uU, %3uU }\n", 0, 0); - - tl->indent -= INDENT; - - Fc(tl, 1, "};\n\n"); - vcc_NextToken(tl); -} - -/*--------------------------------------------------------------------*/ - static void Compound(struct tokenlist *tl) { @@ -1099,7 +978,7 @@ Parse(struct tokenlist *tl) ERRCHK(tl); switch (tl->t->tok) { case T_ACL: - Acl(tl); + vcc_Acl(tl); break; case T_SUB: Function(tl); diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index 254c78c9..fd13be1e 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -94,14 +94,26 @@ struct proc { /*--------------------------------------------------------------------*/ +/* vcc_acl.c */ + +void vcc_Acl(struct tokenlist *tl); +unsigned vcc_IpVal(struct tokenlist *tl); +void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl); + /* vcc_compile.c */ extern const char *vcc_default_vcl_b, *vcc_default_vcl_e; +void Fh(struct tokenlist *tl, int indent, const char *fmt, ...); +void Fc(struct tokenlist *tl, int indent, const char *fmt, ...); +unsigned UintVal(struct tokenlist *tl); +void AddDef(struct tokenlist *tl, struct token *t, enum ref_type type); +void AddRef(struct tokenlist *tl, struct token *t, enum ref_type type); /* vcc_obj.c */ extern struct var vcc_be_vars[]; extern struct var vcc_vars[]; extern const char *vrt_obj_h; + /* vcc_token.c */ void vcc_ErrToken(struct tokenlist *tl, struct token *t); void vcc_ErrWhere(struct tokenlist *tl, struct token *t); @@ -112,3 +124,9 @@ void vcc_Lexer(struct tokenlist *tl, const char *b, const char *e); void vcc_NextToken(struct tokenlist *tl); void vcc__ErrInternal(struct tokenlist *tl, const char *func, unsigned line); void vcc_AddToken(struct tokenlist *tl, unsigned tok, const char *b, const char *e); + +#define ERRCHK(tl) do { if ((tl)->err) return; } while (0) +#define ErrInternal(tl) vcc__ErrInternal(tl, __func__, __LINE__) +#define Expect(a, b) vcc__Expect(a, b, __LINE__) +#define ExpectErr(a, b) do { vcc__Expect(a, b, __LINE__); ERRCHK(a);} while (0) + diff --git a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl index 963c3e43..6c628f94 100755 --- a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl +++ b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl @@ -12,6 +12,7 @@ set beobj { # Objects which operate on sessions set spobj { + { client.ip IP } { req.request STRING } { req.url STRING } { req.proto STRING } @@ -22,6 +23,7 @@ set spobj { { req.http. HEADER } } +set tt(IP) "const unsigned char *" set tt(STRING) "const char *" set tt(BOOL) "double" set tt(BACKEND) "struct backend *" diff --git a/varnish-cache/lib/libvcl/vcc_obj.c b/varnish-cache/lib/libvcl/vcc_obj.c index 1a169d18..e9133916 100644 --- a/varnish-cache/lib/libvcl/vcc_obj.c +++ b/varnish-cache/lib/libvcl/vcc_obj.c @@ -22,6 +22,10 @@ struct var vcc_be_vars[] = { }; struct var vcc_vars[] = { + { "client.ip", IP, 9, + "VRT_r_client_ip(sp)", + "VRT_l_client_ip(sp, ", + }, { "req.request", STRING, 11, "VRT_r_req_request(sp)", "VRT_l_req_request(sp, ", @@ -70,6 +74,8 @@ const char *vrt_obj_h = "void VRT_l_backend_host(struct backend *, const char *);\n" "const char * VRT_r_backend_port(struct backend *);\n" "void VRT_l_backend_port(struct backend *, const char *);\n" + "const unsigned char * VRT_r_client_ip(struct sess *);\n" + "void VRT_l_client_ip(struct sess *, const unsigned char *);\n" "const char * VRT_r_req_request(struct sess *);\n" "void VRT_l_req_request(struct sess *, const char *);\n" "const char * VRT_r_req_url(struct sess *);\n" -- 2.39.5