summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Bex <peter@more-magic.net>2013-05-06 22:09:08 +0200
committerPeter Bex <peter@more-magic.net>2016-03-04 21:38:49 +0100
commitb2cae3951f390ebd0e75cf5a7c668b4f90eb81ce (patch)
tree2d22484cbd36f5ef3aa18ec20c4f47e39fe17901
parentc788574f648ed1d00e29d74e88accf72604928bb (diff)
downloadbpf-b2cae3951f390ebd0e75cf5a7c668b4f90eb81ce.tar.gz
Implement buffer-length option, and export the interface setting/getting procedures as well as the procedure to retrieve the buffer length.
Switch to c-pointer and use LOCATION to get pointers from scheme objects.
-rw-r--r--bpf-interface.scm25
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))
+
)