modules/admin-full: rework luci-bwc/libiwinfo integration
[project/luci.git] / modules / admin-full / src / luci-bwc.c
index 9352ce8..05e4667 100644 (file)
@@ -32,7 +32,7 @@
 #include <arpa/inet.h>
 
 #include <dlfcn.h>
-
+#include <iwinfo.h>
 
 #define STEP_COUNT     60
 #define STEP_TIME      1
@@ -47,9 +47,9 @@
 #define DB_LD_FILE     DB_PATH "/load"
 
 #define IF_SCAN_PATTERN \
-       " %[^ :]:%" SCNu64 " %" SCNu64 \
+       " %[^ :]:%u %u" \
        " %*d %*d %*d %*d %*d %*d" \
-       " %" SCNu64 " %" SCNu64
+       " %u %u"
 
 #define LD_SCAN_PATTERN \
        "%f %f %f"
@@ -62,48 +62,34 @@ struct file_map {
 };
 
 struct traffic_entry {
-       uint64_t time;
-       uint64_t rxb;
-       uint64_t rxp;
-       uint64_t txb;
-       uint64_t txp;
+       uint32_t time;
+       uint32_t rxb;
+       uint32_t rxp;
+       uint32_t txb;
+       uint32_t txp;
 };
 
 struct conn_entry {
-       uint64_t time;
+       uint32_t time;
        uint32_t udp;
        uint32_t tcp;
        uint32_t other;
 };
 
 struct load_entry {
-       uint64_t time;
+       uint32_t time;
        uint16_t load1;
        uint16_t load5;
        uint16_t load15;
 };
 
 struct radio_entry {
-       uint64_t time;
+       uint32_t time;
        uint16_t rate;
        uint8_t  rssi;
        uint8_t  noise;
 };
 
-
-static uint64_t htonll(uint64_t value)
-{
-       int num = 1;
-
-       if (*(char *)&num == 1)
-               return htonl((uint32_t)(value & 0xFFFFFFFF)) |
-                      htonl((uint32_t)(value >> 32));
-
-       return value;
-}
-
-#define ntohll htonll
-
 static int readpid(void)
 {
        int fd;
@@ -155,9 +141,7 @@ static void reset_countdown(int sig)
 static char *progname;
 static int prognamelen;
 
-static int (*iw_get_rate)(const char *, int *) = NULL;
-static int (*iw_get_rssi)(const char *, int *) = NULL;
-static int (*iw_get_noise)(const char *, int *) = NULL;
+static struct iwinfo_ops *backend = NULL;
 
 
 static int init_directory(char *path)
@@ -204,9 +188,9 @@ static int init_file(char *path, int esize)
        return -1;
 }
 
-static inline uint64_t timeof(void *entry)
+static inline uint32_t timeof(void *entry)
 {
-       return ((struct traffic_entry *)entry)->time;
+       return ntohl(((struct traffic_entry *)entry)->time);
 }
 
 static int update_file(const char *path, void *entry, int esize)
@@ -267,66 +251,52 @@ static void umap_file(struct file_map *m)
                close(m->fd);
 }
 
-static void * iwinfo_open(void)
+static void * iw_open(void)
 {
-       return dlopen("/usr/lib/lua/iwinfo.so", RTLD_LAZY);
+       return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
 }
 
-static int iwinfo_update(
+static int iw_update(
        void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
 ) {
-       int (*probe)(const char *);
+       struct iwinfo_ops *(*probe)(const char *);
        int val;
 
-       if (!iw_get_rate)
+       if (!backend)
        {
-               if ((probe = dlsym(iw, "nl80211_probe")) != NULL && probe(ifname))
-               {
-                       iw_get_rate  = dlsym(iw, "nl80211_get_bitrate");
-                       iw_get_rssi  = dlsym(iw, "nl80211_get_signal");
-                       iw_get_noise = dlsym(iw, "nl80211_get_noise");
-               }
-               else if ((probe = dlsym(iw, "madwifi_probe")) != NULL && probe(ifname))
-               {
-                       iw_get_rate  = dlsym(iw, "madwifi_get_bitrate");
-                       iw_get_rssi  = dlsym(iw, "madwifi_get_signal");
-                       iw_get_noise = dlsym(iw, "madwifi_get_noise");
-               }
-               else if ((probe = dlsym(iw, "wl_probe")) != NULL && probe(ifname))
-               {
-                       iw_get_rate  = dlsym(iw, "wl_get_bitrate");
-                       iw_get_rssi  = dlsym(iw, "wl_get_signal");
-                       iw_get_noise = dlsym(iw, "wl_get_noise");
-               }
-               else
-               {
+               probe = dlsym(iw, "iwinfo_backend");
+
+               if (!probe)
+                       return 0;
+
+               backend = probe(ifname);
+
+               if (!backend)
                        return 0;
-               }
        }
 
-       *rate = (iw_get_rate && !iw_get_rate(ifname, &val)) ? val : 0;
-       *rssi = (iw_get_rssi && !iw_get_rssi(ifname, &val)) ? val : 0;
-       *noise = (iw_get_noise && !iw_get_noise(ifname, &val)) ? val : 0;
+       *rate = (backend->bitrate && !backend->bitrate(ifname, &val)) ? val : 0;
+       *rssi = (backend->signal && !backend->signal(ifname, &val)) ? val : 0;
+       *noise = (backend->noise && !backend->noise(ifname, &val)) ? val : 0;
 
        return 1;
 }
 
-static void iwinfo_close(void *iw)
+static void iw_close(void *iw)
 {
-       void (*do_close)(void);
+       void (*finish)(void);
+
+       finish = dlsym(iw, "iwinfo_finish");
 
-       if ((do_close = dlsym(iw, "nl80211_close")) != NULL) do_close();
-       if ((do_close = dlsym(iw, "madwifi_close")) != NULL) do_close();
-       if ((do_close = dlsym(iw, "wl_close"))      != NULL) do_close();
-       if ((do_close = dlsym(iw, "wext_close"))    != NULL) do_close();
-       if ((do_close = dlsym(iw, "iwinfo_close"))  != NULL) do_close();
+       if (finish)
+               finish();
 
        dlclose(iw);
 }
 
 
 static int update_ifstat(
-       const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
+       const char *ifname, uint32_t rxb, uint32_t rxp, uint32_t txb, uint32_t txp
 ) {
        char path[1024];
 
@@ -346,11 +316,11 @@ static int update_ifstat(
                }
        }
 
-       e.time = htonll(time(NULL));
-       e.rxb  = htonll(rxb);
-       e.rxp  = htonll(rxp);
-       e.txb  = htonll(txb);
-       e.txp  = htonll(txp);
+       e.time = htonl(time(NULL));
+       e.rxb  = htonl(rxb);
+       e.rxp  = htonl(rxp);
+       e.txb  = htonl(txb);
+       e.txp  = htonl(txp);
 
        return update_file(path, &e, sizeof(struct traffic_entry));
 }
@@ -376,7 +346,7 @@ static int update_radiostat(
                }
        }
 
-       e.time  = htonll(time(NULL));
+       e.time  = htonl(time(NULL));
        e.rate  = htons(rate);
        e.rssi  = rssi;
        e.noise = noise;
@@ -404,7 +374,7 @@ static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
                }
        }
 
-       e.time  = htonll(time(NULL));
+       e.time  = htonl(time(NULL));
        e.udp   = htonl(udp);
        e.tcp   = htonl(tcp);
        e.other = htonl(other);
@@ -432,7 +402,7 @@ static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
                }
        }
 
-       e.time   = htonll(time(NULL));
+       e.time   = htonl(time(NULL));
        e.load1  = htons(load1);
        e.load5  = htons(load5);
        e.load15 = htons(load15);
@@ -443,7 +413,7 @@ static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
 static int run_daemon(void)
 {
        FILE *info;
-       uint64_t rxb, txb, rxp, txp;
+       uint32_t rxb, txb, rxp, txp;
        uint32_t udp, tcp, other;
        uint16_t rate;
        uint8_t rssi, noise;
@@ -494,7 +464,7 @@ static int run_daemon(void)
        }
 
        /* initialize iwinfo */
-       iw = iwinfo_open();
+       iw = iw_open();
 
        /* go */
        for (reset_countdown(0); countdown >= 0; countdown--)
@@ -524,19 +494,19 @@ static int run_daemon(void)
                {
                        for (i = 0; i < 5; i++)
                        {
-#define iwinfo_checkif(pattern) \
+#define iw_checkif(pattern) \
                                do {                                                      \
                                        snprintf(ifname, sizeof(ifname), pattern, i);         \
-                                       if (iwinfo_update(iw, ifname, &rate, &rssi, &noise))  \
+                                       if (iw_update(iw, ifname, &rate, &rssi, &noise))  \
                                        {                                                     \
                                                update_radiostat(ifname, rate, rssi, noise);      \
                                                continue;                                         \
                                        }                                                     \
                                } while(0)
 
-                               iwinfo_checkif("wlan%d");
-                               iwinfo_checkif("ath%d");
-                               iwinfo_checkif("wl%d");
+                               iw_checkif("wlan%d");
+                               iw_checkif("ath%d");
+                               iw_checkif("wl%d");
                        }
                }
 
@@ -551,6 +521,10 @@ static int run_daemon(void)
                                if (strstr(line, "TIME_WAIT"))
                                        continue;
 
+                               if (strstr(line, "src=127.0.0.1 ") &&
+                                   strstr(line, "dst=127.0.0.1 "))
+                                       continue;
+
                                if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
                                {
                                        if (!strcmp(ifname, "tcp"))
@@ -585,7 +559,7 @@ static int run_daemon(void)
        unlink(PID_PATH);
 
        if (iw)
-               iwinfo_close(iw);
+               iw_close(iw);
 
        return 0;
 }
@@ -636,11 +610,11 @@ static int run_dump_ifname(const char *ifname)
                if (!e->time)
                        continue;
 
-               printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
-                          ", %" PRIu64 ", %" PRIu64 " ]%s\n",
-                       ntohll(e->time),
-                       ntohll(e->rxb), ntohll(e->rxp),
-                       ntohll(e->txb), ntohll(e->txp),
+               printf("[ %u, %u, %" PRIu32
+                          ", %u, %u ]%s\n",
+                       ntohl(e->time),
+                       ntohl(e->rxb), ntohl(e->rxp),
+                       ntohl(e->txb), ntohl(e->txp),
                        ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
        }
 
@@ -672,8 +646,8 @@ static int run_dump_radio(const char *ifname)
                if (!e->time)
                        continue;
 
-               printf("[ %" PRIu64 ", %d, %d, %d ]%s\n",
-                       ntohll(e->time),
+               printf("[ %u, %d, %d, %d ]%s\n",
+                       ntohl(e->time),
                        e->rate, e->rssi, e->noise,
                        ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
        }
@@ -706,8 +680,8 @@ static int run_dump_conns(void)
                if (!e->time)
                        continue;
 
-               printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
-                       ntohll(e->time), ntohl(e->udp),
+               printf("[ %u, %u, %u, %u ]%s\n",
+                       ntohl(e->time), ntohl(e->udp),
                        ntohl(e->tcp), ntohl(e->other),
                        ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
        }
@@ -740,8 +714,8 @@ static int run_dump_load(void)
                if (!e->time)
                        continue;
 
-               printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
-                       ntohll(e->time),
+               printf("[ %u, %u, %u, %u ]%s\n",
+                       ntohl(e->time),
                        ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
                        ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
        }