guile-netlink/netlink/route/route.scm

route.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 route)
19
  #:use-module (ice-9 match)
20
  #:use-module (netlink data)
21
  #:use-module (netlink route attrs)
22
  #:use-module (srfi srfi-9)
23
  #:use-module (rnrs bytevectors)
24
  #:export (make-route-message
25
            route-message?
26
            route-message-family
27
            route-message-dest-len
28
            route-message-src-len
29
            route-message-tos
30
            route-message-table
31
            route-message-protocol
32
            route-message-scope
33
            route-message-type
34
            route-message-flags
35
            route-message-attrs
36
            deserialize-route-message))
37
38
(define (align pos to)
39
  (+ pos -1 (- to (modulo (- pos 1) to))))
40
41
(define-data-type route-message
42
  (lambda (msg)
43
    (+ 12 (apply + (map (lambda (d) (align (data-size d) 4)) attrs))))
44
  (lambda (msg pos bv)
45
    (match msg
46
      (($ route-message-type family dest-len src-len tos table protocol
47
          scope type flags attrs)
48
       (bytevector-u8-set! bv pos family)
49
       (bytevector-u8-set! bv (+ pos 1) dest-len)
50
       (bytevector-u8-set! bv (+ pos 2) src-len)
51
       (bytevector-u8-set! bv (+ pos 3) tos)
52
       (bytevector-u8-set! bv (+ pos 4) table)
53
       (bytevector-u8-set! bv (+ pos 5) protocol)
54
       (bytevector-u8-set! bv (+ pos 6) scope)
55
       (bytevector-u8-set! bv (+ pos 7) type)
56
       (bytevector-u32-set! bv (+ pos 8) flags (native-endianness))
57
       (let loop ((attrs attrs) (pos (+ pos 12)))
58
         (match attrs
59
           ((attr attrs ...)
60
            (serialize attr pos bv)
61
            (loop attrs (+ pos (align (data-size attr) 4))))
62
           (() #t))))))
63
  (family addr-message-family addr-message-type-family)
64
  (dest-len addr-message-dest-len addr-message-type-dest-len)
65
  (src-len addr-message-src-len addr-message-type-src-len)
66
  (tos addr-message-tos addr-message-type-tos)
67
  (table addr-message-table addr-message-type-table)
68
  (protocol addr-message-protocol addr-message-type-protocol)
69
  (scope addr-message-scope addr-message-type-scope)
70
  (type addr-message-kind addr-message-type-type)
71
  (flags addr-message-flags addr-message-type-flags)
72
  (attrs addr-message-attrs addr-message-type-attrs))
73
74
(define (deserialize-route-message decoder bv pos)
75
  (let ((family (bytevector-u8-ref bv pos)))
76
    (make-route-message
77
      family
78
      (bytevector-u8-ref bv (+ pos 1))
79
      (bytevector-u8-ref bv (+ pos 2))
80
      (bytevector-u8-ref bv (+ pos 3))
81
      (bytevector-u8-ref bv (+ pos 4))
82
      (bytevector-u8-ref bv (+ pos 5))
83
      (bytevector-u8-ref bv (+ pos 6))
84
      (bytevector-u8-ref bv (+ pos 7))
85
      (bytevector-u32-ref bv (+ pos 8) (native-endianness))
86
      (let ((len (bytevector-length bv)))
87
        (let loop ((pos (+ pos 12)) (attrs '()))
88
          (if (>= pos len)
89
              attrs
90
              (let ((attr (deserialize (cond
91
                                         ((equal? family AF_INET) 'ipv4-route-attr)
92
                                         ((equal? family AF_INET6) 'ipv6-route-attr)
93
                                         (else (throw 'unknown-family family)))
94
                                       decoder bv pos)))
95
                (loop (+ pos (align (data-size attr) 4))
96
                      (cons attr attrs)))))))))
97