From e7f5691d4540e2cbcbc9f22f8b593f15890057b3 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 25 Jul 2015 13:06:01 +0200 Subject: syscalls: Add 'network-interfaces', which wraps libc's 'getifaddrs'. Based on discussions with Rohan Prinja . * guix/build/syscalls.scm (): New record type. (write-interface, values->interface, unfold-interface-list, network-interfaces, free-ifaddrs): New procedures. (ifaddrs): New C struct. (%struct-ifaddrs-type, %sizeof-ifaddrs): New macros. * tests/syscalls.scm ("network-interfaces returns one or more interfaces", "network-interfaces returns \"lo\""): New tests. --- tests/syscalls.scm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/syscalls.scm b/tests/syscalls.scm index 3b71cd7b1c..090e1e7858 100644 --- a/tests/syscalls.scm +++ b/tests/syscalls.scm @@ -211,6 +211,29 @@ ;; We get EPERM with Linux 3.18ish and EACCES with 2.6.32. (memv (system-error-errno args) (list EPERM EACCES)))))) +(test-equal "network-interfaces returns one or more interfaces" + '(#t #t #t) + (match (network-interfaces) + ((interfaces ..1) + (list (every interface? interfaces) + (every string? (map interface-name interfaces)) + (every vector? (map interface-address interfaces)))))) + +(test-equal "network-interfaces returns \"lo\"" + (list #t (make-socket-address AF_INET (inet-pton AF_INET "127.0.0.1") 0)) + (match (filter (lambda (interface) + (string=? "lo" (interface-name interface))) + (network-interfaces)) + ((loopbacks ..1) + (list (every (lambda (lo) + (not (zero? (logand IFF_LOOPBACK (interface-flags lo))))) + loopbacks) + (match (find (lambda (lo) + (= AF_INET (sockaddr:fam (interface-address lo)))) + loopbacks) + (#f #f) + (lo (interface-address lo))))))) + (test-end) -- cgit v1.2.3