diff options
Diffstat (limited to 'bpf-interface.scm')
-rw-r--r-- | bpf-interface.scm | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/bpf-interface.scm b/bpf-interface.scm index 49046b0..5a3be0c 100644 --- a/bpf-interface.scm +++ b/bpf-interface.scm @@ -4,7 +4,7 @@ ;;; Copyright (c) 2013 by Peter Bex, see file COPYING.BSD ;;; (module bpf-interface - (bpf-open bpf-close bpf? bpf-interface-set! bpf-interface) + (bpf-open bpf-close bpf? bpf-buffer-length bpf-interface bpf-interface-set!) (import chicken scheme foreign) @@ -31,7 +31,7 @@ (define ioctl1 (foreign-lambda* int (((c-pointer int) err) (int fd) - (unsigned-long req) (scheme-pointer val1)) + (unsigned-long req) (c-pointer val1)) "int res = ioctl(fd, req, val1);" "*err = errno;" "return(res);")) @@ -77,6 +77,9 @@ (e (exn i/o file) (lp (fx+ i 1) fn e)))) (bpf (make-bpf fd))) (set-finalizer! bpf bpf-close) + ;; Length _must_ be set before interface is assigned + (when buffer-length + (bpf-buffer-length-set! bpf buffer-length)) (bpf-interface-set! bpf interface) bpf))))) @@ -86,6 +89,15 @@ (bpf-fd-set! bpf #f)) (void)) +;; Returns the *actual* size that was set, the requested size is too big +;; Remember, this can only be done *before* setting the interface. +;; Oddly enough, the interface can be switched afterwards, so there's +;; no way to set a new interface *and* buffer-length... +(define (bpf-buffer-length-set! bpf requested-length) + (let-location ((new-length int requested-length)) + (ioctl (bpf-fd bpf) (foreign-value "BIOCSBLEN" int) (location new-length)) + new-length)) + (define interface-name-maximum-length (foreign-value "IF_NAMESIZE" int)) (define (bpf-interface-set! bpf interface) @@ -99,13 +111,18 @@ "char *ifname = ((struct ifreq *)i)->ifr_name;" "strncpy(ifname, s, len);" "ifname[len] = '\\0';") ifreq interface len) - (ioctl (bpf-fd bpf) (foreign-value "BIOCSETIF" int) ifreq) + (ioctl (bpf-fd bpf) (foreign-value "BIOCSETIF" int) (location ifreq)) (void))) (define (bpf-interface bpf) (let ((ifreq (make-blob (foreign-value "sizeof(struct ifreq)" int)))) - (ioctl (bpf-fd bpf) (foreign-value "BIOCGETIF" int) ifreq) + (ioctl (bpf-fd bpf) (foreign-value "BIOCGETIF" int) (location ifreq)) ((foreign-lambda* c-string ((scheme-pointer i)) "C_return(((struct ifreq *)i)->ifr_name);") ifreq))) +(define (bpf-buffer-length bpf) + (let-location ((length int)) + (ioctl (bpf-fd bpf) (foreign-value "BIOCGBLEN" int) (location length)) + length)) + ) |