ip.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  46. ip := net.ParseIP(s)
  47. if ip == nil {
  48. return nil
  49. }
  50. return newIP(ip, nil)
  51. }
  52. // ToIP will return a net.IP in standard form from this IP.
  53. // If this IP can not be converted to a valid net.IP, will return nil.
  54. func (i *IP) ToIP() net.IP {
  55. switch {
  56. case i.IP.To4() != nil:
  57. return i.IP.To4()
  58. case i.IP.To16() != nil:
  59. return i.IP.To16()
  60. default:
  61. return nil
  62. }
  63. }
  64. // String returns the string form of this IP.
  65. func (i *IP) String() string {
  66. if len(i.Mask) > 0 {
  67. return i.IPNet.String()
  68. }
  69. return i.IP.String()
  70. }
  71. // MarshalText implements the encoding.TextMarshaler interface.
  72. // The encoding is the same as returned by String,
  73. // But when len(ip) is zero, will return an empty slice.
  74. func (i *IP) MarshalText() ([]byte, error) {
  75. if len(i.IP) == 0 {
  76. return []byte{}, nil
  77. }
  78. return []byte(i.String()), nil
  79. }
  80. // UnmarshalText implements the encoding.TextUnmarshaler interface.
  81. // The textual bytes are expected in a form accepted by Parse,
  82. // But when len(b) is zero, will return an empty IP.
  83. func (i *IP) UnmarshalText(b []byte) error {
  84. if len(b) == 0 {
  85. *i = IP{}
  86. return nil
  87. }
  88. ip := ParseIP(string(b))
  89. if ip == nil {
  90. return fmt.Errorf("invalid IP address %s", string(b))
  91. }
  92. *i = *ip
  93. return nil
  94. }