chiark / gitweb /
Import release 0.1.1
[secnet.git] / util.c
diff --git a/util.c b/util.c
index 7e5a39a575e583344ef1b64338470b06b491b38b..503dfe27b3aa0f3fd740b5aef0a258fe6d1d083c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -8,17 +8,16 @@
  *
  */
 
-#include "config.h"
+#include "secnet.h"
 #include <stdio.h>
-#include <stdarg.h>
 #include <string.h>
 #include <errno.h>
-#include <syslog.h>
 #include <unistd.h>
-#include <values.h>
+#include <limits.h>
 #include <assert.h>
+#include <sys/wait.h>
 #include "util.h"
-#include "secnet.h"
+#include "unaligned.h"
 
 #define MIN_BUFFER_SIZE 64
 #define DEFAULT_BUFFER_SIZE 4096
@@ -240,7 +239,12 @@ uint32_t write_mpbin(MP_INT *a, uint8_t *buffer, uint32_t buflen)
     return i;
 }
 
-bool_t subnet_match(struct subnet_list *list, uint32_t address)
+bool_t subnet_match(struct subnet *s, uint32_t address)
+{
+    return (s->prefix==(address&s->mask));
+}
+
+bool_t subnet_matches_list(struct subnet_list *list, uint32_t address)
 {
     uint32_t i;
     for (i=0; i<list->entries; i++) {
@@ -249,6 +253,31 @@ bool_t subnet_match(struct subnet_list *list, uint32_t address)
     return False;
 }
 
+bool_t subnets_intersect(struct subnet a, struct subnet b)
+{
+    uint32_t mask=a.mask&b.mask;
+    return ((a.prefix&mask)==(b.prefix&mask));
+}
+
+bool_t subnet_intersects_with_list(struct subnet a, struct subnet_list *b)
+{
+    uint32_t i;
+
+    for (i=0; i<b->entries; i++) {
+       if (subnets_intersect(a,b->list[i])) return True;
+    }
+    return False;
+}
+
+bool_t subnet_lists_intersect(struct subnet_list *a, struct subnet_list *b)
+{
+    uint32_t i;
+    for (i=0; i<a->entries; i++) {
+       if (subnet_intersects_with_list(a->list[i],b)) return True;
+    }
+    return False;
+}
+
 /* The string buffer must be at least 16 bytes long */
 string_t ipaddr_to_string(uint32_t addr)
 {
@@ -279,10 +308,42 @@ string_t subnet_to_string(struct subnet *sn)
     for (i=0; mask; i++) {
        mask=(mask<<1);
     }
-    snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, i);
+    if (i!=sn->len) {
+       fatal("subnet_to_string: invalid subnet structure!\n");
+    }
+    snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, sn->len);
     return s;
 }
 
+int sys_cmd(const char *path, char *arg, ...)
+{
+    va_list ap;
+    int rv;
+    pid_t c;
+
+    va_start(ap,arg);
+    c=fork();
+    if (c) {
+       /* Parent -> wait for child */
+       waitpid(c,&rv,0);
+    } else if (c==0) {
+       char *args[100];
+       int i;
+       /* Child -> exec command */
+       args[0]=arg;
+       i=1;
+       while ((args[i++]=va_arg(ap,char *)));
+       execvp(path,args);
+       exit(1);
+    } else {
+       /* Error */
+       fatal_perror("sys_cmd(%s,%s,...)");
+    }
+
+    va_end(ap);
+    return rv;
+}
+
 /* Take a list of log closures and merge them */
 struct loglist {
     struct log_if *l;
@@ -387,6 +448,7 @@ static char *phases[NR_PHASES]={
     "PHASE_GETOPTS",
     "PHASE_READCONFIG",
     "PHASE_SETUP",
+    "PHASE_GETRESOURCES",
     "PHASE_DROPPRIV",
     "PHASE_RUN",
     "PHASE_SHUTDOWN"
@@ -396,12 +458,13 @@ void enter_phase(uint32_t new_phase)
 {
     struct phase_hook *i;
 
-    Message(M_DEBUG_PHASE,"entering %s... ", phases[new_phase]);
+    if (hooks[new_phase])
+       Message(M_DEBUG_PHASE,"Running hooks for %s...\n", phases[new_phase]);
     current_phase=new_phase;
 
     for (i=hooks[new_phase]; i; i=i->next)
        i->fn(i->state, new_phase);
-    Message(M_DEBUG_PHASE,"now in %s\n",phases[new_phase]);
+    Message(M_DEBUG_PHASE,"Now in %s\n",phases[new_phase]);
 }
 
 bool_t add_hook(uint32_t phase, hook_fn *fn, void *state)
@@ -491,7 +554,7 @@ void buf_append_string(struct buffer_if *buf, string_t s)
     uint16_t len;
 
     len=strlen(s);
-    *(uint16_t *)buf_append(buf,2)=htons(len);
+    buf_append_uint16(buf,len);
     memcpy(buf_append(buf,len),s,len);
 }
 
@@ -515,19 +578,13 @@ static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
     item_t *item;
     dict_t *dict;
     bool_t lockdown=False;
+    uint32_t len=DEFAULT_BUFFER_SIZE;
     
     st=safe_malloc(sizeof(*st),"buffer_apply");
     st->cl.description="buffer";
     st->cl.type=CL_BUFFER;
     st->cl.apply=NULL;
     st->cl.interface=&st->ops;
-    st->ops.free=True;
-    st->ops.owner=NULL;
-    st->ops.flags=0;
-    st->ops.loc=loc;
-    st->ops.size=0;
-    st->ops.len=DEFAULT_BUFFER_SIZE;
-    st->ops.start=NULL;
 
     /* First argument, if present, is buffer length */
     item=list_elem(args,0);
@@ -536,11 +593,11 @@ static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
            cfgfatal(st->ops.loc,"buffer","first parameter must be a "
                     "number (buffer size)\n");
        }
-       st->ops.len=item->data.number;
-       if (st->ops.len<MIN_BUFFER_SIZE) {
+       len=item->data.number;
+       if (len<MIN_BUFFER_SIZE) {
            cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n");
        }
-       if (st->ops.len>MAX_BUFFER_SIZE) {
+       if (len>MAX_BUFFER_SIZE) {
            cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n");
        }
     }
@@ -556,9 +613,9 @@ static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
                                False);
     }
 
-    st->ops.base=safe_malloc(st->ops.len,"buffer");
+    buffer_new(&st->ops,len);
     if (lockdown) {
-       Message(M_WARNING,"buffer: XXX lockdown\n");
+       /* XXX mlock the buffer if possible */
     }
     
     return new_closure(&st->cl);