Move attribute list size and serialization to a common module.

Julien LepillerMon Feb 01 23:10:44+0100 2021

642b7de

Move attribute list size and serialization to a common module.

netlink/route.scm

1616
;;;; 
1717
1818
(define-module (netlink route)
19-
  #:export (align))
19+
  #:export (align
20+
            route-attr-list-size
21+
            serialize-route-attr-list))
2022
2123
(define (align pos to)
2224
  (+ 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

3434
3535
(define-data-type addr-message
3636
  (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))))
3838
  (lambda (msg pos bv)
3939
    (match msg
4040
      (($ addr-message-type family prefix-len flags scope index attrs)

4343
       (bytevector-u8-set! bv (+ pos 2) flags)
4444
       (bytevector-u8-set! bv (+ pos 3) scope)
4545
       (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))))
5247
  (family addr-message-family addr-message-type-family)
5348
  (prefix-len addr-message-prefix-len addr-message-type-prefix-len)
5449
  (flags addr-message-flags addr-message-type-flags)

netlink/route/link.scm

3333
3434
(define-data-type link-message
3535
  (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))))
3737
  (lambda (msg pos bv)
3838
    (match msg
3939
      (($ link-message-type family type index flags change attrs)

4242
       (bytevector-u32-set! bv (+ pos 4) index (native-endianness))
4343
       (bytevector-u32-set! bv (+ pos 8) flags (native-endianness))
4444
       (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))))
5146
  (family link-message-family link-message-type-family)
5247
  (type link-message-kind link-message-type-type)
5348
  (index link-message-index link-message-type-index)

netlink/route/route.scm

3838
3939
(define-data-type route-message
4040
  (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))))
4242
  (lambda (msg pos bv)
4343
    (match msg
4444
      (($ route-message-type family dest-len src-len tos table protocol

5252
       (bytevector-u8-set! bv (+ pos 6) scope)
5353
       (bytevector-u8-set! bv (+ pos 7) type)
5454
       (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))))
6156
  (family addr-message-family addr-message-type-family)
6257
  (dest-len addr-message-dest-len addr-message-type-dest-len)
6358
  (src-len addr-message-src-len addr-message-type-src-len)