ip.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 CNI authors
  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. package ip
  15. import (
  16. "fmt"
  17. "net"
  18. "strings"
  19. )
  20. // IP is a CNI maintained type inherited from net.IPNet which can
  21. // represent a single IP address with or without prefix.
  22. type IP struct {
  23. net.IPNet
  24. }
  25. // newIP will create an IP with net.IP and net.IPMask
  26. func newIP(ip net.IP, mask net.IPMask) *IP {
  27. return &IP{
  28. IPNet: net.IPNet{
  29. IP: ip,
  30. Mask: mask,
  31. },
  32. }
  33. }
  34. // ParseIP will parse string s as an IP, and return it.
  35. // The string s must be formed like <ip>[/<prefix>].
  36. // If s is not a valid textual representation of an IP,
  37. // will return nil.
  38. func ParseIP(s string) *IP {
  39. if strings.ContainsAny(s, "/") {
  40. ip, ipNet, err := net.ParseCIDR(s)
  41. if err != nil {
  42. return nil
  43. }
  44. return newIP(ip, ipNet.Mask)
  45. } else {
  46. ip := net.ParseIP(s)
  47. if ip == nil {
  48. return nil
  49. }
  50. return newIP(ip, nil)
  51. }
  52. }
  53. // ToIP will return a net.IP in standard form from this IP.
  54. // If this IP can not be converted to a valid net.IP, will return nil.
  55. func (i *IP) ToIP() net.IP {
  56. switch {
  57. case i.IP.To4() != nil:
  58. return i.IP.To4()
  59. case i.IP.To16() != nil:
  60. return i.IP.To16()
  61. default:
  62. return nil
  63. }
  64. }
  65. // String returns the string form of this IP.
  66. func (i *IP) String() string {
  67. if len(i.Mask) > 0 {
  68. return i.IPNet.String()
  69. }
  70. return i.IP.String()
  71. }
  72. // MarshalText implements the encoding.TextMarshaler interface.
  73. // The encoding is the same as returned by String,
  74. // But when len(ip) is zero, will return an empty slice.
  75. func (i *IP) MarshalText() ([]byte, error) {
  76. if len(i.IP) == 0 {
  77. return []byte{}, nil
  78. }
  79. return []byte(i.String()), nil
  80. }
  81. // UnmarshalText implements the encoding.TextUnmarshaler interface.
  82. // The textual bytes are expected in a form accepted by Parse,
  83. // But when len(b) is zero, will return an empty IP.
  84. func (i *IP) UnmarshalText(b []byte) error {
  85. if len(b) == 0 {
  86. *i = IP{}
  87. return nil
  88. }
  89. ip := ParseIP(string(b))
  90. if ip == nil {
  91. return fmt.Errorf("invalid IP address %s", string(b))
  92. }
  93. *i = *ip
  94. return nil
  95. }