address_others.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !linux
  15. // +build !linux
  16. package iproute2
  17. import (
  18. "github.com/vishvananda/netlink"
  19. )
  20. type Address struct {
  21. *Link
  22. addrBad bool
  23. addrs []*netlink.Addr
  24. testcb func()
  25. }
  26. func nop() {}
  27. func NewAddress(ifname string, addresses ...string) *Address {
  28. l := NewLink(ifname)
  29. r := &Address{
  30. Link: l,
  31. testcb: nop,
  32. }
  33. addrs := make([]*netlink.Addr, len(addresses))
  34. for i, address := range addresses {
  35. addr, err := netlink.ParseAddr(address)
  36. if err != nil {
  37. r.addrBad = true
  38. r.addErr(err, "parse addr")
  39. continue
  40. }
  41. addrs[i] = addr
  42. }
  43. if !r.addrBad {
  44. r.addrs = addrs
  45. }
  46. return r
  47. }
  48. func (address *Address) Exact() *Address {
  49. return nil
  50. }
  51. func (address *Address) link() (link netlink.Link, ok bool) {
  52. if address.addrBad {
  53. return
  54. }
  55. link = address.Link.link
  56. if link != nil {
  57. ok = true
  58. }
  59. return
  60. }
  61. func (address *Address) Add() *Address {
  62. link, ok := address.link()
  63. if !ok {
  64. return address
  65. }
  66. for _, addr := range address.addrs {
  67. err := netlink.AddrReplace(link, addr)
  68. if err != nil {
  69. address.addErr(err, "Add: AddrReplace %s ", addr)
  70. }
  71. }
  72. return address
  73. }