Move attribute list size and serialization to a common module.
netlink/route.scm
16 | 16 | ;;;; | |
17 | 17 | ||
18 | 18 | (define-module (netlink route) | |
19 | - | #:export (align)) | |
19 | + | #:export (align | |
20 | + | route-attr-list-size | |
21 | + | serialize-route-attr-list)) | |
20 | 22 | ||
21 | 23 | (define (align pos to) | |
22 | 24 | (+ pos -1 (- to (modulo (- pos 1) to)))) | |
25 | + | ||
26 | + | (define (route-attr-list-size attrs) | |
27 | + | (apply + (map (lambda (d) (align (data-size d) 4)) attrs))) | |
28 | + | ||
29 | + | (define (serialize-route-attr-list attrs pos bv) | |
30 | + | (let loop ((attrs attrs) (pos pos)) | |
31 | + | (match attrs | |
32 | + | ((attr attrs ...) | |
33 | + | (serialize attr pos bv) | |
34 | + | (loop attrs (+ pos (align (data-size attr) 4)))) | |
35 | + | (() #t)))) |
netlink/route/addr.scm
34 | 34 | ||
35 | 35 | (define-data-type addr-message | |
36 | 36 | (lambda (msg) | |
37 | - | (+ 8 (apply + (map (lambda (d) (align (data-size d) 4)) attrs)))) | |
37 | + | (+ 8 (route-attr-list-size (addr-message-type-attrs msg)))) | |
38 | 38 | (lambda (msg pos bv) | |
39 | 39 | (match msg | |
40 | 40 | (($ addr-message-type family prefix-len flags scope index attrs) | |
… | |||
43 | 43 | (bytevector-u8-set! bv (+ pos 2) flags) | |
44 | 44 | (bytevector-u8-set! bv (+ pos 3) scope) | |
45 | 45 | (bytevector-u32-set! bv (+ pos 4) index (native-endianness)) | |
46 | - | (let loop ((attrs attrs) (pos (+ pos 8))) | |
47 | - | (match attrs | |
48 | - | ((attr attrs ...) | |
49 | - | (serialize attr pos bv) | |
50 | - | (loop attrs (+ pos (align (data-size attr) 4)))) | |
51 | - | (() #t)))))) | |
46 | + | (serialize-route-attr-list attrs (+ pos 8) bv)))) | |
52 | 47 | (family addr-message-family addr-message-type-family) | |
53 | 48 | (prefix-len addr-message-prefix-len addr-message-type-prefix-len) | |
54 | 49 | (flags addr-message-flags addr-message-type-flags) |
netlink/route/link.scm
33 | 33 | ||
34 | 34 | (define-data-type link-message | |
35 | 35 | (lambda (msg) | |
36 | - | (+ 16 (apply + (map (lambda (d) (align (data-size d) 4)) attrs)))) | |
36 | + | (+ 16 (route-attr-list-size (link-message-type-attrs msg)))) | |
37 | 37 | (lambda (msg pos bv) | |
38 | 38 | (match msg | |
39 | 39 | (($ link-message-type family type index flags change attrs) | |
… | |||
42 | 42 | (bytevector-u32-set! bv (+ pos 4) index (native-endianness)) | |
43 | 43 | (bytevector-u32-set! bv (+ pos 8) flags (native-endianness)) | |
44 | 44 | (bytevector-u32-set! bv (+ pos 12) change (native-endianness)) | |
45 | - | (let loop ((attrs attrs) (pos (+ pos 16))) | |
46 | - | (match attrs | |
47 | - | ((attr attrs ...) | |
48 | - | (serialize attr pos bv) | |
49 | - | (loop attrs (+ pos (align (data-size attr) 4)))) | |
50 | - | (() #t)))))) | |
45 | + | (serialize-route-attr-list attrs (+ pos 16) bv)))) | |
51 | 46 | (family link-message-family link-message-type-family) | |
52 | 47 | (type link-message-kind link-message-type-type) | |
53 | 48 | (index link-message-index link-message-type-index) |
netlink/route/route.scm
38 | 38 | ||
39 | 39 | (define-data-type route-message | |
40 | 40 | (lambda (msg) | |
41 | - | (+ 12 (apply + (map (lambda (d) (align (data-size d) 4)) attrs)))) | |
41 | + | (+ 12 (route-attr-list-size (addr-message-type-attrs msg)))) | |
42 | 42 | (lambda (msg pos bv) | |
43 | 43 | (match msg | |
44 | 44 | (($ route-message-type family dest-len src-len tos table protocol | |
… | |||
52 | 52 | (bytevector-u8-set! bv (+ pos 6) scope) | |
53 | 53 | (bytevector-u8-set! bv (+ pos 7) type) | |
54 | 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)))))) | |
55 | + | (serialize-route-attr-list attrs (+ pos 12) bv)))) | |
61 | 56 | (family addr-message-family addr-message-type-family) | |
62 | 57 | (dest-len addr-message-dest-len addr-message-type-dest-len) | |
63 | 58 | (src-len addr-message-src-len addr-message-type-src-len) |