From: Rohan Prinja <rohan.prinja@gmail.com>
To: guix-devel <guix-devel@gnu.org>
Subject: [PATCH] getifaddrs wrapper
Date: Fri, 19 Jun 2015 15:20:16 +0530 [thread overview]
Message-ID: <CAAT4Lc4jN+YdYnX_09gxB5UL_d+QiLWhG07GEqVR6rSL1Uh0AQ@mail.gmail.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 24 bytes --]
PTAL, thank you
-Rohan
[-- Attachment #1.2: Type: text/html, Size: 57 bytes --]
[-- Attachment #2: 0001-add-wrapper-for-getifaddrs-3-to-guix-build-syscalls..patch --]
[-- Type: application/octet-stream, Size: 3207 bytes --]
From 668910afbe979145a7699708817e28d219ec0750 Mon Sep 17 00:00:00 2001
From: Rohan Prinja <rohan.prinja@gmail.com>
Date: Fri, 19 Jun 2015 12:05:05 +0530
Subject: [PATCH] add wrapper for getifaddrs (3) to guix/build/syscalls.scm
---
guix/build/syscalls.scm | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 3585bf2..e5d296a 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -36,6 +36,7 @@
swapon
swapoff
processes
+ getifaddrs
IFF_UP
IFF_BROADCAST
@@ -381,6 +382,81 @@ the C structure with the given TYPES."
(address (int128 ~ big))
(scopeid int32))
+;; TODO: no support for unions yet.
+;; This only supports broadcast addrs.
+(define-c-struct ifaddrs ;<ifaddrs.h>
+ read-ifaddrs
+ write-ifaddrs!
+ (ifa-next '*)
+ (ifa-name '*)
+ (ifa-flags unsigned-int)
+ (ifa-addr '*)
+ (ifa-netmask '*)
+ (ifu-broadcastaddr '*)
+ (ifa-data '*))
+
+(define-syntax-rule (bytevector-slice bv start len)
+ (let* ((res (make-bytevector len 0))
+ (_ (bytevector-copy! bv start res 0 len)))
+ res))
+
+;; See getifaddrs (3) for a description of
+;; struct ifaddrs.
+(define %struct-ifaddrs-type
+ `(* * ,unsigned-int * * * *))
+
+(define %getifaddrs
+ (let* ((ptr (dynamic-func "getifaddrs" (dynamic-link)))
+ (proc (pointer->procedure int ptr (list '*)))
+ (struct-init (list %null-pointer
+ %null-pointer
+ 0
+ %null-pointer
+ %null-pointer
+ %null-pointer
+ %null-pointer)))
+ (lambda ()
+ "Wrapper around getifaddrs (3)."
+ (let* ((ifap (make-c-struct %struct-ifaddrs-type
+ struct-init))
+ (ifapp (scm->pointer ifap)) ; ifap ptr
+ (ret (proc ifapp))
+ (err (errno)))
+ (if (zero? ret)
+ (next-ifaddr (parse-ifaddrs ifapp))
+ (throw 'system-error "getifaddrs" "~S: ~A"
+ (list ifap (strerror err))
+ (list err)))))))
+
+(define (getifaddrs)
+ "Obtain a list of network interfaces on the local system."
+ (let ((ifaddrs (%getifaddrs)))
+ (let lp ((curr ifaddrs) (res '()))
+ (if (last-interface? curr)
+ (reverse res)
+ (lp (next-ifaddr curr) (cons curr res))))))
+
+;; Given a pointer to a struct ifaddrs, parse it into a list.
+(define-syntax-rule (parse-ifaddrs ptr)
+ (parse-c-struct ptr %struct-ifaddrs-type))
+
+;; Retrieve a bytevector aliasing the memory pointed to by the
+;; ifa_next struct ifaddrs* pointer.
+(define-syntax-rule (next-ifaddr ifaddrs)
+ (parse-c-struct (car ifaddrs) %struct-ifaddrs-type))
+
+;; Retrieve interface name.
+(define-syntax-rule (ifaddr-name ifaddrs)
+ (pointer->string (cadr ifaddrs)))
+
+;; Retrieve interface flags.
+(define-syntax-rule (ifaddr-flags ifaddrs)
+ (list-ref ifaddrs 2))
+
+;; Is an interface the last in the intrusive linked list of struct ifaddrs?
+(define-syntax-rule (last-interface? ifaddrs)
+ (null-pointer? (car ifaddrs)))
+
(define (write-socket-address! sockaddr bv index)
"Write SOCKADDR, a socket address as returned by 'make-socket-address', to
bytevector BV at INDEX."
--
1.9.1
next reply other threads:[~2015-06-19 9:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-19 9:50 Rohan Prinja [this message]
2015-06-19 12:03 ` [PATCH] getifaddrs wrapper Ludovic Courtès
2015-07-02 10:59 ` Rohan Prinja
2015-07-02 12:23 ` Ludovic Courtès
2015-07-16 8:30 ` Rohan Prinja
2015-07-16 13:50 ` Rohan Prinja
2015-07-16 15:38 ` Ludovic Courtès
2015-07-17 10:57 ` Rohan Prinja
2015-07-25 12:59 ` Ludovic Courtès
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAAT4Lc4jN+YdYnX_09gxB5UL_d+QiLWhG07GEqVR6rSL1Uh0AQ@mail.gmail.com \
--to=rohan.prinja@gmail.com \
--cc=guix-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/guix.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.