guile-netlink/netlink/route/link.scm

link.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 link)
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-link-message
26
            link-message?
27
            link-message-family
28
            link-message-kind
29
            link-message-index
30
            link-message-flags
31
            link-message-attrs
32
            deserialize-link-message))
33
34
(define-data-type link-message
35
  (lambda (msg)
36
     (+ 16 (route-attr-list-size (link-message-type-attrs msg))))
37
  (lambda (msg pos bv)
38
    (match msg
39
      (($ link-message-type family type index flags change attrs)
40
       (bytevector-u16-set! bv pos family (native-endianness))
41
       (bytevector-u16-set! bv (+ pos 2) type (native-endianness))
42
       (bytevector-u32-set! bv (+ pos 4) index (native-endianness))
43
       (bytevector-u32-set! bv (+ pos 8) flags (native-endianness))
44
       (bytevector-u32-set! bv (+ pos 12) change (native-endianness))
45
       (serialize-route-attr-list attrs (+ pos 16) bv))))
46
  (family link-message-family link-message-type-family)
47
  (type link-message-kind link-message-type-type)
48
  (index link-message-index link-message-type-index)
49
  (flags link-message-flags link-message-type-flags)
50
  (change link-message-change link-message-type-change)
51
  (attrs link-message-attrs link-message-type-attrs))
52
53
(define (deserialize-link-message decoder bv pos)
54
  (make-link-message
55
    (bytevector-u16-ref bv pos (native-endianness))
56
    (bytevector-u16-ref bv (+ pos 2) (native-endianness))
57
    (bytevector-u32-ref bv (+ pos 4) (native-endianness))
58
    (bytevector-u32-ref bv (+ pos 8) (native-endianness))
59
    (bytevector-u32-ref bv (+ pos 12) (native-endianness))
60
    (deserialize-attr-list 'link-attr decoder bv (+ pos 16))))
61