Added a convenience macro to make the filtering out of unneeded
interfaces cleaner.

On 16 July 2015 at 14:00, Rohan Prinja <[email protected]> wrote:
> A bug causing broadcast-addr and other fields AFTER the flags field in
> 'struct getifaddrs' to be accessed incorrectly has been fixed.
>
> On 2 July 2015 at 17:53, Ludovic Courtès <[email protected]> wrote:
>> Rohan Prinja <[email protected]> skribis:
>>
>>> PTAL, tests to follow soon.
>>
>> It’s looking good now, thanks!
>>
>> Could you send the updated patch against master, that includes a simple
>> test in tests/syscalls.scm that makes sure that ‘getifaddrs’ returns a
>> possibly empty list of <interface-address>?
>>
>> TIA,
>> Ludo’.
From 6192e9a17b52b95d307fc80268f854d8e886005c Mon Sep 17 00:00:00 2001
From: Rohan Prinja <[email protected]>
Date: Thu, 16 Jul 2015 19:19:29 +0530
Subject: [PATCH] guix/build/syscalls.scm, tests/syscalls.scm: un-export
 make-ifaddrs, and add a convenience macro to filter out non-useful interfaces

---
 guix/build/syscalls.scm | 17 +++++++++++------
 tests/syscalls.scm      | 19 ++++++-------------
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index a50a9bf..0cda091 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -56,7 +56,7 @@
             interface-address-broadcast-addr
             interface-address-netmask-addr
 
-            make-ifaddrs
+            remove-if-netmask-null
 
             IFF_UP
             IFF_BROADCAST
@@ -534,11 +534,6 @@ unsigned-int ifa-flags field"
            (inet-ntop family address)
            #f))))
 
-;; Note: address fields in 'struct getifaddrs' are pointers to
-;; 'struct sockaddr'. In 'interface-address-broadcast-addr' we are
-;; implicitly typecasting this 'sockaddr' pointer to a
-;; 'sockaddr_in' pointer.
-
 ;; Note: getifaddrs returns multiple interfaces with the same
 ;; e.g. on my system I see multiple "eth0"s. The difference is
 ;; that for one of the eth0's, the family of the address
@@ -552,6 +547,16 @@ sockaddr' from an <interface-address> record type."
          (bv (pointer->bytevector addr %sizeof-struct-sockaddr)))
     (bytevector->sockaddr bv)))
 
+;; Note: address fields in 'struct getifaddrs' are pointers to
+;; 'struct sockaddr'. In 'extract-address-field' we are
+;; implicitly typecasting this 'sockaddr' pointer to a
+;; 'sockaddr_in' pointer.
+
+;; Utility macro to remove all ifaces from the output IFACES of
+;; (getifaddrs) that have a null-pointer in the 'netmask' field.
+(define-syntax-rule (remove-if-netmask-null ifaces)
+  (remove (compose null-pointer? interface-address-netmask) ifaces))
+
 ;; Given an <interface-address> record IFACE, return its
 ;; address field as a sockaddr if it exists, otherwise return #f.
 (define (interface-address-address iface)
diff --git a/tests/syscalls.scm b/tests/syscalls.scm
index 3665575..87c58ea 100644
--- a/tests/syscalls.scm
+++ b/tests/syscalls.scm
@@ -232,10 +232,9 @@ For example, (4 2 2 2 2 1 3 3) => (4 2 1 3)."
              (let* ((is-eth-iface? (lambda (i)
                                      (string-prefix? "eth"
                                                      (interface-address-name i))))
-                    (ifaddrs (getifaddrs))
+                    (ifaddrs (remove-if-netmask-null (getifaddrs)))
                     (eth-ifaces (filter is-eth-iface? ifaddrs))
-                    (getifaddrs-tmp (map interface-address-address eth-ifaces))
-                    (getifaddrs-result (remove not getifaddrs-tmp))
+                    (getifaddrs-result (map interface-address-address eth-ifaces))
                     (ifconfig-result (ifconfig-extract-addr-of "eth" 'address)))
                (member ifconfig-result getifaddrs-result)))
 
@@ -243,10 +242,9 @@ For example, (4 2 2 2 2 1 3 3) => (4 2 1 3)."
              (let* ((is-eth-iface? (lambda (i)
                                      (string-prefix? "eth"
                                                      (interface-address-name i))))
-                    (ifaddrs (getifaddrs))
+                    (ifaddrs (remove-if-netmask-null (getifaddrs)))
                     (eth-ifaces (filter is-eth-iface? ifaddrs))
-                    (getifaddrs-tmp (map interface-address-broadcast-addr eth-ifaces))
-                    (getifaddrs-result (remove not getifaddrs-tmp))
+                    (getifaddrs-result (map interface-address-broadcast-addr eth-ifaces))
                     (ifconfig-result (ifconfig-extract-addr-of "eth" 'broadcast)))
                (member ifconfig-result getifaddrs-result)))
 
@@ -254,14 +252,9 @@ For example, (4 2 2 2 2 1 3 3) => (4 2 1 3)."
              (let* ((is-eth-iface? (lambda (i)
                                      (string-prefix? "eth"
                                                      (interface-address-name i))))
-                    (ifaddrs (getifaddrs))
+                    (ifaddrs (remove-if-netmask-null (getifaddrs)))
                     (eth-ifaces (filter is-eth-iface? ifaddrs))
-                    (getifaddrs-tmp (remove (lambda (i)
-                                               (null-pointer?
-                                                (interface-address-netmask i)))
-                                             eth-ifaces))
-                    (getifaddrs-tmp (map interface-address-netmask-addr getifaddrs-tmp))
-                    (getifaddrs-result (remove not getifaddrs-tmp))
+                    (getifaddrs-result (map interface-address-netmask-addr eth-ifaces))
                     (ifconfig-result (ifconfig-extract-addr-of "eth" 'netmask)))
                (member ifconfig-result getifaddrs-result)))
 
-- 
1.9.1

Reply via email to