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