all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] getifaddrs wrapper
@ 2015-06-19  9:50 Rohan Prinja
  2015-06-19 12:03 ` Ludovic Courtès
  0 siblings, 1 reply; 9+ messages in thread
From: Rohan Prinja @ 2015-06-19  9:50 UTC (permalink / raw)
  To: guix-devel


[-- 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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-07-25 12:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-19  9:50 [PATCH] getifaddrs wrapper Rohan Prinja
2015-06-19 12:03 ` 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

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.