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