Add route message types
Makefile.am
| 9 | 9 | netlink/deserialize.scm \ | |
| 10 | 10 | netlink/route/addr.scm \ | |
| 11 | 11 | netlink/route/attrs.scm \ | |
| 12 | - | netlink/route/link.scm | |
| 12 | + | netlink/route/link.scm \ | |
| 13 | + | netlink/route/route.scm | |
| 13 | 14 | ||
| 14 | 15 | info_TEXINFOS= doc/guile-netlink.texi |
netlink/deserialize.scm
| 20 | 20 | #:use-module ((netlink route addr) #:prefix route:) | |
| 21 | 21 | #:use-module ((netlink route attrs) #:prefix route:) | |
| 22 | 22 | #:use-module ((netlink route link) #:prefix route:) | |
| 23 | + | #:use-module ((netlink route route) #:prefix route:) | |
| 23 | 24 | #:use-module (netlink standard) | |
| 24 | 25 | #:use-module (netlink message) | |
| 25 | 26 | #:export (%default-message-decoder | |
… | |||
| 40 | 41 | (,RTM_SETLINK . ,route:deserialize-link-message) | |
| 41 | 42 | (,RTM_NEWADDR . ,route:deserialize-addr-message) | |
| 42 | 43 | (,RTM_DELADDR . ,route:deserialize-addr-message) | |
| 43 | - | (,RTM_GETADDR . ,route:deserialize-addr-message)) | |
| 44 | + | (,RTM_GETADDR . ,route:deserialize-addr-message) | |
| 45 | + | (,RTM_NEWROUTE . ,route:deserialize-route-message) | |
| 46 | + | (,RTM_DELROUTE . ,route:deserialize-route-message) | |
| 47 | + | (,RTM_GETROUTE . ,route:deserialize-route-message)) | |
| 44 | 48 | (message-hdr ,deserialize-message-header '()) | |
| 45 | 49 | (link-attr ,(route:deserialize-route-attr 'link-attr) | |
| 46 | 50 | ,@route:%default-route-link-attr-decoder) | |
| 47 | - | (ipv4-attr ,(route:deserialize-route-attr 'ipv4-attr) | |
| 48 | - | ,@route:%default-route-addr-ipv4-attr-decoder) | |
| 49 | - | (ipv6-attr ,(route:deserialize-route-attr 'ipv6-attr) | |
| 50 | - | ,@route:%default-route-addr-ipv6-attr-decoder))) | |
| 51 | + | (ipv4-addr-attr ,(route:deserialize-route-attr 'ipv4-addr-attr) | |
| 52 | + | ,@route:%default-route-addr-ipv4-attr-decoder) | |
| 53 | + | (ipv6-addr-attr ,(route:deserialize-route-attr 'ipv6-addr-attr) | |
| 54 | + | ,@route:%default-route-addr-ipv6-attr-decoder) | |
| 55 | + | (ipv4-route-attr ,(route:deserialize-route-attr 'ipv4-route-attr) | |
| 56 | + | ,@route:%default-route-route-ipv4-attr-decoder) | |
| 57 | + | (ipv6-route-attr ,(route:deserialize-route-attr 'ipv6-route-attr) | |
| 58 | + | ,@route:%default-route-route-ipv6-attr-decoder))) | |
netlink/route/addr.scm
| 71 | 71 | (if (>= pos len) | |
| 72 | 72 | attrs | |
| 73 | 73 | (let ((attr (deserialize (cond | |
| 74 | - | ((equal? family AF_INET) 'ipv4-attr) | |
| 75 | - | ((equal? family AF_INET6) 'ipv6-attr) | |
| 74 | + | ((equal? family AF_INET) 'ipv4-addr-attr) | |
| 75 | + | ((equal? family AF_INET6) 'ipv6-addr-attr) | |
| 76 | 76 | (else (throw 'unknown-family family))) | |
| 77 | 77 | decoder bv pos))) | |
| 78 | 78 | (loop (+ pos (align (data-size attr) 4)) |
netlink/route/attrs.scm
| 38 | 38 | deserialize-route-attr | |
| 39 | 39 | deserialize-route-attr-data-string | |
| 40 | 40 | deserialize-route-attr-data-u8 | |
| 41 | + | deserialize-route-attr-data-u16 | |
| 41 | 42 | deserialize-route-attr-data-u32 | |
| 42 | 43 | deserialize-route-attr-data-s32 | |
| 43 | 44 | deserialize-route-attr-data-ethernet | |
| 44 | 45 | deserialize-route-attr-data-ipv4 | |
| 45 | 46 | deserialize-route-attr-data-ipv6 | |
| 46 | 47 | deserialize-route-attr-data-bv | |
| 47 | - | %default-route-link-attr-decoder | |
| 48 | 48 | %default-route-addr-ipv4-attr-decoder | |
| 49 | - | %default-route-addr-ipv6-attr-decoder)) | |
| 49 | + | %default-route-addr-ipv6-attr-decoder | |
| 50 | + | %default-route-link-attr-decoder | |
| 51 | + | %default-route-route-ipv4-attr-decoder | |
| 52 | + | %default-route-route-ipv6-attr-decoder)) | |
| 50 | 53 | ||
| 51 | 54 | (define-data-type route-attr | |
| 52 | 55 | attr-type-size | |
… | |||
| 165 | 168 | (define (deserialize-route-attr-data-s32 decoder bv pos) | |
| 166 | 169 | (make-s32-route-attr (bytevector-s32-ref bv pos (native-endianness)))) | |
| 167 | 170 | ||
| 171 | + | (define (deserialize-route-attr-data-u16 decoder bv pos) | |
| 172 | + | (make-u32-route-attr (bytevector-u16-ref bv pos (native-endianness)))) | |
| 173 | + | ||
| 168 | 174 | (define (deserialize-route-attr-data-u8 decoder bv pos) | |
| 169 | 175 | (make-u8-route-attr (bytevector-u8-ref bv pos))) | |
| 170 | 176 | ||
… | |||
| 214 | 220 | ;(,IFA_CACHEINFO . ,deserialize-route-attr-data-cache-info) | |
| 215 | 221 | (default . ,deserialize-route-attr-data-bv))) | |
| 216 | 222 | ||
| 223 | + | (define (default-route-route-attr-decoder address-decoder) | |
| 224 | + | `((,RTA_DST . ,address-decoder) | |
| 225 | + | (,RTA_SRC . ,address-decoder) | |
| 226 | + | (,RTA_IIF . ,deserialize-route-attr-data-u32) | |
| 227 | + | (,RTA_OIF . ,deserialize-route-attr-data-u32) | |
| 228 | + | (,RTA_GATEWAY . ,address-decoder) | |
| 229 | + | (,RTA_PRIORITY . ,deserialize-route-attr-data-u32) | |
| 230 | + | (,RTA_PREFSRC . ,address-decoder) | |
| 231 | + | (,RTA_METRICS . ,deserialize-route-attr-data-u32) | |
| 232 | + | ;; TODO: struct rtnexthop | |
| 233 | + | ;(,RTA_MULTIPATH . ,deserialize-route-attr-data-rt-next-hop) | |
| 234 | + | (,RTA_FLOW . ,deserialize-route-attr-data-u32) | |
| 235 | + | ; TODO: struct rta_cacheinfo | |
| 236 | + | ;(,RTA_CACHEINFO . ,deserialize-route-attr-data-rta-cache-info) | |
| 237 | + | (,RTA_TABLE . ,deserialize-route-attr-data-u32) | |
| 238 | + | (,RTA_MARK . ,deserialize-route-attr-data-u32) | |
| 239 | + | ;; TODO: struct rta_mfc_stats | |
| 240 | + | ;(,RTA_MFC_STATS . ,deserialize-route-attr-data-rta-mfc-stats) | |
| 241 | + | ;; TODO: struct rtvia | |
| 242 | + | ;(,RTA_VIA . ,,deserialize-route-attr-data-rtvia) | |
| 243 | + | (,RTA_NEWDST . ,address-decoder) | |
| 244 | + | (,RTA_PREF . ,deserialize-route-attr-data-u8) | |
| 245 | + | (,RTA_ENCAP_TYPE . ,deserialize-route-attr-data-u16) | |
| 246 | + | ;; TODO: defined by RTA_ENCAP_TYPE | |
| 247 | + | ;(,RTA_ENCAP . ??) | |
| 248 | + | (,RTA_EXPIRES . ,deserialize-route-attr-data-u32) | |
| 249 | + | (default . ,deserialize-route-attr-data-bv))) | |
| 250 | + | ||
| 217 | 251 | (define %default-route-addr-ipv4-attr-decoder | |
| 218 | 252 | (default-route-addr-attr-decoder deserialize-route-attr-data-ipv4)) | |
| 219 | 253 | ||
| 220 | 254 | (define %default-route-addr-ipv6-attr-decoder | |
| 221 | 255 | (default-route-addr-attr-decoder deserialize-route-attr-data-ipv6)) | |
| 256 | + | ||
| 257 | + | (define %default-route-route-ipv4-attr-decoder | |
| 258 | + | (default-route-route-attr-decoder deserialize-route-attr-data-ipv4)) | |
| 259 | + | ||
| 260 | + | (define %default-route-route-ipv6-attr-decoder | |
| 261 | + | (default-route-route-attr-decoder deserialize-route-attr-data-ipv6)) | |
netlink/route/route.scm unknown status 1
| 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))))))))) |