|  | varnish-cache/lib/libvcc/vcc_utils.c | 
| 0 | 
   | 
  /*-  | 
| 1 | 
   | 
   * Copyright (c) 2006 Verdens Gang AS  | 
| 2 | 
   | 
   * Copyright (c) 2006-2015 Varnish Software AS  | 
| 3 | 
   | 
   * All rights reserved.  | 
| 4 | 
   | 
   *  | 
| 5 | 
   | 
   * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>  | 
| 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 | 
   | 
  #include "config.h"  | 
| 32 | 
   | 
   | 
| 33 | 
   | 
  #include <math.h>  | 
| 34 | 
   | 
  #include <string.h>  | 
| 35 | 
   | 
  #include <stdlib.h>  | 
| 36 | 
   | 
  #include <sys/socket.h>  | 
| 37 | 
   | 
  #include <sys/types.h>  | 
| 38 | 
   | 
  #include <unistd.h>  | 
| 39 | 
   | 
   | 
| 40 | 
   | 
  #include "vcc_compile.h"  | 
| 41 | 
   | 
   | 
| 42 | 
   | 
  #include "vre.h"  | 
| 43 | 
   | 
  #include "vnum.h"  | 
| 44 | 
   | 
  #include "vsa.h"  | 
| 45 | 
   | 
  #include "vss.h"  | 
| 46 | 
   | 
  #include "vtcp.h"  | 
| 47 | 
   | 
   | 
| 48 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 49 | 
   | 
   | 
| 50 | 
   | 
  static void  | 
| 51 | 
  348800 | 
  vcc_cstrcat(struct vcc *tl, struct vsb *vsb)  | 
| 52 | 
   | 
  { | 
| 53 | 
   | 
          struct token *t1;  | 
| 54 | 
   | 
   | 
| 55 | 
  348800 | 
          assert(tl->t->tok == CSTR);  | 
| 56 | 
  348800 | 
          VSB_cat(vsb, tl->t->dec);  | 
| 57 | 
   | 
   | 
| 58 | 
  348800 | 
          t1 = vcc_PeekToken(tl);  | 
| 59 | 
  348800 | 
          AN(t1);  | 
| 60 | 
   | 
   | 
| 61 | 
  348960 | 
          while (t1->tok == '+') { | 
| 62 | 
  200 | 
                  vcc_NextToken(tl);  | 
| 63 | 
  200 | 
                  SkipToken(tl, '+');  | 
| 64 | 
  200 | 
                  ExpectErr(tl, CSTR);  | 
| 65 | 
  160 | 
                  VSB_cat(vsb, tl->t->dec);  | 
| 66 | 
  160 | 
                  t1 = vcc_PeekToken(tl);  | 
| 67 | 
  160 | 
                  AN(t1);  | 
| 68 | 
   | 
          }  | 
| 69 | 
  348800 | 
  }  | 
| 70 | 
   | 
   | 
| 71 | 
   | 
  void  | 
| 72 | 
  348800 | 
  vcc_regexp(struct vcc *tl, struct vsb *vgc_name)  | 
| 73 | 
   | 
  { | 
| 74 | 
   | 
          struct vsb *pattern;  | 
| 75 | 
   | 
          struct token *t0;  | 
| 76 | 
   | 
          char buf[BUFSIZ];  | 
| 77 | 
   | 
          vre_t *t;  | 
| 78 | 
   | 
          int error, erroroffset;  | 
| 79 | 
   | 
          struct inifin *ifp;  | 
| 80 | 
   | 
   | 
| 81 | 
  348800 | 
          t0 = tl->t;  | 
| 82 | 
  348800 | 
          pattern = VSB_new_auto();  | 
| 83 | 
  348800 | 
          AN(pattern);  | 
| 84 | 
  348800 | 
          vcc_cstrcat(tl, pattern);  | 
| 85 | 
  348800 | 
          AZ(VSB_finish(pattern));  | 
| 86 | 
   | 
   | 
| 87 | 
  348800 | 
          t = VRE_compile(VSB_data(pattern), 0, &error, &erroroffset, 0);  | 
| 88 | 
  348800 | 
          if (t == NULL) { | 
| 89 | 
  80 | 
                  VSB_cat(tl->sb, "Regexp compilation error:\n\n");  | 
| 90 | 
  80 | 
                  AZ(VRE_error(tl->sb, error));  | 
| 91 | 
  80 | 
                  VSB_cat(tl->sb, "\n\n");  | 
| 92 | 
  80 | 
                  vcc_ErrWhere2(tl, t0, tl->t);  | 
| 93 | 
  80 | 
                  VSB_destroy(&pattern);  | 
| 94 | 
  80 | 
                  return;  | 
| 95 | 
   | 
          }  | 
| 96 | 
  348720 | 
          VRE_free(&t);  | 
| 97 | 
  348720 | 
          bprintf(buf, "VGC_re_%u", tl->unique++);  | 
| 98 | 
  348720 | 
          if (vgc_name)  | 
| 99 | 
  348720 | 
                  VSB_cat(vgc_name, buf);  | 
| 100 | 
   | 
   | 
| 101 | 
  348720 | 
          Fh(tl, 0, "static struct vre *%s;\n", buf);  | 
| 102 | 
  348720 | 
          ifp = New_IniFin(tl);  | 
| 103 | 
  348720 | 
          VSB_printf(ifp->ini, "\tVPI_re_init(&%s, ",buf);  | 
| 104 | 
  348720 | 
          VSB_quote(ifp->ini, VSB_data(pattern), -1, VSB_QUOTE_CSTR);  | 
| 105 | 
  348720 | 
          VSB_cat(ifp->ini, ");");  | 
| 106 | 
  348720 | 
          VSB_printf(ifp->fin, "\t\tVPI_re_fini(%s);", buf);  | 
| 107 | 
  348720 | 
          VSB_destroy(&pattern);  | 
| 108 | 
  348800 | 
  }  | 
| 109 | 
   | 
   | 
| 110 | 
   | 
  /*  | 
| 111 | 
   | 
   * The IPv6 crew royally screwed up the entire idea behind  | 
| 112 | 
   | 
   * struct sockaddr, see libvarnish/vsa.c for blow-by-blow account.  | 
| 113 | 
   | 
   *  | 
| 114 | 
   | 
   * There is no sane or even remotely portable way to initialize  | 
| 115 | 
   | 
   * a sockaddr for random protocols at compile time.  | 
| 116 | 
   | 
   *  | 
| 117 | 
   | 
   * In our case it is slightly more tricky than that, because we don't  | 
| 118 | 
   | 
   * even want to #include the struct sockaddr* definitions.  | 
| 119 | 
   | 
   *  | 
| 120 | 
   | 
   * Instead we make sure the sockaddr is sane (for our values of  | 
| 121 | 
   | 
   * sane) and dump it as our own "struct suckaddr" type, in binary,  | 
| 122 | 
   | 
   * using the widest integer type, hoping that this will ensure sufficient  | 
| 123 | 
   | 
   * alignment.  | 
| 124 | 
   | 
   */  | 
| 125 | 
   | 
   | 
| 126 | 
   | 
  static void  | 
| 127 | 
  56040 | 
  vcc_suckaddr(struct vcc *tl, const char *host, const struct suckaddr *vsa,  | 
| 128 | 
   | 
      const char **ip, const char **ip_ascii, const char **p_ascii)  | 
| 129 | 
   | 
  { | 
| 130 | 
   | 
          char a[VTCP_ADDRBUFSIZE];  | 
| 131 | 
   | 
          char p[VTCP_PORTBUFSIZE];  | 
| 132 | 
  56040 | 
          const int sz = sizeof(unsigned long long);  | 
| 133 | 
  56040 | 
          const unsigned n = (vsa_suckaddr_len + sz - 1) / sz;  | 
| 134 | 
  56040 | 
          unsigned long long b[n];  | 
| 135 | 
   | 
          unsigned len;  | 
| 136 | 
   | 
          char *q;  | 
| 137 | 
   | 
   | 
| 138 | 
  56040 | 
          VTCP_name(vsa, a, sizeof a, p, sizeof p);  | 
| 139 | 
  56040 | 
          Fh(tl, 0, "\n/* \"%s\" -> %s */\n", host, a);  | 
| 140 | 
  56040 | 
          if (ip_ascii != NULL)  | 
| 141 | 
  54960 | 
                  *ip_ascii = TlDup(tl, a);  | 
| 142 | 
  56040 | 
          if (p_ascii != NULL && *p_ascii == NULL)  | 
| 143 | 
  54920 | 
                  *p_ascii = TlDup(tl, p);  | 
| 144 | 
   | 
   | 
| 145 | 
  56040 | 
          Fh(tl, 0, "static const unsigned long long");  | 
| 146 | 
  56040 | 
          Fh(tl, 0, " suckaddr_%u[%d] = {\n", tl->unique, n); | 
| 147 | 
  56040 | 
          memcpy(b, vsa, vsa_suckaddr_len);  | 
| 148 | 
  280200 | 
          for (len = 0; len < n; len++)  | 
| 149 | 
  448320 | 
                  Fh(tl, 0, "%s    0x%0*llxULL",  | 
| 150 | 
  224160 | 
                      len ? ",\n" : "", sz * 2, b[len]);  | 
| 151 | 
  56040 | 
          Fh(tl, 0, "\n};\n");  | 
| 152 | 
   | 
   | 
| 153 | 
  56040 | 
          q = TlAlloc(tl, 40);  | 
| 154 | 
  56040 | 
          AN(q);  | 
| 155 | 
  56040 | 
          assert(snprintf(q, 40, "(const void*)suckaddr_%u", tl->unique) < 40);  | 
| 156 | 
  56040 | 
          *ip = q;  | 
| 157 | 
  56040 | 
          tl->unique++;  | 
| 158 | 
  56040 | 
  }  | 
| 159 | 
   | 
   | 
| 160 | 
   | 
  /*--------------------------------------------------------------------  | 
| 161 | 
   | 
   * This routine is a monster, but at least we only have one such monster.  | 
| 162 | 
   | 
   * Look up a IP number, and return IPv4/IPv6 address as VGC produced names  | 
| 163 | 
   | 
   * and optionally ascii strings.  | 
| 164 | 
   | 
   *  | 
| 165 | 
   | 
   * For IP compile time constants we only want one IP#, but it can be  | 
| 166 | 
   | 
   * IPv4 or IPv6.  | 
| 167 | 
   | 
   *  | 
| 168 | 
   | 
   * For backends, we accept up to one IPv4 and one IPv6.  | 
| 169 | 
   | 
   */  | 
| 170 | 
   | 
   | 
| 171 | 
   | 
  struct rss { | 
| 172 | 
   | 
          unsigned                magic;  | 
| 173 | 
   | 
  #define RSS_MAGIC               0x11e966ab  | 
| 174 | 
   | 
   | 
| 175 | 
   | 
          const struct suckaddr   *vsa4;  | 
| 176 | 
   | 
          const struct suckaddr   *vsa6;  | 
| 177 | 
   | 
          struct vsb              *vsb;  | 
| 178 | 
   | 
          int                     retval;  | 
| 179 | 
   | 
          int                     wrong;  | 
| 180 | 
   | 
  };  | 
| 181 | 
   | 
   | 
| 182 | 
   | 
  static int v_matchproto_(vss_resolved_f)  | 
| 183 | 
  56120 | 
  rs_callback(void *priv, const struct suckaddr *vsa)  | 
| 184 | 
   | 
  { | 
| 185 | 
   | 
          struct rss *rss;  | 
| 186 | 
   | 
          int v;  | 
| 187 | 
   | 
          char a[VTCP_ADDRBUFSIZE];  | 
| 188 | 
   | 
          char p[VTCP_PORTBUFSIZE];  | 
| 189 | 
   | 
   | 
| 190 | 
  56120 | 
          CAST_OBJ_NOTNULL(rss, priv, RSS_MAGIC);  | 
| 191 | 
  56120 | 
          assert(VSA_Sane(vsa));  | 
| 192 | 
   | 
   | 
| 193 | 
  56120 | 
          v = VSA_Get_Proto(vsa);  | 
| 194 | 
  56120 | 
          assert(v != AF_UNIX);  | 
| 195 | 
  56120 | 
          VTCP_name(vsa, a, sizeof a, p, sizeof p);  | 
| 196 | 
  56120 | 
          VSB_printf(rss->vsb, "\t%s:%s\n", a, p);  | 
| 197 | 
  56120 | 
          if (v == AF_INET) { | 
| 198 | 
  55520 | 
                  if (rss->vsa4 == NULL)  | 
| 199 | 
  55440 | 
                          rss->vsa4 = VSA_Clone(vsa);  | 
| 200 | 
  80 | 
                  else if (VSA_Compare(vsa, rss->vsa4))  | 
| 201 | 
  80 | 
                          rss->wrong++;  | 
| 202 | 
  55520 | 
                  rss->retval++;  | 
| 203 | 
  56120 | 
          } else if (v == AF_INET6) { | 
| 204 | 
  600 | 
                  if (rss->vsa6 == NULL)  | 
| 205 | 
  600 | 
                          rss->vsa6 = VSA_Clone(vsa);  | 
| 206 | 
  0 | 
                  else if (VSA_Compare(vsa, rss->vsa6))  | 
| 207 | 
  0 | 
                          rss->wrong++;  | 
| 208 | 
  600 | 
                  rss->retval++;  | 
| 209 | 
  600 | 
          }  | 
| 210 | 
  56120 | 
          return (0);  | 
| 211 | 
   | 
  }  | 
| 212 | 
   | 
   | 
| 213 | 
   | 
  void  | 
| 214 | 
  56160 | 
  Resolve_Sockaddr(struct vcc *tl,  | 
| 215 | 
   | 
      const char *host,  | 
| 216 | 
   | 
      const char *def_port,  | 
| 217 | 
   | 
      const char **ipv4,  | 
| 218 | 
   | 
      const char **ipv4_ascii,  | 
| 219 | 
   | 
      const char **ipv6,  | 
| 220 | 
   | 
      const char **ipv6_ascii,  | 
| 221 | 
   | 
      const char **p_ascii,  | 
| 222 | 
   | 
      int maxips,  | 
| 223 | 
   | 
      const struct token *t_err,  | 
| 224 | 
   | 
      const char *errid)  | 
| 225 | 
   | 
  { | 
| 226 | 
   | 
          int error;  | 
| 227 | 
   | 
          struct rss rss[1];  | 
| 228 | 
   | 
          const char *err;  | 
| 229 | 
   | 
   | 
| 230 | 
  56160 | 
          *ipv4 = NULL;  | 
| 231 | 
  56160 | 
          *ipv6 = NULL;  | 
| 232 | 
  56160 | 
          if (p_ascii != NULL)  | 
| 233 | 
  55000 | 
                  *p_ascii = NULL;  | 
| 234 | 
   | 
   | 
| 235 | 
  56160 | 
          INIT_OBJ(rss, RSS_MAGIC);  | 
| 236 | 
  56160 | 
          rss->vsb = VSB_new_auto();  | 
| 237 | 
  56160 | 
          AN(rss->vsb);  | 
| 238 | 
   | 
   | 
| 239 | 
  56160 | 
          error = VSS_resolver(host, def_port, rs_callback, rss, &err);  | 
| 240 | 
  56160 | 
          AZ(VSB_finish(rss->vsb));  | 
| 241 | 
  56160 | 
          if (err != NULL) { | 
| 242 | 
  320 | 
                  VSB_printf(tl->sb,  | 
| 243 | 
   | 
                      "%s '%.*s' could not be resolved to an IP address:\n"  | 
| 244 | 
   | 
                      "\t%s\n"  | 
| 245 | 
   | 
                      "(Sorry if that error message is gibberish.)\n",  | 
| 246 | 
  160 | 
                      errid, PF(t_err), err);  | 
| 247 | 
  160 | 
                  vcc_ErrWhere(tl, t_err);  | 
| 248 | 
  160 | 
                  if (rss->vsa4 != NULL)  | 
| 249 | 
  0 | 
                          VSA_free(&rss->vsa4);  | 
| 250 | 
  160 | 
                  if (rss->vsa6 != NULL)  | 
| 251 | 
  0 | 
                          VSA_free(&rss->vsa6);  | 
| 252 | 
  160 | 
                  VSB_destroy(&rss->vsb);  | 
| 253 | 
  160 | 
                  ZERO_OBJ(rss, sizeof rss);  | 
| 254 | 
  160 | 
                  return;  | 
| 255 | 
   | 
          }  | 
| 256 | 
  56000 | 
          AZ(error);  | 
| 257 | 
  56000 | 
          if (rss->vsa4 != NULL) { | 
| 258 | 
  55440 | 
                  vcc_suckaddr(tl, host, rss->vsa4, ipv4, ipv4_ascii, p_ascii);  | 
| 259 | 
  55440 | 
                  VSA_free(&rss->vsa4);  | 
| 260 | 
  55440 | 
          }  | 
| 261 | 
  56000 | 
          if (rss->vsa6 != NULL) { | 
| 262 | 
  600 | 
                  vcc_suckaddr(tl, host, rss->vsa6, ipv6, ipv6_ascii, p_ascii);  | 
| 263 | 
  600 | 
                  VSA_free(&rss->vsa6);  | 
| 264 | 
  600 | 
          }  | 
| 265 | 
  56000 | 
          if (rss->retval == 0) { | 
| 266 | 
  0 | 
                  VSB_printf(tl->sb,  | 
| 267 | 
   | 
                      "%s '%.*s': resolves to "  | 
| 268 | 
   | 
                      "neither IPv4 nor IPv6 addresses.\n",  | 
| 269 | 
  0 | 
                      errid, PF(t_err) );  | 
| 270 | 
  0 | 
                  vcc_ErrWhere(tl, t_err);  | 
| 271 | 
  0 | 
          }  | 
| 272 | 
  56000 | 
          if (rss->wrong || rss->retval > maxips) { | 
| 273 | 
  80 | 
                  VSB_printf(tl->sb,  | 
| 274 | 
   | 
                      "%s %.*s: resolves to too many addresses.\n"  | 
| 275 | 
   | 
                      "Only one IPv4 %s IPv6 are allowed.\n"  | 
| 276 | 
   | 
                      "Please specify which exact address "  | 
| 277 | 
   | 
                      "you want to use, we found all of these:\n%s",  | 
| 278 | 
  40 | 
                      errid, PF(t_err),  | 
| 279 | 
  40 | 
                      maxips > 1 ? "and one" :  "or",  | 
| 280 | 
  40 | 
                      VSB_data(rss->vsb));  | 
| 281 | 
  40 | 
                  vcc_ErrWhere(tl, t_err);  | 
| 282 | 
  40 | 
          }  | 
| 283 | 
  56000 | 
          VSB_destroy(&rss->vsb);  | 
| 284 | 
  56000 | 
          ZERO_OBJ(rss, sizeof rss);  | 
| 285 | 
  56160 | 
  }  | 
| 286 | 
   | 
   | 
| 287 | 
   | 
  /*--------------------------------------------------------------------  | 
| 288 | 
   | 
  * Recognize boolean const "true" or "false"  | 
| 289 | 
   | 
  */  | 
| 290 | 
   | 
   | 
| 291 | 
   | 
  uint8_t  | 
| 292 | 
  160 | 
  vcc_BoolVal(struct vcc *tl)  | 
| 293 | 
   | 
  { | 
| 294 | 
   | 
          struct symbol* sym;  | 
| 295 | 
   | 
   | 
| 296 | 
  160 | 
          if (tl->t->tok != ID) { | 
| 297 | 
  40 | 
                  VSB_cat(tl->sb, "Expected \"true\" or \"false\"\n");  | 
| 298 | 
  40 | 
                  vcc_ErrWhere(tl, tl->t);  | 
| 299 | 
  40 | 
                  return (0);  | 
| 300 | 
   | 
          }  | 
| 301 | 
  120 | 
          sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_NOERR, XREF_NONE);  | 
| 302 | 
  120 | 
          if (sym == NULL || sym->type != BOOL) { | 
| 303 | 
  40 | 
                  VSB_cat(tl->sb, "Expected \"true\" or \"false\"\n");  | 
| 304 | 
  40 | 
                  vcc_ErrWhere(tl, tl->t);  | 
| 305 | 
  40 | 
                  return (0);  | 
| 306 | 
   | 
          }  | 
| 307 | 
  80 | 
          return (sym->eval_priv != NULL);  | 
| 308 | 
  160 | 
  }  | 
| 309 | 
   | 
   | 
| 310 | 
   | 
  /*--------------------------------------------------------------------  | 
| 311 | 
   | 
   * Recognize and convert units of duration, return seconds.  | 
| 312 | 
   | 
   */  | 
| 313 | 
   | 
   | 
| 314 | 
   | 
  double  | 
| 315 | 
  127000 | 
  vcc_DurationUnit(struct vcc *tl)  | 
| 316 | 
   | 
  { | 
| 317 | 
   | 
          double sc;  | 
| 318 | 
   | 
   | 
| 319 | 
  127000 | 
          assert(tl->t->tok == ID);  | 
| 320 | 
  127000 | 
          sc = VNUM_duration_unit(1.0, tl->t->b, tl->t->e);  | 
| 321 | 
  127000 | 
          if (!isnan(sc)) { | 
| 322 | 
  126960 | 
                  vcc_NextToken(tl);  | 
| 323 | 
  126960 | 
                  return (sc);  | 
| 324 | 
   | 
          }  | 
| 325 | 
  40 | 
          VSB_cat(tl->sb, "Unknown duration unit ");  | 
| 326 | 
  40 | 
          vcc_ErrToken(tl, tl->t);  | 
| 327 | 
  40 | 
          VSB_printf(tl->sb, "\n%s\n", VNUM_LEGAL_DURATION);  | 
| 328 | 
  40 | 
          vcc_ErrWhere(tl, tl->t);  | 
| 329 | 
  40 | 
          return (1.0);  | 
| 330 | 
  127000 | 
  }  | 
| 331 | 
   | 
   | 
| 332 | 
   | 
  /*--------------------------------------------------------------------  | 
| 333 | 
   | 
   * Recognize and convert { CNUM } to unsigned value | 
| 334 | 
   | 
   * The tokenizer made sure we only get digits.  | 
| 335 | 
   | 
   */  | 
| 336 | 
   | 
   | 
| 337 | 
   | 
  uint64_t  | 
| 338 | 
  8840 | 
  vcc_UintVal(struct vcc *tl)  | 
| 339 | 
   | 
  { | 
| 340 | 
   | 
          int64_t retval;  | 
| 341 | 
   | 
   | 
| 342 | 
  8840 | 
          if (tl->t->tok != CNUM) { | 
| 343 | 
  0 | 
                  Expect(tl, CNUM);  | 
| 344 | 
  0 | 
                  return (0);  | 
| 345 | 
   | 
          }  | 
| 346 | 
  8840 | 
          retval = (int64_t)round(tl->t->num);  | 
| 347 | 
  8840 | 
          if (retval < 0) { | 
| 348 | 
  0 | 
                  VSB_cat(tl->sb, "UINT cannot be negative\n");  | 
| 349 | 
  0 | 
                  vcc_ErrWhere(tl, tl->t);  | 
| 350 | 
  0 | 
                  return (0);  | 
| 351 | 
   | 
          }  | 
| 352 | 
  8840 | 
          vcc_NextToken(tl);  | 
| 353 | 
  8840 | 
          return (retval);  | 
| 354 | 
  8840 | 
  }  | 
| 355 | 
   | 
   | 
| 356 | 
   | 
  static double  | 
| 357 | 
  1920 | 
  vcc_DoubleVal(struct vcc *tl)  | 
| 358 | 
   | 
  { | 
| 359 | 
   | 
          double retval;  | 
| 360 | 
   | 
   | 
| 361 | 
  1920 | 
          if (tl->t->tok != CNUM && tl->t->tok != FNUM) { | 
| 362 | 
  40 | 
                  Expect(tl, CNUM);  | 
| 363 | 
  40 | 
                  return (0);  | 
| 364 | 
   | 
          }  | 
| 365 | 
  1880 | 
          retval = tl->t->num;  | 
| 366 | 
  1880 | 
          vcc_NextToken(tl);  | 
| 367 | 
  1880 | 
          return (retval);  | 
| 368 | 
  1920 | 
  }  | 
| 369 | 
   | 
   | 
| 370 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 371 | 
   | 
   | 
| 372 | 
   | 
  void  | 
| 373 | 
  1920 | 
  vcc_Duration(struct vcc *tl, double *d)  | 
| 374 | 
   | 
  { | 
| 375 | 
   | 
          double v, sc;  | 
| 376 | 
   | 
   | 
| 377 | 
  1920 | 
          v = vcc_DoubleVal(tl);  | 
| 378 | 
  1920 | 
          ERRCHK(tl);  | 
| 379 | 
  1880 | 
          ExpectErr(tl, ID);  | 
| 380 | 
  1880 | 
          sc = vcc_DurationUnit(tl);  | 
| 381 | 
  1880 | 
          *d = v * sc;  | 
| 382 | 
  1920 | 
  }  | 
| 383 | 
   | 
   | 
| 384 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 385 | 
   | 
   | 
| 386 | 
   | 
  void  | 
| 387 | 
  2000 | 
  vcc_ByteVal(struct vcc *tl, VCL_INT *d)  | 
| 388 | 
   | 
  { | 
| 389 | 
   | 
          double v;  | 
| 390 | 
   | 
          VCL_INT retval;  | 
| 391 | 
   | 
          const char *errtxt;  | 
| 392 | 
   | 
   | 
| 393 | 
  2000 | 
          if (tl->t->tok != CNUM && tl->t->tok != FNUM) { | 
| 394 | 
  0 | 
                  Expect(tl, CNUM);  | 
| 395 | 
  0 | 
                  return;  | 
| 396 | 
   | 
          }  | 
| 397 | 
  2000 | 
          v = tl->t->num;  | 
| 398 | 
  2000 | 
          vcc_NextToken(tl);  | 
| 399 | 
  2000 | 
          if (tl->t->tok != ID) { | 
| 400 | 
  40 | 
                  VSB_cat(tl->sb, "Expected BYTES unit (B, KB, MB...) got ");  | 
| 401 | 
  40 | 
                  vcc_ErrToken(tl, tl->t);  | 
| 402 | 
  40 | 
                  VSB_cat(tl->sb, "\n");  | 
| 403 | 
  40 | 
                  vcc_ErrWhere(tl, tl->t);  | 
| 404 | 
  40 | 
                  return;  | 
| 405 | 
   | 
          }  | 
| 406 | 
  1960 | 
          retval = VNUM_bytes_unit(v, tl->t->b, tl->t->e, 0, &errtxt);  | 
| 407 | 
  1960 | 
          if (errno) { | 
| 408 | 
  40 | 
                  VSB_cat(tl->sb, errtxt);  | 
| 409 | 
  40 | 
                  vcc_ErrToken(tl, tl->t);  | 
| 410 | 
  40 | 
                  vcc_ErrWhere(tl, tl->t);  | 
| 411 | 
  40 | 
                  return;  | 
| 412 | 
   | 
          }  | 
| 413 | 
  1920 | 
          vcc_NextToken(tl);  | 
| 414 | 
  1920 | 
          *d = retval;  | 
| 415 | 
  2000 | 
  }  | 
| 416 | 
   | 
   | 
| 417 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 418 | 
   | 
   | 
| 419 | 
   | 
  int  | 
| 420 | 
  4440 | 
  vcc_IsFlagRaw(struct vcc *tl, const struct token *t1, const struct token *t2)  | 
| 421 | 
   | 
  { | 
| 422 | 
   | 
   | 
| 423 | 
  4440 | 
          if (t1->tok != '-' && t1->tok != '+')  | 
| 424 | 
  3440 | 
                  return (-1);  | 
| 425 | 
  1000 | 
          if (t2->b != t1->e) { | 
| 426 | 
  40 | 
                  VSB_cat(tl->sb, "Expected a flag at:\n");  | 
| 427 | 
  40 | 
                  vcc_ErrWhere(tl, t1);  | 
| 428 | 
  40 | 
                  return (-1);  | 
| 429 | 
   | 
          }  | 
| 430 | 
  960 | 
          return (t1->tok == '+' ? 1 : 0);  | 
| 431 | 
  4440 | 
  }  | 
| 432 | 
   | 
   | 
| 433 | 
   | 
  int  | 
| 434 | 
  2400 | 
  vcc_IsFlag(struct vcc *tl)  | 
| 435 | 
   | 
  { | 
| 436 | 
   | 
          struct token *t;  | 
| 437 | 
   | 
          int retval;  | 
| 438 | 
   | 
   | 
| 439 | 
  2400 | 
          t = vcc_PeekToken(tl);  | 
| 440 | 
  2400 | 
          if (t == NULL)  | 
| 441 | 
  0 | 
                  return (-1);  | 
| 442 | 
  2400 | 
          retval = vcc_IsFlagRaw(tl, tl->t, t);  | 
| 443 | 
  2400 | 
          if (retval >= 0)  | 
| 444 | 
  720 | 
                  vcc_NextToken(tl);  | 
| 445 | 
  2400 | 
          return (retval);  | 
| 446 | 
  2400 | 
  }  | 
| 447 | 
   | 
   | 
| 448 | 
   | 
  char *  | 
| 449 | 
  31858280 | 
  vcc_Dup_be(const char *b, const char *e)  | 
| 450 | 
   | 
  { | 
| 451 | 
   | 
          char *p;  | 
| 452 | 
   | 
   | 
| 453 | 
  31858280 | 
          AN(b);  | 
| 454 | 
  31858280 | 
          if (e == NULL)  | 
| 455 | 
  0 | 
                  e = strchr(b, '\0');  | 
| 456 | 
  31858280 | 
          AN(e);  | 
| 457 | 
  31858280 | 
          assert(e >= b);  | 
| 458 | 
   | 
   | 
| 459 | 
  31858280 | 
          p = strndup(b, e - b);  | 
| 460 | 
  31858280 | 
          AN(p);  | 
| 461 | 
  31858280 | 
          return (p);  | 
| 462 | 
   | 
  }  | 
| 463 | 
   | 
   | 
| 464 | 
   | 
  int  | 
| 465 | 
  99760 | 
  vcc_Has_vcl_prefix(const char *b)  | 
| 466 | 
   | 
  { | 
| 467 | 
  90960 | 
          return (  | 
| 468 | 
  99760 | 
              (b[0] == 'v' || b[0] == 'V') &&  | 
| 469 | 
  8920 | 
              (b[1] == 'c' || b[1] == 'C') &&  | 
| 470 | 
  99880 | 
              (b[2] == 'l' || b[2] == 'L') &&  | 
| 471 | 
  120 | 
              (b[3] == '_')  | 
| 472 | 
   | 
          );  | 
| 473 | 
   | 
  }  |