syscalls: Struct deserializer can now return arbitrary objects.

* guix/build/syscalls.scm (read-types): Add RETURN and VALUES parameters.
  (define-c-struct): Add WRAP-FIELDS parameter and pass it to 'read-types'.
  (sockaddr-in, sockaddr-in6): Add first argument that uses
  'make-socket-address'.
  (read-socket-address): Remove 'match' on the result of 'read-sockaddr-in'
  and 'read-sockaddr-in6'.
This commit is contained in:
Ludovic Courtès 2015-07-17 23:31:29 +02:00
parent 4f7b564adb
commit 13f0c6ed41
1 changed files with 16 additions and 16 deletions

View File

@ -398,22 +398,23 @@ system to PUT-OLD."
(define-syntax read-types (define-syntax read-types
(syntax-rules () (syntax-rules ()
((_ bv offset ()) ((_ return bv offset () (values ...))
'()) (return values ...))
((_ bv offset (type0 types ...)) ((_ return bv offset (type0 types ...) (values ...))
(cons (read-type bv offset type0) (read-types return
(read-types bv (+ offset (type-size type0)) (types ...)))))) bv (+ offset (type-size type0)) (types ...)
(values ... (read-type bv offset type0))))))
(define-syntax define-c-struct (define-syntax define-c-struct
(syntax-rules () (syntax-rules ()
"Define READ as an optimized serializer and WRITE! as a deserializer for "Define READ as a deserializer and WRITE! as a serializer for the C
the C structure with the given TYPES." structure with the given TYPES. READ uses WRAP-FIELDS to return its value."
((_ name read write! (fields types) ...) ((_ name wrap-fields read write! (fields types) ...)
(begin (begin
(define (write! bv offset fields ...) (define (write! bv offset fields ...)
(write-types bv offset (types ...) (fields ...))) (write-types bv offset (types ...) (fields ...)))
(define (read bv offset) (define (read bv offset)
(read-types bv offset (types ...))))))) (read-types wrap-fields bv offset (types ...) ()))))))
;;; ;;;
@ -463,6 +464,8 @@ the C structure with the given TYPES."
32)) 32))
(define-c-struct sockaddr-in ;<linux/in.h> (define-c-struct sockaddr-in ;<linux/in.h>
(lambda (family port address)
(make-socket-address family address port))
read-sockaddr-in read-sockaddr-in
write-sockaddr-in! write-sockaddr-in!
(family unsigned-short) (family unsigned-short)
@ -470,6 +473,8 @@ the C structure with the given TYPES."
(address (int32 ~ big))) (address (int32 ~ big)))
(define-c-struct sockaddr-in6 ;<linux/in6.h> (define-c-struct sockaddr-in6 ;<linux/in6.h>
(lambda (family port flowinfo address scopeid)
(make-socket-address family address port flowinfo scopeid))
read-sockaddr-in6 read-sockaddr-in6
write-sockaddr-in6! write-sockaddr-in6!
(family unsigned-short) (family unsigned-short)
@ -501,14 +506,9 @@ bytevector BV at INDEX."
"Read a socket address from bytevector BV at INDEX." "Read a socket address from bytevector BV at INDEX."
(let ((family (bytevector-u16-native-ref bv index))) (let ((family (bytevector-u16-native-ref bv index)))
(cond ((= family AF_INET) (cond ((= family AF_INET)
(match (read-sockaddr-in bv index) (read-sockaddr-in bv index))
((family port address)
(make-socket-address family address port))))
((= family AF_INET6) ((= family AF_INET6)
(match (read-sockaddr-in6 bv index) (read-sockaddr-in6 bv index))
((family port flowinfo address scopeid)
(make-socket-address family address port
flowinfo scopeid))))
(else (else
"unsupported socket address family" family)))) "unsupported socket address family" family))))