guile-netlink/netlink/route/addr.scm

addr.scm

1
;;;; This file is part of Guile Netlink
2
;;;;
3
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
4
;;;; 
5
;;;; This library is free software: you can redistribute it and/or modify
6
;;;; it under the terms of the GNU General Public License as published by
7
;;;; the Free Software Foundation, either version 3 of the License, or
8
;;;; (at your option) any later version.
9
;;;;
10
;;;; This library is distributed in the hope that it will be useful,
11
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;;;; GNU General Public License for more details.
14
;;;;
15
;;;; You should have received a copy of the GNU General Public License
16
;;;; along with this library.  If not, see <https://www.gnu.org/licenses/>.
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