addr.scm
1 | ;;;; Copyright (C) 2021 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 (ip addr) |
19 | #:use-module (ice-9 match) |
20 | #:use-module (ip link) |
21 | #:use-module (ip utils) |
22 | #:use-module (netlink route addr) |
23 | #:use-module (netlink route attrs) |
24 | #:use-module (netlink connection) |
25 | #:use-module (netlink constant) |
26 | #:use-module (netlink deserialize) |
27 | #:use-module (netlink message) |
28 | #:use-module (netlink standard) |
29 | #:use-module (srfi srfi-1) |
30 | #:export (addr-add |
31 | addr-del |
32 | addr-show)) |
33 | |
34 | (define (cidr->addr str) |
35 | (match (string-split str #\/) |
36 | ((addr) addr) |
37 | ((addr prefix) addr) |
38 | (_ (throw 'incorrect-cidr-notation str)))) |
39 | |
40 | (define (cidr->prefix str) |
41 | (match (string-split str #\/) |
42 | ((addr) #f) |
43 | ((addr prefix) (string->number prefix)) |
44 | (_ (throw 'incorrect-cidr-notation str)))) |
45 | |
46 | (define* (addr-del device cidr #:key (ipv6? #f)) |
47 | (define request-num (random 65535)) |
48 | (define prefix (cidr->prefix cidr)) |
49 | (define addr (cidr->addr cidr)) |
50 | |
51 | (define index |
52 | (cond |
53 | ((number? device) device) |
54 | ((string? device) (link-name->index device)))) |
55 | |
56 | (define message |
57 | (make-message |
58 | RTM_DELADDR |
59 | (logior NLM_F_REQUEST NLM_F_ACK) |
60 | request-num |
61 | 0 |
62 | (make-addr-message |
63 | (if ipv6? AF_INET6 AF_INET) |
64 | (if prefix prefix 0) |
65 | 0 |
66 | 0 |
67 | index |
68 | (list |
69 | (make-route-attr IFA_LOCAL |
70 | ((if ipv6? |
71 | make-ipv6-route-attr |
72 | make-ipv4-route-attr) |
73 | addr)) |
74 | (make-route-attr IFA_ADDRESS |
75 | ((if ipv6? |
76 | make-ipv6-route-attr |
77 | make-ipv4-route-attr) |
78 | addr)))))) |
79 | |
80 | (let ((sock (connect-route))) |
81 | (send-msg message sock) |
82 | (let ((answer (receive-and-decode-msg sock %default-route-decoder))) |
83 | (close-socket sock) |
84 | (answer-ok? (last answer))))) |
85 |