#include <sys/mman.h>
#include <arpa/inet.h>
+#include <dlfcn.h>
+#include <iwinfo.h>
#define STEP_COUNT 60
#define STEP_TIME 1
#define DB_PATH "/var/lib/luci-bwc"
#define DB_IF_FILE DB_PATH "/if/%s"
+#define DB_RD_FILE DB_PATH "/radio/%s"
#define DB_CN_FILE DB_PATH "/connections"
#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"
};
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;
};
-
-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
+struct radio_entry {
+ uint32_t time;
+ uint16_t rate;
+ uint8_t rssi;
+ uint8_t noise;
+};
static int readpid(void)
{
}
+static char *progname;
+static int prognamelen;
+
+static struct iwinfo_ops *backend = NULL;
+
+
static int init_directory(char *path)
{
char *p = path;
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)
close(m->fd);
}
+static void * iw_open(void)
+{
+ return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
+}
+
+static int iw_update(
+ void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
+) {
+ struct iwinfo_ops *(*probe)(const char *);
+ int val;
+
+ if (!backend)
+ {
+ probe = dlsym(iw, "iwinfo_backend");
+
+ if (!probe)
+ return 0;
+
+ backend = probe(ifname);
+
+ if (!backend)
+ return 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 iw_close(void *iw)
+{
+ void (*finish)(void);
+
+ finish = dlsym(iw, "iwinfo_finish");
+
+ 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];
}
}
- 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));
}
+static int update_radiostat(
+ const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
+) {
+ char path[1024];
+
+ struct stat s;
+ struct radio_entry e;
+
+ snprintf(path, sizeof(path), DB_RD_FILE, ifname);
+
+ if (stat(path, &s))
+ {
+ if (init_file(path, sizeof(struct radio_entry)))
+ {
+ fprintf(stderr, "Failed to init %s: %s\n",
+ path, strerror(errno));
+
+ return -1;
+ }
+ }
+
+ e.time = htonl(time(NULL));
+ e.rate = htons(rate);
+ e.rssi = rssi;
+ e.noise = noise;
+
+ return update_file(path, &e, sizeof(struct radio_entry));
+}
+
static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
{
char path[1024];
}
}
- e.time = htonll(time(NULL));
+ e.time = htonl(time(NULL));
e.udp = htonl(udp);
e.tcp = htonl(tcp);
e.other = htonl(other);
}
}
- e.time = htonll(time(NULL));
+ e.time = htonl(time(NULL));
e.load1 = htons(load1);
e.load5 = htons(load5);
e.load15 = htons(load15);
return update_file(path, &e, sizeof(struct load_entry));
}
-static int run_daemon(char *progname)
+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;
float lf1, lf5, lf15;
char line[1024];
char ifname[16];
-
+ int i;
+ void *iw;
struct sigaction sa;
struct stat s;
return 1;
}
+ /* initialize iwinfo */
+ iw = iw_open();
+
/* go */
for (reset_countdown(0); countdown >= 0; countdown--)
{
/* alter progname for ps, top */
- sprintf(progname, "luci-bwc %d", countdown);
+ memset(progname, 0, prognamelen);
+ snprintf(progname, prognamelen, "luci-bwc %d", countdown);
if ((info = fopen("/proc/net/dev", "r")) != NULL)
{
fclose(info);
}
+ if (iw)
+ {
+ for (i = 0; i < 5; i++)
+ {
+#define iw_checkif(pattern) \
+ do { \
+ snprintf(ifname, sizeof(ifname), pattern, i); \
+ if (iw_update(iw, ifname, &rate, &rssi, &noise)) \
+ { \
+ update_radiostat(ifname, rate, rssi, noise); \
+ continue; \
+ } \
+ } while(0)
+
+ iw_checkif("wlan%d");
+ iw_checkif("ath%d");
+ iw_checkif("wl%d");
+ }
+ }
+
if ((info = fopen(ipc, "r")) != NULL)
{
udp = 0;
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"))
unlink(PID_PATH);
+ if (iw)
+ iw_close(iw);
+
return 0;
}
-static int check_daemon(char *progname)
+static void check_daemon(void)
{
int pid;
if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
{
/* daemon ping failed, try to start it up */
- if (run_daemon(progname))
+ if (run_daemon())
{
fprintf(stderr,
"Failed to ping daemon and unable to start it up: %s\n",
strerror(errno));
- return 1;
+ exit(1);
}
}
else if (kill(pid, SIGUSR1))
{
fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
- return 1;
+ exit(2);
}
-
- return 0;
}
-static int run_dump_ifname(char *progname, const char *ifname)
+static int run_dump_ifname(const char *ifname)
{
int i;
char path[1024];
struct file_map m;
struct traffic_entry *e;
+ check_daemon();
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
- if (check_daemon(progname))
- {
- return 1;
- }
-
if (mmap_file(path, sizeof(struct traffic_entry), &m))
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
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) ? "," : "");
}
return 0;
}
-static int run_dump_conns(char *progname)
+static int run_dump_radio(const char *ifname)
{
int i;
char path[1024];
struct file_map m;
- struct conn_entry *e;
+ struct radio_entry *e;
- snprintf(path, sizeof(path), DB_CN_FILE);
+ check_daemon();
+ snprintf(path, sizeof(path), DB_RD_FILE, ifname);
- if (check_daemon(progname))
+ if (mmap_file(path, sizeof(struct radio_entry), &m))
{
+ fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
return 1;
}
+ for (i = 0; i < m.size; i += sizeof(struct radio_entry))
+ {
+ e = (struct radio_entry *) &m.mmap[i];
+
+ if (!e->time)
+ continue;
+
+ printf("[ %u, %d, %d, %d ]%s\n",
+ ntohl(e->time),
+ e->rate, e->rssi, e->noise,
+ ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
+ }
+
+ umap_file(&m);
+
+ return 0;
+}
+
+static int run_dump_conns(void)
+{
+ int i;
+ char path[1024];
+ struct file_map m;
+ struct conn_entry *e;
+
+ check_daemon();
+ snprintf(path, sizeof(path), DB_CN_FILE);
+
if (mmap_file(path, sizeof(struct conn_entry), &m))
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
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) ? "," : "");
}
return 0;
}
-static int run_dump_load(char *progname)
+static int run_dump_load(void)
{
int i;
char path[1024];
struct file_map m;
struct load_entry *e;
+ check_daemon();
snprintf(path, sizeof(path), DB_LD_FILE);
- if (check_daemon(progname))
- {
- return 1;
- }
-
if (mmap_file(path, sizeof(struct load_entry), &m))
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
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) ? "," : "");
}
{
int opt;
- while ((opt = getopt(argc, argv, "t:i:cl")) > -1)
+ progname = argv[0];
+ prognamelen = -1;
+
+ for (opt = 0; opt < argc; opt++)
+ prognamelen += 1 + strlen(argv[opt]);
+
+ while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
{
switch (opt)
{
case 'i':
if (optarg)
- return run_dump_ifname(argv[0], optarg);
+ return run_dump_ifname(optarg);
+ break;
+
+ case 'r':
+ if (optarg)
+ return run_dump_radio(optarg);
break;
case 'c':
- return run_dump_conns(argv[0]);
+ return run_dump_conns();
case 'l':
- return run_dump_load(argv[0]);
+ return run_dump_load();
default:
break;
fprintf(stderr,
"Usage:\n"
" %s [-t timeout] -i ifname\n"
+ " %s [-t timeout] -r radiodev\n"
" %s [-t timeout] -c\n"
" %s [-t timeout] -l\n",
- argv[0], argv[0], argv[0]
+ argv[0], argv[0], argv[0], argv[0]
);
return 1;