Add support for deleting addresses
doc/guile-netlink.texi
| 540 | 540 | network addresses on your machine. They are equivalent to the @command{ip addr} | |
| 541 | 541 | family of commands, from @code{iproute2}. | |
| 542 | 542 | ||
| 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 | + | ||
| 543 | 565 | @deffn {Scheme Procedure} addr-del @var{device} @var{cidr} [@var{#:ipv6?} #f] | |
| 544 | 566 | Delete the address given in @var{cidr} from @var{device}. @var{device} can | |
| 545 | 567 | contain the name of the link, as a string, or its index, as a number. | |
… | |||
| 559 | 581 | @end example | |
| 560 | 582 | ||
| 561 | 583 | 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. | |
| 563 | 585 | @end deffn | |
| 564 | 586 | ||
| 565 | 587 | @bye | |
ip/addr.scm
| 82 | 82 | (let ((answer (receive-and-decode-msg sock %default-route-decoder))) | |
| 83 | 83 | (close-socket sock) | |
| 84 | 84 | (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))))) |