Commits

Peter Bex committed d88b117

Add stats procedure

Comments (0)

Files changed (1)

bpf-interface.scm

 ;;;       OS X seems to support only the smallest common denominator.
 (module bpf-interface
   (bpf-open bpf-close bpf? bpf-buffer-length bpf-flush!
-            bpf-interface bpf-interface-set!
+            bpf-interface bpf-interface-set! bpf-stats
             bpf-datalink-type bpf-datalink-type-set! bpf-list-datalink-types
             bpf-read-timeout bpf-read-timeout-set!)
 
     (ioctl (bpf-fd bpf) (foreign-value "BIOCSRTIMEOUT" int) (location tv))
     (void)))
 
+(define (bpf-stats bpf)
+  (let ((st (make-blob (foreign-value "sizeof(struct bpf_stat)" int))))
+    (ioctl (bpf-fd bpf) (foreign-value "BIOCGSTATS" int) (location st))
+    (let ((result '()))
+      (let-syntax ((add-stat!
+                    (syntax-rules ()
+                      ((_ ?scheme-name ?c-name)
+                       (set! result
+                         (alist-cons '?scheme-name
+                                     ((foreign-lambda* unsigned-integer64
+                                          (((c-pointer "struct bpf_stat") st))
+                                        "C_return((uint64_t)st->" ?c-name ");")
+                                      (location st))
+                                     result))))))
+        (add-stat! received "bs_recv")
+        (add-stat! dropped "bs_drop")
+        (cond-expand (netbsd (add-stat! captured "bs_capt")))
+        result))))
 
 )