Add support for deleting addresses

Julien LepillerSat Mar 13 22:56:21+0100 2021

c862f66

Add support for deleting addresses

doc/guile-netlink.texi

540540
network addresses on your machine.  They are equivalent to the @command{ip addr}
541541
family of commands, from @code{iproute2}.
542542
543+
@deffn {Scheme Procedure} addr-add @var{device} @var{cidr} [@var{#:ipv6?} #f]
544+
Add the address given in @var{cidr} to @var{device}. @var{device} can
545+
contain the name of the link, as a string, or its index, as a number.
546+
547+
@var{cidr} must be a string containing the address and prefix length, in
548+
CIDR notation (@code{addr/prefix}).
549+
550+
@example
551+
(addr-add "enp1s0" "192.0.2.15/24")
552+
@end example
553+
554+
If you wish to remove an IPv6 address instead, set @code{#:ipv6} to @code{#t},
555+
as in the following example.
556+
557+
@example
558+
(addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t)
559+
@end example
560+
561+
Note that using the wrong ip type with the wrong value for the @code{#:ipv6?}
562+
flag will result in a @code{Bad address} exception from inet-pton.
563+
@end deffn
564+
543565
@deffn {Scheme Procedure} addr-del @var{device} @var{cidr} [@var{#:ipv6?} #f]
544566
Delete the address given in @var{cidr} from @var{device}. @var{device} can
545567
contain the name of the link, as a string, or its index, as a number.

559581
@end example
560582
561583
Note that using the wrong ip type with the wrong value for the @code{#:ipv6?}
562-
flag will result in a @code{Bad address} error from netlink.
584+
flag will result in a @code{Bad address} exception from inet-pton.
563585
@end deffn
564586
565587
@bye

ip/addr.scm

8282
    (let ((answer (receive-and-decode-msg sock %default-route-decoder)))
8383
      (close-socket sock)
8484
      (answer-ok? (last answer)))))
85+
86+
(define* (addr-add device cidr #:key (ipv6? #f))
87+
  (define request-num (random 65535))
88+
  (define prefix (cidr->prefix cidr))
89+
  (define addr (cidr->addr cidr))
90+
91+
  (define index
92+
    (cond
93+
      ((number? device) device)
94+
      ((string? device) (link-name->index device))))
95+
96+
  (define message
97+
    (make-message
98+
      RTM_NEWADDR
99+
      (logior NLM_F_REQUEST NLM_F_ACK NLM_F_EXCL NLM_F_CREATE)
100+
      request-num
101+
      0
102+
      (make-addr-message
103+
        (if ipv6? AF_INET6 AF_INET)
104+
        (if prefix prefix 0)
105+
        0
106+
        0
107+
        index
108+
        (list
109+
          (make-route-attr IFA_LOCAL
110+
            ((if ipv6?
111+
                 make-ipv6-route-attr
112+
                 make-ipv4-route-attr)
113+
             addr))
114+
          (make-route-attr IFA_ADDRESS
115+
            ((if ipv6?
116+
                 make-ipv6-route-attr
117+
                 make-ipv4-route-attr)
118+
             addr))))))
119+
120+
  (let ((sock (connect-route)))
121+
    (send-msg message sock)
122+
    (let ((answer (receive-and-decode-msg sock %default-route-decoder)))
123+
      (close-socket sock)
124+
      (answer-ok? (last answer)))))