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) |
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 (addr-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 addr-message-family addr-message-type-family) |
57 | (dest-len addr-message-dest-len addr-message-type-dest-len) |
58 | (src-len addr-message-src-len addr-message-type-src-len) |
59 | (tos addr-message-tos addr-message-type-tos) |
60 | (table addr-message-table addr-message-type-table) |
61 | (protocol addr-message-protocol addr-message-type-protocol) |
62 | (scope addr-message-scope addr-message-type-scope) |
63 | (type addr-message-kind addr-message-type-type) |
64 | (flags addr-message-flags addr-message-type-flags) |
65 | (attrs addr-message-attrs addr-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 |