Hi,
I'm new to CFFI and also to Common Lisp, i trying to use CFFI to call getifaddrs function.
I have defined a cffi-grovel for ifaddrss struct
(cstruct ifaddrs "struct ifaddrs" (next "ifa_next" :type :pointer) (name "ifa_name" :type :pointer) (flags "ifa_flags" :type :unsigned-int) (address "ifa_addr" :type :pointer) (netmask "ifa_netmask" :type :pointer) (broadcast "ifa_broadaddr" :type :pointer) (point-to-point-destination "ifa_dstaddr" :type :pointer) (data "ifa_data" :type :pointer))
In my package i define getifaddrs
(cffi:defcfun "getifaddrs" :int "" (ifap :pointer))
I try to access the name slot of ifaddrs struct but nothing seems to work, not sure what i doing wrong
(cffi:with-foreign-object (ifa 'ifaddrs)
(ice-cffi:getifaddrs ifa)
(cffi:foreign-string-to-lisp (cffi:foreign-slot-value ifa 'ifaddrs 'name)))
0: (SB-SYS:MEMORY-FAULT-ERROR) 1: ("foreign function: call_into_lisp") 2: ("foreign function: post_signal_tramp") 3: (CFFI::FOREIGN-STRING-LENGTH #.(SB-SYS:INT-SAP #X00000060) :ENCODING :UTF-8 :OFFSET 0) 4: (CFFI:FOREIGN-STRING-TO-LISP #.(SB-SYS:INT-SAP #X00000060) :OFFSET 0 :COUNT NIL :MAX-CHARS 536870908 :ENCODING NIL)
(cffi:with-foreign-pointer (ifa (cffi:foreign-type-size :pointer))
(ice-cffi:getifaddrs ifa)
(cffi:foreign-string-to-lisp (cffi:foreign-slot-value ifa 'ifaddrs 'name))) 0: ((LAMBDA (BABEL-ENCODINGS::SRC BABEL-ENCODINGS::START BABEL-ENCODINGS::END BABEL-ENCODINGS::DEST BABEL-ENCODINGS::D-START)) #.(SB-SYS:INT-SAP #X080819B8) 0 9 "^@^@^@^@^@^@^@^@^@" 0) 1: (CFFI:FOREIGN-STRING-TO-LISP #.(SB-SYS:INT-SAP #X080819B8) :OFFSET 0 :COUNT NIL :MAX-CHARS 536870908 :ENCODING NIL)
any help will be appreciated.
Jose
Hello,
On Sun, Dec 12, 2010 at 1:10 AM, pepone.onrez pepone.onrez@gmail.com wrote:
(cffi:with-foreign-object (ifa 'ifaddrs) (ice-cffi:getifaddrs ifa) (cffi:foreign-string-to-lisp (cffi:foreign-slot-value ifa 'ifaddrs 'name)))
Here's getifaddrs's signature: int getifaddrs(struct ifaddrs **ifap);
getifaddrs() allocates the actual struct. Here's how it would be called using C code:
struct ifaddrs *p; getifaddrs(&p); // do something with p->name
The equivalent CFFI code would be something like:
(with-foreign-object (p :pointer) (getifaddrs p) (foreign-slot-value (mem-ref p :pointer) 'ifaddrs 'name))
If you define NAME's type to be :STRING, that code will yield a Lisp string.
HTH,
On Sun, Dec 12, 2010 at 1:45 AM, Luís Oliveira luismbo@gmail.com wrote:
The equivalent CFFI code would be something like:
(with-foreign-object (p :pointer) (getifaddrs p) (foreign-slot-value (mem-ref p :pointer) 'ifaddrs 'name))
If you define NAME's type to be :STRING, that code will yield a Lisp string.
That works, thanks
The man page shows that the allocated struct should be free calling freeifaddrs:
void freeifaddrs(struct ifaddrs *ifa);
(freeifaddrs (cffi:mem-ref p :pointer))
is that the correct way to call freeifaddrs?
Regards, Jose