summaryrefslogtreecommitdiff
path: root/ifstat.c
diff options
context:
space:
mode:
Diffstat (limited to 'ifstat.c')
-rw-r--r--ifstat.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/ifstat.c b/ifstat.c
new file mode 100644
index 0000000..2566e8d
--- /dev/null
+++ b/ifstat.c
@@ -0,0 +1,67 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2004 demon <demon@vhost.dymdns.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <nlist.h>
+#include <limits.h>
+#include <err.h>
+#include "main.h"
+
+kvm_t *kvmd = NULL;
+void *ifhead;
+
+int if_init(void) {
+ char *vmunix = NULL, *core = NULL;
+ char errbuf[_POSIX2_LINE_MAX];
+
+ struct nlist nl[] = { { "_ifnet" }, { NULL } };
+
+ kvmd = kvm_openfiles(vmunix, core, NULL, O_RDONLY, errbuf);
+ if (kvmd == NULL)
+ errx(1,"%s", errbuf);
+
+ if ((kvm_nlist(kvmd, nl)) == -1)
+ errx(1, "%s", kvm_geterr(kvmd));
+
+ if (kvm_read(kvmd, nl->n_value, &ifhead, sizeof(nl->n_value)) != sizeof(nl->n_value))
+ errx(1, "%s", kvm_geterr(kvmd));
+}
+
+int if_stat(char *ifname) {
+ struct ifnet ifnet;
+ void *addr;
+
+ addr = ifhead;
+
+ for (; addr != NULL; addr = (struct ifnet *)ifnet.if_list.tqe_next) {
+ if (kvm_read(kvmd, (unsigned long)addr, &ifnet, sizeof(ifnet)) != sizeof(ifnet))
+ errx(1, "%s", kvm_geterr(kvmd));
+ if (strncmp(ifnet.if_xname, ifname, strlen(ifname)) == NULL) {
+ curr.ib = ifnet.if_ibytes;
+ curr.ob = ifnet.if_obytes;
+ }
+ }
+}
+
+int if_fini(void) {
+ kvm_close(kvmd);
+ exit(0);
+}