Changeset 125a593dd802ff9e00102dcfa8de54c1422c307e

Show
Ignore:
Timestamp:
03/14/08 05:14:37 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1205471677 +0000
git-parent:

[43a88c5d8b1393c3016a9639e2e8812dd203bbf6]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1205471677 +0000
Message:

implementation of the apply stuff

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • configure.in

    r6afd275 r125a593  
    7676AC_CHECK_LIB(termcap, tputs, , ) 
    7777AC_CHECK_LIB(curses, clear, , ) 
     78AC_CHECK_LIB(pcre, pcre_compile, , 
     79        [ 
     80                AC_MSG_ERROR([libpcre not found, but required]) 
     81        ] 
     82) 
    7883AC_CHECK_LIB(crypto, ERR_load_CRYPTO_strings, , 
    7984        [ 
  • src/noit_conf_checks.c

    r4cdbcdb r125a593  
    255255  if(!pobj || pobj->type != XPATH_NODESET || 
    256256     xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { 
    257     nc_printf(ncct, "no checks found\n"); 
     257    nc_printf(ncct, "no checks found for '%s'\n", wanted); 
    258258    goto out; 
    259259  } 
  • src/noit_console_state.c

    r4cdbcdb r125a593  
    1212#include "noit_console.h" 
    1313#include "noit_tokenizer.h" 
     14 
     15#include <netinet/in.h> 
     16#include <arpa/inet.h> 
     17#include <pcre.h> 
    1418 
    1519cmd_info_t console_command_exit = { 
     
    4246    memcpy(cp, value, vlen);                        /* copy replacement */ 
    4347    cp += vlen;                                     /* advance destination */ 
    44     searchstart += patlen;                          /* set new searchstart */ 
     48    searchstart = nextpat + patlen;                 /* set new searchstart */ 
    4549  } 
    4650  /* Pick up the trailer (plus '\0') */ 
     
    4852  return result; 
    4953} 
     54static pcre *IP_match = NULL; 
     55static pcre *numeric_match = NULL; 
    5056static int 
    51 expand_range(const char *range, char ***set) { 
    52   int count; 
    53   /* (full:)?(\d+).(\d+)\.(\d+)(?:\.(\d+))?(?:/(\d+))? */ 
    54   /* (\d+)(?:,(\d+))?\.\.(\d+) */ 
    55 /* FIXME: TEST! */ 
    56   *set = malloc(10 * sizeof(*set)); 
    57   for(count=0;count<10;count++) { 
    58     (*set)[count] = strdup("00-test-expand"); 
    59     snprintf((*set)[count], 3, "%02d", count); 
    60     (*set)[count][2] = '-'; 
    61   } 
    62  
    63   return count; 
     57expand_range(const char *range, char ***set, int max_count, const char **err) { 
     58  int count, erroff, ovector[30], rv; 
     59  char buff[32]; /* submatches */ 
     60  const char *pcre_err; 
     61  *err = NULL; 
     62  if(!IP_match) { 
     63    IP_match = pcre_compile("^(full:)?(\\d+\\.\\d+\\.\\d+\\.\\d+)/(\\d+)$", 
     64                            0, &pcre_err, &erroff, NULL); 
     65    if(!IP_match) { 
     66      *err = "IP match pattern failed to compile!"; 
     67      noitL(noit_error, "pcre_compiled failed offset %d: %s\n", erroff, pcre_err); 
     68      return -1; 
     69    } 
     70  } 
     71  if(!numeric_match) { 
     72    numeric_match = pcre_compile("^(\\d+)(?:,(\\d+))?\\.\\.(\\d+)$", 
     73                                 0, &pcre_err, &erroff, NULL); 
     74    if(!numeric_match) { 
     75      *err = "numeric match pattern failed to compile!"; 
     76      noitL(noit_error, "pcre_compiled failed offset %d: %s\n", erroff, pcre_err); 
     77      return -1; 
     78    } 
     79  } 
     80  rv = pcre_exec(IP_match, NULL, range, strlen(range), 0, 0, ovector, 30); 
     81  if(rv >= 0) { 
     82    int mask, full = 0, i; 
     83    u_int32_t host_addr; 
     84    struct in_addr addr; 
     85    /* 0 is the full monty, 1 is "" or "full:", 2 is the IP, 3 is the mask */ 
     86    pcre_copy_substring(range, ovector, rv, 1, buff, sizeof(buff)); 
     87    full = buff[0] ? 1 : 0; 
     88    pcre_copy_substring(range, ovector, rv, 3, buff, sizeof(buff)); 
     89    mask = atoi(buff); 
     90    if(mask == 32) full = 1; /* host implies.. the host */ 
     91    if(mask < 0 || mask > 32) { 
     92      *err = "invalid netmask"; 
     93      return 0; 
     94    } 
     95    count = 1 << (32-mask); 
     96    pcre_copy_substring(range, ovector, rv, 2, buff, sizeof(buff)); 
     97    if(inet_pton(AF_INET, buff, &addr) != 1) { 
     98      *err = "could not parse IP address"; 
     99      return 0; 
     100    } 
     101    host_addr = ntohl(addr.s_addr); 
     102    host_addr &= ~((u_int32_t)count - 1); 
     103 
     104    if(!full) count -= 2; /* No network or broadcast */ 
     105    if(count > max_count || !count) return -count; 
     106    if(!full) host_addr++; /* Skip the network address */ 
     107 
     108    *set = malloc(count * sizeof(**set)); 
     109    for(i=0; i<count; i++)  { 
     110      addr.s_addr = htonl(host_addr + i); 
     111      inet_ntop(AF_INET, &addr, buff, sizeof(buff)); 
     112      (*set)[i] = strdup(buff); 
     113    } 
     114    return count; 
     115  } 
     116  rv = pcre_exec(numeric_match, NULL, range, strlen(range), 0, 0, ovector, 30); 
     117  if(rv >= 0) { 
     118    int s, n, e, i; 
     119    pcre_copy_substring(range, ovector, rv, 1, buff, sizeof(buff)); 
     120    s = atoi(buff); 
     121    pcre_copy_substring(range, ovector, rv, 3, buff, sizeof(buff)); 
     122    e = atoi(buff); 
     123    pcre_copy_substring(range, ovector, rv, 2, buff, sizeof(buff)); 
     124    if(buff[0]) n = atoi(buff); 
     125    else n = (s<e) ? s+1 : s-1; 
     126 
     127    /* Ensure that s < n < e */ 
     128    if((s<e && s>n) || (s>e && s<n)) { 
     129      *err = "mixed up sequence"; 
     130      return 0; 
     131    } 
     132    i = n - s; /* Our increment */ 
     133    count = (e - s) / i + 1; 
     134    *set = malloc(count * sizeof(**set)); 
     135    count = 0; 
     136    for(; (i>0 && s<e) || (i<0 && s>e); s += i) { 
     137      snprintf(buff, sizeof(buff), "%d", s); 
     138      (*set)[count] = strdup(buff); 
     139      count++; 
     140    } 
     141    return count; 
     142  } 
     143  *err = "cannot understand range"; 
     144  return 0; 
    64145} 
    65146int 
     
    71152  char *name, *range; 
    72153  char **nargv, **expanded; 
     154  const char *err; 
    73155  int problems = 0; 
    74156  if(argc < 3) { 
     
    81163  argv += 2; 
    82164 
    83   count = expand_range(range, &expanded); 
     165  count = expand_range(range, &expanded, 256, &err); 
    84166  if(!count) { 
    85     nc_printf(ncct, "apply error: '%s' range produced nothing\n", range); 
     167    nc_printf(ncct, "apply error: '%s' range produced nothing [%s]\n", 
     168              range, err ? err : "unknown error"); 
    86169    return -1; 
    87170  } 
     
    159242    } 
    160243    if(!cmd) { 
    161       if(ambiguous) { 
    162         nc_printf(ncct, "Ambiguous command: '%s'\n", argv[0]); 
    163         amb = next; 
    164         for(amb = next; amb; noit_skiplist_next(&stack->state->cmds, &amb)) { 
     244      if(ambiguous || !strcmp(argv[0], "?")) { 
     245        char *partial = ambiguous ? argv[0] : ""; 
     246        if(ambiguous) nc_printf(ncct, "Ambiguous command: '%s'\n", argv[0]); 
     247        amb = ambiguous ? next : noit_skiplist_getlist(&stack->state->cmds); 
     248        for(; amb; noit_skiplist_next(&stack->state->cmds, &amb)) { 
    165249          cmd = amb->data; 
    166           if(strncasecmp(cmd->name, argv[0], strlen(argv[0])) == 0) 
     250          if(!strlen(partial) || strncasecmp(cmd->name, partial, strlen(partial)) == 0) 
    167251            nc_printf(ncct, "\t%s\n", cmd->name); 
    168252          else