guile-netlink/netlink/route/addr.scm

addr.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (netlink route addr)
19
  #:use-module (ice-9 match)
20
  #:use-module (netlink data)
21
  #:use-module (netlink route)
22
  #:use-module (netlink route attrs)
23
  #:use-module (srfi srfi-9)
24
  #:use-module (rnrs bytevectors)
25
  #:export (make-addr-message
26
            addr-message?
27
            addr-message-family
28
            addr-message-prefix-len
29
            addr-message-flags
30
            addr-message-scope
31
            addr-message-index
32
            addr-message-attrs
33
            deserialize-addr-message))
34
35
(define-data-type addr-message
36
  (lambda (msg)
37
    (+ 8 (apply + (map (lambda (d) (align (data-size d) 4)) attrs))))
38
  (lambda (msg pos bv)
39
    (match msg
40
      (($ addr-message-type family prefix-len flags scope index attrs)
41
       (bytevector-u8-set! bv pos family)
42
       (bytevector-u8-set! bv (+ pos 1) prefix-len)
43
       (bytevector-u8-set! bv (+ pos 2) flags)
44
       (bytevector-u8-set! bv (+ pos 3) scope)
45
       (bytevector-u32-set! bv (+ pos 4) index (native-endianness))
46
       (let loop ((attrs attrs) (pos (+ pos 8)))
47
         (match attrs
48
           ((attr attrs ...)
49
            (serialize attr pos bv)
50
            (loop attrs (+ pos (align (data-size attr) 4))))
51
           (() #t))))))
52
  (family addr-message-family addr-message-type-family)
53
  (prefix-len addr-message-prefix-len addr-message-type-prefix-len)
54
  (flags addr-message-flags addr-message-type-flags)
55
  (scope addr-message-scope addr-message-type-scope)
56
  (index addr-message-index addr-message-type-index)
57
  (attrs addr-message-attrs addr-message-type-attrs))
58
59
(define (deserialize-addr-message decoder bv pos)
60
  (let ((family (bytevector-u8-ref bv pos)))
61
    (make-addr-message
62
      family
63
      (bytevector-u8-ref bv (+ pos 1))
64
      (bytevector-u8-ref bv (+ pos 2))
65
      (bytevector-u8-ref bv (+ pos 3))
66
      (bytevector-u32-ref bv (+ pos 4) (native-endianness))
67
      (let ((len (bytevector-length bv)))
68
        (let loop ((pos (+ pos 8)) (attrs '()))
69
          (if (>= pos len)
70
              attrs
71
              (let ((attr (deserialize (cond
72
                                         ((equal? family AF_INET) 'ipv4-addr-attr)
73
                                         ((equal? family AF_INET6) 'ipv6-addr-attr)
74
                                         (else (throw 'unknown-family family)))
75
                                       decoder bv pos)))
76
                (loop (+ pos (align (data-size attr) 4))
77
                      (cons attr attrs)))))))))
78