ip: Move split-flags to utils and make public

Julien LepillerSun Mar 14 00:30:16+0100 2021

82bbfca

ip: Move split-flags to utils and make public

ip/link.scm

5656
  (addr  link-addr)
5757
  (brd   link-brd))
5858
59-
(define (split-flags flags)
60-
  (let loop ((max-flag 262144) (flags flags) (result '()))
61-
    (cond
62-
      ((equal? max-flag 1)
63-
       (if (equal? flags 1)
64-
           (cons (int->device-flags 1) result)
65-
           result))
66-
      ((< flags max-flag)
67-
       (loop (/ max-flag 2) flags result))
68-
      (else
69-
        (loop (/ max-flag 2) (- flags max-flag)
70-
              (cons
71-
                (int->device-flags max-flag)
72-
                result))))))
73-
7459
(define (get-links)
7560
  (define request-num (random 65535))
7661
  (define message

9681
                   (get-attr attrs IFLA_IFNAME)
9782
                   (link-message-index data)
9883
                   (link-message-kind data)
99-
                   (split-flags (link-message-flags data))
84+
                   (map int->device-flags (split-flags (link-message-flags data)))
10085
                   (get-attr attrs IFLA_MTU)
10186
                   (get-attr attrs IFLA_QDISC)
10287
                   (get-attr attrs IFLA_OPERSTATE)

ip/utils.scm

11
;;;; Copyright (C) 2021 Julien Lepiller <julien@lepiller.eu>
2-
;;;; 
2+
;;;;
33
;;;; This library is free software; you can redistribute it and/or
44
;;;; modify it under the terms of the GNU Lesser General Public
55
;;;; License as published by the Free Software Foundation; either
66
;;;; version 3 of the License, or (at your option) any later version.
7-
;;;; 
7+
;;;;
88
;;;; This library is distributed in the hope that it will be useful,
99
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
1010
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1111
;;;; Lesser General Public License for more details.
12-
;;;; 
12+
;;;;
1313
;;;; You should have received a copy of the GNU Lesser General Public
1414
;;;; License along with this library; if not, write to the Free Software
1515
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16-
;;;; 
16+
;;;;
1717
1818
(define-module (ip utils)
1919
  #:use-module (ice-9 match)

2323
  #:use-module (netlink route attrs)
2424
  #:use-module (netlink standard)
2525
  #:export (answer-ok?
26-
            get-attr))
26+
            get-attr
27+
            split-flags))
2728
2829
(define (answer-ok? answer)
2930
  (cond

4546
    (match attrs
4647
      (() #f)
4748
      ((attr) (nl-data-data (route-attr-data attr))))))
49+
50+
(define (split-flags flags)
51+
  (let loop ((max-flag 262144) (flags flags) (result '()))
52+
    (cond
53+
      ((equal? max-flag 1)
54+
       (if (equal? flags 1)
55+
           (cons 1 result)
56+
           result))
57+
      ((< flags max-flag)
58+
       (loop (/ max-flag 2) flags result))
59+
      (else
60+
        (loop (/ max-flag 2) (- flags max-flag)
61+
              (cons max-flag result))))))