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 (apply + (map (lambda (d) (align (data-size d) 4)) attrs)))) |
| 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 | (let loop ((attrs attrs) (pos (+ pos 12))) |
| 56 | (match attrs |
| 57 | ((attr attrs ...) |
| 58 | (serialize attr pos bv) |
| 59 | (loop attrs (+ pos (align (data-size attr) 4)))) |
| 60 | (() #t)))))) |
| 61 | (family addr-message-family addr-message-type-family) |
| 62 | (dest-len addr-message-dest-len addr-message-type-dest-len) |
| 63 | (src-len addr-message-src-len addr-message-type-src-len) |
| 64 | (tos addr-message-tos addr-message-type-tos) |
| 65 | (table addr-message-table addr-message-type-table) |
| 66 | (protocol addr-message-protocol addr-message-type-protocol) |
| 67 | (scope addr-message-scope addr-message-type-scope) |
| 68 | (type addr-message-kind addr-message-type-type) |
| 69 | (flags addr-message-flags addr-message-type-flags) |
| 70 | (attrs addr-message-attrs addr-message-type-attrs)) |
| 71 | |
| 72 | (define (deserialize-route-message decoder bv pos) |
| 73 | (let ((family (bytevector-u8-ref bv pos))) |
| 74 | (make-route-message |
| 75 | family |
| 76 | (bytevector-u8-ref bv (+ pos 1)) |
| 77 | (bytevector-u8-ref bv (+ pos 2)) |
| 78 | (bytevector-u8-ref bv (+ pos 3)) |
| 79 | (bytevector-u8-ref bv (+ pos 4)) |
| 80 | (bytevector-u8-ref bv (+ pos 5)) |
| 81 | (bytevector-u8-ref bv (+ pos 6)) |
| 82 | (bytevector-u8-ref bv (+ pos 7)) |
| 83 | (bytevector-u32-ref bv (+ pos 8) (native-endianness)) |
| 84 | (let ((len (bytevector-length bv))) |
| 85 | (let loop ((pos (+ pos 12)) (attrs '())) |
| 86 | (if (>= pos len) |
| 87 | attrs |
| 88 | (let ((attr (deserialize (cond |
| 89 | ((equal? family AF_INET) 'ipv4-route-attr) |
| 90 | ((equal? family AF_INET6) 'ipv6-route-attr) |
| 91 | (else (throw 'unknown-family family))) |
| 92 | decoder bv pos))) |
| 93 | (loop (+ pos (align (data-size attr) 4)) |
| 94 | (cons attr attrs))))))))) |
| 95 |