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 (route-attr-list-size (addr-message-type-attrs msg))))
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
       (serialize-route-attr-list attrs (+ pos 8) bv))))
47
  (family addr-message-family addr-message-type-family)
48
  (prefix-len addr-message-prefix-len addr-message-type-prefix-len)
49
  (flags addr-message-flags addr-message-type-flags)
50
  (scope addr-message-scope addr-message-type-scope)
51
  (index addr-message-index addr-message-type-index)
52
  (attrs addr-message-attrs addr-message-type-attrs))
53
54
(define (deserialize-addr-message decoder bv pos)
55
  (let ((family (bytevector-u8-ref bv pos)))
56
    (make-addr-message
57
      family
58
      (bytevector-u8-ref bv (+ pos 1))
59
      (bytevector-u8-ref bv (+ pos 2))
60
      (bytevector-u8-ref bv (+ pos 3))
61
      (bytevector-u32-ref bv (+ pos 4) (native-endianness))
62
      (deserialize-attr-list
63
        (cond
64
          ((equal? family AF_INET) 'ipv4-addr-attr)
65
          ((equal? family AF_INET6) 'ipv6-addr-attr)
66
          (else (throw 'unknown-family family)))
67
        decoder bv (+ pos 8)))))
68