message.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 message) |
19 | #:use-module (ice-9 match) |
20 | #:use-module (netlink data) |
21 | #:use-module (rnrs bytevectors) |
22 | #:use-module (srfi srfi-9) |
23 | #:export (make-message |
24 | message? |
25 | message-kind |
26 | message-flags |
27 | message-seq |
28 | message-pid |
29 | message-data |
30 | deserialize-message)) |
31 | |
32 | (define-data-type message |
33 | message-type-len |
34 | (lambda (msg pos bv) |
35 | (match msg |
36 | (($ message-type type flags seq pid data) |
37 | (bytevector-u32-set! bv pos (message-type-len msg) (native-endianness)) |
38 | (bytevector-u16-set! bv (+ pos 4) type (native-endianness)) |
39 | (bytevector-u16-set! bv (+ pos 6) flags (native-endianness)) |
40 | (bytevector-u32-set! bv (+ pos 8) seq (native-endianness)) |
41 | (bytevector-u32-set! bv (+ pos 12) pid (native-endianness)) |
42 | (serialize data (+ 16 pos) bv)))) |
43 | (type message-kind message-type-type) |
44 | (flags message-flags message-type-flags) |
45 | (seq message-seq message-type-seq) |
46 | (pid message-pid message-type-pid) |
47 | (data message-data message-type-data)) |
48 | |
49 | (define (message-type-len msg) |
50 | (+ 16 (data-size (message-type-data msg)))) |
51 | |
52 | (define (deserialize-message decoder bv pos) |
53 | (let* ((len (bytevector-u32-ref bv pos (native-endianness))) |
54 | (type (bytevector-u16-ref bv (+ pos 4) (native-endianness))) |
55 | (deserialize (get-next-deserialize decoder 'message type))) |
56 | (make-message |
57 | type |
58 | (bytevector-u16-ref bv (+ pos 6) (native-endianness)) |
59 | (bytevector-u32-ref bv (+ pos 8) (native-endianness)) |
60 | (bytevector-u32-ref bv (+ pos 12) (native-endianness)) |
61 | (deserialize decoder bv (+ pos 16))))) |
62 |