Add array_lock and array_unlock.
[apps/pfixtools.git] / postlicyd / rbl.c
index cf1d734..7bf6357 100644 (file)
  */
 
 #include <arpa/inet.h>
-#include <fcntl.h>
 #include <netinet/in.h>
 #include <sys/mman.h>
-#include <sys/stat.h>
 
 #include "common.h"
 #include "rbl.h"
 #include "str.h"
+#include "file.h"
+#include "array.h"
 
 #define IPv4_BITS        5
 #define IPv4_PREFIX(ip)  ((uint32_t)(ip) >> IPv4_BITS)
@@ -59,9 +59,8 @@ enum {
 };
 
 struct rbldb_t {
-    uint32_t len, size;
-    uint32_t *ips;
-    bool     locked;
+    A(uint32_t) ips;
+    bool        locked;
 };
 
 static int get_o(const char *s, const char **out)
@@ -119,35 +118,19 @@ static int parse_ipv4(const char *s, const char **out, uint32_t *ip)
 rbldb_t *rbldb_create(const char *file, bool lock)
 {
     rbldb_t *db;
-    const char *map, *p, *end;
-    struct stat st;
-    int fd;
+    file_map_t map;
+    const char *p, *end;
 
-    fd = open(file, O_RDONLY, 0000);
-    if (fd < 0) {
-        UNIXERR("open");
+    if (!file_map_open(&map, file, false)) {
         return NULL;
     }
 
-    if (fstat(fd, &st) < 0) {
-        UNIXERR("fstat");
-        close(fd);
-        return NULL;
-    }
-
-    p = map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-    if (map == MAP_FAILED) {
-        UNIXERR("mmap");
-        close(fd);
-        return NULL;
-    }
-    close(fd);
-
-    end = map + st.st_size;
-    while (end > map && end[-1] != '\n') {
+    p   = map.map;
+    end = map.end;
+    while (end > p && end[-1] != '\n') {
         --end;
     }
-    if (end != map + st.st_size) {
+    if (end != map.end) {
         syslog(LOG_WARNING, "file %s miss a final \\n, ignoring last line",
                file);
     }
@@ -162,31 +145,28 @@ rbldb_t *rbldb_create(const char *file, bool lock)
         if (parse_ipv4(p, &p, &ip) < 0) {
             p = (char *)memchr(p, '\n', end - p) + 1;
         } else {
-            if (db->len >= db->size) {
-                db->size += 64 * 1024;
-                p_realloc(&db->ips, db->size);
-            }
-            db->ips[db->len++] = ip;
+            array_add(db->ips, ip);
         }
     }
-    munmap((void*)map, st.st_size);
+    file_map_close(&map);
 
     /* Lookup may perform serveral I/O, so avoid swap.
      */
-    db->locked = lock && mlock(db->ips, db->len * sizeof(*(db->ips))) == 0;
+    array_adjust(db->ips);
+    db->locked = lock && array_lock(db->ips);
     if (lock && !db->locked) {
         UNIXERR("mlock");
     }
 
-    if (db->len) {
+    if (db->ips.len) {
 #       define QSORT_TYPE uint32_t
-#       define QSORT_BASE db->ips
-#       define QSORT_NELT db->len
+#       define QSORT_BASE db->ips.data
+#       define QSORT_NELT db->ips.len
 #       define QSORT_LT(a,b) *a < *b
 #       include "qsort.c"
     }
 
-    syslog(LOG_INFO, "rbl %s loaded, %d IPs", file, db->len);
+    syslog(LOG_INFO, "rbl %s loaded, %d IPs", file, db->ips.len);
     return db;
 }
 
@@ -194,29 +174,29 @@ void rbldb_delete(rbldb_t **db)
 {
     if (*db) {
         if ((*db)->locked) {
-            (void)munlock((*db)->ips, (*db)->len * sizeof(*(*db)->ips));
+            array_unlock((*db)->ips);
         }
-        p_delete(&(*db)->ips);
+        array_wipe((*db)->ips);
         p_delete(&(*db));
     }
 }
 
 uint32_t rbldb_stats(rbldb_t *rbl)
 {
-    return rbl->len;
+    return rbl->ips.len;
 }
 
 bool rbldb_ipv4_lookup(rbldb_t *db, uint32_t ip)
 {
-    int l = 0, r = db->len;
+    int l = 0, r = db->ips.len;
 
     while (l < r) {
         int i = (r + l) / 2;
 
-        if (db->ips[i] == ip)
+        if (array_elt(db->ips, i) == ip)
             return true;
 
-        if (ip < db->ips[i]) {
+        if (ip < array_elt(db->ips, i)) {
             r = i;
         } else {
             l = i + 1;