guile-netlink/netlink/route/route.scm

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